/* jarvis dashboard theme: dense, muted, analyst-style. Light and dark via CSS variables. */
:root {
  --bg: #f3f4f6; --surface: #ffffff; --surface-2: #f8f9fb; --border: #d9dde3;
  --text: #1f2937; --text-muted: #6b7280; --accent: #2563eb; --accent-soft: #dbeafe;
  --ok: #16a34a; --warn: #d97706; --crit: #dc2626; --muted: #9ca3af; --stale: #7c3aed;
  --s1: #2563eb; --s2: #d97706; --s3: #16a34a; --s4: #dc2626; --s5: #7c3aed; --s6: #0891b2;
  --grid-line: #e5e7eb; --shadow: 0 1px 2px rgba(0,0,0,.06);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
html[data-theme="dark"] {
  --bg: #0f1216; --surface: #171b21; --surface-2: #1e232b; --border: #2a3039;
  --text: #e5e7eb; --text-muted: #9aa3ad; --accent: #60a5fa; --accent-soft: #1e3a5f;
  --ok: #22c55e; --warn: #f59e0b; --crit: #ef4444; --muted: #6b7280; --stale: #a78bfa;
  --s1: #60a5fa; --s2: #fbbf24; --s3: #4ade80; --s4: #f87171; --s5: #c084fc; --s6: #22d3ee;
  --grid-line: #262c35; --shadow: none;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    --bg: #0f1216; --surface: #171b21; --surface-2: #1e232b; --border: #2a3039;
    --text: #e5e7eb; --text-muted: #9aa3ad; --accent: #60a5fa; --accent-soft: #1e3a5f;
    --ok: #22c55e; --warn: #f59e0b; --crit: #ef4444; --muted: #6b7280; --stale: #a78bfa;
    --s1: #60a5fa; --s2: #fbbf24; --s3: #4ade80; --s4: #f87171; --s5: #c084fc; --s6: #22d3ee;
    --grid-line: #262c35; --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 13px/1.4 var(--font); }
button, input, select { font: inherit; color: inherit; }
main { padding: 8px 10px 40px; }

/* ---- top bar --------------------------------------------------------------------------- */
#topbar {
  display: flex; align-items: center; gap: 14px; min-height: 44px; padding: 0 12px; flex-wrap: wrap;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 50;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; letter-spacing: .2px; white-space: nowrap; }
.logo { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.spacer { flex: 1; }
.tabs { display: flex; gap: 2px; height: 100%; }
.tabs button {
  background: none; border: 0; border-bottom: 2px solid transparent; padding: 0 12px; height: 44px;
  color: var(--text-muted); cursor: pointer; font-weight: 500;
}
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
.controls { display: flex; align-items: center; gap: 8px; }
.clock { font-family: var(--mono); font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.btn {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 4px; padding: 4px 10px;
  cursor: pointer; color: var(--text); white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.icon { padding: 3px 7px; }
.btn.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.edit-tools { display: flex; gap: 6px; }
.edit-tools[hidden], [hidden] { display: none !important; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); font-size: 12px; color: var(--text-muted); white-space: nowrap;
}
.pill .dot, .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.pill.ok .dot { background: var(--ok); } .pill.warn .dot { background: var(--warn); }
.pill.crit .dot { background: var(--crit); } .pill.stale .dot { background: var(--stale); }
.alert-pill { cursor: pointer; }
.alert-pill.has-alerts { color: var(--crit); border-color: var(--crit); font-weight: 600; }
.badge { font-size: 10px; padding: 1px 6px; border-radius: 3px; font-weight: 700; letter-spacing: .5px; }
.badge.sim { background: var(--warn); color: #fff; }

/* ---- drawer ---------------------------------------------------------------------------- */
.drawer {
  position: fixed; right: 10px; top: 50px; width: 420px; max-height: 70vh; overflow: auto; z-index: 60;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--border); }

/* ---- grid & panels --------------------------------------------------------------------- */
.tab-page { display: none; }
.tab-page.active { display: block; }
.grid-stack { min-height: 200px; }
.grid-stack-item-content {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.editing .grid-stack-item-content { border-style: dashed; border-color: var(--accent); }
.panel-head {
  display: flex; align-items: center; gap: 8px; padding: 5px 10px; border-bottom: 1px solid var(--border);
  font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .4px;
  min-height: 28px; flex: 0 0 auto;
}
.panel-head .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel-head .extra { font-weight: 400; text-transform: none; letter-spacing: 0; font-family: var(--mono); font-size: 11px; }
.panel-head .tools { display: none; gap: 4px; }
.editing .panel-head .tools { display: flex; }
.panel-head .tools button { background: none; border: 0; cursor: pointer; color: var(--text-muted); padding: 0 4px; }
.panel-head .tools button:hover { color: var(--accent); }
.panel-head .seg { display: inline-flex; border: 1px solid var(--border); border-radius: 4px; overflow: hidden; }
.panel-head .seg button { background: none; border: 0; padding: 1px 6px; cursor: pointer; color: var(--text-muted); font-size: 11px; }
.panel-head .seg button.active { background: var(--accent-soft); color: var(--accent); }
.panel-head label.chk { font-weight: 400; text-transform: none; letter-spacing: 0; display: inline-flex; gap: 3px; align-items: center; cursor: pointer; }
.panel-head .live-btn { background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent); border-radius: 4px; padding: 1px 6px; font-size: 11px; cursor: pointer; text-transform: none; letter-spacing: 0; }
.panel-head .max { background: none; border: 0; cursor: pointer; color: var(--text-muted); padding: 0 2px; font-size: 14px; line-height: 1; }
.panel-head .max:hover { color: var(--accent); }

/* maximized panel: covers the viewport until the button is clicked again or Escape is pressed */
.grid-stack-item.maximized {
  position: fixed !important; inset: 0 !important; width: auto !important; height: auto !important;
  transform: none !important; z-index: 55; padding: 8px; background: var(--bg);
}
.grid-stack-item.maximized > .grid-stack-item-content { inset: 8px; }
body.has-maximized { overflow: hidden; }
.editing .grid-stack-item.maximized .grid-stack-item-content { cursor: default; }
.panel-body { flex: 1 1 auto; min-height: 0; position: relative; overflow: hidden; }
.panel-body.pad { padding: 8px 10px; }
.panel-empty { color: var(--text-muted); padding: 10px; font-style: italic; }
.editing .panel-body { pointer-events: none; }
.editing .grid-stack-item-content { cursor: move; user-select: none; }

/* stat panel */
.stat { height: 100%; display: flex; flex-direction: column; padding: 8px 12px 4px; }
.stat .value { font-size: 28px; font-weight: 600; font-family: var(--mono); line-height: 1.1; display: flex; align-items: baseline; gap: 6px; }
.stat .value .unit { font-size: 13px; color: var(--text-muted); font-family: var(--font); font-weight: 500; }
.stat .sub { display: flex; justify-content: space-between; color: var(--text-muted); font-size: 11px; margin-top: 2px; }
.stat .spark { flex: 1; min-height: 18px; width: 100%; }
.stat.q-warn .value { color: var(--warn); } .stat.q-crit .value { color: var(--crit); }
.stat.q-stale .value { color: var(--stale); } .stat.q-muted .value { color: var(--muted); }
.stat .qtag { font-size: 10px; text-transform: uppercase; letter-spacing: .5px; font-weight: 700; }
.q-ok .qtag { color: var(--ok); } .q-warn .qtag { color: var(--warn); } .q-crit .qtag { color: var(--crit); }
.q-stale .qtag { color: var(--stale); } .q-muted .qtag { color: var(--muted); }

/* status panel */
.status-list { display: flex; flex-direction: column; gap: 4px; padding: 6px 10px; overflow: auto; height: 100%; }
.status-row { display: grid; grid-template-columns: 14px 1fr auto; align-items: center; gap: 8px; padding: 3px 0; border-bottom: 1px dashed var(--grid-line); }
.status-row:last-child { border-bottom: 0; }
.status-row .lamp { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.lamp.ok { background: var(--ok); box-shadow: 0 0 0 2px color-mix(in srgb, var(--ok) 25%, transparent); }
.lamp.warn { background: var(--warn); } .lamp.crit { background: var(--crit); animation: blink 1s infinite; }
.lamp.stale { background: var(--stale); } .lamp.muted { background: var(--muted); }
.status-row .state { font-family: var(--mono); font-size: 12px; }
.status-row .state.crit { color: var(--crit); font-weight: 700; } .status-row .state.warn { color: var(--warn); }
@keyframes blink { 50% { opacity: .35; } }

/* chart panel */
.chart-wrap { width: 100%; height: 100%; }
.uplot { font-family: var(--font); }
.u-legend { font-size: 11px; color: var(--text-muted); }
.u-legend .u-marker { border-radius: 2px; }
.u-select { background: color-mix(in srgb, var(--accent) 20%, transparent); }

/* image panel */
.image-wrap { height: 100%; display: flex; flex-direction: column; }
.image-wrap img { flex: 1; min-height: 0; object-fit: contain; width: 100%; background: #000; cursor: zoom-in; image-rendering: auto; }
.image-wrap .caption { display: flex; gap: 14px; padding: 4px 10px; font-family: var(--mono); font-size: 11px; color: var(--text-muted); flex-wrap: wrap; }

/* alerts panel */
table.alerts { width: 100%; border-collapse: collapse; font-size: 12px; }
table.alerts th { text-align: left; font-weight: 600; color: var(--text-muted); padding: 4px 8px; border-bottom: 1px solid var(--border); font-size: 11px; text-transform: uppercase; }
table.alerts td { padding: 4px 8px; border-bottom: 1px solid var(--grid-line); vertical-align: top; }
table.alerts tr.crit td:first-child { border-left: 3px solid var(--crit); }
table.alerts tr.warn td:first-child { border-left: 3px solid var(--warn); }
table.alerts tr.stale td:first-child { border-left: 3px solid var(--stale); }
table.alerts .lvl { font-weight: 700; text-transform: uppercase; font-size: 11px; }
tr.crit .lvl { color: var(--crit); } tr.warn .lvl { color: var(--warn); } tr.stale .lvl { color: var(--stale); }
table.alerts .acked { opacity: .55; }
.alerts-ok { padding: 12px; color: var(--ok); font-weight: 600; }

/* log panel */
.log { font-family: var(--mono); font-size: 11px; padding: 4px 8px; overflow: auto; height: 100%; }
.log div { white-space: pre-wrap; border-bottom: 1px dashed var(--grid-line); padding: 1px 0; }
.log .t { color: var(--text-muted); } .log .alert { color: var(--crit); } .log .driver { color: var(--accent); }

/* dialog */
dialog { border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); padding: 16px 18px; width: 460px; max-width: 95vw; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h3 { margin: 0 0 10px; }
dialog label { display: block; margin: 8px 0; font-size: 12px; color: var(--text-muted); }
dialog label.inline { display: flex; align-items: center; gap: 6px; }
dialog input[type=text], dialog input:not([type]), dialog select { width: 100%; padding: 5px 7px; border: 1px solid var(--border); border-radius: 4px; background: var(--surface-2); margin-top: 3px; }
.channel-list { max-height: 180px; overflow: auto; border: 1px solid var(--border); border-radius: 4px; margin-top: 4px; padding: 4px; background: var(--surface-2); }
.channel-list label { display: flex; gap: 6px; align-items: center; margin: 2px 0; font-size: 12px; color: var(--text); cursor: pointer; }
.channel-list code { color: var(--text-muted); font-size: 11px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 12px 0 0; padding: 0; }
dialog .opt:not(.show) { display: none !important; }
.hint { color: var(--text-muted); font-size: 11px; }

/* ---- tablets: 6-column grid (gridstack.min.css only ships 12- and 1-column widths) --------- */
.gs-6 > .grid-stack-item { width: 16.667%; }
.gs-6 > .grid-stack-item[gs-w="2"] { width: 33.333%; }
.gs-6 > .grid-stack-item[gs-w="3"] { width: 50%; }
.gs-6 > .grid-stack-item[gs-w="4"] { width: 66.667%; }
.gs-6 > .grid-stack-item[gs-w="5"] { width: 83.333%; }
.gs-6 > .grid-stack-item[gs-w="6"] { width: 100%; }
.gs-6 > .grid-stack-item[gs-x="1"] { left: 16.667%; }
.gs-6 > .grid-stack-item[gs-x="2"] { left: 33.333%; }
.gs-6 > .grid-stack-item[gs-x="3"] { left: 50%; }
.gs-6 > .grid-stack-item[gs-x="4"] { left: 66.667%; }
.gs-6 > .grid-stack-item[gs-x="5"] { left: 83.333%; }

/* Layout editing needs the full 12-column grid; scaled-down views are read-only. */
@media (max-width: 1100px) { #edit-btn, #edit-tools { display: none !important; } }

/* ---- phones (the grid collapses to one column, see layout.js) ------------------------------ */
@media (max-width: 700px) {
  main { padding: 6px 6px 40px; }
  #topbar { gap: 8px; padding: 0 8px; min-height: 40px; }
  .brand { font-size: 13px; }
  .tabs { order: 10; flex: 1 0 100%; height: 38px; overflow-x: auto; scrollbar-width: none; margin: 0 -8px; padding: 0 4px; }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { height: 38px; padding: 0 10px; flex: 0 0 auto; }
  .controls { gap: 6px; }
  #clock, #health { display: none !important; }
  .drawer { left: 8px; right: 8px; width: auto; top: 86px; max-height: 75vh; }
  .stat .value { font-size: 24px; }
  .panel-head { padding: 5px 8px; }
  .panel-head .seg button { padding: 2px 5px; }
  table.alerts th, table.alerts td { padding: 4px 6px; }
}
@media (pointer: coarse) {
  .btn { padding: 6px 12px; }
  .tabs button { padding: 0 14px; }
  .image-wrap img { cursor: default; }
}
