:root {
  --navy: #0a3d62;
  --navy-2: #0e4c7a;
  --gold: #c99a2e;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --text: #16202a;
  --muted: #5d6b79;
  --line: #e3e7ec;
  --warn-bg: #fff4e0;
  --warn-fg: #8a5300;
  --warn-line: #f2c97a;
  --info-bg: #e8f1fb;
  --info-fg: #164f86;
  --good-bg: #e7f6ec;
  --good-fg: #1d6b3a;
  --shadow: 0 1px 2px rgba(16, 32, 48, .06), 0 4px 16px rgba(16, 32, 48, .06);
  --radius: 16px;
  --tabbar-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1318;
    --surface: #162029;
    --surface-2: #1c2833;
    --text: #e8edf2;
    --muted: #93a2b1;
    --line: #26343f;
    --warn-bg: #3a2a0c;
    --warn-fg: #ffd08a;
    --warn-line: #6b4d14;
    --info-bg: #13283d;
    --info-fg: #9cc8f5;
    --good-bg: #12301f;
    --good-fg: #8fdcaa;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overscroll-behavior-y: contain;
}
a { color: var(--info-fg); }
button { font: inherit; color: inherit; }
input, textarea, select { font: inherit; font-size: 16px; color: var(--text); }

/* ---------- Header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--navy);
  color: #fff;
  padding: calc(10px + var(--safe-t)) 16px 12px;
  display: flex; align-items: center; gap: 12px;
}
.topbar .crest {
  width: 36px; height: 36px; border-radius: 10px; flex: none;
  background: var(--gold); color: var(--navy); font-weight: 800; display: grid; place-items: center; font-size: 15px;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 700; letter-spacing: .1px; }
.topbar .sub { font-size: 13px; opacity: .8; }
.topbar .spacer { flex: 1; }
.icon-btn {
  border: 0; background: transparent; width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center; cursor: pointer;
}
.topbar .icon-btn { color: #fff; }
.icon-btn:active { background: rgba(127, 127, 127, .18); }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- Views ---------- */
main { padding: 12px 12px calc(var(--tabbar-h) + var(--safe-b) + 20px); max-width: 720px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 12px; overflow: hidden; border: 1px solid var(--line);
}
.card-head { display: flex; align-items: center; gap: 8px; padding: 12px 8px 4px 16px; }
.card-head h2 { margin: 0; font-size: 17px; font-weight: 700; }
.card-head .subtitle { font-size: 13px; color: var(--muted); }
.card-head .titles { flex: 1; min-width: 0; }
.card-head .icon-btn { width: 36px; height: 36px; color: var(--muted); }
.card-body { padding: 4px 16px 14px; }
.card.collapsed .card-body { display: none; }
.card .spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.card-foot { font-size: 12px; color: var(--muted); padding: 0 16px 12px; }

.alert { border-radius: 12px; padding: 10px 12px; margin: 8px 0; font-size: 15px; display: flex; gap: 10px; align-items: flex-start; }
.alert .dot { flex: none; font-size: 16px; line-height: 1.3; }
.alert.warn { background: var(--warn-bg); color: var(--warn-fg); border: 1px solid var(--warn-line); font-weight: 600; }
.alert.info { background: var(--info-bg); color: var(--info-fg); }
.alert.good { background: var(--good-bg); color: var(--good-fg); }

.lessons { list-style: none; padding: 0; margin: 6px 0 0; }
.lessons li { display: grid; grid-template-columns: 52px 1fr auto; gap: 10px; align-items: center; padding: 9px 0; border-top: 1px solid var(--line); }
.lessons li:first-child { border-top: 0; }
.lessons .time { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 14px; }
.lessons .subj { font-weight: 600; }
.lessons .who { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lessons li.kit .subj::after { content: " · PE kit"; color: var(--warn-fg); font-weight: 700; font-size: 13px; }
.lessons .now { border-left: 3px solid var(--gold); padding-left: 8px; margin-left: -11px; }

.list-h { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin: 14px 0 4px; }
.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: 9px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list .t { font-weight: 600; font-size: 15px; }
.list li.warn .t { color: var(--warn-fg); }
.list .s { font-size: 13px; color: var(--muted); margin-top: 2px; }
.badge { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; padding: 2px 7px; border-radius: 99px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); margin-right: 6px; vertical-align: 1px; }
.date-chip { float: right; font-size: 12px; color: var(--muted); margin-left: 8px; }
.empty { color: var(--muted); font-size: 15px; padding: 6px 0; }

.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-left: 20px; }
.md li { margin: 4px 0; }
.md h3, .md h4 { margin: 12px 0 4px; font-size: 16px; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 6px; font-size: 14px; }
.md table { border-collapse: collapse; font-size: 14px; display: block; overflow-x: auto; }
.md td, .md th { border: 1px solid var(--line); padding: 4px 8px; }

.add-card {
  width: 100%; border: 2px dashed var(--line); background: transparent; color: var(--muted);
  border-radius: var(--radius); padding: 16px; font-weight: 600; cursor: pointer; margin-bottom: 12px;
}

/* ---------- Chat ---------- */
#view-ask.active { display: flex; flex-direction: column; min-height: calc(100dvh - 140px); }
.chat-log { flex: 1; }
.msg { margin: 10px 0; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { min-width: 0; max-width: 88%; padding: 10px 14px; border-radius: 18px; background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--navy); color: #fff; border-color: var(--navy); border-bottom-right-radius: 6px; }
.msg.bot .bubble { border-bottom-left-radius: 6px; }
.status-line { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; margin-top: 4px; }
.status-line::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--gold); animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { from { opacity: .3 } to { opacity: 1 } }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 8px 14px; font-size: 14px; cursor: pointer; white-space: nowrap; }
.chip:active { background: var(--surface-2); }
.welcome { text-align: center; color: var(--muted); padding: 28px 12px 8px; }
.welcome b { color: var(--text); font-size: 18px; display: block; margin-bottom: 4px; }

.composer {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + var(--safe-b)); z-index: 15;
  background: linear-gradient(to top, var(--bg) 70%, transparent); padding: 10px 12px 8px;
}
.composer-inner { max-width: 720px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); background: var(--surface); border-radius: 22px;
  padding: 11px 16px; max-height: 140px; min-height: 46px; line-height: 1.35; outline: none;
}
.composer textarea:focus { border-color: var(--navy-2); }
.send { width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--navy); color: #fff; display: grid; place-items: center; cursor: pointer; flex: none; }
.send:disabled { opacity: .45; }
.send svg { width: 20px; height: 20px; }
.deep-toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); max-width: 720px; margin: 0 auto 6px; padding-left: 4px; }
.deep-toggle input { width: 18px; height: 18px; accent-color: var(--navy); }
#view-ask .spacer-bottom { height: 120px; }

/* ---------- Settings ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; box-shadow: var(--shadow); }
.panel h2 { font-size: 17px; margin: 0 0 8px; }
.row { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 15px; }
.row:first-of-type { border-top: 0; }
.row .k { color: var(--muted); }
.row .v { text-align: right; overflow-wrap: anywhere; }
.btn { border: 0; background: var(--navy); color: #fff; padding: 11px 16px; border-radius: 12px; font-weight: 600; cursor: pointer; min-height: 44px; }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: #b3261e; }
.btn.small { padding: 6px 12px; min-height: 34px; font-size: 14px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.field { display: block; margin: 10px 0; }
.field span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 4px; }
.field input, .field textarea, .field select { width: 100%; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 11px 12px; outline: none; }
.field textarea { min-height: 110px; resize: vertical; }
.qr { display: block; width: 240px; max-width: 100%; margin: 10px auto; border-radius: 12px; background: #fff; padding: 8px; }
.pair-code { font: 700 28px/1.2 ui-monospace, Menlo, monospace; letter-spacing: 4px; text-align: center; padding: 12px; background: var(--surface-2); border-radius: 12px; margin: 10px 0; }
.pill { display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 99px; }
.pill.ok { background: var(--good-bg); color: var(--good-fg); }
.pill.bad { background: var(--warn-bg); color: var(--warn-fg); }
.muted { color: var(--muted); font-size: 14px; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); display: flex; justify-content: center;
}
.tabbar-inner { display: flex; width: 100%; max-width: 720px; }
.tab { flex: 1; height: var(--tabbar-h); border: 0; background: transparent; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 12px; color: var(--muted); cursor: pointer; }
.tab svg { width: 24px; height: 24px; }
.tab.active { color: var(--navy); font-weight: 700; }
@media (prefers-color-scheme: dark) { .tab.active { color: var(--gold); } }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 40; opacity: 0; pointer-events: none; transition: opacity .2s; }
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; background: var(--surface);
  border-radius: 20px 20px 0 0; padding: 8px 16px calc(16px + var(--safe-b)); max-height: 88dvh; overflow-y: auto;
  transform: translateY(105%); transition: transform .25s ease; max-width: 720px; margin: 0 auto;
}
.sheet.open { transform: translateY(0); }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet h3 { margin: 4px 0 8px; }
.type-list { list-style: none; padding: 0; margin: 0; }
.type-list li button { width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 12px; margin-bottom: 8px; cursor: pointer; }
.type-list .d { display: block; font-size: 13px; color: var(--muted); }

.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 50; font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; background: var(--navy); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px; padding: 24px 20px; box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
.login-card .crest { width: 56px; height: 56px; border-radius: 16px; background: var(--gold); color: var(--navy); font-weight: 800; display: grid; place-items: center; font-size: 22px; margin: 0 auto 12px; }
.login-card h1 { text-align: center; font-size: 20px; margin: 0 0 4px; }
.login-card p { text-align: center; color: var(--muted); margin: 0 0 16px; }
.login-card .btn { width: 100%; margin-top: 6px; }
.err { color: #b3261e; font-size: 14px; min-height: 20px; margin-top: 8px; text-align: center; }
