:root {
  color-scheme: light;
  --brand: #b7833a;
  --brand-strong: #8e6326;
  --brand-soft: #f0e4ce;
  --brand-contrast: #ffffff;
  --muted: #6f6a5c;
  --rule: #c6bfad;
  --line-strong: #d8cfbe;
  --paper-card: #fbf8f1;
  --paper-3: #ddd7c8;
  --paper-2: #eae5da;
  --paper: #f2eee5;
  --surface-sunken: #f3efe7;
  --ink-3: #2a2b25;
  --ink-2: #1a1b17;
  --ink: #1a1712;
  --tile-amber: #e08a2b;
  --tile-teal: #2f8f83;
  --tile-indigo: #4b4fa6;
  --tile-coral: #d95c4a;
  --tile-olive: #7a8b3c;
  --tile-plum: #7e4a72;
  --shadow-sm: 0 1px 2px rgba(26, 23, 18, 0.05), 0 1px 1px rgba(26, 23, 18, 0.04);
  --shadow-md: 0 4px 16px rgba(26, 23, 18, 0.07), 0 2px 4px rgba(26, 23, 18, 0.04);
  --shadow-lg: 0 12px 40px rgba(26, 23, 18, 0.1), 0 4px 10px rgba(26, 23, 18, 0.05);
}

:root.dark {
  color-scheme: dark;
  --brand: #d5a45a;
  --brand-strong: #f0c577;
  --brand-soft: #392f20;
  --brand-contrast: #1a1712;
  --muted: #beb7a7;
  --rule: #514a3f;
  --line-strong: #665d4e;
  --paper-card: #211f1a;
  --paper-3: #38342b;
  --paper-2: #2d2a24;
  --paper: #181713;
  --surface-sunken: #12110f;
  --ink-3: #eee8dc;
  --ink-2: #f8f2e7;
  --ink: #fbf8f1;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.28);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.38);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
select {
  font: inherit;
}

button {
  border: 0;
  background: var(--brand);
  color: var(--brand-contrast);
  min-height: 42px;
  border-radius: 8px;
  padding: 0 16px;
  font-weight: 800;
  cursor: pointer;
}

button:hover {
  background: var(--brand-strong);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.app-shell {
  width: min(1180px, calc(100% - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 32px 0;
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 320px;
  gap: 22px;
  align-items: start;
}

.brand-bar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand-mark,
.hub-link,
.network-links a,
.mini-footer a,
.mini-footer button {
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 12px;
}

.brand-mark strong,
.brand-mark small {
  display: block;
}

.brand-mark strong {
  font-size: 1rem;
}

.brand-mark small {
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
}

.mark-grid {
  display: grid;
  width: 38px;
  height: 38px;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}

.mark-grid span {
  border-radius: 4px;
}

.mark-grid span:nth-child(1) { background: var(--tile-amber); }
.mark-grid span:nth-child(2) { background: var(--tile-teal); }
.mark-grid span:nth-child(3) { background: var(--tile-indigo); }
.mark-grid span:nth-child(4) { background: var(--tile-coral); }

.hub-link,
.network-links a {
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--paper-card);
  padding: 10px 14px;
  color: var(--muted);
  font-weight: 900;
  box-shadow: var(--shadow-sm);
}

.network-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.network-links a {
  padding: 8px 11px;
  font-size: 0.84rem;
  white-space: nowrap;
}

.network-links a:hover {
  color: var(--ink);
  border-color: color-mix(in srgb, var(--tile-amber) 55%, var(--rule));
}

.game-panel,
.side-panel {
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
}

.game-panel {
  padding: clamp(16px, 3vw, 28px);
}

.side-panel {
  padding: 22px;
  position: sticky;
  top: 22px;
}

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

.eyebrow {
  margin: 0 0 4px;
  color: var(--brand);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 6vw, 4.4rem);
  line-height: 0.95;
  letter-spacing: 0;
}

h2 {
  font-size: 1.05rem;
  letter-spacing: 0;
}

.icon-button {
  width: 42px;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--rule);
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 24px 0 18px;
}

.stat {
  border: 1px solid var(--rule);
  background: var(--surface-sunken);
  border-radius: 8px;
  padding: 12px;
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

.stat strong {
  display: block;
  margin-top: 5px;
  font-size: 1.3rem;
}

.board-frame {
  width: min(72vh, 100%);
  max-width: 680px;
  margin: 0 auto;
}

.board {
  --size: 4;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  /* Without explicit rows, Grid auto-sizes each row to its content
     (tile font/line-height), which can make the board taller than wide
     and override `aspect-ratio: 1` at narrow widths — rows must be
     fractional too so both axes divide evenly into a true square. */
  grid-template-rows: repeat(var(--size), 1fr);
  /* Grid items default to a content-based automatic minimum size, which
     can exceed the aspect-ratio-derived height at narrow widths and win
     over it. Overriding min-height/min-width to 0 lets aspect-ratio
     actually govern the box instead of being overridden by tile content. */
  min-width: 0;
  min-height: 0;
  gap: clamp(6px, 1.2vw, 10px);
  aspect-ratio: 1;
  padding: clamp(8px, 1.6vw, 14px);
  background: var(--ink-2);
  border: 1px solid var(--ink-3);
  border-radius: 8px;
  touch-action: none;
  user-select: none;
}

.tile {
  display: grid;
  place-items: center;
  /* Grid items (this tile) also default to a content-based automatic
     min-height, same as the .board container above — without this, a
     tile's own bold/large numeral content can still force its row taller
     than its 1fr share even after the container itself is square. */
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-radius: 7px;
  color: #1a1712;
  /* Scales inversely with board size (36 = 6vw * 4, the size the flat
     6vw value was originally tuned for) so a 6x6 board's narrower tiles
     get proportionally smaller text instead of the same size as 4x4 —
     without this, two-digit numbers overflow their tile at larger sizes. */
  font-size: clamp(1.05rem, calc(24vw / var(--size)), 2.65rem);
  font-weight: 950;
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.16), 0 8px 20px rgba(0, 0, 0, 0.16);
  transition: transform 120ms ease, filter 120ms ease;
}

.tile:hover {
  filter: brightness(1.04);
  transform: translateY(-1px);
}

.tile.empty {
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.04), transparent), rgba(255, 255, 255, 0.07);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.1);
}

.tile.hue-0 { background: var(--tile-amber); }
.tile.hue-1 { background: var(--tile-teal); }
.tile.hue-2 { background: var(--tile-indigo); color: #fbf8f1; }
.tile.hue-3 { background: var(--tile-coral); color: #fbf8f1; }
.tile.hue-4 { background: var(--tile-olive); }
.tile.hue-5 { background: var(--tile-plum); color: #fbf8f1; }

.message {
  min-height: 24px;
  margin: 16px 0;
  color: var(--muted);
  font-weight: 700;
  text-align: center;
}

.message.win {
  color: var(--brand);
}

.controls {
  display: grid;
  grid-template-columns: 1.35fr repeat(5, minmax(82px, 1fr));
  gap: 10px;
  align-items: end;
}

.field span {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
}

select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--surface-sunken);
  color: var(--ink);
  padding: 0 12px;
  font-weight: 800;
}

.leaderboard {
  display: grid;
  gap: 10px;
  min-height: 162px;
  margin: 0;
  padding-left: 24px;
}

.leaderboard li {
  padding: 10px 12px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--surface-sunken);
  font-weight: 800;
}

.leaderboard span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.help {
  margin-top: 24px;
  padding-top: 18px;
  border-top: 1px solid var(--rule);
  color: var(--muted);
  line-height: 1.55;
}

.mini-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--rule);
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.mini-footer nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.mini-footer a,
.mini-footer button {
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
  color: var(--brand);
  font: inherit;
  font-weight: 800;
}

.guide-panel {
  grid-column: 1 / -1;
  display: grid;
  gap: 12px;
  padding: clamp(18px, 3vw, 28px);
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--paper-card);
  box-shadow: var(--shadow-md);
  color: var(--muted);
  line-height: 1.65;
}

.guide-panel h2 {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.25rem, 3vw, 1.9rem);
  letter-spacing: 0;
}

.guide-panel p {
  margin: 0;
}

.guide-panel details {
  padding: 10px 12px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--surface-sunken);
}

.guide-panel summary {
  color: var(--ink);
  cursor: pointer;
  font-weight: 900;
}

.guide-panel details p {
  margin-top: 8px;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .side-panel {
    box-shadow: none;
    position: static;
  }
}

@media (max-width: 680px) {
  .app-shell {
    width: min(100% - 20px, 560px);
    padding: 10px 0;
  }

  .brand-bar {
    align-items: flex-start;
    flex-direction: column;
  }

  .network-links {
    justify-content: flex-start;
    width: 100%;
  }

  .network-links a {
    padding: 7px 9px;
    font-size: 0.78rem;
  }

  .brand-mark small {
    display: none;
  }

  .status-grid {
    grid-template-columns: 1fr 1fr 1fr;
  }

  .controls {
    grid-template-columns: 1fr 1fr;
  }

  .field {
    grid-column: 1 / -1;
  }

  .tile {
    font-size: clamp(0.68rem, calc(36vw / var(--size)), 2rem);
  }
}

/* Compact production layout: keep the full game above the fold on desktop. */
@media (min-width: 901px) {
  .app-shell {
    width: min(1040px, calc(100% - 24px));
    min-height: 100vh;
    padding: 12px 0;
    grid-template-columns: minmax(340px, 1fr) 260px;
    gap: 12px;
  }

  .brand-bar {
    min-height: 32px;
  }

  .mark-grid {
    width: 30px;
    height: 30px;
    gap: 3px;
  }

  .brand-mark {
    gap: 9px;
  }

  .brand-mark strong {
    font-size: 0.92rem;
  }

  .brand-mark small {
    font-size: 0.76rem;
  }

  .hub-link,
  .network-links a {
    padding: 7px 11px;
    font-size: 0.84rem;
  }

  .game-panel {
    padding: 12px;
  }

  .side-panel {
    padding: 12px;
    top: 12px;
  }

  .eyebrow {
    margin-bottom: 2px;
    font-size: 0.68rem;
  }

  h1 {
    font-size: clamp(1.7rem, 3.2vw, 2.6rem);
  }

  h2 {
    margin-bottom: 10px;
    font-size: 0.98rem;
  }

  .icon-button {
    width: 34px;
    min-height: 34px;
  }

  .status-grid {
    gap: 8px;
    margin: 12px 0 10px;
  }

  .stat {
    padding: 8px 10px;
  }

  .stat span {
    font-size: 0.68rem;
  }

  .stat strong {
    margin-top: 2px;
    font-size: 1.05rem;
  }

  .board-frame {
    width: min(48vh, 100%, 440px);
    max-width: 440px;
  }

  .board {
    gap: 6px;
    padding: 9px;
  }

  .tile {
    border-radius: 6px;
    font-size: clamp(0.85rem, calc(15.6vh / var(--size)), 1.85rem);
    box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.16), 0 5px 14px rgba(0, 0, 0, 0.14);
  }

  .message {
    min-height: 20px;
    margin: 9px 0;
    font-size: 0.88rem;
  }

  .controls {
    grid-template-columns: 1.25fr repeat(3, minmax(76px, 1fr));
    gap: 8px;
  }

  .controls button,
  select {
    min-height: 36px;
    padding: 0 10px;
    font-size: 0.88rem;
  }

  .field span {
    margin-bottom: 4px;
    font-size: 0.68rem;
  }

  .leaderboard {
    gap: 7px;
    min-height: 0;
    padding-left: 20px;
  }

  .leaderboard li {
    padding: 7px 9px;
    font-size: 0.9rem;
  }

  .leaderboard span {
    font-size: 0.74rem;
  }

  .help {
    margin-top: 14px;
    padding-top: 12px;
    font-size: 0.88rem;
    line-height: 1.42;
  }

  .help p {
    margin-bottom: 0;
  }

  .mini-footer {
    margin-top: 13px;
    padding-top: 11px;
    font-size: 0.78rem;
  }
}

@media (min-width: 901px) and (max-height: 760px) {
  .brand-mark small,
  .help {
    display: none;
  }

  .app-shell {
    padding: 8px 0;
    gap: 8px;
  }

  .game-panel,
  .side-panel {
    padding: 10px;
  }

  .board-frame {
    width: min(44vh, 100%, 400px);
  }

  h1 {
    font-size: clamp(1.5rem, 2.6vw, 2.1rem);
  }

  .status-grid {
    margin: 6px 0;
  }

  .message {
    margin: 5px 0;
  }
}

/* Footer ad band -- mirrors the classicgames/neonattack pattern
   (ca-pub-4269583761744184, shared display slot 2790900580): labeled,
   height-reserved, and placed below the game/sidebar row so it never
   sits next to the board, controls, or leaderboard. */
.ad-footer {
  grid-column: 1 / -1;
  width: 100%;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-radius: 8px;
  border: 1px solid var(--rule);
  background: var(--surface-sunken);
}

.ad-footer p {
  margin: 0;
  padding: 8px 12px 0;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.ad-footer .adsbygoogle {
  display: block;
  flex: 1;
  min-height: 90px;
}

.cookie-banner {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  z-index: 60;
  display: flex;
  justify-content: center;
  padding: 16px;
}

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

.cookie-banner-inner {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 640px;
  width: 100%;
  background: var(--paper-card);
  border: 1px solid var(--rule);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  padding: 14px 18px;
}

.cookie-banner p {
  flex: 1;
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--muted);
}

.cookie-banner a {
  color: var(--muted);
  text-decoration: underline;
}

.cookie-banner-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.cookie-banner-actions button {
  min-height: 32px;
  padding: 0 12px;
  font-size: 0.78rem;
}

#rejectCookies {
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--muted);
}

#rejectCookies:hover {
  background: var(--paper);
}

@media (max-width: 680px) {
  .cookie-banner-inner {
    flex-direction: column;
    align-items: stretch;
  }

  .cookie-banner-actions {
    justify-content: flex-end;
  }
}

/* Responsive fit pass: compact the puzzle controls so the board and controls fit on phones/tablets. */
.mini-footer button {
  min-height: 34px;
  padding: 0 8px;
}

@media (max-width: 900px) {
  .game-panel {
    padding: 10px;
  }

  .topbar {
    gap: 8px;
  }

  h1 {
    font-size: clamp(1.55rem, 8vw, 2.4rem);
  }

  .status-grid {
    gap: 6px;
    margin: 10px 0 8px;
  }

  .stat {
    padding: 7px 8px;
  }

  .stat strong {
    font-size: 1rem;
  }

  .board-frame {
    width: min(46svh, 100%, 430px);
  }

  .message {
    min-height: 18px;
    margin: 8px 0;
    font-size: 0.84rem;
  }

  .controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 7px;
  }

  .field {
    grid-column: auto;
  }

  .controls button,
  select {
    min-height: 38px;
    padding: 0 7px;
    font-size: 0.78rem;
  }
}

@media (max-height: 480px) and (orientation: landscape) {
  .app-shell {
    width: min(100% - 12px, 900px);
    min-height: 100svh;
    padding: 6px 0;
    gap: 6px;
  }

  .brand-bar {
    min-height: 28px;
  }

  .brand-mark small,
  .hub-link,
  .side-panel {
    display: none;
  }

  .game-panel {
    display: grid;
    grid-template-columns: minmax(170px, 42svh) minmax(260px, 1fr);
    grid-template-areas:
      "top stats"
      "board controls"
      "message controls";
    gap: 8px 10px;
    align-items: center;
    padding: 8px;
  }

  .topbar {
    grid-area: top;
  }

  h1 {
    font-size: clamp(1.35rem, 4vw, 1.8rem);
  }

  .status-grid {
    grid-area: stats;
    margin: 0;
  }

  .board-frame {
    grid-area: board;
    width: min(42svh, 100%);
  }

  .message {
    grid-area: message;
    margin: 0;
    text-align: left;
  }

  .controls {
    grid-area: controls;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-self: center;
  }
}

/* Responsive fit pass follow-up: preserve usable tile and icon targets. */
.icon-button {
  flex: 0 0 42px;
  min-width: 42px;
}

@media (max-width: 900px) and (orientation: portrait) {
  .board-frame {
    width: min(44svh, 100%, 430px);
  }
}

@media (max-height: 480px) and (orientation: landscape) {
  .icon-button {
    flex-basis: 34px;
    min-width: 34px;
    width: 34px;
  }

  .game-panel {
    grid-template-columns: minmax(200px, 50svh) minmax(260px, 1fr);
  }

  .board-frame {
    width: min(50svh, 100%);
  }
}

.related-games {
  grid-column: 1 / -1;
  display: grid;
  gap: 12px;
  padding: clamp(16px, 3vw, 24px);
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--paper-card);
  box-shadow: var(--shadow-md);
}

.related-games h2 {
  margin: 0;
  color: var(--ink);
  font-size: clamp(1.2rem, 2.6vw, 1.7rem);
  letter-spacing: 0;
}

.related-games nav {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.related-games a {
  display: grid;
  gap: 5px;
  min-width: 0;
  min-height: 72px;
  padding: 10px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--surface-sunken);
  color: var(--muted);
  text-decoration: none;
}

.related-games strong {
  color: var(--ink);
  font-size: 0.9rem;
  line-height: 1.15;
}

.related-games span {
  font-size: 0.78rem;
  line-height: 1.35;
}

.related-games strong,
.related-games span {
  min-width: 0;
  overflow-wrap: anywhere;
}

@media (max-width: 820px) {
  .related-games nav {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 420px) {
  .related-games {
    padding: 14px;
  }

  .related-games nav {
    grid-template-columns: 1fr;
  }

  .related-games a {
    min-height: 0;
  }
}
