@import url('https://fonts.googleapis.com/css2?family=Source+Sans+3:wght@400;600;700&display=swap');

:root {
  --bg: #EFF4F8;            /* page background */
  --card: #FFFFFF;          /* card / content */
  --divider: #CFD6DF;       /* divider / rail border */
  --rail: #D7DDE4;          /* left icon rail */
  --active: #DCE0E5;        /* active menu item */
  --input-border: #B3BBC7;  /* input / search border */
  --text: #30363E;          /* primary text */
  --muted: #7E8692;         /* secondary / muted text */
  --green: #449631;         /* normal badge */
  --green-bg: #F4F9F2;      /* light green badge */
  --top-from: #CBD6E9;
  --top-to: #A5BDED;
  --accent: #C7D4FD;        /* illustration accent */
  --accent-strong: #3D5FC4;

  --red: #C2362B;    --red-bg: #FDEFED;
  --amber: #B25E00;  --amber-bg: #FFF4E3;
  --info: #3D5FC4;   --info-bg: #EEF2FD;
  --grey-bg: #F1F3F6;

  /* chart series (validated categorical slots 1-4) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(48, 54, 62, .04), 0 4px 16px rgba(48, 54, 62, .05);
  --rail-w: 72px;
  --topbar-h: 60px;
  --status-h: 0px;
  --panel-w: 340px;
  --gap: 20px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: calc(var(--topbar-h) + 16px); }
body {
  margin: 0;
  font-family: 'Source Sans 3', 'Source Sans Pro', -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  color: #30363E;
  background: #EFF4F8;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 18px; }
h3 { font-size: 15px; }
p { margin: 0; }
a { color: var(--accent-strong); }
button, input, select { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pad { padding: 12px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.ico { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ico.lg { width: 28px; height: 28px; }
:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 2px; border-radius: 6px; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: var(--rail-w) minmax(0, 1fr); min-height: calc(100vh - var(--topbar-h) - var(--status-h)); }

.rail {
  position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h) - var(--status-h)); background: var(--rail);
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 14px 0;
  border-right: 1px solid var(--divider); z-index: 30;
}
.rail-item {
  width: 60px; padding: 8px 0 6px; border-radius: 10px; display: flex; flex-direction: column; align-items: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 600;
}
.rail-item:hover { color: var(--text); background: rgba(255, 255, 255, .45); }
.rail-item.active { color: var(--text); background: var(--active); box-shadow: inset 0 0 0 1px var(--divider); }

.tree-tools { display: flex; gap: 14px; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent-strong); font-size: 13px; font-weight: 600; cursor: pointer; }
.tree { overflow-x: auto; margin: 0 -6px; padding: 0 6px; position: relative; } /* full height: every node visible */
.grid-tree { display: grid; grid-template-columns: minmax(320px, 1fr) minmax(0, 1.5fr); gap: var(--gap); align-items: start; }
.tree-card { display: flex; flex-direction: column; gap: 12px; }
.tree-card .card-head { margin-bottom: 0; }
.tree-legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12.5px; color: var(--muted); padding-top: 10px; border-top: 1px solid var(--divider); }
.tree-legend span { display: inline-flex; align-items: center; gap: 5px; }
.ring { display: inline-block; width: 8px; height: 8px; border-radius: 50%; border: 2px solid var(--muted); flex: none; }
.ring.critical { border-color: var(--red); }
.ring.warning { border-color: #E58A00; }

.main-col { min-width: 0; display: flex; flex-direction: column; }

/* Top header (sticky): row 1 white - logo + app name | current section | AI status */
.site-head { position: sticky; top: 0; z-index: 30; }
.topbar {
  display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center; gap: 16px; height: 60px; padding: 0 24px; background: var(--card); border-bottom: 1px solid var(--divider);
}
.brand-logo { display: inline-flex; align-items: center; gap: 12px; min-width: 0; color: var(--text); text-decoration: none; justify-self: start; }
.brand-logo img { display: block; height: 30px; width: auto; flex: none; }
.app-title { font-size: 20px; font-weight: 700; text-align: center; white-space: nowrap; }
.top-right { justify-self: end; display: flex; align-items: center; gap: 8px; }

/* Header row 2 (blue): page title + period controls + actions, full width */
.page-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap;
  padding: 10px 24px; border-bottom: 1px solid var(--divider);
  background: linear-gradient(90deg, var(--top-from), var(--top-to));
}
.page-title { min-width: 0; }
.page-title h2 { font-size: 18px; }
.page-sub { font-size: 13px; color: #4A525D; font-weight: 600; }
.controls { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* Daily / Weekly / Monthly: segmented control, blue active segment */
.segmented { display: inline-flex; background: var(--card); border: 1px solid var(--input-border); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.segmented button { border: 0; background: transparent; height: 30px; padding: 0 16px; border-radius: 6px; font-weight: 600; cursor: pointer; color: var(--muted); }
.segmented button:hover { color: var(--text); background: var(--grey-bg); }
.segmented button[aria-checked="true"] { background: var(--accent-strong); color: #fff; }
.date-nav { display: inline-flex; align-items: center; gap: 4px; }
.date-nav input, .date-nav select {
  height: 38px; padding: 0 10px; border: 1px solid var(--input-border); border-radius: var(--radius-sm); background: var(--card);
}
.date-nav select { min-width: 140px; font-weight: 600; cursor: pointer; }
.date-nav input:invalid, .field input:invalid { border-color: var(--red); }

.btn {
  display: inline-flex; align-items: center; gap: 8px; height: 38px; padding: 0 16px; border-radius: var(--radius-sm);
  border: 1px solid var(--divider); background: var(--card); font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn.primary { background: var(--text); border-color: var(--text); color: #fff; }
.btn.primary:hover { background: #20252B; }
.btn.ghost:hover { background: var(--bg); }
.btn.small { height: 32px; padding: 0 12px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: progress; }
.icon-btn {
  width: 36px; height: 36px; display: inline-grid; place-items: center; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: transparent; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: rgba(255, 255, 255, .55); border-color: rgba(48, 54, 62, .1); }

.content { padding: 24px; display: flex; flex-direction: column; gap: var(--gap); max-width: 1760px; width: 100%; margin: 0 auto; }

/* Main area + right panel */
.workspace { display: grid; grid-template-columns: minmax(0, 1fr) var(--panel-w); gap: var(--gap); align-items: start; }
.main-area { min-width: 0; display: flex; flex-direction: column; gap: var(--gap); container: main / inline-size; }
.right-panel { position: sticky; top: calc(var(--topbar-h) + 16px); max-height: calc(100vh - var(--topbar-h) - var(--status-h) - 32px); overflow: auto; border-radius: var(--radius); }
.right-panel .details { min-height: 0; }
/* nothing selected: the details panel is hidden and the main area takes the full width */
.workspace.no-details { grid-template-columns: minmax(0, 1fr); }
.workspace.no-details .right-panel { display: none; }
.d-close { flex: none; width: 30px; height: 30px; margin: -4px -6px 0 0; color: var(--muted); }
.d-close:hover { color: var(--text); background: var(--bg); }

/* Bottom status bar */
.statusbar {
  position: sticky; bottom: 0; z-index: 30; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 22px;
  min-height: 34px; padding: 6px 24px; background: var(--card); border-top: 1px solid var(--divider); font-size: 12.5px; color: var(--muted);
}
.sb-item { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.sb-copy { margin-left: auto; white-space: nowrap; }
.content.loading .section { opacity: .55; transition: opacity .15s; }
.section { min-width: 0; }
.card { background: var(--card); border: 1px solid var(--divider); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; min-width: 0; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.card-head p { font-size: 13px; margin-top: 2px; }
.grid-2-1 { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: var(--gap); align-items: start; }

/* ---------- Messages ---------- */
.messages:empty { display: none; }
.messages { display: flex; flex-direction: column; gap: 8px; }
.msg { padding: 10px 14px; border-radius: var(--radius-sm); border: 1px solid var(--divider); background: var(--card); font-size: 14px; }
.msg.warn { background: var(--amber-bg); border-color: #F2D3A6; }
.msg.error { background: var(--red-bg); border-color: #F1C1BB; color: #7D211A; }
.msg.ok { background: var(--green-bg); border-color: #CFE5C8; }

/* ---------- Status ---------- */
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.critical { background: var(--red); }
.dot.warning { background: #E58A00; }
.dot.normal { background: var(--green); }
.dot.info { background: var(--info); }
.dot.nodata { background: #C3C9D2; }
.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12.5px; font-weight: 600; white-space: nowrap;
  background: var(--grey-bg); color: var(--muted);
}
.badge.normal { background: var(--green-bg); color: var(--green); }
.badge.warning { background: var(--amber-bg); color: var(--amber); }
.badge.critical { background: var(--red-bg); color: var(--red); }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.estimated { background: #EEF0F4; color: #5D6672; border: 1px dashed var(--input-border); }
.dot.estimated { background: transparent; border: 1.5px dashed #8A93A0; width: 9px; height: 9px; }
.kpi.is-est .kpi-value { color: #5D6672; }
.kpi-total { border-color: #9DB0E6; box-shadow: inset 0 3px 0 var(--accent-strong), var(--shadow); }
.metric-grid .est-note { grid-column: 1 / -1; background: #F4F6FA; border-style: dashed; font-size: 13px; color: #5D6672; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; border: 1px solid var(--divider); background: var(--bg); }

/* ---------- KPIs ---------- */
.kpi-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); grid-auto-rows: 1fr; gap: 16px; } /* 9 KPIs: three even rows */
.kpi { background: var(--card); border: 1px solid var(--divider); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.kpi-label { font-size: 13px; color: var(--muted); font-weight: 600; display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.kpi-value { font-size: 28px; font-weight: 700; line-height: 1.15; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi-value small { font-size: 14px; font-weight: 600; color: var(--muted); margin-left: 4px; letter-spacing: 0; }
.kpi-foot { font-size: 13px; color: var(--muted); }
.delta.up { color: var(--red); } .delta.down { color: var(--green); }

/* ---------- Flow ---------- */
#flow { container: flow / inline-size; }
.flow { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 0; }
.flow-step { position: relative; display: flex; flex-direction: column; align-items: flex-start; padding: 14px 18px 14px 14px; border: 1px solid var(--divider); background: var(--card); min-width: 0; cursor: pointer; text-align: left; }
.flow-step:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); }
.flow-step:last-child { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.flow-step + .flow-step { border-left: 0; }
.flow-step:hover { background: #F8FAFC; }
.flow-step::after {
  content: ""; position: absolute; right: -7px; top: 50%; width: 12px; height: 12px; transform: translateY(-50%) rotate(45deg);
  background: inherit; border-top: 1px solid var(--divider); border-right: 1px solid var(--divider); z-index: 1;
}
.flow-step:last-child::after { display: none; }
.flow-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); display: block; width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.flow-value { max-width: 100%; font-size: 19px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.flow-value small { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 3px; }
.flow-meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; line-height: 1.35; }
.flow-meta b { color: var(--text); font-weight: 600; }
.flow-step .badge { margin-top: auto; }
.flow-meta { margin-bottom: 8px; }

/* ---------- Chart ---------- */
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 16px; height: 2px; border-radius: 2px; display: inline-block; }
.chart { position: relative; height: 290px; }
.chart svg, .spark svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .grid line, .spark .grid line { stroke: #E7EBF0; stroke-width: 1; }
.chart .axis text, .spark .axis text { fill: var(--muted); font-size: 11.5px; }
.chart .series path, .spark .series path { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .end-label { font-size: 12px; fill: var(--text); font-weight: 600; }
.chart .crosshair, .spark .crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .limit { stroke: var(--red); stroke-width: 1; stroke-dasharray: 2 4; opacity: .5; }
.chart:focus-visible, .spark:focus-visible { outline-offset: 4px; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--card); border: 1px solid var(--divider); border-radius: var(--radius-sm);
  box-shadow: 0 6px 18px rgba(48, 54, 62, .12); padding: 8px 10px; font-size: 12.5px; min-width: 150px; z-index: 5;
}
.tooltip .t-head { color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; }
.tooltip .t-row i { width: 12px; height: 2px; border-radius: 2px; }
.tooltip .t-row b { font-variant-numeric: tabular-nums; min-width: 52px; }
.tooltip .t-row span { color: var(--muted); }
.spark { height: 100px; margin-top: 4px; position: relative; }

/* ---------- Alerts ---------- */
.alert-counts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.alert-count { border: 1px solid var(--divider); border-radius: var(--radius-sm); padding: 10px 12px; }
.alert-count b { display: block; font-size: 22px; line-height: 1.1; }
.alert-count span { font-size: 12.5px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.alert-count.critical b { color: var(--red); }
.alert-count.warning b { color: var(--amber); }
.loss-summary tr.total td { font-weight: 700; border-top: 2px solid var(--divider); background: #F7F9FB; }
.table.loss-summary .sub, .table.loss-summary .id, .table.loss-summary th { white-space: normal; }
/* billing has 11 columns: compact cells and wrapping headers so it fits its card */
#billingTable th { white-space: normal; vertical-align: bottom; }
#billingTable { font-size: 13.5px; }
#billingTable th, #billingTable td { padding: 9px 7px; }

.loss-summary tr[data-goto-tab] { cursor: pointer; }
.sub-head { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 18px 0 10px; }
.inv:focus { outline: none; }
.inv.flash { animation: flash 1.2s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 3px var(--accent-strong); } 100% { box-shadow: 0 0 0 3px transparent; } }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--divider); border-radius: var(--radius-sm); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  text-align: left; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted);
  padding: 10px 12px; background: #F7F9FB; border-bottom: 1px solid var(--divider); white-space: nowrap; position: sticky; top: 0;
}
.table th.num { text-align: right; }
.table th[data-sort] { cursor: pointer; user-select: none; }
.table th[data-sort]:hover { color: var(--text); }
.table th .arrow { font-size: 10px; margin-left: 3px; }
.table td { padding: 10px 12px; border-bottom: 1px solid #E9EDF2; vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr.clickable { cursor: pointer; }
.table tbody tr.clickable:hover td { background: #F8FAFC; }
.table tbody tr.selected td { background: #F2F5FB; }
.table tbody tr.row-critical td { background: var(--red-bg); }
.table tbody tr.row-warning td { background: var(--amber-bg); }
.table tbody tr.row-critical td:first-child { box-shadow: inset 3px 0 0 var(--red); }
.table tbody tr.row-warning td:first-child { box-shadow: inset 3px 0 0 #E58A00; }
.table tbody tr.row-critical:hover td { background: #FBE3E0; }
.table tbody tr.row-warning:hover td { background: #FFEBCC; }
.table tbody tr.row-critical.selected td { background: #F8D7D3; }
.table tbody tr.row-warning.selected td { background: #FFE2B8; }

/* Loss hotspots */
.hotspots { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.hotspots > .msg { grid-column: 1 / -1; }
.hot {
  display: flex; flex-direction: column; gap: 4px; text-align: left; cursor: pointer; min-width: 0;
  border: 1px solid var(--divider); border-radius: var(--radius-sm); padding: 12px 14px 12px 16px; background: var(--card); color: var(--text);
}
.hot.critical { background: var(--red-bg); border-color: #F1C1BB; box-shadow: inset 4px 0 0 var(--red); }
.hot.warning { background: var(--amber-bg); border-color: #F2D3A6; box-shadow: inset 4px 0 0 #E58A00; }
.hot:hover { filter: brightness(.985); }
.hot.selected { outline: 2px solid var(--text); outline-offset: 1px; }
.hot:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 2px; }
.hot-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.hot-type { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.hot-type .ico { width: 14px; height: 14px; }
.hot-id { font-size: 17px; font-weight: 700; display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; }
.hot-id span { font-size: 12.5px; font-weight: 400; color: var(--muted); }
.hot-value { font-size: 28px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.hot.critical .hot-value { color: var(--red); }
.hot.warning .hot-value { color: var(--amber); }
.hot-bar { position: relative; height: 6px; border-radius: 3px; background: rgba(48, 54, 62, .1); margin: 4px 0 2px; }
.hot-bar i { position: absolute; inset: 0 auto 0 0; border-radius: 3px; }
.hot.critical .hot-bar i { background: var(--red); }
.hot.warning .hot-bar i { background: #E58A00; }
.hot-bar b { position: absolute; top: -3px; width: 2px; height: 12px; background: var(--text); opacity: .45; border-radius: 1px; }
.hot-foot { font-size: 12.5px; color: var(--muted); }
.table.compact td, .table.compact th { padding: 8px 10px; }
.table .id { font-weight: 600; white-space: nowrap; }
.table .sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.table .empty-row td { text-align: center; color: var(--muted); padding: 22px; }


.tabs { display: flex; flex-wrap: wrap; gap: 2px; border-bottom: 1px solid var(--divider); margin-bottom: 14px; }
.tabs button { border: 0; background: none; padding: 8px 14px; font-weight: 600; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--text); }
.tabs .count { font-size: 12px; margin-left: 4px; padding: 0 6px; border-radius: 999px; background: var(--red-bg); color: var(--red); }

/* ---------- Stat strip ---------- */
.stat-strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 0; border: 1px solid var(--divider); border-radius: var(--radius-sm); margin-bottom: 14px; }
.stat { padding: 12px 14px; border-right: 1px solid var(--divider); min-width: 0; }
.stat:last-child { border-right: 0; }
.stat span { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.stat b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat b small { font-size: 12px; font-weight: 600; color: var(--muted); margin-left: 3px; }

/* ---------- Details ---------- */
.details { min-height: 200px; }
.details .empty, .empty { color: var(--muted); padding: 28px 8px; text-align: center; font-size: 14px; }
.d-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.d-icon { width: 40px; height: 40px; border-radius: 10px; background: var(--accent); display: grid; place-items: center; flex: none; }
.d-title { font-size: 18px; font-weight: 700; word-break: break-word; }
.d-path { font-size: 12.5px; color: var(--muted); }
.d-badges { flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.metric-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 14px; }
.metric { background: var(--bg); border-radius: var(--radius-sm); padding: 9px 12px; min-width: 0; }
.metric span { font-size: 12px; color: var(--muted); font-weight: 600; display: block; }
.metric b { font-size: 16px; font-variant-numeric: tabular-nums; word-break: break-word; }
.d-section { margin-top: 16px; }
.d-section h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 8px; }
.d-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

/* ---------- Tree ---------- */
.tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul ul { padding-left: 6px; margin-left: 10px; border-left: 1px solid var(--divider); }
.tree li { margin: 1px 0; }
.t-node { display: flex; align-items: center; gap: 2px; border-radius: var(--radius-sm); min-height: 32px; }
.t-node:hover { background: rgba(255, 255, 255, .7); }
.t-node.selected { background: var(--active); box-shadow: inset 0 0 0 1px var(--divider); }
.t-caret { width: 22px; height: 22px; border: 0; background: none; display: grid; place-items: center; cursor: pointer; color: var(--muted); border-radius: 5px; flex: none; padding: 0; }
.t-caret .ico { width: 14px; height: 14px; transition: transform .12s; }
.t-caret[aria-expanded="true"] .ico { transform: rotate(90deg); }
.t-caret.leaf { visibility: hidden; width: 8px; }
.t-label { flex: 1; min-width: 0; display: flex; align-items: center; gap: 7px; border: 0; background: none; padding: 5px 6px 5px 2px; cursor: pointer; text-align: left; border-radius: 6px; }
.t-label .ico { width: 15px; height: 15px; color: var(--muted); }
.t-name { flex: 1; min-width: 3.5em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; font-weight: 600; }
.t-node[data-type="meter"] .t-name { font-weight: 400; }
.t-val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; flex: none; }
.t-mark { width: 10px; display: inline-flex; justify-content: center; flex: none; }
[hidden] { display: none !important; }

/* ---------- Investigations ---------- */
.inv-split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 16px; align-items: start; }
.inv-pane { position: sticky; top: calc(var(--topbar-h) + 16px); min-width: 0; }
.inv-pane .inv-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.inv-pane .inv-text { grid-template-columns: minmax(0, 1fr); }
.inv-pane .d-actions { margin-top: 0; }
#alertTable tr.selected td:first-child { box-shadow: inset 4px 0 0 var(--text); }
.inv-id { font-weight: 700; font-size: 15px; }
.inv { border: 1px solid var(--divider); border-left-width: 4px; border-radius: var(--radius-sm); padding: 14px 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.inv.clickable { cursor: pointer; transition: background .12s, box-shadow .12s; }
.inv.clickable:hover { background: #F8FAFC; box-shadow: 0 2px 10px rgba(48, 54, 62, .08); }
.inv.clickable:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 2px; }
.inv.critical { border-left-color: var(--red); }
.inv.warning { border-left-color: #E58A00; }
.inv.info { border-left-color: var(--info); }
.inv-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.inv-asset { font-weight: 700; font-size: 15px; border: 0; background: none; padding: 0; cursor: pointer; color: var(--text); text-decoration: underline; text-decoration-color: var(--divider); text-underline-offset: 3px; }
.inv-asset:hover { text-decoration-color: var(--text); }
.inv-type { font-size: 13px; color: var(--muted); }
.inv-period { margin-left: auto; font-size: 13px; color: var(--muted); }
.inv-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.inv-metrics div { background: var(--bg); border-radius: 6px; padding: 7px 10px; min-width: 0; }
.inv-metrics span { font-size: 11.5px; color: var(--muted); font-weight: 600; display: block; }
.inv-metrics b { font-size: 14px; font-variant-numeric: tabular-nums; }
.inv-text { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; font-size: 14px; }
.inv-text span { display: block; font-size: 11.5px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 2px; }
.filters { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.chips { display: inline-flex; gap: 4px; }
.chips button { border: 1px solid var(--divider); background: var(--card); border-radius: 999px; padding: 4px 12px; font-size: 13px; font-weight: 600; cursor: pointer; color: var(--muted); }
.chips button[aria-checked="true"] { background: var(--text); border-color: var(--text); color: #fff; }
select, .field input {
  height: 36px; padding: 0 10px; border: 1px solid var(--input-border); border-radius: var(--radius-sm); background: var(--card); max-width: 100%;
}
.field input { width: 100%; }
.field input.bad, .field select.bad { border-color: var(--red); background: var(--red-bg); }
#genDialog { width: min(560px, calc(100vw - 32px)); }
/* plant is a fixed list: shown as a plain box, no dropdown arrow */
#genPlantSelect { appearance: none; -webkit-appearance: none; -moz-appearance: none; background-image: none; }
#genPlantSelect:disabled { opacity: 1; color: var(--text); cursor: default; }
.gen-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 12px; }
.gen-date { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; font-size: 13.5px; padding: 8px 12px; border-radius: var(--radius-sm); background: var(--bg); }
.gen-date input { height: 32px; padding: 0 8px; border: 1px solid var(--input-border); border-radius: var(--radius-sm); background: var(--card); }
#genEnergy { text-align: right; font-variant-numeric: tabular-nums; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.check input, .radio input { accent-color: var(--text); width: 16px; height: 16px; }

/* ---------- AI ---------- */
.ai-form { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.ai-focus { font-size: 14px; color: var(--muted); display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ai-focus strong { color: var(--text); }
.ai-row { display: flex; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.field-error { color: var(--red); font-size: 13.5px; font-weight: 600; }
.field-error:empty { display: none; }
.ai-output { border: 1px solid var(--divider); border-radius: var(--radius-sm); padding: 4px 22px 18px; background: #FBFCFD; font-size: 15px; }
.ai-output h2 { font-size: 16px; margin: 18px 0 6px; }
.ai-output h3 { font-size: 15px; margin: 14px 0 4px; }
.ai-output ul, .ai-output ol { padding-left: 22px; margin: 6px 0; }
.ai-output li { margin: 3px 0; }
.ai-output p { margin: 6px 0; }
.ai-output code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: 13.5px; }
.ai-output table { border-collapse: collapse; margin: 8px 0; font-size: 14px; display: block; overflow-x: auto; }
.ai-output th, .ai-output td { border: 1px solid var(--divider); padding: 5px 9px; text-align: left; }
.ai-meta { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12.5px; color: var(--muted); padding-top: 14px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--divider); border-top-color: var(--text); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Data ---------- */
.issues { margin-top: 12px; font-size: 13.5px; }
.issues li { margin: 2px 0; color: #7D211A; }
.history { margin-top: 14px; }
.gen-entries { margin-top: 14px; }
.gen-entries .head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.link-btn.danger { color: var(--red); display: inline-flex; align-items: center; gap: 4px; }
.link-btn.danger .ico { width: 15px; height: 15px; }
.link-btn:disabled { opacity: .5; cursor: default; }

/* ---------- Modal ---------- */
.modal { border: 0; padding: 0; border-radius: var(--radius); width: min(640px, calc(100vw - 32px)); max-height: calc(100vh - 48px); box-shadow: 0 20px 60px rgba(30, 36, 44, .25); color: var(--text); }
.modal::backdrop { background: rgba(48, 54, 62, .35); }
.modal-shell { display: flex; flex-direction: column; max-height: calc(100vh - 48px); }
.modal-head { display: flex; justify-content: space-between; gap: 12px; padding: 20px 22px 14px; border-bottom: 1px solid var(--divider); }
.modal-head p { font-size: 13.5px; margin-top: 2px; }
.modal-body { padding: 18px 22px; overflow: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 22px; border-top: 1px solid var(--divider); background: #FAFBFC; }
.drop {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 22px; border: 1.5px dashed var(--input-border); border-radius: var(--radius-sm);
  background: var(--bg); cursor: pointer; text-align: center; color: var(--muted);
}
.drop strong { color: var(--text); }
.drop.drag { border-color: var(--accent-strong); background: #E9EFFD; }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop:focus-within { border-color: var(--accent-strong); }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; border: 0; padding: 0; margin: 0; min-width: 0; }
.field > span, .field legend { font-weight: 600; font-size: 13px; padding: 0; }
.field select { width: 100%; }
.radio { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.columns-hint { font-size: 13px; color: var(--muted); background: var(--bg); border-radius: var(--radius-sm); padding: 10px 12px; }
.columns-hint code { color: var(--text); font-size: 12.5px; }
.columns-hint summary { cursor: pointer; color: var(--text); font-weight: 600; }
.columns-hint p { margin-top: 8px; }
.tpl-steps { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 4px; }
.drop.busy { pointer-events: none; opacity: .75; }
.upload-result > .btn { margin-top: 10px; }
.upload-result:empty { display: none; }
.result-block { border: 1px solid var(--divider); border-radius: var(--radius-sm); padding: 12px 14px; margin-top: 8px; font-size: 14px; }
.result-block ul { margin: 6px 0 0; padding-left: 18px; max-height: 180px; overflow: auto; }
.result-block li { margin: 2px 0; }
.result-block .err { color: #7D211A; }
.result-block .wrn { color: var(--amber); }


/* ---------- Responsive ---------- */
/* Layout inside the main area follows its own width (it shrinks when the right panel is shown). */
@container main (max-width: 960px) {
  .grid-2-1, .grid-tree, .inv-split { grid-template-columns: minmax(0, 1fr); }
  .inv-pane { position: static; order: -1; }
  .inv-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@container main (max-width: 760px) {
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-grid > .kpi:last-child:nth-child(odd) { grid-column: 1 / -1; } /* 9 KPIs: the last one takes the full row */
  .inv-text { grid-template-columns: minmax(0, 1fr); }
  /* alerts table becomes stacked cards */
  .table.stack thead { display: none; }
  .table.stack, .table.stack tbody { display: block; }
  .table.stack tr { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px 12px; padding: 12px 14px; border-bottom: 1px solid var(--divider); }
  .table.stack td { display: block; padding: 0; border: 0; text-align: left; background: none !important; box-shadow: none !important; }
  .table.stack td::before { content: attr(data-label); display: block; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
  .table.stack tr.row-critical { background: var(--red-bg); box-shadow: inset 3px 0 0 var(--red); }
  .table.stack tr.row-warning { background: var(--amber-bg); box-shadow: inset 3px 0 0 #E58A00; }
  .table.stack .empty-row { display: block; }
  .table.stack td:first-child { grid-column: 1 / -1; }
  .table.stack td:empty { display: none; }
  .table.stack tr.total { background: #F7F9FB; }
}
@container main (max-width: 520px) {
  #alertTable .col-date { display: none; }
  #alertTable th { white-space: normal; }
  #alertTable th, #alertTable td { padding-left: 8px; padding-right: 8px; }
  #alertTable .badge { padding: 2px 7px; font-size: 12px; }
}
@container main (max-width: 480px) {
  .kpi-grid { gap: 10px; }
  .kpi { padding: 12px; }
  .kpi-label { flex-wrap: wrap; font-size: 12px; gap: 4px; }
  .kpi-value { font-size: 22px; }
  .kpi-value small { font-size: 12px; margin-left: 2px; }
  .kpi-foot { font-size: 12px; }
  .hotspots { grid-template-columns: minmax(0, 1fr); }
  .inv-metrics { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
}
@container flow (max-width: 1100px) {
  .flow { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
  .flow-step, .flow-step + .flow-step { border: 1px solid var(--divider); border-radius: var(--radius-sm); }
  .flow-step::after { right: -12px; }
  .flow-step:nth-child(4n)::after { display: none; }
}
@container flow (max-width: 600px) {
  /* vertical flow with arrows pointing down */
  .flow { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .flow-step::after, .flow-step:nth-child(4n)::after {
    display: block; right: auto; left: 50%; top: auto; bottom: -15px; transform: translateX(-50%) rotate(135deg);
  }
  .flow-step:last-child::after { display: none; }
}

@media (max-width: 1240px) {
  /* keep the controls on one line: the action buttons show only their icons */
  #openGen span, #openUpload span { display: none; }
  #openGen, #openUpload { padding: 0 11px; }
}
@media (max-width: 1199px) {
  /* right panel content moves under the hierarchy (see placeDetails in app.js) */
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .right-panel { display: none; }
}
@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .rail {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; flex-direction: row; justify-content: space-around;
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--divider); gap: 0;
  }
  .rail-item { width: auto; flex: 1 1 0; min-width: 0; padding: 6px 0; font-size: 10px; }
  .rail-item span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .content { padding: 16px; }
  /* the rail is the fixed bottom bar here, so the status bar sits at the end of the page above it */
  .statusbar { position: static; padding: 10px 16px calc(74px + env(safe-area-inset-bottom)); }
  .gen-grid { grid-template-columns: 1fr; }
  html { scroll-padding-top: calc(var(--topbar-h) + 12px); }
}
@media (max-width: 767px) {
  .topbar { grid-template-columns: minmax(0, 1fr) auto; height: 56px; padding: 0 16px; gap: 10px; }
  .topbar { grid-template-columns: auto minmax(0, 1fr) auto; }
  .brand-logo img { height: 22px; }
  .app-title { font-size: 15px; white-space: normal; line-height: 1.2; }
  .ai-status { font-size: 12px; padding: 3px 8px; }
  /* only the controls row stays pinned on phones; the brand row scrolls away to save height */
  .site-head { display: contents; }
  .page-head { position: sticky; top: 0; z-index: 30; padding: 8px 16px; }
  .page-title { display: none; }
  .controls { width: 100%; display: grid; grid-template-columns: minmax(0, 1fr) auto auto; gap: 8px; }
  .segmented { grid-column: 1 / -1; width: 100%; }
  .segmented button { flex: 1; }
  .date-nav { min-width: 0; }
  .date-nav input, .date-nav select { flex: 1; min-width: 0; }
  .card { padding: 16px; }
  .tree > ul { min-width: 300px; } /* scrolls inside its panel instead of squeezing names */
  .ai-row { flex-direction: column; }
  .stat { border-right: 0; border-bottom: 1px solid var(--divider); }
  .inv-period { margin-left: 0; width: 100%; }
  .metric-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .sb-item { white-space: normal; }
  .sb-copy { margin-left: 0; width: 100%; white-space: normal; padding-top: 6px; border-top: 1px solid var(--divider); }
}
@media (max-width: 420px) {
  /* eight menu items share the bottom bar */
  .rail { padding-left: 2px; padding-right: 2px; }
  .rail-item { font-size: 9px; letter-spacing: -.01em; }
  .rail-item[data-short] span { display: none; }
  .rail-item[data-short]::after { content: attr(data-short); }
  .hot { padding: 10px 12px 10px 14px; }
  .hot-value { font-size: 24px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}

/* ---- 3D power line view (power3d.js) ---- */
.p3d-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin: 22px 0 12px; }
.p3d-head h3 { font-size: 15px; font-weight: 700; }
.p3d-head p { font-size: 13px; margin-top: 2px; max-width: 62ch; }
.p3d-actions { display: flex; gap: 8px; }
.power3d { position: relative; height: 440px; border-radius: var(--radius-sm); overflow: hidden; background: #0E1A2B; }
.p3d-canvas, .p3d-canvas canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.p3d-labels { position: absolute; inset: 0; pointer-events: none; }
.p3d-label {
  position: absolute; left: 0; top: 0; pointer-events: auto; cursor: pointer; text-align: left; font: inherit;
  display: flex; flex-direction: column; gap: 1px; padding: 6px 9px 7px; min-width: 96px; max-width: 170px;
  color: #E8EEF6; background: rgba(14, 26, 43, .82); border: 1px solid rgba(255, 255, 255, .14); border-left: 3px solid var(--c, #5FD4FF);
  border-radius: 6px; backdrop-filter: blur(4px); box-shadow: 0 4px 14px rgba(0, 0, 0, .35); will-change: transform; white-space: nowrap;
}
.p3d-label:hover, .p3d-label:focus-visible { background: rgba(28, 46, 71, .95); outline: none; border-color: rgba(255, 255, 255, .35); border-left-color: var(--c, #5FD4FF); }
.p3d-label b { font-size: 15px; font-variant-numeric: tabular-nums; }
.p3d-t { font-size: 10.5px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: #A9B7C9; }
.p3d-s { font-size: 11.5px; color: #B8C4D3; }
.p3d-unexp { color: #D8B4FE; }
.p3d-badge { display: inline-flex; align-items: center; gap: 5px; font-size: 11px; font-weight: 600; color: var(--c); margin-top: 2px; }
.p3d-badge i { width: 7px; height: 7px; border-radius: 50%; background: var(--c); }
.p3d-legend { position: absolute; left: 12px; bottom: 10px; display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: 11.5px; color: #C9D3DF; pointer-events: none; max-width: calc(100% - 24px); }
.p3d-legend span { display: inline-flex; align-items: center; gap: 5px; }
.p3d-legend i { width: 9px; height: 9px; border-radius: 50%; background: var(--c); box-shadow: 0 0 6px var(--c); }
.p3d-hint { position: absolute; right: 12px; top: 10px; font-size: 11.5px; color: #8EA0B6; pointer-events: none; }
.p3d-empty, .p3d-fallback { display: none; position: absolute; inset: 0; place-items: center; color: #C9D3DF; font-size: 14px; background: rgba(14, 26, 43, .55); pointer-events: none; }
.p3d-fallback { display: grid; }
.power3d.no-data .p3d-empty { display: grid; }
@container flow (max-width: 700px) {
  .power3d { height: 340px; }
  .p3d-label { min-width: 0; padding: 4px 7px; }
  .p3d-label b { font-size: 13px; }
  .p3d-label .p3d-s, .p3d-hint { display: none; }
  .p3d-legend { font-size: 10.5px; }
}
@media (prefers-reduced-motion: reduce) { .p3d-label { transition: none; } }
