:root {
  --bg: #f5f6fb;
  --card: #ffffff;
  --text: #171a2b;
  --muted: #646a85;
  --line: #e4e6f0;
  --primary: #4f46e5;
  --primary-2: #7c3aed;
  --primary-soft: #eef0ff;
  --ok: #16a34a;
  --ok-soft: #e7f7ec;
  --bad: #dc2626;
  --bad-soft: #fdecec;
  --warn: #d97706;
  --warn-soft: #fff4e0;
  --shadow: 0 1px 2px rgba(20, 24, 50, .06), 0 4px 16px rgba(20, 24, 50, .06);
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1020;
    --card: #181b30;
    --text: #eceefa;
    --muted: #9aa0bd;
    --line: #2a2e48;
    --primary: #8b85ff;
    --primary-2: #a78bfa;
    --primary-soft: #23264a;
    --ok: #34d399;
    --ok-soft: #12302a;
    --bad: #f87171;
    --bad-soft: #3a1c22;
    --warn: #fbbf24;
    --warn-soft: #3a2f14;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.hidden { display: none !important; }

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom));
  min-height: 100vh;
}
.app.no-tabbar { padding-bottom: calc(110px + env(safe-area-inset-bottom)); }
.loading { padding: 40vh 0; text-align: center; color: var(--muted); }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.card.accent { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: #fff; }
.card.accent .muted { color: rgba(255, 255, 255, .8); }
.card.warn { background: var(--warn-soft); box-shadow: none; }
.card.soft { background: var(--primary-soft); box-shadow: none; }

.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.stack > * + * { margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 12px; padding: 13px 16px; min-height: 48px;
  background: var(--primary); color: #fff; font-weight: 600; cursor: pointer;
  width: 100%; text-align: center;
}
.btn:active { transform: scale(.98); }
.btn[disabled] { opacity: .55; cursor: default; }
.btn.secondary { background: var(--primary-soft); color: var(--primary); }
.btn.ghost { background: transparent; color: var(--primary); border: 1.5px solid var(--line); }
.btn.white { background: #fff; color: var(--primary); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .9rem; width: auto; border-radius: 10px; }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.link-btn { background: none; border: 0; color: var(--primary); padding: 0; cursor: pointer; font-weight: 600; }

.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; gap: 8px; }
.topbar .who { font-size: .9rem; color: var(--muted); }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 10px; border-radius: 999px; font-size: .8rem; font-weight: 600;
  background: var(--primary-soft); color: var(--primary);
}
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.grey { background: var(--line); color: var(--muted); }

.stat { background: var(--card); border-radius: 14px; padding: 12px; box-shadow: var(--shadow); text-align: center; }
.stat b { display: block; font-size: 1.35rem; }
.stat span { font-size: .75rem; color: var(--muted); }

/* login */
.login { text-align: center; padding-top: 12vh; }
.logo { width: 84px; height: 84px; border-radius: 22px; box-shadow: var(--shadow); }
.nick-list { display: grid; gap: 12px; margin-top: 24px; }
.nick-btn { font-size: 1.15rem; min-height: 64px; }

/* test */
.qhead { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.qhead .back { font-size: 1.4rem; background: none; border: 0; padding: 4px 8px 4px 0; cursor: pointer; color: var(--text); }
.progress { flex: 1; height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: linear-gradient(90deg, var(--primary), var(--primary-2)); border-radius: 99px; transition: width .3s; }
.qcount { font-weight: 700; font-size: .9rem; min-width: 46px; text-align: right; }
.dots { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.dots button { width: 28px; height: 28px; border-radius: 50%; border: 1.5px solid var(--line); background: var(--card); font-size: .75rem; font-weight: 700; cursor: pointer; padding: 0; color: var(--muted); }
.dots button.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.dots button.bad { background: var(--bad); border-color: var(--bad); color: #fff; }
.dots button.cur { border-color: var(--primary); color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); }
.instruction { font-size: .85rem; color: var(--muted); margin-bottom: 8px; }
.passage {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; margin-bottom: 12px; font-size: .95rem; line-height: 1.6;
  white-space: pre-line; max-height: 42vh; overflow-y: auto;
}
.stem { font-size: 1.05rem; font-weight: 600; margin-bottom: 14px; white-space: pre-line; }
.blank { display: inline-block; min-width: 54px; border-bottom: 2px solid var(--primary); margin: 0 3px; vertical-align: baseline; height: 1em; }
.options { display: grid; gap: 10px; }
.opt {
  display: flex; gap: 12px; align-items: flex-start; text-align: left;
  width: 100%; padding: 12px 14px; border-radius: 14px; cursor: pointer;
  border: 1.5px solid var(--line); background: var(--card); line-height: 1.45;
}
.opt .letter {
  flex: 0 0 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  background: var(--primary-soft); color: var(--primary); font-weight: 700; font-size: .85rem;
}
.opt:disabled { cursor: default; }
.opt.correct { border-color: var(--ok); background: var(--ok-soft); }
.opt.correct .letter { background: var(--ok); color: #fff; }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt.wrong .letter { background: var(--bad); color: #fff; }
.opt.dim { opacity: .6; }
.explain { margin-top: 14px; padding: 12px 14px; border-radius: 12px; background: var(--primary-soft); font-size: .95rem; }
.explain b { display: block; margin-bottom: 2px; }
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.bottombar .btn { max-width: 608px; margin: 0 auto; display: flex; }

/* result */
.score-ring { width: 150px; height: 150px; margin: 6px auto 10px; position: relative; }
.score-ring svg { transform: rotate(-90deg); }
.score-ring .val { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; }
.score-ring .val b { font-size: 2.2rem; line-height: 1; }
.score-ring .val span { display: block; font-size: .85rem; color: var(--muted); }
.review { border-top: 1px solid var(--line); padding: 12px 0; }
.review:first-child { border-top: 0; }
.review summary { cursor: pointer; list-style: none; display: flex; gap: 10px; align-items: flex-start; }
.review summary::-webkit-details-marker { display: none; }
.review .mark { flex: 0 0 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-size: .8rem; font-weight: 700; }
.review .mark.ok { background: var(--ok); }
.review .mark.bad { background: var(--bad); }
.review .body { margin-top: 10px; font-size: .95rem; }
.review .ans { margin: 6px 0; }

/* lists */
.list-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); }
.list-item:first-child { border-top: 0; }
.score-badge { font-weight: 800; font-size: 1.05rem; padding: 6px 10px; border-radius: 10px; min-width: 58px; text-align: center; }
.score-badge.hi { background: var(--ok-soft); color: var(--ok); }
.score-badge.mid { background: var(--warn-soft); color: var(--warn); }
.score-badge.lo { background: var(--bad-soft); color: var(--bad); }

.bar { height: 8px; background: var(--bad-soft); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--ok); border-radius: 99px; }
.topic-row { padding: 12px 0; border-top: 1px solid var(--line); }
.topic-row:first-child { border-top: 0; }

.trend { display: flex; align-items: flex-end; gap: 6px; height: 90px; padding-top: 6px; }
.trend i { flex: 1; background: linear-gradient(to top, var(--primary), var(--primary-2)); border-radius: 6px 6px 2px 2px; min-height: 4px; position: relative; }
.trend i span { position: absolute; top: -18px; left: 50%; transform: translateX(-50%); font-size: .65rem; color: var(--muted); font-style: normal; }

/* duel */
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--line); margin-right: 4px; vertical-align: 1px; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.players { display: flex; align-items: stretch; gap: 8px; margin-bottom: 12px; }
.player { flex: 1; background: var(--card); border-radius: 14px; padding: 10px 12px; box-shadow: var(--shadow); text-align: center; }
.player.me { outline: 2px solid var(--primary); }
.player b { display: block; font-size: 1.6rem; line-height: 1.2; }
.vs { align-self: center; font-weight: 800; color: var(--muted); font-size: .8rem; }
.timer { height: 8px; background: var(--line); border-radius: 99px; overflow: hidden; }
.timer > i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, var(--warn), var(--primary)); border-radius: 99px; transition: width .25s linear; }
.opt.picked { border-color: var(--primary); background: var(--primary-soft); }
.opt.picked .letter { background: var(--primary); color: #fff; }
.opt { position: relative; }
.opp-badge { position: absolute; right: 10px; top: -9px; font-size: .7rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; background: var(--warn); color: #fff; }

/* install help */
.steps { margin: 8px 0 0; padding-left: 20px; }
.steps li { margin-bottom: 6px; }
.share-ico { display: inline-block; width: 18px; height: 18px; vertical-align: -3px; }

/* tabbar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
  display: flex; justify-content: center;
}
.tabbar[hidden] { display: none; }
.tabbar a { flex: 1; max-width: 213px; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: .75rem; color: var(--muted); gap: 2px; }
.tabbar a.on { color: var(--primary); font-weight: 700; }
.tabbar .ti { font-size: 1.3rem; line-height: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .9rem; z-index: 20;
  max-width: calc(100% - 32px); box-shadow: var(--shadow);
}

/* admin */
.admin .app { max-width: 980px; padding-bottom: 40px; }
.admin-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.field { display: flex; gap: 8px; margin-top: 8px; }
.field input { flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--bg); }
.checks { display: flex; flex-wrap: wrap; gap: 8px 16px; margin: 8px 0 12px; }
.checks label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.checks input { width: 18px; height: 18px; accent-color: var(--primary); }
.actions { display: grid; gap: 10px; }
.result-line { padding: 10px 0; border-top: 1px solid var(--line); font-size: .92rem; }
.result-line:first-child { border-top: 0; }
.tabs { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.tabs button { border: 1.5px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 14px; cursor: pointer; font-weight: 600; }
.tabs button.on { background: var(--primary); border-color: var(--primary); color: #fff; }
table.t { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.t th, table.t td { text-align: left; padding: 8px 6px; border-top: 1px solid var(--line); }
table.t th { color: var(--muted); font-weight: 600; border-top: 0; }
.modal { position: fixed; inset: 0; background: rgba(10, 12, 30, .5); z-index: 30; display: flex; align-items: flex-end; justify-content: center; }
.modal > .sheet { background: var(--bg); width: 100%; max-width: 720px; max-height: 90vh; overflow-y: auto; border-radius: 20px 20px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
@media (min-width: 720px) { .modal { align-items: center; } .modal > .sheet { border-radius: 20px; } }
