:root {
  --red: #c8102e;
  --red-dark: #9e0c24;
  --gold: #ffcd00;
  --gold-deep: #e0aa00;   /* yellow that still shows on light backgrounds */
  --ink: #1f1a17;         /* text on yellow */
  --accent-text: #9e0c24; /* red text on page backgrounds */
  --on-gold: #9e0c24;     /* red text on yellow fills (same in dark mode) */
  --bg: #fdf8f3;
  --surface: #ffffff;
  --text: #1f1a17;
  --muted: #6f6660;
  --border: #ece3da;
  --ok: #1a8a4a;
  --ok-bg: #e5f6ec;
  --bad: #c0392b;
  --bad-bg: #fdecea;
  --shadow: 0 2px 10px rgba(60, 30, 10, 0.08);
  --radius: 16px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17120f;
    --surface: #221b17;
    --text: #f4ede7;
    --muted: #b3a79e;
    --border: #3a2f28;
    --ok: #4cd287;
    --ok-bg: #173226;
    --bad: #ff7a6b;
    --bad-bg: #3a1c19;
    --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
    --accent-text: #ff7d8f;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--gold); color: var(--ink);
}
.brand { flex: 1; margin: 0; font-size: 1.25rem; letter-spacing: 0.3px; }
.brand span { color: var(--red); }
.icon-btn {
  background: transparent; border: 0; color: inherit; font-size: 1.4rem;
  width: 36px; height: 36px; border-radius: 50%; cursor: pointer;
}
.points-pill {
  background: var(--red); color: var(--gold); font-weight: 700;
  padding: 4px 12px; border-radius: 999px;
}

.banner { background: #6b4e00; color: #fff; text-align: center; font-size: 0.85rem; padding: 6px 16px; }

main {
  max-width: 560px; margin: 0 auto;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 32px);
}

h2 { margin: 0.2em 0 0.6em; }
h3 { margin: 0 0 0.6em; font-size: 1rem; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px; margin-bottom: 16px;
}

.btn {
  font: inherit; font-weight: 600; border-radius: 12px; border: 2px solid transparent;
  padding: 12px 18px; cursor: pointer; min-height: 48px;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--gold); color: var(--on-gold); }
.btn.primary:hover:not(:disabled) { background: var(--gold-deep); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.link-btn {
  background: none; border: 0; color: var(--accent-text); font: inherit; font-weight: 600;
  cursor: pointer; padding: 8px 0;
}
.link-btn.subtle { color: var(--muted); display: block; margin: 16px auto 0; }

.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin: 12px 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }

input {
  font: inherit; font-size: 1.15rem; width: 100%;
  padding: 12px 14px; border-radius: 12px;
  border: 2px solid var(--border); background: var(--bg); color: var(--text);
}
input:focus { outline: none; border-color: var(--gold-deep); }
label { display: block; font-weight: 600; margin-bottom: 8px; }

/* Login */
.hero { text-align: center; padding: 24px 8px; }
.hero-flag {
  width: 84px; height: 84px; margin: 0 auto 12px; border-radius: 22px;
  /* yellow field, three red stripes across the middle third (each 1/15 of the height) */
  background: linear-gradient(to bottom,
    #ffcd00 0 33.333%, #c8102e 0 40%, #ffcd00 0 46.667%,
    #c8102e 0 53.333%, #ffcd00 0 60%, #c8102e 0 66.667%, #ffcd00 0);
  box-shadow: var(--shadow);
}
.hero p { color: var(--muted); }
#login-form .btn { width: 100%; margin-top: 12px; }
.error { color: var(--bad); margin: 10px 0 0; }
.fine { color: var(--muted); font-size: 0.85rem; text-align: center; }

/* Home */
.greeting { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.notice {
  background: #fff6d6; color: #5c4600; border-radius: 12px; padding: 10px 14px;
  font-size: 0.9rem; margin-bottom: 14px;
}
@media (prefers-color-scheme: dark) { .notice { background: #3b3010; color: #ffe38a; } }

.level-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.level {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); color: var(--text); font: inherit; cursor: pointer;
}
.level:disabled { cursor: not-allowed; opacity: 0.55; }
.level-num {
  flex: none; width: 46px; height: 46px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 800; font-size: 1.1rem;
  background: var(--gold); color: var(--on-gold);
}
.level.passed .level-num { background: var(--ok); color: #fff; }
.level:disabled .level-num { background: var(--border); color: var(--muted); }
.level-body { flex: 1; min-width: 0; }
.level-title { font-weight: 700; }
.level-desc { color: var(--muted); font-size: 0.85rem; }
.level-meta { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }
.level-bar { height: 6px; border-radius: 3px; background: var(--border); margin-top: 6px; overflow: hidden; }
.level-bar > div { height: 100%; background: var(--red); }
.level.passed .level-bar > div { background: var(--ok); }

/* Game */
.game-head { display: flex; justify-content: space-between; color: var(--muted); font-size: 0.9rem; font-weight: 600; }
.progress { height: 8px; background: var(--border); border-radius: 4px; margin: 8px 0 16px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--gold-deep); transition: width 0.3s ease; }

.question { text-align: center; }
.mode-tag {
  display: inline-block; font-size: 0.75rem; font-weight: 700; text-transform: uppercase; letter-spacing: 1px;
  background: var(--bg); color: var(--accent-text); padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border);
}
.prompt { color: var(--muted); margin: 12px 0 4px; }
.target-word { font-size: clamp(2rem, 9vw, 2.8rem); font-weight: 800; line-height: 1.2; margin: 6px 0; word-break: break-word; }
.meaning { color: var(--muted); font-style: italic; margin-bottom: 8px; }
.mic.listening { background: var(--bad); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 10px rgba(192, 57, 43, 0.15); } }
.heard { min-height: 1.4em; margin: 8px 0 0; font-size: 1.05rem; }
.tries { color: var(--muted); font-size: 0.85rem; margin: 4px 0 0; }
.big-audio { font-size: 1.1rem; }

#type-form { margin-top: 8px; }
#type-input { text-align: center; }
#type-form .btn { width: 100%; margin-top: 10px; }
.vn-keys { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin-top: 10px; }
.vn-key {
  font: inherit; font-size: 1.05rem; min-width: 40px; height: 40px; padding: 0 8px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); cursor: pointer;
}
.vn-key.tone { color: var(--accent-text); font-weight: 700; }


.feedback { margin-top: 14px; padding: 12px; border-radius: 12px; font-weight: 600; }
.feedback.ok { background: var(--ok-bg); color: var(--ok); }
.feedback.bad { background: var(--bad-bg); color: var(--bad); }
.feedback .answer { display: block; font-size: 1.3rem; margin-top: 4px; color: var(--text); }
.game-actions .btn { flex: 1; }

/* Results */
.results { text-align: center; }
.score-ring {
  --pct: 0; width: 140px; height: 140px; margin: 0 auto 12px; border-radius: 50%;
  display: grid; place-items: center;
  background: conic-gradient(var(--ring, var(--gold-deep)) calc(var(--pct) * 1%), var(--border) 0);
}
.score-ring span {
  width: 112px; height: 112px; border-radius: 50%; background: var(--surface);
  display: grid; place-items: center; font-size: 1.8rem; font-weight: 800;
}
.points-earned { color: var(--accent-text); font-weight: 800; font-size: 1.2rem; }


.word-list { list-style: none; margin: 0; padding: 0; }
.word-list li {
  display: flex; justify-content: space-between; gap: 12px; align-items: center;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.word-list li:last-child { border-bottom: 0; }
.word-list .vi { font-weight: 700; }
.word-list .en { color: var(--muted); font-size: 0.9rem; text-align: right; }
.word-list button { background: none; border: 0; font-size: 1.1rem; cursor: pointer; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.stat .val { font-size: 1.6rem; font-weight: 800; }
.stat .lbl { color: var(--muted); font-size: 0.8rem; }
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table th, .table td { padding: 6px 4px; border-bottom: 1px solid var(--border); text-align: left; }
.table th { color: var(--muted); font-weight: 600; font-size: 0.8rem; }
.table td.num, .table th.num { text-align: right; }
.empty { color: var(--muted); font-size: 0.9rem; }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 20px); transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 0.9rem;
  max-width: calc(100% - 32px); box-shadow: var(--shadow); z-index: 20;
}
