:root {
  --ink: #20211f;
  --muted: #6f6a61;
  --paper: #f6f0e3;
  --paper-deep: #ebe0ca;
  --terracotta: #a53b2b;
  --terracotta-dark: #77261e;
  --cobalt: #1c4d78;
  --olive: #667044;
  --line: rgba(32, 33, 31, .16);
  --shadow: 0 22px 70px rgba(83, 56, 32, .12);
  --radius: 28px;
  font-family: "Avenir Next", Avenir, "Segoe UI", sans-serif;
  color: var(--ink);
  background: #d9cbb4;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: #d9cbb4; }
body {
  margin: 0;
  min-height: 100dvh;
  background:
    linear-gradient(45deg, transparent 46%, rgba(28, 77, 120, .06) 47% 53%, transparent 54%) 0 0 / 34px 34px,
    linear-gradient(-45deg, transparent 46%, rgba(165, 59, 43, .05) 47% 53%, transparent 54%) 0 0 / 34px 34px,
    #e7dcc8;
}

button, input { font: inherit; }
button { color: inherit; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }

.offline-banner {
  position: fixed; inset: 0 0 auto; z-index: 20; padding: .65rem 1rem;
  color: white; background: var(--cobalt); text-align: center; font-size: .82rem; font-weight: 700;
}

.shell { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: max(22px, env(safe-area-inset-top)) 0 max(32px, env(safe-area-inset-bottom)); }
.masthead { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 28px; }
.brand { color: inherit; display: flex; align-items: center; gap: .7rem; text-decoration: none; }
.brand-tile {
  width: 42px; height: 42px; display: grid; place-items: center; border-radius: 11px;
  color: var(--paper); background: var(--terracotta); box-shadow: inset 0 0 0 3px rgba(246,240,227,.32);
  font: 700 1.45rem/1 Georgia, serif;
}
.brand b { display: block; font: 700 1.1rem/1 Georgia, serif; letter-spacing: .02em; }
.brand small { display: block; margin-top: .22rem; color: var(--muted); font-size: .62rem; letter-spacing: .19em; text-transform: uppercase; }
.header-actions { display: flex; gap: .6rem; align-items: center; }
.ghost-button, .profile-chip, .icon-button, .load-more {
  border: 1px solid var(--line); background: rgba(246,240,227,.65); cursor: pointer;
}
.ghost-button { border-radius: 999px; padding: .65rem 1rem; font-size: .78rem; font-weight: 750; }
.profile-chip { border-radius: 999px; padding: .32rem .65rem .32rem .32rem; display: flex; align-items: center; gap: .45rem; font-size: .78rem; font-weight: 750; }
.profile-chip > span:first-child { width: 30px; height: 30px; display: grid; place-items: center; color: white; background: var(--cobalt); border-radius: 50%; }

.paper-card {
  position: relative; overflow: hidden; border: 1px solid rgba(255,255,255,.55); border-radius: var(--radius);
  background:
    radial-gradient(circle at 15% 5%, rgba(255,255,255,.8), transparent 34%),
    repeating-linear-gradient(0deg, rgba(44,32,17,.018) 0 1px, transparent 1px 5px),
    var(--paper);
  box-shadow: var(--shadow);
}

.login-card { max-width: 760px; margin: clamp(40px, 8vh, 100px) auto; padding: clamp(30px, 6vw, 72px); }
.login-card::after { content: ""; position: absolute; width: 210px; height: 210px; right: -90px; top: -90px; border: 28px double rgba(165,59,43,.12); border-radius: 50%; }
.eyebrow { margin: 0 0 .75rem; color: var(--terracotta); font-size: .68rem; font-weight: 800; letter-spacing: .18em; text-transform: uppercase; }
h1, h2 { font-family: Georgia, "Times New Roman", serif; font-weight: 500; }
.login-card h1 { position: relative; margin: 0; max-width: 620px; font-size: clamp(2.55rem, 7vw, 5.35rem); line-height: .98; letter-spacing: -.045em; }
.lede { max-width: 560px; margin: 1.5rem 0 2rem; color: var(--muted); line-height: 1.65; }
fieldset { border: 0; padding: 0; margin: 0 0 1.25rem; }
legend, .field-label { display: block; margin-bottom: .65rem; color: var(--muted); font-size: .72rem; font-weight: 750; letter-spacing: .06em; text-transform: uppercase; }
.learner-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; max-width: 420px; }
.learner-option { position: relative; display: flex; align-items: center; gap: .7rem; padding: .75rem; border: 1px solid var(--line); border-radius: 16px; cursor: pointer; transition: .2s ease; }
.learner-option input { position: absolute; opacity: 0; }
.learner-option:has(input:checked) { border-color: var(--terracotta); background: rgba(165,59,43,.08); box-shadow: inset 0 0 0 1px var(--terracotta); }
.learner-avatar { width: 36px; height: 36px; display: grid; place-items: center; color: white; background: var(--cobalt); border-radius: 12px; font: 700 1rem Georgia, serif; }
.password-row { display: flex; gap: .7rem; max-width: 560px; }
input { min-width: 0; border: 1px solid var(--line); border-radius: 14px; color: var(--ink); background: rgba(255,255,255,.55); outline: none; }
input:focus { border-color: var(--cobalt); box-shadow: 0 0 0 3px rgba(28,77,120,.12); }
.password-row input { flex: 1; padding: .9rem 1rem; }
.primary-button { border: 0; border-radius: 14px; padding: .9rem 1.4rem; color: white; background: var(--terracotta); font-weight: 780; cursor: pointer; box-shadow: 0 8px 24px rgba(165,59,43,.22); }
.primary-button:hover { background: var(--terracotta-dark); transform: translateY(-1px); }
.form-error { min-height: 1.25rem; color: var(--terracotta-dark); font-size: .8rem; }
.privacy-note { margin-top: 1.6rem; padding-top: 1rem; border-top: 1px solid var(--line); color: var(--muted); font-size: .73rem; }
.privacy-note span { color: var(--olive); }

.section-tabs { display: flex; gap: .2rem; width: fit-content; margin: 0 0 18px; padding: 4px; border: 1px solid var(--line); border-radius: 999px; background: rgba(246,240,227,.55); }
.tab { border: 0; border-radius: 999px; padding: .6rem 1rem; color: var(--muted); background: transparent; cursor: pointer; font-size: .78rem; font-weight: 750; }
.tab.is-active { color: white; background: var(--ink); }
.lesson-layout { display: grid; grid-template-columns: minmax(0, 1.22fr) minmax(320px, .78fr); gap: 18px; min-height: calc(100dvh - 150px); }
.lesson-stage, .conversation-card { min-height: 620px; }
.lesson-stage { padding: clamp(24px, 4vw, 48px); display: flex; flex-direction: column; }
.lesson-heading, .panel-heading, .history-heading { display: flex; justify-content: space-between; gap: 1rem; align-items: flex-start; }
.lesson-heading h1, .history-heading h1 { margin: 0; font-size: clamp(2.1rem, 5vw, 4.2rem); line-height: 1; letter-spacing: -.04em; }
.engine-badge { border: 1px solid var(--line); border-radius: 999px; padding: .48rem .7rem; color: var(--olive); font-size: .65rem; font-weight: 800; letter-spacing: .07em; text-transform: uppercase; }
.scenario-strip { display: flex; align-items: center; gap: .8rem; margin-top: 1.7rem; padding: .8rem 1rem; border: 1px solid var(--line); border-radius: 18px; background: rgba(255,255,255,.35); }
.scenario-number { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 12px; color: white; background: var(--cobalt); font: 700 .85rem Georgia, serif; }
.scenario-strip small, .scenario-strip b { display: block; }
.scenario-strip small { color: var(--muted); font-size: .62rem; text-transform: uppercase; letter-spacing: .08em; }
.scenario-strip b { margin-top: .17rem; font: 500 1rem Georgia, serif; }
.voice-stage { flex: 1; display: grid; place-items: center; align-content: center; text-align: center; }
#waveform { display: block; width: min(100%, 560px); height: 90px; opacity: .8; }
.talk-button { position: relative; width: 154px; height: 154px; margin: .5rem 0 1.2rem; border: 0; border-radius: 50%; color: var(--paper); background: var(--terracotta); cursor: pointer; box-shadow: 0 18px 52px rgba(165,59,43,.34); transition: transform .22s ease; }
.talk-button:hover { transform: scale(1.025); }
.talk-button:disabled { cursor: wait; opacity: .62; }
.talk-button svg { width: 35px; fill: currentColor; }
.talk-button .stop-icon { display: none; }
.talk-button.is-live .mic-icon { display: none; }
.talk-button.is-live .stop-icon { display: inline; }
.talk-button #talk-label { display: block; margin-top: .35rem; font-size: .72rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.talk-rings { position: absolute; inset: -12px; border: 1px solid rgba(165,59,43,.36); border-radius: inherit; }
.is-live .talk-rings { animation: ring 1.8s ease-out infinite; }
@keyframes ring { 0% { transform: scale(.9); opacity: 1; } 100% { transform: scale(1.28); opacity: 0; } }
.connection-status { display: flex; align-items: center; justify-content: center; gap: .5rem; font-size: .8rem; font-weight: 700; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.connection-status[data-state="connected"] .status-dot { background: var(--olive); }
.connection-status[data-state="listening"] .status-dot { background: var(--cobalt); box-shadow: 0 0 0 5px rgba(28,77,120,.1); }
.connection-status[data-state="speaking"] .status-dot { background: var(--terracotta); box-shadow: 0 0 0 5px rgba(165,59,43,.1); }
.voice-stage > p { color: var(--muted); font-size: .72rem; }

.conversation-card { padding: 28px 24px 18px; display: flex; flex-direction: column; }
.panel-heading h2 { margin: 0; font-size: 2rem; }
.icon-button { width: 36px; height: 36px; border-radius: 50%; cursor: pointer; }
.transcript { flex: 1; min-height: 360px; max-height: calc(100dvh - 300px); overflow-y: auto; padding: 1.2rem .2rem; display: flex; flex-direction: column; gap: .8rem; scrollbar-width: thin; }
.empty-state { margin: auto; color: var(--muted); text-align: center; }
.empty-state span { font: italic 2rem Georgia, serif; color: var(--terracotta); }
.empty-state p { font-size: .78rem; }
.message { max-width: 88%; padding: .75rem .9rem; border-radius: 17px; font-size: .86rem; line-height: 1.5; animation: message-in .25s ease both; }
.message small { display: block; margin-bottom: .25rem; font-size: .59rem; font-weight: 850; letter-spacing: .1em; text-transform: uppercase; }
.message.sofia { align-self: flex-start; border: 1px solid var(--line); background: rgba(255,255,255,.5); }
.message.sofia small { color: var(--terracotta); }
.message.user { align-self: flex-end; color: white; background: var(--cobalt); }
.message.user small { color: rgba(255,255,255,.68); }
@keyframes message-in { from { opacity: 0; transform: translateY(7px); } }
.text-fallback { display: flex; gap: .5rem; padding-top: .75rem; border-top: 1px solid var(--line); }
.text-fallback input { flex: 1; padding: .72rem .85rem; font-size: .8rem; }
.text-fallback button { width: 40px; height: 40px; border: 0; border-radius: 12px; color: white; background: var(--ink); cursor: pointer; }
.text-fallback button:disabled { opacity: .35; }

.history-view { padding: clamp(8px, 2vw, 24px) 0; }
.history-heading { align-items: center; margin: 1.2rem 0 2rem; }
.progress-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 22px; }
.progress-stat, .progress-insight, .progress-errors { padding: 20px; border: 1px solid rgba(255,255,255,.55); border-radius: 20px; background: rgba(255,255,255,.68); box-shadow: 0 10px 28px rgba(83,56,32,.06); }
.progress-stat small, .progress-insight small, .progress-errors > small { display: block; color: var(--muted); font-size: .65rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.progress-stat strong { display: block; margin-top: .35rem; color: var(--ink); font-size: clamp(1.8rem, 4vw, 3rem); }
.progress-insight { grid-column: span 2; display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.progress-insight strong { display: block; margin-top: .55rem; line-height: 1.35; }
.progress-errors ul { display: flex; flex-wrap: wrap; gap: 8px; margin: .8rem 0 0; padding: 0; list-style: none; }
.progress-errors li { padding: 7px 10px; border-radius: 999px; background: var(--cream); color: var(--ink); font-size: .72rem; }
.history-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.history-card { position: relative; min-height: 230px; padding: 22px; border: 1px solid rgba(255,255,255,.55); border-radius: 22px; background: var(--paper); box-shadow: 0 12px 35px rgba(83,56,32,.09); cursor: pointer; text-align: left; }
.history-card:hover { transform: translateY(-2px); }
.history-card time { color: var(--terracotta); font-size: .65rem; font-weight: 850; letter-spacing: .1em; text-transform: uppercase; }
.history-card h2 { margin: .8rem 0; font-size: 1.45rem; }
.history-card p { color: var(--muted); font-size: .78rem; line-height: 1.55; }
.history-meta { position: absolute; inset: auto 22px 18px; display: flex; justify-content: space-between; border-top: 1px solid var(--line); padding-top: .7rem; font-size: .67rem; font-weight: 750; }
.load-more { display: block; margin: 1.5rem auto; border-radius: 999px; padding: .8rem 1.2rem; }

dialog { width: min(760px, calc(100% - 24px)); max-height: 88dvh; padding: clamp(22px, 5vw, 48px); border: 0; border-radius: 28px; color: var(--ink); background: var(--paper); box-shadow: 0 35px 100px rgba(30,23,16,.35); }
dialog::backdrop { background: rgba(28,31,31,.58); backdrop-filter: blur(6px); }
.dialog-close { position: absolute; right: 18px; top: 16px; border: 0; background: transparent; font-size: 2rem; cursor: pointer; }
.detail-turn { margin: .7rem 0; padding: .7rem .9rem; border-left: 3px solid var(--line); font-size: .84rem; line-height: 1.55; }
.detail-turn.user { border-color: var(--cobalt); }
.correction-list { padding-left: 1.2rem; }
.correction-list li { margin: .7rem 0; line-height: 1.5; }

button:focus-visible, input:focus-visible { outline: 3px solid rgba(28,77,120,.35); outline-offset: 3px; }

@media (max-width: 900px) {
  .lesson-layout { grid-template-columns: 1fr; }
  .lesson-stage { min-height: 600px; }
  .conversation-card { min-height: 520px; }
  .transcript { max-height: 520px; }
  .history-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .progress-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .progress-insight, .progress-errors { grid-column: span 2; }
}

@media (max-width: 560px) {
  .shell { width: min(100% - 18px, 1180px); }
  .masthead { margin-bottom: 16px; }
  .ghost-button { padding: .55rem .75rem; }
  .login-card { margin: 16px auto; padding: 28px 20px; border-radius: 22px; }
  .login-card h1 { font-size: 2.65rem; }
  .learner-grid, .password-row { grid-template-columns: 1fr; }
  .password-row { flex-direction: column; }
  .section-tabs { position: sticky; top: max(8px, env(safe-area-inset-top)); z-index: 5; backdrop-filter: blur(12px); }
  .lesson-stage, .conversation-card { border-radius: 22px; }
  .lesson-stage { min-height: calc(100dvh - 150px); padding: 24px 18px; }
  .lesson-heading { align-items: flex-start; }
  .lesson-heading h1 { font-size: 2.55rem; }
  .engine-badge { font-size: .55rem; }
  .talk-button { width: 140px; height: 140px; }
  .conversation-card { padding: 24px 18px 14px; }
  .history-grid { grid-template-columns: 1fr; }
  .progress-grid { grid-template-columns: 1fr; }
  .progress-insight, .progress-errors { grid-column: auto; }
  .progress-insight { grid-template-columns: 1fr; }
  .history-card { min-height: 205px; }
}

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

.context-panel { margin: 1rem 0; padding: .8rem 1rem; background: var(--paper, #fffaf1); border: 1px solid #dbcdbf; border-radius: 12px; }
.context-panel summary { cursor: pointer; font-weight: 600; }
.context-panel p, .context-panel li { font-size: .85rem; line-height: 1.5; }
.context-panel input { max-width: 100%; margin-bottom: .7rem; }
.engine-badge select { max-width: 145px; border: 0; background: transparent; font: inherit; color: inherit; }
