/**
 * Light/Dark Theme System
 * Provides comprehensive theming support with light mode as default
 */

/* ===== LIGHT THEME (DEFAULT) ===== */
:root {
    /* Theme Colors - Light Mode */
    --theme-bg-primary: #ffffff;
    --theme-bg-secondary: #f8f9fa;
    --theme-bg-tertiary: #e9ecef;
    --theme-bg-overlay: rgba(0, 0, 0, 0.5);
    --theme-bg-panel: #ffffff;
    --theme-bg-card: #ffffff;
    --theme-bg-input: #ffffff;
    --theme-bg-hover: #f8f9fa;
    
    /* Text Colors - Light Mode */
    --theme-text-primary: #333333;
    --theme-text-secondary: #555555;
    --theme-text-muted: #6c757d;
    --theme-text-inverse: #ffffff;
    --theme-text-link: var(--brand-teal);
    --theme-text-link-hover: var(--brand-teal-dark);
    
    /* Border Colors - Light Mode */
    --theme-border-light: #e0e0e0;
    --theme-border-medium: #dee2e6;
    --theme-border-dark: #ced4da;
    --theme-border-focus: var(--brand-teal);
    
    /* Shadow Colors - Light Mode */
    --theme-shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
    --theme-shadow-md: 0 2px 6px rgba(0, 0, 0, 0.15);
    --theme-shadow-lg: 0 4px 12px rgba(31, 95, 122, 0.4);
    --theme-shadow-xl: 0 4px 20px rgba(0, 0, 0, 0.3);
    --theme-shadow-focus: 0 0 0 3px rgba(31, 95, 122, 0.1);
    
    /* Table Colors - Light Mode */
    --theme-table-bg: #ffffff;
    --theme-table-header-bg: #f8f9fa;
    --theme-table-row-hover: #f8f9fa;
    --theme-table-border: #e9ecef;
    --theme-table-stripe-odd: #ffffff;
    --theme-table-stripe-even: #f6f7fa;
    
    /* Form Colors - Light Mode */
    --theme-form-bg: #ffffff;
    --theme-form-border: #ced4da;
    --theme-form-focus-border: var(--brand-teal);
    --theme-form-disabled-bg: #f1f3f4;
    
    /* Alert Colors - Light Mode */
    --theme-alert-success-bg: rgba(164, 198, 57, 0.15);
    --theme-alert-warning-bg: rgba(255, 153, 102, 0.15);
    --theme-alert-danger-bg: #f8d7da;
    --theme-alert-info-bg: rgba(102, 153, 204, 0.15);
}

/* ===== DARK THEME ===== */
[data-theme="dark"] {
    /* Theme Colors - Dark Mode */
    --theme-bg-primary: #1a1a1a;
    --theme-bg-secondary: #2d2d2d;
    --theme-bg-tertiary: #404040;
    --theme-bg-overlay: rgba(0, 0, 0, 0.7);
    --theme-bg-panel: #2d2d2d;
    --theme-bg-card: #2d2d2d;
    --theme-bg-input: #404040;
    --theme-bg-hover: #404040;
    
    /* Text Colors - Dark Mode */
    --theme-text-primary: #f0f0f0;
    --theme-text-secondary: #c0c0c0;
    --theme-text-muted: #999999;
    --theme-text-inverse: #1a1a1a;
    --theme-text-link: #87ceeb;
    --theme-text-link-hover: #b3d9f2;
    
    /* Border Colors - Dark Mode */
    --theme-border-light: #404040;
    --theme-border-medium: #555555;
    --theme-border-dark: #666666;
    --theme-border-focus: #80b3e6;
    
    /* Shadow Colors - Dark Mode */
    --theme-shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --theme-shadow-md: 0 2px 6px rgba(0, 0, 0, 0.4);
    --theme-shadow-lg: 0 4px 12px rgba(0, 0, 0, 0.5);
    --theme-shadow-xl: 0 4px 20px rgba(0, 0, 0, 0.6);
    --theme-shadow-focus: 0 0 0 3px rgba(128, 179, 230, 0.3);
    
    /* Table Colors - Dark Mode */
    --theme-table-bg: #2d2d2d;
    --theme-table-header-bg: #404040;
    --theme-table-row-hover: #404040;
    --theme-table-border: #555555;
    --theme-table-stripe-odd: #2d2d2d;
    --theme-table-stripe-even: #353535;
    
    /* Form Colors - Dark Mode */
    --theme-form-bg: #404040;
    --theme-form-border: #666666;
    --theme-form-focus-border: #80b3e6;
    --theme-form-disabled-bg: #555555;
    
    /* Alert Colors - Dark Mode */
    --theme-alert-success-bg: rgba(164, 198, 57, 0.2);
    --theme-alert-warning-bg: rgba(255, 153, 102, 0.2);
    --theme-alert-danger-bg: rgba(220, 53, 69, 0.2);
    --theme-alert-info-bg: rgba(102, 153, 204, 0.2);
}

/* ===== THEME TOGGLE BUTTON ===== */
.theme-toggle {
    position: relative;
    background: none;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 20px;
    padding: 8px 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    color: var(--theme-text-inverse);
    font-size: 14px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: 10px;
}

.theme-toggle:hover {
    border-color: rgba(255, 255, 255, 0.5);
    background: rgba(255, 255, 255, 0.1);
    transform: none;
    box-shadow: none;
}

.theme-toggle:focus {
    outline: none;
    border-color: rgba(255, 255, 255, 0.7);
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
}

.theme-toggle .icon {
    font-size: 16px;
    transition: transform 0.3s ease;
}

.theme-toggle .text {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Dark mode toggle styling */
[data-theme="dark"] .theme-toggle {
    border-color: rgba(128, 179, 230, 0.5);
    color: var(--theme-text-primary);
}

[data-theme="dark"] .theme-toggle:hover {
    border-color: rgba(128, 179, 230, 0.7);
    background: rgba(128, 179, 230, 0.1);
}

/* ===== APPLY THEME VARIABLES TO EXISTING ELEMENTS ===== */

/* Base Elements */
html {
    background: var(--theme-bg-secondary);
    color: var(--theme-text-primary);
    transition: background-color 0.3s ease, color 0.3s ease;
}

body {
    background: var(--theme-bg-secondary);
    color: var(--theme-text-primary);
}

/* Text Elements */
h1, h2, h3, h4, h5, h6 {
    color: var(--theme-text-primary);
}

p {
    color: var(--theme-text-primary);
}

/* Header Elements - Always white text on brand gradient background */
#header h1, #header h2, #header h3, #header h4, #header h5, #header h6 {
    color: var(--theme-text-inverse) !important;
}

#header p, #header a, #header .navitem {
    color: var(--theme-text-inverse) !important;
}

/* Footer Elements - Always white text on brand gradient background */
#footer, #footer p, #footer a {
    color: var(--theme-text-inverse) !important;
}

/* Links */
a:link, a:visited {
    color: var(--theme-text-link);
}

a:hover {
    color: var(--theme-text-link-hover);
}

/* Layout Elements */
#container {
    background: var(--theme-bg-secondary);
}

#panel.left {
    background-color: var(--theme-bg-panel);
    border-right: 1px solid var(--theme-border-medium);
}

#mainbody {
    background: var(--theme-bg-primary);
}

/* Tables */
table {
    background-color: var(--theme-table-bg);
    border: 1px solid var(--theme-border-medium);
}

th {
    background-color: var(--theme-table-header-bg);
    color: var(--theme-text-primary);
    border-bottom: 2px solid var(--theme-border-medium);
}

td {
    color: var(--theme-text-primary);
    border-bottom: 1px solid var(--theme-table-border);
}

tr:hover {
    background-color: var(--theme-table-row-hover);
}

.table-striped tbody tr:nth-child(odd) {
    background-color: var(--theme-table-stripe-odd);
}

.table-striped tbody tr:nth-child(even) {
    background-color: var(--theme-table-stripe-even);
}

/* Flight table specific styling */
.line_item_row:nth-child(odd) {
    background: var(--theme-table-stripe-odd);
    color: var(--theme-text-primary);
}

.line_item_row:nth-child(even) {
    background: var(--theme-table-stripe-even);
    color: var(--theme-text-primary);
}

.flight_row {
    background: var(--theme-table-header-bg);
    color: var(--theme-text-primary);
}

.flight_row:hover {
    background: var(--theme-table-row-hover);
}

/* Flight headers with brand gradient - white text */
.flight_header, .flight_header td, .flight_header th {
    color: var(--theme-text-inverse) !important;
}

/* Line item headers with brand gradient - white text */
.line_item_header, .line_item_header td, .line_item_header th {
    color: var(--theme-text-inverse) !important;
}

/* Forms */
.form-control, #search-bar-item, .fuzzMagicBox {
    background-color: var(--theme-form-bg);
    border-color: var(--theme-form-border);
    color: var(--theme-text-primary);
}

.form-control:focus, #search-bar-item:focus {
    border-color: var(--theme-form-focus-border);
    box-shadow: var(--theme-shadow-focus);
}

.form-control:disabled {
    background-color: var(--theme-form-disabled-bg);
}

input, select, textarea {
    background-color: var(--theme-form-bg);
    border-color: var(--theme-form-border);
    color: var(--theme-text-primary);
}

input:focus, select:focus, textarea:focus {
    border-color: var(--theme-form-focus-border);
    box-shadow: var(--theme-shadow-focus);
}

/* Cards and Containers */
.card, #search-box, .mainbodyitems {
    background-color: var(--theme-bg-card);
    border-color: var(--theme-border-medium);
    box-shadow: var(--theme-shadow-sm);
}

.card-header {
    background-color: var(--theme-bg-secondary);
    border-bottom-color: var(--theme-border-medium);
}

/* Card headers with brand gradient - white text */
.card-header.card-header-primary,
.card-header-primary {
    color: var(--theme-text-inverse) !important;
}

.card-header.card-header-primary h1,
.card-header.card-header-primary h2,
.card-header.card-header-primary h3,
.card-header.card-header-primary h4,
.card-header.card-header-primary h5,
.card-header.card-header-primary h6,
.card-header-primary h1,
.card-header-primary h2,
.card-header-primary h3,
.card-header-primary h4,
.card-header-primary h5,
.card-header-primary h6 {
    color: var(--theme-text-inverse) !important;
}

/* Visibility box */
#visibility-box {
    background: var(--theme-bg-secondary);
    border-left-color: var(--brand-teal);
}

/* Containers */
#flights-container, #conversions_container {
    background: var(--theme-bg-card);
}

/* Alerts */
.alert-success {
    background-color: var(--theme-alert-success-bg);
    border-color: var(--success-border);
}

.alert-warning {
    background-color: var(--theme-alert-warning-bg);
    border-color: var(--warning-border);
}

.alert-danger {
    background-color: var(--theme-alert-danger-bg);
    border-color: var(--danger-border);
}

.alert-info {
    background-color: var(--theme-alert-info-bg);
    border-color: var(--info-border);
}

/* Modal */
.modal-content {
    background-color: var(--theme-bg-card);
    box-shadow: var(--theme-shadow-xl);
}

.modal-body {
    color: var(--theme-text-primary);
}

.modal-backdrop {
    background-color: var(--theme-bg-overlay);
}

/* Overlay and Alert Box */
#overlay {
    background-color: var(--theme-bg-overlay);
}

#alertBox {
    background-color: var(--theme-bg-card);
    color: var(--theme-text-primary);
    box-shadow: var(--theme-shadow-xl);
}

/* Fuzzy Search */
#fuzzNameContainer {
    background-color: var(--theme-form-bg);
    border-color: var(--theme-form-border);
    color: var(--theme-text-primary);
}

#fuzzDropdownContainer {
    background-color: var(--theme-bg-card);
    border-color: var(--theme-border-medium);
}

#fuzzResults li {
    color: var(--theme-text-primary);
}

#fuzzResults li:hover,
#fuzzResults li.selected {
    color: var(--theme-text-muted);
}

/* Menu */
#menu {
    background: var(--theme-bg-card);
    border-color: var(--theme-border-dark);
}

#menuitems h3 {
    border-top-color: var(--theme-border-light);
    color: var(--theme-text-primary);
}

#menuitems h3:hover {
    background-color: var(--theme-bg-hover);
}

#menuitems a {
    color: var(--theme-text-primary) !important;
}

#menuitems a:hover {
    color: var(--theme-text-secondary) !important;
}

/* Flight button column */
.flight_button_column button {
    background-color: var(--theme-form-bg);
    border-color: var(--theme-form-border);
    color: var(--theme-text-primary);
}

/* Loader */
.loader {
    border-color: var(--theme-border-light);
    border-top-color: var(--brand-teal);
}

/* ===== SMOOTH TRANSITIONS ===== */
* {
    transition: background-color 0.3s ease, 
                color 0.3s ease, 
                border-color 0.3s ease, 
                box-shadow 0.3s ease;
}

/* Exclude elements that should not have transitions */
button, input[type="button"], .btn, 
.theme-toggle, .loader, 
*:hover, *:focus, *:active {
    transition: background-color 0.2s ease, 
                color 0.2s ease, 
                border-color 0.2s ease, 
                box-shadow 0.2s ease,
                transform 0.2s ease;
}

/* ===== BRAND GRADIENT BACKGROUNDS - ALWAYS WHITE TEXT ===== */

/* Elements with brand gradient backgrounds should always have white text */
.bg-brand-gradient,
.bg-brand-gradient *,
[style*="linear-gradient"][style*="var(--brand-teal)"],
[style*="linear-gradient"][style*="#1f5f7a"] {
    color: var(--theme-text-inverse) !important;
}

/* Section links with brand gradient */
.sectionlink, .partnerlink {
    color: var(--theme-text-inverse) !important;
}

.sectionlink a, .partnerlink a {
    color: var(--theme-text-inverse) !important;
}

.sectionlink:hover, .partnerlink:hover {
    color: var(--theme-text-inverse) !important;
}

.sectionlink:hover a, .partnerlink:hover a {
    color: var(--theme-text-inverse) !important;
}

/* Navigation items in header */
.navitem a {
    color: var(--theme-text-inverse) !important;
}

.navitem a:hover {
    color: rgba(255, 255, 255, 0.8) !important;
}

/* Modal headers with brand gradient */
.modal-header {
    color: var(--theme-text-inverse) !important;
}

.modal-header h1, .modal-header h2, .modal-header h3, 
.modal-header h4, .modal-header h5, .modal-header h6,
.modal-title {
    color: var(--theme-text-inverse) !important;
}

/* Table headers with brand gradient */
.table-primary th, th.primary {
    color: var(--theme-text-inverse) !important;
}

/* Buttons with brand gradient backgrounds */
.btn-primary, button:not(.theme-toggle):not(.btn-secondary):not(.btn-danger):not(.btn-warning):not(.btn-success):not(.sticky-header-button) {
    color: var(--theme-text-inverse) !important;
}

/* Navbar and header navigation */
.navbar, .nav-header, .header-nav {
    color: var(--theme-text-inverse) !important;
}

.navbar *, .nav-header *, .header-nav * {
    color: var(--theme-text-inverse) !important;
}

/* ===== DARK BACKGROUNDS - LIGHT TEXT ===== */

/* In dark mode, ensure text is light on dark backgrounds */
[data-theme="dark"] .bg-secondary,
[data-theme="dark"] .bg-tertiary,
[data-theme="dark"] .card-header:not(.card-header-primary) {
    color: var(--theme-text-primary) !important;
}

[data-theme="dark"] .bg-secondary *,
[data-theme="dark"] .bg-tertiary *,
[data-theme="dark"] .card-header:not(.card-header-primary) * {
    color: var(--theme-text-primary) !important;
}

/* Ensure form labels are visible in dark mode */
[data-theme="dark"] .form-label,
[data-theme="dark"] label {
    color: var(--theme-text-primary) !important;
}

/* Ensure table text is visible in dark mode */
[data-theme="dark"] table,
[data-theme="dark"] th,
[data-theme="dark"] td {
    color: var(--theme-text-primary) !important;
}

/* Specific fixes for sortable tables */
[data-theme="dark"] table.sortable,
[data-theme="dark"] table.sortable th,
[data-theme="dark"] table.sortable td,
[data-theme="dark"] table.sortable tbody,
[data-theme="dark"] table.sortable thead {
    color: var(--theme-text-primary) !important;
}

/* Sortable table buttons in dark mode */
[data-theme="dark"] table.sortable th button,
[data-theme="dark"] table.sortable thead th button,
[data-theme="dark"] #plans th button,
[data-theme="dark"] .sortable th button {
    color: var(--theme-text-primary) !important;
    background: transparent !important;
}

/* Sortable table button hover states in dark mode */
[data-theme="dark"] table.sortable th button:hover,
[data-theme="dark"] table.sortable th button:focus,
[data-theme="dark"] #plans th button:hover,
[data-theme="dark"] #plans th button:focus,
[data-theme="dark"] .sortable th button:hover,
[data-theme="dark"] .sortable th button:focus {
    color: var(--theme-text-primary) !important;
    background-color: var(--theme-bg-hover) !important;
}

/* Sortable table button spans in dark mode */
[data-theme="dark"] table.sortable th button span,
[data-theme="dark"] #plans th button span,
[data-theme="dark"] .sortable th button span {
    color: var(--theme-text-secondary) !important;
}

/* Plans table specific fixes */
[data-theme="dark"] #plans,
[data-theme="dark"] #plans th,
[data-theme="dark"] #plans td,
[data-theme="dark"] #plans tbody,
[data-theme="dark"] #plans thead {
    color: var(--theme-text-primary) !important;
}

/* Plans table links in dark mode */
[data-theme="dark"] #plans a,
[data-theme="dark"] #plans td a,
[data-theme="dark"] #plans th a,
[data-theme="dark"] table#plans a {
    color: var(--theme-text-link) !important;
}

[data-theme="dark"] #plans a:hover,
[data-theme="dark"] #plans td a:hover,
[data-theme="dark"] #plans th a:hover,
[data-theme="dark"] table#plans a:hover {
    color: var(--theme-text-link-hover) !important;
}

/* Override any remaining dark text on dark backgrounds */
[data-theme="dark"] .text-dark,
[data-theme="dark"] .text-black {
    color: var(--theme-text-primary) !important;
}

/* Ensure all table content is readable in dark mode */
[data-theme="dark"] table *,
[data-theme="dark"] .table *,
[data-theme="dark"] tbody *,
[data-theme="dark"] thead *,
[data-theme="dark"] tfoot * {
    color: var(--theme-text-primary) !important;
}

/* Exception for links in tables - use theme link colors */
[data-theme="dark"] table a,
[data-theme="dark"] .table a,
[data-theme="dark"] tbody a,
[data-theme="dark"] thead a,
[data-theme="dark"] tfoot a {
    color: var(--theme-text-link) !important;
}

[data-theme="dark"] table a:hover,
[data-theme="dark"] .table a:hover,
[data-theme="dark"] tbody a:hover,
[data-theme="dark"] thead a:hover,
[data-theme="dark"] tfoot a:hover {
    color: var(--theme-text-link-hover) !important;
}

/* ===== ACCESSIBILITY ===== */

/* Respect user's motion preferences */
@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation: none !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --theme-border-light: #000000;
        --theme-border-medium: #000000;
        --theme-border-dark: #000000;
    }
    
    [data-theme="dark"] {
        --theme-border-light: #ffffff;
        --theme-border-medium: #ffffff;
        --theme-border-dark: #ffffff;
    }
}

/* ===== SORTABLE TABLE DARK MODE FIXES ===== */

/* High-specificity fixes for sortable tables in dark mode */
[data-theme="dark"] body #mainbody .mainbodyitems table,
[data-theme="dark"] body #mainbody .mainbodyitems table *,
[data-theme="dark"] body #mainbody section.mainbodyitems table,
[data-theme="dark"] body #mainbody section.mainbodyitems table * {
    color: var(--theme-text-primary) !important;
}

/* Sortable table specific fixes */
[data-theme="dark"] body #mainbody .mainbodyitems table.sortable,
[data-theme="dark"] body #mainbody .mainbodyitems table.sortable th,
[data-theme="dark"] body #mainbody .mainbodyitems table.sortable td,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans th,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans td {
    color: var(--theme-text-primary) !important;
    background-color: var(--theme-table-bg) !important;
}

/* Plans table links with high specificity */
[data-theme="dark"] body #mainbody .mainbodyitems table#plans a,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans td a,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans th a,
[data-theme="dark"] body #mainbody section.mainbodyitems table#plans a {
    color: var(--theme-text-link) !important;
    text-decoration: none !important;
}

[data-theme="dark"] body #mainbody .mainbodyitems table#plans a:hover,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans td a:hover,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans th a:hover,
[data-theme="dark"] body #mainbody section.mainbodyitems table#plans a:hover {
    color: var(--theme-text-link-hover) !important;
    text-decoration: underline !important;
}

/* Table row hover states in dark mode */
[data-theme="dark"] body #mainbody .mainbodyitems table tr:hover,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans tr:hover {
    background-color: var(--theme-table-row-hover) !important;
}

[data-theme="dark"] body #mainbody .mainbodyitems table tr:hover *,
[data-theme="dark"] body #mainbody .mainbodyitems table#plans tr:hover * {
    color: var(--theme-text-primary) !important;
}

/* ===== ADDITIONAL CONTRAST FIXES ===== */

/* Ensure badges have proper contrast */
.badge-primary {
    color: var(--theme-text-inverse) !important;
}

/* Ensure buttons with brand backgrounds have white text */
.btn-primary, .btn-primary * {
    color: var(--theme-text-inverse) !important;
}

/* Ensure section links have white text */
.sectionlink *, .partnerlink * {
    color: var(--theme-text-inverse) !important;
}

/* Ensure navigation items have proper contrast */
#header .navitem, #header .navitem * {
    color: var(--theme-text-inverse) !important;
}

/* Ensure menu button in header has white text */
#header .menubutton {
    color: var(--theme-text-inverse) !important;
}

/* Ensure footer text is white */
#footer * {
    color: var(--theme-text-inverse) !important;
}

/* Fix any remaining text contrast issues in dark mode */
[data-theme="dark"] {
    /* Ensure all text is readable in dark mode */
    color: var(--theme-text-primary);
}

[data-theme="dark"] input::placeholder,
[data-theme="dark"] textarea::placeholder {
    color: var(--theme-text-muted);
}

/* Ensure dropdown and select elements have proper contrast */
[data-theme="dark"] option {
    background-color: var(--theme-form-bg);
    color: var(--theme-text-primary);
}

/* ===== PRINT STYLES ===== */
@media print {
    * {
        background: white !important;
        color: black !important;
        box-shadow: none !important;
    }
    
    .theme-toggle {
        display: none !important;
    }
}