:root {
  color-scheme: light dark;
  --page: #f3f4f7;
  --ink: #1d2029;
  --muted: #6a7180;
  --tray: #dde1e9;
  --tray-edge: #c9cfdb;
  --tile: #fbfcfe;
  --tile-top: #fff;
  --tile-shade: #aeb6c5;
  --tile-hover: #fff;
  --filled: #344864;
  --filled-edge: #24364f;
  --grid: #aeb6c5;
  --grid-heavy: #697386;
  --focus: #f08a3c;
  --success: #2e8540;
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #14161c;
    --ink: #e8e9ee;
    --muted: #9aa0ad;
    --tray: #1f232c;
    --tray-edge: #2d323e;
    --tile: #3b4354;
    --tile-top: #4a5367;
    --tile-shade: #171a21;
    --tile-hover: #465066;
    --filled: #90acd2;
    --filled-edge: #b7cae4;
    --grid: #60697a;
    --grid-heavy: #aab1bf;
    --success: #7fd48a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 1rem;
  display: flex;
  justify-content: center;
  font-family: system-ui, sans-serif;
  color: var(--ink);
  background: var(--page);
  touch-action: manipulation;
  container-type: inline-size;
}

button, select { font: inherit; }

.layout {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: min(100%, 760px);
}

.topbar, .game-header, .hud, .actions, footer {
  display: flex;
  align-items: center;
}

.topbar, .game-header, footer { justify-content: space-between; }
.topbar { min-height: 1.75rem; gap: 1rem; }
.home-link { color: inherit; font-weight: 650; text-decoration: none; }
.home-link:hover { text-decoration: underline; }
.account { display: flex; align-items: center; gap: 0.65rem; text-align: right; }
.account a { color: inherit; font-weight: 600; }
.account button { border: 0; padding: 0; color: inherit; background: transparent; text-decoration: underline; cursor: pointer; }

.game-header { gap: 1rem; }
h1, h2, p { margin: 0; }
h1 { font-size: clamp(1.6rem, 5vw, 2.35rem); line-height: 1; }
.eyebrow { margin-bottom: 0.25rem; color: var(--muted); font-size: 0.75rem; font-weight: 750; letter-spacing: 0.08em; text-transform: uppercase; }

.button, .primary-button, .tool, .puzzle-card, .pack-tabs button, .close-button {
  border: 1px solid var(--tray-edge);
  border-radius: 7px;
  color: var(--ink);
  background: var(--tile);
  box-shadow: inset 0 2px 0 var(--tile-top), 0 2px 0 var(--tile-shade);
  cursor: pointer;
}
.button, .primary-button { padding: 0.55rem 0.8rem; font-weight: 650; }
.button:hover, .tool:hover, .puzzle-card:hover, .pack-tabs button:hover { background: var(--tile-hover); }
.button:active, .primary-button:active, .tool:active, .puzzle-card:active { transform: translateY(1px); box-shadow: inset 0 1px 0 var(--tile-top), 0 1px 0 var(--tile-shade); }
.button:disabled { opacity: 0.45; cursor: default; }
.primary-button { border-color: #e47b2c; color: #fff; background: #e47b2c; box-shadow: inset 0 2px 0 #f5ad74, 0 2px 0 #a84b12; }

.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "puzzle tools time";
  align-items: center;
  gap: 0.75rem;
}
.hud-item { display: flex; flex-direction: column; align-items: center; min-width: 4.5rem; font-size: 0.8rem; color: var(--muted); }
.hud-item:first-child { justify-self: start; }
.hud-item:last-child { justify-self: end; }
.hud-item:first-child { grid-area: puzzle; }
.hud-item:last-child { grid-area: time; }
.tools { grid-area: tools; }
.hud-item strong { color: var(--ink); font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.tools { display: flex; gap: 3px; max-width: 100%; padding: 4px; overflow-x: auto; border: 1px solid var(--tray-edge); border-radius: 10px; background: var(--tray); }
.tool { display: flex; align-items: center; gap: 0.4rem; padding: 0.48rem 0.7rem; border: 0; font-size: 0.875rem; font-weight: 650; }
.tool.active { background: var(--page); box-shadow: inset 0 1px 2px var(--tile-shade); }
.tool-icon { display: inline-grid; place-items: center; width: 1rem; height: 1rem; }
.fill-icon {
  border-radius: 2px;
  background: var(--tool-color, var(--filled));
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / 25%), 0 0 0 1px rgb(0 0 0 / 25%);
}
.cross-icon { font-size: 1.4rem; line-height: 1; }

.board-shell { position: relative; min-width: 0; overflow: auto; padding: 1px; }
.board {
  --cell-size: 44px;
  display: grid;
  width: max-content;
  min-width: min-content;
  margin: 0 auto;
  padding: 10px;
  border: 1px solid var(--tray-edge);
  border-radius: 10px;
  background: var(--tray);
  user-select: none;
  -webkit-user-select: none;
  /* The fitted board does not need to pan, so a drag can paint cells. The
     optional zoomed view restores touch panning for precise phone input. */
  touch-action: none;
}

.board.zoomed {
  margin-left: 0;
  touch-action: pan-x pan-y pinch-zoom;
}
.clue-corner { min-width: 3.5rem; min-height: 3.5rem; }
.column-clue, .row-clue { display: flex; font-size: clamp(0.65rem, calc(var(--cell-size) * 0.32), 0.85rem); font-weight: 750; font-variant-numeric: tabular-nums; }
.column-clue { flex-direction: column; justify-content: flex-end; align-items: center; gap: 0.05rem; min-height: 3.5rem; padding-bottom: 0.3rem; }
.row-clue { justify-content: flex-end; align-items: center; gap: 0.35rem; min-width: 3.5rem; padding-right: 0.5rem; }
.column-clue.complete, .row-clue.complete { color: var(--success); opacity: 0.65; }
.clue-number { color: var(--clue-light, var(--ink)); }
.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  padding: 0;
  border: 0 solid var(--grid);
  border-right-width: 1px;
  border-bottom-width: 1px;
  border-radius: 0;
  color: var(--muted);
  background: var(--tile);
  font: 500 calc(var(--cell-size) * 0.72) / 1 system-ui, sans-serif;
  cursor: crosshair;
}
.cell[data-col="0"] { border-left-width: 2px; border-left-color: var(--grid-heavy); }
.cell[data-row="0"] { border-top-width: 2px; border-top-color: var(--grid-heavy); }
.cell:nth-child(5n) { /* harmless fallback; data-based heavy lines are below */ }
.cell[data-col="4"], .cell[data-col="9"], .cell[data-col="14"], .cell[data-col="19"], .cell[data-col="24"], .cell[data-col="29"], .cell[data-col="34"], .cell[data-col="39"], .cell[data-col="44"], .cell[data-col="49"] { border-right-width: 2px; border-right-color: var(--grid-heavy); }
.cell[data-row="4"], .cell[data-row="9"], .cell[data-row="14"], .cell[data-row="19"], .cell[data-row="24"], .cell[data-row="29"], .cell[data-row="34"], .cell[data-row="39"], .cell[data-row="44"], .cell[data-row="49"] { border-bottom-width: 2px; border-bottom-color: var(--grid-heavy); }
.cell:hover { background: var(--tile-hover); }
.cell.painted { background: var(--cell-color); box-shadow: inset 0 0 0 1px rgb(0 0 0 / 22%); }
.cell.marked { background: var(--tile); }
.cell:focus-visible, button:focus-visible, a:focus-visible { position: relative; z-index: 2; outline: 3px solid var(--focus); outline-offset: -2px; }

.overlay {
  position: absolute;
  inset: 1px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.7rem;
  min-width: max-content;
  padding: 1.5rem;
  border-radius: 10px;
  color: #fff;
  text-align: center;
  background: rgb(17 20 26 / 86%);
}
.save-note { max-width: 26rem; }
.actions { justify-content: center; gap: 0.5rem; flex-wrap: wrap; }
.hint { margin-left: 0.5rem; color: var(--muted); font-size: 0.8rem; }
.muted { color: var(--muted); }

.leaderboard-section { padding: 1rem; border: 1px solid var(--tray-edge); border-radius: 10px; background: var(--tray); }
.leaderboard-heading { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin-bottom: 0.75rem; }
.leaderboard-heading h2 { font-size: 1.15rem; }
.leaderboard-label { color: var(--muted); font-size: 0.75rem; font-weight: 650; }
.rank-list { display: flex; flex-direction: column; gap: 0.3rem; margin: 0; padding: 0; list-style: none; }
.rank-list li { display: grid; grid-template-columns: 2.5rem minmax(0, 1fr) auto; gap: 0.6rem; align-items: center; padding: 0.4rem 0.55rem; border-radius: 6px; background: var(--tile); }
.rank-list li.me { outline: 2px solid var(--focus); }
.rank-position, .rank-score { font-weight: 700; font-variant-numeric: tabular-nums; }
.rank-position { color: var(--muted); }
.rank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.my-rank { margin-top: 0.65rem; font-size: 0.85rem; }

.puzzle-dialog { width: min(92vw, 650px); max-height: 82vh; padding: 1.25rem; border: 1px solid var(--tray-edge); border-radius: 12px; color: var(--ink); background: var(--page); box-shadow: 0 20px 60px rgb(0 0 0 / 35%); }
.puzzle-dialog::backdrop { background: rgb(0 0 0 / 60%); }
.dialog-header { display: flex; justify-content: space-between; align-items: start; margin-bottom: 1rem; }
.close-button { width: 2.25rem; height: 2.25rem; padding: 0; font-size: 1.5rem; }
.pack-tabs { display: flex; gap: 0.4rem; overflow-x: auto; padding: 3px 3px 6px; }
.pack-tabs button { flex: 0 0 auto; padding: 0.45rem 0.7rem; font-weight: 650; }
.pack-tabs button.active { background: var(--filled); color: var(--page); box-shadow: none; }
.pack-description { margin: 0.5rem 0 1rem; color: var(--muted); }
.puzzle-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr)); gap: 0.6rem; }
.puzzle-card { position: relative; min-height: 5.5rem; padding: 0.7rem; text-align: left; }
.puzzle-card.completed { padding-right: 2.5rem; }
.puzzle-card strong, .puzzle-card span { display: block; }
.puzzle-card span { margin-top: 0.25rem; color: var(--muted); font-size: 0.8rem; }
.puzzle-card.completed::after {
  content: '✓';
  position: absolute;
  top: 0.65rem;
  right: 0.65rem;
  display: grid;
  place-items: center;
  width: 1.15rem;
  height: 1.15rem;
  border: 2px solid currentColor;
  border-radius: 3px;
  color: var(--success);
  font-size: 0.85rem;
  font-weight: 850;
  line-height: 1;
}
.puzzle-card.current { outline: 2px solid var(--focus); }
footer { color: var(--muted); font-size: 0.75rem; }

@media (prefers-color-scheme: dark) {
  .clue-number { color: var(--clue-dark, var(--ink)); }
}

@container (max-width: 30rem) {
  body { padding: 0.75rem; }
  .account strong { display: none; }
  .game-header { align-items: end; }
  .hint { width: 100%; margin: 0.3rem 0 0; text-align: center; }
  .tool { padding-inline: 0.55rem; }
  .hud { grid-template-columns: 1fr 1fr; grid-template-areas: "puzzle time" "tools tools"; }
  .tools { justify-self: center; }
}
