/**
 * Stats Dashboard Styles
 */

:root {
  --color-primary: #3498db;
  --color-secondary: #95a5a6;
  --color-success: #27ae60;
  --color-error: #e74c3c;
  --color-warning: #f39c12;
  --color-info: #3498db; /* 3xx (redirects / 304 Not Modified) */
  --color-bg: #f5f6fa;
  --color-card: #ffffff;
  --color-text: #2c3e50;
  --color-border: #dcdde1;
  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.15);
  --radius: 8px;
}

[data-theme="dark"] {
  --color-bg: #121212;
  --color-card: #1e1e1e;
  --color-text: #e0e0e0;
  --color-border: #333333;
  --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.5);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  background-color: var(--color-bg);
  color: var(--color-text);
  line-height: 1.6;
}

/* ==================== LOGIN PAGE ==================== */

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.login-card {
  background: var(--color-card);
  padding: 40px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  max-width: 400px;
  width: 100%;
}

.login-card h1 {
  text-align: center;
  margin-bottom: 10px;
  color: var(--color-primary);
}

.login-card p {
  text-align: center;
  margin-bottom: 30px;
  color: var(--color-secondary);
}

/* ==================== DASHBOARD ==================== */

.dashboard {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
}

.dashboard-header {
  background: var(--color-card);
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.dashboard-header h1 {
  color: var(--color-primary);
  font-size: 24px;
}

/* ==================== BURGER MENU & DRAWER ==================== */

.drawer-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.drawer-overlay.active {
  opacity: 1;
  visibility: visible;
}

.nav-drawer {
  position: fixed;
  top: 0;
  left: -280px;
  width: 280px;
  height: 100%;
  background: var(--color-card);
  box-shadow: var(--shadow-md);
  z-index: 3000;
  transition: left 0.3s ease;
  overflow-y: auto;
}

.nav-drawer.active {
  left: 0;
}

.drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 2px solid var(--color-primary);
}

.drawer-header h2 {
  color: var(--color-primary);
  font-size: 18px;
  margin: 0;
}

.drawer-menu {
  list-style: none;
  padding: 0;
  margin: 0;
}

.drawer-menu li {
  border-bottom: 1px solid var(--color-border);
}

.drawer-link {
  display: block;
  padding: 15px 20px;
  color: var(--color-text);
  text-decoration: none;
  font-size: 16px;
  transition: background-color 0.2s ease;
}

.drawer-link:hover {
  background-color: var(--color-bg);
}

.drawer-link.active {
  background-color: var(--color-primary);
  color: white;
  font-weight: 600;
}

[data-theme="dark"] .drawer-link:hover {
  background-color: #2a2a2a;
}

.drawer-menu li button.drawer-link {
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
  display: block;
  padding: 15px 20px;
  color: var(--color-text);
  font-size: 16px;
  transition: background-color 0.2s ease;
}

/* ==================== SECTIONS ==================== */

section {
  background: var(--color-card);
  padding: 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 20px;
}

section h2 {
  margin-bottom: 20px;
  color: var(--color-primary);
  font-size: 20px;
  border-bottom: 2px solid var(--color-primary);
  padding-bottom: 10px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  border-bottom: 2px solid var(--color-primary);
  padding-bottom: 10px;
}

.section-header h2 {
  margin-bottom: 0;
  border-bottom: none;
  padding-bottom: 0;
}

/* Group of action icons (download + refresh) in a section header. */
.section-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

section h3 {
  margin-bottom: 10px;
  font-size: 16px;
  color: var(--color-text);
}

/* Discreet caption in a section header (e.g. the Monitoring bucket granularity). */
.section-note {
  font-size: 13px;
  color: var(--color-text);
  opacity: 0.7;
}

/* ==================== FILTERS ==================== */

.filters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
}

.filter-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* ==================== FORM ELEMENTS ==================== */

.form-group {
  display: flex;
  flex-direction: column;
}

.form-group label {
  font-weight: 600;
  margin-bottom: 5px;
  font-size: 14px;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"],
input[type="month"],
select {
  padding: 10px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 14px;
  font-family: inherit;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--color-primary);
}

select[multiple] {
  padding: 5px;
}

/* ==================== BUTTONS ==================== */

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  font-family: inherit;
}

.btn-primary {
  background-color: var(--color-primary);
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
}

.btn-secondary {
  background-color: var(--color-secondary);
  color: white;
}

.btn-secondary:hover {
  background-color: #7f8c8d;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary[type="submit"] {
  width: 100%;
}

.btn-icon {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  padding: 5px;
  border-radius: 50%;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon:hover {
  background-color: var(--color-bg);
  transform: rotate(30deg);
}

[data-theme="dark"] .btn-icon:hover {
  background-color: #333;
}

[data-theme="dark"] #burgerMenuBtn {
  color: #ffffff;
}

.btn-icon:active {
  transform: scale(0.9);
}

/* ==================== KPIs ==================== */

.kpis-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 20px;
}

.kpi-card {
  display: flex;
  align-items: center;
  padding: 20px;
  background: linear-gradient(135deg, var(--color-primary) 0%, #2980b9 100%);
  border-radius: var(--radius);
  color: white;
  box-shadow: var(--shadow-sm);
}

.kpi-icon {
  font-size: 48px;
  margin-right: 20px;
}

.kpi-content {
  flex: 1;
}

.kpi-label {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 5px;
}

.kpi-value {
  font-size: 32px;
  font-weight: bold;
}

/* ==================== HTTP STATUS CODES ==================== */

.status-codes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 12px;
}

.status-code-card {
  padding: 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-card);
  text-align: center;
}

.status-code-value {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
}

.status-code-count {
  font-size: 22px;
  font-weight: bold;
  color: var(--color-text);
}

.status-code-empty {
  color: var(--color-text);
  opacity: 0.7;
}

/* Monitoring: the per-code evolution histograms sit under the total cards. */
.status-codes-charts {
  margin-top: 20px;
}

.status-codes-charts:empty {
  margin-top: 0;
}

/* ==================== CHARTS ==================== */

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 20px;
}

.chart-container {
  padding: 15px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-card);
  min-height: 300px;
  max-height: 400px;
  display: flex;
  flex-direction: column;
}

.chart-container canvas {
  flex: 1;
  /* A flex item does not shrink below its content by default, which would push a
     caption out of the (max-height capped) container. The canvas is the element that
     must give way, not the text explaining what it shows. */
  min-height: 0;
  max-height: 350px;
}

/* The line under a chart that states how a displayed rate was computed (which
   denominator, what was excluded). It is part of the figure, not decoration: a premium
   rate must never appear without it. */
.chart-caption {
  flex: 0 0 auto;
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.4;
  color: var(--color-text);
  opacity: 0.75;
}

/*
 * Trend line under a KPI. Green up / red down carries the reading at a glance, and the
 * baseline sits beside it in the muted style used for every caption that states what a
 * figure was measured against.
 */
/*
 * La tendance, sur une carte au fond bleu et au texte blanc.
 *
 * Deux lignes, pas une : le pourcentage et la fenêtre de comparaison sont deux
 * informations, et les mettre bout à bout produisait une ligne qu'on ne lit pas.
 *
 * Les couleurs ne viennent PAS des variables de thème. Elles sont écrites en dur parce que
 * cette carte a son propre fond, indépendant du thème clair ou sombre : `var(--color-text)`
 * y donnait du gris foncé sur du bleu en thème clair, et `var(--color-success)` un vert
 * pâle sur bleu — les deux illisibles. Le contraste se joue ici contre le bleu de la carte,
 * pas contre le fond de la page.
 */
.kpi-trend {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  line-height: 1.35;
}

/*
 * Pastille claire sur la carte, texte sombre dans la pastille : le contraste est fort des
 * deux côtés, là où du vert sur du bleu n'en avait d'aucun.
 */
.kpi-trend-value {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 700;
  /* Les chiffres gardent la même largeur d'un rafraîchissement à l'autre. */
  font-variant-numeric: tabular-nums;
  background: rgba(255, 255, 255, 0.95);
  color: #34495e;
}

.kpi-trend[data-direction='up'] .kpi-trend-value {
  color: #1e8449;
}

.kpi-trend[data-direction='down'] .kpi-trend-value {
  color: #c0392b;
}

.kpi-trend-basis {
  font-size: 0.76rem;
  color: rgba(255, 255, 255, 0.85);
}

/* ==================== TABLES ==================== */

.table-container {
  overflow-x: auto;
  overflow-y: auto;
  max-height: 500px;
  margin-bottom: 15px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
}

/*
 * Footer line of a ranking when the server's guard cap truncated it. It should never
 * appear — but a cut tail must say so rather than look like the end of the data.
 */
.ranking-truncated {
  text-align: center;
  font-style: italic;
  color: var(--color-text);
  opacity: 0.7;
}

.data-table thead {
  position: sticky;
  top: 0;
  z-index: 10;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table thead {
  background-color: var(--color-primary);
  color: white;
}

.data-table th,
.data-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

.data-table th {
  font-weight: 600;
  font-size: 14px;
}

.data-table td {
  font-size: 13px;
}

.data-table tbody tr:hover {
  background-color: var(--color-bg);
}

[data-theme="dark"] .data-table tbody tr:hover {
  background-color: #2a2a2a;
}

/* Logs tables (Listening Logs, Request Logs): every column stays visible at every
   width — none is ever hidden. On a screen too narrow for the whole grid the table
   keeps its natural width and the container scrolls horizontally (it already has
   `overflow-x: auto`), rather than squeezing the columns down to unreadable
   ribbons. One line per cell is what gives the table that natural width: without
   it, long values would wrap and there would be nothing left to scroll. */
.table-scroll .data-table th,
.table-scroll .data-table td {
  white-space: nowrap;
}

.table-container.table-scroll {
  /* Momentum scrolling on iOS, and a horizontal swipe that reaches the end of the
     table stops there instead of dragging the page behind it. */
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}

/* Dark mode map tiles adjustment */
[data-theme="dark"] .leaflet-tile-container {
  filter: invert(100%) hue-rotate(180deg) brightness(95%) contrast(90%);
}

[data-theme="dark"] .leaflet-container {
  background: #262626;
}

/* Status colors */
.status-success {
  color: var(--color-success);
  font-weight: 600;
}

.status-error {
  color: var(--color-error);
  font-weight: 600;
}

.status-warning {
  color: var(--color-warning);
  font-weight: 600;
}

.status-info {
  color: var(--color-info);
  font-weight: 600;
}

/* ==================== BADGES ==================== */

.badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}

.badge-premium {
  background-color: var(--color-success);
  color: white;
}

.badge-free {
  background-color: var(--color-secondary);
  color: white;
}

/* Unknown premium status: deliberately NOT a badge. It has to read as an absence of
   information, not as a negative answer — a muted dash next to the Premium / Not premium
   pills, never a red "no" pill. */
.premium-unknown {
  display: inline-block;
  color: var(--color-text);
  opacity: 0.45;
  font-weight: 600;
  cursor: help;
}

/* ==================== PAGINATION ==================== */

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
}

.pagination span {
  font-weight: 600;
}

/* ==================== LOGS SECTION ==================== */

.logs-counter {
  margin-bottom: 10px;
  font-weight: 600;
  color: var(--color-secondary);
}

.load-more-container {
  text-align: center;
  margin-top: 15px;
}

/* ==================== ERROR MESSAGE ==================== */

.error-message {
  background-color: var(--color-error);
  color: white;
  padding: 10px;
  border-radius: var(--radius);
  margin-bottom: 15px;
  text-align: center;
}

/* ==================== RESPONSIVE DESIGN ==================== */

/* Tablet */
@media (max-width: 768px) {
  .filters-grid {
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  }

  .charts-grid {
    grid-template-columns: 1fr;
  }

  .kpis-grid {
    grid-template-columns: 1fr;
  }

  .dashboard-header {
    flex-direction: column;
    gap: 15px;
  }

  .dashboard-header h1 {
    font-size: 20px;
  }

  .hide-tablet {
    display: none;
  }

  /* …except in the logs tables, which keep the whole grid (Duration included)
     and scroll horizontally instead. */
  .table-scroll .hide-tablet {
    display: table-cell;
  }
}

/* Mobile */
@media (max-width: 480px) {
  .dashboard {
    padding: 10px;
  }

  section {
    padding: 15px;
  }

  .login-card {
    padding: 30px 20px;
  }

  .filters-grid {
    grid-template-columns: 1fr;
  }

  .filter-actions {
    flex-direction: column;
  }

  .filter-actions .btn {
    width: 100%;
  }

  .hide-mobile {
    display: none;
  }

  /* Logs tables: Platform / Version / Build stay too. */
  .table-scroll .hide-mobile {
    display: table-cell;
  }

  .kpi-icon {
    font-size: 36px;
    margin-right: 15px;
  }

  .kpi-value {
    font-size: 24px;
  }

  .data-table th,
  .data-table td {
    padding: 8px;
    font-size: 12px;
  }
}

/* ==================== SERVER STATUS INDICATOR ==================== */

/* Dot + health-check round-trip time, clickable as a single block. */
.server-status {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 5px 8px;
  border-radius: var(--radius);
  transition: background-color 0.2s;
}

.server-status:hover {
  background-color: var(--color-bg);
}

[data-theme="dark"] .server-status:hover {
  background-color: #333;
}

.status-latency {
  font-family: monospace;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text);
  opacity: 0.85;
  white-space: nowrap;
}

.status-latency.degraded {
  color: var(--color-warning);
  opacity: 1;
}

.status-latency.offline {
  color: var(--color-error);
  opacity: 1;
}

.status-indicator {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  transition: all 0.3s ease;
  background-color: var(--color-secondary);
  display: inline-block;
  flex-shrink: 0;
}

.status-indicator.online {
  background-color: #27ae60;
  box-shadow: 0 0 8px rgba(39, 174, 96, 0.4);
}

.status-indicator.degraded {
  background-color: #f39c12;
  box-shadow: 0 0 8px rgba(243, 156, 18, 0.4);
}

.status-indicator.offline {
  background-color: #e74c3c;
  box-shadow: 0 0 8px rgba(231, 76, 60, 0.4);
}

.status-indicator.loading {
  animation: status-pulse 1s infinite ease-in-out;
}

@keyframes status-pulse {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.7; }
  100% { transform: scale(1); opacity: 1; }
}

/* ==================== HEADER COUNTERS ==================== */

.header-counter {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  padding: 5px 8px;
  border-radius: var(--radius);
  transition: background-color 0.2s;
  color: var(--color-text);
}

.header-counter:hover {
  background-color: var(--color-bg);
}

.header-counter span {
  font-family: monospace;
}

[data-theme="dark"] .header-counter:hover {
  background-color: #333;
}
