:root {
  --header-bg: #1a1a2e;
  --accent: #2d4059;
  --accent-hover: #3a5273;
  --race-header-bg: #c9d6df;
  --table-header-bg: #dbe2ef;
  --panel-bg: #f9f7f7;
  --page-bg: #f5f5f5;
  --text: #141c27;
  --text-heading: #1a1a2e;
  --muted: #666;
  --border: #e0e0e0;
  --border-soft: #c9d6df;
  --danger: #d32f2f;
  --danger-hover: #b71c1c;
  --link: #2d4059;
  --white: #fff;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
}

body {
  min-height: 100vh;
  color: var(--text);
  background: var(--page-bg);
  line-height: 1.6;
  font-family: Inter, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
    Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
  font-size: 15px;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--link);
  text-decoration: none;
  transition: background-color 0.2s, color 0.2s;
}
a:hover {
  color: var(--accent-hover);
}

/* —— Header (AppHeader) —— */
.app-header {
  background-color: var(--header-bg);
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0.75rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 2rem;
}

.nav-links {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.nav-link {
  padding: 0.5rem 1rem;
  background: transparent;
  color: #e0e0e0;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  transition: all 0.2s ease;
  display: inline-block;
  text-decoration: none;
}
.nav-link:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}
.nav-link.active {
  background-color: var(--accent);
  color: white;
}

.auth-actions {
  display: flex;
  gap: 0.75rem;
  align-items: center;
}
.auth-actions form {
  display: inline;
  margin: 0;
}

.btn {
  padding: 0.5rem 1.25rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.2s ease;
  display: inline-block;
  text-decoration: none;
  font-family: inherit;
}
.btn-primary {
  background-color: var(--accent);
  color: white;
}
.btn-primary:hover {
  background-color: var(--accent-hover);
  color: white;
}
.btn-secondary {
  background-color: transparent;
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
}
.btn-secondary:hover {
  background-color: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.5);
  color: white;
}
.btn-logout {
  background-color: var(--danger);
  color: white;
}
.btn-logout:hover {
  background-color: var(--danger-hover);
}

/* —— Main —— */
.main-content {
  flex: 1;
  background-color: var(--page-bg);
  color: var(--text-heading);
  min-height: calc(100vh - 60px);
}

.page-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.page-container h1,
.page-container h2 {
  color: var(--text-heading);
  font-size: 2rem;
  margin-bottom: 1.5rem;
  text-align: center;
  font-weight: 600;
}
.page-container h3 {
  color: var(--accent);
  font-size: 1.5rem;
  margin: 1.25rem 0 0.75rem;
  font-weight: 600;
}

.error,
.error-message {
  color: var(--danger);
  background-color: #ffebee;
  padding: 0.75rem 1rem;
  border-radius: 4px;
  margin-bottom: 1rem;
  text-align: center;
}
.hint {
  color: var(--muted);
}

/* —— Forms —— */
.stack,
.login-form,
.create-team-form,
.race-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 400px;
}
.form-card {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}
.form-view {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 2rem 1.5rem;
  min-height: calc(100vh - 100px);
}
label {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-weight: 500;
  color: var(--accent);
}
input,
select,
textarea {
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  font-family: inherit;
  color: var(--text);
  background: white;
}
input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
}
.form-card .btn,
.stack .btn,
button[type='submit']:not(.btn-logout):not(.nav-link) {
  background-color: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
}
.form-card .btn:hover,
.stack button[type='submit']:hover,
button[type='submit']:not(.btn-logout):hover {
  background-color: #1e2a3a;
}

/* —— Tables / lists —— */
.card-list {
  list-style: none;
  padding: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}
.card-list li {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  justify-content: space-between;
}
.card-list li:last-child {
  border-bottom: none;
}
.card-list li:hover {
  background: #fafafa;
}

.teams-table,
.data-table,
#cars-table table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel-bg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.teams-table thead,
.data-table thead {
  background: var(--accent);
  color: white;
}
.teams-table th,
.data-table th {
  padding: 1rem;
  text-align: left;
  font-weight: 600;
}
.teams-table td,
.data-table td {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.teams-table tbody tr:hover,
.data-table tbody tr:hover {
  background: #f0f0f0;
}

#cars-table table {
  font-size: 0.85rem;
}
#cars-table th {
  background-color: var(--table-header-bg);
  padding: 4px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border-soft);
  color: var(--text);
  font-weight: bold;
}
#cars-table td {
  padding: 4px 8px;
  border-bottom: 1px solid #e8e8e8;
  color: var(--text);
  white-space: nowrap;
}

.swatch {
  display: inline-block;
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 4px;
  vertical-align: middle;
  margin-right: 0.5rem;
  border: 1px solid rgba(0, 0, 0, 0.1);
}

/* —— Tracks grid —— */
.tracks-grid,
.items-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1.5rem;
  margin-top: 1.5rem;
}
.track-card,
.market-item {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: transform 0.2s, box-shadow 0.2s;
  display: flex;
  flex-direction: column;
}
.track-card:hover,
.market-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.track-card a {
  color: inherit;
  display: block;
  padding: 1.25rem;
}
.track-preview {
  background: #f9f9f9;
  padding: 1rem;
  text-align: center;
}
.track-svg {
  max-width: 100%;
  max-height: 220px;
  object-fit: contain;
}

/* —— Live race layout —— */
.race-page {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 56px);
  overflow: hidden;
  background: var(--page-bg);
  position: relative;
}

.race-header,
#race-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem 1.5rem;
  padding: 10px 15px;
  background-color: var(--race-header-bg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  color: var(--text);
  font-weight: bold;
  flex-shrink: 0;
  min-height: 44px;
}
.race-header.empty {
  min-height: 0;
  padding: 0;
  box-shadow: none;
}
.race-info,
.race-status,
.weather-container {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.status-btn {
  background: var(--table-header-bg);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
  padding: 4px 12px;
  font-weight: bold;
  cursor: pointer;
  font-family: inherit;
  color: var(--text);
}
.status-btn:hover {
  background: var(--border-soft);
}
.weather-icon {
  width: 36px;
  height: 36px;
}
.wetness-indicator {
  font-size: 0.9rem;
  font-weight: 600;
}
.mobile-layout {
  display: none;
  width: 100%;
  flex-wrap: wrap;
  gap: 0.35rem 0.75rem;
  align-items: center;
  font-size: 0.9rem;
}
.mobile-weather-icon {
  width: 22px;
  height: 22px;
}

.no-race-container {
  position: absolute;
  inset: 0;
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(245, 245, 245, 0.92);
  padding: 1.5rem;
}
.no-race-card {
  background: white;
  padding: 2rem 2.5rem;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  text-align: center;
  max-width: 420px;
}
.no-race-card h2 {
  margin-bottom: 0.75rem;
  color: var(--text-heading);
}
.no-race-card p {
  color: var(--muted);
  margin-bottom: 1.25rem;
}
.btn-cta {
  background: #5c7aea;
  color: white;
  padding: 0.65rem 1.5rem;
  border-radius: 4px;
  font-weight: 600;
}
.btn-cta:hover {
  background: #4a66d4;
  color: white;
}

.race-body {
  display: flex;
  flex: 1;
  min-height: 0;
  position: relative;
}

.race-sidebar {
  width: min(30%, 375px);
  min-width: 225px;
  background: var(--panel-bg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  z-index: 5;
}

.cars-table-wrapper {
  flex-shrink: 0;
  overflow-x: auto;
}
.cars-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel-bg);
  font-size: 0.85rem;
}
.cars-table thead {
  background: var(--table-header-bg);
}
.cars-table th {
  padding: 4px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border-soft);
  color: var(--text);
  font-weight: bold;
}
.cars-table td {
  padding: 4px 8px;
  border-bottom: 1px solid #e8e8e8;
  color: var(--text);
  white-space: nowrap;
  vertical-align: middle;
}
.cars-table tr.top-3 {
  font-weight: 700;
}
.cars-table .pos-col,
.cars-table .car-number-col,
.cars-table .lap-col {
  width: 3rem;
}
.status-column-content {
  display: flex;
  align-items: center;
  gap: 6px;
}
.tire-icon-container {
  position: relative;
  width: 22px;
  height: 22px;
  overflow: hidden;
}
.tire-icon {
  width: 22px;
  height: 22px;
  display: block;
}
.tire-wear-overlay {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(211, 47, 47, 0.55);
  pointer-events: none;
}
.fuel-gauge-vertical {
  width: 8px;
  height: 22px;
  background: #ddd;
  border-radius: 2px;
  overflow: hidden;
  position: relative;
}
.fuel-gauge-vertical .fuel-gauge-fill {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}
.fuel-high { background: #4caf50; }
.fuel-medium { background: #ff9800; }
.fuel-low { background: #f44336; }
.status-indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}
.status-racing { background: #4caf50; }
.status-pit { background: #2196f3; }
.status-dnf { background: #f44336; }
.status-pit-requested { background: #ffc107; }
.status-finished { background: #1a237e; }

#driver-controls {
  padding: 10px;
  overflow-y: auto;
  flex: 1;
}
.driver-control {
  background: var(--panel-bg);
  border: 2px solid var(--accent);
  border-radius: 4px;
  padding: 10px;
  margin-bottom: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.control-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.75rem;
  margin-bottom: 0.5rem;
}
.control-row.second-row {
  margin-bottom: 0;
}
.driver-label {
  font-size: 0.95rem;
  flex: 1 1 auto;
  min-width: 120px;
}
.fuel-gauge-container {
  flex: 1 1 120px;
  max-width: 180px;
}
.fuel-gauge-label {
  font-size: 0.75rem;
  color: var(--muted);
}
.fuel-gauge {
  height: 12px;
  background: #ddd;
  border-radius: 3px;
  overflow: hidden;
}
.fuel-gauge .fuel-gauge-fill {
  height: 100%;
}
.style-btn,
.pit-btn {
  background: var(--table-header-bg);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
  padding: 6px 12px;
  font-weight: bold;
  cursor: pointer;
  font-family: inherit;
  color: var(--text);
}
.style-btn:hover,
.pit-btn:hover:not(:disabled) {
  background: var(--border-soft);
}
.pit-btn.cancel {
  background: #ffebee;
  color: var(--danger);
  border-color: #ffcdd2;
}
.pit-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.pit-form-inline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
}
.tire-select {
  padding: 4px 6px;
  font-size: 0.85rem;
}
.refuel-label {
  flex-direction: row;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 500;
}
.refuel-label input {
  width: 4rem;
  padding: 4px 6px;
}
.speed-readout {
  font-weight: 700;
  font-size: 0.9rem;
  min-width: 5.5rem;
}

.race-track {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f9f7f7;
  overflow: hidden;
  min-width: 0;
}
.track-overlay {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
}
.track-overlay svg {
  width: 100%;
  height: 100%;
  max-height: calc(100vh - 180px);
}

.track-visualizer {
  position: relative;
  height: 60px;
  flex-shrink: 0;
  background: var(--panel-bg);
  border-top: 1px solid var(--border-soft);
}
.viz-line {
  position: absolute;
  left: 4%;
  right: 4%;
  top: 50%;
  height: 3px;
  background: var(--border-soft);
  transform: translateY(-50%);
}
.viz-marker {
  position: absolute;
  top: 50%;
  width: 22px;
  height: 22px;
  margin-left: -11px;
  margin-top: -11px;
  border-radius: 50%;
  border: 1.5px solid #141c27;
  font-size: 10px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #141c27;
  transition: left 0.3s ease;
  z-index: 1;
}

.connection-status {
  position: fixed;
  bottom: 12px;
  right: 12px;
  z-index: 20;
  opacity: 0.8;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  background: #eee;
  color: var(--text);
  pointer-events: none;
}
.connection-status.connected {
  background: #b5e8b5;
}
.connection-status.disconnected {
  background: #ffa5a5;
}

.race-toolbar {
  display: flex;
  gap: 0.5rem;
  padding: 8px 12px;
  background: var(--panel-bg);
  border-top: 1px solid var(--border-soft);
  flex-shrink: 0;
}
.race-toolbar form {
  margin: 0;
}
.race-toolbar button {
  background-color: var(--table-header-bg);
  border: none;
  border-radius: 4px;
  padding: 5px 10px;
  font-weight: bold;
  cursor: pointer;
  color: var(--text);
  font-family: inherit;
}
.race-toolbar button:hover {
  background-color: var(--border-soft);
}

.controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.inline {
  display: inline;
  margin: 0;
}
.inline-forms {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.stats {
  white-space: pre-wrap;
  font-family: ui-monospace, monospace;
  background: white;
  padding: 1rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
  margin: 1rem 0;
}

/* —— Mobile shell / hamburger —— */
.mobile-menu-toggle {
  display: none;
  background: transparent;
  border: none;
  padding: 0.5rem;
  cursor: pointer;
  color: white;
}
.hamburger-icon,
.hamburger-icon::before,
.hamburger-icon::after {
  display: block;
  width: 22px;
  height: 2px;
  background: white;
  border-radius: 1px;
  transition: transform 0.2s, opacity 0.2s;
  position: relative;
}
.hamburger-icon::before,
.hamburger-icon::after {
  content: '';
  position: absolute;
  left: 0;
}
.hamburger-icon::before { top: -7px; }
.hamburger-icon::after { top: 7px; }
.mobile-menu-toggle.active .hamburger-icon {
  background: transparent;
}
.mobile-menu-toggle.active .hamburger-icon::before {
  top: 0;
  transform: rotate(45deg);
}
.mobile-menu-toggle.active .hamburger-icon::after {
  top: 0;
  transform: rotate(-45deg);
}

@media (max-width: 768px) {
  .mobile-menu-toggle {
    display: block;
    order: -1;
  }
  .header-container {
    padding: 0.5rem 1rem;
    flex-wrap: wrap;
    position: relative;
  }
  .nav-links,
  .auth-actions {
    display: none;
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 0.35rem;
  }
  body.nav-open .nav-links,
  body.nav-open .auth-actions {
    display: flex;
  }
  .nav-link {
    padding: 0.65rem 0.75rem;
    font-size: 1rem;
  }
  .auth-actions .btn {
    width: 100%;
    text-align: center;
  }
  .desktop-only {
    display: none !important;
  }
  .mobile-layout {
    display: flex;
  }
  .race-body {
    flex-direction: column;
  }
  .race-sidebar {
    width: 100%;
    min-width: 0;
    max-height: 42%;
  }
  .cars-table .status-text-col,
  .cars-table .pos-col,
  .cars-table .team-cell {
    display: none;
  }
  .track-visualizer {
    height: 50px;
  }
  .page-container {
    padding: 1.25rem 1rem;
  }
  .page-container h1,
  .page-container h2 {
    font-size: 1.5rem;
  }
  button,
  .btn,
  a.nav-link {
    min-height: 44px;
  }
}

/* —— Entity images (avatars, logos, cars) —— */
.entity-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  text-align: left;
}
.entity-header h1 {
  text-align: left;
  margin-bottom: 0.25rem;
  font-size: 1.75rem;
}
.entity-list {
  list-style: none;
  padding: 0;
  margin: 0 0 1.5rem;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}
.entity-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.entity-row:last-child {
  border-bottom: none;
}
.entity-meta {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  flex: 1 1 auto;
  min-width: 120px;
}
.assign-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin: 0;
}
.avatar,
.car-icon,
.team-logo,
.team-logo-sm,
.team-logo-xs {
  object-fit: contain;
  background: #f3f3f3;
  border-radius: 6px;
  flex-shrink: 0;
}
.avatar {
  border-radius: 50%;
}
.avatar-lg,
.car-icon-lg,
.team-logo {
  border-radius: 10px;
}
.team-logo-xs {
  width: 22px;
  height: 22px;
  vertical-align: middle;
  margin-right: 4px;
  display: inline-block;
}
.logo-cell {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.cars-table .tire-icon,
.tire-icon {
  width: 22px;
  height: 22px;
  object-fit: contain;
  display: block;
}
