:root {
  --bg: #FBFAF7;
  --surface: #FFFFFF;
  --ink: #23222B;
  --muted: #6B6A75;
  --line: #E2DED6;
  --accent: #23222B;
  --radius: 12px;
  --gutter: 16px;
  --sheet-h: 40vh;
  color-scheme: light;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; touch-action: manipulation; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding: 0 max(var(--gutter), env(safe-area-inset-left)) env(safe-area-inset-bottom) max(var(--gutter), env(safe-area-inset-right));
}

body.sheet-open { padding-bottom: calc(var(--sheet-pad, var(--sheet-h)) + 16px); }

.top, .chips, main, .foot { max-width: 600px; margin-left: auto; margin-right: auto; }

.top { padding-top: 14px; }
h1 {
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-weight: 400;
  font-size: 1.5rem;
  margin: 0 0 2px;
  letter-spacing: -0.01em;
}
.lede { margin: 0 0 8px; color: var(--muted); font-size: 0.9rem; }

/* ---- Chips */
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 2px 6px;
  min-height: 48px;
  align-items: center;
  scrollbar-width: thin;
}
.chip {
  display: flex;
  align-items: stretch;
  flex: none;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  overflow: hidden;
}
.chip.active { border-color: var(--ink); box-shadow: 0 0 0 1px var(--ink); }
.chip:not(.on) .chip-main { color: var(--muted); }
.chip button {
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  cursor: pointer;
  min-height: 40px;
}
.chip-toggle {
  width: 40px;
  display: grid;
  place-items: center;
  padding: 0;
}
.chip-toggle::before {
  content: "";
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2.5px solid var(--c);
  background: transparent;
}
.chip-toggle[aria-pressed="true"]::before { background: var(--c); }
.chip-main {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 14px 0 2px;
  font-size: 0.9rem;
  white-space: nowrap;
}
.chip-name { max-width: 14em; overflow: hidden; text-overflow: ellipsis; }
.chip-score {
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
  color: var(--muted);
  border-left: 1px solid var(--line);
  padding-left: 8px;
}

/* ---- Score */

/* ---- Chart */
.chart-wrap { width: 100%; max-width: 560px; margin: 0 auto; }
/* On a wide screen, keep room under the chart for the card sheet. */
@media (min-width: 700px) {
  .chart-wrap { max-width: clamp(320px, (100vh - 520px) * 1.18, 560px); }
}
#chart {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
#labels text { cursor: pointer; }
.handle { cursor: grab; outline: none; }
.handle .focus-ring { opacity: 0; }
.handle.selected .focus-ring, .handle:focus-visible .focus-ring { opacity: 1; }

/* ---- Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 2px 0 4px;
}
.toolbar .group { display: flex; flex-wrap: wrap; gap: 8px; }
button {
  font: inherit;
  font-size: 0.9rem;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: 0.4; cursor: default; }
button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.toast {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  margin: 0;
  max-width: calc(100vw - 32px);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-size: 0.875rem;
  box-shadow: 0 4px 16px rgba(35, 34, 43, 0.2);
}
.toast:empty { display: none; }
.privacy { font-size: 0.8rem; color: var(--muted); margin: 12px 0 16px; }
.foot { font-size: 0.75rem; color: var(--muted); padding-bottom: 16px; min-height: 1em; }

/* ---- Sheet */
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  transform: translateY(105%);
  transition: transform 220ms ease;
  pointer-events: none;
}
.sheet.open { transform: translateY(0); pointer-events: auto; }
@media (prefers-reduced-motion: reduce) { .sheet { transition: none; } }
.sheet-inner {
  max-width: 600px;
  margin: 0 auto;
  max-height: var(--sheet-h);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--surface);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 30px rgba(35, 34, 43, 0.12);
  padding: 0 var(--gutter) calc(16px + env(safe-area-inset-bottom));
}
.sheet.expanded .sheet-inner { max-height: 85vh; }
/* A card that fits needs no grabber. */
.sheet.fits:not(.expanded) .grabber::before { visibility: hidden; }
.sheet.fits:not(.expanded) .grabber { pointer-events: none; }
.grabber {
  display: block;
  width: 100%;
  min-height: 22px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  position: relative;
}
.grabber::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 8px;
  width: 40px;
  height: 5px;
  margin-left: -20px;
  border-radius: 3px;
  background: var(--line);
}
.sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
}
.swatch { width: 14px; height: 14px; border-radius: 4px; flex: none; }
.sheet-head h2 { margin: 0; font-size: 1.15rem; }
.stop { font-variant-numeric: tabular-nums; color: var(--muted); margin-right: auto; }
.close {
  width: 44px;
  padding: 0;
  font-size: 1.5rem;
  line-height: 1;
  border: 0;
  background: none;
}
.measures { margin: 6px 0 10px; color: var(--muted); font-size: 0.9rem; }
.between { margin: 0 0 8px; font-size: 0.85rem; color: var(--muted); }
.pair { display: grid; gap: 10px; }
@media (min-width: 560px) { .pair { grid-template-columns: 1fr 1fr; } }
.card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: var(--bg);
}
.card-level { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.card h3 { margin: 2px 0 6px; font-size: 1.05rem; }
.card p { margin: 0 0 8px; }
.card p:last-child { margin-bottom: 0; }
.unwritten { color: var(--muted); font-style: italic; font-size: 0.875rem; }

/* ---- Dialog */
dialog {
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
  width: min(92vw, 380px);
  color: var(--ink);
  background: var(--surface);
}
dialog::backdrop { background: rgba(35, 34, 43, 0.35); }
dialog label { display: block; font-weight: 600; margin-bottom: 8px; }
dialog input {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 10px 12px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
}
dialog input:focus { outline: 2px solid var(--ink); outline-offset: 1px; }
.dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.dialog-buttons .danger { margin-right: auto; color: #A12C2C; }
.dialog-buttons .danger.armed { background: #A12C2C; border-color: #A12C2C; color: #fff; }

.offscreen { position: fixed; left: -9999px; top: 0; }

/* ---- View switch (icons) and chip scores */
.views { display: inline-flex; border: 1.5px solid var(--line); border-radius: 999px; padding: 2px; background: var(--surface); }
.views button { width: 44px; min-height: 40px; padding: 0; border: 0; border-radius: 999px; background: none; display: grid; place-items: center; }
.views svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.views button[aria-pressed="true"] { background: var(--ink); color: #fff; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--muted); min-height: 44px; cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--ink); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.chip-add {
  flex: none;
  width: 40px;
  min-height: 40px;
  padding: 0;
  border: 1.5px dashed var(--muted);
  border-radius: 50%;
  background: var(--surface);
  font-size: 1.3rem;
  line-height: 1;
}
.toolbar { min-height: 44px; }
.card .signs { margin: 6px 0 0; padding-left: 1.1em; font-size: 0.9rem; }
.card .signs li { margin: 2px 0; }
/* Version and the privacy link, top right: the card never reaches the header. */
.meta { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; font-size: 0.75rem; color: var(--muted); }
.meta a { color: inherit; }

/* ---- Privacy page */
.policy { max-width: 600px; margin: 0 auto; padding-bottom: 32px; }
.policy h2 { font-size: 1.05rem; margin: 20px 0 6px; }
.policy p, .policy li { line-height: 1.55; }
.policy code { font-size: 0.9em; background: #EFECE6; padding: 1px 4px; border-radius: 4px; }

/* ---- Tall screens: a wider layout, and the card right under the chart */
@media (min-width: 900px) and (min-height: 1100px) {
  .top, .chips, main, .foot, .sheet-inner { max-width: 780px; }
  .chart-wrap { max-width: clamp(320px, (100vh - 520px) * 1.18, 760px); }
}
.sheet.floating { position: absolute; top: var(--sheet-top); bottom: auto; transform: none; opacity: 0; transition: opacity 180ms ease; }
.sheet.floating.open { opacity: 1; }
.sheet.floating .sheet-inner { border-bottom: 1px solid var(--line); border-radius: 18px; box-shadow: 0 8px 30px rgba(35, 34, 43, 0.12); padding-top: 14px; }
.sheet.floating .grabber { display: none; }

/* ---- Version stamp, top right */
.title-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.build { font-size: 0.75rem; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---- More menu: copy, PNG, chip scores. It sits above the chart, so the card never covers it. */
.toolbar { position: relative; flex-wrap: wrap; }
.toolbar .views { margin-left: auto; }
.more { position: relative; }
.more summary {
  list-style: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 1.5px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  cursor: pointer;
}
.more summary::-webkit-details-marker { display: none; }
.more summary svg { width: 20px; height: 20px; fill: currentColor; }
.more[open] summary { background: var(--ink); color: #fff; border-color: var(--ink); }
.more summary:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  min-width: 190px;
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 8px 24px rgba(35, 34, 43, 0.14);
}
.menu button { text-align: left; }
.menu .check { padding: 0 8px; }

/* ---- Card: stepping stones, revealed a step at a time */
.stones-wrap { display: flex; gap: 6px; margin-top: 6px; }
.stones { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.stone-row { display: flex; align-items: center; }
.rail-cell { flex: none; display: block; color: var(--axis, var(--ink)); }
.rail-gap { width: 22px; flex: none; }
.rail-thumb { flex: none; display: block; cursor: grab; touch-action: none; border-radius: 8px; }
.rail-thumb:focus-visible { outline: 2px solid var(--axis, var(--ink)); outline-offset: 1px; }
.stone {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--muted);
  text-align: left;
  font-size: 0.95rem;
}
.stone.empty { color: var(--line); font-style: italic; padding-left: 0; }
.stone-level { width: 2.6em; flex: none; font-variant-numeric: tabular-nums; }
.stone.viewed { background: color-mix(in srgb, var(--axis, var(--ink)) 12%, transparent); color: var(--ink); font-weight: 700; font-size: 1.05rem; }
.stone-arrows { display: flex; flex-direction: column; justify-content: space-between; }
.stone-arrows button { width: 40px; min-height: 44px; padding: 0; display: grid; place-items: center; border-radius: 10px; }
.stone-arrows svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.card-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 2px 28px; }
.card-actions button { min-height: 40px; font-weight: 600; }
.reveal { display: flex; flex-direction: column; gap: 8px; padding: 0 10px 0 38px; }
.reveal:empty { display: none; }
.reveal p { margin: 4px 0 0; line-height: 1.5; }
.reveal .signs { margin: 0; padding-left: 1.1em; font-size: 0.92rem; }
#sheet-body { display: flex; flex-direction: column; }
