/* ------------------------------------------------------------------ tokens */
:root {
  color-scheme: light;

  --surface-0: #f4f4f1; /* page */
  --surface-1: #fcfcfb; /* cards & chart surface */
  --surface-2: #efefeb; /* subtle fills, table headers */
  --border: #e2e1dc;
  --grid: #eae9e4;

  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8a8880;

  /* Categorical slots 1-3, validated for the light surface. */
  --series-1: #2a78d6; /* olingan */
  --series-2: #eb6834; /* o'chirilgan */
  --series-3: #1baf7a; /* spam */

  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;

  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 4%);
  --font:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue',
    Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme='light'])) {
    color-scheme: dark;
    --surface-0: #111110;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --border: #2e2e2b;
    --grid: #272725;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 24%);
  }
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --surface-0: #111110;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --border: #2e2e2b;
  --grid: #272725;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 24%);
}

/* ------------------------------------------------------------------- base */
* {
  box-sizing: border-box;
}

/* Layout rules below set `display`, which would otherwise beat the `hidden`
   attribute's UA default. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--surface-0);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3 {
  margin: 0;
  font-weight: 600;
  letter-spacing: -0.01em;
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  padding: 7px 13px;
  transition:
    background 0.12s ease,
    border-color 0.12s ease;
}
button:hover {
  background: var(--surface-2);
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
button.primary {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}
button.primary:hover {
  filter: brightness(1.08);
}
button.danger {
  color: var(--critical);
  border-color: var(--border);
}
button.ghost {
  background: transparent;
}
button.tiny {
  padding: 4px 9px;
  font-size: 12px;
}

input,
select {
  font: inherit;
  padding: 7px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  min-width: 0;
}
input:focus-visible,
select:focus-visible,
button:focus-visible,
svg:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 2px;
}
label.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}

/* ------------------------------------------------------------------ layout */
.app {
  max-width: 1240px;
  margin: 0 auto;
  padding: 20px 20px 64px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-right: auto;
}
.topbar .brand .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--good);
}
.topbar h1 {
  font-size: 17px;
}
.topbar .sub {
  font-size: 12px;
  color: var(--text-muted);
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  padding: 3px;
  border-radius: 10px;
}
.tabs button {
  border: none;
  background: transparent;
  padding: 6px 14px;
  border-radius: 7px;
  color: var(--text-secondary);
}
.tabs button[aria-selected='true'] {
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: var(--shadow);
}

/* One filter row above everything it scopes. */
.filters {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-wrap: wrap;
  padding: 12px 14px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 16px;
}
.presets {
  display: flex;
  gap: 4px;
}
.presets button[aria-pressed='true'] {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}

.grid {
  display: grid;
  gap: 14px;
}
.tiles {
  grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
}
.two-col {
  grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
  align-items: start;
}
@media (max-width: 900px) {
  .two-col {
    grid-template-columns: minmax(0, 1fr);
  }
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.card > header {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 12px;
}
.card > header h2 {
  font-size: 14px;
  white-space: nowrap;
}
/* The hint yields space first, so a card title never wraps in a narrow column. */
.card > header .hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-right: auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ------------------------------------------------------------- stat tiles */
.tile .label {
  font-size: 12px;
  color: var(--text-secondary);
}
.tile .value {
  font-size: 30px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin-top: 2px;
}
.tile .meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.tile.hero .value {
  font-size: 48px;
}

/* ----------------------------------------------------------------- charts */
.chart {
  position: relative;
}
.chart svg {
  display: block;
  width: 100%;
}
.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-secondary);
}
.legend .key {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.legend .swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  flex: none;
}

.tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 8px 10px;
  font-size: 12px;
  min-width: 132px;
  opacity: 0;
  transition: opacity 0.1s ease;
  z-index: 5;
}
.tooltip[data-visible='true'] {
  opacity: 1;
}
.tooltip .t-title {
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-primary);
}
.tooltip .t-row {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-secondary);
}
.tooltip .t-row .swatch {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  flex: none;
}
.tooltip .t-row b {
  margin-left: auto;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

/* ----------------------------------------------------------------- tables */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th,
td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
th {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 600;
}
td.num,
th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
tbody tr:last-child td {
  border-bottom: none;
}
tbody tr.clickable {
  cursor: pointer;
}
tbody tr.clickable:hover {
  background: var(--surface-2);
}
.scroll-x {
  overflow-x: auto;
}
.table-view {
  max-height: 320px;
  overflow: auto;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  white-space: nowrap;
}
.badge::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
}
.badge.active::before {
  background: var(--good);
}
.badge.pending::before {
  background: var(--warning);
}
.badge.disabled::before {
  background: var(--critical);
}

.muted {
  color: var(--text-muted);
}
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}
/* The drawer is narrower than the page, so its order text gets less room
   before the hover card takes over. */
.drawer .truncate {
  max-width: 250px;
}
.drawer .name {
  max-width: 150px;
}
.truncate {
  max-width: 380px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* Long driver names must not push the numeric columns off screen. */
.name {
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.name.wide {
  max-width: 260px;
}
/* Timestamps and id lines must never wrap onto a second row. */
.nowrap,
td.muted,
.mono.muted {
  white-space: nowrap;
}
[data-tip] {
  cursor: help;
}

.spark {
  display: block;
  overflow: visible;
}

/* Hover card used for full driver names and order text. */
.hovercard {
  position: fixed;
  z-index: 70;
  max-width: 420px;
  padding: 8px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  box-shadow: var(--shadow);
  color: var(--text-primary);
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s ease;
}
.hovercard[data-visible='true'] {
  opacity: 1;
}
.hovercard .hc-label {
  display: block;
  color: var(--text-muted);
  font-size: 11px;
  margin-bottom: 3px;
}

/* Bar plus percentage, sized so the number is never clipped even in the
   narrow drawer columns. */
.share {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  min-width: 92px;
}
.share-value {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-left: auto;
}
.share-bar {
  height: 4px;
  border-radius: 2px;
  background: var(--surface-2);
  overflow: hidden;
  flex: 1;
  min-width: 34px;
}
.share-bar > span {
  display: block;
  height: 100%;
  border-radius: 2px;
  background: var(--series-1);
}
/* Actionable failure state: the message is a command to run, so it keeps
   its line breaks and is selectable. */
.error-panel {
  max-width: 620px;
  margin: 40px auto;
  text-align: left;
}
.error-panel h2 {
  font-size: 15px;
  margin-bottom: 10px;
}
.error-panel pre {
  margin: 0 0 14px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text-secondary);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  user-select: text;
}
.empty {
  padding: 28px;
  text-align: center;
  color: var(--text-muted);
}
.stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------- overlays */
.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--surface-0);
  z-index: 50;
  padding: 20px;
}
.gate .card {
  width: min(400px, 100%);
}
.gate .field input {
  width: 100%;
}
.form-error {
  color: var(--critical);
  font-size: 12px;
}

.drawer {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgb(0 0 0 / 38%);
  display: flex;
  justify-content: flex-end;
}
.drawer[hidden] {
  display: none;
}
.drawer .panel {
  width: min(720px, 100%);
  background: var(--surface-0);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 18px;
}
.drawer .panel > header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 14px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 10px 16px;
  z-index: 60;
  max-width: 90vw;
}
.toast[hidden] {
  display: none;
}
.toast.error {
  border-color: var(--critical);
  color: var(--critical);
}

.loading {
  opacity: 0.55;
  transition: opacity 0.15s ease;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
