:root {
  --bg: #eef2f9;
  --bg-grad-1: #f5f7fb;
  --bg-grad-2: #e6edf8;
  --surface: #ffffff;
  --surface-alt: #f1f5fc;
  --border: #dce5f2;
  --text: #172033;
  --muted: #667085;
  --primary: #2f6fed;
  --primary-dark: #214fbb;
  --success: #1f9d6a;
  --warning: #e59a25;
  --danger: #d94a4a;
  --purple: #8b5cf6;
  --cyan: #0ea5a3;
  --radius: 16px;
  --shadow: 0 6px 22px rgba(23, 32, 51, 0.08);
  --shadow-lg: 0 12px 40px rgba(23, 32, 51, 0.16);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: linear-gradient(160deg, var(--bg-grad-1), var(--bg-grad-2));
  color: var(--text);
  font-family: -apple-system, "SF Pro Text", "Segoe UI Variable", "Segoe UI",
    system-ui, Roboto, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body { overscroll-behavior-y: none; }

#app {
  max-width: 1120px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) calc(14px + var(--safe-right))
    calc(20px + var(--safe-bottom)) calc(14px + var(--safe-left));
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- generic ---------- */
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
h1, h2, h3 { margin: 0; }

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

button {
  font-family: inherit;
  font-size: 16px;
  font-weight: 650;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s ease, border-color 0.15s ease;
  min-height: 48px;
  user-select: none;
}
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.5; cursor: default; }

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:active { background: var(--primary-dark); }
.btn-danger { background: #fdecec; border-color: #f3b1b1; color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-block { width: 100%; }

input, select {
  font-family: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  width: 100%;
  min-height: 48px;
}
select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  padding-right: 40px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='none' stroke='%23667085' stroke-width='2' stroke-linecap='round' stroke-linejoin='round' d='M1 1.5l5 5 5-5'/></svg>");
  background-repeat: no-repeat;
  background-position: right 14px center;
  background-size: 12px 8px;
  cursor: pointer;
  text-overflow: ellipsis;
}
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.12); }
input::placeholder { color: #9aa6b8; }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; }

/* ---------- header ---------- */
.appbar {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 14px;
  position: sticky;
  top: calc(6px + var(--safe-top));
  z-index: 20;
}
.rank-badge {
  width: 46px; height: 46px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 900; font-size: 18px;
  overflow: hidden; flex: none;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.4);
}
.rank-badge img { width: 100%; height: 100%; object-fit: contain; }
.appbar-info { flex: 1; min-width: 0; }
.appbar-rank { font-weight: 800; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar-sub { font-size: 12px; color: var(--muted); }
.xp-bar { height: 8px; background: #e7edf6; border-radius: 5px; margin-top: 5px; overflow: hidden; }
.xp-bar > span { display: block; height: 100%; background: var(--primary); border-radius: 5px; transition: width 0.4s ease; }
.appbar-metrics { display: flex; gap: 14px; flex: none; }
.metric { text-align: center; }
.metric b { display: block; font-size: 16px; font-weight: 800; }
.metric span { font-size: 11px; color: var(--muted); }
.iconbtn {
  width: 44px; height: 44px; min-height: 44px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; border-radius: 12px; flex: none;
}

/* ---------- topbar (back + title) ---------- */
.topbar { display: flex; align-items: center; gap: 10px; }
.topbar h2 { font-size: 20px; font-weight: 800; }
.back { font-size: 15px; padding: 10px 14px; }

/* ---------- login ---------- */
.login-wrap {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 18px; text-align: center;
}
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.login-logo { font-size: 52px; }
.login-title { font-size: 24px; font-weight: 800; }
.error-text { color: var(--danger); font-weight: 700; font-size: 14px; min-height: 18px; }

/* ---------- menu ---------- */
.menu-title { font-size: 26px; font-weight: 850; }
.menu-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.tile {
  border-radius: var(--radius);
  padding: 18px 16px;
  min-height: 104px;
  display: flex; flex-direction: column; justify-content: space-between;
  color: #fff; cursor: pointer; border: none; text-align: left;
  box-shadow: var(--shadow);
}
.tile:active { transform: scale(0.98); }
.tile .tile-icon { font-size: 26px; }
.tile .tile-name { font-size: 17px; font-weight: 800; margin-top: 8px; }
.tile .tile-desc { font-size: 12px; opacity: 0.9; font-weight: 500; }
.tile.verbs { background: linear-gradient(135deg, #2f6fed, #214fbb); }
.tile.study { background: linear-gradient(135deg, #0ea5a3, #0b7e7c); }
.tile.input { background: linear-gradient(135deg, #1f9d6a, #14784f); }
.tile.choice { background: linear-gradient(135deg, #8b5cf6, #6d3ed8); }
.tile.matching { background: linear-gradient(135deg, #f0934d, #e2722b); }
.tile.challenge { background: linear-gradient(135deg, #ec5d8b, #c43f6c); }
.tile.stats { background: linear-gradient(135deg, #e59a25, #b87514); }
.tile.admin { background: linear-gradient(135deg, #64748b, #475569); }

/* ---------- settings panel ---------- */
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.settings-grid .full { grid-column: 1 / -1; }

/* ---------- timer ---------- */
.timer { margin: 4px 0; }
.timer-label { font-weight: 800; font-size: 15px; margin-bottom: 4px; }
.timer-track { height: 10px; background: #e7edf6; border-radius: 5px; overflow: hidden; }
.timer-track > span { display: block; height: 100%; border-radius: 5px; width: 100%; }

/* ---------- question / play ---------- */
.question-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 16px;
  text-align: center;
}
.hint-label { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 700; }
.hint-text { font-size: 28px; font-weight: 850; margin: 6px 0 14px; word-break: break-word; }
.progress-pill { display: inline-block; font-size: 12px; font-weight: 700; color: var(--muted); background: var(--surface-alt); border-radius: 999px; padding: 4px 12px; }

.answer-inputs { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }

/* choice columns */
.choice-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 10px; }
.choice-col-title { font-size: 14px; font-weight: 800; text-align: center; margin-bottom: 8px; color: var(--muted); }
.choice-opt {
  width: 100%; margin-bottom: 10px; text-align: center; font-size: 16px;
  min-height: 56px; border-radius: 14px; white-space: normal; word-break: break-word;
}
.choice-opt.selected { background: #eaf1ff; border: 2px solid var(--primary); }
.choice-opt.correct { background: #e7f7ef; border: 2px solid var(--success); color: #0f7a4f; }
.choice-opt.wrong { background: #fdecec; border: 2px solid var(--danger); color: var(--danger); }

/* matching board */
.match-board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.match-col-title { font-size: 12px; font-weight: 800; text-align: center; color: var(--muted); }
.match-card {
  width: 100%; margin-top: 8px; min-height: 56px; padding: 10px 6px;
  font-size: 15px; border-radius: 12px; line-height: 1.2; white-space: normal; word-break: break-word;
}
.match-card .tr { display: block; font-size: 11px; color: var(--muted); font-weight: 500; margin-top: 2px; }
.match-card.selected { background: #eaf1ff; border: 2px solid var(--primary); }
.match-card.matched { background: #e7f7ef; border: 2px solid var(--success); opacity: 0.65; }
.match-card.flash-wrong { background: #fdecec; border: 2px solid var(--danger); }

/* feedback */
.feedback { border-radius: 14px; padding: 14px; margin-top: 12px; border-left: 5px solid var(--primary); background: var(--surface); border: 1px solid var(--border); }
.feedback.correct { border-left-color: var(--success); }
.feedback.wrong { border-left-color: var(--danger); }
.feedback.warning { border-left-color: var(--warning); }
.feedback h3 { font-size: 17px; margin-bottom: 6px; }
.feedback .forms { font-size: 16px; font-weight: 700; }
.feedback ul { margin: 8px 0 0; padding-left: 18px; }
.feedback li { margin: 3px 0; }

/* study / verb details */
.verb-detail-title { font-size: 24px; font-weight: 850; text-align: center; margin-bottom: 12px; }
.form-table { width: 100%; border-collapse: collapse; }
.form-table td { padding: 10px 8px; border-bottom: 1px solid #eef2f8; }
.form-table .lbl { color: var(--muted); font-size: 13px; white-space: nowrap; }
.form-table .val { font-size: 18px; font-weight: 750; }
.form-table .tr { color: var(--muted); font-size: 14px; text-align: right; }
.detail-block { margin-top: 14px; font-size: 15px; }
.detail-block b { color: var(--muted); font-weight: 700; }
.study-nav { display: flex; gap: 12px; margin-top: 14px; }
.study-nav button { flex: 1; }

/* verbs list */
.search-box { margin-bottom: 12px; }
.verb-row {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  border-bottom: 1px solid #eef2f8; cursor: pointer;
}
.verb-row:active { background: var(--surface-alt); }
.verb-row .forms { flex: 1; min-width: 0; }
.verb-row .forms b { font-size: 16px; }
.verb-row .forms span { color: var(--muted); font-size: 13px; }
.verb-row .acc { font-size: 13px; font-weight: 800; white-space: nowrap; }
.verb-list-card { padding: 4px 0; max-height: 60vh; overflow-y: auto; }

/* stats table */
.stat-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.stat-box { background: var(--surface-alt); border-radius: 12px; padding: 12px; text-align: center; }
.stat-box b { display: block; font-size: 22px; font-weight: 850; }
.stat-box span { font-size: 12px; color: var(--muted); }
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th, table.data td { padding: 8px 10px; text-align: left; white-space: nowrap; }
table.data thead th { background: var(--surface-alt); font-weight: 700; font-size: 12px; color: var(--muted); position: sticky; top: 0; }
table.data tbody tr { border-bottom: 1px solid #eef2f8; }

/* admin */
.user-row { display: flex; align-items: center; gap: 10px; padding: 12px 0; border-bottom: 1px solid #eef2f8; }
.user-row .uname { font-weight: 800; flex: 1; }
.tag { font-size: 11px; font-weight: 800; padding: 2px 8px; border-radius: 999px; background: var(--surface-alt); color: var(--muted); }
.tag.admin { background: #e7eefc; color: var(--primary); }

/* stage break / result */
.stage-break, .result-screen { text-align: center; display: flex; flex-direction: column; gap: 16px; align-items: center; padding: 18px; }
.big-emoji { font-size: 56px; }
.result-art { font-family: "SF Mono", Menlo, Consolas, monospace; font-size: 12px; line-height: 1.25; white-space: pre; text-align: left; background: var(--surface-alt); border-radius: 12px; padding: 12px; overflow-x: auto; }
.result-stage-line { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid #eef2f8; font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%);
  background: #172033; color: #fff; padding: 12px 20px; border-radius: 999px; font-weight: 700;
  box-shadow: var(--shadow-lg); z-index: 100; font-size: 14px; max-width: 90vw; text-align: center;
  opacity: 0; transition: opacity 0.2s ease, transform 0.2s ease; pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

.xp-float {
  position: fixed; left: 50%; top: 30%; transform: translateX(-50%);
  font-size: 32px; font-weight: 900; color: var(--success); z-index: 90; pointer-events: none;
  animation: floatUp 1s ease forwards;
}
@keyframes floatUp { from { opacity: 0; transform: transl(-50%, 10px) scale(0.8);} 30% {opacity:1;} to { opacity: 0; transform: translateX(-50%) translateY(-50px) scale(1.1); } }

/* ---------- tablet / desktop ---------- */
@media (min-width: 720px) {
  #app { padding-left: 24px; padding-right: 24px; gap: 18px; }
  .menu-grid { grid-template-columns: repeat(3, 1fr); gap: 16px; }
  .tile { min-height: 130px; }
  .hint-text { font-size: 34px; }
  .menu-title { font-size: 32px; }
  .settings-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (min-width: 1000px) {
  .menu-grid { grid-template-columns: repeat(4, 1fr); }
  .play-2col { display: grid; grid-template-columns: 1.3fr 1fr; gap: 18px; align-items: start; }
}
