:root {
  --paper: #f6f1e7; --surface: #fffdf8; --sunk: #efe8da; --ink: #1f2733; --muted: #667085;
  --line: #ddd3c0; --navy: #22324a; --gold: #e0a526; --accent: #22324a; --accent-ink: #fff;
  --good: #2f7d4f; --bad: #b3261e; --radius: 14px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Noto Serif CJK SC", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Hiragino Sans", "Yu Gothic UI",
          "Microsoft YaHei", "PingFang SC", sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --paper: #14181f; --surface: #1c222c; --sunk: #232b37; --ink: #e8e4da; --muted: #9aa4b2;
          --line: #323b49; --navy: #c9d6ea; --gold: #e6b84a; --good: #6cc08b; --bad: #f08b83; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--paper); color: var(--ink); font: 16px/1.5 var(--sans); -webkit-text-size-adjust: 100%; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0; text-wrap: balance; }
h1 { font-size: 1.9rem; } h2 { font-size: 1.25rem; } h3 { font-size: 1.05rem; }
p { margin: 0; }
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.boot { display: grid; place-items: center; height: 100vh; font-family: var(--serif); font-size: 1.4rem; color: var(--muted); }
.muted { color: var(--muted); } .small { font-size: .875rem; } .right { margin-left: auto; }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.stack { display: grid; gap: 14px; }

/* ---------- controls */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 8px 16px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; text-decoration: none;
  font-weight: 550; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--sunk); }
.btn.sm { min-height: 32px; padding: 4px 10px; font-size: .875rem; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
label { display: grid; gap: 5px; font-size: .875rem; font-weight: 550; }
input, select, textarea { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); min-height: 40px; }
textarea { resize: vertical; }
.hint { font-weight: 400; color: var(--muted); font-size: .8125rem; }
.error { color: var(--bad); font-size: .875rem; min-height: 1.3em; }

/* ---------- top bar */
.top { position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.top-in { max-width: 1180px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 12px; }
.brand { display: flex; align-items: center; gap: 9px; font-family: var(--serif); font-weight: 600; font-size: 1.1rem;
  text-decoration: none; white-space: nowrap; }
.brand img { width: 26px; height: 26px; }
.classname { display: flex; align-items: center; gap: 8px; font-weight: 600; min-width: 0; }
.classname span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.flag { display: inline-block; width: 30px; height: 20px; border-radius: 3px; overflow: hidden; flex: none;
  box-shadow: 0 0 0 1px rgba(0,0,0,.15); }
.flag svg { display: block; width: 100%; height: 100%; }
.flag.lg { width: 60px; height: 40px; border-radius: 5px; }
.tabs { max-width: 1180px; margin: 0 auto; padding: 0 16px; display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.tabs a { padding: 9px 13px; text-decoration: none; color: var(--muted); font-weight: 550; border-bottom: 3px solid transparent;
  white-space: nowrap; }
.tabs a:hover { color: var(--ink); }
.tabs a.on { color: var(--ink); border-bottom-color: var(--accent); }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent);
  color: var(--accent-ink); font-size: .75rem; font-weight: 700; text-align: center; line-height: 20px; }
main { max-width: 1180px; margin: 0 auto; padding: 24px 16px 60px; }

/* ---------- cards & stats */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.card > h2, .card > h3 { margin-bottom: 12px; }
.grid { display: grid; gap: 16px; }
.cols-2 { grid-template-columns: repeat(auto-fit, minmax(min(100%, 330px), 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat .k { font-size: .8125rem; color: var(--muted); font-weight: 550; }
.stat .v { font-family: var(--serif); font-size: 1.7rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat .s { font-size: .8125rem; color: var(--muted); }
.bars { display: flex; align-items: flex-end; gap: 5px; height: 90px; }
.bars .b { flex: 1; display: flex; flex-direction: column; gap: 3px; align-items: center; justify-content: flex-end; height: 100%; font-size: .6875rem; color: var(--muted); }
.bars .b i { display: block; flex: none; width: 100%; max-width: 26px; min-height: 3px; border-radius: 4px 4px 0 0; background: var(--line); }
.bars .b i.has { background: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; background: var(--sunk);
  font-size: .875rem; border: 1px solid var(--line); }
.chip b { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0 2px; font-size: 1rem; line-height: 1; }
.list { display: grid; }
.list > * { padding: 10px 0; border-top: 1px solid var(--line); }
.list > *:first-child { border-top: 0; padding-top: 0; }
.empty { color: var(--muted); font-size: .9375rem; }
.pagehead { display: flex; align-items: end; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.eyebrow { font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); font-weight: 650; }

/* ---------- hallway (home) */
.doors { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); gap: 16px; }
.doorcard { --accent: var(--navy); position: relative; display: grid; gap: 10px; padding: 20px 18px 16px; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-top: 6px solid var(--accent);
  border-radius: var(--radius); cursor: pointer; text-decoration: none; transition: transform .12s, box-shadow .12s; }
.doorcard:hover { transform: translateY(-3px); box-shadow: 0 10px 24px rgba(0,0,0,.12); }
.doorcard .native { font-family: var(--serif); font-size: 1.9rem; font-weight: 600; line-height: 1.1; }
.doorcard .meta { display: flex; gap: 8px; flex-wrap: wrap; font-size: .8125rem; color: var(--muted); }
.doorcard .pill { padding: 2px 9px; border-radius: 999px; background: var(--sunk); font-weight: 600; color: var(--ink); }
.doorcard .due { background: var(--accent); color: #fff; }
.doorcard .enter { font-weight: 600; color: var(--accent); margin-top: 2px; }
@media (prefers-color-scheme: dark) { .doorcard .enter { color: var(--ink); } }

/* ---------- entering a class */
.door { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: var(--accent); color: #fff;
  text-align: center; padding: 24px; animation: doorIn .2s ease-out; }
.door.out { animation: doorOut .3s ease-in forwards; }
.door .native { font-family: var(--serif); font-size: clamp(2.2rem, 8vw, 4rem); font-weight: 600; margin: 14px 0 6px; }
.door .greet { font-size: 1.15rem; opacity: .92; }
.door .who { margin-top: 18px; font-size: .875rem; letter-spacing: .08em; text-transform: uppercase; opacity: .8; }
@keyframes doorIn { from { opacity: 0; } }
@keyframes doorOut { to { opacity: 0; transform: scale(1.04); } }
@media (prefers-reduced-motion: reduce) { .door, .door.out, .doorcard { animation-duration: .01s; transition: none; } }

/* ---------- auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: min(100%, 400px); display: grid; gap: 14px; padding: 26px; }
.auth .brand { font-size: 1.4rem; }

/* ---------- chat */
.chat { display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 16px; height: calc(100dvh - 150px); min-height: 420px; }
.convs { display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.convlist { overflow-y: auto; display: grid; gap: 4px; align-content: start; }
.conv { display: grid; gap: 1px; padding: 8px 10px; border-radius: 10px; text-decoration: none; border: 1px solid transparent; }
.conv:hover { background: var(--sunk); }
.conv.on { background: var(--surface); border-color: var(--line); }
.conv .t { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9375rem; }
.conv .m { font-size: .75rem; color: var(--muted); }
.room { display: flex; flex-direction: column; min-height: 0; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden; }
.roomhead { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.modetag { padding: 2px 10px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: .75rem;
  font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
.msgs { flex: 1; overflow-y: auto; padding: 18px 14px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: min(720px, 92%); display: grid; gap: 4px; }
.msg.user { align-self: flex-end; }
.msg .who { font-size: .75rem; color: var(--muted); font-weight: 600; display: flex; gap: 8px; align-items: center; }
.msg.user .who { justify-content: flex-end; }
.msg .body { padding: 10px 14px; border-radius: 14px; background: var(--sunk); overflow-wrap: anywhere; font-size: 1.02rem; line-height: 1.65; }
.msg.user .body { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant .body { border-bottom-left-radius: 4px; }
.msg .body p + p { margin-top: 2px; }
.msg .body .gap { height: .6em; }
.msg .body ul, .msg .body ol { margin: 4px 0; padding-left: 1.3em; }
.msg .body code { background: color-mix(in srgb, var(--ink) 10%, transparent); padding: 1px 5px; border-radius: 5px; font-size: .92em; }
.msg .body table { border-collapse: collapse; margin: 6px 0; font-size: .95em; display: block; overflow-x: auto; }
.msg .body th, .msg .body td { border: 1px solid var(--line); padding: 4px 9px; text-align: left; }
.msg .body th { background: color-mix(in srgb, var(--ink) 6%, transparent); }
.msg.err .body { background: color-mix(in srgb, var(--bad) 12%, var(--surface)); color: var(--bad); }
.say { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: .75rem; font-weight: 600; padding: 0; }
.say:hover { color: var(--accent); }
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; max-height: 160px; font-size: 1.02rem; }
.modes { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); gap: 12px; }
.mode { display: grid; gap: 4px; padding: 16px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; text-align: left; }
.mode:hover, .mode.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.mode b { font-family: var(--serif); font-size: 1.1rem; }
.picker { padding: 22px; overflow-y: auto; display: grid; gap: 18px; align-content: start; }
.scen { cursor: pointer; }
.scen:hover { border-color: var(--accent); }
.convtoggle { display: none; }
@media (max-width: 760px) {
  .chat { grid-template-columns: 1fr; height: calc(100dvh - 140px); }
  .convs { display: none; }
  .chat.showlist .convs { display: flex; }
  .chat.showlist .room { display: none; }
  .convtoggle { display: inline-flex; }
  main { padding: 14px 10px 40px; }
  h1 { font-size: 1.5rem; }
}

/* ---------- lessons */
.lesson { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.lesson .info { flex: 1; min-width: 200px; }
.lesson .lv { font-size: .75rem; font-weight: 700; padding: 2px 8px; border-radius: 6px; background: var(--sunk); white-space: nowrap; }
.status { font-size: .75rem; font-weight: 700; }
.status.completed { color: var(--good); } .status.in_progress { color: var(--gold); }

/* ---------- flashcards */
.flash { max-width: 560px; margin: 0 auto; display: grid; gap: 16px; }
.flashcard { min-height: 260px; display: grid; place-items: center; text-align: center; padding: 28px 20px; }
.flashcard .term { font-family: var(--serif); font-size: clamp(2rem, 9vw, 3.2rem); font-weight: 600; line-height: 1.2; overflow-wrap: anywhere; }
.flashcard .reading { font-size: 1.2rem; color: var(--muted); margin-top: 10px; }
.flashcard .meaning { font-size: 1.3rem; margin-top: 14px; font-weight: 550; }
.flashcard .example { margin-top: 12px; color: var(--muted); }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.grades .btn { flex-direction: column; gap: 0; min-height: 52px; }
.grades small { font-weight: 400; color: var(--muted); font-size: .6875rem; }

/* ---------- tables */
.tablewrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; }
table.data th { text-align: left; font-size: .8125rem; color: var(--muted); font-weight: 600; padding: 6px 10px; border-bottom: 1px solid var(--line); }
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.data td.term { font-size: 1.15rem; font-weight: 600; white-space: nowrap; }
.formgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); gap: 12px; align-items: end; }
.mistake s { color: var(--bad); } .mistake .fix { color: var(--good); font-weight: 600; }
.ok { color: var(--good); font-weight: 600; }

.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 60; background: var(--ink);
  color: var(--paper); padding: 10px 18px; border-radius: 10px; font-size: .9375rem; max-width: 90vw; }
