:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Monaco, Consolas, monospace;

  /* Theme variables - Light Mode by default */
  --bg-canvas: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-raised: #f1f5f9;
  --bg-bench: #e2e8f0;
  --bg-bench-inner: #f1f5f9;
  --bg-barrier: #1e293b;
  --bg-barrier-translucent: rgba(30, 41, 59, 0.22);
  --border-subtle: #cbd5e1;
  --border-strong: #94a3b8;
  --text-primary: #090d16;
  --text-secondary: #1e293b;
  --text-muted: #475569;
  --text-inverse: #ffffff;
  
  --laser-color: #dc2626;
  --laser-glow: rgba(220, 38, 38, 0.45);
  --laser-core: #ffffff;
  --reticle-color: #0284c7;
  --reticle-glow: rgba(2, 132, 199, 0.35);
  --accent-gold: #b45309;
  --success-color: #15803d;
  --error-color: #b91c1c;

  --focus-ring: #0284c7;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-bench: inset 0 2px 4px rgba(0, 0, 0, 0.06), 0 4px 12px rgba(0, 0, 0, 0.05);
}

[data-theme="dark"] {
  --bg-canvas: #090d16;
  --bg-surface: #111827;
  --bg-surface-raised: #1f2937;
  --bg-bench: #0f172a;
  --bg-bench-inner: #162032;
  --bg-barrier: #020617;
  --bg-barrier-translucent: rgba(2, 6, 23, 0.35);
  --border-subtle: #1e293b;
  --border-strong: #334155;
  --text-primary: #f8fafc;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --text-inverse: #090d16;

  --laser-color: #ff2a55;
  --laser-glow: rgba(255, 42, 85, 0.6);
  --laser-core: #ffffff;
  --reticle-color: #38bdf8;
  --reticle-glow: rgba(56, 189, 248, 0.5);
  --accent-gold: #f59e0b;
  --success-color: #22c55e;
  --error-color: #f87171;

  --focus-ring: #38bdf8;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
  --shadow-bench: inset 0 2px 6px rgba(0, 0, 0, 0.6), 0 8px 24px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-canvas: #090d16;
    --bg-surface: #111827;
    --bg-surface-raised: #1f2937;
    --bg-bench: #0f172a;
    --bg-bench-inner: #162032;
    --bg-barrier: #020617;
    --bg-barrier-translucent: rgba(2, 6, 23, 0.35);
    --border-subtle: #1e293b;
    --border-strong: #334155;
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --text-inverse: #090d16;

    --laser-color: #ff2a55;
    --laser-glow: rgba(255, 42, 85, 0.6);
    --laser-core: #ffffff;
    --reticle-color: #38bdf8;
    --reticle-glow: rgba(56, 189, 248, 0.5);
    --accent-gold: #f59e0b;
    --success-color: #22c55e;
    --error-color: #f87171;

    --focus-ring: #38bdf8;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.45);
    --shadow-bench: inset 0 2px 6px rgba(0, 0, 0, 0.6), 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

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

html {
  font-family: var(--font-sans);
  background-color: var(--bg-canvas);
  color: var(--text-primary);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
}

a:focus-visible, button:focus-visible, input:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Header & Meta Bar */
.site-header {
  width: 100%;
  max-width: 820px;
  padding: 1rem 1.25rem 0.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.brand-link {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-muted);
  text-decoration: none;
  letter-spacing: -0.01em;
}

.brand-link:hover {
  color: var(--text-primary);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.theme-toggle, .lang-toggle, .audio-toggle {
  background: var(--bg-surface-raised);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 0.35rem 0.65rem;
  border-radius: 9999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.theme-toggle:hover, .lang-toggle:hover, .audio-toggle:hover {
  background: var(--bg-surface);
  border-color: var(--border-strong);
  color: var(--text-primary);
}

/* Main Container */
.main-content {
  width: 100%;
  max-width: 820px;
  padding: 0.5rem 1.25rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* Hero & Deck */
.hero {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.hero-title {
  font-size: clamp(1.6rem, 4vw, 2.25rem);
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--text-primary);
  line-height: 1.15;
}

.hero-deck {
  font-size: clamp(0.9375rem, 2vw, 1.0625rem);
  color: var(--text-secondary);
  line-height: 1.45;
  max-width: 680px;
}

/* Optical Bench Stage */
.bench-wrapper {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 1rem;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.bench-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
}

.round-indicator {
  font-weight: 600;
  color: var(--text-primary);
}

.round-spec {
  color: var(--text-muted);
}

.stage-container {
  position: relative;
  width: 100%;
  height: 380px;
  background-color: var(--bg-bench);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow-bench);
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
}

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

/* Reticle carriage handle for accessibility and visual cue */
.carriage-cue {
  position: absolute;
  right: 0;
  pointer-events: none;
  transition: transform 0.05s ease-out;
}

/* Stage Actions & Controls */
.bench-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.status-callout {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 2.25rem;
}

.status-badge {
  display: inline-block;
  padding: 0.15rem 0.45rem;
  border-radius: var(--radius-sm);
  font-weight: 600;
}

.badge-pending {
  background: var(--bg-surface-raised);
  color: var(--text-muted);
}

.badge-result {
  background: var(--bg-surface-raised);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
}

.control-buttons {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn {
  font-family: var(--font-sans);
  font-size: 0.875rem;
  font-weight: 600;
  padding: 0.5rem 1.15rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  border: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  transition: background 0.15s ease, transform 0.05s ease, opacity 0.15s ease;
  min-height: 40px;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--laser-color);
  color: #ffffff;
  box-shadow: 0 2px 8px var(--laser-glow);
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-secondary {
  background: var(--bg-surface-raised);
  border: 1px solid var(--border-subtle);
  color: var(--text-primary);
}

.btn-secondary:hover {
  background: var(--border-subtle);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* Stage instructions footer */
.bench-tip {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
  display: flex;
  justify-content: space-between;
  border-top: 1px solid var(--border-subtle);
  padding-top: 0.5rem;
}

/* Scorecard / Final Summary Panel */
.scorecard-panel {
  display: none;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow-md);
  flex-direction: column;
  gap: 1.25rem;
}

.scorecard-panel.active {
  display: flex;
}

.scorecard-header {
  border-bottom: 1px solid var(--border-subtle);
  padding-bottom: 0.75rem;
}

.scorecard-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 0.25rem;
}

.scorecard-subtitle {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
}

.metric-card {
  background: var(--bg-surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.875rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.metric-label {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.metric-value {
  font-family: var(--font-mono);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
}

.metric-detail {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.scatter-card {
  background: var(--bg-surface-raised);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.scatter-title {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
}

canvas#scatter-canvas {
  width: 100%;
  height: 120px;
  display: block;
}

.diagnosis-box {
  background: var(--bg-surface-raised);
  border-left: 3px solid var(--laser-color);
  padding: 0.875rem 1rem;
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  font-size: 0.875rem;
  color: var(--text-secondary);
  line-height: 1.45;
}

.scorecard-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

/* Editorial Content (Minto Pyramid) */
.editorial-section {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  border-top: 1px solid var(--border-subtle);
  padding-top: 1.75rem;
}

.editorial-heading {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.editorial-text {
  font-size: 0.9375rem;
  color: var(--text-secondary);
  line-height: 1.6;
}

.editorial-text strong {
  color: var(--text-primary);
  font-weight: 600;
}

.sources-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
  padding-left: 0;
}

.sources-list li {
  padding-left: 1rem;
  text-indent: -1rem;
  line-height: 1.4;
}

.sources-list a {
  color: var(--text-secondary);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.sources-list a:hover {
  color: var(--text-primary);
}

.limitations-note {
  font-size: 0.8125rem;
  color: var(--text-muted);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0.75rem 1rem;
  line-height: 1.45;
}

/* Footer */
.site-footer {
  width: 100%;
  max-width: 820px;
  border-top: 1px solid var(--border-subtle);
  padding: 1.5rem 1.25rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8125rem;
  color: var(--text-muted);
  flex-wrap: wrap;
  gap: 0.75rem;
}

.footer-nav {
  display: flex;
  gap: 1rem;
}

.footer-nav a {
  color: var(--text-muted);
  text-decoration: none;
}

.footer-nav a:hover {
  color: var(--text-primary);
  text-decoration: underline;
}

/* Toast notification for copy */
.toast-msg {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text-primary);
  color: var(--bg-canvas);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  font-weight: 500;
  padding: 0.5rem 1rem;
  border-radius: 9999px;
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
  z-index: 100;
}

.toast-msg.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* Screen reader only utility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* Media Queries */
@media (max-width: 600px) {
  .site-header {
    padding: 0.75rem 1rem 0.25rem;
  }

  .main-content {
    padding: 0.25rem 1rem 2rem;
    gap: 1rem;
  }

  .stage-container {
    height: 310px;
  }

  .bench-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .status-callout {
    justify-content: center;
  }

  .control-buttons {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .bench-tip {
    flex-direction: column;
    gap: 0.25rem;
  }

  .metrics-grid {
    grid-template-columns: 1fr;
  }

  .scorecard-actions {
    flex-direction: column;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* 404 Error Page */
.error-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1.5rem;
  text-align: center;
}

.error-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 480px;
}

.error-code {
  font-family: var(--font-mono);
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.error-title {
  font-size: 1.75rem;
  font-weight: 800;
  margin-bottom: 0.75rem;
  color: var(--text-primary);
}

.error-desc {
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
}

