:root {
  color-scheme: light;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #f1f5f9;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --ink: #0f172a;
  --ink-dim: #475569;
  --muted: #94a3b8;

  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --brand-soft: #eff6ff;
  --brand-mid: #bfdbfe;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --success-border: #bbf7d0;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --danger-border: #fecaca;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --warn-border: #fde68a;

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
  --sidebar-w: 240px;
  --font: 'Inter', ui-sans-serif, system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
a { color: inherit; text-decoration: none; }
button,
a,
.chip,
.option,
.exam-dot,
.nav-item,
.bnav-item {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.link { color: var(--brand); font-weight: 600; font-size: 14px; transition: opacity .15s; }
.link:hover { opacity: .7; }

h1, h2, h3, h4 { margin: 0; font-weight: 700; letter-spacing: -.02em; }
.eyebrow { margin: 0; color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; }
.muted { color: var(--muted); }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 8px; border-radius: 4px;
  background: var(--brand-soft); color: var(--brand);
  font-size: 12px; font-weight: 600;
}
.badge-success { background: var(--success-soft); color: var(--success); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }

/* ============ Onboarding ============ */
.onboarding {
  min-height: 100dvh; display: grid; place-items: center; padding: 32px 20px;
  background: var(--bg);
}

.onboarding-card {
  width: 100%; max-width: 400px; padding: 40px 36px;
  border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow-md); text-align: center;
}

.onboarding h1 { margin: 16px 0 8px; font-size: clamp(26px,5vw,34px); }
.lead { margin: 0; color: var(--ink-dim); font-size: 14px; }
.onboarding-form { margin-top: 28px; display: grid; gap: 12px; }
.auth-hint {
  min-height: 18px;
  margin: -2px 0 0;
  color: var(--ink-dim);
  font-size: 12px;
  text-align: left;
}
.auth-hint strong { color: var(--brand); font-weight: 700; }

.onboarding-meta {
  margin-top: 28px; padding-top: 24px; border-top: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(2,1fr); gap: 12px;
}
.onboarding-meta div { display: grid; gap: 2px; text-align: center; }
.onboarding-meta strong { font-size: 20px; font-weight: 700; color: var(--brand); }
.onboarding-meta span { color: var(--muted); font-size: 12px; }

.brand-mark {
  display: grid; place-items: center;
  width: 44px; height: 44px; flex: 0 0 auto;
  border-radius: var(--radius-sm);
  background: url("assets/icon.svg") center / cover no-repeat;
  box-shadow: 0 10px 24px rgba(47,99,232,0.22);
}
.brand-mark.xl { width: 56px; height: 56px; border-radius: 12px; font-size: 20px; margin: 0 auto; }

.field { display: grid; gap: 5px; text-align: left; font-weight: 600; font-size: 13px; color: var(--ink-dim); }

input[type="text"], input[type="email"], input[type="search"], input:not([type]), select {
  width: 100%; min-height: 42px; padding: 0 12px;
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--ink); font-size: 14px;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus {
  outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft);
}

/* ============ App shell ============ */
.app { min-height: 100dvh; display: grid; grid-template-columns: var(--sidebar-w) minmax(0,1fr); }

.sidebar {
  position: sticky; top: 0; height: 100dvh;
  display: flex; flex-direction: column; gap: 4px; padding: 14px 8px;
  background: var(--surface); border-right: 1px solid var(--border); z-index: 20;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 2px 4px; margin-bottom: 14px; }

.brand { display: inline-flex; align-items: center; gap: 10px; padding: 4px; }
.brand-text { display: grid; line-height: 1.2; }
.brand-text strong { font-size: 14px; font-weight: 700; }
.brand-text small { color: var(--muted); font-size: 11px; }

.icon-btn.sidebar-close { display: none; }

.nav { display: grid; gap: 1px; }
.nav-item {
  display: flex; align-items: center; gap: 9px; padding: 10px 10px;
  border-radius: var(--radius-sm); color: var(--ink-dim); font-weight: 500; font-size: 14px;
  transition: background .1s, color .1s;
}
.nav-item:hover { background: var(--surface-2); color: var(--ink); }
.nav-item.active { color: var(--brand); background: var(--brand-soft); font-weight: 600; }
.nav-item.active svg { color: var(--brand); }

.sidebar-bottom { margin-top: auto; }
.sidebar-logout { width: 100%; margin-top: 6px; }
.user-card {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px;
  border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface-2);
  color: var(--ink); text-align: left; transition: border-color .1s;
}
.user-card:hover { border-color: var(--border-strong); }
.user-card .user-meta { display: grid; gap: 1px; flex: 1; min-width: 0; }
.user-card strong { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-card small { color: var(--muted); font-size: 11px; }

.avatar {
  display: grid; place-items: center;
  width: 34px; height: 34px; flex: 0 0 auto;
  border-radius: 50%; color: #fff; font-weight: 700; font-size: 13px;
  background: var(--brand);
}
.avatar.sm { width: 28px; height: 28px; font-size: 11px; }
.avatar.xl { width: 60px; height: 60px; font-size: 22px; }

/* ============ Main ============ */
.main { min-width: 0; padding: 20px 28px 100px; display: grid; grid-template-rows: auto 1fr; gap: 20px; }

.topbar {
  position: sticky; top: 12px; z-index: 10;
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px;
  padding: 10px 14px; border-radius: var(--radius);
  border: 1px solid var(--border); background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px); box-shadow: var(--shadow);
}
.topbar-title h1 { font-size: clamp(16px,2vw,20px); }
.topbar-title .eyebrow { font-size: 10px; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }

.streak-pill {
  display: inline-flex; align-items: center; gap: 5px;
  height: 30px; padding: 0 10px; border-radius: 999px;
  background: var(--warn-soft); border: 1px solid var(--warn-border);
  color: var(--warn); font-weight: 600; font-size: 13px;
}
.sync-pill {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 30px; padding: 0 9px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--muted); font-weight: 600; font-size: 11px;
  white-space: nowrap;
}
.sync-pill.ok { color: var(--success); background: var(--success-soft); border-color: var(--success-border); }
.sync-pill.busy { color: var(--brand); background: var(--brand-soft); border-color: var(--brand-mid); }
.sync-pill.error { color: var(--danger); background: var(--danger-soft); border-color: var(--danger-border); }

.avatar-btn { padding: 0; border-radius: 50%; transition: opacity .15s; }
.avatar-btn:hover { opacity: .8; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  color: var(--ink-dim); border: 1px solid var(--border);
  background: var(--surface); transition: background .1s, color .1s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

#menuBtn { display: none; }

/* ============ Buttons ============ */
.btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 40px; padding: 0 18px; border-radius: var(--radius-sm);
  font-weight: 600; font-size: 14px; border: 1px solid transparent;
  transition: background .1s, border-color .1s, transform .08s; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn:disabled { cursor: default; opacity: .78; }
button.is-loading {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}
button.is-loading > * { opacity: 0 !important; }
button.is-loading::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid currentColor;
  border-right-color: transparent;
  color: var(--brand);
  animation: btnSpin .7s linear infinite;
}
.btn-primary.is-loading::after,
.btn-danger.is-loading::after,
.btn-success.is-loading::after {
  color: #fff;
}
.btn-ghost.is-loading::after {
  color: var(--brand);
}
@keyframes btnSpin { to { transform: rotate(360deg); } }

.btn-primary { color: #fff; background: var(--brand); border-color: var(--brand-dark); }
.btn-primary:hover { background: var(--brand-dark); }

.btn-ghost { color: var(--ink); background: var(--surface); border-color: var(--border-strong); }
.btn-ghost:hover { background: var(--surface-2); }

.btn-danger { color: var(--danger); background: var(--danger-soft); border-color: var(--danger-border); }
.btn-danger:hover { background: #fee2e2; }

.btn-success { color: #fff; background: var(--success); }
.btn-success:hover { background: #15803d; }

.btn-lg { min-height: 48px; padding: 0 24px; font-size: 15px; border-radius: var(--radius); }
.btn-sm { min-height: 30px; padding: 0 12px; font-size: 13px; border-radius: 6px; }
.btn-xl { min-height: 56px; padding: 0 28px; font-size: 16px; border-radius: var(--radius); flex: 1; }

/* ============ Views ============ */
.view { display: grid; gap: 20px; align-content: start; }
.view[hidden] { display: none; }

.section-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 4px;
}
.section-head h3 { font-size: 16px; font-weight: 700; }

/* ============ Hero ============ */
.hero {
  display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 24px;
  padding: 28px 32px; border-radius: var(--radius-lg);
  background: var(--brand); border: none;
}
.hero-text { display: grid; gap: 8px; align-content: center; }
.hero-text h2 { font-size: clamp(20px,2.6vw,30px); line-height: 1.15; color: #fff; }
.hero-text p { color: rgba(255,255,255,0.78); max-width: 480px; margin: 0; font-size: 14px; }
.hero-text strong { color: #fff; }
.hero-cta { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

.hero .btn-primary {
  background: #fff; color: var(--brand);
  border-color: rgba(255,255,255,0.5);
}
.hero .btn-primary:hover { background: rgba(255,255,255,0.92); }
.hero .btn-ghost {
  color: rgba(255,255,255,0.9); background: rgba(255,255,255,0.1);
  border-color: rgba(255,255,255,0.25);
}
.hero .btn-ghost:hover { background: rgba(255,255,255,0.18); }

.hero-stats { display: grid; grid-template-columns: auto 1fr; align-items: center; gap: 16px; }

.ring { position: relative; width: 120px; height: 120px; display: grid; place-items: center; }
.ring svg { transform: rotate(-90deg); }
.ring-track { fill: none; stroke: rgba(255,255,255,0.2); stroke-width: 8; }
.ring-value {
  fill: none; stroke: #fff; stroke-width: 8; stroke-linecap: round;
  stroke-dasharray: 100 100; stroke-dashoffset: 100;
  transition: stroke-dashoffset .6s cubic-bezier(.65,0,.35,1);
}
.ring-inner {
  position: absolute;
  inset: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-align: center;
  pointer-events: none;
}
.ring-inner strong { display: block; font-size: 22px; line-height: 1; font-weight: 800; color: #fff; }
.ring-inner span { color: rgba(255,255,255,0.65); font-size: 10px; line-height: 1.1; font-weight: 700; text-transform: uppercase; letter-spacing: .02em; }

.mini-stats { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; min-width: 160px; }
.mini-stats li {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 7px 10px; border-radius: var(--radius-sm);
  background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.15);
}
.mini-stats span { color: rgba(255,255,255,0.7); font-size: 12px; }
.mini-stats strong { font-weight: 700; font-size: 14px; color: #fff; }

/* ============ Topic grid ============ */
.topic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px,1fr)); gap: 10px; }

.topic-tile {
  display: grid; gap: 10px; padding: 16px;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  text-align: left; cursor: pointer;
  transition: transform .15s, box-shadow .15s;
  box-shadow: var(--shadow);
}
.topic-tile:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }

.topic-tile-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.topic-tile-head-left { display: flex; align-items: center; gap: 10px; min-width: 0; }

.topic-emoji {
  display: grid; place-items: center;
  width: 36px; height: 36px; border-radius: var(--radius-sm); flex: 0 0 auto;
  background: var(--tile-bg, var(--brand));
  font-size: 13px; font-weight: 700; color: #fff; letter-spacing: -.5px;
}
.topic-pct { font-size: 13px; font-weight: 700; flex: 0 0 auto; color: var(--ink-dim); }

.topic-tile h4 { font-size: 13px; font-weight: 600; line-height: 1.35; color: var(--ink); }
.topic-tile small { font-size: 12px; color: var(--muted); }
.topic-tile-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.topic-tile .bar { height: 3px; overflow: hidden; border-radius: 999px; background: var(--border); }
.topic-tile .bar span { display: block; height: 100%; border-radius: inherit; background: var(--tile-bg, var(--brand)); transition: width .5s; }

/* ============ Study ============ */
.study-toolbar {
  display: grid; gap: 12px; padding: 14px;
  border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow);
}

.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  display: inline-flex; align-items: center; height: 32px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--border-strong);
  background: var(--surface-2); color: var(--ink-dim);
  font-weight: 500; font-size: 13px; transition: background .1s, color .1s, border-color .1s;
}
.chip:hover { color: var(--brand); border-color: var(--brand-mid); background: var(--brand-soft); }
.chip.active { color: #fff; background: var(--brand); border-color: var(--brand-dark); font-weight: 600; }

.study-filters { display: grid; grid-template-columns: minmax(180px,1fr) minmax(200px,1.4fr); gap: 10px; }
.select { display: grid; gap: 4px; font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.search { position: relative; display: grid; align-items: end; }
.search svg { position: absolute; left: 10px; bottom: 11px; color: var(--muted); }
.search input { padding-left: 36px; margin-top: auto; }
.search::before {
  content: "Поиск"; display: block; font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .08em; margin-bottom: 4px;
}

.study-progress { display: grid; gap: 5px; }
.progress-bar { height: 4px; border-radius: 999px; overflow: hidden; background: var(--border); }
.progress-bar span { display: block; height: 100%; width: 0%; background: var(--brand); border-radius: inherit; transition: width .4s; }
.progress-meta { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; font-weight: 500; }
.progress-meta strong { color: var(--ink-dim); }

/* ============ Question card ============ */
.card-deck-wrapper { position: relative; user-select: none; touch-action: pan-y; }

.question-card {
  position: relative; padding: 24px;
  border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow-md);
  display: grid; gap: 16px; min-height: 320px;
  cursor: grab; will-change: transform; transition: transform .15s ease;
}
.question-card.is-dragging { cursor: grabbing; transition: none; }
.question-card.anim-out-left  { animation: cardSlideLeft  .26s ease forwards; }
.question-card.anim-out-right { animation: cardSlideRight .26s ease forwards; }
.question-card.anim-in-right  { animation: cardEnterRight .22s ease; }
.question-card.anim-in-left   { animation: cardEnterLeft  .22s ease; }

@keyframes cardSlideLeft  { to { transform: translateX(-110%) rotate(-6deg); opacity: 0; } }
@keyframes cardSlideRight { to { transform: translateX(110%)  rotate(6deg);  opacity: 0; } }
@keyframes cardEnterRight { from { transform: translateX(48px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes cardEnterLeft  { from { transform: translateX(-48px); opacity: 0; } to { transform: none; opacity: 1; } }

.swipe-hint {
  position: absolute; top: 20px; padding: 3px 10px; border-radius: 6px;
  font-size: 13px; font-weight: 700; letter-spacing: .05em;
  border: 2px solid; opacity: 0; transition: opacity .1s; pointer-events: none; z-index: 2;
  text-transform: uppercase;
}
.swipe-hint-right { right: 20px; color: var(--success); border-color: var(--success); }
.swipe-hint-left  { left: 20px;  color: var(--danger);  border-color: var(--danger);  }

.question-meta { display: flex; flex-wrap: wrap; gap: 5px; align-items: center; }

.chip-sm {
  display: inline-flex; align-items: center; gap: 4px;
  height: 22px; padding: 0 7px; border-radius: 4px;
  font-size: 11px; font-weight: 600;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--ink-dim);
}
.chip-sm.brand { background: var(--brand-soft); border-color: var(--brand-mid); color: var(--brand); }
.chip-sm.warn  { background: var(--warn-soft);  border-color: var(--warn-border); color: var(--warn); }
.chip-sm.success { background: var(--success-soft); border-color: var(--success-border); color: var(--success); }
.chip-sm.danger  { background: var(--danger-soft);  border-color: var(--danger-border); color: var(--danger); }

.question-card h4 { font-size: clamp(15px,2vw,18px); line-height: 1.55; font-weight: 500; }

.options { display: grid; gap: 6px; }
.option {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center;
  gap: 10px; width: 100%; padding: 11px 14px;
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface-2); color: var(--ink);
  text-align: left; font-size: 14px; line-height: 1.4;
  transition: border-color .1s, background .1s; cursor: pointer;
}
.option:hover:not(.revealed):not(:disabled) { border-color: var(--brand); background: var(--brand-soft); }

.option-letter {
  display: grid; place-items: center;
  width: 28px; height: 28px; flex: 0 0 auto;
  border-radius: 6px; color: var(--ink-dim); font-weight: 600; font-size: 12px;
  background: var(--surface-3); border: 1px solid var(--border-strong);
  transition: background .1s, color .1s;
}
.option:hover:not(.revealed):not(:disabled) .option-letter { background: var(--brand); color: #fff; border-color: var(--brand); }

.option.correct, .option.revealed-correct { border-color: var(--success-border); background: var(--success-soft); }
.option.correct .option-letter, .option.revealed-correct .option-letter { background: var(--success); color: #fff; border-color: var(--success); }
.option.wrong { border-color: var(--danger-border); background: var(--danger-soft); }
.option.wrong .option-letter { background: var(--danger); color: #fff; border-color: var(--danger); }
.option.selected { border-color: var(--brand-mid); background: var(--brand-soft); }
.option.selected .option-letter { background: var(--brand); color: #fff; border-color: var(--brand); }
.option.selected .option-mark { opacity: 1; color: var(--brand); }

.option-mark { color: var(--muted); font-size: 16px; opacity: 0; transition: opacity .1s; }
.option.correct .option-mark, .option.revealed-correct .option-mark { opacity: 1; color: var(--success); }
.option.wrong .option-mark { opacity: 1; color: var(--danger); }

/* Chat training */
.study-chat-card {
  cursor: default;
  padding: 18px;
  gap: 0;
  min-height: 360px;
  background:
    linear-gradient(rgba(17,19,24,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(17,19,24,0.03) 1px, transparent 1px),
    rgba(255,255,255,0.88);
  background-size: 34px 34px, 34px 34px, auto;
}

.chat-stream {
  display: grid;
  gap: 14px;
  align-content: start;
}

.chat-row {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  gap: 10px;
  align-items: end;
}

.chat-row.user {
  grid-template-columns: minmax(0, 1fr);
  justify-items: end;
}

.chat-avatar {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 13px;
  background: linear-gradient(145deg, #111318, #2f63e8);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  box-shadow: 0 10px 22px rgba(47,99,232,0.18);
}

.chat-avatar.brand-icon {
  background: url("assets/icon.svg") center / cover no-repeat;
  color: transparent;
}

.chat-avatar.ghost {
  visibility: hidden;
}

.chat-bubble {
  max-width: min(760px, 100%);
  padding: 16px 18px;
  border: 1px solid rgba(203,215,228,0.88);
  box-shadow: 0 14px 36px rgba(17,19,24,0.06);
}

.chat-bubble.question {
  border-radius: 18px 18px 18px 6px;
  background: rgba(255,255,255,0.94);
}

.chat-bubble.question .question-meta {
  margin-bottom: 12px;
}

.chat-bubble.question h4 {
  margin: 0;
  font-size: 18px;
  line-height: 1.62;
  font-weight: 650;
}

.chat-bubble.selected {
  border-radius: 18px 18px 6px 18px;
  background: #111318;
  color: #fff;
}

.chat-bubble.selected strong {
  display: block;
  margin-top: 4px;
  font-size: 15px;
  line-height: 1.45;
}

.chat-bubble.feedback {
  border-radius: 18px 18px 18px 6px;
  background: rgba(255,255,255,0.96);
}

.chat-bubble.feedback p {
  margin: 4px 0 0;
  line-height: 1.55;
  color: #38465a;
}

.chat-bubble.feedback.success {
  border-color: var(--success-border);
  background: var(--success-soft);
}

.chat-bubble.feedback.danger {
  border-color: var(--danger-border);
  background: var(--danger-soft);
}

.chat-bubble.feedback.neutral {
  border-color: var(--warn-border);
  background: var(--warn-soft);
}

.chat-label {
  display: block;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #8a95a6;
}

.chat-bubble.selected .chat-label {
  color: rgba(255,255,255,0.62);
}

.chat-bubble.feedback.success .chat-label { color: var(--success); }
.chat-bubble.feedback.danger .chat-label { color: var(--danger); }
.chat-bubble.feedback.neutral .chat-label { color: var(--warn); }

.chat-replies {
  width: min(760px, 100%);
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 8px;
}

.chat-replies .option {
  min-height: 54px;
  border-radius: 16px;
  background: rgba(255,255,255,0.92);
  box-shadow: 0 10px 26px rgba(17,19,24,0.04);
}

.chat-replies .option:disabled {
  opacity: 1;
}

.study-chat-card.is-answered {
  min-height: 260px;
}

/* Flashcard */
.flashcard { min-height: 320px; display: flex; flex-direction: column; }
.flashcard-question {
  font-size: 1.1rem; line-height: 1.6; font-weight: 500; color: var(--ink); flex: 1; padding: 8px 0 20px;
}
.flashcard-answer {
  padding: 14px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted); font-size: 14px; line-height: 1.6;
}
.flashcard-answer.revealed { color: var(--ink); border-color: var(--success-border); background: var(--success-soft); }

.swipe-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 4px; }

.study-actions { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.study-actions #studyReveal { margin-left: auto; }
.exam-run .study-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
.exam-run .study-actions .btn {
  width: 100%;
  min-height: 58px;
}
.exam-finish-cta {
  box-shadow: 0 16px 34px rgba(47,99,232,0.22);
}

.empty-state { padding: 32px; text-align: center; color: var(--muted); }
.empty-state h4 { color: var(--ink); margin-bottom: 8px; font-size: 15px; }

/* ============ Exam ============ */
.exam-shell { display: grid; gap: 16px; }

.exam-setup {
  display: grid; grid-template-columns: minmax(0,1fr) 270px; gap: 16px;
  padding: 24px; border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow);
}
.exam-setup h3 { font-size: 20px; margin-bottom: 6px; }
.exam-options { display: grid; gap: 14px; }
.exam-setup .field { text-transform: uppercase; font-size: 11px; letter-spacing: .07em; }

.exam-quick {
  display: grid; gap: 8px; padding: 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface-2); align-content: start;
}
.exam-quick h4 { font-size: 13px; color: var(--ink-dim); font-weight: 600; }
.exam-quick ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; color: var(--ink-dim); font-size: 13px; }
.exam-quick li { display: flex; gap: 8px; align-items: flex-start; }
.exam-quick li::before { content: "✓"; color: var(--success); font-weight: 700; }

.exam-run { display: grid; gap: 14px; }
.exam-bar {
  display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: center;
  padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface);
}
.exam-timer { font-weight: 700; font-size: 15px; display: inline-flex; align-items: center; gap: 5px; color: var(--warn); }
.exam-timer.danger { color: var(--danger); }

.exam-question-nav {
  display: flex; gap: 4px; flex-wrap: wrap; padding: 12px;
  border-radius: var(--radius); border: 1px solid var(--border); background: var(--surface);
}
.exam-dot {
  width: 30px; height: 30px; border-radius: 6px; border: 1px solid var(--border-strong);
  background: var(--surface-2); color: var(--ink-dim); font-size: 11px; font-weight: 600;
  transition: border-color .1s, background .1s;
}
.exam-dot:hover { border-color: var(--brand); color: var(--brand); background: var(--brand-soft); }
.exam-dot.answered { background: var(--brand-soft); border-color: var(--brand-mid); color: var(--brand); }
.exam-dot.flagged { background: var(--warn-soft); border-color: var(--warn-border); color: var(--warn); }
.exam-dot.current { outline: 2px solid var(--brand); outline-offset: 1px; }
.exam-dot.correct { background: var(--success-soft); border-color: var(--success-border); color: var(--success); }
.exam-dot.wrong { background: var(--danger-soft); border-color: var(--danger-border); color: var(--danger); }

.exam-summary {
  display: grid; gap: 16px; padding: 24px; border-radius: var(--radius-lg);
  border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
}
.exam-summary h3 { font-size: 24px; }
.exam-summary .stat-row { display: grid; grid-template-columns: repeat(auto-fit,minmax(110px,1fr)); gap: 10px; }
.exam-summary .stat-row div { padding: 12px 14px; border-radius: var(--radius-sm); background: var(--surface-2); border: 1px solid var(--border); }
.exam-summary .stat-row strong { font-size: 22px; font-weight: 700; display: block; }
.exam-summary .stat-row span { color: var(--ink-dim); font-size: 12px; }
.exam-summary-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ============ Stats ============ */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit,minmax(140px,1fr)); gap: 10px; }
.stat-card {
  padding: 16px; border-radius: var(--radius); border: 1px solid var(--border);
  background: var(--surface); display: grid; gap: 4px; box-shadow: var(--shadow);
}
.stat-card .eyebrow { font-size: 10px; }
.stat-card strong { font-size: 26px; font-weight: 700; letter-spacing: -.02em; color: var(--brand); }
.stat-card small { color: var(--muted); font-size: 12px; }

.stats-topics {
  display: grid; gap: 0; padding: 0 18px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
}
.stats-topic-row {
  display: grid; grid-template-columns: minmax(0,1fr) 130px auto; align-items: center;
  gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border);
}
.stats-topic-row:last-child { border-bottom: none; }
.stats-topic-row .bar { height: 4px; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.stats-topic-row .bar span { display: block; height: 100%; border-radius: inherit; background: var(--brand); }
.stats-topic-row .pct { font-weight: 700; min-width: 40px; text-align: right; font-size: 13px; color: var(--brand); }
.stats-topic-row .topic-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; font-size: 13px; }

.exam-history { display: grid; gap: 6px; }
.exam-history-item {
  display: grid; grid-template-columns: auto 1fr auto auto; align-items: center;
  gap: 12px; padding: 11px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface);
}
.exam-history-item .eh-score { font-weight: 700; font-size: 15px; color: var(--brand); }
.exam-history-item .eh-date { color: var(--muted); font-size: 12px; }

/* ============ Profile ============ */
.profile-head {
  display: flex; align-items: center; gap: 16px; padding: 20px;
  border-radius: var(--radius-lg); border: 1px solid var(--border);
  background: var(--surface); box-shadow: var(--shadow);
}
.profile-head h3 { font-size: 20px; }
.profile-head > div:nth-child(2) { min-width: 0; flex: 1; }
.profile-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.profile-grid { display: grid; grid-template-columns: repeat(auto-fit,minmax(260px,1fr)); gap: 12px; }
.card {
  display: grid; gap: 10px; padding: 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow);
}
.card h4 { font-size: 14px; font-weight: 600; }

.profile-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.profile-row {
  display: grid; grid-template-columns: auto 1fr auto auto; align-items: center;
  gap: 10px; padding: 9px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2);
}
.profile-row.active { border-color: var(--brand-mid); background: var(--brand-soft); }
.profile-row .pr-meta { display: grid; min-width: 0; }
.profile-row strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 600; }
.profile-row small { color: var(--muted); font-size: 11px; }

/* ============ Bottom nav ============ */
.bottom-nav {
  display: none; position: fixed; z-index: 30; left: 0; right: 0; bottom: 0;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom,0px));
  border-top: 1px solid var(--border); background: rgba(255,255,255,0.95);
  backdrop-filter: blur(12px); justify-content: space-around;
}
.bnav-item {
  display: grid; place-items: center; gap: 2px; padding: 6px 10px;
  border-radius: var(--radius-sm); color: var(--muted); font-size: 10px; font-weight: 500; flex: 1;
  transition: color .1s;
}
.bnav-item.active { color: var(--brand); }

/* ============ Footer ============ */
.app-footer {
  align-self: end;
  padding: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.app-footer a {
  color: var(--brand);
  font-weight: 600;
}
.app-footer a:hover { opacity: .75; }

/* ============ Scrim & toast ============ */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 15; opacity: 0; pointer-events: none; transition: opacity .18s; }
.scrim.open { opacity: 1; pointer-events: auto; }

.toast {
  position: fixed; left: 50%; bottom: 80px;
  transform: translateX(-50%) translateY(12px);
  padding: 9px 16px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--ink); color: #fff; font-weight: 500; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 40;
  box-shadow: var(--shadow-md); white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============ iOS PWA install ============ */
.ios-install {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: end center;
  padding: 18px;
  background: rgba(15,23,42,0.42);
  backdrop-filter: blur(12px);
}

.ios-install-card {
  width: min(440px, 100%);
  padding: 26px;
  border-radius: 28px;
  border: 1px solid rgba(203,215,228,0.9);
  background:
    linear-gradient(rgba(17,19,24,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(17,19,24,0.03) 1px, transparent 1px),
    #ffffff;
  background-size: 34px 34px, 34px 34px, auto;
  box-shadow: 0 30px 90px rgba(15,23,42,0.24);
  text-align: center;
}

.ios-install-card .brand-mark {
  margin: 0 auto 14px;
}

.ios-install-card h2 {
  margin: 4px 0 8px;
  font-size: 25px;
  line-height: 1.12;
  letter-spacing: 0;
}

.ios-install-card .lead {
  margin: 0 auto;
  max-width: 340px;
  color: #4d5562;
  font-size: 15px;
}

.ios-install-steps {
  display: grid;
  gap: 10px;
  margin: 22px 0;
  text-align: left;
}

.ios-step {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 16px;
  border: 1px solid #dce5f0;
  background: rgba(247,249,252,0.9);
  color: #111318;
  font-weight: 750;
}

.ios-step-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: var(--brand-soft);
  color: var(--brand);
}

.ios-step-icon svg {
  width: 24px;
  height: 24px;
}

.ios-install-actions {
  display: grid;
  gap: 10px;
}

body.ios-install-open {
  overflow: hidden;
}

/* ============ Responsive ============ */
@media (max-width: 1024px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100dvh; width: 260px;
    transform: translateX(-105%); transition: transform .2s ease;
    box-shadow: var(--shadow-md);
  }
  .sidebar.open { transform: translateX(0); }
  .icon-btn.sidebar-close { display: inline-grid; }
  #menuBtn { display: inline-grid; }
  .hero { grid-template-columns: 1fr; }
  .hero-stats { justify-content: space-between; }
  .main { padding: 14px 16px 88px; }
  .bottom-nav { display: flex; }
}

@media (max-width: 640px) {
  .topbar { padding: 8px 10px; }
  .topbar-title h1 { font-size: 15px; }
  .topbar-title .eyebrow { display: none; }
  .hero { padding: 20px; gap: 14px; }
  .hero-stats { grid-template-columns: 1fr; }
  .mini-stats { width: 100%; }
  .study-toolbar { padding: 10px; }
  .study-filters { grid-template-columns: 1fr; }
  .question-card { padding: 16px; }
  .option { padding: 10px 12px; }
  .exam-setup { grid-template-columns: 1fr; padding: 16px; }
  .exam-bar { grid-template-columns: 1fr 1fr; }
  .exam-bar .exam-bar-stats { grid-column: 1 / -1; }
  .profile-head { flex-direction: column; }
  .study-actions .btn { flex: 1; min-width: 100px; }
  .stats-topic-row { grid-template-columns: minmax(0,1fr) auto; }
  .stats-topic-row .bar { grid-column: 1 / -1; }
  .swipe-actions .btn-xl { min-height: 50px; font-size: 14px; }
}

@media (min-width: 1025px) { .bottom-nav { display: none; } }

.ring svg defs { display: block; }

/* ============ Visual refresh ============ */
:root {
  --bg: #eef3f8;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --surface-3: #edf2f7;
  --border: #dfe6ee;
  --border-strong: #c8d3df;
  --ink: #111318;
  --ink-dim: #4d5562;
  --muted: #97a2b3;
  --brand: #2f63e8;
  --brand-dark: #1746c7;
  --brand-soft: #eaf1ff;
  --brand-mid: #c7d8ff;
  --accent: #f05a3c;
  --accent-soft: #fff0ec;
  --success: #22966a;
  --success-soft: #eaf8f1;
  --success-border: #bce8d4;
  --warn: #bd7a12;
  --warn-soft: #fff7e8;
  --warn-border: #f3d59a;
  --danger: #d3413f;
  --danger-soft: #fff0ef;
  --danger-border: #f1c6c4;
  --radius-sm: 10px;
  --radius: 14px;
  --radius-lg: 22px;
  --shadow: 0 12px 32px rgba(17,19,24,0.06);
  --shadow-md: 0 24px 70px rgba(17,19,24,0.10);
}

* { letter-spacing: 0 !important; }

body {
  background:
    linear-gradient(rgba(47,99,232,0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(47,99,232,0.035) 1px, transparent 1px),
    radial-gradient(circle at 76% 10%, rgba(240,90,60,0.10), transparent 34%),
    radial-gradient(circle at 4% 20%, rgba(34,150,106,0.09), transparent 30%),
    var(--bg);
  background-size: 42px 42px, 42px 42px, auto, auto, auto;
}

h1, h2, h3, h4 { font-weight: 800; }

.onboarding {
  background:
    linear-gradient(rgba(47,99,232,0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(47,99,232,0.045) 1px, transparent 1px),
    radial-gradient(circle at 18% 18%, rgba(34,150,106,0.12), transparent 30%),
    radial-gradient(circle at 88% 18%, rgba(240,90,60,0.12), transparent 30%),
    #eef3f8;
  background-size: 38px 38px, 38px 38px, auto, auto, auto;
}

.onboarding-card {
  max-width: 430px;
  padding: 44px 38px;
  border-radius: 26px;
  border-color: rgba(200,211,223,0.85);
  box-shadow: 0 28px 80px rgba(27,42,70,0.12);
}

.onboarding h1 {
  margin: 20px 0 10px;
  font-size: 32px;
  line-height: 1.15;
}

.lead {
  color: #38465a;
  font-size: 16px;
  line-height: 1.55;
}

.brand-mark {
  border-radius: 12px;
  background: url("assets/icon.svg") center / cover no-repeat;
  box-shadow: 0 10px 24px rgba(47,99,232,0.22);
}
.brand-mark.xl {
  width: 62px;
  height: 62px;
  border-radius: 16px;
  font-size: 22px;
}

.field {
  gap: 8px;
  color: #344256;
  font-size: 14px;
}

input[type="text"], input[type="email"], input[type="search"], input:not([type]), select {
  min-height: 50px;
  padding: 0 16px;
  border-radius: 12px;
  background: #fbfdff;
  border-color: #cbd7e4;
  font-size: 15px;
  font-weight: 650;
}
input:focus, select:focus {
  border-color: var(--brand);
  background: #eef6ff;
  box-shadow: 0 0 0 4px rgba(47,99,232,0.12);
}

.btn {
  min-height: 42px;
  border-radius: 12px;
  font-weight: 750;
  box-shadow: none;
}
.btn-primary {
  background: #111318;
  border-color: #111318;
}
.btn-primary:hover { background: #252933; }
.onboarding .btn-primary {
  min-height: 56px;
  background: var(--brand);
  border-color: var(--brand);
  box-shadow: 0 14px 30px rgba(47,99,232,0.20);
}
.onboarding .btn-primary:hover { background: var(--brand-dark); }
.btn-ghost {
  background: rgba(255,255,255,0.75);
  border-color: #d7e0ea;
}

.app {
  background: rgba(255,255,255,0.18);
}

.sidebar {
  padding: 18px 10px;
  background: rgba(255,255,255,0.78);
  border-right-color: rgba(203,215,228,0.75);
  backdrop-filter: blur(18px);
}
.brand { padding: 6px; }
.brand-text strong { font-size: 15px; }
.brand-text small { color: #737d8c; font-size: 12px; }

.nav { gap: 4px; }
.nav-item {
  min-height: 42px;
  padding: 10px 12px;
  border-radius: 12px;
  color: #5c6675;
  font-weight: 650;
}
.nav-item:hover {
  background: rgba(17,19,24,0.045);
}
.nav-item.active {
  color: #111318;
  background: #ffffff;
  box-shadow: 0 10px 26px rgba(17,19,24,0.07);
}
.nav-item.active svg { color: var(--accent); }

.user-card {
  border-radius: 14px;
  background: #ffffff;
  box-shadow: 0 10px 26px rgba(17,19,24,0.06);
}

.main {
  padding: 24px 32px 104px;
  gap: 22px;
}

.topbar {
  top: 16px;
  padding: 12px 14px;
  border-radius: 18px;
  background: rgba(255,255,255,0.84);
  border-color: rgba(203,215,228,0.78);
  box-shadow: 0 18px 48px rgba(17,19,24,0.08);
}
.topbar-title h1 {
  font-size: 21px;
  line-height: 1.2;
}

.hero {
  position: relative;
  overflow: hidden;
  grid-template-columns: minmax(0,1fr) minmax(280px,420px);
  gap: 28px;
  padding: 34px;
  border: 1px solid rgba(203,215,228,0.8);
  background:
    linear-gradient(rgba(17,19,24,0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(17,19,24,0.035) 1px, transparent 1px),
    linear-gradient(135deg, #ffffff 0%, #f6faf8 48%, #fff5f1 100%);
  background-size: 38px 38px, 38px 38px, auto;
  box-shadow: var(--shadow-md);
}

.hero-text h2 {
  max-width: 720px;
  color: #111318;
  font-size: 46px;
  line-height: 1.08;
}
.hero-text p {
  max-width: 620px;
  color: #4d5562;
  font-size: 16px;
  line-height: 1.7;
}
.hero-text strong { color: var(--accent); }

.hero .btn-primary {
  background: #111318;
  color: #ffffff;
  border-color: #111318;
}
.hero .btn-primary:hover { background: #252933; }
.hero .btn-ghost {
  color: #111318;
  background: rgba(255,255,255,0.72);
  border-color: #d9e1ea;
}

.hero-stats {
  align-self: center;
  padding: 18px;
  border-radius: 20px;
  background: rgba(255,255,255,0.80);
  border: 1px solid rgba(203,215,228,0.78);
  box-shadow: 0 26px 60px rgba(17,19,24,0.10);
  backdrop-filter: blur(14px);
}
.ring-track { stroke: #e3e9f1; }
.ring-value { stroke: var(--accent); }
.ring-inner strong { color: #111318; }
.ring-inner span { color: #8a95a6; }
.ring-inner {
  inset: 20px;
}
.mini-stats li {
  background: #f7f9fc;
  border-color: #e2e8f0;
}
.mini-stats span { color: #7a8494; }
.mini-stats strong { color: #111318; }

.topic-tile,
.study-toolbar,
.question-card,
.exam-setup,
.exam-quick,
.exam-bar,
.exam-question-nav,
.exam-summary,
.stat-card,
.stats-topics,
.exam-history-item,
.profile-head,
.card {
  border-color: rgba(203,215,228,0.86);
  background: rgba(255,255,255,0.86);
  box-shadow: 0 16px 42px rgba(17,19,24,0.06);
}

.topic-tile:hover {
  transform: translateY(-3px);
  box-shadow: 0 22px 54px rgba(17,19,24,0.10);
}
.topic-emoji {
  border-radius: 11px;
  background: #111318;
}
.topic-tile .bar span,
.progress-bar span,
.stats-topic-row .bar span {
  background: linear-gradient(90deg, var(--accent), var(--success));
}

.chip {
  border-radius: 12px;
  background: #ffffff;
  font-weight: 650;
}
.chip.active {
  background: #111318;
  border-color: #111318;
}

.question-card h4 {
  font-size: 18px;
  line-height: 1.62;
  font-weight: 650;
}
.option {
  border-radius: 14px;
  background: #fbfdff;
}
.option:hover:not(.revealed):not(:disabled) {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.option:hover:not(.revealed):not(:disabled) .option-letter {
  background: var(--accent);
  border-color: var(--accent);
}

.sync-pill.ok::before,
.sync-pill.busy::before,
.sync-pill.error::before {
  content: "";
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: currentColor;
}

@media (max-width: 1024px) {
  .main { padding: 16px 16px 92px; }
  .hero { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  .onboarding-card {
    padding: 34px 28px;
    border-radius: 24px;
  }
  .onboarding h1 { font-size: 30px; }
  .lead { font-size: 15px; }
  .topbar-actions .sync-pill { display: none; }
  .hero {
    padding: 24px;
    border-radius: 20px;
  }
  .hero-text h2 { font-size: 34px; }
  .hero-text p { font-size: 15px; }
  .profile-actions { justify-content: center; }
}

/* ============ Responsive polish ============ */
html,
body {
  overflow-x: hidden;
}

.topbar-title {
  min-width: 0;
}

.topbar-title h1,
.hero-text h2,
.section-head h3,
.topic-tile h4 {
  overflow-wrap: anywhere;
}

.search input[type="search"] {
  padding-left: 48px;
}

.search svg {
  left: 16px;
  bottom: 16px;
  width: 20px;
  height: 20px;
  pointer-events: none;
}

.ring svg {
  width: 100%;
  height: 100%;
}

@media (max-width: 900px) {
  .main {
    padding: 14px 14px calc(98px + env(safe-area-inset-bottom, 0px));
    gap: 16px;
  }

  .topbar {
    top: 10px;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 18px;
  }

  .topbar-title h1 {
    font-size: clamp(18px, 3.2vw, 22px);
    line-height: 1.15;
  }

  .hero {
    gap: 20px;
    padding: 28px;
    border-radius: 26px;
  }

  .hero-text h2 {
    font-size: clamp(34px, 6.4vw, 44px);
    line-height: 1.08;
  }

  .hero-text p {
    max-width: 100%;
  }

  .hero-stats {
    width: 100%;
    max-width: 560px;
    grid-template-columns: 120px minmax(0, 1fr);
    justify-self: start;
  }

  .ring {
    width: 120px;
    height: 120px;
  }

  .mini-stats {
    min-width: 0;
  }

  .study-toolbar,
  .question-card,
  .exam-setup,
  .exam-quick,
  .exam-bar,
  .exam-question-nav,
  .exam-summary,
  .profile-head,
  .card {
    border-radius: 20px;
  }

  .view[data-view="study"] {
    gap: 12px;
  }

  .study-toolbar {
    gap: 10px;
    padding: 12px;
  }

  .study-progress {
    gap: 4px;
  }

  .progress-meta {
    font-size: 13px;
  }
}

@media (max-width: 760px) {
  .main {
    padding-inline: 12px;
  }

  .topbar {
    grid-template-columns: auto minmax(0, 1fr) auto;
    padding: 8px 10px;
  }

  .topbar-actions {
    gap: 6px;
  }

  .topbar-actions .sync-pill {
    display: none;
  }

  .streak-pill {
    height: 40px;
    min-width: 78px;
    justify-content: center;
    padding: 0 12px;
    font-size: 15px;
  }

  .avatar-btn,
  .topbar .avatar.sm {
    width: 40px;
    height: 40px;
  }

  .hero {
    padding: 24px;
    gap: 18px;
  }

  .study-toolbar {
    gap: 8px;
    padding: 10px;
  }

  .study-filters {
    gap: 8px;
  }

  .study-filters .field,
  .select,
  .search::before {
    font-size: 10px;
  }

  .study-filters input,
  .study-filters select {
    min-height: 44px;
    font-size: 15px;
  }

  .search svg {
    bottom: 12px;
  }

  .search input[type="search"] {
    padding-left: 46px;
  }

  .hero-text {
    gap: 10px;
  }

  .hero-text h2 {
    font-size: clamp(30px, 8.2vw, 38px);
  }

  .hero-text p {
    font-size: 15px;
    line-height: 1.58;
  }

  .hero-cta {
    gap: 10px;
  }

  .hero-cta .btn {
    min-height: 52px;
  }

  .hero-stats {
    grid-template-columns: 108px minmax(0, 1fr);
    gap: 14px;
    padding: 14px;
    border-radius: 20px;
  }

  .ring {
    width: 108px;
    height: 108px;
  }

  .ring-inner {
    inset: 18px;
    gap: 3px;
  }

  .ring-inner strong {
    font-size: 21px;
  }

  .ring-inner span {
    font-size: 9px;
  }

  .mini-stats li {
    padding: 9px 12px;
    gap: 10px;
  }

  .section-head {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .topic-grid {
    grid-template-columns: 1fr;
  }

  .study-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .exam-run .study-actions {
    grid-template-columns: 1fr;
  }

  .study-actions #studyReveal {
    grid-column: 1 / -1;
    grid-row: 1;
    margin-left: 0;
  }

  .study-actions .btn {
    width: 100%;
    min-width: 0;
  }

  .study-chat-card {
    padding: 14px;
    min-height: 300px;
  }

  .study-chat-card.is-answered {
    min-height: 220px;
  }

  .chat-row {
    grid-template-columns: 34px minmax(0, 1fr);
    gap: 8px;
  }

  .chat-avatar {
    width: 34px;
    height: 34px;
    border-radius: 12px;
  }

  .chat-bubble {
    padding: 14px;
  }

  .chat-stream {
    gap: 10px;
  }

  .chat-bubble.question h4 {
    font-size: 16px;
    line-height: 1.55;
  }

  .chat-replies {
    grid-template-columns: 1fr;
  }

  .exam-bar {
    grid-template-columns: 1fr;
  }

  .exam-bar .btn {
    width: 100%;
  }

  .profile-head {
    align-items: stretch;
  }

  .profile-actions {
    justify-content: stretch;
  }

  .profile-actions .btn,
  #bindEmailBtn,
  #profileCreate .btn {
    width: 100%;
  }

  .bottom-nav {
    min-height: 72px;
  }
}

@media (max-width: 520px) {
  .onboarding {
    padding: 14px;
  }

  .onboarding-card {
    padding: 28px 22px;
    border-radius: 22px;
  }

  .ios-install {
    padding: 12px;
  }

  .ios-install-card {
    padding: 24px 20px calc(20px + env(safe-area-inset-bottom, 0px));
    border-radius: 26px;
  }

  .ios-install-card h2 {
    font-size: 23px;
  }

  .ios-step {
    grid-template-columns: 40px minmax(0, 1fr);
    padding: 10px;
    font-size: 14px;
  }

  .ios-step-icon {
    width: 40px;
    height: 40px;
    border-radius: 13px;
  }

  .topbar {
    top: 8px;
    border-radius: 16px;
  }

  .topbar-title h1 {
    font-size: clamp(17px, 5.4vw, 21px);
  }

  .streak-pill {
    min-width: 58px;
    padding: 0 10px;
  }

  .hero {
    padding: 20px;
    border-radius: 22px;
  }

  .study-toolbar {
    margin-top: -4px;
    padding: 9px;
  }

  .chips {
    gap: 4px;
  }

  .chip {
    height: 30px;
    padding: 0 10px;
    font-size: 12px;
  }

  .study-progress {
    margin-top: -2px;
  }

  .progress-meta {
    font-size: 12px;
  }

  .hero-text h2 {
    font-size: clamp(28px, 9vw, 34px);
  }

  .hero-cta {
    display: grid;
    grid-template-columns: 1fr;
  }

  .hero-cta .btn {
    width: 100%;
    white-space: normal;
  }

  .hero-stats {
    grid-template-columns: 96px minmax(0, 1fr);
    gap: 12px;
  }

  .ring {
    width: 96px;
    height: 96px;
  }

  .ring-inner {
    inset: 16px;
    gap: 2px;
  }

  .ring-inner strong {
    font-size: 20px;
  }

  .ring-inner span {
    font-size: 8px;
  }

  .mini-stats span,
  .mini-stats strong {
    font-size: 13px;
  }

  .chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    scrollbar-width: none;
  }

  .chips::-webkit-scrollbar {
    display: none;
  }

  .chip {
    flex: 0 0 auto;
  }

  .option {
    grid-template-columns: auto minmax(0, 1fr);
  }

  .option-mark {
    display: none;
  }

  .chat-row.quick {
    grid-template-columns: 1fr;
  }

  .chat-row.quick .chat-avatar {
    display: none;
  }

  .chat-replies .option {
    min-height: 52px;
  }
}

@media (max-width: 390px) {
  .main {
    padding-inline: 10px;
  }

  .topbar-title h1 {
    font-size: 17px;
  }

  .hero {
    padding: 18px;
  }

  .hero-text h2 {
    font-size: 28px;
  }

  .hero-stats {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .ring {
    width: 104px;
    height: 104px;
  }

  .ring-inner {
    inset: 17px;
  }

  .mini-stats {
    width: 100%;
  }

  .study-chat-card {
    padding: 12px;
    min-height: 260px;
  }

  .study-chat-card.is-answered {
    min-height: 190px;
  }

  .chat-row {
    grid-template-columns: 30px minmax(0, 1fr);
  }

  .chat-avatar {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    font-size: 11px;
  }

  .chat-bubble {
    padding: 12px;
  }
}

/* ============ Mobile work-screen containment ============ */
input[type="text"],
input[type="email"],
input[type="search"],
input:not([type]),
select,
textarea {
  font-size: 16px;
}

@media (max-width: 760px) {
  html,
  body,
  .app,
  .main,
  .view {
    max-width: 100%;
    overflow-x: hidden;
  }

  body[data-route="study"],
  body[data-route="exam"] {
    overflow: hidden;
  }

  body[data-route="study"] .app,
  body[data-route="exam"] .app {
    height: 100dvh;
  }

  body[data-route="study"] .main,
  body[data-route="exam"] .main {
    width: 100%;
    height: 100dvh;
    max-width: 100vw;
    overflow: hidden;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 8px;
    padding: 8px 10px calc(74px + env(safe-area-inset-bottom, 0px));
  }

  body[data-route="study"] .topbar,
  body[data-route="exam"] .topbar {
    position: relative;
    top: auto;
    min-width: 0;
  }

  body[data-route="study"] .app-footer,
  body[data-route="exam"] .app-footer {
    display: none;
  }

  body[data-route="study"] .view[data-view="study"],
  body[data-route="exam"] .view[data-view="exam"] {
    min-width: 0;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    align-content: stretch;
  }

  body[data-route="study"] .view[data-view="study"] {
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    gap: 7px;
  }

  body[data-route="exam"] .view[data-view="exam"] {
    grid-template-rows: minmax(0, 1fr);
  }

  .study-toolbar,
  .study-filters,
  .search,
  .question-card,
  .study-chat-card,
  .exam-shell,
  .exam-run,
  .exam-bar,
  .exam-question-nav,
  .study-actions,
  .options,
  .chat-stream,
  .chat-bubble {
    min-width: 0;
    max-width: 100%;
  }

  body[data-route="study"] .study-toolbar {
    gap: 6px;
    padding: 8px;
    border-radius: 18px;
  }

  body[data-route="study"] .chips {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 4px;
    overflow-x: hidden;
    padding-bottom: 0;
    scrollbar-width: auto;
  }

  body[data-route="study"] .chips::-webkit-scrollbar {
    display: none;
  }

  body[data-route="study"] .chip {
    min-width: 0;
    width: 100%;
    height: 29px;
    padding: 0 8px;
    justify-content: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 12px;
  }

  body[data-route="study"] .study-filters {
    grid-template-columns: 1fr;
    gap: 5px;
  }

  body[data-route="study"] .select {
    gap: 2px;
  }

  body[data-route="study"] .study-filters input,
  body[data-route="study"] .study-filters select {
    min-height: 40px;
    font-size: 16px;
  }

  body[data-route="study"] .search input[type="search"] {
    padding-left: 44px;
  }

  body[data-route="study"] .search svg {
    left: 14px;
    bottom: 10px;
    width: 19px;
    height: 19px;
  }

  body[data-route="study"] .study-progress {
    gap: 3px;
  }

  body[data-route="study"] .progress-meta {
    font-size: 12px;
  }

  body[data-route="study"] .study-chat-card {
    min-height: 0;
    height: 100%;
    overflow: hidden;
    padding: 12px;
    border-radius: 18px;
  }

  body[data-route="study"] .chat-stream {
    height: 100%;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 2px;
    gap: 8px;
  }

  body[data-route="study"] .chat-row {
    grid-template-columns: 30px minmax(0, 1fr);
    gap: 7px;
  }

  body[data-route="study"] .chat-row.quick {
    grid-template-columns: 1fr;
  }

  body[data-route="study"] .chat-row.quick .chat-avatar {
    display: none;
  }

  body[data-route="study"] .chat-avatar {
    width: 30px;
    height: 30px;
    border-radius: 10px;
    font-size: 11px;
  }

  body[data-route="study"] .chat-bubble {
    padding: 11px;
  }

  body[data-route="study"] .chat-bubble.question h4 {
    font-size: 15px;
    line-height: 1.45;
  }

  body[data-route="study"] .option {
    grid-template-columns: auto minmax(0, 1fr);
    gap: 9px;
    padding: 9px 10px;
    font-size: 14px;
    line-height: 1.32;
  }

  body[data-route="study"] .chat-replies {
    width: 100%;
    grid-template-columns: 1fr;
    justify-self: stretch;
  }

  body[data-route="study"] .chat-replies .option {
    width: 100%;
    min-height: 46px;
  }

  body[data-route="study"] .study-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  body[data-route="study"] .study-actions .btn {
    min-height: 44px;
  }

  body[data-route="exam"] .exam-shell {
    min-height: 0;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
  }

  body[data-route="exam"] .exam-shell:has(.exam-run) {
    overflow: hidden;
  }

  body[data-route="exam"] .exam-run {
    min-height: 0;
    height: 100%;
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    gap: 7px;
    overflow: hidden;
  }

  body[data-route="exam"] .exam-bar {
    gap: 8px;
    padding: 8px 10px;
    border-radius: 18px;
  }

  body[data-route="exam"] .exam-bar .btn {
    min-height: 44px;
  }

  body[data-route="exam"] .exam-run .question-card {
    min-height: 0;
    height: 100%;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 14px;
    gap: 10px;
    border-radius: 18px;
  }

  body[data-route="exam"] .exam-run .question-card h4 {
    font-size: 16px;
    line-height: 1.5;
  }

  body[data-route="exam"] .exam-run .option {
    grid-template-columns: auto minmax(0, 1fr);
    padding: 10px;
    font-size: 14px;
    line-height: 1.35;
  }

  body[data-route="exam"] .exam-run .study-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  body[data-route="exam"] .exam-run .study-actions .btn {
    min-height: 46px;
  }

  body[data-route="exam"] .exam-question-nav {
    max-height: 72px;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 8px;
  }

  body[data-route="exam"] .exam-dot {
    width: 34px;
    height: 34px;
  }
}

@media (max-width: 420px) {
  body[data-route="study"] .main,
  body[data-route="exam"] .main {
    padding-inline: 8px;
  }

  body[data-route="study"] .chip {
    font-size: 11px;
  }

  body[data-route="study"] .chat-bubble.question h4,
  body[data-route="exam"] .exam-run .question-card h4 {
    font-size: 15px;
  }

  body[data-route="study"] .option,
  body[data-route="exam"] .exam-run .option {
    font-size: 13px;
  }
}
