:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --text: #1d2129;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #5b56e0;
  --accent-ink: #ffffff;
  --ok: #1a9c5b;
  --ok-bg: #e6f6ee;
  --err: #d1394b;
  --err-bg: #fdecee;
  --again: #d1394b;
  --hard: #d9822b;
  --good: #1a9c5b;
  --easy: #2f74d0;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20, 22, 30, .08), 0 6px 20px rgba(20, 22, 30, .05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161c; --surface: #1d2027; --text: #e8eaee; --muted: #98a0ad;
    --border: #2c313b; --accent: #7d78f0; --ok-bg: #123024; --err-bg: #35191d;
    --shadow: 0 1px 3px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25);
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .7rem 1.2rem; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 1.15rem; }
.brand span { color: var(--muted); font-weight: 400; margin-left: .35rem; font-size: .85rem; }
.topbar nav { display: flex; align-items: center; gap: .3rem; margin-left: auto; flex-wrap: wrap; }
.topbar nav a { padding: .4rem .7rem; border-radius: 8px; color: var(--text); }
.topbar nav a:hover { background: var(--bg); text-decoration: none; }
.topbar nav a.active { background: var(--accent); color: var(--accent-ink); }
.logout { margin: 0 0 0 .3rem; }
.logout button { background: none; border: none; color: var(--muted); cursor: pointer; font: inherit; padding: .4rem .5rem; }
.logout button:hover { color: var(--err); }

/* Layout */
.container { max-width: 900px; margin: 0 auto; padding: 1.5rem 1.2rem 4rem; }
.container.narrow { max-width: 380px; }
.page-title { margin: .2rem 0 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
hr { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }

/* Flash */
.flash { padding: .7rem 1rem; border-radius: 10px; margin-bottom: 1rem; }
.flash.ok { background: var(--ok-bg); color: var(--ok); }
.flash.err { background: var(--err-bg); color: var(--err); }

/* Buttons */
.btn {
  display: inline-block; padding: .6rem 1rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  cursor: pointer; font: inherit; text-align: center; transition: .12s;
}
.btn:hover { text-decoration: none; border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.small { padding: .3rem .6rem; font-size: .85rem; border-radius: 8px; }
.btn.danger { color: var(--err); }
.btn.danger:hover { background: var(--err); color: #fff; border-color: var(--err); }
.btn.wide { width: 100%; }
.actions { display: flex; gap: .6rem; flex-wrap: wrap; margin: 1rem 0; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: .8rem; margin: 1rem 0 1.5rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; text-align: center; box-shadow: var(--shadow); }
.stat .num { display: block; font-size: 1.8rem; font-weight: 700; color: var(--accent); }
.stat .lbl { font-size: .8rem; color: var(--muted); }

/* Topic grid */
.topic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; }
.topic-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem; box-shadow: var(--shadow); color: var(--text); }
.topic-card:hover { text-decoration: none; border-color: var(--accent); }
.topic-name { font-weight: 600; font-size: 1.1rem; margin-bottom: .3rem; }
.topic-meta { font-size: .85rem; color: var(--muted); margin-bottom: .7rem; }
.bar { height: 6px; background: var(--bg); border-radius: 3px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--good); }

/* Study card */
.study-head { display: flex; align-items: baseline; justify-content: space-between; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); padding: 1.5rem; margin-top: 1rem; min-height: 320px;
  display: flex; flex-direction: column;
}
.card-top { display: flex; justify-content: space-between; align-items: center; min-height: 1.8rem; }
.badge { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.badge.new { color: var(--accent); font-weight: 600; }
.icon-btn { background: none; border: none; font-size: 1.5rem; cursor: pointer; line-height: 1; }
.q { font-size: 2.6rem; font-weight: 600; text-align: center; margin: auto 0; padding: 1rem 0; word-break: break-word; }
.a { text-align: center; }
.a-main { font-size: 2rem; font-weight: 600; }
.translit { color: var(--muted); font-size: 1.1rem; margin-top: .3rem; }
.notes { color: var(--muted); font-size: .95rem; margin-top: .5rem; font-style: italic; }
.controls { margin-top: auto; padding-top: 1rem; }
.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; }
.grade { padding: .7rem .3rem; border: none; border-radius: 10px; color: #fff; cursor: pointer;
  font: inherit; font-weight: 600; display: flex; flex-direction: column; align-items: center; }
.grade small { font-weight: 400; opacity: .85; font-size: .75rem; margin-top: .15rem; }
.grade.again { background: var(--again); }
.grade.hard { background: var(--hard); }
.grade.good { background: var(--good); }
.grade.easy { background: var(--easy); }
.grade:hover { filter: brightness(1.08); }
.key { font-size: .7rem; opacity: .6; border: 1px solid currentColor; border-radius: 4px; padding: 0 .3rem; margin-left: .3rem; }
.audio-msg { margin-top: 1rem; padding: .7rem .9rem; border-radius: 10px; font-size: .9rem;
  background: var(--err-bg); color: var(--err); text-align: left; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); padding: 2rem; text-align: center; margin-top: 1rem; }

/* Login */
.login-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 2rem; text-align: center; margin-top: 15vh; }
.login-logo { font-size: 2.5rem; font-weight: 700; color: var(--accent); }
.login-box input { width: 100%; padding: .7rem; margin: 1rem 0; border: 1px solid var(--border);
  border-radius: 10px; font: inherit; background: var(--bg); color: var(--text); }

/* Tables */
.table-wrap { overflow-x: auto; }
.grid { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); }
.grid th, .grid td { padding: .6rem .8rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
.grid th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); background: var(--bg); }
.grid tr:last-child td { border-bottom: none; }
.arm { font-size: 1.15rem; }
.err-rate { color: var(--err); font-weight: 600; }
.row-actions { display: flex; gap: .3rem; flex-wrap: wrap; align-items: center; }
.inline { display: inline; margin: 0; }
.edit-row td { background: var(--bg); }

/* Forms */
.form-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.5rem; display: flex; flex-direction: column; gap: .9rem; max-width: 560px; }
.form-card label { display: flex; flex-direction: column; gap: .3rem; font-weight: 500; }
.form-card input, .form-card textarea, .form-card select, .form-inline input, .form-inline select {
  padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 8px; font: inherit;
  background: var(--bg); color: var(--text); }
.arm-input { font-size: 1.2rem; }
.form-inline { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; padding: .5rem 0; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-top: 1.5rem; }
.crumbs { margin-bottom: .5rem; font-size: .9rem; }
.help { margin: 1rem 0; color: var(--muted); font-size: .9rem; }
.help code { background: var(--bg); padding: .1rem .3rem; border-radius: 4px; }
.audio-current { display: flex; align-items: center; gap: .7rem; flex-wrap: wrap; }
.chk { flex-direction: row !important; align-items: center; gap: .3rem; font-weight: 400 !important; }

.hidden { display: none !important; }

@media (max-width: 640px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .q { font-size: 2rem; }
  .topbar { gap: .6rem; }
}
