:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --border: #e5e7eb;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --surface: #1b1f27;
    --text: #f1f3f7;
    --muted: #9aa1ae;
    --accent: #818cf8;
    --accent-soft: #262b3a;
    --border: #2c313d;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar h1 {
  font-size: 1.25rem;
  margin: 0;
}

.mode-tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.25rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.mode-tab {
  border: none;
  background: transparent;
  border-radius: 999px;
  padding: 0.45rem 1rem;
  font: inherit;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}

.mode-tab:hover {
  color: var(--text);
}

.mode-tab.active {
  background: var(--accent);
  color: white;
}

.account-widget {
  position: relative;
}

.account-panel {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  width: 260px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 1rem;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.account-tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.2rem;
  background: var(--bg);
  border-radius: 8px;
}

.account-tab {
  flex: 1;
  border: none;
  background: transparent;
  border-radius: 6px;
  padding: 0.35rem;
  font: inherit;
  font-size: 0.8rem;
  color: var(--muted);
  cursor: pointer;
}

.account-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

#auth-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#auth-form input {
  font: inherit;
  padding: 0.5rem 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

#auth-submit {
  background: var(--accent);
  color: white;
  border: none;
}

.account-profile {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.account-profile p {
  margin: 0;
  font-size: 0.9rem;
}

.account-error {
  font-size: 0.8rem;
  color: #dc2626;
  margin: 0;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1.5rem;
  padding: 0.85rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.toolbar-group label {
  font-size: 0.85rem;
  color: var(--muted);
}

.stat-chip {
  font-size: 0.8rem;
  color: var(--muted);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.ghost-btn {
  background: transparent;
}

select, button {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 0.5rem 0.75rem;
  cursor: pointer;
}

button:hover, select:hover {
  border-color: var(--accent);
}

#flip-btn {
  background: var(--accent);
  color: white;
  border: none;
}

#flip-btn:hover {
  opacity: 0.9;
}

.danger-btn {
  background: #dc2626;
  color: white;
  border: none;
}

.danger-btn:hover {
  background: #b91c1c;
}

@media (prefers-color-scheme: dark) {
  .danger-btn {
    background: #ef4444;
  }
  .danger-btn:hover {
    background: #dc2626;
  }
}

.speak-btn {
  font: inherit;
  font-size: 0.85rem;
  border: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--text);
  border-radius: 999px;
  padding: 0.3rem 0.7rem;
  cursor: pointer;
}

.speak-btn:hover {
  border-color: var(--accent);
}

.card-front .speak-btn {
  position: absolute;
  top: 0.9rem;
  right: 1.1rem;
}

.speak-row {
  display: flex;
  gap: 0.5rem;
}

.switch {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  user-select: none;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-track {
  position: relative;
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: var(--border);
  transition: background 0.2s ease;
  flex-shrink: 0;
}

.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s ease;
}

.switch input:checked + .switch-track {
  background: var(--accent);
}

.switch input:checked + .switch-track .switch-thumb {
  transform: translateX(16px);
}

.switch input:focus-visible + .switch-track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.switch-label {
  font-size: 0.85rem;
  color: var(--muted);
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem 1rem;
  gap: 1.25rem;
}

.progress-wrap {
  width: 100%;
  max-width: 640px;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.progress-bar {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

#progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.2s ease;
}

#progress-label {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

.stage {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 640px;
}

.nav-btn {
  font-size: 1.5rem;
  line-height: 1;
  padding: 0.5rem 0.9rem;
  border-radius: 999px;
}

.card {
  flex: 1;
  height: 380px;
  perspective: 1200px;
  cursor: pointer;
  outline: none;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.5s;
  transform-style: preserve-3d;
}

.card.flipped .card-inner {
  transform: rotateY(180deg);
}

.card.eliminating {
  transition: transform 0.25s ease, opacity 0.25s ease;
  transform: scale(0.9);
  opacity: 0.25;
}

.card-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 2rem;
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
  backface-visibility: hidden;
  text-align: center;
}

.card-back {
  transform: rotateY(180deg);
}

.face-label {
  position: absolute;
  top: 1rem;
  left: 1.25rem;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.hanzi {
  font-size: 3rem;
  font-weight: 700;
}

.pinyin {
  font-size: 1.1rem;
  color: var(--muted);
}

.hint {
  position: absolute;
  bottom: 1rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.meaning {
  font-size: 1.35rem;
  font-weight: 600;
  max-width: 26rem;
}

.example-block {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  max-width: 26rem;
  margin-top: 0.25rem;
}

.example {
  font-size: 0.95rem;
  max-width: 26rem;
}

.example-cn {
  color: var(--text);
}

.example-en {
  color: var(--muted);
  font-style: italic;
}

.bottom-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  justify-content: center;
}

#card-index-label {
  font-size: 0.85rem;
  color: var(--muted);
}

.study-complete {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 640px;
  padding: 3rem 2rem;
  text-align: center;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.study-complete p {
  font-size: 1.1rem;
  margin: 0;
}

#study-view,
#match-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  width: 100%;
}

.match-intro {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 0;
}

.match-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  width: 100%;
  max-width: 640px;
}

.match-col-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 0.6rem;
  text-align: center;
}

.match-tiles {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.match-tile {
  font: inherit;
  padding: 0.65rem 0.9rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-align: center;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease, opacity 0.2s ease;
}

.match-tile:hover:not(.matched) {
  border-color: var(--accent);
}

.match-tile.selected {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.match-tile.matched {
  background: transparent;
  border-color: transparent;
  color: transparent;
  opacity: 0;
  pointer-events: none;
}

.match-tile.wrong {
  background: #fee2e2;
  border-color: #dc2626;
  animation: shake 0.3s ease;
}

@media (prefers-color-scheme: dark) {
  .match-tile.wrong {
    background: #3f1d1d;
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.match-complete {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 2rem;
  text-align: center;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.match-complete p {
  font-size: 1.1rem;
  margin: 0;
}

.percentile-note {
  font-size: 0.95rem !important;
  color: var(--accent);
  font-weight: 600;
}

.match-complete button {
  background: var(--accent);
  color: white;
  border: none;
}

.report-signed-out {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 3rem 2rem;
  text-align: center;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 640px;
}

.report-content {
  display: flex;
  flex-direction: column;
  gap: 1.75rem;
  width: 100%;
  max-width: 720px;
}

.report-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
}

.report-stat-tile {
  background: var(--surface);
  border-radius: 12px;
  padding: 1rem;
  box-shadow: var(--shadow);
  text-align: center;
}

.report-stat-tile .value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.report-stat-tile .label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 0.25rem;
}

.report-block h2 {
  font-size: 1rem;
  margin: 0 0 0.75rem;
}

.report-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.admin-activity-header {
  margin-top: 1.5rem;
}

.admin-activity-header h3 {
  margin: 0;
  font-size: 0.9rem;
}

.report-block-header h2 {
  margin: 0;
}

.report-block-header select {
  font-size: 0.85rem;
  padding: 0.35rem 0.6rem;
}

.report-block-note {
  font-size: 0.8rem;
  color: var(--muted);
  margin: -0.4rem 0 0.75rem;
}

.host-toggle-btn {
  font-size: 0.8rem;
  padding: 0.3rem 0.7rem;
}

.host-toggle-btn.is-on {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

.footer-stats {
  margin-top: 0.35rem;
  font-size: 0.75rem;
}

/* ---------- assessment ---------- */

#assessment-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  width: 100%;
}

.assessment-signed-out {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 3rem 2rem;
  text-align: center;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 640px;
}

.assessment-home {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  width: 100%;
  max-width: 640px;
  justify-content: center;
}

.assessment-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 1.5rem;
  width: 100%;
  max-width: 420px;
  flex: 1 1 260px;
}

.assessment-card h2 {
  margin: 0 0 0.75rem;
  font-size: 1.05rem;
}

.assessment-form {
  display: flex;
  gap: 0.6rem;
}

.assessment-form input,
.assessment-form select {
  flex: 1;
  font: inherit;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.assessment-form button {
  background: var(--accent);
  color: white;
  border: none;
  padding: 0.55rem 1.1rem;
}

.assessment-form-grid {
  flex-direction: column;
  align-items: stretch;
}

.assessment-form-grid label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.85rem;
}

.host-play-check {
  flex-direction: row !important;
  align-items: center;
  gap: 0.5rem !important;
}

#host-new-btn {
  background: var(--accent);
  color: white;
  border: none;
  width: 100%;
  padding: 0.6rem;
}

.assessment-error {
  color: #dc2626;
  font-size: 0.8rem;
  margin: 0.6rem 0 0;
}

.lobby-code-block {
  text-align: center;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 2rem;
  width: 100%;
  max-width: 420px;
}

.lobby-code-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.lobby-code {
  font-size: 2.5rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  font-variant-numeric: tabular-nums;
  margin: 0.4rem 0;
}

.assessment-lobby {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 420px;
}

.assessment-lobby-actions {
  display: flex;
  gap: 0.75rem;
}

#lobby-start-btn,
#host-again-btn {
  background: var(--accent);
  color: white;
  border: none;
}

.assessment-running {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 640px;
}

.running-top {
  display: flex;
  gap: 0.75rem;
}

#running-timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.assessment-question {
  width: 100%;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  text-align: center;
}

.question-prompt {
  font-size: 1.5rem;
  font-weight: 600;
  max-width: 30rem;
  line-height: 1.5;
}

.no-copy {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

.question-prompt .blank {
  display: inline-block;
  min-width: 2.4em;
  border-bottom: 2px solid var(--accent);
}

.question-prompt .meaning {
  display: block;
  font-size: 1.1rem;
  color: var(--muted);
  font-weight: 500;
  margin-bottom: 0.4rem;
}

.question-prompt .en-sentence {
  display: block;
  font-size: 0.95rem;
  color: var(--muted);
  font-style: italic;
  font-weight: 400;
  margin-top: 0.4rem;
}

#answer-input {
  text-align: center;
  font-size: 1.1rem;
}

.answer-feedback {
  font-weight: 600;
  margin: 0;
}

.answer-feedback.correct {
  color: #16a34a;
}

.answer-feedback.wrong {
  color: #dc2626;
}

.podium {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 1rem;
  width: 100%;
  max-width: 480px;
}

.podium-place {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  flex: 1;
}

.podium-name {
  font-weight: 600;
  font-size: 0.9rem;
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.podium-score {
  font-size: 0.8rem;
  color: var(--muted);
}

.podium-bar {
  width: 100%;
  border-radius: 10px 10px 0 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 0.5rem;
  font-size: 1.5rem;
}

.podium-place.gold .podium-bar {
  height: 110px;
  background: linear-gradient(180deg, #fde68a, #f59e0b);
}

.podium-place.silver .podium-bar {
  height: 80px;
  background: linear-gradient(180deg, #e5e7eb, #9ca3af);
}

.podium-place.bronze .podium-bar {
  height: 60px;
  background: linear-gradient(180deg, #fbc79a, #c2703d);
}

.report-table-wrap {
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow-x: auto;
}

.report-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.report-table th,
.report-table td {
  text-align: left;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.report-table th {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.report-table tbody tr:last-child td {
  border-bottom: none;
}

.report-table tr.report-me {
  background: var(--accent-soft);
}

.report-table td:nth-child(3),
.report-table td:nth-child(4),
.report-table td:nth-child(5) {
  font-variant-numeric: tabular-nums;
}

footer {
  text-align: center;
  padding: 1rem;
  font-size: 0.8rem;
  color: var(--muted);
}

@media (max-width: 480px) {
  .card { height: 420px; }
  .hanzi { font-size: 2.25rem; }
  .meaning { font-size: 1.15rem; }
  .example { font-size: 0.85rem; }
  .match-columns { grid-template-columns: 1fr; gap: 2rem; }

  .account-panel {
    position: fixed;
    top: auto;
    left: 1rem;
    right: 1rem;
    width: auto;
  }
}

/* ---------- paywall ---------- */

.paywall-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(15, 23, 42, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  overflow-y: auto;
}

.paywall-card {
  background: var(--surface);
  color: var(--text);
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-width: 480px;
  width: 100%;
  padding: 1.75rem;
  margin: auto;
}

.paywall-card h2 {
  margin-top: 0;
  font-size: 1.25rem;
}

.paywall-section {
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  margin-top: 1rem;
}

.paywall-section h3 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
}

.paywall-section p {
  margin: 0.35rem 0;
}

#paywall-lead-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

#paywall-lead-form input {
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}

#paywall-lead-form button {
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

.paywall-lead-msg.success { color: #16a34a; }
.paywall-lead-msg.error { color: #dc2626; }

#paywall-signout-btn {
  margin-top: 1.25rem;
}
