:root {
  --racing-red: #dc3545;
  --racing-dark: #0f1923;
  --racing-card: #1a2332;
  --racing-card-border: rgba(255,255,255,.06);
  --racing-accent: #3b82f6;
  --racing-text: #e2e8f0;
  --racing-text-muted: #8b97a8;
  --sidebar-width: 250px;
  --sidebar-collapsed-width: 64px;
  --topbar-height: 52px;
}

/* ═══════════════════════════════════════════
   SIDEBAR LAYOUT (CSS Grid)
   ═══════════════════════════════════════════ */

/* Desktop: grid layout with sidebar */
@media (min-width: 992px) {
  html {
    overflow: hidden;
    height: 100%;
  }
  body {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    grid-template-rows: var(--topbar-height) 1fr;
    height: 100%;
    overflow: hidden;
    transition: grid-template-columns 0.2s ease;
  }
  .app-sidebar {
    grid-row: 1 / -1;
    grid-column: 1;
    position: relative !important;
    transform: none !important;
    visibility: visible !important;
    width: auto !important;
    border-right: 1px solid rgba(255,255,255,.06);
    z-index: 10;
  }
  .app-topbar {
    grid-row: 1;
    grid-column: 2;
  }
  .app-main {
    grid-row: 2;
    grid-column: 2;
    overflow-y: auto;
    overflow-x: hidden;
  }

  /* Collapsed state */
  html.sidebar-collapsed body {
    grid-template-columns: var(--sidebar-collapsed-width) 1fr;
  }
  html.sidebar-collapsed .sidebar-label {
    display: none;
  }
  html.sidebar-collapsed .sidebar-brand-icon {
    font-size: 1.4rem;
  }
  html.sidebar-collapsed .sidebar-brand a {
    justify-content: center;
  }
  html.sidebar-collapsed .sidebar-link {
    justify-content: center;
    padding-left: 0;
    padding-right: 0;
  }
  html.sidebar-collapsed .sidebar-link i {
    font-size: 1.15rem;
  }
  html.sidebar-collapsed .sidebar-section-header {
    padding: 0 0.5rem;
  }
  html.sidebar-collapsed .sidebar-section-header .sidebar-divider {
    display: block;
  }
  html.sidebar-collapsed .sidebar-search .input-group-text {
    border-radius: 8px;
    cursor: pointer;
  }
  html.sidebar-collapsed .sidebar-search-input {
    display: none;
  }
  html.sidebar-collapsed .sidebar-badge {
    position: absolute;
    top: 2px;
    right: 4px;
    font-size: 0.55rem;
    padding: 1px 4px;
    min-width: auto;
  }
  html.sidebar-collapsed .sidebar-footer .sidebar-link {
    justify-content: center;
  }
}

/* Mobile: offcanvas behavior handled by Bootstrap */
@media (max-width: 991.98px) {
  body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
  }
  .app-topbar {
    position: sticky;
    top: 0;
    z-index: 1030;
  }
  .app-main {
    flex: 1;
  }
}

/* ═══════════════════════════════════════════
   SIDEBAR STYLING
   ═══════════════════════════════════════════ */

.app-sidebar {
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
}

.sidebar-inner {
  padding: 0;
}

.sidebar-brand {
  padding: 1rem 1rem 0.5rem;
}

.sidebar-brand a {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.sidebar-brand-icon {
  font-size: 1.3rem;
  flex-shrink: 0;
}

.sidebar-search {
  padding: 0 0.75rem 0.5rem;
}

.sidebar-search .input-group-text {
  background: transparent;
  border-color: rgba(255,255,255,.1);
  color: var(--racing-text-muted);
  font-size: 0.85rem;
}

.sidebar-nav {
  padding: 0 0.5rem;
}

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.5rem 0.75rem;
  margin: 1px 0;
  border-radius: 8px;
  color: inherit;
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 500;
  position: relative;
  transition: background-color 0.15s ease, color 0.15s ease;
  white-space: nowrap;
}

.sidebar-link i {
  font-size: 1.05rem;
  width: 1.3rem;
  text-align: center;
  flex-shrink: 0;
  opacity: 0.7;
}

.sidebar-link:hover {
  text-decoration: none;
}

.sidebar-link.active {
  font-weight: 600;
}

.sidebar-link.active i {
  opacity: 1;
}

.sidebar-badge {
  font-size: 0.65rem;
  margin-left: auto;
}

.sidebar-section-header {
  padding: 0.75rem 0.75rem 0.15rem;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  font-weight: 700;
  position: relative;
}

.sidebar-divider {
  display: none;
  margin: 0.4rem 0;
  opacity: 0.15;
}

.sidebar-footer {
  padding: 0.5rem;
  border-top: 1px solid rgba(255,255,255,.06);
}

.sidebar-footer .sidebar-link {
  cursor: pointer;
  color: inherit;
  text-align: left;
}

/* ═══════════════════════════════════════════
   TOP BAR
   ═══════════════════════════════════════════ */

.app-topbar {
  display: flex;
  align-items: center;
  padding: 0 1.25rem;
  height: var(--topbar-height);
  border-bottom: 1px solid rgba(255,255,255,.06);
}

/* ═══════════════════════════════════════════
   LIGHT THEME
   ═══════════════════════════════════════════ */

[data-bs-theme="light"] body {
  background-color: #f0f2f5;
  color: #1a1a2e;
}

[data-bs-theme="light"] .app-sidebar {
  background: #ffffff;
  border-right-color: rgba(0,0,0,.08) !important;
  box-shadow: 2px 0 12px rgba(0,0,0,.04);
}

[data-bs-theme="light"] .app-topbar {
  background: #ffffff;
  border-bottom-color: rgba(0,0,0,.08);
  box-shadow: 0 1px 4px rgba(0,0,0,.04);
}

[data-bs-theme="light"] .sidebar-brand a {
  color: #1a1a2e;
}

[data-bs-theme="light"] .sidebar-link {
  color: #4a5568;
}

[data-bs-theme="light"] .sidebar-link:hover {
  background-color: rgba(0,0,0,.04);
  color: #1a1a2e;
}

[data-bs-theme="light"] .sidebar-link.active {
  background-color: rgba(59,130,246,.1);
  color: var(--racing-accent);
}

[data-bs-theme="light"] .sidebar-section-header {
  color: #a0aec0;
}

[data-bs-theme="light"] .sidebar-footer {
  border-top-color: rgba(0,0,0,.06);
}

[data-bs-theme="light"] .sidebar-search .input-group-text {
  border-color: rgba(0,0,0,.1);
  color: #a0aec0;
}

[data-bs-theme="light"] .sidebar-search .form-control {
  border-color: rgba(0,0,0,.1);
}

[data-bs-theme="light"] .card {
  border: none;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
  border-radius: 12px;
}

[data-bs-theme="light"] .card-header {
  border-radius: 12px 12px 0 0;
}

[data-bs-theme="light"] .diff-highlight {
  background-color: #fff3cd !important;
  font-weight: 600;
}

/* ═══════════════════════════════════════════
   DARK THEME
   ═══════════════════════════════════════════ */

[data-bs-theme="dark"] body {
  background: linear-gradient(145deg, #0b1120 0%, #0f1923 40%, #131f2e 100%);
  color: var(--racing-text);
}

[data-bs-theme="dark"] .app-sidebar {
  background: rgba(15, 25, 35, 0.95);
  backdrop-filter: blur(12px);
}

[data-bs-theme="dark"] .app-topbar {
  background: rgba(15, 25, 35, 0.85);
  backdrop-filter: blur(12px);
}

[data-bs-theme="dark"] .sidebar-brand a {
  color: var(--racing-text);
}

[data-bs-theme="dark"] .sidebar-link {
  color: var(--racing-text-muted);
}

[data-bs-theme="dark"] .sidebar-link:hover {
  background-color: rgba(255,255,255,.06);
  color: var(--racing-text);
}

[data-bs-theme="dark"] .sidebar-link.active {
  background-color: rgba(59,130,246,.15);
  color: #60a5fa;
}

[data-bs-theme="dark"] .sidebar-section-header {
  color: rgba(255,255,255,.3);
}

[data-bs-theme="dark"] .card {
  background: var(--racing-card);
  border: 1px solid var(--racing-card-border);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}

[data-bs-theme="dark"] .card-header {
  background: rgba(255,255,255,.03);
  border-bottom: 1px solid rgba(255,255,255,.06);
  border-radius: 12px 12px 0 0;
}

[data-bs-theme="dark"] .card-header.bg-dark {
  background: rgba(255,255,255,.05) !important;
}

[data-bs-theme="dark"] .card-header.bg-secondary {
  background: rgba(255,255,255,.05) !important;
}

[data-bs-theme="dark"] .card-header.bg-danger {
  background: rgba(220,53,69,.2) !important;
  border-bottom-color: rgba(220,53,69,.3);
}

[data-bs-theme="dark"] .card-header.bg-light {
  background: rgba(255,255,255,.04) !important;
  color: var(--racing-text) !important;
}

[data-bs-theme="dark"] .table {
  color: var(--racing-text);
  --bs-table-bg: transparent;
}

[data-bs-theme="dark"] .table th {
  color: var(--racing-text-muted);
  border-bottom-color: rgba(255,255,255,.06);
}

[data-bs-theme="dark"] .table td {
  border-bottom-color: rgba(255,255,255,.04);
}

[data-bs-theme="dark"] .table-danger {
  --bs-table-bg: rgba(220,53,69,.1);
  --bs-table-border-color: rgba(220,53,69,.15);
}

[data-bs-theme="dark"] .table-warning {
  --bs-table-bg: rgba(255,193,7,.08);
  --bs-table-border-color: rgba(255,193,7,.12);
}

[data-bs-theme="dark"] .form-control,
[data-bs-theme="dark"] .form-select {
  background-color: #1e2d3d;
  border-color: rgba(255,255,255,.1);
  color: var(--racing-text);
}

[data-bs-theme="dark"] .form-control:focus,
[data-bs-theme="dark"] .form-select:focus {
  background-color: #1e2d3d;
  border-color: var(--racing-accent);
  box-shadow: 0 0 0 .2rem rgba(59,130,246,.2);
  color: var(--racing-text);
}

[data-bs-theme="dark"] .form-select option,
[data-bs-theme="dark"] select option {
  background-color: #1e2d3d;
  color: var(--racing-text);
}

[data-bs-theme="dark"] select,
[data-bs-theme="dark"] .form-select {
  color-scheme: dark;
}

[data-bs-theme="dark"] .form-control::placeholder {
  color: rgba(255,255,255,.3);
}

[data-bs-theme="dark"] .btn-dark {
  background-color: var(--racing-accent);
  border-color: var(--racing-accent);
}

[data-bs-theme="dark"] .btn-dark:hover {
  background-color: #2563eb;
  border-color: #2563eb;
}

[data-bs-theme="dark"] .btn-outline-secondary {
  border-color: rgba(255,255,255,.15);
  color: var(--racing-text-muted);
}

[data-bs-theme="dark"] .btn-outline-secondary:hover {
  background-color: rgba(255,255,255,.08);
  border-color: rgba(255,255,255,.25);
  color: var(--racing-text);
}

[data-bs-theme="dark"] .btn-outline-dark {
  border-color: rgba(255,255,255,.15);
  color: var(--racing-text);
}

[data-bs-theme="dark"] .btn-outline-dark:hover {
  background-color: rgba(255,255,255,.08);
  border-color: rgba(255,255,255,.25);
  color: #fff;
}

[data-bs-theme="dark"] .btn-outline-danger {
  border-color: rgba(220,53,69,.4);
}

[data-bs-theme="dark"] .btn-outline-success .bi,
[data-bs-theme="dark"] .btn-outline-danger .bi {
  filter: brightness(1.2);
}

[data-bs-theme="dark"] .text-muted {
  color: var(--racing-text-muted) !important;
}

[data-bs-theme="dark"] .text-decoration-none {
  color: #60a5fa;
}

[data-bs-theme="dark"] .text-decoration-none:hover {
  color: #93c5fd;
}

[data-bs-theme="dark"] .badge.bg-secondary {
  background-color: rgba(255,255,255,.1) !important;
  color: var(--racing-text-muted);
}

[data-bs-theme="dark"] .badge.bg-success {
  background-color: rgba(25,135,84,.25) !important;
  color: #4ade80;
}

[data-bs-theme="dark"] .badge.bg-danger {
  background-color: rgba(220,53,69,.25) !important;
  color: #f87171;
}

[data-bs-theme="dark"] .badge.bg-warning {
  background-color: rgba(255,193,7,.2) !important;
  color: #fbbf24;
}

[data-bs-theme="dark"] .badge.bg-light {
  background-color: rgba(255,255,255,.06) !important;
  color: var(--racing-text-muted) !important;
}

[data-bs-theme="dark"] .progress {
  background-color: rgba(255,255,255,.08);
  border-radius: 8px;
}

[data-bs-theme="dark"] .progress-bar {
  border-radius: 8px;
}

[data-bs-theme="dark"] .modal-content {
  background-color: var(--racing-card);
  border: 1px solid var(--racing-card-border);
}

[data-bs-theme="dark"] .modal-header {
  border-bottom-color: rgba(255,255,255,.06);
}

[data-bs-theme="dark"] .modal-footer {
  border-top-color: rgba(255,255,255,.06);
}

[data-bs-theme="dark"] .dropdown-menu {
  background-color: #1e2d3d;
  border: 1px solid rgba(255,255,255,.08);
}

[data-bs-theme="dark"] .dropdown-item {
  color: var(--racing-text);
}

[data-bs-theme="dark"] .dropdown-item:hover {
  background-color: rgba(255,255,255,.06);
}

[data-bs-theme="dark"] .dropdown-divider {
  border-top-color: rgba(255,255,255,.06);
}

[data-bs-theme="dark"] .alert-danger {
  background-color: rgba(220,53,69,.15);
  border-color: rgba(220,53,69,.25);
  color: #f87171;
}

[data-bs-theme="dark"] .diff-highlight {
  background-color: #3d3200 !important;
  font-weight: 600;
}

[data-bs-theme="dark"] .badge-warning {
  background-color: #ffc107;
  color: #000;
}

[data-bs-theme="dark"] a.fw-semibold {
  color: #60a5fa;
}

[data-bs-theme="dark"] .border-danger {
  border-color: rgba(220,53,69,.3) !important;
}

[data-bs-theme="dark"] .border-warning {
  border-color: rgba(255,193,7,.3) !important;
}

/* Dark offcanvas sidebar (mobile) */
[data-bs-theme="dark"] .app-sidebar.offcanvas-lg {
  background: rgba(15, 25, 35, 0.98);
}

/* ═══════════════════════════════════════════
   SHARED (both themes)
   ═══════════════════════════════════════════ */

.card {
  border: none;
  border-radius: 12px;
  transition: box-shadow .2s ease;
}

.card-header {
  font-weight: 600;
  border-radius: 12px 12px 0 0;
}

.card-body.p-0 .table {
  margin-bottom: 0;
}

.card-body.p-0 .table tr:last-child td {
  border-bottom: none;
}

.table th {
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  font-weight: 600;
}

.badge-ok { background-color: #198754; }
.badge-warning { background-color: #ffc107; color: #000; }
.badge-critical { background-color: #dc3545; }

.badge {
  font-weight: 500;
  letter-spacing: .02em;
}

.laptime-mono {
  font-family: 'Courier New', monospace;
  font-size: 1.05rem;
}

.sector-badge {
  font-size: .75rem;
  font-family: monospace;
}

.btn-action {
  padding: .2rem .5rem;
  font-size: .8rem;
  border-radius: 6px;
}

.btn {
  border-radius: 8px;
}

.form-control, .form-select {
  border-radius: 8px;
}

#theme-toggle {
  font-size: 1.1rem;
  padding: .25rem .5rem;
}

/* ═══════════════════════════════════════════
   TABLE ENHANCEMENTS
   ═══════════════════════════════════════════ */

/* Row hover */
.table tbody tr {
  transition: background-color 0.1s ease;
}

[data-bs-theme="dark"] .table tbody tr:hover {
  background-color: rgba(255,255,255,.03);
}

[data-bs-theme="light"] .table tbody tr:hover {
  background-color: rgba(0,0,0,.02);
}

/* Sticky table headers inside scrollable cards */
.card-body.p-0 .table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
}

[data-bs-theme="dark"] .card-body.p-0 .table thead th {
  background-color: var(--racing-card);
}

[data-bs-theme="light"] .card-body.p-0 .table thead th {
  background-color: #ffffff;
}

/* ═══════════════════════════════════════════
   BREADCRUMBS
   ═══════════════════════════════════════════ */

.app-breadcrumb {
  padding: 0.6rem 1.5rem 0;
  font-size: 0.82rem;
}

.app-breadcrumb .breadcrumb {
  margin-bottom: 0;
  background: transparent;
  padding: 0;
}

.app-breadcrumb .breadcrumb-item a {
  text-decoration: none;
  opacity: 0.7;
}

.app-breadcrumb .breadcrumb-item a:hover {
  opacity: 1;
}

.app-breadcrumb .breadcrumb-item.active {
  opacity: 0.5;
}

[data-bs-theme="dark"] .app-breadcrumb .breadcrumb-item a {
  color: var(--racing-text-muted);
}

[data-bs-theme="dark"] .app-breadcrumb .breadcrumb-item + .breadcrumb-item::before {
  color: rgba(255,255,255,.2);
}

/* ═══════════════════════════════════════════
   TOAST OVERRIDES
   ═══════════════════════════════════════════ */

.toast {
  border-radius: 10px;
  font-size: 0.9rem;
}

/* Smooth transitions */
body, .card, .btn, .form-control, .form-select, .badge, .table {
  transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}

/* Print: hide sidebar + topbar */
@media print {
  .app-sidebar, .app-topbar { display: none !important; }
  .app-main { grid-column: 1 / -1 !important; }
}

/* ═══════════════════════════════════════════
   MOBILE / PIT-LANE OPTIMIZATIONS
   ═══════════════════════════════════════════ */

@media (max-width: 991.98px) {
  /* Reduce container padding */
  .container-fluid { padding-left: .75rem; padding-right: .75rem; }
  .py-4 { padding-top: 1rem !important; padding-bottom: 1rem !important; }

  /* Cards stack full width */
  .card { border-radius: 8px; margin-bottom: .75rem; }
  .card-header { font-size: .9rem; padding: .6rem .75rem; border-radius: 8px 8px 0 0; }
  .card-body { padding: .75rem; }

  /* Tables: smaller text, tighter padding, horizontal scroll */
  .table-responsive { -webkit-overflow-scrolling: touch; }
  .table { font-size: .8rem; white-space: nowrap; }
  .table th, .table td { padding: .35rem .5rem; }
  .table th { font-size: .7rem; }

  /* Action buttons in tables: stack vertically */
  .table .btn-group,
  .table .d-flex { flex-direction: column; gap: .25rem; }
  .table .btn-action,
  .table .btn-sm { width: 100%; text-align: center; }

  /* Touch-friendly buttons: 44px min tap target */
  .btn { min-height: 44px; min-width: 44px; padding: .5rem .75rem; font-size: .9rem; }
  .btn-sm { min-height: 38px; font-size: .8rem; }
  .btn-action { min-height: 36px; padding: .35rem .6rem; }

  /* Forms: full width inputs */
  .form-control, .form-select { width: 100%; font-size: 1rem; min-height: 44px; }
  .input-group { flex-wrap: wrap; }
  .input-group .form-control { flex: 1 1 100%; }

  /* Row gutters tighter */
  .row { --bs-gutter-x: .75rem; }
  .row > * { margin-bottom: .5rem; }

  /* Page headings */
  h1, .h1 { font-size: 1.4rem; }
  h2, .h2 { font-size: 1.2rem; }
  h3, .h3 { font-size: 1.05rem; }

  /* Lap time mono: slightly smaller */
  .laptime-mono { font-size: .9rem; }

  /* Badges */
  .badge { font-size: .7rem; }
  .sector-badge { font-size: .65rem; }

  /* Modals: full width on mobile */
  .modal-dialog { margin: .5rem; max-width: calc(100% - 1rem); }
}
