/* Goqu Quantum Computing Textbook - Styles
   Colors synchronized with viz.DefaultStyle() and draw.DefaultStyle() */

:root {
  --color-bg: #fafaf9;
  --color-surface: #ffffff;
  --color-text: #333333;
  --color-text-muted: #666666;
  --color-accent: #4C72B0;
  --color-accent-hover: #3B5A8E;
  --color-border: #e0e0e0;
  --color-code-bg: #f5f5f4;
  --color-gate-1q: #BDD7FF;
  --color-gate-2q: #D4BBFF;
  --color-measure: #FFDDAA;
  --color-state: #C44E52;
  --color-success: #22c55e;
  --color-error: #ef4444;
  --color-warning: #f59e0b;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "Fira Code", "JetBrains Mono", monospace;
  --font-size-base: 1rem;
  --line-height: 1.7;
  --content-width: 48rem;
  --sidebar-width: 16rem;
  --spacing: 0.5rem;
  --radius: 6px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #1a1a1a;
    --color-surface: #262626;
    --color-text: #CCCCCC;
    --color-text-muted: #999999;
    --color-accent: #6A9BD2;
    --color-accent-hover: #82B1E0;
    --color-border: #3a3a3a;
    --color-code-bg: #2a2a2a;
    --color-gate-1q: #2A4D7F;
    --color-gate-2q: #4A2D7F;
    --color-measure: #7F5A2D;
    --color-state: #E07070;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { font-size: 100%; scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: var(--font-body);
  font-size: var(--font-size-base);
  line-height: var(--line-height);
  color: var(--color-text);
  background: var(--color-bg);
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100vh;
}

/* --- Sidebar --- */
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 1.5rem 1rem;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  font-size: 0.875rem;
}

.sidebar-logo {
  display: block;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--color-accent);
  text-decoration: none;
  margin-bottom: 1.5rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--color-border);
}

.sidebar details { margin-bottom: 0.5rem; }

.sidebar summary {
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 0.25rem 0;
  list-style: none;
}

.sidebar summary::before {
  content: "\25B6";
  display: inline-block;
  width: 1em;
  font-size: 0.6em;
  transition: transform 0.15s;
}

.sidebar details[open] > summary::before { transform: rotate(90deg); }

.sidebar ol {
  list-style: none;
  padding: 0;
  margin: 0.25rem 0 0.5rem 0;
}

.sidebar ol li { margin: 0; }

.sidebar ol li a {
  display: block;
  padding: 0.3rem 0.5rem 0.3rem 1.2rem;
  color: var(--color-text);
  text-decoration: none;
  border-radius: var(--radius);
  transition: background 0.1s;
}

.sidebar ol li a:hover { background: var(--color-bg); }

.sidebar ol li a[aria-current="page"] {
  background: var(--color-accent);
  color: #fff;
  font-weight: 600;
}

.nav-hamburger { display: none; }
#nav-toggle { display: none; }

@media (max-width: 768px) {
  body { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 280px;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 2px 0 8px rgba(0,0,0,0.15);
  }

  #nav-toggle:checked ~ .sidebar { transform: translateX(0); }

  .nav-hamburger {
    display: flex;
    flex-direction: column;
    gap: 5px;
    position: fixed;
    z-index: 101;
    top: 1rem;
    left: 1rem;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
  }

  .nav-hamburger span {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--color-text);
  }
}

/* --- Main content --- */
main {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 2rem 2rem 4rem;
  width: 100%;
}

@media (max-width: 768px) {
  main { padding: 3.5rem 1rem 4rem; }
}

/* --- Breadcrumb --- */
.breadcrumb {
  font-size: 0.85rem;
  color: var(--color-text-muted);
  margin-bottom: 1.5rem;
}

.breadcrumb a {
  color: var(--color-accent);
  text-decoration: none;
}

.breadcrumb a:hover { text-decoration: underline; }

/* --- Typography --- */
article h1 {
  font-size: 2rem;
  line-height: 1.2;
  margin: 0 0 1.5rem;
  color: var(--color-text);
}

article h2 {
  font-size: 1.4rem;
  margin: 2.5rem 0 1rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--color-border);
}

article h3 {
  font-size: 1.15rem;
  margin: 2rem 0 0.75rem;
}

article p { margin: 0 0 1rem; }

article ul, article ol { margin: 0 0 1rem; padding-left: 1.5rem; }

article li { margin-bottom: 0.25rem; }

article strong { font-weight: 600; }

article a { color: var(--color-accent); }

article a:hover { text-decoration: underline; }

article code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--color-code-bg);
  padding: 0.15em 0.35em;
  border-radius: 3px;
}

article pre {
  background: var(--color-code-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.5;
  margin: 0 0 1rem;
}

article pre code {
  background: none;
  padding: 0;
}

article blockquote {
  margin: 0 0 1rem;
  padding: 0.75rem 1rem;
  border-left: 4px solid var(--color-accent);
  background: var(--color-code-bg);
  border-radius: 0 var(--radius) var(--radius) 0;
}

article blockquote p:last-child { margin-bottom: 0; }

article table {
  width: 100%;
  border-collapse: collapse;
  margin: 0 0 1rem;
  font-size: 0.9rem;
}

article th, article td {
  padding: 0.5rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

article th {
  font-weight: 600;
  background: var(--color-code-bg);
}

/* --- Callout boxes --- */
.callout {
  margin: 1.5rem 0;
  padding: 1rem 1.25rem;
  border-radius: var(--radius);
  border-left: 4px solid;
}

.callout-info {
  background: color-mix(in srgb, var(--color-accent) 8%, transparent);
  border-color: var(--color-accent);
}

.callout-warning {
  background: color-mix(in srgb, var(--color-warning) 8%, transparent);
  border-color: var(--color-warning);
}

.callout-key {
  background: color-mix(in srgb, var(--color-success) 8%, transparent);
  border-color: var(--color-success);
}

.callout p:last-child { margin-bottom: 0; }

.callout strong:first-child { display: block; margin-bottom: 0.25rem; }

/* --- KaTeX display math --- */
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 0.5rem 0; }

/* --- Sandbox (QASM code editor + output) --- */
.sandbox {
  margin: 1.5rem 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.sandbox .code {
  display: block;
  width: 100%;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.5;
  padding: 0.75rem 1rem;
  border: none;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-code-bg);
  color: var(--color-text);
  resize: vertical;
  tab-size: 2;
}

.sandbox .code:focus { outline: 2px solid var(--color-accent); outline-offset: -2px; }

.sandbox-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}

.sandbox-controls button,
.exercise-controls button {
  font-family: var(--font-body);
  font-size: 0.85rem;
  padding: 0.35rem 0.85rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-accent);
  color: #fff;
  cursor: pointer;
  transition: background 0.1s;
}

.sandbox-controls button:hover,
.exercise-controls button:hover { background: var(--color-accent-hover); }

.sandbox-controls select {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  color: var(--color-text);
}

.sandbox .output {
  padding: 1rem;
  overflow-x: auto;
  min-height: 2rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
  align-items: flex-start;
}

.sandbox .output svg { max-width: 100%; height: auto; }

.sandbox .output .error {
  color: var(--color-error);
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.85rem;
}

/* --- Simulation (parameter sliders) --- */
.simulation {
  margin: 1.5rem 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.sim-controls {
  padding: 0.75rem 1rem;
  background: var(--color-code-bg);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
}

.sim-controls label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  font-family: var(--font-mono);
}

.sim-controls input[type="range"] { width: 12rem; accent-color: var(--color-accent); }

.sim-controls output {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  min-width: 3rem;
}

.sim-output {
  padding: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
  align-items: flex-start;
}

.sim-output svg { max-width: 100%; height: auto; }

.sim-qasm { display: none; }

/* --- Exercise (graded coding challenge) --- */
.exercise {
  margin: 1.5rem 0;
  border: 2px solid var(--color-accent);
  border-radius: var(--radius);
  overflow: hidden;
}

.exercise-header {
  padding: 0.6rem 1rem;
  background: var(--color-accent);
  color: #fff;
  font-weight: 600;
  font-size: 0.9rem;
}

.exercise-prompt {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--color-border);
}

.exercise-prompt p:last-child { margin-bottom: 0; }

.exercise .code {
  display: block;
  width: 100%;
  font-family: var(--font-mono);
  font-size: 0.875rem;
  line-height: 1.5;
  padding: 0.75rem 1rem;
  border: none;
  border-bottom: 1px solid var(--color-border);
  background: var(--color-code-bg);
  color: var(--color-text);
  resize: vertical;
  tab-size: 2;
}

.exercise-controls {
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--color-surface);
}

.exercise-feedback {
  padding: 0.75rem 1rem;
  font-size: 0.9rem;
  display: none;
}

.exercise-feedback.visible { display: block; }

.exercise-feedback.pass {
  background: color-mix(in srgb, var(--color-success) 10%, transparent);
  color: var(--color-success);
}

.exercise-feedback.fail {
  background: color-mix(in srgb, var(--color-error) 10%, transparent);
  color: var(--color-error);
}

/* --- Flashcard (spaced repetition) --- */
.flashcard-deck {
  margin: 1.5rem 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.flashcard-deck-header {
  padding: 0.6rem 1rem;
  background: var(--color-code-bg);
  border-bottom: 1px solid var(--color-border);
  font-weight: 600;
  font-size: 0.9rem;
}

.flashcard {
  padding: 1.25rem;
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
  transition: background 0.1s;
}

.flashcard:last-child { border-bottom: none; }

.flashcard:hover { background: var(--color-code-bg); }

.flashcard .card-front { font-weight: 500; }

.flashcard .card-back {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--color-border);
  color: var(--color-text-muted);
  display: none;
}

.flashcard.flipped .card-back { display: block; }

.flashcard-controls {
  display: none;
  gap: 0.5rem;
  padding: 0.5rem 0;
  margin-top: 0.5rem;
}

.flashcard.flipped .flashcard-controls { display: flex; }

.flashcard-controls button {
  font-family: var(--font-body);
  font-size: 0.8rem;
  padding: 0.3rem 0.7rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  cursor: pointer;
  background: var(--color-surface);
  color: var(--color-text);
}

.flashcard-controls button:hover { background: var(--color-code-bg); }

.flashcard-controls button.easy { border-color: var(--color-success); color: var(--color-success); }
.flashcard-controls button.hard { border-color: var(--color-error); color: var(--color-error); }

/* --- Predict-Observe-Explain --- */
.poe {
  margin: 1.5rem 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}

.poe-step {
  padding: 1rem 1.25rem;
  display: none;
}

.poe-step.active { display: block; }

.poe-step h3 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  color: var(--color-accent);
}

.poe-step textarea {
  width: 100%;
  font-family: var(--font-body);
  font-size: 0.9rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
  color: var(--color-text);
  resize: vertical;
}

.poe-step button {
  margin-top: 0.75rem;
  font-family: var(--font-body);
  font-size: 0.85rem;
  padding: 0.4rem 1rem;
  border: none;
  border-radius: var(--radius);
  background: var(--color-accent);
  color: #fff;
  cursor: pointer;
}

.poe-step button:hover { background: var(--color-accent-hover); }

.poe-progress {
  display: flex;
  padding: 0.5rem 1rem;
  background: var(--color-code-bg);
  border-bottom: 1px solid var(--color-border);
  gap: 0;
}

.poe-progress-step {
  flex: 1;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.3rem 0;
  color: var(--color-text-muted);
  border-bottom: 2px solid transparent;
}

.poe-progress-step.active {
  color: var(--color-accent);
  border-bottom-color: var(--color-accent);
}

.poe-progress-step.done { color: var(--color-success); }

/* --- Chapter navigation --- */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--color-border);
  gap: 1rem;
}

.chapter-nav a {
  color: var(--color-accent);
  text-decoration: none;
  font-size: 0.9rem;
}

.chapter-nav a:hover { text-decoration: underline; }

.chapter-nav .prev { text-align: left; }
.chapter-nav .next { text-align: right; margin-left: auto; }
.nav-spacer { flex: 1; }

/* --- Index page --- */
.index-hero {
  text-align: center;
  margin: 2rem 0 3rem;
}

.index-hero h1 { font-size: 2.2rem; margin-bottom: 0.5rem; }

.index-hero p { color: var(--color-text-muted); font-size: 1.1rem; }

.part-group { margin-bottom: 2rem; }

.part-group h2 {
  font-size: 1.1rem;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.75rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--color-border);
}

.chapter-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.chapter-list li {
  margin-bottom: 0.25rem;
}

.chapter-list a {
  display: block;
  padding: 0.5rem 0.75rem;
  text-decoration: none;
  color: var(--color-text);
  border-radius: var(--radius);
  transition: background 0.1s;
}

.chapter-list a:hover { background: var(--color-code-bg); }

.chapter-list .chapter-num {
  display: inline-block;
  width: 2rem;
  color: var(--color-text-muted);
  font-weight: 600;
}

/* --- JS-only interactive widgets --- */
.widget {
  margin: 1.5rem 0;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 1rem;
  background: var(--color-surface);
}

.widget-header {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
  color: var(--color-accent);
}

.widget input[type="text"],
.widget input[type="number"] {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-code-bg);
  color: var(--color-text);
}

.widget button {
  font-family: var(--font-body);
  font-size: 0.85rem;
  padding: 0.35rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-accent);
  color: #fff;
  cursor: pointer;
}

.widget button:hover { background: var(--color-accent-hover); }

.widget-output {
  margin-top: 0.75rem;
  font-family: var(--font-mono);
  font-size: 0.9rem;
}

/* --- Amplitude/state table --- */
.state-table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-family: var(--font-mono);
  font-size: 0.85rem;
}

.state-table th, .state-table td {
  padding: 0.4rem 0.75rem;
  text-align: center;
  border-bottom: 1px solid var(--color-border);
}

.state-table th { background: var(--color-code-bg); font-weight: 600; }

/* --- Utility classes --- */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
}

.flex-center { display: flex; justify-content: center; align-items: center; }
.gap-1 { gap: 1rem; }
.mt-1 { margin-top: 1rem; }
.mb-1 { margin-bottom: 1rem; }
