/* A Small Round of Applause: layout and HUD styling. Palette vars (--pal-*) are filled from AJ.sprites.palette at start. */
:root {
  --red: var(--pal-theater-red, #7a1c24);
  --velvet: var(--pal-velvet, #5a1520);
  --brass: var(--pal-brass, #d4ac52);
  --cream: var(--pal-cream, #f6ecd6);
  --ink: var(--pal-ink, #1a1219);
  --night: #140d14;
  --panel: #241820;
  --panel-2: #2f2029;
  --glow: #ffe08a;
  --focus: #7fe0ff;
  --danger: #e04848;
  /* Legend tags use the spectator colours drawn on the grid (main.js copies AJ.sprites.palette into --pal-*);
     fallbacks mirror that palette. Letter colours keep at least 4.5:1 contrast on each swatch. */
  --t-E: var(--pal-enthusiast, #2e9e6b); --t-S: var(--pal-skeptic, #3d6ea5); --t-L: var(--pal-slow, #8a6fb1); --t-C: var(--pal-critic, #3a3a44);
  --t-E-ink: #1a1219; --t-S-ink: #ffffff; --t-L-ink: #000000; --t-C-ink: #ffffff;
  --serif: Georgia, "Times New Roman", serif;
  --sans: system-ui, "Segoe UI", Arial, sans-serif;
  --panel-w: 330px;
}
@media (min-width: 1600px) { :root { --panel-w: 400px; } }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: radial-gradient(ellipse at top, #2a1520 0%, var(--night) 70%);
  color: var(--cream);
  font: 15px/1.4 var(--sans);
  overflow: hidden;
}
@media (min-width: 1600px) and (min-height: 900px) { body { font-size: 17px; } }

.skip { position: absolute; left: -9999px; top: 0; z-index: 10; background: var(--cream); color: var(--ink); padding: 8px 12px; }
.skip:focus { left: 8px; top: 8px; }

#app { height: 100vh; display: grid; grid-template-rows: auto 1fr; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 6px 16px; min-height: 48px;
  background: linear-gradient(var(--red), var(--velvet));
  border-bottom: 3px solid var(--brass);
}
.logo { margin: 0; font: italic 700 1.45rem/1 var(--serif); color: var(--cream); letter-spacing: 0.02em; text-shadow: 0 2px 0 rgba(0,0,0,0.35); }
.logo-small { font-size: 0.75em; font-weight: 400; }
.top-actions { display: flex; gap: 8px; }

/* ---------- buttons & focus ---------- */
button {
  font: inherit; color: var(--cream); background: var(--panel-2);
  border: 1px solid rgba(212,172,82,0.55); border-radius: 8px;
  padding: 6px 12px; cursor: pointer; min-height: 34px;
}
button:hover:not(:disabled) { background: #3d2a35; border-color: var(--brass); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--brass); color: var(--ink); font-weight: 700; border-color: var(--glow); }
button.primary:hover:not(:disabled) { background: var(--glow); }
button[aria-pressed="true"] { background: #5a4220; border-color: var(--glow); box-shadow: inset 0 0 0 1px var(--glow); }
:focus { outline: none; }
:focus-visible, button:focus-visible, summary:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
canvas#game:focus-visible, canvas#game:focus { outline: 3px solid var(--focus); outline-offset: -3px; }

main { min-height: 0; overflow: hidden; }
.screen { height: 100%; }

/* ---------- menu ---------- */
.menu { overflow-y: auto; padding: 18px 28px 24px; max-width: 1500px; margin: 0 auto; }
.menu-head h2 { margin: 0 0 4px; font: 700 1.6rem var(--serif); color: var(--glow); }
.tagline { margin: 0 0 4px; max-width: 70ch; opacity: 0.9; }
.menu-progress { margin: 0 0 12px; color: var(--brass); font-weight: 600; }
.puzzle-grid {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px;
}
.card {
  width: 100%; height: 100%; text-align: left; display: flex; flex-direction: column; gap: 3px;
  padding: 10px; background: var(--panel); border: 2px solid rgba(212,172,82,0.35); border-radius: 12px; position: relative;
}
.card:hover:not(:disabled) { border-color: var(--brass); background: #2d1e27; }
.card.solved { border-color: rgba(127,200,120,0.7); }
.card-sandbox { background: linear-gradient(135deg, #3a1d28, #241820); border-style: dashed; }
.mini { height: 120px; width: auto; max-width: 100%; align-self: center; border-radius: 6px; margin-bottom: 4px; display: block; }
@media (max-height: 760px) { .mini { height: 90px; } }
.card-num { font-size: 0.78em; text-transform: uppercase; letter-spacing: 0.08em; color: var(--brass); }
.card-title { font: 700 1.12em var(--serif); color: var(--cream); }
.card-blurb { font-size: 0.9em; opacity: 0.85; }
.card-meta { font-size: 0.8em; opacity: 0.7; margin-top: auto; }
.card-badge { position: absolute; top: 14px; right: 14px; background: #2f6b34; color: #fff; font-size: 0.75em; font-weight: 700; padding: 2px 8px; border-radius: 99px; }
.card-error { padding: 20px; background: var(--panel); border-radius: 10px; }
.legend-menu { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin-top: 16px; }

/* ---------- legend tags (shared) ---------- */
.legend-item { display: flex; gap: 8px; align-items: flex-start; font-size: 0.88em; }
.tag {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 1.55em; height: 1.55em; border-radius: 5px; font-weight: 800; color: var(--ink); font-size: 0.95em;
  border: 2px solid rgba(246,236,214,0.55); /* light edge: the charcoal critic swatch must not vanish on the dark panel */
}
.tag-E { background: var(--t-E); color: var(--t-E-ink); }
.tag-S { background: var(--t-S); color: var(--t-S-ink); }
.tag-L { background: var(--t-L); color: var(--t-L-ink); }
.tag-C { background: var(--t-C); color: var(--t-C-ink); }
.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; }
.rules { margin: 0; padding-left: 1.2em; font-size: 0.82em; opacity: 0.9; }
.rules li { margin: 1px 0; }
.rules-menu { margin-top: 10px; font-size: 0.88em; }
.tag-aisle { background: #3a2a22; color: var(--cream); }

/* ---------- play view ---------- */
.play { display: grid; grid-template-columns: minmax(0, 1fr) var(--panel-w); grid-template-rows: minmax(0, 1fr); min-height: 0; position: relative; }
.stage-wrap { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#game { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: manipulation; cursor: pointer; }

.panel {
  background: var(--panel); border-left: 3px solid var(--brass);
  padding: 12px 14px; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; gap: 9px;
  min-height: 0; overflow-wrap: break-word;
}
/* D1: flex items shrink by default, and an explicit min-height (.status, .cell-info) replaces the automatic
   "never smaller than my content" minimum. On short windows those boxes were squeezed to their min-height and
   their text overprinted the legend. Children keep their natural height; the panel scrolls instead. */
.panel > * { flex: none; }
.puzzle-head h2 { margin: 0; font: 700 1.3rem/1.15 var(--serif); color: var(--glow); }
.puzzle-num { margin: 0; font-size: 0.78em; letter-spacing: 0.08em; text-transform: uppercase; color: var(--brass); }
.puzzle-blurb { margin: 3px 0 0; font-style: italic; opacity: 0.9; }
.goal { background: rgba(255,224,138,0.1); border: 1px solid rgba(255,224,138,0.4); border-radius: 8px; padding: 6px 9px; }
.goal-label { font-weight: 800; color: var(--glow); text-transform: uppercase; font-size: 0.8em; letter-spacing: 0.06em; margin-right: 4px; }
.counters { display: flex; flex-direction: column; gap: 1px; font-size: 0.92em; }
#swaps-left { font-weight: 700; color: var(--brass); }
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.status {
  margin: 0; min-height: 3.1em; padding: 6px 9px; border-radius: 8px;
  background: rgba(0,0,0,0.28); border-left: 3px solid var(--brass); font-size: 0.92em;
}
.cell-info { margin: 0; min-height: 2.6em; font-size: 0.85em; opacity: 0.85; }
.legend-panel { display: grid; gap: 4px; }
.keys { font-size: 0.82em; opacity: 0.85; }
.keys summary { cursor: pointer; color: var(--brass); }
.keys p { margin: 4px 0 0; }

.sandbox-tools { display: flex; flex-direction: column; gap: 6px; padding: 8px; border: 1px dashed rgba(212,172,82,0.5); border-radius: 8px; }
.brushes { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; }
.brushes button { padding: 4px; display: inline-flex; align-items: center; justify-content: center; gap: 4px; font-size: 0.85em; }
.sizes { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.size-label { grid-column: 1 / -1; font-size: 0.85em; opacity: 0.85; }
.sizes button { padding: 3px 6px; font-size: 0.85em; min-height: 28px; }

/* ---------- overlay ---------- */
.overlay {
  position: absolute; top: 0; right: 0; bottom: 0; width: var(--panel-w); z-index: 5;
  background: rgba(20,13,20,0.82); padding: 12px; overflow-y: auto;
}
.overlay-card {
  background: rgba(26,18,25,0.98); border: 3px solid var(--brass); border-radius: 14px;
  padding: 14px 16px; box-shadow: 0 10px 40px rgba(0,0,0,0.6);
  animation: rise 0.35s ease-out;
}
.overlay-keys { margin: 8px 0 0; font-size: 0.8em; opacity: 0.7; }
.overlay.won .overlay-card { border-color: var(--glow); }
.overlay.failed .overlay-card { border-color: var(--danger); }
.overlay h2 { margin: 0 0 4px; font: italic 700 1.8rem var(--serif); color: var(--glow); }
.overlay.failed h2 { color: #ff9a9a; }
.overlay-summary { margin: 0 0 4px; font-size: 1.08em; font-weight: 600; }
.overlay-stats { margin: 0 0 4px; font-size: 0.88em; opacity: 0.85; }
.overlay-flavor { margin: 0 0 10px; font-style: italic; color: var(--brass); }
.overlay-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.overlay-buttons .primary { grid-column: 1 / -1; }
@keyframes rise { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ---------- smaller heights (1280x720 and below) ---------- */
@media (max-height: 760px) {
  body { font-size: 14px; }
  .panel { gap: 7px; padding: 10px 12px; }
  button { min-height: 30px; padding: 4px 10px; }
  .menu { padding: 12px 22px 16px; }
  .puzzle-grid { gap: 10px; }
}
@media (max-width: 1100px) {
  .puzzle-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .legend-menu { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  :root { --panel-w: 290px; }
}

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