* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Shared identity tokens — kept in sync with assets/css/docs.css so the
   explorer matches the docs site and honors the same light/dark toggle. */
:root {
  --sidebar-width: 250px;
  --paper: #f4efe4; --paper-2: #ece2d0; --surface: #fcf8f0; --surface-2: #f6efe0;
  --ink: #1b1813; --ink-2: #5a5142; --ink-3: #8a7f6c; --rule: #ddd1ba; --rule-strong: #c7b99c;
  --blue: #1f43c4; --blue-2: #16319a; --red: #d8402a; --ochre: #b2760f;
  --canvas: #f1ead9;
  /* diagram palette for the in-app 2D SVGs (lib/svg.js) — mirrors docs.css */
  --figure-mat: #fbf7ee;
  --dia-surface: #fbf7ee; --dia-ink: #24211a; --dia-inksoft: #6a6151; --dia-rigid: #8a8275; --dia-faint: #c9bda7;
  --dia-cord: #1f57c4; --dia-neg: #cf3a26; --dia-pos: #2f8f43; --dia-ring: #b97d12; --dia-gold: #c79a22; --dia-wood: #b07d52; --dia-wash: #ece3d0;
  --display: "Instrument Serif", "Newsreader", Georgia, serif;
  --serif: "Newsreader", Georgia, serif;
  --mono: "JetBrains Mono", ui-monospace, Menlo, monospace;
  --ui: system-ui, -apple-system, sans-serif;
  --radius: 8px;
  /* legacy aliases → identity tokens (existing rules below adopt the new look) */
  --bg: var(--paper);
  --sidebar-bg: #0f1320;
  --sidebar-text: #cdd5e6;
  --sidebar-active: var(--red);
  --accent: var(--blue);
  --text: var(--ink);
  --text-light: var(--ink-3);
  --border: var(--rule);
  --panel-bg: var(--surface);
}
[data-theme="dark"] {
  --paper: #0f1320; --paper-2: #141a2b; --surface: #172033; --surface-2: #1d2740;
  --ink: #ece5d6; --ink-2: #b4ac99; --ink-3: #808b9f; --rule: #2a3450; --rule-strong: #3b486a;
  --blue: #7aa2ff; --blue-2: #9bb8ff; --red: #ff7d5e; --ochre: #e3b24c; --canvas: #161b28;
  --sidebar-bg: #0b0f1a;
  --figure-mat: #1b2338;
  --dia-surface: #1b2338; --dia-ink: #e8e2d4; --dia-inksoft: #aab2c0; --dia-rigid: #8b94a6; --dia-faint: #3c4760;
  --dia-cord: #6f9cff; --dia-neg: #ff7d5e; --dia-pos: #5cc06f; --dia-ring: #e3b24c; --dia-gold: #e8c24e; --dia-wood: #c99a6e; --dia-wash: #222c44;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0f1320; --paper-2: #141a2b; --surface: #172033; --surface-2: #1d2740;
    --ink: #ece5d6; --ink-2: #b4ac99; --ink-3: #808b9f; --rule: #2a3450; --rule-strong: #3b486a;
    --blue: #7aa2ff; --blue-2: #9bb8ff; --red: #ff7d5e; --ochre: #e3b24c; --canvas: #161b28;
    --sidebar-bg: #0b0f1a;
    --figure-mat: #1b2338;
    --dia-surface: #1b2338; --dia-ink: #e8e2d4; --dia-inksoft: #aab2c0; --dia-rigid: #8b94a6; --dia-faint: #3c4760;
    --dia-cord: #6f9cff; --dia-neg: #ff7d5e; --dia-pos: #5cc06f; --dia-ring: #e3b24c; --dia-gold: #e8c24e; --dia-wood: #c99a6e; --dia-wash: #222c44;
  }
}

html, body {
  height: 100%;
  font-family: var(--ui);
  background: var(--bg);
  color: var(--text);
}

#app {
  display: flex;
  height: 100%;
  overflow: hidden;
}

/* Sidebar */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-header {
  padding: 18px 16px 12px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.sidebar-brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: inherit; }
.sidebar-brand .logomark { width: 26px; height: 26px; flex: none; }
.sidebar-brand .logomark .lm-strand { stroke: var(--red); }
.sidebar-brand .logomark .lm-gap { stroke: var(--sidebar-bg); }

.sidebar-header h1 {
  font-family: var(--display);
  font-size: 1.7rem;
  letter-spacing: 0.06em;
  font-weight: 400;
  line-height: 1;
}
.sidebar-header h1 .x { color: var(--red); font-style: italic; }

.subtitle {
  font-size: 0.7rem;
  font-family: var(--mono);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.45);
  margin-top: 6px;
}

.sidebar-nav { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.sb-link {
  font-family: var(--mono); font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--sidebar-text); text-decoration: none; opacity: .75;
}
.sb-link:hover { opacity: 1; color: #fff; }
.sb-toggle {
  margin-left: auto; appearance: none; background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.15); color: var(--sidebar-text);
  width: 30px; height: 30px; border-radius: 6px; cursor: pointer; font-size: .9rem;
}
.sb-toggle:hover { background: rgba(255,255,255,.16); }
.sb-toggle .moon { display: none; }
[data-theme="dark"] .sb-toggle .sun { display: none; }
[data-theme="dark"] .sb-toggle .moon { display: inline; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .sb-toggle .sun { display: none; }
  :root:not([data-theme="light"]) .sb-toggle .moon { display: inline; }
}

#puzzle-list {
  list-style: none;
  padding: 8px 0;
}

#puzzle-list li {
  padding: 10px 16px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 0.15s;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

#puzzle-list li:hover {
  background: rgba(255,255,255,0.08);
}

#puzzle-list li.active {
  background: rgba(255,255,255,0.07);
  border-left: 3px solid var(--sidebar-active);
}

#puzzle-list li .num {
  font-weight: 600;
  color: rgba(255,255,255,0.4);
  font-size: 0.75rem;
  min-width: 20px;
}

#puzzle-list li .difficulty-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.diff-beginner { background: #4caf50; }
.diff-intermediate { background: #ff9800; }
.diff-advanced { background: #f44336; }
.diff-expert { background: #9c27b0; }

/* Sidebar toggle (mobile) */
#sidebar-toggle {
  display: none;
  position: fixed;
  top: 10px;
  left: 10px;
  z-index: 100;
  background: var(--sidebar-bg);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 6px;
  font-size: 1.2rem;
  cursor: pointer;
}

/* Main content */
#content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.section {
  max-width: 1200px;
  margin: 0 auto;
}

#welcome {
  text-align: center;
  padding: 100px 20px;
  color: var(--text-light);
}

#welcome h2 {
  font-family: var(--serif);
  font-size: 1.8rem;
  margin-bottom: 12px;
  color: var(--text);
}

/* Puzzle header */
#puzzle-header {
  margin-bottom: 24px;
}

#puzzle-name {
  font-family: var(--serif);
  font-size: 2rem;
  font-weight: 560;
  letter-spacing: -.01em;
  margin-bottom: 10px;
}

.puzzle-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.badge {
  font-family: var(--mono);
  font-size: 0.68rem;
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  background: var(--accent);
  color: #fff;
}

.badge-outline {
  background: transparent;
  border: 1px solid var(--rule-strong);
  color: var(--ink-2);
}

.principle {
  font-family: var(--serif);
  font-size: 0.95rem;
  color: var(--text-light);
  font-style: italic;
}

#puzzle-description {
  font-size: 0.9rem;
  line-height: 1.5;
  color: var(--text-light);
  max-width: 700px;
}

/* Viz layout */
#viz-layout {
  display: grid;
  grid-template-columns: 1fr 1.5fr;
  gap: 20px;
  margin-bottom: 20px;
}

.viz-panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.viz-panel h3 {
  font-size: 0.9rem;
  margin-bottom: 12px;
  color: var(--text);
}

.viz-panel h3 .hint {
  font-weight: 400;
  font-size: 0.75rem;
  color: var(--text-light);
}

#svg-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

#canvas-container {
  position: relative;
  width: 100%;
  height: 400px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--canvas);
}

#three-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Animation panel */
#anim-container {
  position: relative;
  width: 100%;
  height: 400px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--canvas);
}

#anim-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#step-label {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30,20,10,0.85);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 0.8rem;
  pointer-events: none;
  white-space: nowrap;
  max-width: 90%;
  overflow: hidden;
  text-overflow: ellipsis;
}

#anim-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0 4px;
}

#anim-controls button {
  background: var(--sidebar-bg);
  color: white;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

#anim-controls button:hover {
  background: #2a2a4e;
}

#btn-play {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  font-size: 1rem;
}

#scrubber {
  flex: 1;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}

#scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

#step-counter {
  font-size: 0.75rem;
  color: var(--text-light);
  min-width: 70px;
  text-align: center;
}

#speed-select {
  font-size: 0.75rem;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

/* Responsive */
@media (max-width: 900px) {
  #viz-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 700px) {
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  #sidebar.open {
    transform: translateX(0);
  }

  #sidebar-toggle {
    display: block;
  }

  #content {
    padding: 16px;
    padding-top: 56px;
  }
}

/* Error messages (graceful degradation) */
.error-msg {
  color: #c33;
  text-align: center;
  padding: 40px 20px;
  font-size: 0.9rem;
  line-height: 1.5;
}

/* Debug overlay */
#debug-overlay {
  position: fixed;
  bottom: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.82);
  color: #0f0;
  font: 11px/1.4 'SF Mono', 'Consolas', 'Menlo', monospace;
  padding: 8px 12px;
  border-radius: 6px;
  z-index: 9999;
  pointer-events: none;
  white-space: pre;
  max-width: 280px;
}
