/* MedMCQ web — visual language mirrors the iOS app: grouped background, white
   cards with continuous corners, system font, iOS accent blue. Colours are
   semantic tokens with light and dark palettes; appearance follows the system
   unless the user picks Light/Dark (data-theme), the same three-way choice as
   the app's AppearanceMode.

   Layout is FLUID. The root font size scales with the viewport (15px on a phone,
   16px on a laptop, ~21px on a 2000px TV) and every structural size is in rem,
   so the sidebar, cards, options and type all grow together on a big screen
   instead of leaving a 960px strip in the corner. Breakpoints:
     < 900px   phone / iPad portrait: bottom tab bar
     >= 900px  iPad landscape / laptop / TV: left sidebar
   Content that benefits from width (chapter grid) uses it; content that should
   stay a readable line length (a question) is capped and centred. */

:root {
  --bg: #f2f2f7;
  --bg-2: #ffffff;
  --bg-3: #e5e5ea;
  --text: #1c1c1e;
  --text-2: #6e6e73;
  --text-3: #aeaeb2;
  --line: rgba(60,60,67,.18);
  --accent: #007aff;
  --accent-soft: rgba(0,122,255,.12);
  --green: #34c759;
  --red: #ff3b30;
  --orange: #ff9500;
  --purple: #af52de;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 14px rgba(0,0,0,.06);
  --radius: .75rem;
  --tab-h: 3.9rem;
  --top-h: 3.25rem;
  --side-w: 13rem;
  --pad: clamp(12px, 1.5vw, 32px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000; --bg-2: #1c1c1e; --bg-3: #2c2c2e;
    --text: #f2f2f7; --text-2: #98989f; --text-3: #636366;
    --line: rgba(84,84,88,.6);
    --accent: #0a84ff; --accent-soft: rgba(10,132,255,.18);
    --green: #30d158; --red: #ff453a; --orange: #ff9f0a; --purple: #bf5af2;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  --bg: #000000; --bg-2: #1c1c1e; --bg-3: #2c2c2e;
  --text: #f2f2f7; --text-2: #98989f; --text-3: #636366;
  --line: rgba(84,84,88,.6);
  --accent: #0a84ff; --accent-soft: rgba(10,132,255,.18);
  --green: #30d158; --red: #ff453a; --orange: #ff9f0a; --purple: #bf5af2;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;
  /* 15px at phone widths, 16px at 1280, ~18px at 1600, ~21px at 2000, capped at 22px */
  font-size: clamp(15px, 0.694vw + 7.1px, 22px);
}
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 1rem/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h1 { font-size: 1.75rem; font-weight: 800; }
h2 { font-size: 1.25rem; font-weight: 700; }
h3 { font-size: .95rem; font-weight: 600; }
.muted { color: var(--text-2); }
.small { font-size: .8125rem; }
.mono { font-variant-numeric: tabular-nums; }

/* ---- shell ---- */
.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--top-h);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(180%) blur(18px); -webkit-backdrop-filter: saturate(180%) blur(18px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner { height: 100%; display: flex; align-items: center; justify-content: space-between; padding: 0 var(--pad); }
.brand { display: flex; align-items: center; gap: .55rem; color: var(--text); font-weight: 800; font-size: 1.125rem; }
.brand-mark { display: inline-grid; place-items: center; width: 1.75rem; height: 1.75rem; border-radius: .5rem; background: var(--accent); color: #fff; font-weight: 800; font-size: 1rem; }
.icon-btn { background: var(--bg-3); border: 0; border-radius: 999px; height: 2.125rem; padding: 0 .75rem; cursor: pointer; font-size: .8125rem; font-weight: 600; color: var(--text); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--bg-2) 86%, transparent);
  backdrop-filter: saturate(180%) blur(18px); -webkit-backdrop-filter: saturate(180%) blur(18px);
  border-top: 1px solid var(--line);
}
.tabbar a { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: .6875rem; font-weight: 600; color: var(--text-3); }
.tabbar a.active { color: var(--accent); }
.tab-glyph { font-size: 1.375rem; line-height: 1; }
.badge[hidden] { display: none; }
.badge { position: absolute; top: .5rem; left: calc(50% + .5rem); min-width: 1.125rem; height: 1.125rem; padding: 0 .3rem; border-radius: 999px; background: var(--red); color: #fff; font-size: .6875rem; font-weight: 700; display: grid; place-items: center; }

.view { padding: var(--pad) var(--pad) calc(var(--tab-h) + 1.5rem + env(safe-area-inset-bottom)); }
/* Width policy: grids use the room they are given (capped so a TV does not become
   nine tiny columns); reading surfaces are capped at a comfortable line length. */
.view-inner { max-width: 110rem; margin: 0 auto; }
.view-inner.mid { max-width: 70rem; }
.view-inner.narrow { max-width: 56rem; }

/* Sidebar layout from iPad landscape upwards. */
@media (min-width: 900px) {
  .tabbar { top: var(--top-h); bottom: auto; left: 0; right: auto; width: var(--side-w); height: auto; grid-template-columns: 1fr; align-content: start; gap: .25rem; padding: .875rem .625rem; border-top: 0; border-right: 1px solid var(--line); background: transparent; backdrop-filter: none; -webkit-backdrop-filter: none; position: fixed; min-height: calc(100vh - var(--top-h)); }
  .tabbar a { flex-direction: row; justify-content: flex-start; gap: .625rem; padding: .5625rem .75rem; border-radius: .625rem; font-size: .9375rem; }
  .tabbar a.active { background: var(--accent-soft); }
  .tab-glyph { font-size: 1.125rem; width: 1.375rem; text-align: center; }
  .badge { position: static; margin-left: auto; }
  .view { margin-left: var(--side-w); padding: calc(var(--pad) + .5rem) calc(var(--pad) + .5rem) 3rem; }
}
/* Wide desktops and TVs: a roomier sidebar; the rem scale does the rest. */
@media (min-width: 1500px) { :root { --side-w: 14rem; } }
/* Short landscape phones: reclaim vertical space. */
@media (max-width: 899px) and (max-height: 480px) {
  :root { --top-h: 2.6rem; --tab-h: 3rem; }
  .tab-glyph { display: none; }
}
/* Hover affordances only where a pointer exists (not on touch). */
@media (hover: hover) {
  .icon-btn:hover { filter: brightness(.96); }
  .chapter-card:hover { transform: translateY(-1px); }
  .opt:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
}

/* ---- controls ---- */
.seg { display: inline-flex; background: var(--bg-3); border-radius: .5625rem; padding: 2px; gap: 2px; max-width: 100%; overflow-x: auto; scrollbar-width: none; }
.seg::-webkit-scrollbar { display: none; }
.seg button { border: 0; background: transparent; border-radius: .4375rem; padding: .375rem .75rem; font-size: .8125rem; font-weight: 600; color: var(--text-2); cursor: pointer; white-space: nowrap; }
.seg button.on { background: var(--bg-2); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg.wide { display: flex; } .seg.wide button { flex: 1; }
.row { display: flex; gap: .625rem; align-items: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: .75rem; }
.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 1.125rem 0 .5rem; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .5rem; min-height: 2.75rem; padding: 0 1.125rem; border: 0; border-radius: .75rem; background: var(--accent); color: #fff; font-weight: 700; cursor: pointer; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.block { width: 100%; }
.btn.ghost { background: var(--bg-3); color: var(--text); }
.btn.danger { background: var(--red); }
.btn.warn { background: var(--orange); }
.btn.green { background: var(--green); }
.btn.sm { min-height: 2.125rem; padding: 0 .75rem; font-size: .8125rem; border-radius: .5625rem; }
input[type=number], input[type=text], select { font: inherit; padding: .5rem .625rem; border-radius: .5625rem; border: 1px solid var(--line); background: var(--bg-2); color: var(--text); }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: .625rem; padding: .6875rem .875rem; border-bottom: 1px solid var(--line); cursor: pointer; }
.toggle:last-child { border-bottom: 0; }
.toggle input { width: 1.25rem; height: 1.25rem; accent-color: var(--accent); }

/* ---- cards ---- */
.card { background: var(--bg-2); border-radius: var(--radius); box-shadow: var(--shadow); }
.card.pad { padding: 1rem; }
/* Chapter grid: as many 14rem+ columns as fit, so a phone gets one, an iPad
   two or three, a laptop three or four, a TV five or six. */
.grid { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr)); }
.chapter-card { display: flex; flex-direction: column; gap: .625rem; padding: .875rem; border-radius: var(--radius); color: #fff; cursor: pointer; border: 0; text-align: left; box-shadow: var(--shadow); min-height: 7.4rem; transition: transform .12s ease; }
.chapter-card .ch-num { font-size: .6875rem; font-weight: 800; letter-spacing: .08em; opacity: .85; }
.chapter-card .ch-title { font-size: .9375rem; font-weight: 700; line-height: 1.25; }
.chapter-card .ch-meta { margin-top: auto; display: flex; justify-content: space-between; align-items: center; font-size: .75rem; font-weight: 600; opacity: .95; }
.bar { height: 5px; border-radius: 999px; background: rgba(255,255,255,.3); overflow: hidden; }
.bar > i { display: block; height: 100%; background: #fff; border-radius: 999px; }
.g0 { background: linear-gradient(135deg, #ff5e62, #ff9966); }
.g1 { background: linear-gradient(135deg, #2193b0, #6dd5ed); }
.g2 { background: linear-gradient(135deg, #c850c0, #4158d0); }
.g3 { background: linear-gradient(135deg, #11998e, #38ef7d); }
.g4 { background: linear-gradient(135deg, #d26900, #c89b14); }
.g5 { background: linear-gradient(135deg, #5b86e5, #36d1dc); }

.queue { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); gap: .75rem; max-width: 40rem; }
.queue-card { display: flex; align-items: center; gap: .75rem; padding: .875rem; border: 0; text-align: left; cursor: pointer; }
.queue-card .q-count { font-size: 1.625rem; font-weight: 800; }
.queue-card .q-title { font-weight: 700; }
.queue-card .q-sub { font-size: .75rem; color: var(--text-2); }

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; }
@media (min-width: 700px) { .stat-grid { grid-template-columns: repeat(4, 1fr); } }
.stat { padding: .875rem; }
.stat .v { font-size: 1.625rem; font-weight: 800; }
.stat .k { font-size: .75rem; color: var(--text-2); font-weight: 600; }
.stat.good .v { color: var(--green); } .stat.warn .v { color: var(--orange); } .stat.bad .v { color: var(--red); } .stat.info .v { color: var(--accent); } .stat.purple .v { color: var(--purple); }

.hbar { display: grid; grid-template-columns: minmax(7.5rem, 34%) 1fr 3rem; align-items: center; gap: .625rem; padding: .5rem 0; border-bottom: 1px solid var(--line); font-size: .8125rem; }
.hbar:last-child { border-bottom: 0; }
.hbar .track { height: .5rem; border-radius: 999px; background: var(--bg-3); overflow: hidden; }
.hbar .track > i { display: block; height: 100%; border-radius: 999px; }
.c-good { background: var(--green); } .c-warn { background: var(--orange); } .c-bad { background: var(--red); }

.list { display: flex; flex-direction: column; }
.list-row { display: flex; align-items: center; gap: .875rem; padding: .75rem .875rem; border-bottom: 1px solid var(--line); color: var(--text); cursor: pointer; background: transparent; border-left: 0; border-right: 0; border-top: 0; width: 100%; text-align: left; }
.list-row:last-child { border-bottom: 0; }
.list-row .doc { font-size: 1.5rem; color: var(--red); width: 1.875rem; text-align: center; }
.list-row .t { font-weight: 600; } .list-row .s { font-size: .8125rem; color: var(--text-2); }
.list-row .chev { margin-left: auto; color: var(--text-3); }

/* ---- session ---- */
.session-top { display: flex; align-items: center; gap: .75rem; margin-bottom: .75rem; }
.progress { flex: 1; height: 6px; background: var(--bg-3); border-radius: 999px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .25s ease; }
.session-meta { display: flex; justify-content: space-between; font-size: .8125rem; color: var(--text-2); margin-bottom: .75rem; }
.qcard { padding: clamp(14px, 1.25rem, 28px); }
.qhead { display: flex; align-items: center; justify-content: space-between; gap: .625rem; margin-bottom: .625rem; }
.tag { display: inline-flex; align-items: center; gap: .375rem; font-size: .75rem; font-weight: 600; color: var(--accent); background: var(--accent-soft); padding: .25rem .5625rem; border-radius: 999px; max-width: 70%; }
.tag span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.flag { background: transparent; border: 0; cursor: pointer; font-size: 1.25rem; color: var(--text-3); padding: .25rem .375rem; border-radius: .5rem; }
.flag.on { color: var(--orange); }
.stem { font-size: 1.0625rem; font-weight: 600; line-height: 1.45; margin: .25rem 0 1rem; white-space: pre-wrap; }
.options { display: flex; flex-direction: column; gap: .5rem; }
.opt { display: grid; grid-template-columns: 1.875rem 1fr; gap: .625rem; align-items: start; text-align: left; padding: .75rem .875rem; border-radius: .625rem; border: 1.5px solid var(--line); background: var(--bg-2); cursor: pointer; line-height: 1.4; }
.opt:disabled { cursor: default; }
.opt .letter { display: grid; place-items: center; width: 1.625rem; height: 1.625rem; border-radius: 999px; background: var(--bg-3); font-weight: 700; font-size: .8125rem; }
.opt.correct { border-color: var(--green); background: color-mix(in srgb, var(--green) 14%, var(--bg-2)); }
.opt.correct .letter { background: var(--green); color: #fff; }
.opt.wrong { border-color: var(--red); background: color-mix(in srgb, var(--red) 14%, var(--bg-2)); }
.opt.wrong .letter { background: var(--red); color: #fff; }
.explain { margin-top: 1rem; padding: .875rem; border-radius: .625rem; background: var(--bg); border: 1px solid var(--line); font-size: .9375rem; line-height: 1.5; white-space: pre-wrap; }
.explain h3 { margin-bottom: .375rem; }
.explain .why { margin-top: .625rem; padding-top: .625rem; border-top: 1px dashed var(--line); }
.explain .why b { color: var(--red); }
.verdict { font-weight: 800; margin-bottom: .375rem; }
.verdict.ok { color: var(--green); } .verdict.no { color: var(--red); }
.session-bottom { position: sticky; bottom: calc(var(--tab-h) + .5rem + env(safe-area-inset-bottom)); margin-top: .875rem; padding: .75rem; }
@media (min-width: 900px) { .session-bottom { bottom: .75rem; } }
.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: .625rem; }
.done { text-align: center; padding: 2.25rem 1rem; }
.done .big { font-size: 4.5rem; line-height: 1; margin-bottom: .75rem; }
.kbd { font-size: .6875rem; color: var(--text-3); text-align: center; margin-top: .5rem; }
@media (hover: none) { .kbd { display: none; } }   /* no keyboard hints on touch devices */
kbd { font: .6875rem ui-monospace, Menlo, monospace; padding: 1px 5px; border-radius: 4px; border: 1px solid var(--line); background: var(--bg-2); }

/* ---- pdf ---- */
.pdf-frame { width: 100%; height: calc(100vh - var(--top-h) - 9.5rem); min-height: 420px; border: 0; border-radius: var(--radius); background: var(--bg-3); }
@media (max-width: 899px) { .pdf-frame { height: calc(100vh - var(--top-h) - var(--tab-h) - 11rem); min-height: 360px; } }
.bm { display: flex; align-items: center; gap: .625rem; padding: .5rem .75rem; border-bottom: 1px solid var(--line); }
.bm:last-child { border-bottom: 0; }
.bm button.x { margin-left: auto; background: transparent; border: 0; color: var(--text-3); cursor: pointer; font-size: 1rem; }

/* ---- modal ---- */
.modal-bg { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.4); display: grid; place-items: center; padding: 1rem; }
.modal { width: min(26rem, 100%); padding: 1.25rem; }
.modal h2 { margin-bottom: .5rem; }
.modal .actions { display: flex; gap: .625rem; margin-top: 1rem; }
.modal .actions .btn { flex: 1; }

.empty { text-align: center; color: var(--text-2); padding: 2.5rem 1rem; }
.loading { text-align: center; color: var(--text-2); padding: 2.5rem; }
.spin { display: inline-block; width: 1.375rem; height: 1.375rem; border: 3px solid var(--bg-3); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: middle; margin-right: .5rem; }
@keyframes spin { to { transform: rotate(360deg); } }
.fade-in { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .fade-in, .progress > i, .chapter-card { animation: none; transition: none; } }
