:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1d2433;
  --muted: #6b7489;
  --line: #e3e7f0;
  --primary: #5b5bf0;
  --primary-2: #8a5cf6;
  --on-primary: #ffffff;
  --ok: #1fae6a;
  --ok-bg: #e3f7ec;
  --bad: #e5484d;
  --bad-bg: #fde8e8;
  --warn: #f59e0b;
  --gold: #f5b301;
  --radius: 18px;
  --shadow: 0 4px 16px rgba(30, 40, 80, .08);
}
html.dark {
  --bg: #12141c;
  --card: #1d2130;
  --text: #eef0f6;
  --muted: #9aa3b8;
  --line: #2c3245;
  --primary: #7b7bff;
  --primary-2: #a07bff;
  --ok: #3ecf8e;
  --ok-bg: #173527;
  --bad: #ff6b6f;
  --bad-bg: #3d1d20;
  --shadow: 0 4px 16px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px; line-height: 1.4;
  -webkit-user-select: none; user-select: none;
}
input { -webkit-user-select: text; user-select: text; }
#app { max-width: 520px; margin: 0 auto; padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); }

.screen { animation: fade .2s ease; }
.screen:not(.active) { display: none !important; }
.center { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; min-height: 70vh; }
.center.col { min-height: 0; padding-top: 8px; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

h1.title { font-size: 26px; margin: 8px 0 14px; }
h2 { margin: 6px 0; }
h3 { font-size: 15px; color: var(--muted); margin: 18px 0 8px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.muted { color: var(--muted); }
.big-emoji { font-size: 64px; line-height: 1.1; }

.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid var(--line); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* header */
.top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.badge { background: var(--card); border: 1px solid var(--line); border-radius: 99px; padding: 6px 12px; font-size: 14px; max-width: 65%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lang-switch { display: flex; background: var(--card); border: 1px solid var(--line); border-radius: 99px; padding: 3px; }
.lang-switch button { border: 0; background: none; color: var(--muted); font-weight: 700; padding: 6px 12px; border-radius: 99px; font-size: 14px; cursor: pointer; }
.lang-switch button.on { background: var(--primary); color: var(--on-primary); }

.alert { background: var(--bad-bg); color: var(--bad); border-radius: 12px; padding: 10px 12px; margin-bottom: 12px; font-size: 14px; }

.modes { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 6px 0 14px; }
.modes button { border: 2px solid var(--line); background: var(--card); color: var(--text); border-radius: 18px; padding: 12px 4px; cursor: pointer; text-align: center; box-shadow: var(--shadow); }
.modes button .ico { display: block; font-size: 32px; line-height: 1.2; }
.modes button b { display: block; font-size: 14px; margin-top: 4px; line-height: 1.2; }
.modes button small { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }
.modes button.on { border-color: var(--primary); background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: var(--on-primary); }
.modes button.on small { color: var(--on-primary); opacity: .85; }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.field { display: block; margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }
.field span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.field input { width: 100%; font-size: 17px; padding: 12px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--bg); color: var(--text); outline: none; }
.field input:focus { border-color: var(--primary); }
.field input.invalid { border-color: var(--bad); }

.grades { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.grades button { border: 2px solid var(--line); background: var(--card); color: var(--text); border-radius: 16px; padding: 12px 4px; font-size: 22px; font-weight: 800; cursor: pointer; }
.grades button small { display: block; font-size: 12px; font-weight: 600; color: var(--muted); }
.grades button.on { border-color: var(--primary); background: var(--primary); color: var(--on-primary); }
.grades button.on small { color: var(--on-primary); opacity: .85; }

.levels { display: grid; gap: 8px; }
.levels button { display: flex; align-items: center; gap: 12px; text-align: left; border: 2px solid var(--line); background: var(--card); color: var(--text); border-radius: 16px; padding: 12px 14px; cursor: pointer; }
.levels button .ico { font-size: 28px; }
.levels button b { display: block; font-size: 17px; }
.levels button span.meta { font-size: 13px; color: var(--muted); }
.levels button.on { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }

.btn { display: block; width: 100%; border: 0; border-radius: 16px; padding: 14px; font-size: 17px; font-weight: 700; cursor: pointer; margin-top: 10px; }
.btn.primary { background: linear-gradient(135deg, var(--primary), var(--primary-2)); color: var(--on-primary); }
.btn.big { padding: 17px; font-size: 19px; margin-top: 18px; }
.btn.ghost { background: var(--card); color: var(--primary); border: 1.5px solid var(--line); }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; }
.err { color: var(--bad); font-size: 14px; min-height: 18px; margin-top: 10px; }

/* quiz */
.quiz-top { display: flex; gap: 8px; justify-content: space-between; }
.pill { background: var(--card); border-radius: 99px; padding: 7px 14px; font-weight: 700; box-shadow: var(--shadow); font-variant-numeric: tabular-nums; }
.pill.timer.low { background: var(--bad); color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { transform: scale(1.06); } }
.timebar { height: 8px; border-radius: 99px; background: var(--line); margin: 12px 0 8px; overflow: hidden; }
.timebar div { height: 100%; width: 100%; background: linear-gradient(90deg, var(--ok), var(--warn)); transition: width 1s linear; }
.dots { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 12px; }
.dots i { flex: 1; min-width: 8px; height: 6px; border-radius: 99px; background: var(--line); }
.dots i.ok { background: var(--ok); }
.dots i.bad { background: var(--bad); }
.dots i.cur { background: var(--primary); }

.qcard { background: var(--card); border-radius: 22px; padding: 26px 18px; min-height: 150px; display: flex; align-items: center; justify-content: center; box-shadow: var(--shadow); text-align: center; }
.qtext { font-size: 22px; font-weight: 600; white-space: pre-line; word-break: break-word; }
.qtext.math { font-size: 36px; font-weight: 800; letter-spacing: .02em; }
.qtext.emoji { font-size: 30px; line-height: 1.5; }

.options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.options.three { grid-template-columns: 1fr 1fr 1fr; }
.options button { border: 2px solid var(--line); background: var(--card); color: var(--text); border-radius: 18px; padding: 18px 8px; font-size: 24px; font-weight: 800; cursor: pointer; min-height: 70px; word-break: break-word; }
.options button.text { font-size: 18px; }
.options button.pic { font-size: 40px; padding: 10px 8px; }
.qprompt { font-size: 17px; color: var(--muted); font-weight: 600; }
.qpic { font-size: 84px; line-height: 1.15; margin-top: 6px; }
.options button:active { transform: scale(.97); }
.options button.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.options button.bad { background: var(--bad); border-color: var(--bad); color: #fff; animation: shake .35s; }
.options button.dim { opacity: .45; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

.feedback { margin-top: 14px; border-radius: 18px; padding: 14px; text-align: center; }
.feedback.ok { background: var(--ok-bg); color: var(--ok); }
.feedback.bad { background: var(--bad-bg); color: var(--bad); }
.fb-title { font-size: 20px; font-weight: 800; }
.fb-sub { font-size: 17px; margin-top: 4px; color: var(--text); }
.fb-sub b { color: var(--ok); font-size: 22px; }
.feedback.ok .btn { display: none; }

/* result */
.r-score { font-size: 52px; font-weight: 900; background: linear-gradient(135deg, var(--primary), var(--primary-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.r-score small { font-size: 18px; color: var(--muted); -webkit-text-fill-color: var(--muted); }
.r-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: 100%; margin-top: 10px; }
.r-stats div { background: var(--card); border-radius: 16px; padding: 12px 6px; box-shadow: var(--shadow); }
.r-stats b { display: block; font-size: 20px; }
.r-stats span { font-size: 12px; color: var(--muted); }
.mistakes { margin-top: 18px; }
.mistake { background: var(--card); border-radius: 14px; padding: 12px; margin-top: 8px; border-left: 4px solid var(--bad); }
.mistake .mq { white-space: pre-line; font-weight: 600; }
.mistake .ma { font-size: 14px; margin-top: 4px; }
.mistake .ma .y { color: var(--bad); text-decoration: line-through; }
.mistake .ma .c { color: var(--ok); font-weight: 700; }

/* board */
.tabs { display: flex; background: var(--card); border-radius: 14px; padding: 4px; gap: 4px; margin-bottom: 10px; box-shadow: var(--shadow); }
.tabs button { flex: 1; border: 0; background: none; color: var(--muted); font-weight: 700; padding: 10px; border-radius: 10px; font-size: 15px; cursor: pointer; }
.tabs button.on { background: var(--primary); color: var(--on-primary); }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: none; }
.chips button { flex: none; border: 1.5px solid var(--line); background: var(--card); color: var(--text); border-radius: 99px; padding: 6px 12px; font-size: 14px; cursor: pointer; }
.chips button.on { border-color: var(--primary); color: var(--primary); font-weight: 700; }
.board { margin-top: 8px; }
.row { display: flex; align-items: center; gap: 10px; background: var(--card); border-radius: 14px; padding: 10px 12px; margin-bottom: 6px; }
.row.me { outline: 2px solid var(--primary); }
.row .rk { width: 32px; text-align: center; font-weight: 800; font-size: 18px; color: var(--muted); }
.row .nm { flex: 1; min-width: 0; }
.row .nm b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .nm span { font-size: 12px; color: var(--muted); }
.row .sc { font-weight: 900; font-size: 18px; color: var(--primary); }
.empty { text-align: center; color: var(--muted); padding: 30px 0; }
#b-me .row { margin: 8px 0; }
