:root {
  color-scheme: light;
  --page-margin: 22px;
  --bg: #d6dde7;
  --panel: rgba(255, 255, 255, 0.76);
  --panel-soft: rgba(245, 248, 252, 0.84);
  --text: #111827;
  --muted: #4b5563;
  --line: rgba(71, 85, 105, 0.24);
  --ok: #047857;
  --warn: #b45309;
  --bad: #dc2626;
  --accent: #0284c7;
  --leader: #2563eb;
  --follower: #0891b2;
  --target: #f59e0b;
  --ideal: #8b5cf6;
}

* { box-sizing: border-box; }
html, body { width: 100%; min-height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  padding: var(--page-margin);
  overflow: hidden;
  background:
    linear-gradient(130deg, rgba(255, 255, 255, 0.82) 0%, rgba(214, 223, 235, 0.62) 26%, transparent 58%),
    linear-gradient(38deg, rgba(116, 143, 178, 0.56) 0%, transparent 38%, rgba(245, 249, 255, 0.74) 100%),
    linear-gradient(118deg, #aeb9c8 0%, #d7e0eb 36%, #eef3f8 64%, #b7c4d4 100%);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(rgba(255, 255, 255, 0.22) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.18) 1px, transparent 1px);
  background-size: 42px 42px, 42px 42px;
  opacity: 0.42;
}
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 18% 18%, rgba(255, 255, 255, 0.5), transparent 30%),
    radial-gradient(circle at 82% 78%, rgba(116, 143, 178, 0.32), transparent 34%);
  mix-blend-mode: soft-light;
  opacity: 0.72;
}

.shell {
  position: relative;
  z-index: 1;
  height: calc(100vh - (var(--page-margin) * 2));
  display: grid;
  grid-template-rows: auto minmax(0, 1.35fr) minmax(210px, 0.72fr) minmax(160px, 0.46fr);
  gap: 8px;
  overflow: hidden;
}
.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 12px;
}
h1, h2, p { margin: 0; }
h1 { color: #0f172a; font-size: 23px; font-weight: 800; text-shadow: 0 1px 16px rgba(255,255,255,0.62); }
h2 { font-size: 14px; font-weight: 780; }
.topbar p, small { color: var(--muted); font-size: 11px; }
.nav-links { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.nav-links a {
  min-height: 26px;
  padding: 5px 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: rgba(255,255,255,0.72);
  color: #0f172a;
  text-decoration: none;
  font-size: 12px;
  font-weight: 780;
}
.nav-links a:hover { border-color: rgba(2,132,199,0.38); color: var(--accent); }
.status-stack, .panel-head, .legend-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.status-stack { justify-content: flex-end; flex-wrap: wrap; }

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.7);
  color: var(--text);
  font-size: 12px;
  font-weight: 780;
  white-space: nowrap;
}
.pill.ok { border-color: rgba(4,120,87,0.32); background: rgba(209,250,229,0.72); color: var(--ok); }
.pill.warn { border-color: rgba(180,83,9,0.34); background: rgba(254,243,199,0.78); color: var(--warn); }
.pill.bad { border-color: rgba(220,38,38,0.34); background: rgba(254,226,226,0.78); color: var(--bad); }
.pill.neutral { color: #334155; }

.panel, .metric {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  backdrop-filter: blur(20px) saturate(1.45);
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.72),
    inset 0 -1px 0 rgba(15,23,42,0.04),
    0 16px 42px rgba(31,41,55,0.18);
  position: relative;
  overflow: hidden;
}
.panel::before, .metric::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  border-radius: inherit;
  background: linear-gradient(132deg, rgba(255,255,255,0.36), transparent 42%, rgba(148,163,184,0.08) 78%, transparent);
  opacity: 0.82;
}
.panel > *, .metric > * { position: relative; z-index: 1; }
.panel { padding: 8px; }

.hero-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(330px, 26vw, 440px);
  gap: 8px;
  min-height: 0;
}
.map-panel { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; min-height: 0; }
.canvas-wrap {
  position: relative;
  min-height: 0;
  margin-top: 6px;
  border: 1px solid rgba(71,85,105,0.22);
  border-radius: 8px;
  overflow: hidden;
  background: rgba(248,250,252,0.66);
}
#miniMap { width: 100%; height: 100%; display: block; }
.map-hud {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}
.map-hud div {
  padding: 7px 8px;
  border: 1px solid rgba(71,85,105,0.22);
  border-radius: 7px;
  background: rgba(255,255,255,0.82);
}
.map-hud span { display: block; color: var(--muted); font-size: 10px; font-weight: 720; text-transform: uppercase; }
.map-hud strong { display: block; font-size: 17px; margin-top: 2px; }
.legend-row {
  justify-content: flex-start;
  flex-wrap: wrap;
  margin-top: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 720;
}
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 99px; margin-right: 5px; vertical-align: middle; }
.dot.leader { background: var(--leader); }
.dot.follower { background: var(--follower); }
.dot.target { background: var(--target); }
.line.ideal { display: inline-block; width: 16px; height: 0; border-top: 3px dashed var(--ideal); margin-right: 5px; vertical-align: middle; }

.summary-panel { display: grid; grid-template-rows: auto minmax(0,1fr); min-height: 0; }
.decision-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 6px; min-height: 0; margin-top: 6px; }
.metric {
  min-height: 0;
  padding: 8px 9px;
  background: linear-gradient(145deg, rgba(255,255,255,0.88), rgba(226,235,246,0.6)), var(--panel);
  display: grid;
  grid-template-rows: auto minmax(22px, auto) auto;
  align-content: center;
  gap: 2px;
}
.metric.emphasis { border-color: rgba(2,132,199,0.32); }
.metric.compact strong { font-size: 17px; }
.label { color: var(--muted); font-size: 10px; font-weight: 730; text-transform: uppercase; }
.metric strong { display: block; overflow-wrap: anywhere; font-size: 19px; line-height: 1.08; }
.metric small { color: var(--muted); font-size: 10px; }

.machine-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; min-height: 0; }
.machine-panel { display: grid; grid-template-rows: auto minmax(0,1fr); min-height: 0; }
.state-machine {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  align-items: stretch;
  gap: 6px;
  margin-top: 8px;
  min-height: 0;
}
.state-node {
  position: relative;
  min-height: 82px;
  padding: 9px 8px;
  border: 1px solid rgba(71,85,105,0.22);
  border-radius: 8px;
  background: rgba(255,255,255,0.62);
  display: grid;
  grid-template-rows: auto minmax(0,1fr) auto;
  gap: 4px;
  overflow: hidden;
}
.state-node::after {
  content: "";
  position: absolute;
  right: -11px;
  top: 50%;
  width: 18px;
  height: 2px;
  background: rgba(71,85,105,0.28);
}
.state-node:last-child::after { display: none; }
.state-node .num { color: var(--muted); font-size: 10px; font-weight: 800; }
.state-node strong { font-size: 14px; line-height: 1.08; }
.state-node small { font-size: 10px; }
.state-node.done { background: rgba(209,250,229,0.62); border-color: rgba(4,120,87,0.3); }
.state-node.active { background: rgba(219,234,254,0.86); border-color: rgba(37,99,235,0.42); box-shadow: 0 0 0 2px rgba(37,99,235,0.12), 0 12px 28px rgba(37,99,235,0.16); }
.state-node.warn { background: rgba(254,243,199,0.78); border-color: rgba(180,83,9,0.34); }
.state-node.bad { background: rgba(254,226,226,0.78); border-color: rgba(220,38,38,0.34); }
.state-node.pending { color: #475569; }

.bottom-grid { display: grid; grid-template-columns: minmax(0, 1fr) clamp(330px, 26vw, 440px); gap: 8px; min-height: 0; }
.chart-panel { display: grid; grid-template-rows: auto minmax(0,1fr); min-height: 0; }
#errorChart { width: 100%; height: 100%; min-height: 0; display: block; margin-top: 6px; background: rgba(248,250,252,0.7); border: 1px solid rgba(71,85,105,0.22); border-radius: 7px; }
.event-panel { display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; }
.event-list { margin: 6px 0 0; padding: 0; list-style: none; display: grid; align-content: start; gap: 5px; min-height: 0; overflow-y: auto; }
.event-list li, .empty-list {
  min-height: 32px;
  padding: 6px 7px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: #111827;
  font-size: 11px;
}
.event-list strong { display: block; font-size: 12px; }
.event-list small { display: block; color: var(--muted); margin-top: 2px; }

@media (max-width: 980px) {
  body { overflow: auto; }
  .shell { height: auto; min-height: calc(100vh - (var(--page-margin) * 2)); grid-template-rows: auto auto auto auto; }
  .topbar, .hero-grid, .machine-grid, .bottom-grid { grid-template-columns: 1fr; }
  .canvas-wrap { min-height: 360px; }
  .state-machine { grid-template-columns: repeat(3, minmax(0,1fr)); }
}
@media (max-width: 640px) {
  .decision-grid, .map-hud, .state-machine { grid-template-columns: 1fr; }
  .status-stack, .nav-links { justify-content: flex-start; }
}
