/* ---------- Theme ---------- */
:root {
  --bg: #121213;
  --text: #fff;
  --muted: #818384;
  --border: #3a3a3c;
  --border-strong: #565758;
  --green: #538d4e;
  --yellow: #b59f3b;
  --grey: #3a3a3c;
  --toast-bg: #fff;
  --toast-text: #000;
  --error: #e23b3b;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #fff;
  --text: #000;
  --muted: #787c7e;
  --border: #d3d6da;
  --border-strong: #878a8c;
  --green: #6aaa64;
  --yellow: #c9b458;
  --grey: #787c7e;
  --toast-bg: #000;
  --toast-text: #fff;
  color-scheme: light;
}

/* ---------- Sizing ----------
   The board is as large as fits both the width (beside the palette and reroll gutter)
   and the height (minus everything else on the page), capped at --max. */
:root {
  --vh: 100vh;
  --header: 65px;
  --sw: 44px;        /* swatch size */
  --gap: 14px;       /* palette ↔ board */
  --gutter: 34px;    /* room for row reroll buttons */
  --chrome: 290px;   /* vertical space used by everything except the board */
  --max: 350px;
  --fit-w: calc(100vw - 32px - var(--sw) - var(--gap) - var(--gutter));
  --fit-h: calc((var(--vh) - var(--chrome)) * 5 / 6);
  --board: max(200px, min(var(--max), var(--fit-w), var(--fit-h)));
}
@supports (height: 100dvh) { :root { --vh: 100dvh; } }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: var(--vh);
  background: var(--bg);
  color: var(--text);
  font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
svg { width: 24px; height: 24px; fill: currentColor; display: block; }
.muted { color: var(--muted); }

.icon-btn { background: none; border: 0; padding: 6px; border-radius: 4px; display: inline-flex; }
@media (hover: hover) {
  .icon-btn:hover { background: color-mix(in srgb, var(--text) 10%, transparent); }
}

/* ---------- Header ---------- */
.header {
  position: relative;
  height: var(--header);
  display: flex; align-items: center; justify-content: center;
  border-bottom: 1px solid var(--border);
  padding: 0 56px;
}
.title {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 36px; font-weight: 700; white-space: nowrap;
}
.theme-btn { position: absolute; right: max(12px, env(safe-area-inset-right)); }

/* ---------- Layout ---------- */
.main {
  display: grid; justify-items: center; align-content: start;
  grid-template-areas: "target" "board" "presets" "actions";
  gap: 14px;
  padding: 12px max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.target { grid-area: target; }
.board-wrap { grid-area: board; }
.presets { grid-area: presets; }
.actions { grid-area: actions; }

/* ---------- Target word ---------- */
.target {
  width: min(100%, calc(var(--board) + var(--sw) + var(--gap) + var(--gutter)));
  height: 40px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.tabs { display: flex; gap: 14px; }
.tab {
  background: none; border: 0; padding: 6px 0;
  color: var(--muted); font-size: 13px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase;
  border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--text); }
.panel { display: flex; align-items: center; gap: 4px; font-size: 14px; }
.panel[hidden] { display: none; }
.answer { color: var(--text); font-weight: 700; letter-spacing: 0.15em; margin-left: 6px; }
#revealBtn.on .eye, #revealBtn:not(.on) .eye-off { display: none; }
#customInput {
  width: 140px; padding: 4px; text-align: center;
  background: transparent; color: var(--text);
  border: 2px solid var(--border); border-radius: 0; outline: none;
  font-size: 20px; font-weight: 700; letter-spacing: 0.3em; text-transform: uppercase;
}
#customInput:focus { border-color: var(--border-strong); }
#customInput::placeholder { color: var(--border); }

/* ---------- Board ---------- */
.board-wrap { display: flex; align-items: center; gap: var(--gap); padding-right: var(--gutter); }
.board {
  width: var(--board);
  display: grid; gap: 5px;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.row { position: relative; display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.row.error .tile { box-shadow: inset 0 0 0 2px var(--error); }
.tile {
  aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--border);
  font-size: calc(var(--board) * 0.09); font-weight: 700; text-transform: uppercase;
  cursor: crosshair;
}
.tile.g, .tile.y, .tile.b { border-color: transparent; color: #fff; }
.tile.g { background: var(--green); }
.tile.y { background: var(--yellow); }
.tile.b { background: var(--grey); }

.row-reroll {
  position: absolute; left: calc(100% + 4px); top: 50%; transform: translateY(-50%);
  color: var(--muted); padding: 4px;
}
.row-reroll svg { width: 20px; height: 20px; }
.row-reroll[hidden] { display: none; }

/* ---------- Palette + presets ---------- */
.palette { display: flex; flex-direction: column; gap: calc(var(--sw) / 4); }
.swatch { width: var(--sw); height: var(--sw); border: 0; border-radius: 4px; padding: 0; }
.swatch.g { background: var(--green); }
.swatch.y { background: var(--yellow); }
.swatch.b { background: var(--grey); }
.swatch.e { background: none; border: 2px solid var(--border); }
.swatch.active { outline: 2px solid var(--text); outline-offset: 3px; }
.swatch:active { transform: scale(0.92); }

.presets { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; }
.preset {
  display: inline-flex; padding: 5px;
  background: none; border: 1px solid var(--border); border-radius: 4px; color: var(--muted);
}
.preset svg { width: 20px; height: 20px; margin: 2px; }
.mini { display: grid; grid-template-columns: repeat(5, 4px); grid-auto-rows: 4px; gap: 1px; }
.mini i { border-radius: 1px; background: color-mix(in srgb, var(--border) 50%, transparent); }
.mini i.g { background: var(--green); }
.mini i.y { background: var(--yellow); }
.mini i.b { background: var(--grey); }
@media (hover: hover) {
  .preset:hover { border-color: var(--border-strong); color: var(--text); }
  .row-reroll:hover { color: var(--text); background: none; }
}

/* ---------- Actions ---------- */
.actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 10px 14px; }
.toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.toggle input { width: 15px; height: 15px; margin: 0; accent-color: var(--green); }
.key {
  height: 48px; padding: 0 28px; border: 0; border-radius: 4px;
  background: var(--green); color: #fff;
  font-size: 14px; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase;
}
.key:active { filter: brightness(0.9); }
.result-actions { display: flex; }
.result-actions.off { visibility: hidden; }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; top: calc(var(--header) + 16px); left: 50%; transform: translateX(-50%); z-index: 10;
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
}
.toast {
  max-width: 90vw; padding: 13px 16px; border-radius: 4px;
  background: var(--toast-bg); color: var(--toast-text);
  font-size: 14px; font-weight: 700; text-align: center;
  transition: opacity 300ms;
}

/* ---------- Responsive (kept last so these override the rules above) ---------- */

/* Phones */
@media (max-width: 480px) {
  :root { --header: 50px; --sw: 38px; --gap: 10px; --gutter: 30px; --chrome: 265px; --max: 400px; }
  .header { padding: 0 48px; }
  .title { font-size: 26px; }
  .key { padding: 0 20px; }
}
@media (max-width: 340px) {
  :root { --sw: 32px; --gap: 8px; --gutter: 28px; }
  .title { font-size: 22px; }
}

/* Tablets and other tall screens */
@media (min-width: 700px) and (min-height: 900px) {
  :root { --sw: 52px; --gap: 18px; --gutter: 40px; --max: 440px; --chrome: 320px; }
}

/* Phones in landscape: board on the left, controls on the right */
@media (orientation: landscape) and (max-height: 560px) {
  :root {
    --header: 44px; --sw: 36px; --gap: 10px; --gutter: 30px; --chrome: 76px; --max: 400px;
    --fit-w: calc(100vw - 340px - var(--sw) - var(--gap) - var(--gutter));
  }
  .title { font-size: 22px; }
  .main {
    grid-template-columns: auto minmax(0, 280px);
    grid-template-rows: 1fr auto auto auto 1fr; /* controls centred beside the board */
    grid-template-areas: "board ." "board target" "board presets" "board actions" "board .";
    align-content: center; justify-content: center; column-gap: 32px; row-gap: 18px;
  }
  .target { width: 100%; }
}
