/**
 * =====================================================
 * THEME SYSTEM - Light & Dark Mode
 * =====================================================
 * 
 * This file contains all color variables for both light
 * and dark themes. The light theme is the default, and 
 * dark theme is applied via the `.dark` class on the 
 * root HTML element.
 * 
 * Design Philosophy:
 * - Dark mode is a direct visual adaptation of light theme
 * - Same layout, spacing, shadows, and structure
 * - Only colors are changed
 * - Maintains visual hierarchy and depth
 * - Cards remain distinct from background
 * - Shadows are adjusted but not removed
 */

/* ========== LIGHT THEME (DEFAULT) ========== */

:root {
  --pm-font-ui: "Plus Jakarta Sans", Inter, "Segoe UI", Roboto, system-ui, -apple-system, sans-serif;
  --pm-font-display: "Plus Jakarta Sans", Inter, "Segoe UI", Roboto, system-ui, sans-serif;

  /* Adaptive Accent Tokens (Light defaults) */
  --accent-primary: var(--accent-primary-light, #00322d);
  --accent-hover: var(--accent-hover-light, #002b26);
  --accent-soft: var(--accent-soft-light, #b1eee4);
  --accent-border: var(--accent-border-light, #244742);
  --accent-card: var(--accent-card-light, #eef8f5);
  --accent-chart: var(--accent-chart-light, #00322d);
  --accent-progress: var(--accent-progress-light, #2e7a6f);
  --accent-contrast: var(--accent-contrast-light, #ffffff);
  --accent-primary-rgb: var(--accent-primary-rgb-light, 0, 50, 45);
  --accent-contrast-rgb-space: var(--accent-contrast-rgb-space-light, 255 255 255);

  /* Backward-compatible accent aliases */
  --color-accent: var(--accent-primary);
  --color-accent-hover: var(--accent-hover);
  --color-accent-soft: var(--accent-soft);
  --color-accent-contrast: var(--accent-contrast);
  --color-accent-rgb: var(--accent-primary-rgb);
  --color-accent-rgb-space: var(--accent-primary-rgb-space-light, 0 50 45);

  /* Primary colors - theme mapped to adaptive accent tokens */
  --theme-primary: var(--accent-primary);
  --theme-primary-rgb: var(--accent-primary-rgb);
  --theme-primary-hover: var(--accent-hover);
  --theme-primary-soft: var(--accent-soft);
  --theme-primary-strong: var(--color-accent-strong, #004b44);
  --on-primary: var(--accent-contrast);

  /* Background & Surface Colors */
  --theme-bg-page: #f8faf8;
  --theme-bg-surface: #f8faf8;
  --theme-bg-bright: #f8faf8;

  /* Card & Container Backgrounds */
  --theme-card-bg: #ffffff;
  --theme-container-lowest: #ffffff;
  --theme-container-low: #f2f4f2;
  --theme-container: #eceeec;
  --theme-container-high: #e7e9e7;
  --theme-container-highest: #e1e3e1;
  --theme-container-variant: #e1e3e1;
  --theme-container-dim: #d8dad9;

  /* Text Colors */
  --theme-text-primary: #191c1b;
  --theme-text-secondary: #3f4947;
  --theme-text-tertiary: #3f4947;
  --theme-text-disabled: rgba(25, 28, 27, 0.38);

  /* Border Colors */
  --theme-border: #bfc9c6;
  --theme-border-variant: #bfc9c6;
  --theme-border-light: rgba(0, 0, 0, 0.08);
  --theme-border-subtle: rgba(0, 0, 0, 0.05);

  /* Semantic Colors */
  --theme-error: #ba1a1a;
  --theme-error-bg: #ffdad6;
  --theme-error-dark: #93000a;
  --theme-success: var(--theme-primary);
  --theme-success-bg: var(--accent-card);
  --theme-success-dark: var(--theme-primary-strong);
  --theme-warning: #f59e0b;
  --theme-warning-bg: #fff3cd;
  --theme-info: #3b82f6;
  --theme-info-bg: #dbeafe;

  /* Shadow Colors */
  --theme-shadow-sm: rgba(25, 28, 27, 0.04);
  --theme-shadow-md: rgba(25, 28, 27, 0.06);
  --theme-shadow-lg: rgba(25, 28, 27, 0.08);
  --theme-shadow-xl: rgba(25, 28, 27, 0.12);

  /* Overlay & Backdrop */
  --theme-overlay: rgba(25, 28, 27, 0.32);
  --theme-backdrop: rgba(25, 28, 27, 0.16);

  /* Input & Form Elements */
  --theme-input-bg: #ffffff;
  --theme-input-border: #bfc9c6;
  --theme-input-text: #191c1b;
  --theme-input-placeholder: #3f4947;
  --theme-input-focus-ring: rgba(var(--theme-primary-rgb), 0.12);

  /* Skeleton & Loading */
  --theme-skeleton-bg: #eceeec;
  --theme-skeleton-shimmer: rgba(255, 255, 255, 0.42);

  /* Scrollbar */
  --theme-scrollbar-track: transparent;
  --theme-scrollbar-thumb: rgba(0, 0, 0, 0.05);
  --theme-scrollbar-thumb-hover: rgba(0, 0, 0, 0.1);

  /* ========== SEMANTIC CARD COLORS ========== */
  /* Cash & Box */
  --color-cash-green: #10b981;
  --color-cash-green-light: rgba(16, 185, 129, 0.12);
  
  /* Card Payments */
  --color-card-blue: #3b82f6;
  --color-card-blue-light: rgba(59, 130, 246, 0.12);
  
  /* Expenses */
  --color-expense-red: #ef4444;
  --color-expense-red-light: rgba(239, 68, 68, 0.12);
  
  /* Success/Green Button */
  --color-success-button: #10b981;
  --color-success-button-hover: #059669;

  /* ========== MAP EXISTING CSS VARIABLES ========== */
  /* These are used by app.css and other stylesheets */

  /* Primary */
  --primary: var(--theme-primary);
  --primary-rgb: var(--theme-primary-rgb);
  
  /* Surfaces (from Material Design) */
  --surface: var(--theme-bg-surface);
  --surface-bright: var(--theme-bg-bright);
  --surface-container-lowest: var(--theme-container-lowest);
  --surface-container-low: var(--theme-container-low);
  --surface-container: var(--theme-container);
  --surface-container-high: var(--theme-container-high);
  --surface-container-highest: var(--theme-container-highest);
  --surface-variant: var(--theme-container-variant);
  --surface-dim: var(--theme-container-dim);

  /* Text/On Surface */
  --on-surface: var(--theme-text-primary);
  --on-surface-variant: var(--theme-text-secondary);
  --on-background: var(--theme-text-primary);

  /* Outline/Borders */
  --outline: var(--theme-border);
  --outline-variant: var(--theme-border-variant);

  /* Colors for accents */
  --color-accent-rgb: var(--theme-primary-rgb);
  
  /* ========== TAILWIND RUNTIME RGB TOKENS (LIGHT) ========== */
  --pm-background-rgb: 248 250 248;
  --pm-surface-rgb: 248 250 248;
  --pm-surface-bright-rgb: 248 250 248;
  --pm-surface-container-lowest-rgb: 255 255 255;
  --pm-surface-container-low-rgb: 242 244 242;
  --pm-surface-container-rgb: 236 238 236;
  --pm-surface-container-high-rgb: 231 233 231;
  --pm-surface-container-highest-rgb: 225 227 225;
  --pm-surface-variant-rgb: 225 227 225;
  --pm-surface-dim-rgb: 216 218 217;
  --pm-on-surface-rgb: 25 28 27;
  --pm-on-surface-variant-rgb: 63 73 71;
  --pm-on-background-rgb: 25 28 27;
  --pm-primary-rgb: var(--color-accent-rgb-space, 0 50 45);
  --pm-primary-container-rgb: 0 75 68;
  --pm-primary-fixed-rgb: 177 238 228;
  --pm-primary-fixed-dim-rgb: 149 210 200;
  --pm-on-primary-rgb: var(--accent-contrast-rgb-space-light, 255 255 255);
  --pm-on-primary-container-rgb: 255 255 255;
  --pm-on-primary-fixed-rgb: 0 32 28;
  --pm-on-primary-fixed-variant-rgb: 9 80 73;
  --pm-inverse-primary-rgb: 149 210 200;
  --pm-secondary-rgb: 93 94 97;
  --pm-on-secondary-rgb: 255 255 255;
  --pm-secondary-container-rgb: 226 226 229;
  --pm-secondary-fixed-rgb: 226 226 229;
  --pm-secondary-fixed-dim-rgb: 198 198 201;
  --pm-on-secondary-container-rgb: 99 100 103;
  --pm-on-secondary-fixed-rgb: 26 28 30;
  --pm-on-secondary-fixed-variant-rgb: 69 71 73;
  --pm-tertiary-rgb: 74 32 14;
  --pm-on-tertiary-rgb: 255 255 255;
  --pm-tertiary-container-rgb: 101 53 34;
  --pm-on-tertiary-container-rgb: 226 158 133;
  --pm-tertiary-fixed-rgb: 255 219 206;
  --pm-tertiary-fixed-dim-rgb: 253 182 156;
  --pm-on-tertiary-fixed-rgb: 53 16 2;
  --pm-on-tertiary-fixed-variant-rgb: 107 58 38;
  --pm-outline-rgb: 112 121 119;
  --pm-outline-variant-rgb: 191 201 198;
  --pm-error-rgb: 186 26 26;
  --pm-on-error-rgb: 255 255 255;
  --pm-error-container-rgb: 255 218 214;
  --pm-on-error-container-rgb: 147 0 10;
  --pm-inverse-surface-rgb: 46 49 48;
  --pm-inverse-on-surface-rgb: 239 241 239;
  --pm-surface-tint-rgb: var(--color-accent-rgb-space, 0 50 45);
}

/* ========== DARK THEME ========== */

html.dark,
.dark {
  /* Adaptive Accent Tokens (Dark mode) */
  --accent-primary: var(--accent-primary-dark, #95d2c8);
  --accent-hover: var(--accent-hover-dark, #a6ddd4);
  --accent-soft: var(--accent-soft-dark, #17312c);
  --accent-border: var(--accent-border-dark, #7abab0);
  --accent-card: var(--accent-card-dark, #203530);
  --accent-chart: var(--accent-chart-dark, #9fdcd1);
  --accent-progress: var(--accent-progress-dark, #b3e7de);
  --accent-contrast: var(--accent-contrast-dark, #0b1916);
  --accent-primary-rgb: var(--accent-primary-rgb-dark, 149, 210, 200);
  --accent-contrast-rgb-space: var(--accent-contrast-rgb-space-dark, 10 25 22);

  /* Backward-compatible accent aliases */
  --color-accent: var(--accent-primary);
  --color-accent-hover: var(--accent-hover);
  --color-accent-soft: var(--accent-soft);
  --color-accent-contrast: var(--accent-contrast);
  --color-accent-rgb: var(--accent-primary-rgb);
  --color-accent-rgb-space: var(--accent-primary-rgb-space-dark, 149 210 200);

  /* Primary colors mapped to adaptive tokens */
  --theme-primary: var(--accent-primary);
  --theme-primary-rgb: var(--accent-primary-rgb);
  --theme-primary-hover: var(--accent-hover);
  --theme-primary-soft: var(--accent-soft);
  --theme-primary-strong: var(--accent-border);
  --on-primary: var(--accent-contrast);

  /* Background & Surface Colors */
  --theme-bg-page: #0f1410;
  --theme-bg-surface: #0f1410;
  --theme-bg-bright: #1a1f1d;

  /* Card & Container Backgrounds */
  --theme-card-bg: #1a1f1d;
  --theme-container-lowest: #0f1410;
  --theme-container-low: #141a18;
  --theme-container: #1a1f1d;
  --theme-container-high: #252c2a;
  --theme-container-highest: #2e3530;
  --theme-container-variant: #2e3530;
  --theme-container-dim: #0f1410;

  /* Text Colors */
  --theme-text-primary: #e8f0ee;
  --theme-text-secondary: #b3bdb9;
  --theme-text-tertiary: #b3bdb9;
  --theme-text-disabled: rgba(232, 240, 238, 0.38);

  /* Border Colors */
  --theme-border: #3f4947;
  --theme-border-variant: #3f4947;
  --theme-border-light: rgba(255, 255, 255, 0.08);
  --theme-border-subtle: rgba(255, 255, 255, 0.04);

  /* Semantic Colors */
  --theme-error: #f8b4b0;
  --theme-error-bg: rgba(186, 26, 26, 0.2);
  --theme-error-dark: #ffdad6;
  --theme-success: var(--theme-primary);
  --theme-success-bg: var(--accent-card);
  --theme-success-dark: var(--theme-primary-strong);
  --theme-warning: #ffc25c;
  --theme-warning-bg: rgba(245, 158, 11, 0.15);
  --theme-info: #64b4f6;
  --theme-info-bg: rgba(59, 130, 246, 0.15);

  /* Shadow Colors */
  --theme-shadow-sm: rgba(0, 0, 0, 0.2);
  --theme-shadow-md: rgba(0, 0, 0, 0.3);
  --theme-shadow-lg: rgba(0, 0, 0, 0.4);
  --theme-shadow-xl: rgba(0, 0, 0, 0.5);

  /* Overlay & Backdrop */
  --theme-overlay: rgba(0, 0, 0, 0.64);
  --theme-backdrop: rgba(0, 0, 0, 0.32);

  /* Input & Form Elements */
  --theme-input-bg: #1a1f1d;
  --theme-input-border: #3f4947;
  --theme-input-text: #e8f0ee;
  --theme-input-placeholder: #b3bdb9;
  --theme-input-focus-ring: rgba(var(--theme-primary-rgb), 0.24);

  /* Skeleton & Loading */
  --theme-skeleton-bg: #252c2a;
  --theme-skeleton-shimmer: rgba(149, 210, 200, 0.12);

  /* Scrollbar */
  --theme-scrollbar-track: transparent;
  --theme-scrollbar-thumb: rgba(255, 255, 255, 0.08);
  --theme-scrollbar-thumb-hover: rgba(255, 255, 255, 0.12);

  /* ========== SEMANTIC CARD COLORS (DARK) ========== */
  /* Cash & Box */
  --color-cash-green: #34d399;
  --color-cash-green-light: rgba(52, 211, 153, 0.18);
  
  /* Card Payments */
  --color-card-blue: #60a5fa;
  --color-card-blue-light: rgba(96, 165, 250, 0.18);
  
  /* Expenses */
  --color-expense-red: #f87171;
  --color-expense-red-light: rgba(248, 113, 113, 0.18);
  
  /* Success/Green Button */
  --color-success-button: #34d399;
  --color-success-button-hover: #10b981;

  /* ========== MAP EXISTING CSS VARIABLES ========== */
  /* These are used by app.css and other stylesheets */

  /* Primary */
  --primary: var(--theme-primary);
  --primary-rgb: 149, 210, 200;
  
  /* Surfaces (from Material Design) */
  --surface: var(--theme-bg-surface);
  --surface-bright: var(--theme-bg-bright);
  --surface-container-lowest: var(--theme-container-lowest);
  --surface-container-low: var(--theme-container-low);
  --surface-container: var(--theme-container);
  --surface-container-high: var(--theme-container-high);
  --surface-container-highest: var(--theme-container-highest);
  --surface-variant: var(--theme-container-variant);
  --surface-dim: var(--theme-container-dim);

  /* Text/On Surface */
  --on-surface: var(--theme-text-primary);
  --on-surface-variant: var(--theme-text-secondary);
  --on-background: var(--theme-text-primary);

  /* Outline/Borders */
  --outline: var(--theme-border);
  --outline-variant: var(--theme-border-variant);

  /* Colors for accents */
  --color-accent-rgb: var(--theme-primary-rgb);
  
  /* ========== TAILWIND RUNTIME RGB TOKENS (DARK) ========== */
  --pm-background-rgb: 15 20 16;
  --pm-surface-rgb: 15 20 16;
  --pm-surface-bright-rgb: 26 31 29;
  --pm-surface-container-lowest-rgb: 15 20 16;
  --pm-surface-container-low-rgb: 20 26 24;
  --pm-surface-container-rgb: 26 31 29;
  --pm-surface-container-high-rgb: 37 44 42;
  --pm-surface-container-highest-rgb: 46 53 48;
  --pm-surface-variant-rgb: 46 53 48;
  --pm-surface-dim-rgb: 15 20 16;
  --pm-on-surface-rgb: 232 240 238;
  --pm-on-surface-variant-rgb: 179 189 185;
  --pm-on-background-rgb: 232 240 238;
  --pm-primary-rgb: var(--color-accent-rgb-space, 149 210 200);
  --pm-primary-container-rgb: 0 75 68;
  --pm-primary-fixed-rgb: 8 40 35;
  --pm-primary-fixed-dim-rgb: 149 210 200;
  --pm-on-primary-rgb: var(--accent-contrast-rgb-space-dark, 10 25 22);
  --pm-on-primary-container-rgb: 220 255 243;
  --pm-on-primary-fixed-rgb: 220 255 243;
  --pm-on-primary-fixed-variant-rgb: 149 210 200;
  --pm-inverse-primary-rgb: 149 210 200;
  --pm-secondary-rgb: 179 189 185;
  --pm-on-secondary-rgb: 15 20 16;
  --pm-secondary-container-rgb: 37 44 42;
  --pm-secondary-fixed-rgb: 37 44 42;
  --pm-secondary-fixed-dim-rgb: 63 73 71;
  --pm-on-secondary-container-rgb: 226 226 229;
  --pm-on-secondary-fixed-rgb: 226 226 229;
  --pm-on-secondary-fixed-variant-rgb: 198 198 201;
  --pm-tertiary-rgb: 253 182 156;
  --pm-on-tertiary-rgb: 53 16 2;
  --pm-tertiary-container-rgb: 107 58 38;
  --pm-on-tertiary-container-rgb: 255 219 206;
  --pm-tertiary-fixed-rgb: 107 58 38;
  --pm-tertiary-fixed-dim-rgb: 253 182 156;
  --pm-on-tertiary-fixed-rgb: 255 219 206;
  --pm-on-tertiary-fixed-variant-rgb: 253 182 156;
  --pm-outline-rgb: 97 108 105;
  --pm-outline-variant-rgb: 63 73 71;
  --pm-error-rgb: 248 180 176;
  --pm-on-error-rgb: 53 16 2;
  --pm-error-container-rgb: 74 19 19;
  --pm-on-error-container-rgb: 255 218 214;
  --pm-inverse-surface-rgb: 232 240 238;
  --pm-inverse-on-surface-rgb: 15 20 16;
  --pm-surface-tint-rgb: 149 210 200;
}

/* ========== APPLY THEME VARIABLES ========== */

/* Body background and text */
body {
  background-color: var(--theme-bg-page);
  color: var(--theme-text-primary);
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Override Tailwind color usage */
html {
  background-color: var(--theme-bg-page);
  color: var(--theme-text-primary);
}

/* ========== SCROLLBAR STYLING ========== */

/* WebKit browsers (Chrome, Safari, Edge) */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--theme-scrollbar-track);
}

::-webkit-scrollbar-thumb {
  background: var(--theme-scrollbar-thumb);
  border-radius: 4px;
  transition: background 0.2s ease;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--theme-scrollbar-thumb-hover);
}

/* Firefox */
* {
  scrollbar-color: var(--theme-scrollbar-thumb) var(--theme-scrollbar-track);
  scrollbar-width: thin;
}

/* ========== INPUT ELEMENTS ========== */

input,
textarea,
select {
  background-color: var(--theme-input-bg);
  color: var(--theme-input-text);
  border-color: var(--theme-input-border);
  transition: background-color 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

input::placeholder,
textarea::placeholder {
  color: var(--theme-input-placeholder);
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--theme-primary);
  box-shadow: 0 0 0 3px var(--theme-input-focus-ring);
  outline: none;
}

/* ========== LINK COLORS ========== */

a {
  color: var(--theme-primary);
  transition: color 0.2s ease;
}

a:hover {
  color: var(--theme-primary-hover);
}

/* ========== SELECTION ========== */

::selection {
  background-color: var(--theme-primary);
  color: #ffffff;
}

/* ========== FOCUS VISIBLE ========== */

:focus-visible {
  outline: 2px solid var(--theme-primary);
  outline-offset: 2px;
}

/* ========== BUTTONS ========== */

button {
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

/* ========== CARDS & CONTAINERS ========== */

.card,
.modal,
[role="dialog"] {
  background-color: var(--theme-card-bg);
  color: var(--theme-text-primary);
  border-color: var(--theme-border);
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

/* ========== TABLES ========== */

table {
  background-color: var(--theme-card-bg);
  color: var(--theme-text-primary);
  border-color: var(--theme-border-light);
}

thead {
  background-color: var(--theme-container);
  color: var(--theme-text-primary);
}

tbody tr {
  border-color: var(--theme-border-light);
  transition: background-color 0.15s ease;
}

tbody tr:hover {
  background-color: var(--theme-container-low);
}

td,
th {
  border-color: var(--theme-border-light);
  color: var(--theme-text-primary);
}

/* ========== BADGES & LABELS ========== */

.badge {
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* ========== MOTION SAFETY ========== */
/* Respect user's motion preferences and avoid global transition lag. */
@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
