:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #f0f2f5; --border: #dde1e7; --text: #1d232c; --muted: #5d6673;
  --primary: #3a63a8; --primary-ink: #ffffff; --primary-soft: #e6edf8; --success: #2f7d55; --success-soft: #e3f3ea;
  --warn: #9a6400; --warn-soft: #fbf0d9; --danger: #b3372f; --danger-soft: #fbe7e5; --focus: #2b6cdf;
  --radius: 10px; --shadow: 0 1px 2px rgba(20,30,50,.06), 0 4px 16px rgba(20,30,50,.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161c; --surface: #1a2028; --surface-2: #222a34; --border: #303a46; --text: #e6eaf0; --muted: #9aa5b3;
    --primary: #7fa6e8; --primary-ink: #0f1622; --primary-soft: #23324a; --success: #6cc496; --success-soft: #1d3428;
    --warn: #e2b357; --warn-soft: #3a2f17; --danger: #ef8a80; --danger-soft: #3d2220; --focus: #8db4ff;
    --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12161c; --surface: #1a2028; --surface-2: #222a34; --border: #303a46; --text: #e6eaf0; --muted: #9aa5b3;
  --primary: #7fa6e8; --primary-ink: #0f1622; --primary-soft: #23324a; --success: #6cc496; --success-soft: #1d3428;
  --warn: #e2b357; --warn-soft: #3a2f17; --danger: #ef8a80; --danger-soft: #3d2220; --focus: #8db4ff;
  --shadow: 0 1px 2px rgba(0,0,0,.3); color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--primary); }
h1, h2, h3, h4 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.55rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1.02rem; }
p { margin: 0 0 .75em; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 100; }
.skip:focus { left: 8px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* layout */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px; position: sticky; top: 0;
  height: 100vh; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.brand { font-weight: 700; font-size: 1.1rem; padding: 4px 10px 14px; display: flex; gap: 8px; align-items: center; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--primary); color: var(--primary-ink);
  display: grid; place-items: center; font-size: 14px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--text);
  text-decoration: none; font-size: .95rem; }
.nav a:hover { background: var(--surface-2); }
.nav a[aria-current="page"] { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav .ico { width: 18px; text-align: center; opacity: .8; }
.side-foot { margin-top: auto; padding: 10px; font-size: .82rem; color: var(--muted); }
main { padding: 24px clamp(16px, 3vw, 36px) 80px; max-width: 1180px; width: 100%; }
.topbar { display: none; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head p { color: var(--muted); margin: 0; }
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 50; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .side.open { transform: none; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface);
    border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 40; }
  main { padding: 16px 16px 80px; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 45; }
}

/* surfaces */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow); }
.card + .card { margin-top: 14px; }
.card h2, .card h3 { display: flex; align-items: center; gap: 8px; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 960px) { .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 640px) { .g2, .g3, .g4 { grid-template-columns: 1fr; } }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.muted { color: var(--muted); } .small { font-size: .86rem; } .tiny { font-size: .78rem; }
.right { margin-left: auto; }
hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }

/* controls */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 7px 13px; border-radius: 8px; font: inherit; font-size: .92rem;
  cursor: pointer; min-height: 36px; text-decoration: none; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 4px 9px; min-height: 30px; font-size: .84rem; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.btn.big { padding: 12px 18px; font-size: 1rem; }
label { font-size: .88rem; font-weight: 600; display: block; margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.field .hint { font-weight: 400; color: var(--muted); font-size: .8rem; margin-top: 3px; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=time],
input[type=datetime-local], input[type=search], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  color: var(--text); font: inherit; min-height: 38px; }
textarea { min-height: 90px; resize: vertical; }
input[type=checkbox], input[type=radio] { width: 17px; height: 17px; accent-color: var(--primary); vertical-align: middle; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; margin: 0; cursor: pointer; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 6px 12px; font: inherit; font-size: .88rem; cursor: pointer; color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--primary); color: var(--primary-ink); }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: 9px 14px; font: inherit; color: var(--muted); cursor: pointer;
  border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* badges, alerts */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: .76rem;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; font-weight: 600; }
.badge.ok { background: var(--success-soft); color: var(--success); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.info { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.alert { border-radius: 9px; padding: 10px 12px; border: 1px solid var(--border); background: var(--surface-2); font-size: .92rem; }
.alert.warn { background: var(--warn-soft); border-color: transparent; color: var(--text); }
.alert.bad { background: var(--danger-soft); border-color: transparent; }
.alert.ok { background: var(--success-soft); border-color: transparent; }
.alert.info { background: var(--primary-soft); border-color: transparent; }
.alert + .alert, .alert + * { margin-top: 10px; }
.demo-banner { background: var(--warn-soft); color: var(--text); padding: 8px 14px; font-size: .88rem; border-radius: 9px; margin-bottom: 14px; }

/* lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; gap: 10px; align-items: center; padding: 10px 2px; border-bottom: 1px solid var(--border); }
.list > li:last-child { border-bottom: 0; }
.task { display: flex; gap: 10px; align-items: flex-start; padding: 10px 4px; border-bottom: 1px solid var(--border); }
.task:last-child { border-bottom: 0; }
.task .t-title { font-weight: 500; }
.task.done .t-title { text-decoration: line-through; color: var(--muted); }
.task .meta { font-size: .8rem; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; margin-top: 2px; align-items: center; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
.table-wrap { overflow-x: auto; }

/* stats */
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat .v { font-size: 1.6rem; font-weight: 700; letter-spacing: -.01em; }
.stat .l { font-size: .8rem; color: var(--muted); }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--primary); border-radius: 99px; transition: width .4s; }
.bar.ok > span { background: var(--success); } .bar.warn > span { background: var(--warn); } .bar.bad > span { background: var(--danger); }

/* modal & toast */
.modal-back { position: fixed; inset: 0; background: rgba(10,15,25,.45); display: grid; place-items: center; z-index: 80;
  padding: 16px; animation: fade .15s ease; }
.modal { background: var(--surface); border-radius: 12px; width: min(640px, 100%); max-height: 92vh; overflow: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,.25); animation: rise .18s ease; }
.modal.wide { width: min(980px, 100%); }
.modal header { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 1; }
.modal header h2 { margin: 0; font-size: 1.08rem; }
.modal .body { padding: 16px 18px; }
.modal footer { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px; border-top: 1px solid var(--border);
  position: sticky; bottom: 0; background: var(--surface); }
.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 90; max-width: calc(100vw - 32px); }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 9px; font-size: .9rem; box-shadow: var(--shadow);
  animation: rise .2s ease; max-width: 380px; }
.toast.bad { background: var(--danger); color: #fff; } .toast.ok { background: var(--success); color: #fff; }
@keyframes fade { from { opacity: 0 } } @keyframes rise { from { opacity: 0; transform: translateY(6px) } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* states */
.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty .big { font-size: 2rem; margin-bottom: 6px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%;
  animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
.skel { background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2)); background-size: 200% 100%;
  animation: shimmer 1.2s infinite; border-radius: 6px; height: 14px; margin: 8px 0; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* today */
.hero { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; justify-content: space-between; }
.now-card { border: 2px solid var(--primary); background: var(--primary-soft); }
.exam-chip { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 9px; background: var(--surface-2); }
.days { font-weight: 700; font-size: 1.15rem; min-width: 46px; text-align: center; }

/* calendar */
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; }
.cal .dow { font-size: .75rem; color: var(--muted); text-align: center; font-weight: 600; padding: 4px 0; }
.cal .day { min-height: 104px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 4px 5px;
  font-size: .76rem; overflow: hidden; display: flex; flex-direction: column; gap: 2px; }
.cal .day.other { opacity: .5; } .cal .day.today { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.cal .day .num { font-weight: 600; display: flex; justify-content: space-between; }
.cal .ev { border-radius: 4px; padding: 1px 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: var(--surface-2);
  border-left: 3px solid var(--muted); cursor: pointer; }
.cal .ev.exam { font-weight: 700; background: var(--danger-soft); }
.cal .ev.done { text-decoration: line-through; opacity: .6; }
.cal .day.drop { background: var(--primary-soft); }
@media (max-width: 700px) { .cal .day { min-height: 64px; } .cal .ev { font-size: .68rem; } }

/* practice */
.q { border: 1px solid var(--border); border-radius: 10px; padding: 14px; background: var(--surface); }
.q + .q { margin-top: 12px; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  margin-top: 8px; cursor: pointer; font-weight: 400; }
.opt:hover { background: var(--surface-2); }
.opt.sel { border-color: var(--primary); background: var(--primary-soft); }
.opt.right { border-color: var(--success); background: var(--success-soft); }
.opt.wrong { border-color: var(--danger); background: var(--danger-soft); }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.1rem; padding: 4px 10px; border-radius: 8px; background: var(--surface-2); }
.timer.low { background: var(--danger-soft); color: var(--danger); }
.qnav { display: flex; gap: 6px; flex-wrap: wrap; }
.qnav button { width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font: inherit; color: var(--text); }
.qnav button.answered { background: var(--primary-soft); border-color: var(--primary); }
.qnav button.cur { outline: 2px solid var(--primary); }

/* flashcard */
.flash { min-height: 220px; display: grid; place-items: center; text-align: center; font-size: 1.2rem; padding: 28px; cursor: pointer;
  border-radius: 14px; border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow); white-space: pre-wrap; }

/* chat */
.chat { display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 88%; padding: 10px 14px; border-radius: 12px; }
.msg.user { align-self: flex-end; background: var(--primary); color: var(--primary-ink); white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }
.msg.assistant p:last-child { margin-bottom: 0; }
.cite { display: inline-flex; align-items: center; padding: 0 6px; border-radius: 6px; background: var(--primary-soft); color: var(--primary);
  font-size: .78rem; font-weight: 600; border: 0; cursor: pointer; margin: 0 1px; font-family: inherit; vertical-align: baseline; }
.md h3, .md h4 { margin-top: .6em; } .md ul, .md ol { padding-left: 1.3em; margin: 0 0 .7em; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.md pre { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow-x: auto; }
.md blockquote { border-left: 3px solid var(--border); margin: 0 0 .7em; padding-left: 10px; color: var(--muted); }
.source-text { white-space: pre-wrap; background: var(--surface-2); padding: 12px; border-radius: 8px; font-size: .9rem; max-height: 60vh; overflow: auto; }

/* timer widget */
.focus-fab { position: fixed; right: 16px; bottom: 16px; z-index: 60; }
.focus-panel { position: fixed; right: 16px; bottom: 70px; width: 290px; z-index: 60; }
.focus-time { font-size: 2.4rem; font-weight: 700; text-align: center; font-variant-numeric: tabular-nums; }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(420px, 100%); padding: 26px; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--border); } .chart text { fill: var(--muted); font-size: 10px; }
kbd { border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; font-size: .8rem; background: var(--surface-2); }
details > summary { cursor: pointer; }
.drag { cursor: grab; }

/* ---------------- phone layout ---------------- */
.tabbar { display: none; }
@media (max-width: 860px) {
  .topbar { padding-top: max(10px, env(safe-area-inset-top)); }
  main { padding-bottom: calc(96px + env(safe-area-inset-bottom)); }
  .tabbar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 55; background: var(--surface);
    border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); }
  .tabbar a, .tabbar button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 7px 0 6px;
    font: inherit; font-size: .7rem; color: var(--muted); text-decoration: none; background: none; border: 0; min-height: 52px; cursor: pointer; }
  .tabbar span { font-size: 1.15rem; line-height: 1; }
  .tabbar a[aria-current="page"] { color: var(--primary); font-weight: 600; }
  .focus-fab { bottom: calc(66px + env(safe-area-inset-bottom)); right: 12px; }
  .focus-panel { bottom: calc(118px + env(safe-area-inset-bottom)); right: 12px; left: 12px; width: auto; }
  .toasts { bottom: calc(70px + env(safe-area-inset-bottom)); left: 12px; right: 12px; }
  .toast { max-width: none; }
  /* full-screen dialogs */
  .modal-back { padding: 0; place-items: stretch; }
  .modal, .modal.wide { width: 100%; max-height: none; height: 100%; border-radius: 0; display: flex; flex-direction: column; }
  .modal .body { flex: 1; overflow-y: auto; }
  .modal header { padding-top: max(14px, env(safe-area-inset-top)); }
  .modal footer { padding-bottom: max(12px, env(safe-area-inset-bottom)); flex-wrap: wrap; }
  /* 16px inputs stop iOS from zooming in on focus */
  input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=time],
  input[type=datetime-local], input[type=search], select, textarea { font-size: 16px; }
  .btn { min-height: 40px; }
  .btn.sm { min-height: 34px; }
  .page-head h1 { font-size: 1.3rem; }
  .page-head > div:last-child { width: 100%; }
  .card { padding: 14px; }
  .task .row .btn.sm { padding: 4px 7px; }
  .qnav button { width: 38px; height: 38px; }
  .msg { max-width: 94%; }
  .chat { min-height: 40vh; }
  .side { padding-top: max(16px, env(safe-area-inset-top)); }
  .today-grid, #today-grid, #tutor-grid { grid-template-columns: 1fr !important; }
}
@media (display-mode: standalone) { body { overscroll-behavior-y: none; } }
