:root {
  --bg: #f5f0e8;
  --card: #faf7f1;
  --panel: #f0ebe1;
  --keyboard: #1a2744;
  --text: #1a2744;
  --muted: #4a5568;
  --accent: #c9944a;
  --accent-strong: #b8860b;
  --navy: #1a2744;
  --navy-light: #2d3f5e;
  --gold: #c9944a;
  --gold-bright: #daa520;
  --line: rgba(26, 39, 68, 0.15);
  --shadow: 0 16px 40px rgba(26, 39, 68, 0.12);
  --success: #2e7d4f;
  --success-soft: #e0f0e5;
  --danger: #b45b4d;
  --danger-soft: #fde8e4;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

button {
  font: inherit;
  color: inherit;
}

.game-card {
  background: var(--card);
  border: 3px solid var(--navy);
  box-shadow: var(--shadow), inset 0 0 0 1px var(--gold);
  color: var(--text);
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.game-card::before {
  content: "";
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(201, 148, 74, 0.35);
  border-radius: 24px;
  pointer-events: none;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 0.76rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--gold);
  font-weight: 700;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(1.65rem, 4.4vw, 2rem);
  letter-spacing: -0.03em;
  color: var(--navy);
}

.streak-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 68px;
  padding: 9px 12px;
  border-radius: 999px;
  background: var(--navy);
  color: var(--gold-bright);
  font-weight: 800;
  font-size: 1rem;
  border: 1px solid rgba(201, 148, 74, 0.4);
}

.subhead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.95rem;
  color: var(--muted);
}

.quote-panel {
  background: var(--panel);
  border: 1.5px solid var(--navy);
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 8px;
  flex: 1 1 auto;
  min-height: 0;
}

.quote-grid {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  justify-content: center;
  gap: 6px 6px;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 2px 12px;
  max-width: 100%;
  width: 100%;
  box-sizing: border-box;
}

.word-group {
  display: flex;
  align-items: flex-end;
  flex-wrap: nowrap;
  gap: 3px;
  margin: 0 4px 4px 0;
  max-width: 100%;
}

.cipher-cell {
  width: var(--cell-w, 34px);
  min-width: 0;
  flex-shrink: 1;
  height: 50px;
  border: 1.5px solid var(--navy);
  border-radius: 10px;
  background: #fff;
  display: grid;
  grid-template-rows: auto 1fr;
  align-items: center;
  justify-items: center;
  padding: 3px 2px 4px;
  cursor: pointer;
  transition:
    transform 140ms ease,
    border-color 180ms ease,
    background 180ms ease,
    box-shadow 180ms ease;
}

.cipher-cell:active,
.key-button:active,
.hint-button:active,
.action-button:active {
  transform: scale(0.97);
}

.cipher-cell.is-selected {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(201, 148, 74, 0.3);
  background: #fffbf0;
}

.cipher-cell.is-cursor {
  position: relative;
}
.cipher-cell.is-cursor::after {
  content: "";
  position: absolute;
  bottom: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 16px;
  height: 2.5px;
  border-radius: 2px;
  background: var(--gold);
  animation: cursor-blink 1s ease-in-out infinite;
}
@keyframes cursor-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

.cipher-cell.is-linked {
  transform: translateY(-1px);
}

.cipher-cell.is-correct {
  background: var(--success-soft);
  border-color: rgba(63, 129, 86, 0.45);
  box-shadow: 0 10px 20px rgba(63, 129, 86, 0.18);
}

.cipher-cell.is-wrong {
  background: var(--danger-soft);
  border-color: rgba(180, 91, 77, 0.45);
  animation: shake 320ms ease;
}

.cipher-cell.is-hinted {
  animation: pop 420ms ease;
}

.cipher-top {
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--navy);
}

.guess-bottom {
  min-height: 1em;
  font-size: 1rem;
  font-weight: 800;
  line-height: 1;
}

.cipher-cell.is-empty .guess-bottom {
  color: transparent;
  position: relative;
}

.cipher-cell.is-empty .guess-bottom::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 2px;
  transform: translateX(-50%);
  width: 18px;
  height: 2px;
  border-radius: 999px;
  background: rgba(26, 39, 68, 0.4);
}

.cipher-punct {
  align-self: flex-end;
  padding: 0 1px 8px;
  font-size: 1.28rem;
  line-height: 1;
  color: var(--text);
}

.selection-hint {
  margin: 0;
  min-height: 1.3em;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.2;
  flex-shrink: 0;
}

.stats-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 2px 0;
  flex-shrink: 0;
}

.stat {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.hint-btn {
  padding: 5px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--gold);
  background: transparent;
  color: var(--gold-bright);
  font-weight: 700;
  font-size: 0.78rem;
  cursor: pointer;
  transition: transform 140ms ease, opacity 140ms ease;
}

.hint-btn:active {
  transform: scale(0.96);
}

.hint-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.solve-reveal {
  display: none;
  position: absolute;
  inset: 0;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 14px;
  overflow-y: auto;
  text-align: center;
  z-index: 10;
  background: var(--card);
  border-radius: 22px;
}

.solve-reveal.is-visible {
  display: flex;
}

.reveal-score {
  font-size: 0.82rem;
  color: var(--muted);
  flex-shrink: 0;
  padding: 4px 0;
}

.reveal-score strong {
  color: var(--text);
}

.page-image {
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  object-fit: contain;
  border-radius: 8px;
}

.next-button {
  padding: 12px 28px;
  border-radius: 14px;
  border: 2px solid var(--navy);
  background: var(--navy);
  color: var(--gold-bright);
  font-weight: 800;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 140ms ease, opacity 140ms ease;
}

.next-button:active {
  transform: scale(0.97);
}

.meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.meta-chip,
.hint-button,
.result-chip,
.action-button {
  border-radius: 12px;
  border: 1.5px solid rgba(26, 39, 68, 0.2);
}

.meta-chip {
  padding: 12px 14px;
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.8);
  font-weight: 700;
}

.hint-button,
.action-button {
  min-height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.8);
  cursor: pointer;
  transition: transform 140ms ease, border-color 180ms ease, background 180ms ease, opacity 180ms ease;
}

.hint-button strong {
  min-width: 22px;
  font-size: 1.05rem;
}

.hint-button:disabled,
.action-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.result-panel {
  display: grid;
  gap: 12px;
  background: rgba(26, 39, 68, 0.06);
  border: 1.5px solid rgba(26, 39, 68, 0.15);
  border-radius: 14px;
  padding: 14px;
}

.result-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.result-chip {
  background: rgba(255, 255, 255, 0.8);
  padding: 10px 12px;
  text-align: center;
}

.result-label {
  display: block;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--muted);
  margin-bottom: 6px;
}

.result-chip strong {
  font-size: 1.05rem;
}

.result-actions {
  display: flex;
  justify-content: center;
}

.action-button.primary {
  width: 100%;
  background: var(--navy);
  border-color: var(--navy);
  color: var(--gold-bright);
  font-weight: 800;
}

.keyboard-wrap {
  flex-shrink: 0;
}

.keyboard {
  display: grid;
  gap: 5px;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

.key-button {
  position: relative;
  flex: 1;
  min-width: 0;
  max-width: 42px;
  height: 50px;
  border-radius: 10px;
  border: 1.5px solid rgba(26, 39, 68, 0.25);
  background: var(--navy);
  color: #fff;
  cursor: pointer;
  font-weight: 800;
  letter-spacing: 0.04em;
  transition:
    transform 140ms ease,
    border-color 180ms ease,
    background 180ms ease,
    opacity 180ms ease,
    box-shadow 180ms ease;
}

.key-button.is-active {
  border-color: var(--gold);
  background: var(--gold);
  color: var(--navy);
  box-shadow: 0 0 0 3px rgba(201, 148, 74, 0.3);
}

.key-button.is-correct {
  background: var(--success);
  border-color: var(--success);
  color: #fff;
  opacity: 1;
}

.key-button.is-used {
  opacity: 0.8;
}

.key-button.is-occupied {
  opacity: 0.64;
}

.key-main {
  font-size: 1rem;
}

.key-sub {
  position: absolute;
  top: 5px;
  right: 6px;
  font-size: 0.62rem;
  color: var(--gold-bright);
}

.key-button.backspace {
  max-width: 62px;
  flex: 1.45;
}

.reveal-label {
  margin: 0;
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--accent-strong);
  font-weight: 700;
}

.solved-cipher {
  font-size: clamp(1.22rem, 4vw, 1.58rem);
  line-height: 1.45;
  font-weight: 800;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translate(-50%, 16px);
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: 14px;
  background: rgba(61, 44, 34, 0.92);
  color: #fff;
  font-size: 0.92rem;
  box-shadow: 0 18px 30px rgba(0, 0, 0, 0.22);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  text-align: center;
  z-index: 20;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ── Info button ─────────────────────────────────────────── */
.info-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 10;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--gold);
  background: var(--card);
  color: var(--gold);
  font-weight: 800;
  font-size: 0.85rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
  transition: background 120ms, color 120ms;
}
.info-btn:active {
  background: var(--gold);
  color: var(--card);
}

/* ── Tutorial overlay ────────────────────────────────────── */
.tutorial-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fadeIn 200ms ease;
}
.tutorial-overlay[hidden] { display: none; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.tutorial-card {
  background: var(--card);
  border: 2px solid var(--gold);
  border-radius: 20px;
  padding: 20px 18px 18px;
  max-width: 380px;
  width: 100%;
  max-height: 85dvh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}

.tutorial-close {
  position: absolute;
  top: 10px;
  right: 12px;
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.tutorial-title {
  margin: 0 0 6px;
  font-size: 1.15rem;
  color: var(--navy);
  text-align: center;
  letter-spacing: -0.02em;
}

.tutorial-goal {
  margin: 0 0 14px;
  font-size: 0.82rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.35;
}

.tutorial-steps {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.tutorial-step {
  display: flex;
  align-items: center;
  gap: 10px;
}

.step-num {
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--navy);
  color: var(--gold-bright);
  font-weight: 800;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.step-demo {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 5px;
}

.step-text {
  margin: 0;
  font-size: 0.82rem;
  color: var(--text);
  line-height: 1.3;
}
.step-text strong {
  color: var(--gold);
}

/* demo cells */
.demo-cell {
  width: 34px;
  height: 42px;
  border: 2px solid var(--line);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--card);
}
.demo-cell.demo-selected {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(201, 148, 74, 0.3);
}
.demo-cell.demo-correct {
  background: var(--success-soft);
  border-color: var(--success);
}
.demo-cell.demo-wrong {
  background: var(--danger-soft);
  border-color: var(--danger);
  animation: demo-shake 0.4s ease infinite;
}
@keyframes demo-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

.demo-cipher {
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--gold);
  line-height: 1;
}
.demo-guess {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1;
}
.demo-guess.demo-blank {
  width: 12px;
  height: 2px;
  background: var(--muted);
  border-radius: 1px;
  margin-top: 2px;
}
.demo-arrow {
  font-size: 1rem;
  color: var(--muted);
}
.demo-eq {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--muted);
}
.demo-key {
  width: 28px;
  height: 30px;
  border-radius: 6px;
  background: var(--navy);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.demo-hint-btn {
  padding: 4px 12px;
  border-radius: 8px;
  background: var(--gold);
  color: #fff;
  font-weight: 700;
  font-size: 0.75rem;
}

.tutorial-got-it {
  display: block;
  width: 100%;
  margin-top: 16px;
  padding: 10px;
  border: none;
  border-radius: 12px;
  background: var(--navy);
  color: var(--gold-bright);
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 120ms;
}
.tutorial-got-it:active {
  background: var(--navy-light);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1520;
    --card: #182030;
    --panel: #1e2a3e;
    --text: #e8e0d4;
    --muted: #a0998e;
    --line: rgba(201, 148, 74, 0.15);
    --shadow: 0 16px 48px rgba(0, 0, 0, 0.42);
    --success-soft: #1a3526;
    --danger-soft: #3e201c;
  }

  .game-card {
    background: var(--card);
    border-color: var(--gold);
  }

  .game-card::before {
    border-color: rgba(201, 148, 74, 0.2);
  }

  .topbar h1 {
    color: var(--gold-bright);
  }

  .cipher-cell {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(201, 148, 74, 0.35);
  }

  .cipher-top {
    color: var(--gold-bright);
  }

  .guess-bottom {
    color: var(--text);
  }

  .cipher-cell.is-empty .guess-bottom::after {
    background: rgba(201, 148, 74, 0.35);
  }

  .cipher-cell.is-selected {
    background: rgba(201, 148, 74, 0.12);
    border-color: var(--gold);
  }

  .quote-panel {
    background: var(--panel);
    border-color: rgba(201, 148, 74, 0.25);
  }

  .meta-chip,
  .hint-button,
  .result-chip,
  .action-button {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(201, 148, 74, 0.2);
  }

  .key-button {
    background: var(--navy-light);
    border-color: rgba(201, 148, 74, 0.25);
  }

  .cipher-punct {
    color: var(--text);
  }

  .toast {
    background: rgba(245, 234, 220, 0.94);
    color: #231912;
  }

  .tutorial-title {
    color: var(--gold-bright);
  }
}

@media (max-height: 760px) {
  .game-card {
    padding: 8px;
    gap: 4px;
  }

  .quote-panel {
    padding: 6px;
  }

  .quote-grid {
    gap: 4px 4px;
  }

  .word-group {
    gap: 2px;
    margin: 0 4px 3px 0;
  }

  .cipher-cell {
    width: var(--cell-w, 30px);
    height: 42px;
    border-radius: 8px;
  }

  .cipher-top {
    font-size: 0.65rem;
  }

  .guess-bottom {
    font-size: 0.9rem;
  }

  .key-button {
    min-height: 38px;
    height: 38px;
  }

  .keyboard {
    gap: 4px;
  }

  .keyboard-row {
    gap: 4px;
  }

  .selection-hint {
    min-height: 1.1em;
    font-size: 0.72rem;
  }

  .stats-bar {
    padding: 1px 0;
  }
}

@media (max-width: 400px) {
  .cipher-cell {
    width: var(--cell-w, 28px);
    min-width: 0;
    height: 40px;
    border-radius: 7px;
    padding: 2px 1px 3px;
  }

  .cipher-top {
    font-size: 0.6rem;
  }

  .guess-bottom {
    font-size: 0.85rem;
  }

  .word-group {
    gap: 2px;
    margin: 0 3px 3px 0;
  }

  .quote-grid {
    gap: 4px 3px;
    padding: 2px 8px;
  }

  .game-card {
    padding: 8px;
    border-radius: 18px;
  }

  .app-shell {
    padding: 6px;
  }
}

@media (min-width: 720px) {
  .app-shell {
    padding: 20px;
    align-items: center;
  }

  .game-card {
    height: min(100%, 860px);
  }
}

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

@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.08); }
  100% { transform: scale(1); }
}
