/* Smart Meter PLN dashboard.
   Palette derives from the reader's theme where possible; explicit dark values
   are used because the app is designed dark-first (meter rooms, night shifts). */

:root {
  color-scheme: dark light;
  --bg: #0b1015;
  --surface: #131b23;
  --surface-2: #1a242e;
  --border: #253039;
  --fg: #e8eef3;
  --muted: #8fa3b3;
  --accent: #4cc2ff;
  --accent-2: #7cf7c4;
  --success: #46d18a;
  --warning: #f2b544;
  --error: #ff6b6b;
  --c1: #4cc2ff;
  --c2: #7cf7c4;
  --c3: #f2b544;
  --c4: #c792ea;
  --c5: #ff8a65;
  --c6: #64b5f6;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f7f9;
    --surface: #ffffff;
    --surface-2: #eef2f6;
    --border: #d7e0e8;
    --fg: #14202b;
    --muted: #5b6f7f;
    --accent: #0a7ea4;
    --accent-2: #12805c;
    --success: #12805c;
    --warning: #a4700a;
    --error: #c62828;
    --c1: #0a7ea4;
    --c2: #12805c;
    --c3: #a4700a;
    --c4: #6a3f9e;
    --c5: #c2521e;
    --c6: #1d5fa8;
    --shadow: 0 4px 16px rgba(20, 32, 43, .10);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .82rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
p { margin: .35rem 0; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre, .mono { font-family: var(--mono); font-size: .86rem; }
code { background: var(--surface-2); padding: .08em .35em; border-radius: 4px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.grow { flex: 1 1 220px; }
.error { color: var(--error); }
.skip-link { position: absolute; left: -999px; }
.skip-link:focus { left: 8px; top: 8px; z-index: 100; background: var(--surface); padding: .5rem .75rem; border-radius: var(--radius-sm); }

/* ---------------------------------------------------------------- topbar */
.topbar {
  display: flex; align-items: center; gap: 1rem; flex-wrap: wrap;
  padding: .7rem 1.25rem; border-bottom: 1px solid var(--border);
  background: var(--surface); position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: .6rem; }
.brand-mark { width: 28px; height: 28px; color: var(--accent); }
.brand strong { display: block; font-size: .98rem; }
.mainnav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1 1 auto; }
.mainnav a {
  padding: .35rem .6rem; border-radius: var(--radius-sm); color: var(--muted); font-size: .9rem;
}
.mainnav a:hover { background: var(--surface-2); color: var(--fg); text-decoration: none; }
.mainnav a[aria-current="page"] { background: var(--surface-2); color: var(--fg); font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: .5rem; margin-left: auto; }

/* --------------------------------------------------------------- layout */
.page { padding: 1.25rem; max-width: 1400px; margin: 0 auto; display: flex; flex-direction: column; gap: 1.1rem; }
.page-head { display: flex; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.page-head > div:first-child { flex: 1 1 320px; }
.page-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 1.1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.split { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr); gap: 1rem; }
@media (max-width: 1000px) { .split { grid-template-columns: 1fr; } }
.stack { display: flex; flex-direction: column; gap: .6rem; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .75rem; margin-bottom: .75rem; }
.card-foot { margin-top: .75rem; padding-top: .6rem; border-top: 1px solid var(--border); display: flex; align-items: center; gap: .6rem; }
.kpi-value { font-size: 1.6rem; font-weight: 650; margin: .3rem 0 0; font-variant-numeric: tabular-nums; }
.kpi { display: flex; flex-direction: column; gap: .1rem; }

/* --------------------------------------------------------------- controls */
.btn {
  display: inline-flex; align-items: center; gap: .4rem; cursor: pointer;
  padding: .42rem .8rem; border-radius: var(--radius-sm); font-size: .88rem; font-weight: 550;
  background: var(--surface-2); color: var(--fg); border: 1px solid var(--border);
}
.btn:hover { border-color: var(--accent); text-decoration: none; }
.btn-sm { padding: .28rem .55rem; font-size: .82rem; }
.btn-primary { background: var(--accent); color: #04141d; border-color: transparent; }
.btn-warn { background: var(--warning); color: #241a02; border-color: transparent; }
.btn-danger { background: var(--error); color: #2a0505; border-color: transparent; }
.btn-ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

input, select, textarea {
  background: var(--surface-2); color: var(--fg); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: .4rem .55rem; font: inherit; font-size: .88rem; width: 100%;
}
input[type="checkbox"] { width: auto; }
label { display: flex; flex-direction: column; gap: .25rem; font-size: .82rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: .4rem; color: var(--fg); }
.toolbar { display: flex; gap: .7rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: .9rem; }
.toolbar label { min-width: 120px; }
.toolbar > label.grow { flex: 1 1 240px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.seg button { background: transparent; border: 0; color: var(--muted); padding: .3rem .6rem; cursor: pointer; font: inherit; font-size: .82rem; }
.seg button.active { background: var(--surface-2); color: var(--fg); font-weight: 600; }

.pill { padding: .18rem .5rem; border-radius: 999px; font-size: .76rem; border: 1px solid var(--border); }
.pill-ok { color: var(--success); border-color: color-mix(in srgb, var(--success) 45%, var(--border)); }
.pill-warn { color: var(--warning); border-color: color-mix(in srgb, var(--warning) 45%, var(--border)); }
.pill-bad { color: var(--error); border-color: color-mix(in srgb, var(--error) 45%, var(--border)); }
.pill-muted { color: var(--muted); }

/* ----------------------------------------------------------------- tables */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.table th, .table td { text-align: left; padding: .5rem .55rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr.selected { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.table .num, .table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-compact th, .table-compact td { padding: .34rem .45rem; }

/* ------------------------------------------------------------------ lists */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .45rem; max-height: 340px; overflow-y: auto; }
.list-lg { max-height: 520px; }
.list-tight { max-height: 260px; gap: .3rem; }
.list li {
  border: 1px solid var(--border); border-left: 3px solid var(--border);
  border-radius: var(--radius-sm); padding: .5rem .65rem; background: var(--surface-2);
  display: flex; gap: .5rem; align-items: flex-start; justify-content: space-between;
}
.list li.sev-critical { border-left-color: var(--error); }
.list li.sev-warning { border-left-color: var(--warning); }
.list li.sev-info { border-left-color: var(--accent); }
.list li.sev-ok { border-left-color: var(--success); }
.list li .row-main { flex: 1 1 auto; min-width: 0; }
.list li .row-actions { display: flex; gap: .3rem; }
.list li .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bullets { margin: .3rem 0; padding-left: 1.1rem; color: var(--muted); display: flex; flex-direction: column; gap: .3rem; }
.bullets strong { color: var(--fg); }

.feed { list-style: none; margin: 0; padding: 0; font-family: var(--mono); font-size: .78rem; max-height: 360px; overflow-y: auto; display: flex; flex-direction: column-reverse; gap: .25rem; }
.feed li { padding: .3rem .45rem; border-radius: var(--radius-sm); background: var(--surface-2); display: flex; gap: .5rem; }
.feed .feed-time { color: var(--muted); flex: 0 0 62px; }
.feed .feed-topic { color: var(--accent); flex: 0 0 130px; }
.feed .feed-body { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ----------------------------------------------------------------- charts */
.chart { width: 100%; min-height: 190px; }
.chart-sm { min-height: 120px; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--border); stroke-dasharray: 2 4; }
.chart .axis-label { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--c1); }
.chart .bar:hover { fill: var(--accent-2); }
.chart .bar-dim { fill: color-mix(in srgb, var(--c1) 45%, transparent); }
.chart .line { fill: none; stroke: var(--c2); stroke-width: 2; }
.chart .area { fill: color-mix(in srgb, var(--c2) 22%, transparent); stroke: none; }
.chart .dot { fill: var(--c2); }
.chart .legend { fill: var(--muted); font-size: 11px; }
.chart .cursor-line { stroke: var(--accent); stroke-dasharray: 3 3; }
.chart-tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .35rem .5rem;
  font-size: .78rem; box-shadow: var(--shadow); white-space: nowrap;
}

/* --------------------------------------------------------------- calendar */
.calendar { display: grid; grid-template-columns: repeat(7, 1fr); gap: .3rem; }
.calendar .dow { font-size: .7rem; color: var(--muted); text-align: center; text-transform: uppercase; }
.calendar .cell {
  aspect-ratio: 1; border-radius: var(--radius-sm); border: 1px solid var(--border);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: .78rem; background: var(--surface-2); position: relative;
}
.calendar .cell.empty { border-style: dashed; background: transparent; }
.calendar .cell.future { opacity: .35; }
.calendar .cell .kwh { font-size: .68rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.forecast { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .8rem; }
.forecast .metric { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .6rem .7rem; background: var(--surface-2); }
.forecast .metric .value { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.quality { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; margin-bottom: .9rem; }
.quality .metric { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .5rem .6rem; }
.quality .metric .value { font-size: 1.1rem; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- detail */
.detail-pane { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .75rem; background: var(--surface-2); overflow-y: auto; max-height: 640px; }
.detail-pane img { max-width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.detail-pane dl { display: grid; grid-template-columns: auto 1fr; gap: .25rem .6rem; margin: .6rem 0; font-size: .84rem; }
.detail-pane dt { color: var(--muted); }
.detail-pane dd { margin: 0; font-variant-numeric: tabular-nums; }
.code { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .6rem .7rem; overflow: auto; max-height: 300px; margin: 0; white-space: pre-wrap; word-break: break-word; }

/* ---------------------------------------------------------------- modals */
.modal {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
  color: var(--fg); padding: 1.1rem; width: min(680px, 92vw); box-shadow: var(--shadow);
}
.modal::backdrop { background: rgba(0, 0, 0, .55); }
.modal h2 { margin-bottom: .8rem; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin: .9rem 0 0; padding: 0; }
.modal form { display: flex; flex-direction: column; gap: .6rem; }

.gate { position: fixed; inset: 0; background: rgba(4, 8, 12, .78); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 1rem; }
.gate-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.3rem; width: min(380px, 94vw); display: flex; flex-direction: column; gap: .7rem; box-shadow: var(--shadow); }

/* ---------------------------------------------------------------- toasts */
.toasts { position: fixed; bottom: 1rem; right: 1rem; display: flex; flex-direction: column; gap: .5rem; z-index: 60; max-width: min(420px, 92vw); }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm); padding: .55rem .7rem; box-shadow: var(--shadow); font-size: .86rem;
  animation: slide-in .18s ease-out;
}
.toast.ok { border-left-color: var(--success); }
.toast.warn { border-left-color: var(--warning); }
.toast.err { border-left-color: var(--error); }
.toast details { margin-top: .3rem; font-family: var(--mono); font-size: .76rem; color: var(--muted); }
@keyframes slide-in { from { transform: translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }

/* --------------------------------------------------------------- footer */
.footer { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; padding: 1rem 1.25rem 2rem; max-width: 1400px; margin: 0 auto; }
.error-page main { max-width: 520px; margin: 12vh auto; text-align: center; }
.error-page h1 { font-size: 3rem; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
