/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Theme variables ── */
:root {
  --bg:        #ffffff;
  --bg2:       #f6f8fa;
  --bg3:       #e1e4e8;
  --border:    #d1d9e0;
  --text:      #1f2328;
  --text2:     #57606a;
  --accent:    #0969da;
  --accent2:   #0550ae;
  --green:     #1a7f37;
  --green-bg:  #dafbe1;
  --purple:    #8250df;
  --purple-bg: #fbefff;
  --orange:    #bc4c00;
  --orange-bg: #fff1e5;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #0d1117;
    --bg2:       #161b22;
    --bg3:       #21262d;
    --border:    #30363d;
    --text:      #e6edf3;
    --text2:     #8b949e;
    --accent:    #58a6ff;
    --accent2:   #79c0ff;
    --green:     #3fb950;
    --green-bg:  #0f2a0f;
    --purple:    #d2a8ff;
    --purple-bg: #1a1a3a;
    --orange:    #ffa657;
    --orange-bg: #3a1a0e;
  }
}

[data-theme="dark"] {
  --bg:        #0d1117;
  --bg2:       #161b22;
  --bg3:       #21262d;
  --border:    #30363d;
  --text:      #e6edf3;
  --text2:     #8b949e;
  --accent:    #58a6ff;
  --accent2:   #79c0ff;
  --green:     #3fb950;
  --green-bg:  #0f2a0f;
  --purple:    #d2a8ff;
  --purple-bg: #1a1a3a;
  --orange:    #ffa657;
  --orange-bg: #3a1a0e;
}

/* ── Base ── */
html { scroll-behavior: smooth; font-size: 16px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

/* ── Sticky header ── */
#site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}

#top-nav {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 24px;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.site-title { font-weight: 700; font-size: 15px; white-space: nowrap; }

.nav-links { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.nav-links a {
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text2);
  text-decoration: none;
  transition: background 0.15s, color 0.15s;
}
.nav-links a:hover { background: var(--bg3); color: var(--text); }

#theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 16px;
  color: var(--text);
}
#theme-toggle:hover { background: var(--bg3); }

/* ── Sections ── */
main { max-width: 900px; margin: 0 auto; padding: 40px 24px 80px; }
section { margin-bottom: 72px; }
section h2 {
  font-size: 1.6rem;
  font-weight: 700;
  margin-bottom: 24px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
section h3 { font-size: 1.1rem; font-weight: 600; margin: 24px 0 12px; }
section p  { margin-bottom: 12px; color: var(--text); }

ul.refs { margin: 0 0 12px; padding-left: 20px; }
ul.refs li { margin-bottom: 10px; color: var(--text); line-height: 1.55; }
ul.refs a { color: var(--accent); white-space: nowrap; }

ol.rule-list { margin: 0 0 12px; padding-left: 22px; }
ol.rule-list li { margin-bottom: 10px; color: var(--text); line-height: 1.55; }

/* ── Code blocks ── */
pre { border-radius: 8px; overflow-x: auto; font-size: 13px; margin: 16px 0; }
code { font-family: "SFMono-Regular", Consolas, monospace; font-size: 0.875em; }
:not(pre) > code {
  background: var(--bg3);
  padding: 2px 6px;
  border-radius: 4px;
}

/* ── Nav active link ── */
.nav-links a.active { background: var(--bg3); color: var(--accent); font-weight: 600; }

/* ── Odd/Even section ── */
.side-by-side { display: flex; gap: 24px; flex-wrap: wrap; margin: 24px 0; }
.oe-box { flex: 1; min-width: 200px; background: var(--bg2); border: 1px solid var(--border); border-radius: 8px; padding: 16px; }
.oe-label { font-size: 12px; font-weight: 600; color: var(--text2); margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.04em; }
.oe-string { display: flex; align-items: center; gap: 3px; margin-bottom: 6px; }
.oe-cell { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; background: var(--bg3); border: 1px solid var(--border); border-radius: 4px; font-family: monospace; font-size: 15px; }
.oe-center { background: var(--purple-bg); border-color: var(--purple); color: var(--purple); }
.oe-gap { width: 16px; text-align: center; font-size: 18px; color: var(--accent); font-weight: bold; }
.oe-indices { display: flex; gap: 3px; font-size: 10px; color: var(--text2); font-family: monospace; }
.oe-indices span { width: 32px; text-align: center; }
.oe-ci { color: var(--purple) !important; font-weight: bold; }
.oe-note { margin-top: 8px; font-size: 12px; color: var(--text2); font-family: monospace; }

/* ── Separator transform demo ── */
.transform-demo { background: var(--bg2); border: 1px solid var(--border); border-radius: 8px; padding: 20px; margin: 20px 0; }
.td-row { display: flex; align-items: center; gap: 12px; margin: 8px 0; }
.td-label { font-size: 12px; color: var(--text2); width: 100px; flex-shrink: 0; }
.td-chars { display: flex; gap: 2px; }
.td-char { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; background: var(--bg3); border: 1px solid var(--border); border-radius: 4px; font-family: monospace; font-size: 14px; }
.td-sep { width: 22px; height: 30px; display: flex; align-items: center; justify-content: center; color: var(--accent); font-family: monospace; font-size: 14px; opacity: 0.7; }
.td-arrow { text-align: center; color: var(--text2); font-size: 13px; margin: 6px 0; }

/* ── Variable reference table ── */
.var-table { width: 100%; border-collapse: collapse; font-size: 14px; margin: 16px 0; }
.var-table th, .var-table td { padding: 10px 14px; border: 1px solid var(--border); text-align: left; }
.var-table th { background: var(--bg2); font-weight: 600; }
.var-table tr:nth-child(even) td { background: var(--bg2); }
.color-i { color: var(--orange); font-weight: 600; }
.color-c { color: var(--purple); font-weight: 600; }
.color-r { color: var(--green); font-weight: 600; }
.color-m { color: var(--accent); font-weight: 600; }

/* ── Animation section ── */
#anim-controls-top { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }

#variant-selector { display: flex; gap: 4px; flex-wrap: wrap; }
.variant-btn {
  padding: 5px 12px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--bg2); color: var(--text2); cursor: pointer; font-size: 13px; transition: all 0.15s;
}
.variant-btn:hover { background: var(--bg3); color: var(--text); }
.variant-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

#custom-input-wrap { display: flex; align-items: center; gap: 8px; font-size: 13px; }
#custom-input {
  font-family: monospace; font-size: 15px; padding: 5px 10px;
  border: 1px solid var(--border); border-radius: 6px;
  background: var(--bg2); color: var(--text); width: 220px;
}
#custom-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

#step-description {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px 14px; font-size: 13px; color: var(--text2); min-height: 42px;
  margin-bottom: 16px; line-height: 1.5;
}
#step-description strong { color: var(--text); }

#viz-container {
  position: relative; overflow-x: auto; margin-bottom: 16px;
  padding-bottom: 12px;
}
#cell-row {
  display: flex; gap: 2px; padding: 60px 8px 0;
  width: max-content;
}

/* ── p[] radii row ── */
#p-array { width: max-content; margin-top: 72px; }
.p-array-label { font-size: 11px; color: var(--text2); padding-left: 8px; margin-bottom: 4px; font-family: monospace; }
.p-array-label code { color: var(--purple); background: none; }
#p-row { display: flex; gap: 2px; padding-left: 8px; }
.p-cell {
  height: 24px; display: flex; align-items: center; justify-content: center;
  border-radius: 4px; border: 1px solid var(--border); background: var(--bg2);
  color: var(--text2); font-family: monospace; font-size: 12px; flex-shrink: 0;
}
.p-cell.p-current { background: var(--orange-bg); border-color: var(--orange); color: var(--orange); font-weight: 700; }
.p-cell.p-center  { background: var(--purple-bg); border-color: var(--purple); color: var(--purple); }

/* ── Synced execution trace ── */
#code-sync {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 8px;
  margin-bottom: 16px; overflow: hidden;
}
.code-sync-title {
  font-size: 11px; color: var(--text2); padding: 8px 14px;
  border-bottom: 1px solid var(--border); background: var(--bg3);
}
#code-sync-pre { margin: 0; padding: 10px 0; font-family: monospace; font-size: 13px; line-height: 1.5; overflow-x: auto; }
.csl { display: flex; align-items: center; padding: 0 14px 0 6px; white-space: pre; }
.csl-mark { width: 14px; color: var(--orange); font-size: 11px; flex-shrink: 0; }
.csl-text { color: var(--text2); }
.csl.active { background: var(--orange-bg); }
.csl.active .csl-text { color: var(--text); font-weight: 600; }
.csl.active .csl-mark::before { content: '▶'; }

/* ── Active-rule (cases) panel ── */
#rules-panel {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 8px;
  margin-bottom: 16px; overflow: hidden;
}
.rules-title {
  font-size: 11px; color: var(--text2); padding: 8px 14px;
  border-bottom: 1px solid var(--border); background: var(--bg3);
}
#rules-list { padding: 6px 0; }
.rule { display: flex; align-items: flex-start; padding: 3px 14px 3px 6px; font-size: 13px; line-height: 1.45; }
.rule-mark { width: 14px; color: var(--orange); flex-shrink: 0; }
.rule-id { font-family: monospace; font-weight: 700; color: var(--text2); width: 34px; flex-shrink: 0; }
.rule-text { color: var(--text2); }
.rule.active { background: var(--orange-bg); }
.rule.active .rule-id, .rule.active .rule-text { color: var(--text); }
.rule.active .rule-mark::before { content: '▶'; }

.viz-cell {
  width: 36px; height: 36px; display: flex; align-items: center; justify-content: center;
  border-radius: 4px; border: 1px solid var(--border); font-family: monospace; font-size: 15px;
  background: var(--bg3); color: var(--text); transition: background 0.2s, border-color 0.2s;
  flex-shrink: 0; position: relative;
}
.viz-cell.role-zone     { background: var(--green-bg);  border-color: var(--green);  color: var(--green); }
.viz-cell.role-center   { background: var(--purple-bg); border-color: var(--purple); color: var(--purple); border-width: 2px; }
.viz-cell.role-mirror   { background: var(--purple-bg); border-color: var(--purple); color: var(--purple); }
.viz-cell.role-current  { background: var(--orange-bg); border-color: var(--orange); color: var(--orange); border-width: 2px; }
.viz-cell.role-right    { background: var(--green-bg);  border-color: var(--green);  border-width: 2px; }
.viz-cell.role-sep      { font-size: 11px; color: var(--text2); opacity: 0.6; width: 16px; }
.viz-cell.role-gap      { width: 10px; background: none; border: none; opacity: 0; }

#viz-svg { position: absolute; top: 0; left: 0; pointer-events: none; overflow: visible; }

/* ── Playback controls ── */
#playback-controls { background: var(--bg2); border: 1px solid var(--border); border-radius: 8px; padding: 14px 16px; }
#scrubber-wrap { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
#step-label { font-size: 12px; color: var(--text2); white-space: nowrap; min-width: 90px; font-family: monospace; }
#scrubber { flex: 1; accent-color: var(--accent); }
#btn-row { display: flex; gap: 8px; align-items: center; }
#btn-row button {
  padding: 6px 18px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--bg3); color: var(--text); cursor: pointer; font-size: 13px; transition: all 0.15s;
}
#btn-row button:hover { background: var(--bg); }
#btn-play { background: var(--green-bg); border-color: var(--green); color: var(--green); font-weight: 600; }
#btn-play.playing { background: var(--orange-bg); border-color: var(--orange); color: var(--orange); }
#btn-row label { font-size: 12px; color: var(--text2); display: flex; align-items: center; gap: 6px; margin-left: auto; }
#speed-input { accent-color: var(--accent); width: 80px; }

/* ── Code tab system ── */
.outer-tabs, .lang-tabs { display: flex; gap: 3px; flex-wrap: wrap; margin-bottom: 0; }
.outer-tabs { border-bottom: 1px solid var(--border); padding-bottom: 0; }
.otab, .ltab {
  padding: 6px 14px; border-radius: 6px 6px 0 0; border: 1px solid transparent;
  background: none; color: var(--text2); cursor: pointer; font-size: 13px;
  border-bottom: none; transition: all 0.15s; margin-bottom: -1px;
}
.otab:hover, .ltab:hover { color: var(--text); background: var(--bg2); }
.otab.active { background: var(--bg); border-color: var(--border); border-bottom-color: var(--bg); color: var(--text); font-weight: 600; }
.ltab.active { color: var(--accent); border-bottom: 2px solid var(--accent); }

.outer-panel { display: none; border: 1px solid var(--border); border-top: none; border-radius: 0 0 8px 8px; padding: 20px; }
.outer-panel.active { display: block; }
.variant-desc { margin-bottom: 12px; font-size: 13px; color: var(--text2); }
.lang-tabs { margin-bottom: 12px; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.lang-panel { display: none; }
.lang-panel.active { display: block; }

/* ── Responsive ── */
@media (max-width: 640px) {
  main { padding: 24px 16px 60px; }
  .nav-links { display: none; }
  #anim-controls-top { flex-direction: column; align-items: flex-start; }
  .side-by-side { flex-direction: column; }
  .outer-tabs { overflow-x: auto; flex-wrap: nowrap; }
  .lang-tabs  { overflow-x: auto; flex-wrap: nowrap; }
}
