/* ---------- Theme tokens ---------- */
:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #171716;
  --surface-2: #1f1f1d;
  --surface-3: #2a2a28;
  --border: rgba(255, 255, 255, 0.08);
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #262624;
  --axis: #383835;
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.15);
  --accent-today: rgba(57, 135, 229, 0.45);
  --good: #0ca30c;
  --good-text: #3fc43f;
  --warning: #fab219;
  --critical: #d03b3b;
  --flash: rgba(57, 135, 229, 0.20);
}
:root[data-theme="light"] {
  color-scheme: light;
  --page: #f3f2ee;
  --surface: #fcfcfb;
  --surface-2: #f4f3ef;
  --surface-3: #e9e7e1;
  --border: rgba(11, 11, 11, 0.09);
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6f6e69;
  --grid: #e7e6e0;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, 0.11);
  --accent-today: rgba(42, 120, 214, 0.40);
  --good-text: #006300;
  --flash: rgba(42, 120, 214, 0.14);
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { font-size: clamp(14px, 0.42vw + 9px, 18px); }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--page);
  color: var(--text);
  font-family: "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}
button { font: inherit; color: inherit; }

.app {
  display: flex; flex-direction: column;
  gap: 1.25rem;
  padding: 1.4rem 2.2rem 1.6rem;
  max-width: 2200px; margin: 0 auto;
}

/* ---------- Top bar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem 1.5rem; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 1rem; min-width: 0; }
.logo {
  width: 3rem; height: 3rem; border-radius: 0.8rem; flex: none;
  background: var(--accent); display: grid; place-items: center;
}
.logo svg { width: 1.7rem; height: 1.7rem; fill: #fff; }
.brand h1 { margin: 0; font-size: 1.75rem; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
.brand p { margin: 0.15rem 0 0; color: var(--text-2); font-size: 0.95rem; }

.topbar-right { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.demo-pill {
  display: inline-flex; align-items: center; gap: 0.4rem;
  padding: 0.3rem 0.7rem; border-radius: 999px; font-size: 0.8rem; font-weight: 600;
  color: var(--text-2); border: 1px dashed var(--muted);
}
.demo-pill[hidden] { display: none; }
.demo-pill svg { width: 0.95rem; height: 0.95rem; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.conn {
  display: inline-flex; align-items: center; gap: 0.5rem;
  padding: 0.35rem 0.8rem; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border);
  font-size: 0.85rem; font-weight: 600; color: var(--text-2);
}
.conn-dot { width: 0.55rem; height: 0.55rem; border-radius: 50%; background: var(--muted); }
.conn[data-state="live"] { color: var(--text); }
.conn[data-state="live"] .conn-dot { background: var(--good); animation: pulse 2s infinite; }
.conn[data-state="offline"] .conn-dot { background: var(--critical); }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0.55); }
  70% { box-shadow: 0 0 0 0.45rem rgba(12, 163, 12, 0); }
  100% { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0); }
}
.clock { text-align: right; }
.clock-time { font-size: 1.6rem; font-weight: 650; letter-spacing: -0.01em; line-height: 1.1; font-variant-numeric: tabular-nums; }
.clock-date { font-size: 0.82rem; color: var(--text-2); }
.icon-btn {
  width: 2.4rem; height: 2.4rem; border-radius: 0.65rem; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 1.1rem; height: 1.1rem; fill: var(--text-2); }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 1.25rem; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 1rem;
  padding: 1.1rem 1.5rem 1.2rem; min-width: 0;
}
.kpi-label { font-size: 0.85rem; font-weight: 600; color: var(--text-2); }
.kpi-value {
  font-size: 3.1rem; font-weight: 700; letter-spacing: -0.035em; line-height: 1.05;
  margin: 0.35rem 0 0.3rem; font-variant-numeric: tabular-nums;
}
.kpi-value small { font-size: 1.5rem; font-weight: 600; color: var(--text-2); letter-spacing: -0.01em; margin-left: 0.1rem; }
.kpi-sub { font-size: 0.88rem; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-sub .up { color: var(--good-text); font-weight: 650; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  grid-template-areas: "chart robot" "recent units";
  gap: 1.25rem;
}
.panel-chart { grid-area: chart; }
.panel-robot { grid-area: robot; }
.panel-recent { grid-area: recent; }
.panel-dest { grid-area: units; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 1rem;
  padding: 1.15rem 1.5rem 1.2rem; min-width: 0;
  display: flex; flex-direction: column;
}
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 0.8rem; }
.panel-head h2 { margin: 0; font-size: 1.1rem; font-weight: 650; letter-spacing: -0.01em; }
.panel-sub { margin: 0.15rem 0 0; font-size: 0.85rem; color: var(--text-2); }

/* Big screens: whole dashboard fits one screen, no scrolling */
@media (min-width: 1200px) and (min-height: 850px) {
  html, body { height: 100%; overflow: hidden; }
  .app { height: 100%; }
  .layout { flex: 1; min-height: 0; grid-template-rows: minmax(0, 1.1fr) minmax(0, 1fr); }
  .hbars { overflow: hidden; }
  .panel { min-height: 0; }
  .chart { flex: 1; min-height: 0; height: auto; }
  .panel .chart-hours { height: auto; min-height: 8rem; }
  .recent { flex: 1; min-height: 0; overflow: hidden; }
}

/* ---------- Daily chart ---------- */
.legend-note { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--text-2); white-space: nowrap; }
.ref-swatch { width: 1.4rem; border-top: 2px dashed var(--text-2); }
.chart { position: relative; height: 17rem; }
.chart svg { position: absolute; inset: 0; display: block; width: 100%; height: 100%; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 12px; }
.chart .bar { fill: var(--accent); }
.chart .bar.today { fill: var(--accent-today); }
.chart .ref { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 5 4; }
.chart .ref-label { fill: var(--text); font-size: 12px; font-weight: 650; }
.chart .hit { fill: transparent; }
.chart .col:hover .bar { opacity: 0.8; }
.chart-hours { height: 14rem; }

/* ---------- Robot ---------- */
.robot { display: flex; flex-direction: column; gap: 0.75rem; flex: 1; min-height: 0; }
.robot-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; }
.robot-id { display: flex; align-items: center; gap: 0.75rem; }
.robot-avatar {
  width: 2.8rem; height: 2.8rem; border-radius: 0.75rem; background: var(--accent-soft);
  display: grid; place-items: center; flex: none;
}
.robot-avatar svg { width: 1.6rem; height: 1.6rem; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; }
.robot-name { font-size: 1.25rem; font-weight: 700; letter-spacing: -0.01em; }
.robot-loc { font-size: 0.85rem; color: var(--text-2); }
.state {
  display: inline-flex; align-items: center; gap: 0.45rem;
  padding: 0.3rem 0.75rem; border-radius: 999px; background: var(--surface-2);
  font-size: 0.85rem; font-weight: 650; white-space: nowrap;
}
.state i { width: 0.55rem; height: 0.55rem; border-radius: 50%; background: var(--muted); }
.state.st-delivering i, .state.st-returning i { background: var(--accent); }
.state.st-idle i { background: var(--good); }
.state.st-charging i, .state.st-maintenance i { background: var(--warning); }
.state.st-error i, .state.st-offline i { background: var(--critical); }

.task {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 0.8rem;
  padding: 0.8rem 1rem; display: flex; flex-direction: column; gap: 0.5rem;
}
.task-label { font-size: 0.78rem; font-weight: 650; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.06em; }
.task-route { display: flex; align-items: center; gap: 0.5rem; font-size: 1.1rem; font-weight: 650; flex-wrap: wrap; }
.task-route .arrow { color: var(--muted); font-weight: 400; }
.task-meta { display: flex; justify-content: space-between; gap: 0.6rem; font-size: 0.85rem; color: var(--text-2); }
.task.is-idle .task-route { font-size: 1rem; font-weight: 600; color: var(--text-2); }
.progress { height: 0.5rem; border-radius: 999px; background: var(--surface-3); overflow: hidden; }
.progress > span { display: block; height: 100%; border-radius: 999px; background: var(--accent); transition: width 1s ease; }

.robot-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; margin-top: auto; padding-top: 0.65rem; border-top: 1px solid var(--grid); }
.rstat-label { font-size: 0.78rem; color: var(--text-2); }
.rstat-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.battery { display: flex; align-items: center; gap: 0.7rem; font-size: 0.85rem; }
.battery-label { color: var(--text-2); }
.battery-track { flex: 1; height: 0.45rem; background: var(--surface-3); border-radius: 999px; overflow: hidden; }
.battery-fill { height: 100%; border-radius: 999px; background: var(--good); transition: width 0.6s ease; }
.battery-fill.low { background: var(--warning); }
.battery-fill.critical { background: var(--critical); }
.battery-val { font-weight: 650; font-variant-numeric: tabular-nums; min-width: 3rem; text-align: right; }

/* ---------- Recent deliveries ---------- */
.recent { display: flex; flex-direction: column; }
.rrow {
  display: grid; grid-template-columns: 5.2rem minmax(0, 1fr) auto 4.6rem;
  align-items: center; gap: 1rem;
  padding: 0.6rem 0.25rem; border-bottom: 1px solid var(--grid);
}
.rrow:last-child { border-bottom: 0; }
.rrow.flash { animation: flash 2.4s ease-out; border-radius: 0.5rem; }
@keyframes flash { from { background: var(--flash); } to { background: transparent; } }
.r-time { color: var(--text-2); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.r-main { min-width: 0; }
.r-route { font-weight: 650; display: flex; align-items: center; gap: 0.4rem; white-space: nowrap; overflow: hidden; }
.r-route .arrow { color: var(--muted); font-weight: 400; }
.r-sub { font-size: 0.8rem; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-dur { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; }
.empty { padding: 1.5rem 0; color: var(--text-2); text-align: center; }
.empty-state {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  padding: 1rem; color: var(--text-2); font-size: 0.95rem;
  border: 1px dashed var(--border); border-radius: 0.8rem;
}
.kpi-value.kpi-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 1.75rem; letter-spacing: -0.02em; padding: 0.55rem 0 0.35rem; }

.pill {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.18rem 0.6rem 0.18rem 0.45rem; border-radius: 999px;
  font-size: 0.8rem; font-weight: 650; white-space: nowrap;
  background: var(--surface-2); color: var(--text);
}
.pill svg { width: 0.95rem; height: 0.95rem; flex: none; fill: none; }
.pill.s-delivered { background: color-mix(in srgb, var(--good) 15%, transparent); }
.pill.s-delivered svg { stroke: var(--good); }
.pill.s-in_transit { background: var(--accent-soft); }
.pill.s-in_transit svg { stroke: var(--accent); }
.pill.s-failed { background: color-mix(in srgb, var(--critical) 17%, transparent); }
.pill.s-failed svg { stroke: var(--critical); }
.pill.s-queued svg, .pill.s-cancelled svg { stroke: var(--muted); }
.badge-stat {
  font-size: 0.68rem; font-weight: 750; letter-spacing: 0.06em;
  padding: 0.08rem 0.4rem; border-radius: 0.3rem; background: var(--critical); color: #fff;
}

/* ---------- Unit bars ---------- */
.hbars { display: flex; flex-direction: column; gap: 0.7rem; }
.hbar { display: grid; grid-template-columns: minmax(6rem, 8.5rem) 1fr 2.6rem; align-items: center; gap: 0.8rem; font-size: 0.92rem; }
.hbar-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hbar-track { height: 0.65rem; }
.hbar-fill { height: 100%; background: var(--accent); border-radius: 0 4px 4px 0; min-width: 2px; transition: width 0.6s ease; }
.hbar-value { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; }

/* ---------- Tooltip ---------- */
.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface-3); color: var(--text); border: 1px solid var(--border);
  border-radius: 0.55rem; padding: 0.5rem 0.7rem; font-size: 0.85rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3); white-space: nowrap;
}
.tooltip .tt-sub { color: var(--text-2); }

/* ---------- Responsive ---------- */
@media (max-width: 1199px) {
  .layout { grid-template-columns: 1fr; grid-template-areas: "chart" "robot" "recent" "units"; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 640px) {
  .app { padding: 1rem 1rem 1.5rem; gap: 1rem; }
  .kpis { gap: 0.75rem; }
  .kpi { padding: 1rem; }
  .kpi-value { font-size: 2.3rem; }
  .kpi-value small { font-size: 1.1rem; }
  .brand h1 { font-size: 1.35rem; }
  .topbar-right { width: 100%; justify-content: space-between; }
  .chart { height: 13rem; }
  .rrow { grid-template-columns: 4.4rem minmax(0, 1fr) auto; }
  .r-dur { display: none; }
  .panel { padding: 1rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
