/*
 * MatchstickGames — colours, type and the game: the board, matches, the row of controls, hints.
 * Every page loads it first; the frame and the pages around the game are in site.css.
 *
 * Variant 2 «Зошит» (docs/design/v2-notebook.html, docs/design/README.md): the light theme is warm
 * paper with blue ink, the dark one neutral graphite with the same ink; the system chooses (prefers-color-scheme), unless
 * the visitor picks a theme in the footer (data-theme on <html>, see the dark block below).
 * The board and the page are plain. game.js writes no colours: it gives the SVG it draws classes
 * (.guide, .vertex, .shape, .cherry…, .tp-line), and the colours below follow the theme.
 * Spacing: 4 · 8 · 12 · 16 · 24 · 32 · 48.
 */

:root {
  --page: #fbfaf5;
  --side: #f3f1e8;
  --ink: #1f2633;
  --muted: #5f6676;
  --line: #e1ddcf;
  --accent: #2346a0;
  --accent-soft: rgba(35, 70, 160, 0.08);
  --ok: #22794a;
  --danger: #c0392b;

  --board-bg: #fdfcf8;
  --board-grid: rgba(35, 70, 160, 0.13);
  --board-vertex: rgba(35, 70, 160, 0.35);
  --shape-fill: rgba(35, 70, 160, 0.06);
  --shape-stroke: rgba(35, 70, 160, 0.45);
  --won-fill: rgba(34, 121, 74, 0.1);
  --won-stroke: rgba(34, 121, 74, 0.6);
  /* Parts of a divided figure: six tints, drawn under the matches with the grid showing through */
  --part-alpha: 0.17;
  --part-label: rgba(31, 38, 51, 0.62);
  /* Fixed matches (puzzle.fixed, the outline of "divide the figure"): graphite, they do not move */
  --fixed-wood: linear-gradient(90deg, #6f7581 0%, #a3a8b1 7%, #8b919c 72%, #4b505a 83%, #2c3038 89%, #1d2026 100%);
  --fixed-flat: #7d838e;
  --fixed-head: #2a2e36;
  /* Light boards: the site's birch (#efd2a0) nearly vanishes on paper, so the wood is darker here */
  --match-wood: linear-gradient(90deg, #a8733a 0%, #d8ab6c 7%, #d0a061 72%, #7a4518 83%, #3b1d0b 89%, #261105 100%);
  --wood-flat: #c9975a;
  --match-head: #d6311f;
  --match-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  --cherry: #e11d48;
  --cherry-ring: #fda4af;
  --cherry-stem: #3f9d55;
  --pop-bg: #fff;
  --pop-fg: #1f2633;
  --pop-border: var(--line);
  --ghost-ok: #2346a0;
  --ghost-bad: #c0392b;
  --ad-fg: rgba(0, 0, 0, 0.45);
  --ad-border: rgba(0, 0, 0, 0.2);

  --radius: 8px;
  color-scheme: light;
}

/* The dark theme: by the system (prefers-color-scheme) unless the visitor chose light, or always when
   they chose dark. The theme switch in the footer (layout.php theme_switch(), js/site.js) puts
   data-theme="light" | "dark" on <html>; the inline script in <head> (head.php) does it before the
   first paint, so a chosen theme does not flash the other one. The block is written twice because
   CSS cannot share one set of declarations between a media query and a plain selector: change both.

   Neutral graphite with the light theme's blue ink, lightened. The first dark theme was a green
   blackboard with chalk-yellow accents, and players called it swampy (18.10.2026) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --page: #16171a;
    --side: #1c1d21;
    --ink: #e8e9ec;
    --muted: #9a9ea7;
    --line: #2e3036;
    --accent: #8fb0ff;
    --accent-soft: rgba(143, 176, 255, 0.12);
    --ok: #7bd39a;
    --danger: #ff8a80;

    --board-bg: #1e2024;
    --board-grid: rgba(143, 176, 255, 0.14);
    --board-vertex: rgba(143, 176, 255, 0.35);
    --shape-fill: rgba(143, 176, 255, 0.07);
    --shape-stroke: rgba(143, 176, 255, 0.5);
    --won-fill: rgba(123, 211, 154, 0.1);
    --won-stroke: rgba(123, 211, 154, 0.6);
    --part-alpha: 0.2;
    --part-label: rgba(232, 233, 236, 0.66);
    --fixed-wood: linear-gradient(90deg, #8e939c 0%, #d3d6db 7%, #bfc3ca 72%, #6c717a 83%, #3b3f46 89%, #2a2d33 100%);
    --fixed-flat: #b4b8c0;
    --fixed-head: #e8e9ec;
    /* Dark boards keep the site's birch */
    --match-wood: linear-gradient(90deg, #d19e61 0%, #f5deb5 7%, #efd2a0 72%, #8f5522 83%, #3b1d0b 89%, #261105 100%);
    --wood-flat: #e3c08c;
    --match-shadow: 0 2px 4px rgba(0, 0, 0, 0.55);
    --cherry-stem: #4ade80;
    --pop-bg: #26282d;
    --pop-fg: #e8e9ec;
    --pop-border: #3a3d44;
    --ghost-ok: #8fb0ff;
    --ghost-bad: #ff8a80;
    --ad-fg: rgba(255, 255, 255, 0.45);
    --ad-border: rgba(255, 255, 255, 0.2);

    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --page: #16171a;
  --side: #1c1d21;
  --ink: #e8e9ec;
  --muted: #9a9ea7;
  --line: #2e3036;
  --accent: #8fb0ff;
  --accent-soft: rgba(143, 176, 255, 0.12);
  --ok: #7bd39a;
  --danger: #ff8a80;

  --board-bg: #1e2024;
  --board-grid: rgba(143, 176, 255, 0.14);
  --board-vertex: rgba(143, 176, 255, 0.35);
  --shape-fill: rgba(143, 176, 255, 0.07);
  --shape-stroke: rgba(143, 176, 255, 0.5);
  --won-fill: rgba(123, 211, 154, 0.1);
  --won-stroke: rgba(123, 211, 154, 0.6);
  --part-alpha: 0.2;
  --part-label: rgba(232, 233, 236, 0.66);
  --fixed-wood: linear-gradient(90deg, #8e939c 0%, #d3d6db 7%, #bfc3ca 72%, #6c717a 83%, #3b3f46 89%, #2a2d33 100%);
  --fixed-flat: #b4b8c0;
  --fixed-head: #e8e9ec;
  /* Dark boards keep the site's birch */
  --match-wood: linear-gradient(90deg, #d19e61 0%, #f5deb5 7%, #efd2a0 72%, #8f5522 83%, #3b1d0b 89%, #261105 100%);
  --wood-flat: #e3c08c;
  --match-shadow: 0 2px 4px rgba(0, 0, 0, 0.55);
  --cherry-stem: #4ade80;
  --pop-bg: #26282d;
  --pop-fg: #e8e9ec;
  --pop-border: #3a3d44;
  --ghost-ok: #8fb0ff;
  --ghost-bad: #ff8a80;
  --ad-fg: rgba(255, 255, 255, 0.45);
  --ad-border: rgba(255, 255, 255, 0.2);

  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

[hidden] {
  display: none !important;
}

html {
  background: var(--page);
  -webkit-text-size-adjust: 100%;
}

body {
  background: var(--page);
  color: var(--ink);
  font: 16px/1.55 Commissioner, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Japanese (/ja/): no web font, since a Japanese one weighs megabytes; the system Gothic of macOS/iOS,
   Windows and Android instead. lang="ja" on <html> also makes the browser pick the Japanese forms of
   the kanji shared with Chinese. Kanji need more room between lines; line-break: strict keeps small
   kana and ー off the start of a line; headings break between phrases where the browser can
   (word-break: auto-phrase, Chrome 119+), elsewhere anywhere as usual. The Latin tracking of small
   labels and headings (letter-spacing, uppercase) only spreads kana apart, so it is off here */
body:lang(ja) {
  font-family: 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', 'Yu Gothic UI', 'Yu Gothic', Meiryo, 'Noto Sans JP', sans-serif;
  line-height: 1.75;
  line-break: strict;
}

:lang(ja) h1,
:lang(ja) h2,
:lang(ja) .tile-title,
:lang(ja) .victory-title {
  line-height: 1.45;
  word-break: auto-phrase;
}

:lang(ja) h1,
:lang(ja) .brand,
:lang(ja) .ad,
:lang(ja) .target-preview-label {
  letter-spacing: normal;
  text-transform: none;
}

/* Links are told by their colour; the underline comes on hover and keyboard focus only (owner's
   decision, 19.10.2026: underlines everywhere made the pages busy). :where() keeps the hover rule
   at the weight of a bare "a", so cards, the tree and buttons that say text-decoration: none stay
   without it even on hover */
a {
  color: var(--accent);
  text-decoration: none;
  text-underline-offset: 3px;
}

a:where(:hover, :focus-visible) {
  text-decoration: underline;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* =========================================================================
   Buttons
   ========================================================================= */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--page);
  color: var(--ink);
  font-size: 15px;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
}

.btn svg {
  width: 16px;
  height: 16px;
}

.btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn-primary,
.btn-primary:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--page);
}

.btn-primary:hover {
  opacity: 0.9;
}

/* =========================================================================
   The row above the board: moves, status, solved badge, Undo, Reset
   ========================================================================= */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  margin-top: 16px;
  user-select: none;
}

.moves-counter {
  font-size: 15px;
  white-space: nowrap;
}

.moves-label {
  color: var(--muted);
  margin-right: 4px;
}

.moves-value {
  font-variant-numeric: tabular-nums;
}

.moves-value strong {
  font-size: 18px;
}

.moves-counter.limit-reached {
  color: var(--danger);
}

.moves-counter.limit-reached .moves-label {
  color: inherit;
}

.moves-counter.won {
  color: var(--ok);
}

.constraint-pill {
  font-size: 14px;
  color: var(--muted);
}

.constraint-pill:empty {
  display: none;
}

.badge-solved {
  font-size: 14px;
  font-weight: 600;
  color: var(--ok);
}

.controls .tools {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

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

.shake {
  animation: shakeReject 0.28s ease;
}

/* =========================================================================
   The board
   ========================================================================= */
.board-wrap {
  position: relative;
  margin-top: 12px;
}

.board-viewport {
  position: relative;
  height: clamp(360px, calc(100vh - 300px), 640px);
  background: var(--board-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* The start position drawn by PHP, until the game takes over the board */
.board-static {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8%;
}

.board-static .board-svg {
  width: 100%;
  height: 100%;
}

.board-grid-svg,
.board-overlay-svg,
.matches-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.board-grid-svg {
  pointer-events: none;
  z-index: 4;
}

.board-overlay-svg {
  pointer-events: none;
  z-index: 8;
}

.matches-layer {
  z-index: 12;
}

.board-grid-svg .guide {
  stroke: var(--board-grid);
}

.board-grid-svg .vertex {
  fill: var(--board-vertex);
}

/* Equations: the segment slots are only a hint; full-strength guides and dots compete with the digits */
.board-viewport[data-grid="equation"] .board-grid-svg .guide {
  opacity: 0.5;
}

.board-viewport[data-grid="equation"] .board-grid-svg .vertex {
  opacity: 0.2;
}

.board-overlay-svg .shape {
  fill: var(--shape-fill);
  stroke: var(--shape-stroke);
}

.board-overlay-svg .shape.won {
  fill: var(--won-fill);
  stroke: var(--won-stroke);
}

/* "Divide the figure": each part in its own tint, its area in the middle (game.js renderParts) */
.board-overlay-svg .part {
  fill-opacity: var(--part-alpha);
}

.board-overlay-svg .part-0 { fill: #2f9e5b; }
.board-overlay-svg .part-1 { fill: #3d6fd6; }
.board-overlay-svg .part-2 { fill: #e07b39; }
.board-overlay-svg .part-3 { fill: #9b5cc9; }
.board-overlay-svg .part-4 { fill: #d4a017; }
.board-overlay-svg .part-5 { fill: #d9486e; }

.board-overlay-svg .part-area {
  fill: var(--part-label);
  font-family: inherit;
  font-weight: 600;
}

/* The colon of a clock: two dots, not matches (game.js renderGridGuides, board.php) */
.colon-dot {
  fill: var(--fixed-head);
}

/* The cherry of level 1 (a drawing hint): on the board and in the SVG of board.php */
.cherry {
  fill: var(--cherry);
  stroke: var(--cherry-ring);
}

.cherry-shine {
  fill: #ffe4e6;
  fill-opacity: 0.75;
}

.cherry-stem {
  fill: none;
  stroke: var(--cherry-stem);
  stroke-linecap: round;
}

/* A match: birch with a charred band and a red head; the wood follows the theme (--match-wood) */
.match {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  touch-action: none;
  will-change: transform;
  transform-origin: 50% 50%;
  transition:
    transform 0.22s cubic-bezier(0.22, 1, 0.36, 1),
    filter 0.18s ease,
    opacity 0.2s ease;
}

.match:hover {
  filter: brightness(1.06) drop-shadow(0 0 5px rgba(0, 0, 0, 0.25));
}

.match.remove-mode {
  cursor: pointer;
}

.match.remove-mode:hover {
  filter: brightness(1.08) drop-shadow(0 0 8px rgba(200, 40, 30, 0.5));
}

/* Add mode: the matches on the board stay; the empty places take a click */
.match.add-mode,
.match.add-mode:hover {
  cursor: default;
  filter: none;
}

.board-viewport[data-mode="add"] {
  cursor: pointer;
}

.match.dragging {
  cursor: grabbing;
  transition: none !important;
  z-index: 40;
  filter: drop-shadow(0 10px 14px rgba(0, 0, 0, 0.35));
}

.match.locked {
  cursor: not-allowed;
}

.match-body {
  position: relative;
  width: 100%;
  height: var(--match-thickness, 7px);
  border-radius: 1.5px;
  background: var(--match-wood);
  box-shadow:
    var(--match-shadow),
    inset 0 1px 0 rgba(255, 248, 232, 0.42),
    inset 0 -1px 0 rgba(120, 76, 30, 0.48);
  pointer-events: none;
}

/* A fixed match: graphite instead of birch, and it takes no pointer, so it never covers the end of a
   match that can move */
.match.fixed {
  pointer-events: none;
}

.match.fixed .match-body {
  background: var(--fixed-wood);
}

.match.fixed .match-head {
  background: var(--fixed-head);
}

.match-head {
  position: absolute;
  right: -2px;
  top: 50%;
  transform: translateY(-50%);
  width: var(--head-length, 15px);
  height: var(--head-width, 11px);
  border-radius: 60% 45% 45% 60%;
  background: radial-gradient(circle at 72% 42%, #ff6b4a 0%, #e03122 46%, #7c160e 84%, #450a06 100%);
  box-shadow: var(--match-shadow), inset 1px 0 2px rgba(45, 8, 5, 0.78);
}

@keyframes matchCelebrate {
  0% { filter: brightness(1); }
  45% { filter: brightness(1.25); }
  100% { filter: brightness(1); }
}

.match.celebrate {
  animation: matchCelebrate 0.85s ease forwards;
}

/* The place a dragged match will land on */
.match-ghost {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  pointer-events: none;
  z-index: 7;
}

.match-ghost::before {
  content: '';
  position: absolute;
  width: var(--ghost-len, 68px);
  height: var(--ghost-thick, 12px);
  left: calc(var(--ghost-len, 68px) * -0.5);
  top: calc(var(--ghost-thick, 12px) * -0.5);
  border-radius: 999px;
  border: 2px dashed var(--ghost-ok);
  background: color-mix(in srgb, var(--ghost-ok) 15%, transparent);
  transform: rotate(var(--ghost-angle, 0deg));
  transition: transform 0.08s ease-out;
}

.match-ghost.invalid::before {
  border-color: var(--ghost-bad);
  background: color-mix(in srgb, var(--ghost-bad) 15%, transparent);
}

.ghost-badge,
.board-toast,
.victory-banner {
  background: var(--pop-bg);
  color: var(--pop-fg);
  border: 1px solid var(--pop-border);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
}

.ghost-badge {
  position: absolute;
  left: 0;
  top: 24px;
  transform: translateX(-50%);
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.board-toast {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 45;
  max-width: 90%;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 14px;
  text-align: center;
  pointer-events: none;
}

.victory-banner {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  /* Compact (owner's decision, 22.10.2026): the title, the buttons ("Next" first) and the rating, each
     in its own row where they do not fit together; the board stays visible around it */
  width: min(390px, calc(100% - 24px));
  padding: 12px 16px;
  border-radius: var(--radius);
}

/* Three rows (owner's decision, 22.10.2026): the title, "Did you like it? 👍 👎", then "Next" and
   "Play again". The title never wraps; only a banner narrower than the title cuts it with an ellipsis */
.victory-title {
  font-size: 17px;
  line-height: 1.3;
  color: var(--ok);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.victory-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

/* The rating row: muted text and borderless icons, so "Next" stays the main thing. The question, the
   two icon buttons of 44 px for a finger, then "Thanks!" after them; a pressed button takes the accent
   (both themes through the tokens) */
.victory-rate {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  font-size: 14px;
  line-height: 1.35;
  color: var(--muted);
}

/* The question and "Thanks!" in one grid cell, as wide as the longer text (the invisible sizer holds
   the width of "Thanks!" before it is shown): after a vote the text changes, the buttons stay put.
   The banner is anchored at its bottom, so a second line would move them up instead */
.victory-rate-text {
  display: grid;
  margin-right: 4px;
  white-space: nowrap;
}

.victory-rate-text > * {
  grid-area: 1 / 1;
}

.victory-rate-sizer,
.victory-rate.thanked .victory-rate-q {
  visibility: hidden;
}

.rate-btn {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 50%;
  background: none;
  color: var(--muted);
  cursor: pointer;
}

.rate-btn svg {
  width: 22px;
  height: 22px;
}

.rate-btn:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.rate-btn[aria-pressed="true"] {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent);
}

.rate-btn[aria-pressed="true"] svg {
  fill: currentColor;
  fill-opacity: 0.18;
}

/* The target diagram: only pattern puzzles have it, in the corner of the board */
.target-preview-box {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 4px 12px 8px;
  background: var(--page);
  border: 1px solid var(--line);
  border-radius: 6px;
  pointer-events: none;
}

.target-preview-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.target-preview-svg {
  width: 84px;
  height: 64px;
  overflow: visible;
}

.target-preview-svg .tp-line {
  stroke: var(--ink);
}

.target-preview-svg .tp-head {
  fill: var(--match-head);
}

/* =========================================================================
   Hints: opened one by one on request
   ========================================================================= */
.puzzle-hints {
  max-width: 68ch;
  margin-top: 16px;
}

.hint-list {
  list-style: none;
}

.hint {
  margin-bottom: 8px;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  background: var(--accent-soft);
}

.hint-label {
  font-weight: 700;
}

/* =========================================================================
   Phone
   ========================================================================= */
@media (max-width: 1023px) {
  /* First row: moves, the solved badge and Undo / Reset as icons. The live status (squares found,
     the equation; js/game.js marks it .strict) gets a line of its own under them: squeezed into the
     first row it broke into two lines ("Squares: 3 of / 2", 18.10.2026). The general hint "drag a
     match…" stays on the desktop only: here the rule note under the board says the same. */
  .controls {
    flex-wrap: wrap;
    gap: 4px 12px;
    margin-top: 8px;
  }

  .constraint-pill {
    order: 1;
    flex: 0 0 100%;
    min-width: 0;
    font-size: 13px;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .constraint-pill:not(.strict) {
    display: none;
  }

  .badge-solved {
    white-space: nowrap;
  }

  .btn {
    padding: 0 12px;
  }

  .controls .btn {
    justify-content: center;
    width: 40px;
    padding: 0;
  }

  .controls .btn span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  /* The board from edge to edge of the screen */
  .board-wrap {
    margin-top: 8px;
    margin-inline: -16px;
  }

  .board-viewport {
    height: min(58vh, 460px);
    border-radius: 0;
    border-inline: 0;
  }

  /* A phone turned sideways: 58vh would leave ~220px. The board takes the screen minus the top
     bar and the row of the counter and buttons, so scrolled to that row all three fit */
  @media (orientation: landscape) and (max-height: 520px) {
    .board-viewport {
      height: max(240px, calc(100dvh - 112px));
    }
  }

  .target-preview-box {
    top: 8px;
    right: 8px;
    padding: 4px 8px;
  }

  .target-preview-svg {
    width: 60px;
    height: 46px;
  }

  .victory-banner {
    bottom: 12px;
  }
}
