/* =========================================================================
   Puzzle Lab — design tokens
   Concept: a field-notebook where puzzles get ruled out on graph paper.
   Type: system serif for the wordmark, system mono for the puzzle grid
   and keys (grid ↔ graph paper ↔ monospace all read as "ruled-out"),
   system sans for everything else. No external font requests, so the
   app looks right offline on the very first load.
   ========================================================================= */

:root {
  --color-bg: #eeece1;
  --color-bg-raised: #f7f6ef;
  --color-ink: #1f2420;
  --color-ink-soft: #565f57;
  --color-border: #c9c6b5;
  --color-border-strong: #1f2420;

  --color-accent: #3a6b52;      /* pine — brand + "correct" */
  --color-accent-ink: #f7f6ef;
  --color-present: #b8892c;     /* mustard — "present" */
  --color-present-ink: #1f2420;
  --color-absent: #8b8a7d;      /* stone — "absent" */
  --color-absent-ink: #f7f6ef;
  --color-danger: #a3402f;

  --font-display: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;

  --radius: 6px;
  --nav-height: 64px;
  --header-height: 56px;
  --max-width: 480px;

  color-scheme: light;
}

html.theme-dark {
  --color-bg: #161a17;
  --color-bg-raised: #1e2420;
  --color-ink: #eceae0;
  --color-ink-soft: #a3a99e;
  --color-border: #333b34;
  --color-border-strong: #eceae0;

  --color-accent: #5c9a7c;
  --color-accent-ink: #101410;
  --color-present: #d3ac57;
  --color-present-ink: #101410;
  --color-absent: #5b6058;
  --color-absent-ink: #eceae0;
  --color-danger: #d97f6c;

  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-ink);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* Visible focus state everywhere, keyboard-navigable */
button:focus-visible,
[role="switch"]:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

/* =========================================================================
   App shell
   ========================================================================= */

#app {
  max-width: var(--max-width);
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--color-border);
  border-right: 1px solid var(--color-border);
  background: var(--color-bg);
}

#offline-banner {
  background: var(--color-ink);
  color: var(--color-bg);
  text-align: center;
  font-size: 0.8rem;
  padding: 6px 12px;
  letter-spacing: 0.02em;
}

#app-header {
  height: var(--header-height);
  flex: 0 0 auto;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--color-border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-display);
  font-size: 1.15rem;
  letter-spacing: 0.01em;
}

.brand__mark {
  color: var(--color-accent);
}

.brand__puzzle-name {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--color-ink-soft);
}

#app-main {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px));
}

.screen-title {
  font-family: var(--font-display);
  font-size: 1.4rem;
  margin: 4px 0 16px;
}

#app-nav {
  flex: 0 0 auto;
  height: var(--nav-height);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  border-top: 1px solid var(--color-border);
  background: var(--color-bg-raised);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

#app-nav button {
  background: none;
  border: none;
  font-size: 1.3rem;
  color: var(--color-ink-soft);
  display: flex;
  align-items: center;
  justify-content: center;
}

#app-nav button[aria-pressed="true"] {
  color: var(--color-accent);
  position: relative;
}

#app-nav button[aria-pressed="true"]::after {
  content: "";
  position: absolute;
  bottom: 6px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--color-accent);
}

/* =========================================================================
   Wordle board
   ========================================================================= */

.wordle {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.board {
  --tile-gap: 6px;
  display: grid;
  gap: var(--tile-gap);
  padding: 12px;
  border-radius: var(--radius);
  /* Signature texture: faint graph-paper grid behind the tiles. */
  background-image: linear-gradient(var(--color-border) 1px, transparent 1px),
    linear-gradient(90deg, var(--color-border) 1px, transparent 1px);
  background-size: 14px 14px;
  background-color: var(--color-bg-raised);
  border: 1px solid var(--color-border);
}

.board-row {
  display: flex;
  gap: var(--tile-gap);
}

.tile {
  width: clamp(38px, 12vw, 56px);
  height: clamp(38px, 12vw, 56px);
  border: 2px solid var(--color-border);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: clamp(1.1rem, 5vw, 1.5rem);
  background: var(--color-bg);
  color: var(--color-ink);
  position: relative;
  touch-action: manipulation;
}

.tile--filled {
  border-color: var(--color-border-strong);
}

.tile--pop {
  animation: tile-pop 100ms ease-out;
}

.tile--correct {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: var(--color-accent-ink);
}

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

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

/* Never rely on color alone: a small corner mark encodes the same
   information as the fill color, for color-blind and greyscale-display
   players. */
.tile--correct::after,
.tile--present::after,
.tile--absent::after {
  position: absolute;
  bottom: 2px;
  right: 4px;
  font-size: 0.6em;
  font-family: var(--font-body);
  opacity: 0.85;
}
.tile--correct::after {
  content: "✓";
}
.tile--present::after {
  content: "↕";
}
.tile--absent::after {
  content: "·";
}

@keyframes tile-pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.06);
  }
  100% {
    transform: scale(1);
  }
}

.inline-error {
  margin: 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--color-danger);
  color: #fff;
  font-size: 0.85rem;
}

.result-banner {
  text-align: center;
  padding: 12px 16px;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  background: var(--color-bg-raised);
  max-width: 320px;
}

.result-banner__heading {
  font-family: var(--font-display);
  font-size: 1.2rem;
  margin: 0 0 4px;
}

.result-banner__detail,
.result-banner__next {
  margin: 2px 0;
  font-size: 0.88rem;
  color: var(--color-ink-soft);
}

.result-banner__actions {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}

.hint-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 4px 0 8px;
}

.hint-count {
  font-size: 0.78rem;
  color: var(--color-ink-soft);
}

/* =========================================================================
   Keyboard
   ========================================================================= */

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  touch-action: manipulation;
}

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

.key {
  flex: 1;
  max-width: 44px;
  min-width: 28px;
  height: 48px;
  border: none;
  border-radius: 4px;
  background: var(--color-bg-raised);
  color: var(--color-ink);
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 0.9rem;
  border: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: center;
}

.key--wide {
  max-width: 64px;
  font-size: 0.72rem;
}

.key--correct {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: var(--color-accent-ink);
}

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

.key--absent {
  background: var(--color-absent);
  border-color: var(--color-absent);
  color: var(--color-absent-ink);
  opacity: 0.75;
}

.key:active {
  transform: translateY(1px);
}

/* =========================================================================
   Stats screen
   ========================================================================= */

.stats-summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 24px;
}

.stat-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 4px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-bg-raised);
}

.stat-block__value {
  font-family: var(--font-display);
  font-size: 1.5rem;
}

.stat-block__label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-ink-soft);
  margin-top: 2px;
  text-align: center;
}

.section-title {
  font-family: var(--font-display);
  font-size: 1rem;
  margin: 0 0 10px;
}

.distribution-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.distribution-label {
  width: 14px;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--color-ink-soft);
}

.distribution-bar-track {
  flex: 1;
  background: var(--color-bg-raised);
  border: 1px solid var(--color-border);
  border-radius: 3px;
  overflow: hidden;
}

.distribution-bar {
  background: var(--color-border);
  color: var(--color-ink-soft);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  padding: 3px 6px;
  min-width: 20px;
  text-align: right;
}

.distribution-bar--filled {
  background: var(--color-accent);
  color: var(--color-accent-ink);
}

.empty-state {
  color: var(--color-ink-soft);
  font-size: 0.88rem;
  margin-top: 8px;
}

/* =========================================================================
   Settings screen
   ========================================================================= */

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px;
  border-bottom: 1px solid var(--color-border);
}

.settings-label {
  font-size: 0.95rem;
}

.switch {
  width: 44px;
  height: 26px;
  border-radius: 999px;
  border: 1px solid var(--color-border);
  background: var(--color-bg-raised);
  position: relative;
  transition: background 120ms ease;
}

.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--color-ink-soft);
  transition: transform 120ms ease;
}

.switch--on {
  background: var(--color-accent);
  border-color: var(--color-accent);
}

.switch--on::after {
  background: var(--color-accent-ink);
  transform: translateX(18px);
}

.settings-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 20px;
}

.btn {
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--color-border-strong);
  background: var(--color-bg-raised);
  color: var(--color-ink);
  font-size: 0.9rem;
}

.btn--danger {
  border-color: var(--color-danger);
  color: var(--color-danger);
}

.btn:disabled {
  opacity: 0.5;
}

/* =========================================================================
   Swiftle — Spotify embed cover (blurred until answered)
   ========================================================================= */

.spotify-embed-wrap {
  position: relative;
  margin: 10px 0;
}

.spotify-embed-wrap--covered iframe {
  filter: blur(18px) brightness(0.8);
}

.spotify-embed-cover {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  border-radius: 12px;
  /* Visual only — clicks pass through so the real Spotify play button
     underneath (blurred, not hidden) still works. */
  pointer-events: none;
}

/* =========================================================================
   Info screen
   ========================================================================= */

.rules-list {
  list-style: none;
  padding: 0;
  margin: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rules-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  margin-right: 8px;
  vertical-align: middle;
}

.rules-swatch--correct {
  background: var(--color-accent);
}
.rules-swatch--present {
  background: var(--color-present);
}
.rules-swatch--absent {
  background: var(--color-absent);
}

/* =========================================================================
   Toast
   ========================================================================= */

#toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translate(-50%, -140%);
  background: var(--color-ink);
  color: var(--color-bg);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 0.85rem;
  z-index: 20;
  transition: transform 160ms ease;
  pointer-events: none;
}

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

.fatal-error {
  text-align: center;
  padding: 40px 20px;
}

/* =========================================================================
   Puzzle controls: type / mode / variant / archive date
   ========================================================================= */

.control-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin-bottom: 10px;
}

.chip {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--color-border);
  background: var(--color-bg-raised);
  color: var(--color-ink-soft);
  font-size: 0.82rem;
}

.chip--active {
  border-color: var(--color-accent);
  color: var(--color-accent);
  background: var(--color-bg);
  font-weight: 600;
}

.date-input {
  padding: 8px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  background: var(--color-bg-raised);
  color: var(--color-ink);
  font-family: inherit;
}

/* ---- Anagram tiles ---- */

.anagram-tiles {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 320px;
}

.tile--letter {
  cursor: pointer;
}

.tile--used {
  opacity: 0.35;
}

/* ---- Trivia ---- */

.trivia-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 360px;
}

/* ---- Household leaderboard ---- */

.household-table {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.household-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  align-items: center;
  gap: 6px;
  padding: 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-bg-raised);
  font-size: 0.82rem;
}

.household-row__name {
  font-weight: 600;
}

/* =========================================================================
   Responsive: give the board and keyboard more room on larger screens
   ========================================================================= */

@media (min-width: 640px) {
  :root {
    --max-width: 560px;
  }
  #app {
    border-left: 1px solid var(--color-border);
    border-right: 1px solid var(--color-border);
    box-shadow: 0 0 0 1px var(--color-border);
    margin-top: 24px;
    margin-bottom: 24px;
    border-radius: 10px;
    overflow: hidden;
    min-height: calc(100% - 48px);
  }
}

@media (min-width: 900px) {
  :root {
    --max-width: 640px;
  }
}

/* Respect reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
  .tile--pop {
    animation: none;
  }
  * {
    transition-duration: 0.001ms !important;
    animation-duration: 0.001ms !important;
  }
}
