/**
 * Theme System - Dark Mode & Design System
 * Defines CSS variables for light and dark themes
 */

:root {
  /* Light Theme Colors */
  --bg-primary: #ffffff;
  --bg-secondary: #f5f5f5;
  --bg-tertiary: #e8e8e8;
  --bg-elevated: #ffffff;

  --text-primary: #1a1a1a;
  --text-secondary: #666666;
  --text-tertiary: #767676;
  --text-inverse: #ffffff;

  --border-light: #e0e0e0;
  --border-medium: #cccccc;
  --border-dark: #999999;

  /* Muffle rehearsal mode: text-shadow radius that stands in for the blurred
     glyphs (character-highlight-rehearsal.css). A length, so it holds in both
     themes; the colour comes from --text-secondary. */
  --muffle-blur: 0.9em;

  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.12);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.16);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.20);

  --accent-primary: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: #dbeafe;

  --success: #10b981;
  --warning: #f59e0b;
  --error: #ef4444;
  --info: #3b82f6;

  /* Diff colors (compare page): blue = added, red = deleted */
  --diff-added-text: #1d4ed8;
  --diff-added-bg: rgba(59, 130, 246, 0.12);
  --diff-removed-text: #dc2626;
  --diff-removed-bg: rgba(239, 68, 68, 0.1);

  /* Analysis Colors (maintain existing) */
  --analysis-bg: #fff9e6;
  --analysis-border: #ffcc00;
  --objective-bg: #e6f3ff;
  --objective-border: #0066cc;
  --tactics-bg: #ffe6e6;
  --tactics-border: #cc0000;
  --body-language-bg: #e6ffe6;
  --body-language-border: #00cc00;
  --note-bg: #f0f0f0;
  --note-border: #999999;
  --beat-bg: #333333;
  --beat-text: #ffffff;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;

  /* Border Radius — two values only (P3.2): sm for surfaces, full for pills.
     md/lg survive as aliases because stored cloud-scene HTML references them
     in inline styles; new code must not use them. */
  --radius-sm: 4px;
  --radius-md: var(--radius-sm);
  --radius-lg: var(--radius-sm);
  --radius-full: 9999px;

  /* Panels — shared base width for all slide-in sidebars (P2.2):
     character options, scene notes, line notes. Responsive overrides
     (280px @768px, 100% @480px) stay in each component's own file. */
  --panel-width: 380px;

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

  /* Typography */
  --font-body: 'Courier New', monospace;
  --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
  --font-mono: 'Courier New', 'Monaco', monospace;
}

/* Dark Theme */
[data-theme="dark"] {
  --bg-primary: #1a1a1a;
  --bg-secondary: #2d2d2d;
  --bg-tertiary: #3d3d3d;
  --bg-elevated: #252525;

  --text-primary: #e8e8e8;
  --text-secondary: #b8b8b8;
  --text-tertiary: #949494;
  --text-inverse: #1a1a1a;

  --border-light: #3d3d3d;
  --border-medium: #4d4d4d;
  --border-dark: #666666;

  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.6);

  --accent-primary: #60a5fa;
  --accent-hover: #3b82f6;
  --accent-light: #1e3a5f;

  /* Analysis Colors (adjusted for dark mode) */
  --analysis-bg: #3d3520;
  --analysis-border: #b8930d;
  --objective-bg: #1e3a5f;
  --objective-border: #3b82f6;
  --tactics-bg: #4d2626;
  --tactics-border: #ef4444;
  --body-language-bg: #264d26;
  --body-language-border: #22c55e;
  --note-bg: #2d2d2d;
  --note-border: #666666;
  --beat-bg: #0f0f0f;
  --beat-text: #e8e8e8;

  /* Diff status colors (compare page) */
  --diff-added-text: #7fb4ff;
  --diff-added-bg: rgba(96, 165, 250, 0.16);
  --diff-removed-text: #f87171;
  --diff-removed-bg: rgba(239, 68, 68, 0.14);
}

/* Theme-switch fade: theme-toggle.ts sets .theme-transitioning on <html>
   for 400ms around a theme flip, so the wildcard only exists during the
   switch — no permanent transition tracking on every element (P3.1) */
.theme-transitioning * {
  transition: background-color var(--transition-base),
              color var(--transition-base),
              border-color var(--transition-base),
              box-shadow var(--transition-base);
}

/* Interactive elements keep color transitions permanently */
button, a, .analysis-card {
  transition: background-color var(--transition-base),
              border-color var(--transition-base),
              color var(--transition-base),
              box-shadow var(--transition-base);
}

/* iOS Safari text autosizing inflates glyphs past the computed font-size, which
   desyncs the caret from the text in form fields */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Base body styling */
body {
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  font-family: var(--font-body);
}

/* Theme Toggle Button */
.theme-toggle {
  background-color: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-medium);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  white-space: nowrap;
  transition: background-color var(--transition-base), border-color var(--transition-base), color var(--transition-base);
}

.theme-toggle:hover {
  background-color: var(--bg-tertiary);
}

/* Container improvements */
.container {
  background-color: var(--bg-primary);
  box-shadow: var(--shadow-lg);
  border-radius: var(--radius-sm);
  transition: box-shadow var(--transition-base);
}

/* Card improvements */
.analysis-card {
  background-color: var(--bg-elevated);
  border: 1px solid var(--border-light);
  box-shadow: var(--shadow-sm);
}

.analysis-card:hover {
  border-color: var(--border-medium);
}

/* Nav header theming */
.nav-header {
  background-color: var(--bg-primary) !important;
  border-bottom: 1px solid var(--border-light);
  box-shadow: var(--shadow-sm) !important;
}

[data-theme="dark"] .nav-header {
  background-color: var(--bg-elevated) !important;
}

/* Button improvements */
button {
  cursor: pointer;
  border: none;
}

/* Links */
a {
  color: var(--accent-primary);
  transition: color var(--transition-fast);
}

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

/* Analysis sections */
.analysis-section {
  background-color: var(--analysis-bg);
  border-left: 4px solid var(--analysis-border);
  border-radius: var(--radius-sm);
}

.objective {
  background-color: var(--objective-bg);
  border-left: 4px solid var(--objective-border);
  border-radius: var(--radius-sm);
}

.tactics {
  background-color: var(--tactics-bg);
  border-left: 4px solid var(--tactics-border);
  border-radius: var(--radius-sm);
}

.body-language {
  background-color: var(--body-language-bg);
  border-left: 4px solid var(--body-language-border);
  border-radius: var(--radius-sm);
}

.note {
  background-color: var(--note-bg);
  border-left: 4px solid var(--note-border);
  border-radius: var(--radius-sm);
}

.beat-marker {
  background-color: var(--beat-bg);
  color: var(--beat-text);
  border-radius: var(--radius-sm);
}

/* Gray heading bars inside analysis blocks (scoped to analysis pages so
   index/login <h3>s stay square) */
body[data-analysis-id] h3 {
  border-radius: var(--radius-sm);
}

/* Line actions and physical notes */
.line-action {
  color: var(--tactics-border);
}

.physical-note {
  color: var(--objective-border);
}

/* Headings */
h1, h2, h3, h4, h5, h6 {
  color: var(--text-primary);
  font-family: var(--font-ui);
}

/* Loading spinner */
.spinner {
  border: 3px solid var(--border-light);
  border-top-color: var(--accent-primary);
}

/* Scrollbar styling (webkit browsers) */
::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}

::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
  background: var(--border-medium);
  border-radius: var(--radius-full);
  border: 2px solid var(--bg-secondary);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--border-dark);
}

/* Selection color */
::selection {
  background-color: var(--accent-light);
  color: var(--text-primary);
}

/* Focus styles */
button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent-primary);
  outline-offset: 2px;
}

/* Touch: no double-tap zoom anywhere (pinch-zoom stays - a11y). Lets the
   double-tap bookmark gesture (line-bookmark.ts) land and stops a stray
   double-tap on a nav button from zooming the page. Descendant
   touch-action: none rules (cover handle, resize grips) still win. */
@media (pointer: coarse) {
  html, body {
    touch-action: manipulation;
  }
}

/* Print mode - force light theme */
@media print {
  html, body {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    background-color: white !important;
    color: black !important;
  }

  * {
    border-color: #ccc !important;
  }

  .theme-toggle,
  .nav-header {
    display: none !important;
  }
}
