/* =====================================================================
   THE BLACK CHAMBER: styles
   ---------------------------------------------------------------------
   Look: a nod to 1940s typewritten paper (cream page, typewriter
   letters, a red "TOP SECRET" stamp) with modern, roomy controls.

   All colours are "tokens" defined once below. Change a value here and
   it updates everywhere. Dark mode just swaps the token values.

   Sections:
     1. Colour tokens (light + dark)
     2. Page basics
     3. Top bar
     4. Buttons, cards, shared bits
     5. Home
     6. Play screen (board, toolbar, keyboard)
     7. Result, library, stats, settings, journey, about
     8. Toast + dialog
   ===================================================================== */

/* ---------- 1. Colour tokens ---------- */
:root {
  --paper: #f4ecdc;        /* page background */
  --card: #fbf7ee;         /* cards and panels */
  --ink: #1f2532;          /* main text */
  --ink-soft: #6b6457;     /* secondary text */
  --line: #d9ccb1;         /* borders */
  --line-strong: #a89a7e;  /* letter box borders */
  --box: #fffdf7;          /* empty letter box */
  --accent: #a8322a;       /* stamp red: primary buttons */
  --accent-ink: #fffaf2;   /* text on accent */
  --same: #f3dd9b;         /* boxes sharing the selected cipher letter */
  --active: #1f2532;       /* outline of the exact selected box */
  --locked: #2c6a4c;       /* letters revealed by hints */
  --error: #b3261e;        /* wrong letters after Check */
  --error-bg: #f8dcd7;
  --solved-bg: #dcebdc;
  --key: #fffdf7;          /* on-screen keyboard keys */
  --key-shadow: #c9bb9d;
  --shadow: 0 1px 2px rgba(60, 45, 20, .08), 0 4px 14px rgba(60, 45, 20, .07);

  --font-type: "Courier Prime", "Courier New", Courier, ui-monospace, monospace;
  --font-letters: ui-monospace, "SF Mono", "Menlo", "Roboto Mono", Consolas, monospace;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --radius: 10px;
  --topbar-h: 52px;
  color-scheme: light;
}

/* Dark colours: used when the phone is in dark mode (and the player
   hasn't forced Light), or when Settings > Theme is Dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #141a24;
    --card: #1c2431;
    --ink: #ece5d6;
    --ink-soft: #a29b8c;
    --line: #2e394b;
    --line-strong: #56637a;
    --box: #222c3b;
    --accent: #d9645a;
    --accent-ink: #1a1112;
    --same: #4b4329;
    --active: #f0d48a;
    --locked: #7fcfa2;
    --error: #ff8a80;
    --error-bg: #4a2427;
    --solved-bg: #1f3a2c;
    --key: #263142;
    --key-shadow: #0c1018;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #141a24;
  --card: #1c2431;
  --ink: #ece5d6;
  --ink-soft: #a29b8c;
  --line: #2e394b;
  --line-strong: #56637a;
  --box: #222c3b;
  --accent: #d9645a;
  --accent-ink: #1a1112;
  --same: #4b4329;
  --active: #f0d48a;
  --locked: #7fcfa2;
  --error: #ff8a80;
  --error-bg: #4a2427;
  --solved-bg: #1f3a2c;
  --key: #263142;
  --key-shadow: #0c1018;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

/* ---------- 2. Page basics ---------- */
*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100dvh;
  /* Stop iOS from enlarging text in landscape, and from the grey
     flash when tapping. */
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }
a { color: inherit; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px;
}

/* Only the active screen is shown. */
.screen { display: none; padding: 16px 0 32px; }
.screen.active { display: block; }

[hidden] { display: none !important; }

/* Visible keyboard focus for desktop users. */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- 3. Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}
.topbar-title {
  flex: 1;
  margin: 0;
  font-family: var(--font-type);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: .04em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
body[data-screen="home"] .topbar-title { visibility: hidden; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  margin-left: -6px;
  border: 0;
  border-radius: 50%;
  background: none;
  cursor: pointer;
}
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active { background: var(--line); }

.timer {
  font-family: var(--font-letters);
  font-size: .95rem;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
}

/* ---------- 4. Buttons, cards, shared bits ---------- */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 50px;
  padding: 10px 18px;
  border: 1.5px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--card);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition: transform .08s ease;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-danger { width: 100%; margin-top: 20px; color: var(--error); border-color: var(--error); background: transparent; }

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

.stack { display: grid; gap: 10px; margin-top: 16px; }

.lede { color: var(--ink-soft); margin-top: 0; }

/* ---------- 5. Home ---------- */
.home-hero { text-align: center; padding: 28px 0 12px; position: relative; }

.stamp {
  display: inline-block;
  font-family: var(--font-type);
  font-weight: 700;
  font-size: .8rem;
  letter-spacing: .25em;
  color: var(--accent);
  border: 2.5px solid var(--accent);
  border-radius: 4px;
  padding: 3px 10px 3px 13px;
  transform: rotate(-6deg);
  opacity: .85;
}
.home-title {
  font-family: var(--font-type);
  font-size: clamp(2rem, 10vw, 2.8rem);
  line-height: 1.1;
  margin: 14px 0 6px;
  letter-spacing: .02em;
}
.home-sub { margin: 0; color: var(--ink-soft); font-family: var(--font-serif); font-style: italic; font-size: 1.05rem; }

.home-actions { display: grid; gap: 10px; margin-top: 24px; }
.home-progress { text-align: center; color: var(--ink-soft); font-size: .9rem; margin: 14px 0 22px; }

.journey-card {
  display: grid;
  gap: 4px;
  padding: 16px 18px;
  border: 1.5px dashed var(--line-strong);
  border-radius: var(--radius);
  text-decoration: none;
}
.journey-card strong { font-family: var(--font-type); font-size: 1.15rem; }
.journey-card span:last-child { color: var(--ink-soft); font-size: .92rem; }
.journey-label {
  justify-self: start;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--accent);
}

.home-links { display: flex; justify-content: center; gap: 28px; margin-top: 28px; }
.home-links a { color: var(--ink-soft); font-weight: 600; text-decoration: none; padding: 8px 0; }

/* ---------- 6. Play screen ---------- */

/* The play screen fills the phone exactly: info at top, puzzle in the
   middle (scrolls if long), tools + keyboard pinned at the bottom. */
#screen-play.active {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--topbar-h) - env(safe-area-inset-top));
  padding: 8px 0 0;
}

.play-info {
  display: flex;
  justify-content: space-between;
  font-size: .82rem;
  color: var(--ink-soft);
  padding: 0 2px 4px;
}

.start-hint-banner {
  margin: 0 0 6px;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--locked);
  font-family: var(--font-letters);
  font-weight: 700;
  font-size: .9rem;
  text-align: center;
}

.board-wrap {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0 12px;
}

/* --cell is the width of one letter box, set by player.js so the
   longest word always fits on one line. */
.board {
  --cell: 30px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-end;
  column-gap: calc(var(--cell) * .55);  /* space between words */
  row-gap: 12px;                         /* space between lines */
}

/* A word never breaks across two lines. */
.word { display: inline-flex; gap: 3px; white-space: nowrap; }

.cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: var(--cell);
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation; /* no double-tap zoom delay */
}

/* The small cipher letter above each box */
.cell-cipher {
  font-family: var(--font-type);
  font-weight: 700;
  font-size: calc(var(--cell) * .46);
  line-height: 1.25;
  color: var(--ink-soft);
}

/* The guess box itself: a clear outlined square, one per letter */
.cell-guess {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: calc(var(--cell) * 1.18);
  border: 1.5px solid var(--line-strong);
  border-radius: 5px;
  background: var(--box);
  font-family: var(--font-letters);
  font-weight: 700;
  font-size: calc(var(--cell) * .66);
  line-height: 1;
  transition: background-color .12s ease;
}

/* Every box with the same cipher letter as the selected one */
.cell.same .cell-guess { background: var(--same); }
.cell.same .cell-cipher { color: var(--ink); }

/* The exact box that was tapped */
.cell.active .cell-guess { border-color: var(--active); box-shadow: 0 0 0 1.5px var(--active); }

.cell.locked .cell-guess { color: var(--locked); }

.cell.error .cell-guess { color: var(--error); border-color: var(--error); background: var(--error-bg); }

/* Punctuation sits on the box line, narrower than a letter */
.punct {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: calc(var(--cell) * .4);
  height: calc(var(--cell) * 1.18);
  font-family: var(--font-letters);
  font-weight: 700;
  font-size: calc(var(--cell) * .66);
  color: var(--ink-soft);
}

/* Little celebration when the puzzle is finished */
.board.solved .cell-guess { background: var(--solved-bg); border-color: var(--locked); animation: pop .45s ease; }
@keyframes pop { 40% { transform: scale(1.12); } }
@media (prefers-reduced-motion: reduce) {
  .board.solved .cell-guess { animation: none; }
}

/* Tool buttons */
.toolbar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 6px 0;
  border-top: 1px solid var(--line);
}
.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 6px 0 4px;
  border: 0;
  border-radius: 8px;
  background: none;
  font-size: .72rem;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
  touch-action: manipulation;
}
.tool svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.tool:active { background: var(--line); }
.tool:disabled { opacity: .35; }

/* On-screen keyboard */
.keyboard {
  display: grid;
  gap: 7px;
  padding: 4px 0 calc(8px + env(safe-area-inset-bottom));
  user-select: none;
  -webkit-user-select: none;
}
.kb-row { display: flex; justify-content: center; gap: 5px; }
.kb-key {
  flex: 1 1 0;
  max-width: 44px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: var(--key);
  box-shadow: 0 1.5px 0 var(--key-shadow);
  font-family: var(--font-letters);
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: none;
  transition: opacity .15s ease, transform .06s ease;
}
.kb-key.used { opacity: .35; }
.kb-key.pressed { transform: translateY(1px) scale(.94); box-shadow: none; }
.kb-erase { flex-grow: 1.5; max-width: 66px; font-size: 1.3rem; }

/* Short phones (e.g. iPhone SE): squeeze a little */
@media (max-height: 640px) {
  .kb-key { height: 42px; }
  .keyboard { gap: 5px; }
  .tool span { display: none; }
}

/* ---------- 7. Other screens ---------- */

/* Result */
.result-card { text-align: center; }
.result-heading {
  display: inline-block;
  margin: 0 0 12px;
  font-family: var(--font-type);
  font-weight: 700;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: var(--accent);
  border: 2px solid var(--accent);
  border-radius: 4px;
  padding: 2px 10px 2px 13px;
  transform: rotate(-3deg);
}
.result-quote { margin: 4px 0 10px; font-family: var(--font-serif); font-size: 1.3rem; line-height: 1.45; }
.result-author { margin: 0; font-weight: 700; }
.result-source { margin: 2px 0 10px; color: var(--ink-soft); font-size: .88rem; font-style: italic; }
.result-note { margin: 0 0 14px; font-size: .95rem; }
.result-stats, .stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin: 0;
}
.result-stats div, .stat-grid div { padding: 8px; border-radius: 8px; background: var(--paper); }
dt { font-size: .75rem; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .06em; }
dd { margin: 0; font-family: var(--font-letters); font-weight: 700; font-size: 1.15rem; }

/* Library */
.filters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 12px; }
.chip {
  flex: none;
  padding: 7px 14px;
  border: 1.5px solid var(--line-strong);
  border-radius: 999px;
  background: none;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
}
.chip.active { background: var(--ink); color: var(--paper); border-color: var(--ink); }

.puzzle-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.puzzle-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  text-align: left;
  cursor: pointer;
}
.row-main { flex: 1; display: grid; min-width: 0; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: .82rem; color: var(--ink-soft); }
.row-status { font-size: .78rem; color: var(--ink-soft); }
.tag {
  display: inline-block;
  margin-left: 4px;
  padding: 0 6px;
  border-radius: 4px;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .05em;
  color: var(--accent);
  border: 1px solid currentColor;
}
.status-dot { flex: none; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--line-strong); }
.status-dot.solved { background: var(--locked); border-color: var(--locked); }
.status-dot.revealed { background: var(--line-strong); }
.status-dot.progress { border-color: var(--accent); background: var(--accent); box-shadow: 0 0 0 3px var(--same); }
.empty { text-align: center; color: var(--ink-soft); padding: 24px; }

/* Settings */
.settings { display: grid; gap: 0; padding: 4px 18px; }
.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 60px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.setting:last-child { border-bottom: 0; }
.setting small { display: block; color: var(--ink-soft); font-size: .8rem; }

.segmented { display: flex; border: 1.5px solid var(--line-strong); border-radius: 8px; overflow: hidden; }
.segmented button { padding: 7px 11px; border: 0; background: none; font-size: .88rem; font-weight: 600; cursor: pointer; }
.segmented button + button { border-left: 1.5px solid var(--line-strong); }
.segmented button.active { background: var(--ink); color: var(--paper); }

/* An on/off switch made from a checkbox */
.switch {
  appearance: none;
  -webkit-appearance: none;
  flex: none;
  position: relative;
  width: 50px;
  height: 30px;
  margin: 0;
  border-radius: 999px;
  background: var(--line-strong);
  cursor: pointer;
  transition: background .15s ease;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .15s ease;
}
.switch:checked { background: var(--locked); }
.switch:checked::after { transform: translateX(20px); }

/* Journey */
.journey-list { list-style: none; margin: 0; padding: 0 0 0 18px; border-left: 2px dashed var(--line-strong); display: grid; gap: 14px; }
.journey-list li {
  position: relative;
  display: grid;
  gap: 2px;
  padding: 12px 14px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.journey-list li::before {
  content: "";
  position: absolute;
  left: -26px;
  top: 18px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--paper);
  border: 2px solid var(--line-strong);
}
.journey-list li.live::before { background: var(--locked); border-color: var(--locked); }
.journey-list .era { font-family: var(--font-type); font-size: .8rem; color: var(--accent); font-weight: 700; }
.journey-list strong { font-family: var(--font-serif); font-size: 1.1rem; }
.journey-list span { font-size: .92rem; }
.journey-status { color: var(--ink-soft); font-size: .78rem !important; text-transform: uppercase; letter-spacing: .06em; }

/* About */
.prose h3 { font-family: var(--font-type); margin: 18px 0 4px; }
.prose h3:first-child { margin-top: 0; }
.prose p { margin: 0 0 10px; }

/* ---------- 8. Toast + dialog ---------- */
.toast {
  position: fixed;
  left: 50%;
  top: calc(var(--topbar-h) + env(safe-area-inset-top) + 10px);
  z-index: 30;
  max-width: calc(100% - 32px);
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  font-size: .9rem;
  font-weight: 600;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -8px);
  transition: opacity .2s ease, transform .2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(10, 12, 18, .5);
}
.dialog {
  width: 100%;
  max-width: 360px;
  padding: 20px;
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.dialog p { margin: 0 0 16px; font-size: 1.05rem; }
.dialog-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Desktop: a bit more breathing room */
@media (min-width: 700px) {
  .kb-key { max-width: 52px; height: 52px; }
  .board { row-gap: 16px; }
}
