:root {
  --bg: #000;
  --ink: #f5f5f7;
  --ink-2: rgba(245, 245, 247, .72);
  --ink-3: rgba(245, 245, 247, .5);
  --glass: rgba(22, 22, 28, .78);
  --glass-2: rgba(255, 255, 255, .1);
  --line: rgba(255, 255, 255, .14);
  --a1: #8b5cf6;
  --a2: #22d3ee;
  --a3: #f472b6;
  --grad: linear-gradient(135deg, var(--a1), var(--a2));
  --danger: #ff453a;
  --mark: #ff2d55;
  --sa-t: env(safe-area-inset-top, 0px);
  --sa-b: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); overflow: hidden; }
body {
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", "Segoe UI", sans-serif;
  position: fixed; inset: 0;
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- 카메라 ---------- */
#app { position: fixed; inset: 0; }
#cam, #still {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; background: #000;
}
#still.contain { object-fit: contain; }

#frame { position: absolute; inset: calc(var(--sa-t) + 76px) 22px calc(var(--sa-b) + 170px + var(--ctl-extra, 0px)); pointer-events: none; opacity: .55; transition: opacity .3s; }
#frame i { position: absolute; width: 28px; height: 28px; border: 0 solid rgba(255,255,255,.9); }
#frame i:nth-child(1) { top: 0; left: 0; border-top-width: 3px; border-left-width: 3px; border-top-left-radius: 12px; }
#frame i:nth-child(2) { top: 0; right: 0; border-top-width: 3px; border-right-width: 3px; border-top-right-radius: 12px; }
#frame i:nth-child(3) { bottom: 0; left: 0; border-bottom-width: 3px; border-left-width: 3px; border-bottom-left-radius: 12px; }
#frame i:nth-child(4) { bottom: 0; right: 0; border-bottom-width: 3px; border-right-width: 3px; border-bottom-right-radius: 12px; }
.busy #frame, .answering #frame { opacity: 0; }

/* 인식 순간: 빛줄기가 화면을 훑는다 */
#scan { position: absolute; inset: 0; overflow: hidden; pointer-events: none; background: radial-gradient(ellipse at center, transparent 40%, rgba(0,0,0,.45)); }
#scan .beam {
  position: absolute; left: -10%; right: -10%; height: 34%;
  background: linear-gradient(180deg, transparent, rgba(139,92,246,.0) 20%, rgba(139,92,246,.28) 70%, rgba(34,211,238,.75) 98%, transparent);
  filter: blur(1px);
  animation: sweep 1.8s cubic-bezier(.45,.05,.55,.95) infinite;
}
@keyframes sweep { from { transform: translateY(-40%); } to { transform: translateY(300%); } }

#marker {
  position: absolute; width: 64px; height: 64px; margin: -32px 0 0 -32px; border-radius: 50%;
  border: 3px solid var(--mark); box-shadow: 0 0 0 2px rgba(255,255,255,.85), 0 0 24px rgba(255,45,85,.6);
  pointer-events: none; animation: pop .25s ease-out;
}
@keyframes pop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.cam-msg { position: absolute; inset: 0; display: grid; place-content: center; gap: 14px; text-align: center; padding: 24px; color: var(--ink-2); }
.cam-msg p { margin: 0; white-space: pre-line; }
.cam-msg .pill { justify-self: center; }

/* ---------- 위쪽 ---------- */
.top {
  position: absolute; top: 0; left: 0; right: 0;
  padding: calc(var(--sa-t) + 10px) 16px 10px;
  display: flex; align-items: center; justify-content: space-between;
  background: linear-gradient(180deg, rgba(0,0,0,.55), transparent);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .02em; font-size: 17px; text-shadow: 0 1px 6px rgba(0,0,0,.5); }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--grad); box-shadow: 0 0 12px rgba(34,211,238,.7); }
.dot.big { width: 56px; height: 56px; box-shadow: 0 0 40px rgba(139,92,246,.55); }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: rgba(0,0,0,.35); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); }
.icon-btn.small { width: 32px; height: 32px; background: var(--glass-2); }

.hint {
  position: absolute; left: 16px; right: 16px; bottom: calc(var(--sa-b) + 150px + var(--ctl-extra, 0px)); margin: 0;
  text-align: center; font-size: 13px; color: var(--ink-2); text-shadow: 0 1px 6px rgba(0,0,0,.8);
  transition: opacity .3s;
}
.busy .hint, .answering .hint, .has-q .hint { opacity: 0; }

/* ---------- 아래 조작부 ---------- */
.controls {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 10px 16px calc(var(--sa-b) + 18px);
  background: linear-gradient(0deg, rgba(0,0,0,.6), transparent);
  display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.row { width: 100%; max-width: 420px; display: flex; align-items: center; justify-content: space-between; }
.side-btn { width: 64px; display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 11px; color: var(--ink-2); }
.side-btn svg { width: 46px; height: 46px; padding: 11px; border-radius: 50%; background: rgba(0,0,0,.38); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); color: var(--ink); }

.shoot { position: relative; width: 84px; height: 84px; border-radius: 50%; }
.shoot .ring { position: absolute; inset: 0; border-radius: 50%; border: 4px solid rgba(255,255,255,.92); }
.shoot .core {
  position: absolute; inset: 9px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0 8%, transparent 9%), var(--grad);
  box-shadow: 0 0 26px rgba(139,92,246,.55), inset 0 0 18px rgba(255,255,255,.25);
  animation: breathe 3.2s ease-in-out infinite;
  transition: transform .15s;
}
.shoot:active .core { transform: scale(.88); }
@keyframes breathe { 50% { box-shadow: 0 0 40px rgba(34,211,238,.65), inset 0 0 18px rgba(255,255,255,.25); } }
.busy .shoot .ring {
  border-color: transparent;
  background: conic-gradient(from 0deg, var(--a1), var(--a2), var(--a3), var(--a1)) border-box;
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 4px));
          mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 4px));
  animation: spin 1s linear infinite;
}
.busy .shoot .core { transform: scale(.78); opacity: .7; }
@keyframes spin { to { transform: rotate(360deg); } }

.q-bar { width: 100%; max-width: 420px; display: flex; gap: 8px; }
.q-bar input {
  flex: 1; min-width: 0; height: 44px; padding: 0 16px; border-radius: 22px;
  border: 1px solid var(--line); background: var(--glass); color: var(--ink); font-size: 16px;
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px); outline: none;
  -webkit-user-select: text; user-select: text;
}
.q-chip {
  max-width: min(420px, 100%); display: flex; align-items: center; gap: 6px;
  padding: 7px 8px 7px 14px; border-radius: 18px; font-size: 14px;
  background: rgba(139,92,246,.32); border: 1px solid rgba(139,92,246,.6);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.q-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.q-chip button { width: 24px; height: 24px; border-radius: 50%; background: rgba(255,255,255,.18); line-height: 1; flex: none; }

/* 렌즈·배율 버튼 (기본 카메라 앱처럼) */
.lenses { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.lens-row {
  display: flex; gap: 4px; padding: 4px; border-radius: 22px;
  background: rgba(0,0,0,.4); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.lens-row button { min-width: 40px; height: 34px; padding: 0 10px; border-radius: 17px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.lens-row button.on { background: rgba(255,255,255,.2); color: #fde68a; }
.lens-row.zoom button { min-width: 36px; height: 28px; font-size: 12px; }
.busy .lenses, .answering .lenses { visibility: hidden; }

.pill { height: 36px; padding: 0 14px; border-radius: 18px; background: var(--glass-2); border: 1px solid var(--line); font-size: 14px; white-space: nowrap; }
.q-bar .pill { height: 44px; border-radius: 22px; }
.pill.danger { color: var(--danger); }

/* ---------- 답변 카드 ---------- */
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 5;
  max-height: 72%; display: flex; flex-direction: column;
  background: var(--glass); border-top: 1px solid var(--line);
  border-radius: 24px 24px 0 0;
  backdrop-filter: blur(28px) saturate(1.4); -webkit-backdrop-filter: blur(28px) saturate(1.4);
  box-shadow: 0 -10px 40px rgba(0,0,0,.4);
  transform: translateY(0); transition: transform .32s cubic-bezier(.2,.8,.2,1), max-height .32s;
  animation: rise .36s cubic-bezier(.2,.8,.2,1);
}
.sheet.full { max-height: calc(100% - var(--sa-t) - 12px); }
.sheet.closing { transform: translateY(100%); }
@keyframes rise { from { transform: translateY(100%); } }
.sheet-head { position: relative; display: flex; justify-content: flex-end; padding: 10px 12px 0; min-height: 40px; touch-action: none; }
.grab { position: absolute; left: 50%; top: 8px; width: 40px; height: 5px; margin-left: -20px; border-radius: 3px; background: rgba(255,255,255,.3); }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 20px 16px; -webkit-user-select: text; user-select: text; overscroll-behavior: contain; }
.sheet-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 20px calc(var(--sa-b) + 12px); border-top: 1px solid var(--line); }
.meta { font-size: 12px; color: var(--ink-3); }

.status { display: flex; align-items: center; gap: 10px; margin: 2px 0 10px; font-size: 15px; color: var(--ink-2); }
.orb { width: 14px; height: 14px; border-radius: 50%; background: var(--grad); animation: orb 1.2s ease-in-out infinite; flex: none; }
@keyframes orb { 50% { transform: scale(.6); opacity: .6; } }
.shimmer {
  background: linear-gradient(90deg, var(--ink-3) 0%, var(--ink) 50%, var(--ink-3) 100%);
  background-size: 200% 100%; -webkit-background-clip: text; background-clip: text; color: transparent;
  animation: shimmer 1.6s linear infinite;
}
@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

.thought { margin: -4px 0 12px; font-size: 13px; color: var(--ink-3); font-style: italic; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 12px; }
.chip { font-size: 12px; padding: 4px 10px; border-radius: 12px; background: rgba(34,211,238,.14); border: 1px solid rgba(34,211,238,.35); color: #a5f3fc; }

.answer { font-size: 17px; line-height: 1.62; word-break: keep-all; overflow-wrap: anywhere; }
.answer > :first-child { margin-top: 0; }
.answer p { margin: 0 0 .7em; }
.answer h1, .answer h2, .answer h3, .answer h4 { font-size: 1.02em; margin: 1.1em 0 .4em; color: #c4b5fd; }
.answer strong { color: #fff; font-weight: 700; }
.answer ul, .answer ol { margin: 0 0 .7em; padding-left: 1.3em; }
.answer li { margin: .2em 0; }
.answer code { font: .9em ui-monospace, Menlo, Consolas, monospace; background: rgba(255,255,255,.1); padding: .1em .35em; border-radius: 5px; }
.answer pre { background: rgba(0,0,0,.35); padding: 12px; border-radius: 10px; overflow-x: auto; font-size: 14px; }
.answer pre code { background: none; padding: 0; }
.answer a { color: var(--a2); }
.answer blockquote { margin: 0 0 .7em; padding-left: 12px; border-left: 3px solid var(--a1); color: var(--ink-2); }
.answer hr { border: 0; border-top: 1px solid var(--line); margin: 1em 0; }
.answer .tbl { overflow-x: auto; margin: 0 0 .8em; }
.answer table { border-collapse: collapse; font-size: 15px; }
.answer th, .answer td { border: 1px solid var(--line); padding: 6px 9px; text-align: left; vertical-align: top; }
.answer th { background: rgba(255,255,255,.06); }
.answer.typing > :last-child::after { content: ""; display: inline-block; width: 8px; height: 1em; margin-left: 3px; vertical-align: -2px; border-radius: 2px; background: var(--a2); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.sources { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; padding-top: 12px; border-top: 1px solid var(--line); }
.sources b { font-size: 12px; color: var(--ink-3); font-weight: 600; }
.sources a { font-size: 13px; color: var(--ink-2); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sources a span { color: var(--ink-3); }
.note { margin: 10px 0 0; font-size: 13px; color: #fbbf24; }
.note.err { color: var(--danger); font-size: 15px; }

/* ---------- 로그인 ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: radial-gradient(circle at 50% 30%, #1e1b4b, #000 70%); }
.card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.logo { display: grid; place-items: center; margin-bottom: 4px; }
.card h1 { margin: 0; font-size: 28px; }
.sub { margin: 0 0 12px; color: var(--ink-2); font-size: 14px; }
.card input {
  height: 50px; padding: 0 16px; border-radius: 14px; border: 1px solid var(--line);
  background: rgba(255,255,255,.08); color: var(--ink); font-size: 17px; outline: none;
  -webkit-user-select: text; user-select: text;
}
.card input:focus { border-color: var(--a1); }
.primary { height: 50px; border-radius: 14px; background: var(--grad); color: #fff; font-weight: 700; font-size: 17px; }
.settings-foot .primary { height: 40px; padding: 0 20px; border-radius: 20px; font-size: 15px; }
.err { margin: 0; color: var(--danger); font-size: 14px; }

/* ---------- 설정 ---------- */
.settings {
  width: min(380px, calc(100% - 32px)); border: 1px solid var(--line); border-radius: 20px; padding: 20px;
  background: #16161c; color: var(--ink);
}
.settings::backdrop { background: rgba(0,0,0,.55); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
.settings h2 { margin: 0 0 16px; font-size: 20px; }
.field-label { display: block; font-size: 14px; color: var(--ink-2); margin-bottom: 8px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: rgba(255,255,255,.08); border-radius: 12px; padding: 3px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 8px 0; border-radius: 9px; font-size: 14px; color: var(--ink-2); }
.seg input:checked + span { background: rgba(255,255,255,.18); color: var(--ink); font-weight: 600; }
.help { margin: 8px 0 18px; font-size: 12px; color: var(--ink-3); line-height: 1.5; }
.switch-row { display: flex; align-items: center; justify-content: space-between; font-size: 16px; }
.switch-row input { appearance: none; -webkit-appearance: none; width: 50px; height: 30px; border-radius: 15px; background: rgba(255,255,255,.18); position: relative; transition: background .2s; margin: 0; }
.switch-row input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch-row input:checked { background: #34c759; }
.switch-row input:checked::after { transform: translateX(20px); }
.settings-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.settings { max-height: calc(100dvh - 32px); overflow-y: auto; }
.cam-help { margin: -2px 0 8px; }
.cam-choices { display: flex; flex-direction: column; gap: 2px; margin-bottom: 10px; }
.cam-choice { display: grid; grid-template-columns: auto 1fr; column-gap: 10px; align-items: center; padding: 8px 4px; border-radius: 10px; }
.cam-choice input { grid-row: span 2; width: 18px; height: 18px; accent-color: var(--a1); margin: 0; }
.cam-choice span { font-size: 15px; }
.cam-choice small { font-size: 12px; color: var(--ink-3); }
.cam-info { margin: 0 0 16px; font-size: 13px; color: var(--ink-2); }
.cam-info summary { cursor: pointer; padding: 6px 0; }
.cam-info pre {
  margin: 6px 0 10px; padding: 10px; border-radius: 10px; background: rgba(0,0,0,.35);
  font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-all;
  -webkit-user-select: text; user-select: text;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
