:root {
  --bg: #0f1419;
  --panel: #161b22;
  --panel-2: #1c232c;
  --border: #2a3441;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #58a6ff;
  --accent-2: #79c0ff;
  --good: #3fb950;
  --bad: #f85149;
  --warn: #d29922;
  --mark-bg: rgba(247, 220, 111, 0.25);
  --mark-fg: #f7dc6f;
  --kpi-1: rgba(88,166,255,.12); /* blue */
  --kpi-2: rgba(63,185,80,.12);  /* green */
  --kpi-3: rgba(210,153,34,.14); /* amber */
  --kpi-4: rgba(190,135,255,.14); /* purple */
  --kpi-5: rgba(248,81,73,.12);  /* red */
  --kpi-6: rgba(125,200,255,.12);
  --radius: 10px;
  --sidebar-w: 220px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, system-ui, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --panel: #f6f8fa;
    --panel-2: #eef2f6;
    --border: #d0d7de;
    --text: #1f2328;
    --muted: #59636e;
    --accent: #0969da;
    --accent-2: #0550ae;
    --good: #1a7f37;
    --bad: #cf222e;
    --warn: #9a6700;
    --mark-bg: rgba(255, 213, 79, 0.45);
    --mark-fg: #8a6d00;
    --kpi-1: #e3f0ff;
    --kpi-2: #e5f5e8;
    --kpi-3: #fbf2dc;
    --kpi-4: #f1e8fb;
    --kpi-5: #fde7e7;
    --kpi-6: #e3eefb;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 16px; line-height: 1.5; }
body { display: flex; min-height: 100vh; }

/* ----- Sidebar ----- */
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
  padding: 18px 0 8px;
}
.brand {
  display: flex; align-items: center; gap: 10px;
  text-decoration: none; color: var(--text);
  padding: 4px 18px 18px;
  border-bottom: 1px solid var(--border);
  margin: 0 0 14px;
}
.brand-mark {
  width: 30px; height: 30px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white; display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px;
}
.brand-name { font-weight: 600; font-size: 15px; }

#nav { display: flex; flex-direction: column; gap: 2px; padding: 0 10px; flex: 1; }
#nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 7px;
  color: var(--muted); text-decoration: none; font-size: 14px;
}
#nav a .icon { width: 18px; height: 18px; display: inline-flex; }
#nav a .icon svg { width: 18px; height: 18px; }
#nav a:hover { color: var(--text); background: var(--panel-2); }
#nav a.active { color: var(--text); background: var(--panel-2); }
#nav a.active .icon { color: var(--accent); }

.sidebar-bottom { padding: 8px 10px 0; border-top: 1px solid var(--border); }
.sync-status {
  padding: 4px 10px; font-size: 11px; color: var(--muted);
  display: flex; align-items: center; gap: 6px;
}
.sync-status::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--border); }
.sync-status.ok::before { background: var(--good); }
.sync-status.busy::before { background: var(--accent); }
.sync-status.bad::before { background: var(--bad); }
.sync-status.pending::before { background: var(--warn); }
.sync-status:empty { display: none; }

/* Auth screens */
.auth-screen { max-width: 420px; margin: 60px auto; padding: 24px; }
.auth-form { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
.auth-form input[type="password"], .auth-form input[type="text"] {
  width: 100%; font-size: 16px; padding: 10px 12px;
}
.auth-form .btn { align-self: flex-start; margin-top: 6px; }
.auth-err { color: var(--bad); font-size: 13px; min-height: 18px; margin: 4px 0; }
#settings-link {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 7px;
  color: var(--muted); text-decoration: none; font-size: 14px;
}
#settings-link .icon, #settings-link .icon svg { width: 18px; height: 18px; }
#settings-link:hover { color: var(--text); background: var(--panel-2); }
#settings-link.active { color: var(--text); background: var(--panel-2); }
#settings-link.active .icon { color: var(--accent); }

#menu-toggle {
  display: none;
  position: fixed; top: 12px; left: 12px; z-index: 11;
  width: 38px; height: 38px; border-radius: 8px;
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  cursor: pointer; align-items: center; justify-content: center;
}

main {
  flex: 1;
  max-width: 980px; margin: 0 auto; padding: 32px 32px 80px;
  width: 100%;
}

h1 { font-size: 28px; margin: 0 0 6px; }
h2 { font-size: 20px; margin: 24px 0 12px; }
h3 { font-size: 16px; margin: 16px 0 8px; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }
a { color: var(--accent); }

/* ----- KPI strip (shadcn-inspired: clean cards, accented numbers) ----- */
.kpi-strip {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  margin: 18px 0 28px;
}
.kpi-strip .item {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
}
.kpi-strip .label {
  font-size: 13px; color: var(--muted); font-weight: 500;
}
.kpi-strip .value {
  font-size: 28px; font-weight: 700;
  margin-top: 6px; letter-spacing: -0.02em;
  font-family: var(--sans); line-height: 1.1;
}
.kpi-strip .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.kpi-strip .item.t-1 .value { color: var(--accent); }
.kpi-strip .item.t-2 .value { color: var(--good); }
.kpi-strip .item.t-3 .value { color: var(--warn); }
.kpi-strip .item.t-4 .value { color: var(--accent-2); }
.kpi-strip .item.t-5 .value { color: var(--bad); }
.kpi-strip .item.t-6 .value { color: var(--text); }

/* ----- Mode/action cards ----- */
.grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  cursor: pointer;
  transition: border-color .15s, transform .05s;
  text-decoration: none; color: inherit;
  display: flex; flex-direction: column; gap: 4px;
}
.card:hover { border-color: var(--accent); }
.card:active { transform: scale(.99); }
.card .icon { width: 22px; height: 22px; color: var(--accent); margin-bottom: 6px; }
.card .icon svg { width: 22px; height: 22px; }
.card .title { font-weight: 600; }
.card .meta { font-size: 13px; color: var(--muted); }
.card .stat { font-size: 28px; font-weight: 700; margin-top: 8px; font-family: var(--mono); }

/* ----- Buttons / forms ----- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent); color: white; border: 0;
  padding: 9px 16px; border-radius: 7px; font-weight: 500;
  cursor: pointer; font-size: 14px;
  text-decoration: none;
}
.btn:hover { background: var(--accent-2); }
.btn.secondary { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: var(--border); }
.btn.ghost { background: transparent; color: var(--muted); padding: 4px 8px; }
.btn.ghost:hover { color: var(--text); }
.btn.danger { background: var(--bad); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.btn .icon, .btn .icon svg { width: 14px; height: 14px; }

select, input[type="text"], input[type="number"], textarea {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 8px 10px; font-size: 14px; font-family: var(--sans);
}
textarea { resize: vertical; min-height: 80px; width: 100%; }
label.check { display: flex; align-items: center; gap: 8px; padding: 6px 0; font-size: 14px; cursor: pointer; }

/* ----- Quiz / flashcard ----- */
.option {
  display: block; width: 100%;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 14px; margin: 8px 0;
  cursor: pointer; text-align: left; color: var(--text);
  font-size: 15px; font-family: var(--sans);
  transition: border-color .15s, background .15s;
}
.option:hover:not(:disabled) { border-color: var(--accent); }
.option.selected { border-color: var(--accent); background: var(--panel-2); }
.option.correct { border-color: var(--good); background: rgba(63,185,80,0.08); }
.option.wrong { border-color: var(--bad); background: rgba(248,81,73,0.08); }
.option:disabled { cursor: default; }
.option .letter { font-weight: 700; font-family: var(--mono); margin-right: 10px; color: var(--muted); }
.option.correct .letter, .option.wrong .letter { color: inherit; }

.q-meta { color: var(--muted); font-size: 13px; display: flex; gap: 12px; }
.q-text { font-size: 17px; margin: 12px 0 20px; line-height: 1.55; }

.explanation {
  margin-top: 18px; padding: 14px;
  background: var(--panel-2); border-left: 3px solid var(--accent);
  border-radius: 6px; font-size: 14px;
}
.explanation .label { font-weight: 600; margin-bottom: 4px; font-size: 12px; text-transform: uppercase; color: var(--muted); letter-spacing: .5px; }

.progress {
  height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden;
  margin: 14px 0;
}
.progress > div { height: 100%; background: var(--accent); transition: width .2s; }

.bar-row { display: flex; align-items: center; gap: 12px; margin: 6px 0; font-size: 14px; }
.bar-row .label { width: 90px; color: var(--muted); font-family: var(--mono); font-size: 13px; }
.bar-row .bar { flex: 1; height: 10px; background: var(--panel-2); border-radius: 5px; overflow: hidden; }
.bar-row .bar > div { height: 100%; }
.bar-row .num { width: 80px; text-align: right; font-family: var(--mono); font-size: 13px; color: var(--muted); }

.flashcard {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 40px 28px;
  min-height: 280px; display: flex; flex-direction: column; justify-content: center;
  margin: 18px 0;
  text-align: center;
}
.flashcard .front { font-size: 22px; line-height: 1.4; }
.flashcard .back { font-size: 24px; font-weight: 600; color: var(--accent); margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }
.flashcard .cat { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin-bottom: 14px; }

.rating-row { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }
.rating-row .btn { flex: 1; max-width: 140px; justify-content: center; }
.rating-row .btn.again { background: var(--bad); }
.rating-row .btn.hard { background: var(--warn); color: #000; }
.rating-row .btn.good { background: var(--good); }
.rating-row .btn.easy { background: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
td.right { text-align: right; font-family: var(--mono); }

.tag { display: inline-block; padding: 2px 8px; font-size: 12px; border-radius: 4px; background: var(--panel-2); color: var(--muted); font-family: var(--mono); }
.tag.good { background: rgba(63,185,80,0.15); color: var(--good); }
.tag.bad { background: rgba(248,81,73,0.15); color: var(--bad); }
.tag.warn { background: rgba(210,153,34,0.15); color: var(--warn); }

/* ----- Chapter view ----- */
.chapter { font-size: 16px; line-height: 1.65; }
.chapter h1 { font-size: 28px; margin: 24px 0 12px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
.chapter h2 { font-size: 22px; margin: 28px 0 10px; }
.chapter h3 { font-size: 19px; margin: 22px 0 8px; }
.chapter h4 { font-size: 17px; margin: 18px 0 6px; color: var(--accent-2); }
.chapter p { margin: 10px 0; }
.chapter ul, .chapter ol { padding-left: 22px; }
.chapter li { margin: 4px 0; }
.chapter ol > li { padding-left: 4px; }
.chapter img { max-width: 100%; height: auto; border-radius: 8px; border: 1px solid var(--border); display: block; margin: 12px auto; }
.chapter blockquote { margin: 16px 0; padding: 12px 16px; border-left: 3px solid var(--accent); background: var(--panel); border-radius: 6px; }
.chapter blockquote p { margin: 4px 0; }
.chapter table { margin: 14px 0; background: var(--panel); border-radius: 6px; overflow: hidden; }
.chapter hr { border: 0; border-top: 1px solid var(--border); margin: 28px 0; }
.chapter code { font-family: var(--mono); font-size: 13px; padding: 2px 6px; background: var(--panel-2); border-radius: 4px; }
.chapter h2.highlight, .chapter h3.highlight, .chapter h4.highlight {
  background: rgba(88, 166, 255, 0.18);
  transition: background 1.5s ease-out;
  border-radius: 6px;
  padding: 4px 8px;
}
.chapter mark, mark.hl {
  background: var(--mark-bg); color: inherit; padding: 0 2px; border-radius: 3px;
  cursor: pointer;
}
.chapter mark.hl.has-note { box-shadow: inset 0 -2px 0 var(--mark-fg); }

/* ----- Floating selection toolbar (touch + mouse) ----- */
.sel-toolbar {
  position: absolute; z-index: 50;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  display: inline-flex; gap: 2px; padding: 4px;
  user-select: none; -webkit-user-select: none;
}
.sel-toolbar[hidden] { display: none; }
.sel-toolbar button {
  background: transparent; border: 0; color: var(--text);
  padding: 8px 10px; border-radius: 7px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 14px; min-height: 36px;
}
.sel-toolbar button:hover, .sel-toolbar button:active { background: var(--panel-2); }
.sel-toolbar .icon, .sel-toolbar .icon svg { width: 16px; height: 16px; color: var(--muted); }
.sel-toolbar button:hover .icon { color: var(--text); }

/* ----- Table scroll on narrow widths ----- */
.chapter .table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 14px 0;
  border-radius: 6px;
}
.chapter .table-wrap table { margin: 0; min-width: 100%; }
.chapter .table-wrap::-webkit-scrollbar { height: 8px; }
.chapter .table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ----- Context menu for highlights/notes ----- */
.ctx-menu {
  position: fixed; z-index: 100;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 4px; min-width: 200px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  font-size: 14px;
}
.ctx-menu[hidden] { display: none; }
.ctx-menu button {
  display: flex; align-items: center; gap: 10px;
  width: 100%; background: transparent; border: 0; color: var(--text);
  padding: 8px 10px; border-radius: 6px; cursor: pointer;
  font-size: 14px; text-align: left;
}
.ctx-menu button:hover { background: var(--panel-2); }
.ctx-menu button .icon, .ctx-menu button .icon svg { width: 16px; height: 16px; color: var(--muted); }
.ctx-menu button:hover .icon { color: var(--text); }
.ctx-menu .sep { height: 1px; background: var(--border); margin: 4px 0; }

/* ----- Modal ----- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.modal {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 10px; padding: 22px;
  width: min(520px, 92vw);
}
.modal h2 { margin-top: 0; }
.modal blockquote {
  margin: 8px 0 14px; padding: 10px 12px;
  background: var(--panel-2); border-left: 3px solid var(--mark-fg);
  border-radius: 6px; font-size: 14px;
}

/* ----- Notes view ----- */
.note-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 14px; margin: 10px 0;
}
.note-card .note-meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.note-card .note-meta .spacer { flex: 1; }
.note-card .note-text { margin: 8px 0; padding: 8px 10px; background: var(--mark-bg); border-radius: 4px; font-size: 14px; }
.note-card .note-body { font-size: 14px; margin: 8px 0 0; white-space: pre-wrap; }
.note-actions { display: flex; gap: 6px; }
.note-actions button { background: transparent; border: 0; padding: 4px; color: var(--muted); cursor: pointer; display: inline-flex; }
.note-actions button:hover { color: var(--text); }
.note-actions button svg { width: 14px; height: 14px; }

.kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin: 16px 0; }
.kpi .card .stat { font-size: 24px; }

@media (max-width: 800px) {
  body { display: block; }
  .sidebar {
    position: fixed; top: 0; left: -240px; height: 100vh;
    transition: left .2s; z-index: 12;
    box-shadow: 4px 0 24px rgba(0,0,0,0.0);
  }
  .sidebar.open { left: 0; box-shadow: 4px 0 24px rgba(0,0,0,0.3); }
  main { padding: 56px 16px 80px; max-width: 100%; }
  #menu-toggle { display: inline-flex; }
  .grid { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  main { padding: 56px 12px 80px; }
  h1 { font-size: 22px; }
  h2 { font-size: 18px; }
  .kpi-strip { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .kpi-strip .item { padding: 12px 14px; }
  .kpi-strip .value { font-size: 22px; }
  .btn { padding: 8px 12px; font-size: 13px; }
  .btn-row { gap: 6px; }
  /* Flashcard rating row: shrink buttons so all four fit */
  .rating-row { gap: 6px; }
  .rating-row .btn { max-width: none; padding: 9px 6px; font-size: 13px; min-width: 0; flex: 1 1 0; }
  /* Quiz options: slightly smaller */
  .option { font-size: 14px; padding: 11px 12px; }
  /* Chapter toolbar buttons: prevent breaking weirdly */
  .chapter .btn-row { gap: 6px; }
  /* Modal padding */
  .modal { padding: 18px; }
  /* Note cards: tighter */
  .note-card { padding: 12px; }
  /* Selection toolbar — bigger touch targets */
  .sel-toolbar button { padding: 10px 12px; font-size: 14px; min-height: 40px; }
}
