/* Layout: the glass tower fills the left; a narrow control rail sits right and drops below on phones. */
:root {
  --bg: #e6ecee;
  --panel: #f5f8f9;
  --fg: #14242b;
  --muted: #56696f;
  --rule: #c3d0d4;
  --glass: rgba(60, 110, 128, .11);
  --glass-edge: rgba(30, 70, 85, .55);
  --well: rgba(30, 70, 85, .22);
  --frame: #33505a;
  --p1: #f0642c;
  --p2: #175e6c;
  --focus: #0a66c2;
  --display: "Bricolage Grotesque", "Avenir Next", "Segoe UI", sans-serif;
  --body: "Instrument Sans", "Helvetica Neue", Arial, sans-serif;
  --S: 256px;
  --gap: 105px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1a1f; --panel: #17262c; --fg: #e6eef0; --muted: #93a7ad; --rule: #2b3f46;
    --glass: rgba(150, 205, 220, .09); --glass-edge: rgba(170, 215, 228, .5); --well: rgba(170, 215, 228, .2);
    --frame: #7f9aa3; --p1: #ff7a3d; --p2: #4fc3c8; --focus: #7ab8ff; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1a1f; --panel: #17262c; --fg: #e6eef0; --muted: #93a7ad; --rule: #2b3f46;
  --glass: rgba(150, 205, 220, .09); --glass-edge: rgba(170, 215, 228, .5); --well: rgba(170, 215, 228, .2);
  --frame: #7f9aa3; --p1: #ff7a3d; --p2: #4fc3c8; --focus: #7ab8ff; color-scheme: dark;
}

* { box-sizing: border-box; }
body {
  background: var(--bg); color: var(--fg);
  font-family: var(--body); font-size: 15px; line-height: 1.5;
  padding-block: 20px 32px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.app { max-width: 1080px; margin: 0 auto; display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 24px; align-items: start; }
@media (max-width: 820px) { .app { grid-template-columns: minmax(0, 1fr); } }

header.top { grid-column: 1 / -1; display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 18px; }
h1 { font-family: var(--display); font-weight: 700; font-size: 34px; letter-spacing: -.02em; margin: 0; line-height: 1; }
.tag { color: var(--muted); margin: 0; }

/* ---------- 3D scene ---------- */
.board-col { min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.scene {
  position: relative; height: 620px; perspective: 1500px; overflow: hidden;
  border: 1px solid var(--rule); border-radius: 6px; background:
    radial-gradient(ellipse at 50% 60%, var(--panel), transparent 70%);
  touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none;
}
.scene.flat { touch-action: auto; cursor: default; }
.scene.dragging { cursor: grabbing; }
.fit { position: absolute; inset: 0; transform: translate(var(--px, 0px), var(--py, 0px)) scale(calc(var(--scale, 1) * var(--zoom, 1))); transform-style: preserve-3d; }
.tower {
  position: absolute; left: 50%; top: 50%; width: var(--S); height: var(--S);
  margin: calc(var(--S) / -2) 0 0 calc(var(--S) / -2);
  transform-style: preserve-3d;
  transform: rotateX(var(--tilt, 62deg)) rotateZ(var(--spin, 35deg));
}
.tower.anim { transition: transform .55s ease; }
.flat .tower { transform: none; }

.shelf {
  position: absolute; inset: 0; display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr);
  background: var(--glass); border: 1.5px solid var(--glass-edge); border-radius: 4px;
  transform-style: preserve-3d; pointer-events: none;
  transform: translateZ(calc((var(--i) - 1.5) * var(--gap)));
  transition: transform .55s ease, opacity .25s ease;
}
.flat .shelf { transform: translate(calc(var(--fx) * 142px), calc(var(--fy) * 146px)); }
.shelf-label {
  position: absolute; left: 0; top: -21px; font-family: var(--display); font-weight: 500; font-size: 13px;
  letter-spacing: .08em; text-transform: uppercase; color: var(--muted);
}
.cell { display: grid; place-items: center; border: .5px solid var(--well); transform-style: preserve-3d; }
.well {
  position: relative; width: 46px; height: 46px; border-radius: 50%;
  border: 1.5px solid var(--well); pointer-events: auto; cursor: pointer; transform-style: preserve-3d;
  outline: none;
}
.well:focus-visible { box-shadow: 0 0 0 3px var(--focus); }
.well.full, .over .well { cursor: default; }
.tower[data-turn="1"] { --turn: var(--p1); }
.tower[data-turn="2"] { --turn: var(--p2); }
.tower:not(.over):not(.wait) .well:not(.full):hover { border-color: var(--turn); background: color-mix(in srgb, var(--turn) 35%, transparent); }

[hidden] { display: none !important; }
/* where levels cannot overlap (laid flat, or one level isolated) the whole square takes the tap */
.flat .cell, .tower[data-focus="0"] .shelf[data-l="0"] .cell, .tower[data-focus="1"] .shelf[data-l="1"] .cell,
.tower[data-focus="2"] .shelf[data-l="2"] .cell, .tower[data-focus="3"] .shelf[data-l="3"] .cell { pointer-events: auto; cursor: pointer; }
.well.hint { border-color: var(--turn); background: color-mix(in srgb, var(--turn) 55%, transparent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--turn) 45%, transparent); }

/* focus one level: the rest fade and stop taking clicks */
.tower[data-focus] .shelf { opacity: .16; }
.tower[data-focus] .shelf .well { pointer-events: none; }
.tower[data-focus="0"] .shelf[data-l="0"], .tower[data-focus="1"] .shelf[data-l="1"],
.tower[data-focus="2"] .shelf[data-l="2"], .tower[data-focus="3"] .shelf[data-l="3"] { opacity: 1; }
.tower[data-focus="0"] .shelf[data-l="0"] .well, .tower[data-focus="1"] .shelf[data-l="1"] .well,
.tower[data-focus="2"] .shelf[data-l="2"] .well, .tower[data-focus="3"] .shelf[data-l="3"] .well { pointer-events: auto; }

/* counters: a stack of discs gives the edge its thickness */
.ctr { position: absolute; inset: 2px; transform-style: preserve-3d; transform: translateZ(1px); pointer-events: none; animation: drop .32s ease-out; }
.ctr[data-p="1"] { --c: var(--p1); }
.ctr[data-p="2"] { --c: var(--p2); }
.ctr i { position: absolute; inset: 0; border-radius: 50%; background: color-mix(in srgb, var(--c), #000 32%); }
.ctr i:nth-child(1) { transform: translateZ(2px); }
.ctr i:nth-child(2) { transform: translateZ(4px); }
.ctr i:nth-child(3) { transform: translateZ(6px); }
.ctr i:nth-child(4) {
  transform: translateZ(8px);
  background: radial-gradient(circle at 36% 30%, color-mix(in srgb, var(--c), #fff 38%), var(--c) 58%, color-mix(in srgb, var(--c), #000 18%));
}
.ctr.last i:nth-child(4) { box-shadow: inset 0 0 0 3px color-mix(in srgb, var(--c), #fff 65%); }
.over .ctr:not(.win) { opacity: .22; }
.ctr.win i:nth-child(4) { box-shadow: 0 0 0 3px var(--fg), 0 0 18px 6px var(--c); }
.ctr.win { animation: pulse 1.1s ease-in-out infinite alternate; }
@keyframes drop { from { transform: translateZ(70px); opacity: 0; } to { transform: translateZ(1px); opacity: 1; } }
@keyframes pulse { from { transform: translateZ(1px); } to { transform: translateZ(14px); } }

.leg {
  position: absolute; width: 6px; height: calc(var(--gap) * 3 + 28px); margin-left: -3px;
  margin-top: calc((var(--gap) * 3 + 28px) / -2); background: var(--frame); opacity: .8;
  transform: rotateX(90deg); pointer-events: none; transition: opacity .3s;
}
.leg.b { transform: rotateZ(90deg) rotateX(90deg); }
.base {
  position: absolute; inset: -12px; border-radius: 6px; background: var(--frame); opacity: .35;
  transform: translateZ(calc(var(--gap) * -1.5 - 14px)); pointer-events: none; transition: opacity .3s;
}
.flat .leg, .flat .base { opacity: 0; }
.hint { position: absolute; left: 12px; bottom: 8px; margin: 0; font-size: 12.5px; color: var(--muted); pointer-events: none; }

/* ---------- view controls ---------- */
.viewbar { display: flex; flex-wrap: wrap; gap: 10px 20px; align-items: center; }
.group { display: flex; align-items: center; gap: 8px; }
.group > span, .group > label { font-size: 12px; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.seg { display: inline-flex; border: 1px solid var(--rule); border-radius: 5px; overflow: hidden; }
.seg button {
  font: 500 13.5px var(--body); color: var(--fg); background: transparent; border: 0; border-left: 1px solid var(--rule);
  padding: 6px 11px; cursor: pointer; min-width: 34px;
}
.seg button:first-child { border-left: 0; }
.seg button[aria-pressed="true"] { background: var(--fg); color: var(--bg); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
input[type="range"] { width: 110px; accent-color: var(--fg); }

/* ---------- rail ---------- */
.rail { display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.status { font-family: var(--display); font-weight: 700; font-size: 22px; line-height: 1.2; margin: 0; text-wrap: balance; min-height: 2.4em; }
.players { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.player { border: 1px solid var(--rule); border-radius: 5px; padding: 10px 12px; display: flex; align-items: center; gap: 10px; background: var(--panel); }
.player.on { border-color: var(--fg); box-shadow: inset 0 -3px 0 var(--fg); }
.dot { width: 18px; height: 18px; border-radius: 50%; flex: none; }
.player[data-p="1"] .dot { background: var(--p1); }
.player[data-p="2"] .dot { background: var(--p2); }
.player b { font-weight: 600; display: block; line-height: 1.15; }
.player small { color: var(--muted); font-size: 12.5px; }
.score { margin-left: auto; font-family: var(--display); font-weight: 700; font-size: 22px; font-variant-numeric: tabular-nums; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn { font: 600 14px var(--body); padding: 9px 14px; border-radius: 5px; border: 1px solid var(--fg); background: var(--fg); color: var(--bg); cursor: pointer; }
.btn.ghost { background: transparent; color: var(--fg); border-color: var(--rule); }
.btn:disabled { opacity: .4; cursor: default; }
.rules { border-top: 1px solid var(--rule); padding-top: 14px; color: var(--muted); font-size: 14px; }
.rules h2 { font-family: var(--display); font-weight: 700; font-size: 15px; color: var(--fg); margin: 0 0 6px; }
.rules p { margin: 0 0 8px; }
.rules dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 0; font-variant-numeric: tabular-nums; }
.rules dt { font-weight: 600; color: var(--fg); text-align: right; }
.rules dd { margin: 0; }

@media (prefers-reduced-motion: reduce) {
  .ctr, .ctr.win { animation: none; }
  .tower.anim, .shelf { transition: none; }
}

/* ---------- site chrome ---------- */
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; }
a { color: inherit; text-underline-offset: 3px; }
.nav { max-width: 1080px; margin: 0 auto 22px; display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 26px; border-bottom: 1px solid var(--rule); padding-bottom: 12px; }
.brand { font-family: var(--display); font-weight: 700; font-size: 22px; letter-spacing: -.02em; text-decoration: none; }
.nav ul { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 20px; margin: 0; padding: 0; }
.nav li a { text-decoration: none; color: var(--muted); font-weight: 500; }
.nav li a:hover, .nav li a[aria-current="page"] { color: var(--fg); text-decoration: underline; }
.foot { max-width: 1080px; margin: 44px auto 0; border-top: 1px solid var(--rule); padding-top: 14px; color: var(--muted); font-size: 13px; }
.foot p { margin: 0 0 6px; max-width: 75ch; }

/* ---------- reading pages ---------- */
.prose { max-width: 700px; margin: 0 auto; font-size: 16.5px; line-height: 1.6; }
.prose h1 { font-size: 40px; margin: 8px 0 10px; text-wrap: balance; }
.prose .lede { font-size: 19px; color: var(--muted); margin: 0 0 26px; }
.prose h2 { font-family: var(--display); font-weight: 700; font-size: 24px; letter-spacing: -.01em; margin: 36px 0 8px; text-wrap: balance; }
.prose h3 { font-family: var(--display); font-weight: 700; font-size: 17px; margin: 22px 0 4px; }
.prose p, .prose ul, .prose ol { margin: 0 0 14px; }
.prose li { margin-bottom: 6px; }
.prose .note { border-left: 3px solid var(--p1); padding: 2px 0 2px 14px; color: var(--muted); }
.prose .sources { font-size: 14px; color: var(--muted); }
.tablewrap { overflow-x: auto; margin: 0 0 18px; }
.prose table { border-collapse: collapse; width: 100%; font-size: 15px; }
.prose th, .prose td { text-align: left; vertical-align: top; padding: 9px 12px 9px 0; border-bottom: 1px solid var(--rule); }
.prose th { font-size: 12px; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.prose td:first-child { font-weight: 600; white-space: nowrap; }
.cta { display: inline-block; text-decoration: none; }
.below { max-width: 1080px; margin: 40px auto 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 22px 32px; }
.below h2 { font-family: var(--display); font-weight: 700; font-size: 18px; margin: 0 0 4px; }
.below p { margin: 0; color: var(--muted); }
.faq { max-width: 700px; margin: 44px auto 0; }
.faq h2 { font-family: var(--display); font-weight: 700; font-size: 24px; margin: 0 0 10px; }
.faq h3 { font-family: var(--display); font-weight: 700; font-size: 17px; margin: 18px 0 2px; }
.faq p { margin: 0; color: var(--muted); }

/* ---------- small level diagrams ---------- */
.diagrams { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 18px 24px; margin: 0 0 18px; }
.diagram { margin: 0; }
.diagram figcaption { font-size: 14.5px; margin-top: 8px; color: var(--muted); }
.diagram figcaption b { color: var(--fg); }
.levels { display: flex; gap: 10px; }
.levels > div { display: flex; flex-direction: column; gap: 4px; font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.mini { display: grid; grid-template-columns: repeat(4, 14px); gap: 2px; }
.mini i { width: 14px; height: 14px; border: 1px solid var(--rule); border-radius: 50%; print-color-adjust: exact; -webkit-print-color-adjust: exact; }
.mini i.on { background: var(--p1); border-color: var(--p1); }
.mini i.alt { background: var(--p2); border-color: var(--p2); }

/* ---------- pencil-and-paper sheet ---------- */
.sheet { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 20px; margin: 14px 0 6px; }
.sheet > div { font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.paper { display: grid; grid-template-columns: repeat(4, 1fr); aspect-ratio: 1; border: 1.5px solid var(--fg); margin-top: 4px; }
.paper i { border: .75px solid var(--fg); }

@media print {
  :root { --bg: #fff; --panel: #fff; --fg: #000; --muted: #333; --rule: #999; --p1: #e2581f; --p2: #175e6c; }
  body { padding: 0; font-size: 11pt; }
  .nav ul, .foot, .noprint { display: none !important; }
  .nav { border: 0; margin-bottom: 0; }
  .prose { max-width: none; font-size: 11pt; }
  .prose h1 { font-size: 24pt; }
  .prose h2 { break-after: avoid; }
  .diagram, .sheet { break-inside: avoid; }
  .pagebreak { break-before: page; }
}

.flat .hint { display: none; }

/* ---------- nav buttons ---------- */
.navbtns { margin-left: auto; display: flex; gap: 8px; }
.btn.small { padding: 5px 11px; font-size: 13px; }
@media print { .navbtns { display: none !important; } }

/* ---------- touch: marked square and confirm bar ---------- */
.well.pending { border-color: var(--turn); box-shadow: 0 0 0 4px color-mix(in srgb, var(--turn) 50%, transparent); }
.well.pending::after { content: ""; position: absolute; inset: 2px; border-radius: 50%; background: var(--turn); opacity: .8; transform: translateZ(10px); animation: hover-counter .7s ease-in-out infinite alternate; pointer-events: none; }
.well.col { border-color: var(--turn); border-style: dashed; }
@keyframes hover-counter { from { transform: translateZ(10px); } to { transform: translateZ(34px); } }
.confirm { display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--rule); border-radius: 6px; padding: 8px 10px; cursor: default; }
.confirm span { font-weight: 600; flex: 1; min-width: 0; }
.confirm .btn { min-height: 44px; padding-inline: 14px; white-space: nowrap; }
@media (prefers-reduced-motion: reduce) { .well.pending::after { animation: none; } }
