/* Chain Reaction - paper-craft styling.
 *
 * One stylesheet, no framework, no web fonts. Every color pair below clears
 * WCAG 2.1 AA at 4.5:1 against the surface it sits on; the ink/cream pair the
 * page leans on is roughly 11:1.
 */

:root {
  --paper: #f4ead8;
  --paper-2: #efe1c9;
  --card: #fbf5e8;
  --ink: #3a322b;
  --ink-soft: #5c5045;      /* 7.0:1 on --card */
  --rule: #d3c2a2;
  --accent: #a8442a;        /* 6.3:1 on --card */
  --accent-ink: #fbf5e8;
  --good: #2f6a4c;          /* 5.6:1 on --card */
  --focus: #1c5a8a;
  --shadow: rgba(58, 50, 43, 0.18);
}

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

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

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 17px/1.65 "Iowan Old Style", "Palatino Linotype", Palatino, Georgia,
        "Times New Roman", serif;
}

h1, h2, h3 {
  font-family: "Trebuchet MS", "Segoe UI", Candara, Optima, system-ui, sans-serif;
  line-height: 1.2;
  letter-spacing: -0.01em;
  margin: 0 0 0.5em;
}

h1 { font-size: clamp(1.9rem, 4.2vw, 2.9rem); }
h2 { font-size: clamp(1.4rem, 2.6vw, 1.9rem); }
h3 { font-size: 1.15rem; margin-top: 1.6em; }

p { margin: 0 0 1em; }

a { color: #8a3a22; text-decoration-thickness: 2px; text-underline-offset: 3px; }
a:hover { color: var(--ink); }

/* A focus ring you can actually see, on every focusable thing. */
:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
  border-radius: 4px;
}
.no-focus-visible :focus { outline: 3px solid var(--focus); outline-offset: 3px; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--ink);
  color: var(--paper);
  padding: 10px 18px;
  z-index: 50;
  border-radius: 0 0 8px 0;
}
.skip-link:focus { left: 0; }

.wrap {
  width: min(1120px, 100% - 2.5rem);
  margin-inline: auto;
}

/* ------------------------------------------------------------------ head */

.site-head {
  border-bottom: 3px solid var(--ink);
  background: var(--paper-2);
}
.head-row {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.7rem 0;
  min-height: 58px;
}
.brand {
  width: 26px;
  height: 26px;
  flex: none;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 4px 5px 0 var(--shadow);
}
.head-title {
  font-family: "Trebuchet MS", "Segoe UI", Candara, system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.15rem;
}
.head-nav {
  margin-left: auto;
  display: flex;
  gap: 1.1rem;
  font-size: 0.95rem;
}

/* ----------------------------------------------------------------- intro */

.intro-block { padding: 2.2rem 0 1rem; }
.intro { font-size: 1.08rem; max-width: 64ch; }

/* ------------------------------------------------------------ play block */

.play-block {
  padding: 1rem 0 2.4rem;
  /* Reserved up front so nothing below the game jumps when a screen swaps. */
  min-height: 620px;
}

.panel {
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: 14px;
  box-shadow: 8px 10px 0 var(--shadow);
  padding: 1.5rem;
}

.start-lede { max-width: 62ch; color: var(--ink-soft); }

/* ----------------------------------------------------------------- tiers */

.tier-picker {
  border: 2px dashed var(--rule);
  border-radius: 12px;
  margin: 1.2rem 0;
  padding: 0.9rem 1rem 1.1rem;
  display: grid;
  gap: 0.55rem;
}
.tier-picker legend {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  padding: 0 0.5rem;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.tier {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  padding: 0.6rem 0.7rem;
  border: 2px solid transparent;
  border-radius: 10px;
  cursor: pointer;
}
.tier:hover { background: var(--paper-2); }
.tier input {
  margin: 0.35rem 0 0;
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex: none;
}
.tier:has(input:checked) {
  border-color: var(--ink);
  background: var(--paper-2);
}
.tier:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.tier-body { display: block; }
.tier-name {
  display: block;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
}
.tier-name em { font-style: normal; font-weight: 400; color: var(--ink-soft); }
.tier-note {
  display: block;
  font-size: 0.94rem;
  color: var(--ink-soft);
  max-width: 62ch;
}
.tier-picker-compact { margin: 0.6rem 0; }
.tier-picker-compact .tier { padding: 0.4rem 0.5rem; }
.tier-picker-compact .tier-note { font-size: 0.86rem; }

.tier-list li { margin-bottom: 0.35rem; }

/* --------------------------------------------------------------- buttons */

.btn {
  font: inherit;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  font-size: 0.98rem;
  padding: 0.6rem 1.25rem;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--paper-2);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 4px 5px 0 var(--shadow);
}
.btn:hover:not(:disabled) { background: #e6d5b6; }
.btn:active:not(:disabled) { transform: translate(2px, 2px); box-shadow: 2px 3px 0 var(--shadow); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; box-shadow: none; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: #93361f; }
.btn.on { background: var(--good); color: var(--accent-ink); }

.start-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
  margin-top: 1rem;
}
.start-foot { margin: 1rem 0 0; color: var(--ink-soft); font-size: 0.95rem; }

/* ---------------------------------------------------------- level select */

.level-head { display: flex; align-items: baseline; gap: 1rem; }
.tier-badge {
  margin: 0;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 0.15rem 0.75rem;
  background: var(--paper-2);
}
.chapter-head { margin: 1.5rem 0 0.6rem; font-size: 1.05rem; }
.chapter-sub { font-weight: 400; color: var(--ink-soft); font-size: 0.9rem; }
.chapter-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 0.6rem;
}
.level-cell {
  font: inherit;
  text-align: left;
  display: grid;
  gap: 0.1rem;
  padding: 0.55rem 0.7rem;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 3px 4px 0 var(--shadow);
}
.level-cell:hover { background: var(--paper-2); }
.level-cell.solved { background: #e3eddf; }
.level-cell.starred { border-color: var(--good); border-width: 3px; }
.cell-n {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--ink-soft);
}
.cell-t { font-size: 0.98rem; line-height: 1.25; }
.cell-s { font-size: 0.82rem; color: var(--ink-soft); }
.level-cell.starred .cell-s { color: var(--good); font-weight: 700; }

/* ------------------------------------------------------------------ game */

.game-screen { display: grid; gap: 0.75rem; }

.hud {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 1rem;
  align-items: center;
  border: 3px solid var(--ink);
  border-radius: 12px 12px 0 0;
  background: var(--paper-2);
  padding: 0.55rem 0.9rem;
}
.hud-left { display: grid; }
.hud-level {
  font-size: 0.8rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-soft);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
}
.hud-title {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.1rem;
}
.hud-right { margin-left: auto; display: flex; flex-wrap: wrap; gap: 0.45rem; }
.chip {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-size: 0.82rem;
  font-weight: 700;
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 0.15rem 0.7rem;
  background: var(--card);
  white-space: nowrap;
}
.chip-timer.urgent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.stage-wrap {
  border: 3px solid var(--ink);
  border-top: 0;
  background: var(--paper);
  padding: 0;
  line-height: 0;
}
#stage {
  display: block;
  width: 100%;
  /* The intrinsic 1200x675 attributes plus this ratio reserve the box before
     the script runs, so the first paint is also the final layout. */
  aspect-ratio: 16 / 9;
  height: auto;
  touch-action: none;
  cursor: crosshair;
}
#stage:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; }
.stage-hint {
  margin: 0;
  padding: 0.5rem 0.9rem;
  line-height: 1.5;
  font-size: 0.95rem;
  color: var(--ink-soft);
  border-top: 2px dashed var(--rule);
  min-height: 2.5rem;
}

.tray-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: flex-start;
  border: 3px solid var(--ink);
  border-top: 0;
  border-radius: 0 0 12px 12px;
  background: var(--paper-2);
  padding: 0.7rem 0.9rem;
}
.tray {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  min-height: 62px;
}
.tray-item {
  font: inherit;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.4rem 0.7rem;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  cursor: grab;
  box-shadow: 3px 4px 0 var(--shadow);
}
.tray-item:hover:not(:disabled) { background: #fff9ee; }
.tray-item.holding { background: var(--good); color: var(--accent-ink); }
.tray-item:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }
.tray-glyph {
  width: 26px;
  height: 26px;
  flex: none;
  border: 3px solid var(--ink);
  background: #c79a62;
}
.tray-glyph[data-shape="plank"] { height: 12px; background: #d8ad74; border-radius: 3px; }
.tray-glyph[data-shape="ramp"] { height: 12px; background: #c79a62; border-radius: 3px; transform: rotate(-16deg); }
.tray-glyph[data-shape="ball"] { border-radius: 50%; background: #c65c3a; }
.tray-glyph[data-shape="domino"] { width: 12px; background: #c79a62; border-radius: 2px; }
.tray-glyph[data-shape="balloon"] { border-radius: 50% 50% 45% 45%; background: #9dc0d8; }
.tray-glyph[data-shape="bomb"] { border-radius: 50%; background: #4a4038; }
.tray-name {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  font-size: 0.92rem;
}
.tray-count {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
  font-size: 0.8rem;
  min-width: 22px;
  text-align: center;
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 0 0.3rem;
  background: var(--paper-2);
}
.tray-note {
  font-size: 0.85rem;
  color: var(--ink-soft);
  align-self: center;
  padding-left: 0.3rem;
}

.controls {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.controls .btn { padding: 0.5rem 0.95rem; font-size: 0.92rem; }

.status-line {
  margin: 0;
  min-height: 3.2rem;
  padding: 0.6rem 0.9rem;
  border: 2px dashed var(--rule);
  border-radius: 10px;
  background: var(--card);
  font-size: 0.98rem;
}

/* ------------------------------------------------------------------ menu */

.menu-screen {
  position: fixed;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  width: min(560px, calc(100% - 2rem));
  max-height: calc(100vh - 2rem);
  overflow: auto;
  z-index: 40;
}
.menu-toggles { display: flex; flex-wrap: wrap; gap: 1.2rem; margin-top: 0.6rem; }
.toggle { display: flex; align-items: center; gap: 0.45rem; cursor: pointer; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }

.noscript { margin-top: 1rem; }

/* ----------------------------------------------------------------- prose */

.prose { padding: 1.6rem 0; border-top: 2px solid var(--rule); }
.prose ol, .prose ul { max-width: 72ch; padding-left: 1.3rem; }
.prose li { margin-bottom: 0.45rem; }
.prose p { max-width: 70ch; }

/* ------------------------------------------------------ privacy / terms */

.legal { padding: 2rem 0 2.5rem; }
.legal h2 { margin-top: 1.8em; }
.legal p, .legal li { max-width: 72ch; }
.legal .note { color: var(--ink-soft); font-size: 0.92rem; }
.legal code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 4px;
  padding: 0.05em 0.35em;
}
.legal table {
  border-collapse: collapse;
  width: 100%;
  margin: 1rem 0 1.5rem;
  background: var(--card);
  border: 3px solid var(--ink);
  border-radius: 10px;
  overflow: hidden;
  font-size: 0.94rem;
}
.legal th, .legal td {
  text-align: left;
  vertical-align: top;
  padding: 0.55rem 0.7rem;
  border-bottom: 1px solid var(--rule);
}
.legal th {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  background: var(--paper-2);
}
.legal tbody tr:last-child td { border-bottom: 0; }

.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;
}

/* ------------------------------------------------------------------ foot */

.site-foot {
  border-top: 3px solid var(--ink);
  background: var(--paper-2);
  padding: 1.4rem 0 2.2rem;
  margin-top: 1.5rem;
  font-size: 0.95rem;
}
.site-foot p { max-width: 72ch; }
.foot-fine { color: var(--ink-soft); font-size: 0.88rem; margin-bottom: 0; }

/* -------------------------------------------------------------- narrower */

@media (max-width: 720px) {
  body { font-size: 16px; }
  .head-nav { display: none; }
  .play-block { min-height: 520px; }
  .panel { padding: 1.1rem; }
  .controls { margin-left: 0; width: 100%; }
  .hud-right { margin-left: 0; width: 100%; }
}

/* --------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .btn:active:not(:disabled) { transform: none; }
}

@media (prefers-contrast: more) {
  :root { --ink-soft: #3a322b; --rule: #8d7c5e; }
}
