/* ─────────────────────────────────────────────────────────────
   Playbook — design tokens
   The signature is the module: a 72-cell monocrystalline panel, one cell per task.
   Amber means "done" and nothing else. Blue means "you can press this".
   ───────────────────────────────────────────────────────────── */
:root {
  --paper: #F5F6F7;
  --surface: #FFFFFF;
  --ink: #101418;
  --ink-2: #56606B;
  --ink-3: #8A939C;
  --hairline: #E2E6EA;
  --tint: #1E5AA8;
  --tint-soft: #E7EEF7;
  --cell: #18283A;
  --cell-2: #24394F;
  --busbar: rgba(214, 222, 230, 0.55);
  --backsheet: #E9EDF0;
  --frame: #C3CAD1;
  --frame-hi: #EEF1F3;
  --lit: #F2B33D;
  --lit-2: #F8D27A;
  --lit-ink: #7A5200;
  --danger: #B93A12;
  --scrim: rgba(16, 20, 24, 0.32);
  --material: rgba(245, 246, 247, 0.72);
  --sheet: rgba(255, 255, 255, 0.86);

  --sans: "Geist", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: "Source Serif 4", "Iowan Old Style", Georgia, serif;
  --mono: "Geist Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;

  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);
  --ease-sheet: cubic-bezier(0.32, 0.72, 0, 1);
  --gutter: clamp(16px, 5vw, 40px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0B0D10; --surface: #14181D; --ink: #EDF0F3; --ink-2: #A1ABB5; --ink-3: #6E7883;
    --hairline: #242B33; --tint: #86B4F0; --tint-soft: #16263A; --cell: #13233A; --cell-2: #1D3552;
    --busbar: rgba(120, 138, 156, 0.45); --backsheet: #262E37; --frame: #3A434D; --frame-hi: #5A6570;
    --lit: #F4BD4F; --lit-2: #F8D48A; --lit-ink: #F4BD4F; --danger: #F08A5B;
    --scrim: rgba(0, 0, 0, 0.5); --material: rgba(11, 13, 16, 0.7); --sheet: rgba(24, 28, 34, 0.86);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #0B0D10; --surface: #14181D; --ink: #EDF0F3; --ink-2: #A1ABB5; --ink-3: #6E7883;
  --hairline: #242B33; --tint: #86B4F0; --tint-soft: #16263A; --cell: #13233A; --cell-2: #1D3552;
  --busbar: rgba(120, 138, 156, 0.45); --backsheet: #262E37; --frame: #3A434D; --frame-hi: #5A6570;
  --lit: #F4BD4F; --lit-2: #F8D48A; --lit-ink: #F4BD4F; --danger: #F08A5B;
  --scrim: rgba(0, 0, 0, 0.5); --material: rgba(11, 13, 16, 0.7); --sheet: rgba(24, 28, 34, 0.86);
  color-scheme: dark;
}

/* ── Base ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 1rem/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; font-family: var(--sans); text-wrap: balance; }
p { margin: 0; }
a { color: var(--tint); }
button { font: inherit; color: inherit; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}
:focus-visible { outline: 2px solid var(--tint); outline-offset: 2px; border-radius: 4px; }

.eyebrow {
  font: 500 0.8125rem/1.3 var(--sans);
  letter-spacing: 0.01em;
  color: var(--ink-2);
}
.section-label {
  font: 600 0.8125rem/1.3 var(--sans);
  letter-spacing: 0.02em;
  color: var(--ink-3);
  padding-bottom: 10px;
}

/* Owner-only UI shows once this device is unlocked; locked-only UI shows before. */
html:not([data-unlocked="true"]) .owner-only { display: none !important; }
html[data-unlocked="true"] .locked-only { display: none !important; }

/* ── Bar: a translucent material, content scrolls beneath it ── */
.bar {
  position: sticky; top: 0; z-index: 20;
  background: var(--material);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  transition: box-shadow 200ms var(--ease-out);
}
.bar[data-scrolled="true"] { box-shadow: 0 1px 0 var(--hairline); }
.bar-inner {
  max-width: 1120px; margin: 0 auto;
  padding-inline: var(--gutter); padding-top: env(safe-area-inset-top);
  min-height: 52px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.bar-title { font: 600 1rem/1 var(--sans); letter-spacing: -0.01em; }
.bar-count { font-size: 0.8125rem; color: var(--ink-2); }
.bar-back {
  display: inline-flex; align-items: center; gap: 2px;
  font: 500 1rem/1 var(--sans); color: var(--tint); text-decoration: none;
  padding: 8px 8px 8px 0; margin-left: -4px;
}
.bar-back svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.bar-actions { display: flex; gap: 4px; }
.bar-btn {
  border: 0; background: transparent; color: var(--tint);
  font: 500 0.9375rem/1 var(--sans); padding: 9px 10px; border-radius: 8px; cursor: pointer;
  transition: background-color 100ms var(--ease-out), transform 100ms var(--ease-out);
}
.bar-btn:active { background: var(--tint-soft); transform: scale(0.97); }

/* ── The module — the signature ─────────────────────────────
   Frame (anodised aluminium) → backsheet (the white gaps) → cells.
   Each cell: chamfered corners like a real mono wafer, three busbars, a sunlit layer that fades in. */
.module {
  width: 100%; max-width: 344px;
  padding: 7px;
  border-radius: 5px;
  background: linear-gradient(160deg, var(--frame-hi), var(--frame) 55%, var(--frame-hi));
  box-shadow: 0 1px 1px rgba(16, 20, 24, 0.06), 0 12px 32px -12px rgba(16, 20, 24, 0.22);
}
.module-cells {
  display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 3px;
  padding: 5px; background: var(--backsheet); border-radius: 2px;
}
.cell {
  position: relative; display: block; aspect-ratio: 1;
  clip-path: polygon(15% 0, 85% 0, 100% 15%, 100% 85%, 85% 100%, 15% 100%, 0 85%, 0 15%);
  background: linear-gradient(150deg, var(--cell-2), var(--cell) 70%);
}
.cell::before, .cell::after { content: ""; position: absolute; inset: 0; }
.cell::after {
  background: radial-gradient(120% 120% at 30% 20%, var(--lit-2), var(--lit) 60%);
  opacity: 0;
  transition: opacity 420ms var(--ease-out);
}
.cell::before {
  z-index: 1;
  background: linear-gradient(90deg,
    transparent 0 24%, var(--busbar) 24% 26.5%, transparent 26.5% 48.75%,
    var(--busbar) 48.75% 51.25%, transparent 51.25% 73.5%, var(--busbar) 73.5% 76%, transparent 76%);
}
.cell.on::after { opacity: 1; }
.cell.flash { animation: cell-flash 520ms var(--ease-out); }
@keyframes cell-flash { 0% { filter: brightness(1.35); } 100% { filter: brightness(1); } }
.module[data-focus] .cell:not(.in-focus) { opacity: 0.35; transition: opacity 180ms var(--ease-out); }
.module .cell { transition: opacity 180ms var(--ease-out); }

/* A "string": one chapter's cells in a row */
.string { display: flex; flex-wrap: wrap; gap: 3px; }
.string .cell { width: 22px; }
.string-mini { gap: 2px; }
.string .cell-mini { width: 9px; }
.string-mini .cell::before { display: none; }

/* ── Home ─────────────────────────────────────────────────── */
.home {
  max-width: 1120px; margin: 0 auto;
  padding-inline: var(--gutter); padding-block: clamp(28px, 6vw, 64px) 32px;
  display: grid; gap: clamp(28px, 5vw, 44px);
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "intro" "array" "chapters";
}
.intro { grid-area: intro; display: grid; gap: 12px; max-width: 34rem; }
.intro h1 {
  font-size: clamp(2.25rem, 7.2vw, 3.75rem);
  font-weight: 650; line-height: 1.02; letter-spacing: -0.035em;
}
.lede {
  font: 400 1.125rem/1.55 var(--serif);
  font-optical-sizing: auto;
  color: var(--ink-2);
  max-width: 32rem;
}
.array { grid-area: array; display: grid; gap: 14px; justify-items: start; align-content: start; }
.array-caption { font-size: 0.9375rem; color: var(--ink-2); display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.array-caption strong { color: var(--ink); font-weight: 600; }
.chapters { grid-area: chapters; }
.chapters ol { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--hairline); }
.chapter-row {
  display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; align-items: center; gap: 12px;
  padding: 14px 8px; margin-inline: -8px; border-radius: 10px;
  color: inherit; text-decoration: none;
  transition: background-color 120ms var(--ease-out), transform 100ms var(--ease-out);
}
.chapters li + li .chapter-row { box-shadow: 0 -1px 0 var(--hairline); border-radius: 0; }
.chapter-row:hover { background: var(--surface); }
.chapter-row:active { transform: scale(0.99); background: var(--tint-soft); }
.row-num { color: var(--ink-3); font-size: 0.875rem; }
.row-main { display: grid; gap: 2px; }
.row-title { font: 550 1.0625rem/1.3 var(--sans); letter-spacing: -0.01em; }
.row-meta { font-size: 0.8125rem; color: var(--ink-3); }
.row-progress { display: grid; justify-items: end; gap: 5px; }
.row-progress .string { max-width: 110px; justify-content: flex-end; }
.row-count { font-size: 0.75rem; color: var(--ink-2); }

@media (min-width: 900px) {
  .home {
    grid-template-columns: 360px minmax(0, 1fr);
    grid-template-areas: "intro intro" "array chapters";
    column-gap: 72px;
  }
  .array { position: sticky; top: 84px; }
}

/* ── Chapter ──────────────────────────────────────────────── */
.chapter {
  max-width: 44rem; margin: 0 auto;
  padding-inline: var(--gutter); padding-block: clamp(24px, 5vw, 56px) 24px;
}
.chapter-head { display: grid; gap: 12px; padding-bottom: 28px; border-bottom: 1px solid var(--hairline); }
.chapter-head h1 {
  font-size: clamp(2rem, 6.4vw, 3rem); font-weight: 650; line-height: 1.05; letter-spacing: -0.03em;
}
.dek { font: 400 1.1875rem/1.5 var(--serif); font-optical-sizing: auto; color: var(--ink-2); }
.chapter-head .string { margin-top: 8px; }

.prose { font: 400 1.0625rem/1.68 var(--serif); font-optical-sizing: auto; padding-top: 8px; }
.prose > * { max-width: 36rem; }
.prose p, .prose ul, .prose ol, .prose .table-wrap, .prose blockquote { margin: 1.1em 0 0; }
.prose h2 {
  font: 620 1.375rem/1.25 var(--sans); letter-spacing: -0.02em;
  margin: 2.2em 0 0;
}
.prose h3 { font: 600 1.0625rem/1.35 var(--sans); letter-spacing: -0.005em; margin: 1.8em 0 0; }
.prose ul:not(.tasks), .prose ol { padding-left: 1.25em; }
.prose li + li { margin-top: 0.4em; }
.prose li::marker { color: var(--ink-3); }
.prose strong { font-weight: 620; }
.prose blockquote {
  padding: 16px 18px; border-radius: 12px;
  background: var(--tint-soft); color: var(--ink);
  font-size: 1rem; line-height: 1.6;
}
.prose blockquote p:first-child { margin-top: 0; }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.prose table { border-collapse: collapse; width: 100%; font: 400 0.9375rem/1.45 var(--sans); font-variant-numeric: tabular-nums; }
.prose th { text-align: left; font-weight: 600; color: var(--ink-2); font-size: 0.8125rem; padding: 0 12px 8px 0; border-bottom: 1px solid var(--hairline); }
.prose td { padding: 10px 12px 10px 0; border-bottom: 1px solid var(--hairline); vertical-align: top; }
.prose h2:last-of-type + ul:not(.tasks) { font-size: 0.9375rem; }

/* Tasks */
.tally { font: 500 0.875rem/1.3 var(--sans); color: var(--ink-2); margin-top: 0.6em !important; }
.tasks { list-style: none; padding: 0 !important; margin-top: 12px !important; border-top: 1px solid var(--hairline); }
.tasks li { margin: 0 !important; border-bottom: 1px solid var(--hairline); }
.task {
  width: 100%; min-height: 56px;
  display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 14px; align-items: start;
  padding: 16px 6px; margin: 0;
  border: 0; background: transparent; text-align: left; cursor: pointer;
  font: 450 1rem/1.45 var(--sans); letter-spacing: -0.003em; color: var(--ink);
  transition: background-color 100ms var(--ease-out);
}
.task:active { background: var(--tint-soft); }
.box {
  width: 24px; height: 24px; margin-top: -1px; border-radius: 7px;
  border: 1.5px solid var(--ink-3);
  display: grid; place-items: center;
  transition: background-color 180ms var(--ease-out), border-color 180ms var(--ease-out), transform 120ms var(--ease-out);
}
.task:active .box { transform: scale(0.88); }
.box svg { width: 16px; height: 16px; fill: none; stroke: #101418; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 16; stroke-dashoffset: 16; transition: stroke-dashoffset 260ms var(--ease-out) 60ms; }
.task[aria-checked="true"] .box { background: var(--lit); border-color: var(--lit); }
.task[aria-checked="true"] .box svg { stroke-dashoffset: 0; }
.task[aria-checked="true"] .task-text { color: var(--ink-2); }
html:not([data-unlocked="true"]) .box { border-style: dashed; }

/* Worksheet fields */
.fields { display: grid; gap: 14px; margin-top: 16px !important; grid-template-columns: repeat(auto-fill, minmax(min(100%, 15rem), 1fr)); }
.field { display: grid; gap: 6px; padding: 14px; background: var(--surface); border-radius: 12px; box-shadow: 0 0 0 1px var(--hairline); }
.field label { font: 550 0.875rem/1.35 var(--sans); }
.field input {
  width: 100%; font: 500 1.125rem/1.2 var(--mono); font-variant-numeric: tabular-nums; color: var(--ink);
  background: var(--paper); border: 1px solid var(--hairline); border-radius: 8px; padding: 10px 12px;
}
.field input:focus-visible { outline: 2px solid var(--tint); outline-offset: 0; border-color: transparent; }
.field input:disabled { opacity: 0.6; }
.field-foot { display: flex; justify-content: space-between; gap: 8px; font: 400 0.8125rem/1.35 var(--sans); color: var(--ink-3); }
.saved { color: var(--ink-2); white-space: nowrap; }

.next {
  display: grid; gap: 4px; margin-top: 56px; padding: 20px 22px;
  background: var(--surface); border-radius: 16px; box-shadow: 0 0 0 1px var(--hairline);
  text-decoration: none; color: var(--ink);
  transition: transform 100ms var(--ease-out), box-shadow 160ms var(--ease-out);
}
.next:hover { box-shadow: 0 0 0 1px var(--hairline), 0 10px 30px -14px rgba(16, 20, 24, 0.25); }
.next:active { transform: scale(0.985); }
.next-title { font: 620 1.25rem/1.25 var(--sans); letter-spacing: -0.02em; }

/* ── Footer ───────────────────────────────────────────────── */
.foot {
  max-width: 1120px; margin: 0 auto;
  padding-inline: var(--gutter); padding-block: 40px calc(32px + env(safe-area-inset-bottom));
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 20px;
  font-size: 0.8125rem; color: var(--ink-3);
}
.link-btn { border: 0; background: none; padding: 0; color: var(--tint); cursor: pointer; font: inherit; text-decoration: underline; text-underline-offset: 3px; }

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  min-height: 48px; padding: 0 20px; border-radius: 12px; border: 0; cursor: pointer;
  font: 600 1rem/1 var(--sans); letter-spacing: -0.01em;
  transition: transform 100ms var(--ease-out), background-color 120ms var(--ease-out);
}
.btn:active { transform: scale(0.97); }
.btn-solid { background: var(--tint); color: #fff; }
:root[data-theme="dark"] .btn-solid { color: #0B0D10; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn-solid { color: #0B0D10; } }
.btn-quiet { background: var(--tint-soft); color: var(--tint); }

/* ── PIN sheet: rises from the bottom edge and returns the same way ── */
.scrim {
  position: fixed; inset: 0; z-index: 40; background: var(--scrim);
  opacity: 0; transition: opacity 320ms var(--ease-sheet);
}
.scrim[data-open="true"] { opacity: 1; }
.sheet {
  position: fixed; z-index: 50; left: 0; right: 0; bottom: 0;
  margin: 0 auto; width: 100%; max-width: 440px;
  background: var(--sheet);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  backdrop-filter: blur(30px) saturate(180%);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -1px 0 rgba(255, 255, 255, 0.4) inset, 0 -20px 60px -20px rgba(16, 20, 24, 0.35);
  transform: translateY(105%);
  visibility: hidden;
  transition: transform 380ms var(--ease-sheet), visibility 0s linear 380ms;
}
.sheet[data-open="true"] { transform: translateY(0); visibility: visible; transition: transform 380ms var(--ease-sheet), visibility 0s; }
@media (min-width: 560px) {
  .sheet { bottom: 24px; border-radius: 20px; transform: translateY(calc(100% + 32px)); }
}
.sheet-body { display: grid; gap: 12px; padding: 24px 22px calc(22px + env(safe-area-inset-bottom)); }
.sheet-body h2 { font: 650 1.375rem/1.2 var(--sans); letter-spacing: -0.02em; }
.sheet-note { font-size: 0.9375rem; line-height: 1.45; color: var(--ink-2); }
.sheet-body input {
  margin-top: 4px; width: 100%; text-align: center;
  font: 500 1.75rem/1 var(--mono); letter-spacing: 0.4em; padding: 14px 0 14px 0.4em;
  color: var(--ink); background: var(--surface); border: 1px solid var(--hairline); border-radius: 12px;
}
.sheet-body input:focus-visible { outline: 2px solid var(--tint); outline-offset: 0; }
.sheet-error { min-height: 1.35em; font-size: 0.875rem; color: var(--danger); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; }

.toast {
  position: fixed; z-index: 60; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  width: max-content; max-width: min(92vw, 420px); padding: 12px 16px; border-radius: 14px;
  background: var(--ink); color: var(--paper); font: 500 0.9375rem/1.35 var(--sans);
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  transition: transform 300ms var(--ease-sheet), opacity 200ms var(--ease-out);
}
.toast[data-show="true"] { transform: translate(-50%, 0); opacity: 1; }

/* ── Accessibility preferences ────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
  .sheet, .sheet[data-open="true"] { transition: opacity 200ms ease, visibility 0s linear 200ms; transform: none; opacity: 0; }
  .sheet[data-open="true"] { opacity: 1; transition: opacity 200ms ease, visibility 0s; }
  .toast { transition: opacity 200ms ease; transform: translate(-50%, 0); }
  .task:active .box, .btn:active, .bar-btn:active, .chapter-row:active, .next:active { transform: none; }
}
@media (prefers-reduced-transparency: reduce) {
  .bar { background: var(--paper); backdrop-filter: none; -webkit-backdrop-filter: none; }
  .sheet { background: var(--surface); backdrop-filter: none; -webkit-backdrop-filter: none; }
}
@media (prefers-contrast: more) {
  :root { --hairline: #9AA3AC; --ink-3: #56606B; }
}

/* ── Print: one clean chapter on paper ────────────────────── */
@media print {
  :root { --paper: #fff; --surface: #fff; --ink: #000; --ink-2: #333; --ink-3: #555; --hairline: #bbb; }
  body { background: #fff; }
  .bar, .foot, .next, .sheet, .scrim, .toast, .chapter-head .string, .tally { display: none !important; }
  .chapter { max-width: none; padding: 0; }
  .prose { font-size: 11pt; }
  .prose h2 { break-after: avoid; }
  .task { min-height: 0; padding: 8px 0; break-inside: avoid; }
  .box { border: 1.2pt solid #000 !important; background: #fff !important; }
  .task[aria-checked="true"] .box svg { stroke: #000; }
  .field { box-shadow: none; border: 1pt solid #999; break-inside: avoid; }
  .field input { border: 0; border-bottom: 1pt solid #000; border-radius: 0; background: #fff; }
  .prose a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 8.5pt; color: #555; word-break: break-all; }
}
