/*
 * Theme: colors, base styles, and shared components.
 * Adapted from https://context-lab.com/llm-course/ demo styling
 * (Dartmouth green / river blue / bonfire orange palette). Dark by default,
 * toggled to light via [data-theme="light"] on <html>.
 */

:root {
  /* Primary Colors - Dartmouth Brand */
  --primary-color: #00693e;
  --primary-dark: #005a34;
  --primary-light: #1a8a5a;
  --secondary-color: #267aba;
  --accent-color: #ffa00f;

  /* Tertiary Colors - for plots, charts, emphasis (Dartmouth palette) */
  --color-river-blue: #267aba;
  --color-river-navy: #003c73;
  --color-spring-green: #c4dd88;
  --color-rich-spring: #a5d75f;
  --color-summer-yellow: #f5dc69;
  --color-bonfire-orange: #ffa00f;
  --color-bonfire-red: #9d162e;
  --color-tuck-orange: #d94415;
  --color-violet: #8a6996;
  --color-autumn-brown: #643c20;
  --color-forest-green: #12312b;

  /* Backgrounds */
  --bg-color: #0f172a;
  --bg-secondary: #1e293b;
  --surface-color: #1e293b;
  --surface-hover: #334155;

  /* Text */
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  /* Borders */
  --border-color: #334155;
  --divider-color: #475569;

  /* Status */
  --success-color: #10b981;
  --warning-color: #f59e0b;
  --error-color: #ef4444;
  --info-color: #3b82f6;

  /* Gradients */
  --gradient-primary: linear-gradient(135deg, #00693e 0%, #005a34 100%);
  --gradient-secondary: linear-gradient(135deg, #267aba 0%, #003c73 100%);
  --gradient-accent: linear-gradient(135deg, #00693e 0%, #267aba 100%);

  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.2);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.3);
  --shadow-glow: 0 0 20px rgba(0, 105, 62, 0.4);

  /* Spacing */
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
  --spacing-xl: 2rem;
  --spacing-2xl: 3rem;

  /* Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --radius-full: 9999px;

  /* Transitions */
  --transition-fast: 150ms ease;
  --transition-base: 300ms ease;
  --transition-slow: 500ms ease;

  /* Z-index */
  --z-fixed: 1030;

  /* Robot-arm metallic shell (brushed steel; darker in dark mode for
     contrast against the dark canvas background, lighter in light mode). */
  --skin: #6b7480;
  --skin-shade: #3d434b;
  --skin-highlight: #dfe4e9;
  --skin-outline: #1c2024;
  --table-color: #8a6a48;
  --ball-color: #d94415;

  /* Accessible variant of --primary-color for text/borders on dark surfaces
     (the raw brand green fails WCAG AA at small text sizes on dark backgrounds). */
  --primary-accessible: #2bb37a;

  /* Passband/filter shading on the control-tab plots, themed instead of a hardcoded rgba(). */
  --passband-fill: rgba(43, 179, 122, 0.18);

  /* Hero title gradient: lightened relative to --gradient-primary so the
     text-clipped title stays readable against the dark hero background
     (the raw brand green reads fine as a button fill, but low-contrast as
     text on dark). The light theme below restores the course's own colors. */
  --hero-gradient: linear-gradient(135deg, #1a8a5a 0%, #2bb37a 100%);
}

/* Light theme overrides */
:root[data-theme='light'] {
  --bg-color: #ffffff;
  --bg-secondary: #f8fafc;
  --surface-color: #f8fafc;
  --surface-hover: #e2e8f0;
  --text-primary: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border-color: #e2e8f0;
  --divider-color: #cbd5e1;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
  --primary-accessible: #00693e;
  --passband-fill: rgba(0, 105, 62, 0.13);
  --hero-gradient: var(--gradient-primary);

  /* Metallic shell, lightened for contrast against a white canvas background. */
  --skin: #9aa3ad;
  --skin-shade: #6b7480;
  --skin-highlight: #f4f6f8;
  --skin-outline: #2b3138;
}

/* ============ Base ============ */

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

html {
  scroll-behavior: smooth;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background-color: var(--bg-color);
  color: var(--text-primary);
  line-height: 1.6;
  transition: background-color var(--transition-base), color var(--transition-base);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: 700;
  line-height: 1.2;
  color: var(--text-primary);
}

h2 { font-size: clamp(1.5rem, 3vw, 2.25rem); margin-bottom: var(--spacing-md); }
h3 { font-size: clamp(1.15rem, 2.2vw, 1.5rem); margin-bottom: var(--spacing-sm); }

p { color: var(--text-secondary); }

a { color: var(--secondary-color); text-decoration: none; transition: color var(--transition-fast); }
a:hover { color: var(--primary-light); }

code {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  background: var(--surface-hover);
  padding: 0.125rem 0.375rem;
  border-radius: var(--radius-sm);
  font-size: 0.875em;
}

/* ============ Nav ============ */

.demo-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: var(--spacing-md) var(--spacing-xl);
  background: var(--bg-color);
  border-bottom: 1px solid var(--border-color);
  z-index: var(--z-fixed);
  backdrop-filter: blur(10px);
}

.demo-nav .back-link {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  color: var(--text-secondary);
  font-weight: 500;
}
.demo-nav .back-link:hover { color: var(--primary-color); }

.demo-nav .theme-toggle {
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-full);
  width: 40px;
  height: 40px;
  font-size: 1.15rem;
  cursor: pointer;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
}
.demo-nav .theme-toggle:hover {
  background: var(--surface-hover);
  border-color: var(--primary-color);
}

body:has(.demo-nav) { padding-top: 70px; }

/* ============ Hero ============ */

.hero-header {
  padding: 1.1rem 0 0.85rem;
  text-align: center;
  background: linear-gradient(180deg, var(--surface-color) 0%, var(--bg-color) 100%);
}

.hero-title {
  font-size: clamp(1.5rem, 3.2vw, 2.15rem);
  font-weight: 800;
  line-height: 1.2;
  margin-bottom: 0.4rem;
  background: var(--hero-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.hero-subtitle {
  font-size: clamp(0.85rem, 1.5vw, 1rem);
  color: var(--text-secondary);
  max-width: 700px;
  margin: 0 auto;
  padding: 0 var(--spacing-md);
}

/* ============ Buttons ============ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-sm);
  padding: 0.65rem 1.25rem;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-base);
  white-space: nowrap;
  font-family: inherit;
}

.btn-primary {
  background: var(--gradient-primary);
  color: white;
  box-shadow: var(--shadow-md);
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

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

.btn-outline {
  background: transparent;
  color: var(--primary-accessible);
  border: 2px solid var(--primary-accessible);
}
.btn-outline:hover { background: var(--primary-color); color: white; }

.btn-icon {
  padding: 0.4rem 0.6rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
}
.btn-icon:hover { color: var(--error-color); background: var(--surface-hover); }

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

input:disabled { opacity: 0.45; cursor: not-allowed; }

/* ============ Cards / panels ============ */

.card {
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: var(--spacing-lg);
}

.card-header {
  margin-bottom: var(--spacing-md);
  padding-bottom: var(--spacing-md);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  row-gap: var(--spacing-sm);
  gap: var(--spacing-md);
}

.card-title { font-size: 1.1rem; font-weight: 700; }

.caption {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-top: var(--spacing-sm);
}

/* ============ Controls ============ */

.controls-panel {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-lg);
  padding: var(--spacing-md) var(--spacing-lg);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
}

.control-group {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.control-group label {
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 0.85rem;
  white-space: nowrap;
}

.control-group select,
.control-group input[type='text'],
.control-group input[type='number'] {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 0.9rem;
  font-family: inherit;
  background: var(--surface-color);
  color: var(--text-primary);
}

/* A <select> with a long option label (e.g. the Preset dropdown's "Human arm
   — Fourier spin (links = components)") otherwise sizes to fit that text and
   overflows a narrow mobile viewport; let it shrink and ellipsize instead. */
.control-group select {
  min-width: 0;
  max-width: 100%;
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.control-group:has(select) { min-width: 0; }

.control-group select:focus,
.control-group input:focus {
  outline: none;
  border-color: var(--primary-color);
}

input[type='range'] {
  -webkit-appearance: none;
  appearance: none;
  width: 120px;
  height: 6px;
  background: var(--surface-hover);
  border-radius: var(--radius-full);
  outline: none;
}
input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: var(--primary-color);
  border-radius: 50%;
  cursor: pointer;
  transition: all var(--transition-fast);
}
input[type='range']::-webkit-slider-thumb:hover { transform: scale(1.2); box-shadow: var(--shadow-glow); }
input[type='range']::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--primary-color);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}

input[type='checkbox'] { accent-color: var(--primary-color); width: 16px; height: 16px; cursor: pointer; }

/* ============ Loading / errors ============ */

.spinner {
  display: inline-block;
  width: 32px;
  height: 32px;
  border: 4px solid var(--surface-hover);
  border-top-color: var(--primary-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.alert {
  padding: var(--spacing-md) var(--spacing-lg);
  border-radius: var(--radius-md);
  border-left: 4px solid;
}
.alert-error {
  background: rgba(239, 68, 68, 0.1);
  border-left-color: var(--error-color);
  color: var(--error-color);
}

/* KaTeX should inherit surrounding text color */
.katex { color: inherit; }

/* ============ Accessibility: visible focus ============ */

a:focus-visible,
button:focus-visible,
.tab-btn:focus-visible,
input:focus-visible,
select:focus-visible,
canvas:focus-visible {
  outline: 2px solid var(--secondary-color);
  outline-offset: 2px;
}
