/* theme.css: the designer's stylesheet (agents/prism.md), loaded after the template's own, so its
   rules win. Fresco writes it at the design stage; the builder keeps it linked and does not edit it.

   Direction: "brass night-lamp panel on a paper desk". The page is warm paper (or warm night in
   dark mode); the board is always a night panel, because lamps only read against the dark.
   Signature: the shape stamp, a 5x5 mini grid beside "Shape: plus" that draws the flip set. */

:root {
  /* Palette: page chrome (light) */
  --bg: #f6f1e7;
  --surface: #fffaf0;
  --raised: #ffffff;
  --text: #1d1a16;          /* 15.9:1 on --bg */
  --muted: #5c554b;         /* 6.5:1 on --bg */
  --border: #d9cfbd;
  --border-strong: #b9ad97;
  --accent: #9a4708;        /* 5.7:1 on --bg, text and links */
  --accent-hover: #7c3906;
  --accent-active: #66300a;
  --on-accent: #ffffff;     /* 6.4:1 on --accent */
  --success: #166534;       /* 6.3:1 on --bg */
  --warning: #92400e;       /* 6.3:1 on --bg */
  --danger: #b91c1c;        /* 5.8:1 on --bg */
  --focus: #1d1a16;

  /* Palette: the board (night panel in both modes; game.js reads these with getComputedStyle) */
  --field: #14120f;
  --field-edge: #2c2720;
  --lamp-on: #ffc95c;       /* lit: filled disc */
  --lamp-on-ring: rgba(255, 201, 92, 0.4); /* crisp 3px ring, 4px outside the disc, no blur */
  --lamp-on-core: #fff1cf;  /* small highlight dot, upper left of the disc */
  --lamp-off: #6b6253;      /* dark: 2px hollow ring, 3.1:1 on --field */
  --preview: #f3ede2;       /* flip-set outline: 2px solid ring, 6px outside the lamp */
  --hint: #5fd4bd;          /* hint: 2px dashed ring plus four ticks */
  --focus-lamp: #ffffff;    /* keyboard focus: four corner brackets around the cell */
  --field-text: #b8ad9c;    /* lit counter on the canvas, 8.5:1 on --field */

  /* Type */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --fs-xs: 0.75rem;   /* 12: small caps labels */
  --fs-sm: 0.875rem;  /* 14: stat labels, notes */
  --fs-md: 1rem;      /* 16: body, buttons */
  --fs-lg: 1.25rem;   /* 20: shape name, panel line */
  --fs-xl: 1.75rem;   /* 28: heading */
  --fs-2xl: 2.25rem;  /* 36: "All dark" */

  /* Space */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
  --s-7: 48px;
  --s-8: 64px;

  /* Shape */
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --shadow-1: 0 1px 2px rgba(29, 26, 22, 0.08), 0 2px 6px rgba(29, 26, 22, 0.06);
  --shadow-2: 0 8px 24px rgba(29, 26, 22, 0.16), 0 2px 6px rgba(29, 26, 22, 0.08);

  --t-fast: 160ms;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12100d;
    --surface: #1b1814;
    --raised: #24201a;
    --text: #f3ede2;        /* 16.6:1 on --bg */
    --muted: #b8ad9c;       /* 8.5:1 on --bg */
    --border: #3a342b;
    --border-strong: #574e41;
    --accent: #f5b342;      /* 10:1 on --bg */
    --accent-hover: #ffc560;
    --accent-active: #e09a24;
    --on-accent: #1d1a16;   /* 9.4:1 on --accent */
    --success: #6ee7a0;
    --warning: #fbbf24;
    --danger: #f87171;      /* 6.8:1 on --bg */
    --focus: #f3ede2;
    --field: #0b0a08;
    --field-edge: #3a342b;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-2: 0 10px 28px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.35);
  }
}

/* ---------- Page ---------- */

html {
  background: var(--bg);
}

body {
  min-height: 100%;
  margin: 0;
  padding: var(--s-4);
  box-sizing: border-box;
  display: block;
  background: var(--bg);
  color: var(--text);
  font: 400 var(--fs-md) / 1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

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

.app {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: var(--s-3);
}

/* Row 1: heading and level tag */
.masthead {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s-3);
}

h1,
.masthead h1 {
  margin: 0;
  font-size: var(--fs-xl);
  font-weight: 750;
  letter-spacing: -0.02em;
  line-height: 1.15;
  color: var(--text);
}

.level-tag {
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
}

/* Row 2: the shape card, home of the signature stamp */
.shape-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: var(--s-4);
  padding: var(--s-3) var(--s-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-1);
}

.shape-text {
  min-width: 0;
}

.shape-name {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 700;
  line-height: 1.25;
  color: var(--text);
}

.shape-note {
  margin: 2px 0 0;
  font-size: var(--fs-sm);
  color: var(--muted);
  line-height: 1.4;
}

/* The stamp: 25 <i> cells in a 5x5 grid. .on = flipped by a press, .origin = the pressed lamp. */
.stamp {
  display: grid;
  grid-template-columns: repeat(5, 10px);
  grid-template-rows: repeat(5, 10px);
  gap: 3px;
  padding: 6px;
  background: var(--field);
  border-radius: var(--r-sm);
  flex: none;
}

.stamp i {
  display: block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1.5px solid var(--lamp-off);
}

.stamp i.on {
  background: var(--lamp-on);
  border-color: var(--lamp-on);
}

.stamp i.origin {
  background: var(--lamp-on);
  border-color: var(--lamp-on-core);
  box-shadow: 0 0 0 1.5px var(--field), 0 0 0 3px var(--lamp-on);
}

.stamp.is-new {
  animation: stamp-pop 180ms var(--ease);
}

@keyframes stamp-pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

/* HUD: Moves and Par, stacked to the right of the shape text */
.hud,
.shape-card .hud {
  display: flex;
  gap: var(--s-4);
  align-items: center;
  font-variant-numeric: tabular-nums;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.1;
  min-width: 3ch;
}

.stat-label {
  font-size: var(--fs-xs);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  font-size: var(--fs-lg);
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* Status line: one polite line, height reserved */
.status {
  min-height: 1.5em;
  margin: 0;
  font-size: var(--fs-sm);
  font-weight: 500;
  line-height: 1.5;
  color: var(--muted);
  text-align: center;
  overflow-wrap: anywhere;
}

.status.is-error {
  color: var(--danger);
  font-weight: 600;
}

.status.is-good {
  color: var(--success);
  font-weight: 600;
}

/* ---------- Stage and board ---------- */

.stage {
  position: relative;
  align-self: center;
  width: fit-content;
}

.stage canvas {
  display: block;
  width: auto;
  height: min(560px, max(420px, calc(100svh - 300px)));
  aspect-ratio: 360 / 560;
  max-width: calc(100vw - 2 * var(--s-4));
  background: var(--field);
  border: 1px solid var(--field-edge);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-2);
  touch-action: none;
  cursor: pointer;
}

.stage canvas:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.stage canvas.is-done {
  cursor: default;
}

/* All dark panel: overlays the lower band of the canvas, never the board itself */
.done-panel {
  position: absolute;
  left: var(--s-3);
  right: var(--s-3);
  bottom: var(--s-3);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s-4);
  padding: var(--s-6) var(--s-5) var(--s-5);
  background: var(--surface);
  color: var(--text);
  border-top: 4px solid var(--accent);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-1);
  text-align: center;
  animation: panel-in 180ms var(--ease);
}

.done-title {
  margin: 0;
  font-size: var(--fs-2xl);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.done-text {
  margin: 0;
  font-size: var(--fs-md);
  color: var(--muted);
  max-width: 34ch;
  font-variant-numeric: tabular-nums;
}

.done-text strong {
  color: var(--text);
}

@keyframes panel-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Buttons ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--s-2);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  min-height: 44px;
  min-width: 44px;
  padding: 10px var(--s-4);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--raised);
  color: var(--text);
  font: 600 var(--fs-md) / 1.2 var(--font);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform var(--t-fast) var(--ease), opacity var(--t-fast) var(--ease);
}

.btn svg {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

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

.btn:active {
  transform: translateY(1px) scale(0.98);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.btn:disabled:hover {
  border-color: var(--border-strong);
  color: var(--text);
}

/* The one filled button: Next / Replay on the All dark panel */
.btn-primary {
  min-width: 160px;
  min-height: 48px;
  padding: 12px var(--s-6);
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700;
  font-size: var(--fs-lg);
}

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

.btn-primary:active {
  background: var(--accent-active);
  border-color: var(--accent-active);
}

/* Hint keeps its width as "1 left" becomes "0 left" */
.btn-hint {
  min-width: 128px;
  align-items: baseline;
}

.done-panel .btn-primary {
  margin-top: var(--s-2);
}

.btn-hint .count {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  font-size: var(--fs-sm);
  font-weight: 600;
  line-height: inherit;
  align-self: baseline;
  color: var(--muted);
}

.btn-hint:hover .count {
  color: inherit;
}

/* Quiet: the rewarded Extra hint offer */
.btn-quiet {
  border-color: transparent;
  background: transparent;
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.btn-quiet:hover {
  border-color: transparent;
  background: var(--surface);
  color: var(--accent-hover);
}

button:focus-visible,
a:focus-visible,
.btn:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

a {
  color: var(--accent);
  text-underline-offset: 3px;
}

a:hover {
  color: var(--accent-hover);
}

kbd {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  padding: 1px 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--raised);
  color: var(--text);
}

.keys span.k {
  display: inline-block;
  white-space: nowrap;
}

.keys {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--muted);
  text-align: center;
}

/* No-JS and load failure notice */
.notice {
  margin: 0;
  padding: var(--s-3) var(--s-4);
  border: 1px solid var(--danger);
  border-left-width: 4px;
  border-radius: var(--r-sm);
  background: var(--surface);
  color: var(--text);
  font-size: var(--fs-md);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

[hidden] {
  display: none !important;
}

/* ---------- Layout ---------- */

/* Desktop: the shape card and controls sit left of the board, so the twist and the board are
   seen together at first glance. */
@media (min-width: 760px) {
  body {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--s-6);
  }

  .app {
    max-width: 760px;
    display: grid;
    grid-template-columns: 300px auto;
    grid-template-areas:
      "head  stage"
      "shape stage"
      "status stage"
      "ctrl  stage"
      "keys  stage"
      ".     stage";
    grid-template-rows: auto auto auto auto auto 1fr;
    column-gap: var(--s-6);
    row-gap: var(--s-4);
    align-items: start;
  }

  .masthead { grid-area: head; }
  .shape-card { grid-area: shape; grid-template-columns: auto 1fr; }
  .shape-card .hud { grid-column: 1 / -1; justify-content: flex-start; gap: var(--s-6); }
  .shape-card .stat { align-items: flex-start; }
  .status { grid-area: status; text-align: left; }
  .controls { grid-area: ctrl; justify-content: flex-start; }
  .keys { grid-area: keys; text-align: left; }
  .stage { grid-area: stage; }

  .stamp {
    grid-template-columns: repeat(5, 14px);
    grid-template-rows: repeat(5, 14px);
    gap: 4px;
    padding: 8px;
  }

  .stamp i {
    width: 14px;
    height: 14px;
  }
}

/* Phones: the keyboard line is not useful; the board fits under the shape card. */
@media (max-width: 759px) {
  .keys {
    display: none;
  }

  .stage canvas {
    height: clamp(340px, calc(100svh - 480px), 560px);
  }

  .controls {
    display: flex;
    flex-wrap: nowrap;
    gap: var(--s-2);
    justify-content: center;
  }

  .controls .btn {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: var(--s-3);
  }

  .controls .btn-hint {
    flex: 1.3 1 0;
    padding-inline: var(--s-2);
  }

  .btn-hint .count {
    font-size: var(--fs-xs, 0.75rem);
  }
}

/* Short landscape phones: the portrait board keeps its shape and fills the height. */
@media (orientation: landscape) and (max-height: 500px) {
  .app {
    max-width: 760px;
    display: grid;
    grid-template-columns: minmax(240px, 1fr) auto;
    column-gap: var(--s-5);
    row-gap: var(--s-2);
  }

  .stage {
    grid-column: 2;
    grid-row: 1 / span 5;
  }

  .stage canvas {
    height: calc(100svh - 2 * var(--s-4));
  }
}

/* ---------- Motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }

  .btn:active {
    transform: none;
  }
}
