:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #ecebe6;
  --text: #1c1c1a;
  --muted: #6b6a64;
  --border: #dcdad3;
  --accent: #0051ba;
  --accent-text: #ffffff;
  --accent-soft: #dbe7f7;
  --auf: #8a5a00;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 5%);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212;
    --surface: #1d1d1c;
    --surface-2: #2a2a28;
    --text: #efeee9;
    --muted: #a3a29b;
    --border: #353532;
    --accent: #4f93ff;
    --accent-text: #0b0b0b;
    --accent-soft: #1f3150;
    --auf: #e0a84a;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121212;
  --surface: #1d1d1c;
  --surface-2: #2a2a28;
  --text: #efeee9;
  --muted: #a3a29b;
  --border: #353532;
  --accent: #4f93ff;
  --accent-text: #0b0b0b;
  --accent-soft: #1f3150;
  --auf: #e0a84a;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.45;
  padding: 0 max(16px, env(safe-area-inset-right)) 32px max(16px, env(safe-area-inset-left));
  max-width: 1240px;
  margin-inline: auto;
}

a { color: inherit; }

button, select { font: inherit; color: inherit; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* header ---------------------------------------------------------------- */

.site-header {
  display: flex;
  align-items: center;
  gap: 8px 20px;
  flex-wrap: wrap;
  padding: max(12px, env(safe-area-inset-top)) 0 12px;
}

.brand {
  display: inline-flex; align-items: center; gap: 10px;
  text-decoration: none; font-weight: 700; font-size: 1.1rem;
  min-height: 44px;
}
.brand-logo { width: 30px; height: 30px; display: grid; place-items: center; }
.brand-logo svg { max-width: 100%; max-height: 100%; }

.section-tabs {
  display: flex; gap: 6px; overflow-x: auto; max-width: 100%;
  scrollbar-width: none; flex: 1 1 auto;
  -webkit-overflow-scrolling: touch;
}
.section-tabs::-webkit-scrollbar { display: none; }
.section-tabs a {
  flex: none; text-decoration: none; padding: 10px 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); font-size: 0.95rem;
  white-space: nowrap;
}
.section-tabs a.set-link { background: transparent; border-color: transparent; color: var(--muted); padding-inline: 4px 8px; }
.section-tabs a[aria-current="page"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }

/* home ------------------------------------------------------------------- */

.home .lede { color: var(--muted); max-width: 60ch; font-size: 1.05rem; margin: 8px 0 24px; }

.set-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: calc(var(--radius) + 4px);
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 24px;
}
.set-card h2 { margin: 0 0 4px; font-size: 1.6rem; }
.set-desc { margin: 0 0 16px; color: var(--muted); }

.section-buttons {
  list-style: none; margin: 0; padding: 0;
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.section-button {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 16px 12px; border-radius: var(--radius); text-decoration: none;
  background: var(--surface-2); border: 1px solid transparent; height: 100%;
  transition: border-color .15s, transform .15s;
}
.section-button:hover, .section-button:focus-visible { border-color: var(--accent); }
.section-button:active { transform: scale(0.98); }
.section-icon { width: 96px; height: 96px; display: grid; place-items: center; }
.section-icon svg { max-width: 100%; max-height: 100%; }
.section-name { font-weight: 650; font-size: 1.05rem; text-align: center; }
.section-count { color: var(--muted); font-size: 0.85rem; }

/* section page ----------------------------------------------------------- */

.section-intro { margin: 0 2px 14px; color: var(--muted); max-width: 80ch; }
.section-intro strong { color: var(--text); }
.home .set-card:first-child { margin-top: 8px; }

.case-grid {
  display: grid; gap: 8px;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  margin: 4px 0 20px;
}
.case-group {
  grid-column: 1 / -1; margin: 10px 2px 0; font-size: 0.8rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted);
}
.case-group:first-child { margin-top: 0; }
.case-button {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 6px 8px; border-radius: var(--radius); text-decoration: none;
  background: var(--surface); border: 2px solid var(--border);
  min-height: 44px;
}
.case-button:hover { border-color: var(--muted); }
.case-button[aria-current="true"] { border-color: var(--accent); background: var(--accent-soft); }
.case-icon { width: 72px; height: 72px; display: grid; place-items: center; }
.case-icon svg { max-width: 100%; max-height: 100%; }
.case-name { font-size: 0.85rem; text-align: center; line-height: 1.2; font-weight: 550; }

.detail {
  display: grid; gap: 20px; align-items: start;
}
@media (min-width: 900px) {
  .detail { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .player { position: sticky; top: 12px; }
  /* keep the whole player, controls included, on screen */
  .viewport { aspect-ratio: auto; height: clamp(280px, calc(100vh - 330px), 620px); max-height: none; }
}

.case {
  background: var(--surface); border: 1px solid var(--border); border-radius: calc(var(--radius) + 4px);
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 16px;
}
.js .case { display: none; margin-bottom: 0; }
.js .case.active { display: block; }

.case-head { display: flex; gap: 16px; align-items: center; margin-bottom: 16px; }
.to-cases {
  margin-left: auto; align-self: flex-start; flex: none; display: inline-flex; align-items: center; gap: 2px;
  min-height: 36px; padding: 0 12px 0 6px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted); font-size: 0.85rem; cursor: pointer;
}
.to-cases svg { width: 20px; height: 20px; fill: currentColor; }
.to-cases:hover { color: var(--text); border-color: var(--muted); }
.detail, .case-grid { scroll-margin-top: 12px; }
/* tall enough that choosing any case can bring its details to the top */
.detail { min-height: calc(100vh - 24px); }
.case-head h2 { margin: 0 0 4px; font-size: 1.7rem; line-height: 1.15; }
.big-icon { flex: none; width: 132px; height: 132px; display: grid; place-items: center; }
.big-icon svg { max-width: 100%; max-height: 100%; }
.notes { margin: 0; color: var(--muted); }

.alg { position: relative; padding: 12px; margin: 0 -12px; border-radius: 12px; transition: background-color .15s; }
/* dividers inset to the text, so they stay straight beside the rounded highlight */
.alg::before { content: ""; position: absolute; top: 0; left: 12px; right: 12px; border-top: 1px solid var(--border); }
/* the algorithm currently in the player */
.alg.loaded { background: var(--accent-soft); }
.alg.loaded::before, .alg.loaded + .alg::before { display: none; }
.alg.loaded + .setup { border-top-color: transparent; }
.setup .alg-row { padding: 8px 12px; margin: 0 -12px; border-radius: 12px; transition: background-color .15s; }
.setup.loaded .alg-row { background: var(--accent-soft); }
.alg-label { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-bottom: 4px; }
.alg-length { text-transform: none; letter-spacing: 0; margin-left: 6px; opacity: 0.8; }
.alg-row { display: flex; align-items: center; gap: 12px; }
.alg-text { font-family: var(--mono); font-size: 1.25rem; line-height: 1.5; flex: 1; word-spacing: 0.1em; }
.alg.primary .alg-text { font-size: 1.55rem; font-weight: 600; }
.alg-text.small { font-size: 1.05rem; }
.alg-text .mv, .alg-text .grp { white-space: nowrap; }
.auf { color: var(--auf); font-weight: 600; }
.adjust { white-space: nowrap; }
.then { font-family: var(--sans); font-size: 0.62em; font-weight: 500; color: var(--muted); letter-spacing: 0.02em; vertical-align: 0.12em; }
/* whole-cube rotations are free moves: shown lighter */
.alg-text .rot { color: var(--muted); font-weight: 400; }
.alg.loaded .alg-label { color: var(--accent); }
[data-select] { cursor: pointer; -webkit-tap-highlight-color: transparent; }
@media (hover: hover) {
  .alg[data-select]:hover .alg-text, .setup [data-select]:hover .alg-text { color: var(--accent); }
}

.play-button {
  flex: none; width: 48px; height: 48px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface-2); cursor: pointer;
  display: grid; place-items: center;
}
.play-button svg { width: 24px; height: 24px; fill: currentColor; }
.play-button:hover { border-color: var(--accent); }
.alg.loaded .play-button, .setup.loaded .play-button { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.setup { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 4px; }
.setup summary { cursor: pointer; color: var(--muted); font-size: 0.95rem; min-height: 32px; display: flex; align-items: center; }
.setup-help { margin: 6px 0 8px; color: var(--muted); font-size: 0.9rem; }

/* player ------------------------------------------------------------------ */

.player {
  background: var(--surface); border: 1px solid var(--border); border-radius: calc(var(--radius) + 4px);
  padding: 12px; box-shadow: var(--shadow);
}
.player[hidden] { display: none; }
.player-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); padding: 2px 6px 6px; }

.viewport {
  position: relative; width: 100%; aspect-ratio: 1 / 1; max-height: 62vh;
  margin-inline: auto; touch-action: none;
  background: radial-gradient(circle at 50% 45%, var(--surface-2), var(--surface) 70%);
  border-radius: var(--radius);
}
.viewport canvas { width: 100%; height: 100%; display: block; cursor: grab; }
.viewport canvas:active { cursor: grabbing; }
.reset-view {
  position: absolute; right: 8px; top: 8px; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  display: grid; place-items: center; opacity: 0.85;
}
.reset-view svg { width: 20px; height: 20px; fill: currentColor; }

.chips {
  list-style: none; margin: 12px 0 8px; padding: 0;
  display: flex; flex-wrap: wrap; gap: 6px 4px; align-items: center;
}
.chips { --chip-height: 44px; }
.chips li { display: inline-flex; align-items: center; gap: 4px; flex-wrap: nowrap; }
.chip {
  font-family: var(--mono); font-size: 1.1rem; min-width: 44px; height: var(--chip-height); padding: 0 8px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer;
}
.chip.pre, .chip.post { color: var(--auf); border-style: dashed; }
.chip.rot { color: var(--muted); }
.chip.done { opacity: 0.55; }
.chip.current { background: var(--accent); color: var(--accent-text); border-color: var(--accent); opacity: 1; }
.chip-start { font-family: var(--sans); font-size: 0.85rem; padding: 0 10px; }
/* parentheses drawn as curves the full height of a chip */
.bracket {
  width: 7px; height: calc(var(--chip-height) + 6px);
  border: 2px solid var(--muted); opacity: 0.7;
}
.bracket.open { border-right: none; border-radius: 14px 0 0 14px / 50% 0 0 50%; margin-left: 4px; }
.bracket.close { border-left: none; border-radius: 0 14px 14px 0 / 0 50% 50% 0; margin-right: 4px; }

.controls { display: flex; justify-content: center; gap: 8px; margin: 8px 0; }
.controls button {
  width: 52px; height: 52px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface-2); cursor: pointer; display: grid; place-items: center;
}
.controls svg { width: 26px; height: 26px; fill: currentColor; }
.controls .primary-control { width: 64px; height: 64px; background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.controls .primary-control svg { width: 32px; height: 32px; }
.controls button:disabled { opacity: 0.4; cursor: default; }
.icon-pause { display: none; }
.playing .icon-pause { display: block; }
.playing .icon-play { display: none; }

.options { display: flex; justify-content: center; gap: 20px; flex-wrap: wrap; font-size: 0.95rem; color: var(--muted); }
.options label { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; }
.options select { padding: 6px 8px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); }
.options input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }

@media (max-width: 520px) {
  .case-grid { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 6px; }
  .case-icon { width: 58px; height: 58px; }
  .case-name { font-size: 0.78rem; }
  .case { padding: 16px; }
  .case-head { gap: 12px; }
  .big-icon { width: 96px; height: 96px; }
  .case-head h2 { font-size: 1.4rem; }
  .alg.primary .alg-text { font-size: 1.3rem; }
  .alg-text { font-size: 1.1rem; }
  .section-buttons { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
  .section-icon { width: 76px; height: 76px; }
  .controls button { width: 46px; height: 46px; }
  .controls .primary-control { width: 56px; height: 56px; }
  .chips { --chip-height: 40px; }
  .chip { min-width: 40px; font-size: 1rem; }
}

/* method guides ------------------------------------------------------------ */

.set-card h2 a { text-decoration: none; }
.set-card h2 a:hover { text-decoration: underline; }
.set-guide-link { margin: -8px 0 16px; }
.set-guide-link a { color: var(--accent); text-decoration: none; font-weight: 550; }
.section-tabs a.set-link[aria-current="page"] { color: var(--text); font-weight: 650; background: transparent; border-color: transparent; }

.guide-body {
  background: var(--surface); border: 1px solid var(--border); border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow); padding: 8px 28px 28px; max-width: 860px; margin: 4px auto 0;
  font-size: 1.05rem; line-height: 1.6;
}
.guide-body h1 { font-size: 2rem; margin: 20px 0 8px; line-height: 1.2; }
.guide-body h2 { font-size: 1.4rem; margin: 32px 0 8px; padding-top: 20px; border-top: 1px solid var(--border); }
.guide-body h3 { font-size: 1.1rem; margin: 20px 0 4px; }
.guide-body p, .guide-body li { max-width: 68ch; }
.guide-body code { font-family: var(--mono); font-size: 0.95em; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; white-space: nowrap; }
.guide-body table { border-collapse: collapse; margin: 12px 0; font-size: 0.95rem; }
.guide-body th, .guide-body td { text-align: left; padding: 6px 12px 6px 0; border-bottom: 1px solid var(--border); vertical-align: top; }
.guide-body .lede { font-size: 1.15rem; color: var(--muted); }

.guide-figures { display: flex; flex-wrap: wrap; gap: 16px; margin: 16px 0; }
.guide-cube { margin: 0; width: 150px; text-align: center; }
.guide-cube-art { height: 132px; display: grid; place-items: center; }
.guide-cube-art svg { max-width: 100%; max-height: 100%; }
.guide-cube figcaption { font-size: 0.85rem; color: var(--muted); line-height: 1.3; margin-top: 6px; }

.guide-section {
  display: flex; align-items: center; gap: 14px; margin: 12px 0 4px; padding: 10px 16px 10px 10px;
  border-radius: var(--radius); background: var(--surface-2); border: 1px solid transparent;
  text-decoration: none; max-width: 420px;
}
.guide-section:hover { border-color: var(--accent); }
.guide-section .section-icon { width: 64px; height: 64px; }
.guide-section-text { display: flex; flex-direction: column; }
.guide-section-text span { color: var(--accent); font-size: 0.9rem; }

@media (max-width: 520px) {
  .guide-body { padding: 4px 16px 20px; font-size: 1rem; }
  .guide-cube { width: calc(50% - 8px); }
}
