/* ==========================================================================
   AGIBOT X2 Dashboard
   Design tokens first, then layout, then components.
   Series colours are the validated data-viz palette; chrome greys are neutral
   so nothing competes with the data.
   ========================================================================== */

:root {
  color-scheme: dark;

  /* Surfaces */
  --bg:          #0b0c0e;
  --surface:     #141619;
  --surface-2:   #1b1e22;
  --surface-3:   #23262b;
  --surface-hi:  #2b2f36;
  --border:      #262a30;
  --border-soft: #1e2126;

  /* Ink */
  --text:    #f2f4f7;
  --text-2:  #a4adb8;
  --text-3:  #6d7681;

  /* Categorical series - validated for CVD against a dark surface */
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;

  /* Status - reserved, never used for a series */
  --good:     #0ca30c;
  --warning:  #fab219;
  --serious:  #ec835a;
  --critical: #d03b3b;

  --accent: var(--series-1);
  --accent-soft: rgba(57, 135, 229, 0.14);
  --accent-line: rgba(57, 135, 229, 0.4);

  /* Chart chrome */
  --grid:     #22262c;
  --axis:     #333941;

  --radius:    12px;
  --radius-sm: 8px;
  --radius-lg: 18px;

  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px -12px rgba(0, 0, 0, .6);
  --shadow-lg: 0 20px 60px -20px rgba(0, 0, 0, .8);

  --rail-w: 216px;
  --topbar-h: 60px;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;

  --ease: cubic-bezier(.2, .7, .3, 1);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg:          #f4f5f7;
  --surface:     #ffffff;
  --surface-2:   #f7f8fa;
  --surface-3:   #eef0f4;
  --surface-hi:  #e4e7ec;
  --border:      #dfe3e9;
  --border-soft: #e9ecf1;

  --text:   #10131a;
  --text-2: #4d5663;
  --text-3: #7d8794;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --accent: var(--series-1);
  --accent-soft: rgba(42, 120, 214, 0.1);
  --accent-line: rgba(42, 120, 214, 0.35);

  --grid: #e8eaee;
  --axis: #cfd4dc;

  --shadow: 0 1px 2px rgba(16, 19, 26, .06), 0 8px 24px -14px rgba(16, 19, 26, .18);
  --shadow-lg: 0 24px 60px -24px rgba(16, 19, 26, .28);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body { overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
a { color: var(--accent); }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface-hi); border-radius: 8px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

/* ==========================================================================
   App shell
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: var(--rail-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas: "top top" "rail content";
  height: 100vh;
  height: 100dvh;
}

/* Grid and flex children default to min-width:auto, so their min-content width
   props the track open and the whole page scrolls sideways on a phone. Every
   shell region opts out and scrolls internally instead. */
.app > * { min-width: 0; }

/* -- Topbar -------------------------------------------------------------- */

.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; flex-shrink: 0; }

.brand-mark {
  width: 34px; height: 34px;
  border-radius: 10px;
  background: linear-gradient(150deg, var(--series-1), #1f4f8f);
  display: grid; place-items: center;
  flex-shrink: 0;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.2);
}
.brand-dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(255,255,255,.25);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text strong { font-size: 14px; letter-spacing: -0.01em; }
.brand-text span { font-size: 11px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar-status {
  display: flex; align-items: center; gap: 8px;
  flex: 1 1 0; min-width: 0;
  overflow-x: auto; overflow-y: hidden;
  scrollbar-width: none;
}
.topbar-status::-webkit-scrollbar { display: none; }

.pill {
  display: inline-flex; align-items: center; gap: 7px;
  height: 30px; padding: 0 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
  transition: background .15s var(--ease);
}
.pill-key { color: var(--text-3); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.pill-label { font-weight: 550; font-variant-numeric: tabular-nums; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); flex-shrink: 0; }
.pill .dot[data-state="online"] { background: var(--good); box-shadow: 0 0 0 3px rgba(12,163,12,.18); }
.pill .dot[data-state="sim"]    { background: var(--warning); box-shadow: 0 0 0 3px rgba(250,178,25,.18); }
.pill .dot[data-state="offline"]{ background: var(--critical); box-shadow: 0 0 0 3px rgba(208,59,59,.18); }
.pill[data-level="warning"] { border-color: rgba(250,178,25,.4); }
.pill[data-level="critical"]{ border-color: rgba(208,59,59,.5); background: rgba(208,59,59,.1); }

.topbar-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .15s var(--ease), border-color .15s var(--ease);
}
.icon-btn:hover { background: var(--surface-3); border-color: var(--surface-hi); }
.ico { width: 18px; height: 18px; fill: currentColor; display: block; }

.btn-stop {
  display: inline-flex; align-items: center; gap: 8px;
  height: 34px; padding: 0 16px;
  background: var(--critical);
  color: #fff;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-weight: 700; font-size: 12px; letter-spacing: .08em;
  cursor: pointer;
  transition: filter .15s var(--ease), transform .08s var(--ease);
}
.btn-stop:hover { filter: brightness(1.12); }
.btn-stop:active { transform: scale(.97); }
.btn-stop .ico { width: 16px; height: 16px; }

/* -- Rail ---------------------------------------------------------------- */

.rail {
  grid-area: rail;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 12px 10px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
}

.rail-group {
  font-size: 10px; text-transform: uppercase; letter-spacing: .1em;
  color: var(--text-3); font-weight: 600;
  padding: 14px 10px 6px;
}
.rail-group:first-child { padding-top: 4px; }

.rail-item {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 11px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  background: transparent;
  border: 0; width: 100%;
  cursor: pointer;
  text-align: left;
  font-size: 13px; font-weight: 500;
  position: relative;
  transition: background .13s var(--ease), color .13s var(--ease);
}
.rail-item .ico { width: 17px; height: 17px; flex-shrink: 0; opacity: .85; }
.rail-item:hover { background: var(--surface-2); color: var(--text); }
.rail-item[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 600;
}
.rail-item[aria-current="page"] .ico { opacity: 1; color: var(--accent); }
.rail-item[aria-current="page"]::before {
  content: ""; position: absolute; left: -10px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 20px; border-radius: 0 3px 3px 0; background: var(--accent);
}
.rail-badge {
  margin-left: auto;
  font-size: 10px; font-weight: 700;
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px;
  background: var(--surface-3); color: var(--text-2);
  display: grid; place-items: center;
  font-variant-numeric: tabular-nums;
}
.rail-badge[data-tone="warn"] { background: rgba(250,178,25,.2); color: var(--warning); }
.rail-badge[data-tone="bad"]  { background: rgba(208,59,59,.2); color: var(--critical); }

/* -- Content ------------------------------------------------------------- */

.content {
  grid-area: content;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 22px 60px;
  scroll-behavior: smooth;
}
.content:focus { outline: none; }

.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; flex-wrap: wrap;
  margin-bottom: 18px;
}
.page-head h1 { font-size: 22px; letter-spacing: -0.02em; }
.page-head p { margin: 4px 0 0; color: var(--text-2); font-size: 13px; max-width: 68ch; }
.page-head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ==========================================================================
   Layout primitives
   ========================================================================== */

.grid { display: grid; gap: 14px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid.cols-auto { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
/* A narrow leading column beside a flexible one. Collapses rather than
   overflowing, which a fixed two-track template would do on a phone. */
.grid.split { grid-template-columns: minmax(250px, 330px) 1fr; }
.grid.pair  { grid-template-columns: 1fr 1fr; }
@media (max-width: 900px) {
  .grid.split, .grid.pair { grid-template-columns: 1fr; }
}
.stack { display: flex; flex-direction: column; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }
.span-2 { grid-column: span 2; }
@media (max-width: 820px) { .span-2 { grid-column: span 1; } }

/* ==========================================================================
   Card
   ========================================================================== */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex; flex-direction: column;
}
.card-head {
  display: flex; align-items: center; gap: 10px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border-soft);
}
.card-head h3 { font-size: 13px; letter-spacing: .01em; }
.card-head .sub { font-size: 11px; color: var(--text-3); }
.card-head-actions { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.card-body { padding: 16px; flex: 1; }
.card-body.flush { padding: 0; }
.card-foot {
  padding: 10px 16px;
  border-top: 1px solid var(--border-soft);
  background: var(--surface-2);
  font-size: 12px; color: var(--text-2);
}

/* ==========================================================================
   Buttons and inputs
   ========================================================================== */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 34px; padding: 0 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 550;
  cursor: pointer;
  white-space: nowrap;
  transition: background .13s var(--ease), border-color .13s var(--ease), transform .08s var(--ease);
}
.btn:hover:not(:disabled) { background: var(--surface-3); border-color: var(--surface-hi); }
.btn:active:not(:disabled) { transform: scale(.98); }
.btn:disabled { opacity: .42; cursor: not-allowed; }
.btn .ico { width: 15px; height: 15px; }

.btn-primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.12); background: var(--accent); }
.btn-danger { background: var(--critical); border-color: transparent; color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(1.12); background: var(--critical); }
.btn-warn { background: rgba(250,178,25,.16); border-color: rgba(250,178,25,.4); color: var(--warning); }
.btn-warn:hover:not(:disabled) { background: rgba(250,178,25,.24); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.btn-sm { height: 28px; padding: 0 10px; font-size: 12px; }
.btn-block { width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label { font-size: 12px; font-weight: 550; color: var(--text-2); }
/* Not scoped to .field - hints are also used standalone beside toggles. */
.hint { font-size: 11px; color: var(--text-3); line-height: 1.45; }

.input, .select, .textarea {
  height: 34px;
  padding: 0 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  width: 100%;
  transition: border-color .13s var(--ease), background .13s var(--ease);
}
.textarea { height: auto; min-height: 78px; padding: 9px 11px; resize: vertical; line-height: 1.5; }
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--accent); background: var(--surface);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.input::placeholder, .textarea::placeholder { color: var(--text-3); }
.select { appearance: none; cursor: pointer; padding-right: 30px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: right 13px center, right 8px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
.input[type="color"] { padding: 3px; height: 34px; cursor: pointer; }

/* Toggle */
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  width: 38px; height: 21px; border-radius: 999px;
  background: var(--surface-hi); border: 1px solid var(--border);
  position: relative; flex-shrink: 0;
  transition: background .16s var(--ease);
}
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 15px; height: 15px; border-radius: 50%;
  background: var(--text-2);
  transition: transform .16s var(--ease), background .16s var(--ease);
}
.switch input:checked + .switch-track { background: var(--accent); border-color: transparent; }
.switch input:checked + .switch-track::after { transform: translateX(17px); background: #fff; }
.switch input:focus-visible + .switch-track { box-shadow: 0 0 0 3px var(--accent-soft); }
.switch-label { font-size: 13px; }

/* Range */
.range { display: flex; align-items: center; gap: 12px; }
.range input[type="range"] {
  flex: 1; appearance: none; height: 4px; border-radius: 2px;
  background: var(--surface-hi); cursor: pointer; min-width: 60px;
}
.range input[type="range"]::-webkit-slider-thumb {
  appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--surface);
  box-shadow: 0 1px 3px rgba(0,0,0,.4); cursor: grab;
}
.range input[type="range"]::-webkit-slider-thumb:active { cursor: grabbing; }
.range input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--surface); cursor: grab;
}
.range-value {
  min-width: 62px; text-align: right;
  font-variant-numeric: tabular-nums; font-size: 12px;
  color: var(--text-2); font-family: var(--mono);
}

/* Segmented control */
.segmented {
  display: inline-flex; padding: 3px; gap: 2px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.segmented button {
  border: 0; background: transparent; color: var(--text-2);
  padding: 5px 12px; border-radius: 6px; cursor: pointer;
  font-size: 12px; font-weight: 550;
  transition: background .13s var(--ease), color .13s var(--ease);
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface-hi); color: var(--text); }

/* ==========================================================================
   Stat tiles & meters
   ========================================================================== */

.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 15px;
  display: flex; flex-direction: column; gap: 3px;
  min-width: 0;
}
.stat-label {
  font-size: 11px; color: var(--text-3);
  text-transform: uppercase; letter-spacing: .07em; font-weight: 600;
}
.stat-value {
  font-size: 25px; font-weight: 600; letter-spacing: -.02em;
  line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.stat-value .unit { font-size: 13px; font-weight: 500; color: var(--text-3); margin-left: 3px; }
.stat-sub { font-size: 11.5px; color: var(--text-3); }
.stat-spark { margin-top: 6px; height: 30px; }
.stat[data-tone="good"] .stat-value { color: var(--good); }
.stat[data-tone="warning"] .stat-value { color: var(--warning); }
.stat[data-tone="critical"] .stat-value { color: var(--critical); }

.hero {
  font-size: 52px; font-weight: 600; letter-spacing: -.03em; line-height: 1;
}
.hero .unit { font-size: 20px; color: var(--text-3); margin-left: 4px; font-weight: 500; }

.meter { height: 7px; border-radius: 4px; background: var(--surface-hi); overflow: hidden; }
.meter-fill { height: 100%; border-radius: 4px; background: var(--accent); transition: width .35s var(--ease), background .3s var(--ease); }
.meter-fill[data-tone="good"]     { background: var(--good); }
.meter-fill[data-tone="warning"]  { background: var(--warning); }
.meter-fill[data-tone="critical"] { background: var(--critical); }

/* ==========================================================================
   Badges, chips, notes
   ========================================================================== */

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  height: 21px; padding: 0 8px;
  border-radius: 6px;
  font-size: 11px; font-weight: 600;
  background: var(--surface-3); color: var(--text-2);
  white-space: nowrap;
}
.badge[data-tone="good"]     { background: rgba(12,163,12,.15);  color: var(--good); }
.badge[data-tone="warning"]  { background: rgba(250,178,25,.15); color: var(--warning); }
.badge[data-tone="serious"]  { background: rgba(236,131,90,.15); color: var(--serious); }
.badge[data-tone="critical"] { background: rgba(208,59,59,.16);  color: var(--critical); }
.badge[data-tone="accent"]   { background: var(--accent-soft);   color: var(--accent); }

.note {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 12.5px; line-height: 1.5;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-2);
}
.note strong { color: var(--text); font-weight: 600; }
.note-icon { flex-shrink: 0; font-size: 14px; line-height: 1.3; }
.note[data-level="critical"] { background: rgba(208,59,59,.08); border-color: rgba(208,59,59,.3); }
.note[data-level="warning"]  { background: rgba(250,178,25,.07); border-color: rgba(250,178,25,.28); }
.note[data-level="info"]     { background: var(--accent-soft); border-color: var(--accent-line); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 7px 16px; font-size: 12.5px; align-items: baseline; }
.kv dt { color: var(--text-3); white-space: nowrap; }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 12px; word-break: break-all; }

.mono { font-family: var(--mono); font-size: 12px; }
.muted { color: var(--text-3); }
.nowrap { white-space: nowrap; }

/* ==========================================================================
   Tables
   ========================================================================== */

.table-wrap { overflow-x: auto; }
table.data {
  width: 100%; border-collapse: collapse; font-size: 12.5px;
}
table.data th {
  text-align: left; font-weight: 600; font-size: 11px;
  text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-3);
  padding: 9px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  position: sticky; top: 0; z-index: 1;
  white-space: nowrap;
}
table.data td {
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-soft);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.num { text-align: right; font-family: var(--mono); font-size: 11.5px; }
table.data td.name { font-weight: 500; white-space: normal; }

/* ==========================================================================
   Joystick
   ========================================================================== */

.joystick-wrap { display: flex; gap: 22px; flex-wrap: wrap; align-items: flex-start; }

.joystick {
  width: 200px; height: 200px;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 42%, var(--surface-3), var(--surface-2) 68%);
  border: 1px solid var(--border);
  position: relative;
  touch-action: none;
  cursor: grab;
  flex-shrink: 0;
  user-select: none;
}
.joystick:active { cursor: grabbing; }
.joystick::before, .joystick::after {
  content: ""; position: absolute; background: var(--border); pointer-events: none;
}
.joystick::before { left: 12%; right: 12%; top: 50%; height: 1px; }
.joystick::after { top: 12%; bottom: 12%; left: 50%; width: 1px; }
.joystick-ring {
  position: absolute; inset: 22%;
  border: 1px dashed var(--border); border-radius: 50%; pointer-events: none;
}
.joystick-knob {
  position: absolute; width: 56px; height: 56px;
  left: 50%; top: 50%; margin: -28px 0 0 -28px;
  border-radius: 50%;
  background: linear-gradient(160deg, var(--accent), #1f4f8f);
  box-shadow: 0 4px 14px rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.24);
  pointer-events: none;
  transition: transform .05s linear;
}
.joystick[data-active="false"] .joystick-knob { transition: transform .22s var(--ease); }
.joystick-label {
  position: absolute; font-size: 10px; color: var(--text-3);
  text-transform: uppercase; letter-spacing: .08em; pointer-events: none; font-weight: 600;
}
.joystick-label.n { top: 8px; left: 50%; transform: translateX(-50%); }
.joystick-label.s { bottom: 8px; left: 50%; transform: translateX(-50%); }
.joystick-label.w { left: 8px; top: 50%; transform: translateY(-50%); }
.joystick-label.e { right: 8px; top: 50%; transform: translateY(-50%); }

.joystick-readout { display: flex; flex-direction: column; gap: 12px; min-width: 190px; flex: 1; }

/* Key hints */
.keys { display: flex; gap: 5px; flex-wrap: wrap; }
kbd {
  display: inline-grid; place-items: center;
  min-width: 22px; height: 22px; padding: 0 6px;
  background: var(--surface-3); border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  font-family: var(--mono); font-size: 11px; color: var(--text-2);
}

/* ==========================================================================
   Preset / emoji / swatch grids
   ========================================================================== */

.tile-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(126px, 1fr)); gap: 8px; }

.tile {
  display: flex; flex-direction: column; gap: 3px;
  padding: 11px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer; text-align: left;
  transition: background .13s var(--ease), border-color .13s var(--ease), transform .08s var(--ease);
}
.tile:hover:not(:disabled) { background: var(--surface-3); border-color: var(--accent-line); }
.tile:active:not(:disabled) { transform: scale(.97); }
.tile:disabled { opacity: .4; cursor: not-allowed; }
.tile-name { font-size: 12.5px; font-weight: 550; }
.tile-meta { font-size: 10.5px; color: var(--text-3); font-family: var(--mono); }
.tile[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }

.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 7px; }
.emoji-tile {
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 11px 6px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  transition: background .13s var(--ease), border-color .13s var(--ease), transform .08s var(--ease);
}
.emoji-tile:hover { background: var(--surface-3); border-color: var(--accent-line); }
.emoji-tile:active { transform: scale(.95); }
.emoji-glyph { font-size: 24px; line-height: 1; }
.emoji-name { font-size: 10px; color: var(--text-3); text-align: center; line-height: 1.25; }
.emoji-tile[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }

.swatches { display: flex; gap: 7px; flex-wrap: wrap; }
.swatch {
  width: 34px; height: 34px; border-radius: 9px;
  border: 1px solid var(--border);
  cursor: pointer; position: relative;
  transition: transform .1s var(--ease), box-shadow .13s var(--ease);
}
.swatch:hover { transform: scale(1.08); }
.swatch[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }

/* ==========================================================================
   Charts
   ========================================================================== */

.chart { width: 100%; display: block; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis-line { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick-text { fill: var(--text-3); font-size: 10px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.chart .series-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .series-area { stroke: none; opacity: .1; }
.chart .end-dot { stroke: var(--surface); stroke-width: 2; }
.chart .crosshair { stroke: var(--text-3); stroke-width: 1; stroke-dasharray: 3 3; pointer-events: none; }
.chart .end-label { fill: var(--text-2); font-size: 10.5px; font-family: var(--font); font-variant-numeric: tabular-nums; }

.legend { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 8px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--text-2); }
.legend-key { width: 11px; height: 2.5px; border-radius: 2px; flex-shrink: 0; }

.chart-tip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--surface-3);
  border: 1px solid var(--surface-hi);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 11.5px;
  box-shadow: var(--shadow-lg);
  min-width: 118px;
  opacity: 0; transition: opacity .1s;
}
.chart-tip[data-show="true"] { opacity: 1; }
.chart-tip .tip-time { color: var(--text-3); font-size: 10.5px; margin-bottom: 5px; }
.chart-tip .tip-row { display: flex; align-items: center; gap: 7px; justify-content: space-between; }
.chart-tip .tip-row b { font-variant-numeric: tabular-nums; font-weight: 600; }

/* Gauge / attitude. The horizon plate is drawn oversized and clipped, so this
   svg must not inherit the overflow:visible that line charts need for labels. */
.attitude { display: grid; place-items: center; }
.attitude .chart { overflow: hidden; }

/* ==========================================================================
   Camera
   ========================================================================== */

.cam {
  position: relative;
  background: #000;
  border-radius: var(--radius-sm);
  overflow: hidden;
  aspect-ratio: 4 / 3;
  display: grid; place-items: center;
}
.cam img { width: 100%; height: 100%; object-fit: cover; display: block; image-rendering: auto; }
.cam-placeholder { color: var(--text-3); font-size: 12px; text-align: center; padding: 20px; line-height: 1.6; }
.cam-overlay {
  position: absolute; inset: auto 0 0 0;
  padding: 8px 10px;
  background: linear-gradient(transparent, rgba(0,0,0,.75));
  display: flex; justify-content: space-between; align-items: center;
  font-size: 11px; color: #e8ecf2;
  font-family: var(--mono);
}
.cam-live { display: inline-flex; align-items: center; gap: 5px; }
.cam-live::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--critical);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .3 } }

/* ==========================================================================
   Console / log
   ========================================================================== */

.log {
  font-family: var(--mono); font-size: 11.5px;
  max-height: 460px; overflow-y: auto;
  background: var(--bg);
}
.log-row {
  display: grid; grid-template-columns: 74px 62px 100px 1fr;
  gap: 10px; padding: 5px 14px;
  border-bottom: 1px solid var(--border-soft);
  align-items: baseline;
}
.log-row:hover { background: var(--surface-2); }
.log-time { color: var(--text-3); }
.log-level { font-weight: 700; font-size: 10px; text-transform: uppercase; letter-spacing: .05em; }
.log-level[data-level="info"] { color: var(--series-1); }
.log-level[data-level="warn"] { color: var(--warning); }
.log-level[data-level="error"] { color: var(--critical); }
.log-source { color: var(--text-3); overflow: hidden; text-overflow: ellipsis; }
.log-msg { color: var(--text-2); white-space: pre-wrap; word-break: break-word; }

/* ==========================================================================
   Toasts & modal
   ========================================================================== */

.toasts {
  position: fixed; right: 18px; bottom: 18px; z-index: 100;
  display: flex; flex-direction: column-reverse; gap: 9px;
  max-width: min(400px, calc(100vw - 36px));
}
.toast {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 11px 14px;
  background: var(--surface-3);
  border: 1px solid var(--surface-hi);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  font-size: 12.5px;
  animation: toast-in .22s var(--ease);
}
.toast[data-tone="good"]     { border-left-color: var(--good); }
.toast[data-tone="warning"]  { border-left-color: var(--warning); }
.toast[data-tone="critical"] { border-left-color: var(--critical); }
.toast.leaving { animation: toast-out .18s var(--ease) forwards; }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; margin-bottom: 2px; }
.toast-msg { color: var(--text-2); word-break: break-word; }
@keyframes toast-in  { from { opacity: 0; transform: translateY(10px) scale(.97) } }
@keyframes toast-out { to   { opacity: 0; transform: translateX(20px) } }

.modal-backdrop {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.6);
  display: grid; place-items: center;
  padding: 20px;
  backdrop-filter: blur(3px);
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  max-width: 440px; width: 100%;
  box-shadow: var(--shadow-lg);
  animation: toast-in .2s var(--ease);
}
.modal h2 { font-size: 16px; margin-bottom: 8px; }
.modal p { color: var(--text-2); font-size: 13px; margin: 0 0 20px; line-height: 1.55; }
.modal-actions { display: flex; gap: 9px; justify-content: flex-end; }

/* ==========================================================================
   Offline gate - covers everything while the robot is unreachable
   ========================================================================== */

.gate {
  position: fixed; inset: 0; z-index: 300;
  display: grid; place-items: center;
  padding: 24px;
  background: var(--bg);
  animation: gate-in .25s var(--ease);
}
.gate[hidden] { display: none; }
@keyframes gate-in { from { opacity: 0 } }

.gate-card {
  max-width: 520px; width: 100%;
  text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
}
.gate-icon { color: var(--warning); line-height: 0; }
.gate-icon svg { animation: gate-pulse 2.6s ease-in-out infinite; }
@keyframes gate-pulse { 0%, 100% { opacity: .85 } 50% { opacity: .38 } }

.gate-card h1 { font-size: 26px; letter-spacing: -.02em; }
.gate-card > p {
  margin: 0; color: var(--text-2); font-size: 14px; line-height: 1.6; max-width: 44ch;
}

.gate-status {
  display: inline-flex; align-items: center; gap: 10px;
  padding: 9px 16px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12.5px; color: var(--text-2);
}
.gate-spinner {
  width: 13px; height: 13px; flex-shrink: 0;
  border: 2px solid var(--surface-hi);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}

.gate-facts {
  display: grid; grid-template-columns: auto auto; gap: 6px 18px;
  margin: 0; font-size: 12px;
  padding: 14px 20px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: left;
}
.gate-facts dt { color: var(--text-3); }
.gate-facts dd { margin: 0; font-family: var(--mono); font-size: 11.5px; text-align: right; }

.gate-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.gate-address {
  width: 100%; max-width: 420px;
  display: flex; flex-direction: column; gap: 7px;
  padding: 14px 16px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: left;
}
.gate-address[hidden] { display: none; }
.gate-address > label { font-size: 12px; font-weight: 550; color: var(--text-2); }
.gate-address-row { display: flex; gap: 8px; }
.gate-address-row .input { flex: 1; min-width: 0; }

.gate-results {
  width: 100%; max-width: 460px;
  display: flex; flex-direction: column; gap: 7px;
  text-align: left;
}
.gate-results[hidden] { display: none; }
.gate-result {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 13px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
}
.gate-result .host { font-family: var(--mono); font-size: 12.5px; }
.gate-result .spacer { flex: 1; }
/* Needs to out-specify `.gate-card > p` above, which would otherwise make the
   hint as prominent as the main message. */
.gate-card > p.gate-hint {
  margin: 0; font-size: 11.5px; color: var(--text-3); max-width: 46ch; line-height: 1.6;
}

/* While the gate is up the shell behind it must not be reachable by keyboard. */
.app[inert] { filter: blur(2px); }

/* ==========================================================================
   Empty / loading
   ========================================================================== */

.empty {
  padding: 44px 20px; text-align: center; color: var(--text-3); font-size: 13px;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.empty-title { font-size: 14px; font-weight: 600; color: var(--text-2); }

.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer { to { background-position: -200% 0 } }

.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg) } }

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 980px) {
  :root { --rail-w: 60px; }
  .rail-item span:not(.rail-badge) { display: none; }
  .rail-item { justify-content: center; padding: 11px 0; }
  .rail-group { display: none; }
  .rail-item .rail-badge { position: absolute; top: 3px; right: 3px; margin: 0; }
  .brand-text { display: none; }
}

@media (max-width: 720px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: var(--topbar-h) 1fr auto;
    grid-template-areas: "top" "content" "rail";
  }
  .rail {
    flex-direction: row;
    border-right: 0; border-top: 1px solid var(--border);
    overflow-x: auto; overflow-y: hidden;
    padding: 7px 8px;
    padding-bottom: max(7px, env(safe-area-inset-bottom));
  }
  .rail-item { flex-direction: column; gap: 3px; min-width: 56px; font-size: 10px; padding: 7px 4px; }
  .rail-item span:not(.rail-badge) { display: block; font-size: 9.5px; }
  .rail-item[aria-current="page"]::before { display: none; }
  .content { padding: 14px 13px 28px; }
  .topbar { padding: 0 11px; gap: 10px; }
  .topbar-status { gap: 5px; }
  .pill[data-optional] { display: none; }
  .page-head h1 { font-size: 19px; }
  .toasts { right: 10px; left: 10px; bottom: 78px; max-width: none; }
  .joystick { width: 172px; height: 172px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}
