:root {
  /* Washi-paper cream, dialed back from the original — large bright fields
     (the board, mainly) read as glare rather than paper at full brightness. */
  --bg: #e9dfc7;
  --surface: #f2eadb;
  --border: #d2c19c;
  --text: #2c2a1f;
  --text-dim: #83744f;
  --accent: #2e4b5e;
  --accent-strong: #1f3542;
  --accent-contrast: #f6efdf;
  --sea: #22384a;
  --sea-2: #182a38;
  --island: #f6efdd;
  --island-border: #beac7c;
  --clue-text: #2c2a1f;
  --error: #b5533f;
  --logo-bg: #22384a;
  --logo-dot: #fdf8ec;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(40, 32, 16, 0.05), 0 8px 24px rgba(40, 32, 16, 0.07);
  color-scheme: light;
}

:root[data-theme="dark"] {
  /* Sumi-ink ground — neutral near-black, not brown. Real sumi ink is a
     cool/neutral black, not warm like wood or leather; the previous brown
     undertone fought the gold accent instead of setting it off. */
  --bg: #18181a;
  --surface: #201f22;
  --border: #38373a;
  --text: #e9e5da;
  --text-dim: #9c9a92;
  --accent: #c3a568;
  --accent-strong: #d6ba80;
  --accent-contrast: #1c1c1e;
  --sea: #101a22;
  --sea-2: #0a1218;
  --island: #232327;
  --island-border: #403f44;
  --clue-text: #e9e5da;
  --error: #d9805f;
  --logo-bg: #fdf8ec;
  --logo-dot: #22384a;
  color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Header ---------- */

.site-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 16px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

/* Desktop: the timer joins the settings controls as one right-aligned
   group, brand stays pinned left. On mobile this same margin is overridden
   below since the timer instead sits in a fixed 3-column header grid. */
.timer-wrap { margin-left: auto; }

.hamburger-btn {
  display: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.drawer-close-btn {
  display: none;
}

.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(20, 18, 12, 0.4);
  z-index: 40;
}
.drawer-backdrop.open { display: block; }

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

.brand-mark {
  /* Dedicated logo colors (--logo-bg/--logo-dot, set per theme below) —
     kept separate from the puzzle-grid variables because reusing those
     made the dark-mode logo go dark-on-dark. Light mode: dark square,
     light dots (original look). Dark mode: light square, dark dots
     (reads far better against the near-black header). */
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: var(--logo-bg);
  position: relative;
  display: inline-block;
  overflow: hidden;
}
.brand-mark::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 7px;
  height: 7px;
  background: var(--logo-dot);
  border-radius: 2px;
}
.brand-mark::before {
  content: "";
  position: absolute;
  bottom: 3px;
  right: 3px;
  width: 7px;
  height: 7px;
  background: var(--logo-dot);
  border-radius: 2px;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.brand-name {
  font-family: 'Shippori Mincho', serif;
  font-weight: 600;
  font-size: 1.3rem;
  letter-spacing: 0.03em;
}

.brand-kanji {
  font-family: 'Shippori Mincho', serif;
  font-size: 0.68rem;
  color: var(--text-dim);
  letter-spacing: 0.08em;
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* Native select stays in the DOM for its value/change-event only — the
   difficulty-switch buttons are what's actually shown, in every viewport. */
.select-wrap { display: none; }

.select-wrap select {
  appearance: none;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 30px 8px 12px;
  border-radius: 10px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%), linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 16px) center, calc(100% - 11px) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 10px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

/* Fixed width so the label swapping between "Reset" (daily) and
   "New Puzzle" (practice) doesn't shift every button after it. */
#newGameBtn { width: 120px; }

.mode-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.mode-btn {
  border: none;
  background: var(--bg);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 8px 14px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.mode-btn:hover:not(.active) { color: var(--text); }
.mode-btn.active {
  background: var(--accent);
  color: var(--accent-contrast);
}

/* Mobile-only alternative to the native <select> — a touch-friendly 3-way
   switch, used everywhere now — a native <select>'s open dropdown list
   can't be restyled cross-browser, so it always looks visibly different
   from the rest of a custom-styled UI. The underlying <select> stays in
   the DOM (script.js reads its value) but is never shown. */
.difficulty-switch {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.diff-btn {
  flex: 1;
  border: none;
  border-left: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-dim);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 10px 0;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.diff-btn:first-child { border-left: none; }
.diff-btn.active + .diff-btn { border-left-color: transparent; }
.diff-btn.active {
  background: var(--accent);
  color: var(--accent-contrast);
}

.countdown-wrap {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.countdown-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

.countdown {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--accent);
}

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

.timer {
  position: relative;
  border: none;
  background: none;
  padding: 0;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text-dim);
  min-width: 54px;
  min-height: 20px;
  text-align: center;
  cursor: pointer;
}
.timer:hover { color: var(--accent); }
.timer.paused { color: transparent; }
.timer.paused::after {
  content: "❚❚";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  font-size: 1.5em;
  letter-spacing: 0.05em;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }

.icon-btn-sm {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--text-dim);
}

/* ---------- Ad slots (reserved space, wired up later) ---------- */

.ad-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: transparent;
  opacity: 0.7;
}

.ad-top {
  margin: 16px auto 0;
  width: min(728px, calc(100% - 32px));
  height: 60px;
}

.ad-side {
  width: 160px;
  min-height: 400px;
  flex-shrink: 0;
}

.ad-footer {
  margin: 8px auto 24px;
  width: min(728px, calc(100% - 32px));
  height: 60px;
}

@media (max-width: 900px) {
  .ad-side { display: none; }
}

/* ---------- Main / board ---------- */

.game-wrap {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 24px;
  padding: 24px;
}

/* On desktop the board is the whole point of the page — center it in the
   available space instead of pinning it to the top, so it isn't stranded
   above a lot of empty room on tall viewports. Mobile keeps flex-start:
   content there usually exceeds viewport height, so top-anchoring is right. */
@media (min-width: 901px) {
  .game-wrap { align-items: center; }
}

.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.status-row {
  width: 100%;
  max-width: 560px;
}

.stats-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 16px;
  width: 100%;
  max-width: 560px;
}

.hint-btn {
  border: 1px solid var(--border);
  background: none;
  color: var(--accent);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease, opacity 0.15s ease;
}
.hint-btn:hover { background: var(--surface); }
.hint-btn:disabled {
  color: var(--text-dim);
  opacity: 0.5;
  cursor: default;
  background: none;
}

/* Check is a core, frequently-used action (like Hint) — it stays visible
   in the stats row on mobile instead of being buried in the settings
   drawer. Desktop already has room for it inline in the header, so this
   mobile-only duplicate stays hidden there. */
.check-btn-mobile { display: none; }
.hint-btn #hintCount { font-family: 'JetBrains Mono', monospace; }

.status {
  font-size: 0.9rem;
  color: var(--text-dim);
  font-weight: 500;
}

.status.error { color: var(--error); }
.status.success { color: var(--accent); }

.island-counter {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.total-solves-badge {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.8rem;
  color: var(--text-dim);
  white-space: nowrap;
}

.streak-badge {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}
.total-solves-badge .star { color: #e8b330; margin-right: 3px; }

.board {
  display: grid;
  gap: 1px;
  background: var(--island-border);
  border: 2px solid var(--island-border);
  border-radius: 8px;
  padding: 2px;
  box-shadow: var(--shadow);
  touch-action: manipulation;
  user-select: none;
  width: min(92vw, calc(var(--cols, 9) * 44px));
  max-width: 560px;
}

.cell {
  aspect-ratio: 1 / 1;
  width: 100%;
  background: var(--island);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: clamp(0.6rem, calc(4.5vw / max(1, var(--cols, 9) / 9)), 1rem);
  color: var(--clue-text);
  cursor: pointer;
  position: relative;
  transition: background 0.1s ease;
}

.cell.sea {
  background: var(--sea);
  background-image: linear-gradient(135deg, var(--sea) 0%, var(--sea-2) 100%);
}

.cell.marked-white::after {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--island-border);
}

.cell.clue {
  cursor: default;
  font-weight: 700;
}

.cell.clue.sea { color: var(--island); }

.cell.wrong {
  animation: shake 0.3s ease;
  box-shadow: inset 0 0 0 2px var(--error);
}

/* Hint-revealed cells: same colors as a normal guess, but flagged with a
   thin accent ring so it's clear the cell was given, not worked out. */
.cell.hinted { box-shadow: inset 0 0 0 2px var(--accent); }
.cell.hinted.marked-white::after { background: var(--accent); }

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

.hint {
  font-size: 0.78rem;
  color: var(--text-dim);
  max-width: 420px;
  text-align: center;
  margin: 0;
}

/* ---------- Rules ---------- */

.rules {
  max-width: 560px;
  margin: 0 auto;
  width: 100%;
  padding: 0 24px;
}

.rules-toggle {
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--text);
  font-family: 'Shippori Mincho', serif;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
  padding: 12px 0;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.rules-toggle:hover { color: var(--accent); }

.rules-toggle-arrow {
  display: inline-block;
  color: var(--accent);
  font-size: 0.75em;
  transition: transform 0.15s ease;
}
.rules-toggle[aria-expanded="true"] .rules-toggle-arrow { transform: rotate(90deg); }

.rules-body ul {
  margin: 0 0 16px;
  padding-left: 0;
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.7;
  list-style: none;
}

.rules-body li {
  padding-left: 1.3em;
  position: relative;
}
.rules-body li::before {
  content: "•";
  position: absolute;
  left: 0.15em;
  color: var(--accent);
}

.rules-body p {
  margin: 0 0 12px;
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.7;
}
.rules-body p:last-child { margin-bottom: 16px; }
.rules-body p:first-child { margin-top: 0; }

.example-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin: 8px 0 18px;
}

.example-board {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 2px;
  background: var(--island-border);
  border: 2px solid var(--island-border);
  border-radius: 6px;
  padding: 2px;
  width: 160px;
}

.ex-cell {
  aspect-ratio: 1 / 1;
  background: var(--island);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.75rem;
  color: var(--clue-text);
}
.ex-cell.ex-sea {
  background: var(--sea);
  background-image: linear-gradient(135deg, var(--sea) 0%, var(--sea-2) 100%);
}

.example-caption {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.8rem;
  line-height: 1.6;
  text-align: center;
  max-width: 420px;
}

/* ---------- Footer ---------- */

.site-footer {
  text-align: center;
  padding: 20px 16px 28px;
  margin-top: 8px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 0.78rem;
}

.site-footer span {
  font-family: 'Shippori Mincho', serif;
  letter-spacing: 0.02em;
}

/* ---------- Win overlay ---------- */

.win-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 12, 15, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.win-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 40px;
  text-align: center;
  box-shadow: var(--shadow);
}

.win-card h2 {
  font-family: 'Shippori Mincho', serif;
  font-size: 1.6rem;
  margin: 0 0 8px;
  color: var(--accent);
}

.win-card p {
  color: var(--text-dim);
  margin: 0 0 8px;
  font-family: 'JetBrains Mono', monospace;
}

.win-stars {
  font-size: 1.8rem;
  letter-spacing: 0.1em;
  margin: 4px 0 20px;
  color: var(--island-border);
}
.win-stars .filled { color: #e8b330; }

.win-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.win-stars span {
  display: inline-block;
  opacity: 0;
  transform: scale(0);
  animation: starPop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}
.win-stars span:nth-child(1) { animation-delay: 0.05s; }
.win-stars span:nth-child(2) { animation-delay: 0.25s; }
.win-stars span:nth-child(3) { animation-delay: 0.45s; }

@keyframes starPop {
  from { opacity: 0; transform: scale(0); }
  to { opacity: 1; transform: scale(1); }
}

@media (max-width: 600px) {
  /* Mobile header becomes a fixed 3-column bar: hamburger, centered
     wordmark, timer. Everything else (mode, difficulty, reset, check,
     theme) lives in a slide-in drawer so it's out of the way until asked
     for — the board should be the thing you see, not a stack of buttons. */
  .site-header {
    display: grid;
    grid-template-columns: auto 1fr auto;
    padding: 10px 14px;
    gap: 10px;
  }
  .hamburger-btn { display: flex; }
  .brand { justify-content: center; }
  .brand-text { align-items: center; }
  .brand-mark { width: 18px; height: 18px; }
  .brand-name { font-size: 1.05rem; }
  .brand-kanji { font-size: 0.6rem; }
  .timer-wrap { margin-left: 0; } /* desktop's right-grouping margin doesn't apply to the grid layout */

  .controls {
    position: fixed;
    top: 0;
    right: 0;
    height: 100%;
    width: min(80vw, 300px);
    background: var(--surface);
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
    padding: 64px 20px 24px;
    box-shadow: -8px 0 30px rgba(20, 18, 12, 0.25);
    transform: translateX(100%);
    transition: transform 0.25s ease;
    z-index: 50;
    overflow-y: auto;
  }
  .controls.open { transform: translateX(0); }

  .drawer-close-btn {
    display: flex;
    position: absolute;
    top: 14px;
    right: 14px;
    width: 32px;
    height: 32px;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    cursor: pointer;
  }

  .mode-toggle { width: 100%; }
  .mode-btn { flex: 1; padding: 10px 0; }
  .difficulty-switch { width: 100%; }
  #newGameBtn { width: 100%; }
  .controls #checkBtn { display: none; } /* lives in the stats row instead — see .check-btn-mobile */
  .controls #themeToggle { align-self: flex-start; }
  .check-btn-mobile { display: inline-block; }

  .game-wrap { padding: 10px 6px; }
  .board-column { gap: 10px; }
  .board { width: min(96vw, calc(var(--cols, 9) * 44px)); }
  .stats-row { gap: 6px 10px; }
  .hint-btn, .island-counter, .total-solves-badge, .countdown-label, .countdown, .timer {
    font-size: 0.75rem;
  }
}
