:root {
  --bg: #f6f7fb; --card: #ffffff; --text: #1d2433; --muted: #6b7385;
  --accent: #3b6cf6; --accent-text: #fff; --tutor: #eef2ff; --student: #e7f7ee; --border: #dde1ea;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #12151c; --card: #1b1f29; --text: #e6e9f0; --muted: #9aa3b5;
    --accent: #6d8fff; --tutor: #232a3d; --student: #1f3329; --border: #2d3342; }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; font-family: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  background: var(--bg); color: var(--text); }
header { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--card); border-bottom: 1px solid var(--border); }
h1 { font-size: 20px; margin: 0; } h1 small { color: var(--muted); font-weight: 500; font-size: 14px; }
.controls { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; font-size: 14px; }
.badge { font-size: 12px; color: var(--muted); border: 1px solid var(--border); border-radius: 99px; padding: 2px 8px; }
/* 💳 크레딧 표시 */
a.badge.credit { text-decoration: none; font-variant-numeric: tabular-nums; white-space: nowrap; }
a.badge.credit.low { color: #b54708; border-color: #f5c27a; }
a.badge.credit.out { color: #8a1c1c; border-color: #f0a8a8; background: #fde8e8; }
.msg.error.limit { max-width: 92%; text-align: center; line-height: 1.5; }
.msg.error.limit a { color: inherit; font-weight: 700; }
main { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 16px; padding: 16px;
  max-width: 1300px; margin: 0 auto; height: calc(100vh - 62px); }
@media (max-width: 860px) { main { grid-template-columns: minmax(0, 1fr); height: auto; } }
section { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 14px;
  display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.viewport { position: relative; background: #000; border-radius: 10px; overflow: hidden; aspect-ratio: 4/3; }
video, #snapshot { width: 100%; height: 100%; object-fit: contain; display: block; }
.cam-msg { position: absolute; inset: 0; display: grid; place-items: center; color: #ccd; font-size: 14px; text-align: center; padding: 20px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
button, select, .file-btn { font: inherit; font-size: 14px; padding: 8px 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--card); color: var(--text); cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button:disabled { opacity: .45; cursor: not-allowed; }
.note { font-size: 12px; color: var(--muted); margin: 0; }
.problem { background: var(--bg); border-radius: 10px; padding: 10px 12px; border: 1px dashed var(--border); }
.problem .label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; min-height: 240px; }
.msg { max-width: 88%; padding: 10px 12px; border-radius: 12px; line-height: 1.55; white-space: pre-wrap; }
.msg.tutor { background: var(--tutor); align-self: flex-start; border-top-left-radius: 4px; }
.msg.student { background: var(--student); align-self: flex-end; border-top-right-radius: 4px; }
.msg.error { background: #fde8e8; color: #8a1c1c; align-self: center; font-size: 13px; }
.msg.thinking { color: var(--muted); font-style: italic; }
.quick { display: flex; gap: 8px; flex-wrap: wrap; }
.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; min-width: 0; font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); }
#micBtn.on { background: #e5484d; color: #fff; border-color: #e5484d; }
.toggle { display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }
.toggle.small { font-size: 13px; color: var(--muted); }

/* ---------- 학습 기록 ---------- */
#account { display: inline-flex; gap: 8px; align-items: center; }
dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--card); color: var(--text);
  width: min(480px, calc(100vw - 32px)); max-height: calc(100vh - 40px); padding: 16px; }
dialog.wide { width: min(760px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; font-size: 17px; }
.close { padding: 4px 10px; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tabs button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.stack { display: flex; flex-direction: column; gap: 10px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.stack label.toggle { flex-direction: row; }
.stack input:not([type=checkbox]), .stack select { font: inherit; padding: 8px 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); }
.consent { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
  font-size: 13px; line-height: 1.55; display: flex; flex-direction: column; gap: 8px; }
.consent p, .consent ul { margin: 0; } .consent ul { padding-left: 18px; }
.err { color: #c03a3a; font-size: 14px; margin: 8px 0 0; }
.cards { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.card { border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.card .meta { display: flex; gap: 6px; flex-wrap: wrap; font-size: 12px; color: var(--muted); align-items: center; }
.chip { border-radius: 99px; padding: 1px 8px; border: 1px solid var(--border); font-size: 12px; }
.chip.ok { background: #e3f6e9; color: #1d6b3a; border-color: transparent; }
.chip.todo { background: #fff1db; color: #8a5300; border-color: transparent; }
.card details { font-size: 14px; } .card details > div { white-space: pre-wrap; margin-top: 6px; line-height: 1.55; }
.empty { color: var(--muted); font-size: 14px; padding: 16px 0; text-align: center; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 180px) 1fr 92px; gap: 8px; align-items: center; font-size: 14px; margin: 6px 0; }
.bar { height: 12px; background: var(--bg); border-radius: 99px; overflow: hidden; border: 1px solid var(--border); }
.bar > i { display: block; height: 100%; background: var(--accent); }
.bar-row.weak .bar > i { background: #e5484d; } .bar-row.weak b { color: #e5484d; }
.weak-card { border-left: 4px solid #e5484d; }
.danger { border: 1px solid #e5484d55; border-radius: 10px; padding: 10px 12px; }
button.warn { background: #e5484d; color: #fff; border-color: #e5484d; }
.btnlike { font-size: 14px; padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border); color: var(--text); text-decoration: none; text-align: center; }
.msg.saved { align-self: center; font-size: 12px; color: var(--muted); background: transparent; padding: 2px; }
@media (prefers-color-scheme: dark) {
  .chip.ok { background: #1f3a29; color: #8fe0aa; } .chip.todo { background: #3a2e17; color: #f2c47a; }
}

/* ---------- 과목 ---------- */
.subject-tabs { display: flex; gap: 4px; background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 3px; }
.subject-tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 9px; }
.subject-tabs button.on { background: var(--accent); color: var(--accent-text); font-weight: 600; }
#problemText { white-space: pre-wrap; line-height: 1.6; max-height: 32vh; overflow-y: auto; }
.rec-filter { margin-bottom: 8px; font-size: 14px; }
.rec-filter label { display: inline-flex; gap: 6px; align-items: center; }
.subj-head { margin: 16px 0 4px; font-size: 15px; }
.subj-head:first-of-type { margin-top: 4px; }
.ptext { white-space: pre-wrap; line-height: 1.55; max-height: 220px; overflow-y: auto; }
.ptext.short { max-height: 4.8em; overflow: hidden; flex: 1; min-width: 0; }
@media (max-width: 860px) { .subject-tabs { order: 3; width: 100%; justify-content: space-between; } }

/* ---------- 폰 연결 (QR) ---------- */
.phone-chip { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; border: 1px solid var(--border);
  border-radius: 99px; padding: 3px 4px 3px 10px; background: var(--bg); }
button.mini { padding: 3px 10px; font-size: 12px; border-radius: 99px; }
.pair { display: flex; flex-direction: column; gap: 8px; }
.qr { align-self: center; background: #fff; padding: 10px; border-radius: 12px; width: min(260px, 70vw); }
.qr svg { display: block; width: 100%; height: auto; }
.qr:empty { display: none; }
.pair-steps { margin: 0; padding-left: 20px; line-height: 1.6; font-size: 14px; }
.pair-warn { margin: 0; font-size: 13px; line-height: 1.5; background: #fff1db; color: #6b4100; border-radius: 10px; padding: 8px 10px; }
#pairUrl { word-break: break-all; color: var(--accent); }
video.mirror { transform: scaleX(-1); }
.viewport.drop { outline: 3px dashed var(--accent); outline-offset: -6px; }
@media (prefers-color-scheme: dark) { .pair-warn { background: #3a2e17; color: #f2c47a; } }

/* ---------- 폰·태블릿 (반응형) ---------- */
body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.camera .row { gap: 8px; }
/* 손가락으로 누르는 기기: 버튼을 크게, 입력 글자는 16px(아이폰 자동 확대 방지) */
@media (pointer: coarse) {
  button, select, .file-btn { min-height: 44px; font-size: 16px; padding: 10px 16px; }
  .subject-tabs button { min-height: 42px; }
  button.mini { min-height: 34px; font-size: 13px; }
  .input-row input, .stack input:not([type=checkbox]), .stack select { font-size: 16px; min-height: 44px; }
  .toggle input[type=checkbox] { width: 22px; height: 22px; }
  .toggle.small { font-size: 15px; min-height: 40px; }
  #micBtn { min-width: 52px; }
}
/* 태블릿 세로, 폰: 한 줄로 쌓기. 카메라가 화면을 너무 차지하지 않게 */
@media (max-width: 860px) {
  header { padding: 10px 14px; gap: 8px; }
  main { padding: 10px; gap: 10px; }
  .viewport { aspect-ratio: auto; height: min(56vw, 42vh); }
  .camera .row button.big { flex: 1 1 180px; font-size: 17px; }
  .log { min-height: 34vh; max-height: 60vh; }
  .chat { padding-bottom: 10px; }
  .chat .input-row { position: sticky; bottom: env(safe-area-inset-bottom); background: var(--card); padding: 6px 0; z-index: 2; }
}
/* 폰 세로 */
@media (max-width: 600px) {
  h1 { font-size: 18px; }
  .controls { width: 100%; gap: 8px; justify-content: space-between; }
  .controls .badge { order: 9; }
  .subject-tabs button { flex: 1; padding: 6px 4px; }
  main { padding: 8px; }
  section { padding: 10px; border-radius: 12px; }
  .viewport { height: 62vw; }
  #phoneBtn, #phoneChip { display: none !important; }  /* 폰에서는 폰 카메라를 바로 쓰면 됨 */
  .msg { max-width: 94%; }
  dialog, dialog.wide { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; margin: 0; border-radius: 0; border: 0; }
  .bar-row { grid-template-columns: minmax(70px, 120px) 1fr 76px; font-size: 13px; }
}
/* 가로로 눕힌 폰·작은 태블릿: 카메라 | 대화 두 칸 */
@media (orientation: landscape) and (max-height: 560px) {
  main { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); height: auto; }
  header { padding: 6px 12px; } h1 { font-size: 16px; }
  .viewport { height: 58vh; aspect-ratio: auto; }
  .log { min-height: 40vh; max-height: 70vh; }
  .subject-tabs { order: 0; width: auto; }
}
/* 태블릿 가로 (아이패드 등): 기존 두 칸 그대로, 높이만 화면에 맞춤 */
@media (min-width: 861px) and (max-width: 1366px) and (pointer: coarse) {
  main { height: calc(100dvh - 70px); }
}

/* ---------- 폰 카메라 화면 (/m) ---------- */
.phone-page { min-height: 100dvh; }
.phone-head { justify-content: space-between; }
.phone-main { display: flex; flex-direction: column; gap: 12px; padding: 12px; max-width: 640px; margin: 0 auto; height: auto; }
.phone-view { aspect-ratio: 3/4; height: auto; max-height: 62dvh; }
.phone-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.huge { font-size: 19px !important; min-height: 58px; padding: 14px 18px; border-radius: 14px; width: 100%; text-align: center; display: block; }
.file-btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.phone-sub { justify-content: center; }
.phone-sub > * { flex: 1; text-align: center; }
.phone-card { background: var(--card); border: 1px solid var(--border); border-radius: 14px; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.phone-card p { margin: 0; line-height: 1.6; }
.big-ok { font-size: 22px; font-weight: 700; }
.phone-note { text-align: center; }
.busy { opacity: .5; pointer-events: none; }
@media (orientation: landscape) and (max-height: 560px) {
  .phone-main { max-width: none; display: grid; grid-template-columns: 1fr; }
  #camArea:not([hidden]) { display: grid; grid-template-columns: 1.3fr 1fr; gap: 12px; align-items: start; }
  .phone-view { aspect-ratio: 4/3; max-height: 78dvh; }
  .phone-actions { margin-top: 0; }
}
.nolive .phone-view { aspect-ratio: auto; height: 26dvh; }
@media (orientation: landscape) and (max-height: 560px) {
  header .badge, #installBtn { display: none !important; }
  header .controls { gap: 8px; }
}

/* ---------- 폰 화면의 대화 (PC와 같은 대화) ---------- */
.phone-chat { display: flex; flex-direction: column; gap: 10px; }
.phone-chat[hidden] { display: none; }
.phone-chat .log { min-height: 34dvh; max-height: 56dvh; }
.phone-chat .quick button { flex: 1; }
.phone-chat .msg { max-width: 94%; }

/* 🗣️ 말로 대화 */
.voice-btn.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.voice-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px 10px; border: 1px solid var(--accent);
  border-radius: 10px; background: var(--tutor); font-size: 14px; }
.voice-bar .toggle { margin-left: auto; }
.voice-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; }
.voice-bar[data-state=listening] .voice-dot { background: #22a35a; animation: vpulse 1.6s infinite; }
.voice-bar[data-state=hearing] .voice-dot { background: #22a35a; transform: scale(1.3); }
.voice-bar[data-state=thinking] .voice-dot { background: #e0a106; }
.voice-bar[data-state=speaking] .voice-dot { background: var(--accent); animation: vpulse .8s infinite; }
.voice-interim { color: var(--muted); font-style: italic; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1 0 100%; order: 9; }
.voice-interim:empty { display: none; }
@keyframes vpulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .voice-dot { animation: none !important; } }

.voice-engine { font-size: 12px; color: var(--muted); border: 1px solid var(--line, #ccd); border-radius: 999px; padding: 1px 8px; }
.voice-engine:empty { display: none; }
.work-bar { border-color: #22a35a; }
.work-bar[data-state=watching] .voice-dot { background: #22a35a; animation: vpulse 1.6s infinite; }
.work-bar[data-state=sending] .voice-dot { background: #e0a106; }
.work-bar[data-state=wrong] .voice-dot { background: #d64545; }
.work-bar .work-note { font-style: normal; font-size: 12px; white-space: normal; }
.viewport.watching::after { content: "✍️ 풀이 보는 중"; position: absolute; top: 8px; left: 8px; background: rgba(34,163,90,.9); color: #fff;
  font-size: 13px; padding: 2px 8px; border-radius: 999px; }
/* 과목 6개: 좁은 화면에서는 3개씩 두 줄로 (글자가 쪼개지지 않게) */
.subject-tabs button { white-space: nowrap; }
@media (max-width: 560px) {
  .subject-tabs { display: grid; grid-template-columns: repeat(3, 1fr); }
  .subject-tabs button { padding: 6px 4px; }
}

/* ---------- 📖 단계 카드 · 형광 하이라이트 · 사진 위 박스 ---------- */
mark.hl { background: #fff176; color: #1a1a1a; border-radius: 3px; padding: 0 2px; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
mark.hl.bad { background: #ffcdd2; }
.msg.steps-card { max-width: 96%; white-space: normal; display: flex; flex-direction: column; gap: 8px; }
.steps-head { display: flex; gap: 6px; align-items: baseline; } .steps-count { color: var(--muted); font-size: 13px; }
.steps-opts { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; }
.steps { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; counter-reset: step; }
.step { background: var(--card); border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: 10px; padding: 8px 10px;
  display: flex; flex-direction: column; gap: 5px; counter-increment: step; animation: stepIn .25s ease-out; }
.step.focus { box-shadow: 0 0 0 2px #fbc02d inset; }
.step-title { font-weight: 700; font-size: 14px; } .step-title::before { content: counter(step) ". "; color: var(--accent); }
.step-ask { font-size: 14px; color: #7a4f00; background: #fff8e1; border-radius: 8px; padding: 6px 8px; }
.step-body { font-size: 16px; line-height: 1.7; overflow-x: auto; }
.step-why { font-size: 13px; color: var(--muted); white-space: pre-wrap; }
.step-quote { font-size: 13px; }
.step-fill input { flex: 1; min-width: 120px; font: inherit; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--card); color: var(--text); }
.step-result { font-size: 13px; color: var(--muted); } .step-result.ok { color: #1b8a4b; font-weight: 600; }
.blank { display: inline-block; min-width: 2.2em; text-align: center; border-bottom: 2px solid var(--accent); color: var(--accent); font-weight: 700; }
.steps-final { border-top: 1px dashed var(--border); padding-top: 6px; display: flex; flex-direction: column; gap: 4px; }
.steps-answer { font-weight: 700; }
.step.wrong { border-left-color: #e53935; } .step .badge-wrong { color: #c62828; font-size: 12px; font-weight: 700; }
.msg.work-line { font-size: 14px; color: var(--text); }
@keyframes stepIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.hl-layer { position: absolute; inset: 0; pointer-events: none; }
.hl-box { position: absolute; border: 3px solid #fbc02d; background: rgba(255, 241, 118, .28); border-radius: 6px;
  box-shadow: 0 0 0 2px rgba(0,0,0,.25); animation: boxIn .3s ease-out; }
.hl-box.bad { border-color: #e53935; background: rgba(229, 57, 53, .18); }
.hl-box span { position: absolute; top: -22px; left: -3px; background: #fbc02d; color: #1a1a1a; font-size: 12px; font-weight: 700; padding: 1px 6px; border-radius: 6px 6px 6px 0; white-space: nowrap; }
.hl-box.bad span { background: #e53935; color: #fff; }
@keyframes boxIn { from { opacity: 0; transform: scale(1.08); } to { opacity: 1; transform: none; } }
@media (prefers-color-scheme: dark) {
  .step-ask { background: #3a3220; color: #f5d48a; }
}
.hl-box.in span { top: 2px; left: 2px; border-radius: 6px; }
.note-steps { white-space: normal !important; display: flex; flex-direction: column; gap: 6px; }
.note-work { font-size: 13px; }
