:root {
  --bg: #0b1220;
  --surface: #111c2e;
  --surface-strong: #17243a;
  --line: #2c3b52;
  --text: #eef4fb;
  --muted: #95a4b8;
  --cyan: #38bdf8;
  --green: #2dd4a3;
  --yellow: #f7c948;
  --orange: #f59e5b;
  --red: #fb7185;
  --blue: #60a5fa;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, "Noto Sans SC", sans-serif;
  letter-spacing: 0;
}

button,
a { font: inherit; }

.topbar {
  min-height: 76px;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 28px;
  background: #101a2a;
  border-bottom: 1px solid var(--line);
}

.brand-block { min-width: 240px; }
.brand-name { color: var(--cyan); font-size: 20px; font-weight: 800; }
.brand-section { margin-top: 3px; color: #cbd5e1; font-size: 13px; }

.primary-nav {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
}

.primary-nav a {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  padding: 0 15px;
  border: 1px solid transparent;
  border-radius: 6px;
  color: #cbd5e1;
  text-decoration: none;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
}

.primary-nav a:hover { border-color: #3c4d66; background: #17243a; }
.primary-nav a.active { color: #082f49; background: var(--cyan); }

.topbar-actions { display: flex; align-items: center; gap: 14px; }
.updated-at { color: var(--muted); font-size: 12px; white-space: nowrap; }

#refresh-button {
  min-width: 64px;
  min-height: 38px;
  border: 1px solid #47617d;
  border-radius: 6px;
  background: #17243a;
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
}

#refresh-button:hover { border-color: var(--cyan); }
#refresh-button:disabled { cursor: wait; opacity: 0.6; }

main { width: min(1600px, 100%); margin: 0 auto; padding: 22px 28px 40px; }

.quality-band {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  min-height: 74px;
  padding: 13px 16px;
  border: 1px solid #765d22;
  border-left: 4px solid var(--yellow);
  background: #231f16;
}

.quality-copy { display: flex; align-items: center; gap: 14px; min-width: 0; }
.quality-copy strong { font-size: 14px; }
.quality-copy p { margin: 3px 0 0; color: #c8b982; font-size: 12px; line-height: 1.45; }

.quality-tag {
  flex: 0 0 auto;
  padding: 6px 9px;
  border-radius: 4px;
  background: var(--yellow);
  color: #31260b;
  font-size: 12px;
  font-weight: 800;
}

.service-statuses { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.service-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 9px;
  border: 1px solid #4a4535;
  border-radius: 4px;
  color: #cbd5e1;
  font-size: 12px;
  white-space: nowrap;
}
.service-chip b { color: var(--text); font-weight: 700; }
.service-chip i { width: 8px; height: 8px; border-radius: 50%; background: #64748b; }
.service-chip i.online { background: var(--green); }
.service-chip i.warning { background: var(--yellow); }
.service-chip i.offline { background: var(--red); }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(8, minmax(120px, 1fr));
  margin-top: 16px;
  border: 1px solid var(--line);
  background: var(--surface);
}

.kpi {
  min-height: 92px;
  padding: 15px 14px;
  border-right: 1px solid var(--line);
}
.kpi:last-child { border-right: 0; }
.kpi span { display: block; color: var(--muted); font-size: 12px; }
.kpi strong { display: inline-block; margin-top: 8px; font-size: 23px; line-height: 1; }
.kpi small { margin-left: 5px; color: #a8b5c6; font-size: 11px; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(300px, 0.75fr);
  gap: 16px;
  margin-top: 16px;
}

.panel {
  min-width: 0;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.panel-heading,
.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.section-label {
  display: block;
  margin-bottom: 5px;
  color: var(--cyan);
  font-size: 11px;
  font-weight: 800;
}

h1,
h2 { margin: 0; font-size: 18px; line-height: 1.25; }
.panel-meta { color: var(--muted); font-size: 12px; white-space: nowrap; }

.power-rows { display: grid; gap: 15px; }
.power-label { display: flex; align-items: center; justify-content: space-between; margin-bottom: 7px; font-size: 13px; }
.power-label span { color: #cbd5e1; }
.power-label b { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.power-track { height: 11px; overflow: hidden; background: #263449; border-radius: 3px; }
.power-fill { display: block; width: 0; height: 100%; transition: width 350ms ease; }
.power-fill.load { background: var(--orange); }
.power-fill.pv { background: var(--yellow); }
.power-fill.battery { background: var(--green); }
.power-fill.grid { background: var(--blue); }

.balance-footer,
.chart-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 18px;
  padding-top: 13px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
}
.balance-footer strong { color: #cbd5e1; }

.service-list,
.optimization-grid { margin: 0; }
.service-list div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
  min-height: 43px;
  border-bottom: 1px solid var(--line);
}
.service-list div:last-child { border-bottom: 0; }
dt { color: var(--muted); font-size: 12px; }
dd { margin: 0; color: var(--text); font-size: 13px; font-weight: 700; text-align: right; }

.chart-wrap { position: relative; width: 100%; height: 270px; }
#forecast-chart { display: block; width: 100%; height: 270px; }
.chart-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.chart-empty[hidden] { display: none; }
.legend-line { display: inline-block; width: 20px; height: 3px; margin-right: 7px; background: var(--cyan); vertical-align: middle; }
.chart-footer strong { color: var(--yellow); }

.status-badge {
  display: inline-flex;
  align-items: center;
  min-height: 27px;
  padding: 0 9px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 800;
}
.status-badge.neutral { background: #27364b; color: #cbd5e1; }
.status-badge.success { background: #123c34; color: #6ee7c2; }
.status-badge.warning { background: #413619; color: #f7d66c; }

.optimization-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 18px; }
.optimization-grid div { min-width: 0; padding: 11px 0; border-bottom: 1px solid var(--line); }
.optimization-grid dd { margin-top: 6px; overflow-wrap: anywhere; text-align: left; }
.optimization-note { margin: 16px 0 0; color: #d1b878; font-size: 11px; line-height: 1.55; }

.site-section { margin-top: 16px; padding: 20px 0 0; border-top: 1px solid var(--line); }
.site-section .section-heading { padding: 0 4px; }
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
table { width: 100%; min-width: 980px; border-collapse: collapse; font-size: 12px; }
th { padding: 12px 14px; background: var(--surface-strong); color: var(--muted); font-weight: 700; text-align: left; white-space: nowrap; }
td { padding: 13px 14px; border-top: 1px solid var(--line); color: #dbe5f1; white-space: nowrap; }
tbody tr:hover { background: #152238; }
.site-name { color: var(--text); font-weight: 700; }
.site-id { display: block; margin-top: 3px; color: #718198; font-size: 10px; }
.state { display: inline-flex; align-items: center; gap: 6px; }
.state::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--green); }
.state.warning::before { background: var(--yellow); }
.state.stale::before { background: var(--red); }
.empty-cell { height: 110px; color: var(--muted); text-align: center; }

.page-error {
  position: fixed;
  right: 24px;
  bottom: 24px;
  max-width: min(440px, calc(100vw - 48px));
  padding: 12px 15px;
  border: 1px solid #7f3344;
  border-radius: 6px;
  background: #3b1822;
  color: #fecdd6;
  font-size: 12px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
}

@media (max-width: 1260px) {
  .kpi-grid { grid-template-columns: repeat(4, minmax(130px, 1fr)); }
  .kpi:nth-child(4) { border-right: 0; }
  .kpi:nth-child(-n+4) { border-bottom: 1px solid var(--line); }
}

@media (max-width: 900px) {
  .topbar { align-items: flex-start; flex-wrap: wrap; gap: 10px 18px; padding: 13px 18px; }
  .brand-block { flex: 1; min-width: 220px; }
  .primary-nav { order: 3; flex: 0 0 100%; width: 100%; overflow-x: auto; }
  .topbar-actions { margin-left: auto; }
  main { padding: 16px 18px 32px; }
  .quality-band { align-items: flex-start; flex-direction: column; }
  .service-statuses { justify-content: flex-start; }
  .workspace { grid-template-columns: 1fr; }
}

@media (max-width: 620px) {
  .updated-at { display: none; }
  .primary-nav a { min-height: 38px; padding: 0 12px; }
  main { padding-left: 12px; padding-right: 12px; }
  .quality-copy { align-items: flex-start; }
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi { min-height: 84px; border-bottom: 1px solid var(--line); }
  .kpi:nth-child(even) { border-right: 0; }
  .kpi:nth-last-child(-n+2) { border-bottom: 0; }
  .kpi strong { font-size: 20px; }
  .panel { padding: 16px; }
  .optimization-grid { grid-template-columns: 1fr; }
  .chart-wrap,
  #forecast-chart { height: 230px; }
  .balance-footer { align-items: flex-start; flex-direction: column; }
}
