:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15181e;
  --muted: #5d6573;
  --border: #dde1e7;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --idle: #6b7280;
  --armed: #16a34a;
  --l1: #ca8a04;
  --l2: #ea580c;
  --l3: #dc2626;
  --silent: #7c3aed;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(0 0 0 / 5%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --text: #e8eaee;
    --muted: #9aa3b2;
    --border: #2a2f3a;
    --accent: #4c8dff;
    --idle: #8b93a1;
    --armed: #22c55e;
    --l1: #eab308;
    --l2: #f97316;
    --l3: #ef4444;
    --silent: #a78bfa;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.35rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: .85rem; }
.error { color: var(--l3); min-height: 1.2em; margin: 0; font-size: .9rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }

/* buttons */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 10px; padding: 10px 16px; font: inherit; font-weight: 600; cursor: pointer; min-height: 44px;
}
.btn:hover { filter: brightness(.97); }
.btn:focus-visible, .chip:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; }
.btn.danger-ghost { background: transparent; color: var(--l3); border-color: color-mix(in srgb, var(--l3) 40%, var(--border)); }
.btn.small { min-height: 34px; padding: 6px 12px; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: default; }

/* login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 12px; text-align: center; }
.login-card label { display: grid; gap: 6px; text-align: left; font-weight: 600; }
input {
  font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); min-height: 44px; width: 100%;
}

/* app bar: top bar + tabs, sticky together */
.appbar { position: sticky; top: 0; z-index: 1000; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 1001; background: var(--bg); }
.topbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px max(16px, env(safe-area-inset-right)) 6px max(16px, env(safe-area-inset-left));
  padding-top: max(10px, env(safe-area-inset-top)); }
.state-pill { --tone: var(--idle); font-weight: 700; font-size: .85rem; padding: 5px 12px; border-radius: 999px;
  color: var(--tone); background: color-mix(in srgb, var(--tone) 14%, transparent); white-space: nowrap; }
.state-pill[data-tone="armed"] { --tone: var(--armed); }
.state-pill[data-tone="l1"] { --tone: var(--l1); }
.state-pill[data-tone="l2"] { --tone: var(--l2); }
.state-pill[data-tone="l3"] { --tone: var(--l3); }
.state-pill[data-tone="silent"] { --tone: var(--silent); }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  padding: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left)); }
.tabs a { padding: 8px 14px 10px; color: var(--muted); text-decoration: none; font-weight: 600; font-size: .92rem;
  border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.on { color: var(--text); border-bottom-color: var(--accent); }
.tabs a:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 6px; }
@media (max-width: 560px) { .link-pill { display: none; } .vehicle { display: none; } }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; margin-right: auto; }
.link-pill { order: 3; }
.vehicle { font-weight: 500; color: var(--muted); font-size: .9rem; }
.vehicle:not(:empty)::before { content: "· "; }
.pill { font-size: .8rem; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.ok { color: var(--armed); background: color-mix(in srgb, var(--armed) 14%, transparent); }
.pill.bad { color: var(--l3); background: color-mix(in srgb, var(--l3) 14%, transparent); }

.banner { margin: 12px 16px 0; padding: 12px 14px; border-radius: 12px; font-weight: 600;
  background: color-mix(in srgb, var(--l3) 14%, var(--surface)); color: var(--l3); border: 1px solid color-mix(in srgb, var(--l3) 35%, transparent); }

/* views: one tab at a time, each with its own grid */
.view { display: grid; gap: 16px; align-items: start; grid-template-columns: minmax(0, 1fr);
  padding: 16px max(16px, env(safe-area-inset-right)) 24px max(16px, env(safe-area-inset-left)); }
.banner + .view { padding-top: 12px; }
@media (min-width: 900px) {
  /* one row for the bar: brand, tabs, state and actions */
  .appbar { display: flex; align-items: center; gap: 6px 10px;
    padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left)); }
  .topbar { display: contents; }
  .brand { margin-right: 8px; }
  .tabs { order: 1; margin-right: auto; padding: 0; align-self: stretch; }
  .tabs a { padding: 16px 14px 14px; }
  .state-pill, #top-action, .link-pill, #logout { order: 2; }
  .view-home { grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); grid-template-rows: auto 1fr;
    grid-template-areas: "status sensors" "events sensors"; }
  .view-home .status { grid-area: status; }
  .view-home .sensors-card { grid-area: sensors; }
  .view-home .events-card { grid-area: events; }
  .view-home .map-card { grid-area: map; }
}

/* activity charts */
.activity-card .card-head { flex-wrap: wrap; }
.charts { display: grid; gap: 12px; grid-template-columns: minmax(0, 1fr); }
.charts .summary { margin: 0; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.charts .summary dd { font-size: 1.35rem; }
@media (min-width: 700px) { .charts { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.chart { margin: 0; background: var(--surface-2); border-radius: 10px; padding: 8px 10px 4px; min-width: 0; }
.chart figcaption { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: .8rem; }
.chart-title { font-weight: 700; cursor: help; }
.chart-value { color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; overflow-wrap: anywhere; }
.chart canvas { display: block; width: 100%; height: 160px; touch-action: pan-y; }
@media (min-width: 900px) { .chart canvas { height: 210px; } }
.legend { margin-top: 10px; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; }
.swatch { display: inline-block; width: 14px; height: 10px; border-radius: 3px; margin-left: 6px; }
.swatch.armed { background: color-mix(in srgb, var(--armed) 25%, transparent); }
.swatch.alert { width: 2px; height: 12px; background: var(--l3); }

/* status */
.status { --tone: var(--idle); border-top: 4px solid var(--tone); }
.status[data-tone="armed"] { --tone: var(--armed); }
.status[data-tone="l1"] { --tone: var(--l1); }
.status[data-tone="l2"] { --tone: var(--l2); }
.status[data-tone="l3"] { --tone: var(--l3); }
.status[data-tone="silent"] { --tone: var(--silent); }
.status-head { display: flex; gap: 12px; align-items: center; }
.status-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--tone); flex: none;
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--tone) 18%, transparent); }
.status[data-tone="l3"] .status-dot { animation: pulse 1s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 12px color-mix(in srgb, var(--tone) 5%, transparent); } }
@media (prefers-reduced-motion: reduce) { .status-dot { animation: none !important; } }

.levels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin: 16px 0 12px; }
.levels span { text-align: center; font-size: .75rem; font-weight: 600; padding: 6px 2px; border-radius: 8px;
  background: var(--surface-2); color: var(--muted); }
.levels span.on { color: #fff; }
.levels span.on[data-l="0"] { background: var(--armed); }
.levels span.on[data-l="1"] { background: var(--l1); }
.levels span.on[data-l="2"] { background: var(--l2); }
.levels span.on[data-l="3"] { background: var(--l3); }

.actuators { list-style: none; padding: 0; margin: 0 0 16px; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-size: .78rem; color: var(--muted); }
.actuators li { background: var(--surface-2); border-radius: 8px; padding: 6px 10px; font-weight: 600; }
.actuators b { display: block; color: var(--text); font-weight: 600; font-size: .92rem; }
.actuators li.active b { color: var(--l3); }
.controls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

/* map */
#map { height: max(360px, calc(100dvh - 230px)); border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.notice { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); color: var(--muted); font-size: .9rem; }
.pos-summary { margin-top: 12px; }
.map-card .card-head h3 { margin-right: auto; }
.map-open { display: none; }
.map-card.mini .map-open { display: inline; }
.map-card.mini .notice { font-size: .82rem; }
a { color: var(--accent); }
.pos-summary:empty { display: none; }

/* events */
.filters { display: flex; gap: 6px; }
.chip { border: 1px solid var(--border); background: transparent; color: var(--muted); border-radius: 999px;
  padding: 4px 10px; font: inherit; font-size: .8rem; font-weight: 600; cursor: pointer; }
.chip.on { background: var(--surface-2); color: var(--text); }
.events { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; align-content: start; max-height: 480px; overflow: auto; }
.events li { display: grid; grid-template-columns: 10px 1fr auto; gap: 10px; align-items: start; padding: 10px 4px; border-bottom: 1px solid var(--border); }
.events li:last-child { border-bottom: 0; }
.events .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; background: var(--idle); }
.events li[data-p="3"] .dot { background: var(--l1); }
.events li[data-p="4"] .dot { background: var(--l2); }
.events li[data-p="5"] .dot { background: var(--l3); }
.events li[data-silent="1"] .dot { background: var(--silent); }
.events li[data-kind="command"] .dot { background: var(--accent); }
.events li[data-kind="state"] .dot { background: var(--armed); }
.events li[data-kind="node"] .dot { background: var(--silent); }
.events .msg { overflow-wrap: anywhere; }
.events .meta { color: var(--muted); font-size: .78rem; }
.events time { color: var(--muted); font-size: .78rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.events li.new { animation: flash 1.5s ease-out; }
@keyframes flash { from { background: color-mix(in srgb, var(--accent) 15%, transparent); } }
.empty { color: var(--muted); padding: 12px 4px; }
.events li.empty { display: block; }

/* sensor node */
.parts { list-style: none; padding: 0; margin: 8px 0 12px; display: flex; flex-wrap: wrap; gap: 6px; }
.parts li { font-size: .78rem; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.parts li.ok { color: var(--armed); background: color-mix(in srgb, var(--armed) 14%, transparent); }
.parts li.bad { color: var(--l3); background: color-mix(in srgb, var(--l3) 14%, transparent); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin: 0 0 12px; }
.tiles div { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; min-width: 0; display: flex; flex-direction: column; }
.tiles dt { font-size: .75rem; color: var(--muted); font-weight: 600; }
.tiles dd { margin: 2px 0 0; font-size: 1.15rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tiles .sub { display: block; font-size: .72rem; font-weight: 500; color: var(--muted); overflow-wrap: anywhere; }
.tiles .spark { display: block; width: 100%; height: 30px; margin-top: auto; padding-top: 4px; }
.tiles div.hot { outline: 2px solid var(--l2); animation: flash 1.5s ease-out; }
.sensors-card .tiles { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.summary { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.info { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); margin: 0; }
.info dd { font-size: 1rem; overflow-wrap: anywhere; }
.node-col { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); }
.phones-card .controls { grid-template-columns: 1fr 1fr; margin-top: 12px; }
.actions { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.actions li { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.actions li > div { display: grid; gap: 2px; min-width: 0; }
.actions .btn { flex: none; min-width: 108px; }
.enroll { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; font-size: .88rem;
  background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); }

/* lab */
.scenarios { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.scenarios .btn { min-height: 34px; padding: 6px 10px; font-size: .8rem; font-weight: 600; }
.lab-card { align-self: stretch; }
.lab-log:empty::before { content: "Pulsa un escenario para simularlo; aquí verás lo que publica."; }
.lab-log { margin: 0; background: var(--surface-2); border-radius: 10px; padding: 10px; font-size: .75rem;
  max-height: 220px; overflow: auto; white-space: pre-wrap; color: var(--muted); }

/* dialog & toast */
dialog { border: 1px solid var(--border); color: var(--text); width: min(340px, calc(100% - 32px)); }
dialog::backdrop { background: rgb(0 0 0 / 45%); }
#pin-form { display: grid; gap: 10px; }
#pin { font-size: 1.4rem; letter-spacing: .4em; text-align: center; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast { position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 2000;
  max-width: calc(100% - 32px); font-size: .9rem; box-shadow: var(--shadow); }

/* Wide and tall enough: every tab fills the window, and the cards stretch instead of
   leaving the page empty below them. */
@media (min-width: 1280px) and (min-height: 700px) {
  .view { height: calc(100dvh - var(--appbar-h, 53px)); min-height: 640px; align-items: stretch; }
  .view > .card, .lab-card { display: flex; flex-direction: column; min-height: 0; }

  /* Inicio: state + map | sensors | events */
  .view-home { grid-template-rows: auto minmax(0, 1fr); }
  .view-home.with-map { grid-template-columns: minmax(340px, 1fr) minmax(0, 1.5fr) minmax(320px, 1fr);
    grid-template-areas: "status sensors events" "map sensors events"; }
  .view-home.with-map .pos-summary { display: none; }
  .status h2 { font-size: 1.6rem; }
  .sensors-card .tiles { flex: 1; min-height: 0; grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(3, minmax(0, 1fr)) auto; }  /* last row: phone and button, no chart */
  .sensors-card .tiles dd { font-size: 1.35rem; }
  .sensors-card .spark { flex: 1 1 30px; height: 0; min-height: 30px; margin-top: 6px; padding: 0; }
  .events { flex: 1; min-height: 0; max-height: none; }
  .map-card.mini #map { flex: 1; height: auto; min-height: 160px; }

  /* Actividad: summary + 7 charts = 9 cells in 3 columns (the summary takes two) */
  .activity-card .charts { flex: 1; min-height: 0; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-rows: minmax(150px, 1fr); }
  .charts .summary { grid-column: span 2; grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .charts .summary div { justify-content: center; padding: 12px 18px; }
  .charts .summary dd { font-size: 1.9rem; }
  .charts .summary dt, .charts .summary .sub { font-size: .85rem; }
  .chart { display: flex; flex-direction: column; }
  .chart canvas { flex: 1 1 120px; height: 0; min-height: 0; }

  /* Mapa */
  .map-card:not(.mini) #map { flex: 1; height: auto; }

  /* Nodo: three cards in a row; the lab log takes what is left */
  .view-node { grid-template-rows: auto minmax(0, 1fr); }
  .view-node:has(#lab[hidden]) { height: auto; min-height: 0; }
  .lab-log { flex: 1; min-height: 120px; max-height: none; }
}
@media (min-width: 1700px) and (min-height: 700px) {
  .view-home.with-map { grid-template-columns: minmax(380px, 1fr) minmax(0, 1.6fr) minmax(380px, 1fr); }
  /* 8 cells in 4 columns: the summary is one cell, 2 x 2 */
  .activity-card .charts { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .charts .summary { grid-column: auto; grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
