:root {
  color-scheme: dark;
  --bg: #11161a;
  --surface: #182026;
  --surface-2: #202a32;
  --border: #2c3842;
  --border-active: #62788a;
  --text: #f3efe6;
  --muted: #93a3b0;

  --correct: #2f8f5b;
  --correct-border: #3aa56b;
  --present: #c8922b;
  --present-border: #dfa53a;
  --absent: #262f37;
  --absent-text: #73828f;

  --key-bg: #2a3640;
  --key-hover: #364552;
  --key-action: #374754;
  --radius: 10px;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --font-display: ui-serif, Georgia, "Times New Roman", serif;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  overflow-x: hidden;
}

.app {
  min-height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top, 0px) 10px calc(env(safe-area-inset-bottom, 0px) + 12px);
  display: flex;
  flex-direction: column;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 2px 10px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: linear-gradient(145deg, var(--correct), #1f6440);
  color: #fff;
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 700;
  display: grid;
  place-items: center;
  flex: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.22);
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-text h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.14em;
  line-height: 1.1;
}

.puzzle-meta {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.mode-switch {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.mode-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.mode-btn.active {
  background: var(--surface-2);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

.actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.icon-btn:hover {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

.icon-btn:focus-visible,
.mode-btn:focus-visible,
.btn:focus-visible,
.key:focus-visible {
  outline: 2px solid var(--correct-border);
  outline-offset: 2px;
}

/* Toast */
.toast-host {
  position: fixed;
  top: 68px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  pointer-events: none;
}

.toast {
  background: #f3efe6;
  color: #11161a;
  font-size: 13px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 999px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  white-space: nowrap;
}

/* Main stage */
.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding-top: 14px;
}

/* Board */
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 7px;
  width: min(330px, 86vw);
  margin: auto 0;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 7px;
}

.row.shake {
  animation: shake 0.36s ease;
}

.tile {
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
  border: 1.5px solid var(--border);
  background: var(--surface);
  display: grid;
  place-items: center;
  font-size: clamp(22px, 6.2vw, 30px);
  font-weight: 700;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  user-select: none;
  transition: border-color 0.12s, background-color 0.18s, color 0.18s;
}

.tile.filled {
  border-color: var(--border-active);
  background: var(--surface-2);
}

.tile.pop {
  animation: pop 0.14s ease-out;
}

.tile.flip {
  animation: flip 0.48s ease both;
}

.tile.win-bounce {
  animation: bounce 0.45s ease both;
}

.tile.correct {
  background: var(--correct);
  border-color: var(--correct-border);
  color: #fff;
}

.tile.present {
  background: var(--present);
  border-color: var(--present-border);
  color: #fff;
}

.tile.absent {
  background: var(--absent);
  border-color: var(--absent);
  color: var(--absent-text);
}

/* Round banner */
.round-banner {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.round-banner[hidden] {
  display: none;
}

.round-banner-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.round-banner-info strong {
  font-size: 14px;
}

.round-banner-info span {
  font-size: 12px;
  color: var(--muted);
}

.round-banner-actions {
  display: flex;
  gap: 6px;
  flex: none;
}

/* Buttons */
.btn {
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 13px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s, background 0.15s;
}

.btn.primary {
  background: var(--correct);
  border-color: var(--correct-border);
  color: #fff;
}

.btn.secondary {
  background: var(--surface-2);
  color: var(--text);
}

.btn.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--muted);
}

.btn.small {
  padding: 6px 10px;
  font-size: 12px;
}

.btn.full {
  width: 100%;
  padding: 12px 16px;
  font-size: 14px;
}

.btn.ready-on {
  background: var(--surface-2);
  border-color: var(--correct-border);
  color: var(--correct-border);
}

.btn:hover {
  opacity: 0.92;
}

/* Duel panel & lobby */
.duel-panel {
  width: 100%;
  max-width: 440px;
  margin: auto 0;
}

.duel-panel[hidden] {
  display: none;
}

.duel-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.duel-card[hidden] {
  display: none;
}

.duel-card h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.duel-lead {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  color: var(--muted);
}

.duel-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.duel-sub {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.field input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  padding: 10px 12px;
}

.field input:focus {
  outline: 2px solid var(--correct-border);
  outline-offset: 1px;
}

.inline-field {
  flex-direction: row;
  align-items: center;
  gap: 10px;
}

.inline-field span {
  flex: none;
}

.invite-box {
  display: flex;
  gap: 6px;
  align-items: center;
}

.invite-box input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--muted);
  font: 12px ui-monospace, Menlo, monospace;
  padding: 8px 10px;
}

.players-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.player-slot {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.player-slot.is-ready {
  border-color: var(--correct-border);
}

.slot-role {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.player-slot strong {
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ready-pill {
  align-self: flex-start;
  margin-top: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
}

.ready-pill.ready {
  background: rgba(47, 143, 91, 0.22);
  color: #6ad39a;
}

/* Duel HUD */
.duel-hud {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.duel-hud[hidden] {
  display: none;
}

.duel-hud-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.duel-timer {
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.duel-round-badge {
  font-size: 11px;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px;
}

.duel-hud-opp {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
  justify-content: flex-end;
}

.duel-opp-label {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.duel-opp-mini {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: none;
}

.mini-eval-row {
  display: flex;
  gap: 2px;
}

.mini-dot {
  width: 6px;
  height: 6px;
  border-radius: 2px;
  background: var(--absent);
}

.mini-dot.correct {
  background: var(--correct);
}

.mini-dot.present {
  background: var(--present);
}

/* Duel Result */
.duel-result {
  width: 100%;
  max-width: 480px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.duel-result[hidden] {
  display: none;
}

.duel-result-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.duel-result-head strong {
  display: block;
  font-size: 15px;
}

.duel-result-head span {
  display: block;
  font-size: 12px;
  color: var(--muted);
}

.duel-compare {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.duel-col {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.duel-col.winner {
  border-color: var(--correct-border);
}

.duel-col-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 6px;
  font-size: 12px;
}

.duel-col-head strong {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.duel-col-head span {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  flex: none;
}

.duel-col-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.duel-col-grid .mini-dot {
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.board[hidden],
.keyboard[hidden] {
  display: none;
}

/* Keyboard */
.keyboard {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 4px;
  user-select: none;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.key {
  flex: 1;
  min-width: 0;
  height: 50px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  background: var(--key-bg);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  touch-action: manipulation;
  transition: background-color 0.15s, color 0.15s, transform 0.06s;
}

.key:hover {
  background: var(--key-hover);
}

.key:active {
  transform: scale(0.95);
}

.key.wide {
  flex: 1.65;
  font-size: 11px;
  letter-spacing: 0.04em;
  background: var(--key-action);
}

.key.correct {
  background: var(--correct);
  border-color: var(--correct-border);
  color: #fff;
}

.key.present {
  background: var(--present);
  border-color: var(--present-border);
  color: #fff;
}

.key.absent {
  background: #1a2127;
  border-color: #20282f;
  color: #5b6975;
}

/* Modals */
.modal {
  width: min(420px, calc(100vw - 24px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
}

.modal::backdrop {
  background: rgba(8, 11, 14, 0.72);
  backdrop-filter: blur(4px);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.modal-body {
  padding: 16px;
  font-size: 14px;
  line-height: 1.5;
}

.modal-body p {
  margin: 0 0 12px;
  color: #d6dde3;
}

.example {
  margin: 14px 0;
}

.example-row {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
}

.mini-tile {
  width: 38px;
  height: 38px;
  border-radius: 7px;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  display: grid;
  place-items: center;
  font-size: 16px;
  font-weight: 700;
}

.mini-tile.correct {
  background: var(--correct);
  border-color: var(--correct-border);
  color: #fff;
}

.mini-tile.present {
  background: var(--present);
  border-color: var(--present-border);
  color: #fff;
}

.mini-tile.absent {
  background: var(--absent);
  border-color: var(--absent);
  color: var(--absent-text);
}

.help-note {
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
  padding-top: 12px;
  margin-top: 14px;
}

/* Stats */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  margin-bottom: 18px;
}

.kpi {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 4px;
  text-align: center;
}

.kpi strong {
  display: block;
  font-size: 20px;
  font-weight: 700;
  line-height: 1.15;
}

.kpi span {
  display: block;
  margin-top: 4px;
  font-size: 10px;
  color: var(--muted);
  line-height: 1.2;
}

.subhead {
  margin: 0 0 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.dist-bars {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 18px;
}

.dist-row {
  display: grid;
  grid-template-columns: 16px 1fr;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.dist-label {
  color: var(--muted);
  font-weight: 600;
  text-align: right;
}

.dist-track {
  background: var(--bg);
  border-radius: 6px;
  overflow: hidden;
  height: 24px;
}

.dist-fill {
  height: 100%;
  min-width: 24px;
  background: #33414d;
  color: var(--text);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 8px;
  border-radius: 6px;
  transition: width 0.3s ease;
}

.dist-fill.highlight {
  background: var(--correct);
  color: #fff;
}

.stats-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.countdown-box {
  display: flex;
  flex-direction: column;
}

.countdown-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.countdown-time {
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
}

.stats-buttons {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Animations */
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@keyframes flip {
  0% { transform: rotateX(0deg); }
  49% { transform: rotateX(90deg); }
  50% { transform: rotateX(90deg); }
  100% { transform: rotateX(0deg); }
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  45% { transform: translateY(-9px); }
  70% { transform: translateY(2px); }
}

@media (max-width: 420px) {
  .app {
    padding-left: 6px;
    padding-right: 6px;
  }
  .mode-btn {
    padding: 5px 8px;
    font-size: 11px;
  }
  .brand-mark {
    width: 30px;
    height: 30px;
    font-size: 17px;
  }
  .brand-text h1 {
    font-size: 15px;
  }
  .board {
    width: min(312px, 88vw);
    gap: 6px;
  }
  .row {
    gap: 6px;
  }
  .key {
    height: 48px;
    font-size: 14px;
  }
  .kb-row {
    gap: 3px;
  }
  .kpi strong {
    font-size: 17px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile.pop, .tile.flip, .tile.win-bounce, .row.shake {
    animation: none;
  }
}
