/* ─── Reset & Base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:     #0d1117;
  --bg2:    #161b22;
  --bg3:    #21262d;
  --border: #30363d;
  --text:   #e6edf3;
  --dim:    #8b949e;
  --blue:   #3b82f6;
  --green:  #22c55e;
  --yellow: #f59e0b;
  --red:    #ef4444;
  --purple: #a855f7;
  --teal:   #14b8a6;
  --r:      8px;
}

/* Full-viewport, no page scroll — panels scroll internally */
html { height: 100%; }
body {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  font-size: 13px;
  line-height: 1.5;
}

/* ─── Header ──────────────────────────────────────────────────────────────── */
.header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 20px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 200;
}
.header-left  { display: flex; align-items: center; gap: 13px; }
.logo-badge {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  border-radius: 7px; display: grid; place-items: center;
  font-weight: 800; font-size: 14px; letter-spacing: -1px; flex-shrink: 0;
}
.header-title { font-size: 15px; font-weight: 700; }
.header-sub   { font-size: 11px; color: var(--dim); }
.header-right { display: flex; align-items: center; gap: 18px; }
.live-pill {
  display: flex; align-items: center; gap: 6px;
  background: rgba(34,197,94,.1); border: 1px solid rgba(34,197,94,.3);
  border-radius: 20px; padding: 3px 10px;
  font-size: 11px; font-weight: 700; color: var(--green); letter-spacing: .5px;
}
.live-dot {
  width: 7px; height: 7px; background: var(--green);
  border-radius: 50%; animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%,100%{opacity:1;transform:scale(1)} 50%{opacity:.3;transform:scale(.75)} }
.header-time  { font-size: 13px; font-variant-numeric: tabular-nums; color: var(--dim); }
.header-plant { font-size: 12px; color: var(--dim); }

/* ─── KPI Bar ─────────────────────────────────────────────────────────────── */
.kpi-bar {
  display: grid; grid-template-columns: repeat(6,1fr);
  gap: 1px; background: var(--border);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.kpi-card          { background: var(--bg2); padding: 11px 18px; }
.kpi-card.kpi-warn     { background: rgba(245,158,11,.05); }
.kpi-card.kpi-critical { background: rgba(239,68,68,.05); }
.kpi-label { font-size: 10px; color: var(--dim); text-transform: uppercase; letter-spacing: .5px; }
.kpi-value {
  font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums;
  margin: 2px 0 1px; line-height: 1;
}
.kpi-card.kpi-warn .kpi-value     { color: var(--yellow); }
.kpi-card.kpi-critical .kpi-value { color: var(--red); }
.kpi-sub         { font-size: 11px; color: var(--dim); }
.kpi-sub.pos     { color: var(--green); }
.kpi-sub.neg     { color: var(--red); }
.kpi-sub.neutral { color: var(--dim); }

/* ─── Main Tab Nav ────────────────────────────────────────────────────────── */
.main-tab-nav {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 14px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.main-tab {
  display: flex; align-items: center; gap: 8px;
  background: none; border: 1px solid transparent;
  border-radius: 6px; padding: 7px 16px;
  cursor: pointer; color: var(--dim);
  font-size: 13px; font-weight: 600;
  transition: background .15s, color .15s, border-color .15s;
}
.main-tab:hover {
  background: rgba(255,255,255,.04);
  color: var(--text);
  border-color: var(--border);
}
.main-tab.active {
  background: rgba(59,130,246,.1);
  color: var(--blue);
  border-color: rgba(59,130,246,.35);
}
.mt-icon { font-size: 14px; }
.mt-chip {
  font-size: 10px; font-weight: 700;
  padding: 1px 6px; border-radius: 10px;
  background: var(--bg3); color: var(--dim);
  border: 1px solid var(--border);
}
.mt-chip.warn {
  background: rgba(245,158,11,.1); color: var(--yellow);
  border-color: rgba(245,158,11,.3);
  animation: blink-op 2s ease-in-out infinite;
}
@keyframes blink-op { 0%,100%{opacity:1} 50%{opacity:.5} }

/* ─── Content Area ────────────────────────────────────────────────────────── */
.content-area {
  flex: 1;
  min-height: 0; /* critical: lets flex children shrink below content size */
  position: relative;
}

/* ─── Main Panes ──────────────────────────────────────────────────────────── */
.main-pane {
  display: none;
  height: 100%;
  padding: 10px;
  gap: 10px;
  overflow: hidden;
}
.main-pane.active { display: flex; }

/* ─── Panel base ──────────────────────────────────────────────────────────── */
.panel {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--r);
  display: flex; flex-direction: column;
  min-height: 0; overflow: hidden;
}
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 9px 14px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.panel-title {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .5px; color: var(--dim);
}
.blink-badge {
  font-size: 10px; font-weight: 700; padding: 2px 8px; border-radius: 4px;
  color: var(--yellow); background: rgba(245,158,11,.1);
  border: 1px solid rgba(245,158,11,.35);
  animation: blink-op 2s ease-in-out infinite;
}

/* ══════════════════════════════════════════════════════════════════════════
   TAB 1 — FLEET VIEW
   Layout: fleet-panel (left, fixed ~440px wide) | detail-panel (flex 1)
   ══════════════════════════════════════════════════════════════════════════ */
#pane-fleet {
  display: none; /* overridden by .active */
}
#pane-fleet.active { display: flex; }

.fleet-panel  { width: 440px; flex-shrink: 0; }
.detail-panel { flex: 1; min-width: 0; }

/* Fleet legend */
.fleet-legend { display: flex; align-items: center; gap: 10px; }
.fl-dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.fl-dot.ok       { background: var(--green); }
.fl-dot.warn     { background: var(--yellow); }
.fl-dot.critical { background: var(--red); }
.fl-lbl  { font-size: 11px; color: var(--dim); }

/* Fleet grid of cards */
.fleet-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 10px;
  overflow-y: auto;
  flex: 1;
}

.vehicle-card {
  background: var(--bg3);
  border: 1.5px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  cursor: pointer;
  transition: border-color .2s, background .2s, transform .1s;
}
.vehicle-card:hover { background: rgba(255,255,255,.04); transform: translateY(-1px); }
.vehicle-card.selected { background: rgba(59,130,246,.08); border-color: var(--blue) !important; }

.vehicle-card.ok       { border-color: rgba(34,197,94,.3); }
.vehicle-card.warn     { border-color: rgba(245,158,11,.4); }
.vehicle-card.critical { border-color: rgba(239,68,68,.45); }
.vehicle-card.idle     { border-color: var(--border); opacity: .6; }

.vc-id   { font-size: 12px; font-weight: 700; }
.vc-type { font-size: 10px; color: var(--dim); margin-bottom: 7px; }
.vc-health-bar {
  height: 4px; background: var(--bg2); border-radius: 2px;
  margin-bottom: 6px; overflow: hidden;
}
.vc-health-fill { height: 100%; border-radius: 2px; transition: width .6s ease; }
.vc-bottom { display: flex; align-items: center; justify-content: space-between; }
.vc-score  { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.vc-alarms { font-size: 10px; color: var(--dim); }
.vc-alarms.has-alarms { color: var(--yellow); font-weight: 600; }
.vc-persist {
  font-size: 9px; color: var(--dim); margin-top: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.vc-persist.warn-persist { color: var(--yellow); }
.vc-persist.crit-persist { color: var(--red); }

/* Detail panel */
.detail-status-badge {
  font-size: 10px; font-weight: 700; padding: 2px 8px; border-radius: 4px;
}
.dsb-ok       { background: rgba(34,197,94,.1);  color: var(--green);  border: 1px solid rgba(34,197,94,.3); }
.dsb-warn     { background: rgba(245,158,11,.1); color: var(--yellow); border: 1px solid rgba(245,158,11,.3); }
.dsb-critical { background: rgba(239,68,68,.1);  color: var(--red);    border: 1px solid rgba(239,68,68,.3); }

/* detail-body: column — top row fills remaining space, bottom is fixed signal chart */
.detail-body {
  display: flex; flex-direction: column;
  flex: 1; min-height: 0;
}

/* TOP ROW: score+dims (left) | radar (right), side by side */
.detail-top {
  display: grid;
  grid-template-columns: 1fr 1fr;
  flex: 1; min-height: 0;
  overflow: hidden;
}

.detail-left {
  border-right: 1px solid var(--border);
  padding: 14px;
  display: flex; flex-direction: column; gap: 14px;
  overflow-y: auto;
}

/* Ring gauge */
.health-score-wrap  { display: flex; align-items: center; gap: 14px; }
.health-score-ring  { width: 78px; height: 78px; position: relative; flex-shrink: 0; }
.ring-svg           { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg            { fill: none; stroke: var(--bg3); stroke-width: 7; }
.ring-fill          { fill: none; stroke-width: 7; stroke-linecap: round; transition: stroke-dashoffset .7s ease, stroke .4s; }
.ring-inner         { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring-value         { font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1; }
.ring-label         { font-size: 9px; color: var(--dim); text-transform: uppercase; letter-spacing: .4px; }

.detail-meta        { display: flex; flex-direction: column; gap: 5px; }
.dm-row             { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; }
.dm-key             { color: var(--dim); }
.dm-val             { font-weight: 600; font-variant-numeric: tabular-nums; }

/* Dimension score rows */
.dim-scores        { display: flex; flex-direction: column; gap: 9px; }
.dim-score-row     { display: flex; flex-direction: column; gap: 3px; }
.ds-header         { display: flex; align-items: center; justify-content: space-between; }
.ds-name           { font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.ds-vals           { display: flex; align-items: center; gap: 5px; font-size: 10px; }
.ds-score          { font-weight: 700; font-variant-numeric: tabular-nums; }
.ds-sigma          { font-size: 9px; padding: 1px 4px; border-radius: 3px; }
.ds-sigma.neg-hi   { background: rgba(239,68,68,.15);  color: var(--red); }
.ds-sigma.neg-lo   { background: rgba(245,158,11,.15); color: var(--yellow); }
.ds-sigma.pos      { background: rgba(34,197,94,.12);  color: var(--green); }
.ds-bar-wrap       { height: 5px; background: var(--bg3); border-radius: 3px; position: relative; overflow: visible; }
.ds-bar-fill       { height: 100%; border-radius: 3px; transition: width .7s ease; }
.ds-fleet-mark     { position: absolute; top: -3px; bottom: -3px; width: 2px; background: rgba(255,255,255,.22); border-radius: 1px; }

/* Detail radar panel (top-right) */
.detail-radar {
  padding: 14px;
  display: flex; flex-direction: column; gap: 8px;
  overflow: hidden;
}
.fc-title   { font-size: 10px; color: var(--dim); text-transform: uppercase; letter-spacing: .4px; flex-shrink: 0; }
.radar-wrap { flex: 1; min-height: 0; position: relative; }

/* Detail bottom: live signal chart, full width */
.detail-bottom {
  border-top: 1px solid var(--border);
  padding: 10px 14px 12px;
  display: flex; flex-direction: column; gap: 6px;
  height: 185px; flex-shrink: 0;
}
.signal-header { display: flex; align-items: center; justify-content: space-between; flex-shrink: 0; }
.signal-label  { font-size: 11px; font-weight: 600; color: var(--dim); }
.signal-status { font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; padding: 2px 7px; border-radius: 4px; }
.signal-status.ok       { background: rgba(34,197,94,.12); color: var(--green);  border: 1px solid rgba(34,197,94,.3); }
.signal-status.warn     { background: rgba(245,158,11,.12); color: var(--yellow); border: 1px solid rgba(245,158,11,.3); }
.signal-status.critical { background: rgba(239,68,68,.12);  color: var(--red);   border: 1px solid rgba(239,68,68,.3); }
.signal-chart-wrap { flex: 1; min-height: 0; position: relative; }

/* ══════════════════════════════════════════════════════════════════════════
   TAB 2 — INSIGHTS & ANOMALIES
   Layout: anomaly-panel (flex 1) | insight-panel (400px)
   ══════════════════════════════════════════════════════════════════════════ */
#pane-insights {
  display: none;
}
#pane-insights.active { display: flex; }

.anomaly-panel { flex: 1; min-width: 0; }
.insight-panel { flex: 1; min-width: 0; }

/* Anomaly feed */
.anomaly-feed { overflow-y: auto; flex: 1; }

.anomaly-item {
  padding: 10px 13px;
  border-bottom: 1px solid rgba(48,54,61,.5);
  display: flex; gap: 10px; align-items: flex-start;
  cursor: pointer; transition: background .15s;
}
.anomaly-item:hover { background: rgba(255,255,255,.025); }
.anomaly-item:last-child { border-bottom: none; }

.ai-sev-bar { width: 3px; flex-shrink: 0; border-radius: 2px; align-self: stretch; min-height: 40px; }
.ai-sev-bar.critical { background: var(--red); }
.ai-sev-bar.warn     { background: var(--yellow); }
.ai-sev-bar.info     { background: var(--blue); }

.ai-content { flex: 1; min-width: 0; }
.ai-header  { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; margin-bottom: 2px; }
.ai-vehicle { font-size: 12px; font-weight: 700; }
.ai-dim     { font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; padding: 1px 5px; border-radius: 3px; }
.dim-motion   { background: rgba(59,130,246,.18); color: var(--blue); }
.dim-battery  { background: rgba(245,158,11,.18); color: var(--yellow); }
.dim-actuator { background: rgba(168,85,247,.18); color: var(--purple); }
.dim-perf     { background: rgba(20,184,166,.18); color: var(--teal); }
.dim-alarms   { background: rgba(239,68,68,.18);  color: var(--red); }

.ai-desc  { font-size: 11.5px; color: var(--text); }
.ai-fleet { font-size: 10px; color: var(--dim); margin-top: 2px; font-style: italic; }

.ai-right { flex-shrink: 0; display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }

.persist-badge {
  font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums;
  padding: 2px 8px; border-radius: 4px;
}
.persist-badge.critical { background: rgba(239,68,68,.12);  color: var(--red);    border: 1px solid rgba(239,68,68,.3); }
.persist-badge.warn     { background: rgba(245,158,11,.12); color: var(--yellow); border: 1px solid rgba(245,158,11,.3); }
.persist-badge.info     { background: rgba(59,130,246,.12); color: var(--blue);   border: 1px solid rgba(59,130,246,.3); }

.persist-dots { display: flex; gap: 2px; }
.pd { width: 5px; height: 5px; border-radius: 50%; }

/* Inner tabs (AI / Trend) */
.tab-header { gap: 10px; }
.tab-bar    { display: flex; gap: 2px; }
.tab-btn {
  background: none; border: none; cursor: pointer;
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px;
  color: var(--dim); padding: 4px 10px; border-radius: 4px;
  transition: background .15s, color .15s;
}
.tab-btn:hover  { background: rgba(255,255,255,.05); color: var(--text); }
.tab-btn.active { background: rgba(59,130,246,.12); color: var(--blue); }

.ai-badge {
  font-size: 10px; padding: 2px 7px; border-radius: 4px;
  background: rgba(168,85,247,.1); border: 1px solid rgba(168,85,247,.3);
  color: var(--purple);
}

.tab-pane        { display: none; flex: 1; min-height: 0; flex-direction: column; overflow: hidden; }
.tab-pane.active { display: flex; }

.insight-body {
  padding: 14px;
  font-size: 12.5px; line-height: 1.75; color: var(--text);
  overflow-y: auto; flex: 1;
}
.insight-body strong { color: var(--purple); }
.action-list  { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.action-item  { display: flex; align-items: flex-start; gap: 7px; font-size: 12px; color: var(--dim); }
.action-arrow { color: var(--purple); flex-shrink: 0; font-weight: 700; }

.trend-inner {
  display: flex; flex-direction: column; flex: 1;
  padding: 10px 14px 14px; gap: 8px;
}
.legend-row { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.leg-dot    { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.leg-lbl    { font-size: 11px; color: var(--dim); }
.trend-chart-wrap { flex: 1; min-height: 0; position: relative; }

/* ─── Help Button ─────────────────────────────────────────────────────────── */
.help-btn {
  display: flex; align-items: center; gap: 5px;
  background: rgba(59,130,246,.08);
  border: 1px solid rgba(59,130,246,.3);
  border-radius: 6px; padding: 5px 11px;
  color: var(--blue); font-size: 12px; font-weight: 600;
  cursor: pointer; transition: background .15s, border-color .15s;
}
.help-btn:hover { background: rgba(59,130,246,.15); border-color: rgba(59,130,246,.5); }
.help-icon {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--blue); color: #fff;
  font-size: 10px; font-weight: 800;
  display: inline-grid; place-items: center; flex-shrink: 0;
}

/* ─── Help Overlay ────────────────────────────────────────────────────────── */
.help-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.45); z-index: 300;
}
.help-overlay.open { display: block; }

/* ─── Help Drawer ─────────────────────────────────────────────────────────── */
.help-drawer {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: 420px; max-width: 90vw;
  background: var(--bg2);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform .28s cubic-bezier(.4,0,.2,1);
  z-index: 400;
  box-shadow: -8px 0 32px rgba(0,0,0,.5);
}
.help-drawer.open { transform: translateX(0); }

.help-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.help-title { font-size: 14px; font-weight: 700; }
.help-close {
  background: none; border: none; cursor: pointer;
  color: var(--dim); font-size: 16px; line-height: 1;
  padding: 2px 6px; border-radius: 4px;
  transition: color .15s, background .15s;
}
.help-close:hover { color: var(--text); background: rgba(255,255,255,.06); }

.help-body {
  overflow-y: auto; flex: 1; padding: 16px 18px;
  display: flex; flex-direction: column; gap: 20px;
}

.help-section { display: flex; flex-direction: column; gap: 10px; }

.help-section-label {
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .7px; color: var(--blue);
  padding-bottom: 4px; border-bottom: 1px solid rgba(59,130,246,.2);
}

.help-item { display: flex; flex-direction: column; gap: 4px; }

.help-item-title {
  font-size: 12px; font-weight: 700; color: var(--text);
}

.help-item-text {
  font-size: 12px; line-height: 1.7; color: var(--dim);
}
.help-item-text strong { color: var(--text); font-weight: 600; }

.help-note {
  font-size: 11px; color: var(--dim); font-style: italic;
  padding: 10px 12px;
  background: var(--bg3); border-radius: 6px;
  border: 1px solid var(--border);
}

/* ─── Footer ──────────────────────────────────────────────────────────────── */
.footer {
  text-align: center; padding: 7px;
  font-size: 11px; color: var(--dim);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* ─── Scrollbar ───────────────────────────────────────────────────────────── */
::-webkit-scrollbar       { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ─── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 1200px) {
  .kpi-bar     { grid-template-columns: repeat(3,1fr); }
  .fleet-panel { width: 320px; }
  .fleet-grid  { grid-template-columns: repeat(2,1fr); }
  .detail-top  { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .detail-radar { border-top: 1px solid var(--border); }
}
@media (max-width: 900px) {
  body { overflow: auto; height: auto; }
  .content-area { height: auto; }
  .main-pane { height: auto; flex-direction: column; overflow: visible; }
  .main-pane.active { display: flex; }
  .fleet-panel, .detail-panel, .anomaly-panel, .insight-panel { width: 100%; }
  .kpi-bar { grid-template-columns: repeat(2,1fr); }
}
