:root {
  --bg: #f4f6f9; --surface: #ffffff; --border: #e2e7ee; --text: #1b2430; --muted: #66758a;
  --brand: #1F3A5F; --brand-soft: #e8eef7; --accent: #2f6fdb; --danger: #c0392b; --ok: #27ae60;
  --radius: 10px; --shadow: 0 1px 2px rgba(20,35,60,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11161d; --surface: #1a212b; --border: #2b3544; --text: #e7ecf3; --muted: #93a1b5;
    --brand: #0f2744; --brand-soft: #22324a; --accent: #6ea0f5; --shadow: none;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.5 -apple-system, "Segoe UI", Roboto, Inter, sans-serif; }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0; } h2 { font-size: 15px; margin: 0 0 12px; }

.shell { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: 100vh; }
.side { background: var(--brand); color: #dbe6f5; padding: 20px 14px; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { font-weight: 700; font-size: 18px; color: #fff; padding: 4px 10px 22px; letter-spacing: .2px; }
.brand small { display: block; font-weight: 400; font-size: 12px; color: #9db4d3; }
.nav a { display: flex; gap: 10px; align-items: center; padding: 10px 12px; border-radius: 8px; color: #c5d4ea; margin-bottom: 4px; }
.nav a:hover { background: rgba(255,255,255,.08); }
.nav a.active { background: rgba(255,255,255,.16); color: #fff; font-weight: 600; }
.user { margin-top: auto; padding: 12px 10px; border-top: 1px solid rgba(255,255,255,.14); font-size: 13px; }
.user button { background: none; border: 0; color: #9db4d3; cursor: pointer; padding: 0; font: inherit; margin-top: 4px; }
.user button:hover { color: #fff; }

.main { padding: 24px 28px 48px; min-width: 0; }
.head { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.sub { color: var(--muted); font-size: 13px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 16px; }
.kpi b { display: block; font-size: 30px; line-height: 1.15; font-variant-numeric: tabular-nums; }
.kpi span { color: var(--muted); font-size: 13px; }
.two { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); margin-top: 16px; }
.wide { grid-column: 1 / -1; }
.chart-box { position: relative; height: 260px; }
.chart-box.tall { height: 320px; }

.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-bottom: 16px; }
.filters label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-width: 140px; max-width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.btn { display: inline-flex; align-items: center; gap: 6px; font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 8px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.spacer { margin-left: auto; display: flex; gap: 8px; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border); }
td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--brand-soft); }
.table-wrap { overflow-x: auto; }
.thumb { width: 52px; height: 40px; object-fit: cover; border-radius: 6px; background: var(--border); display: block; }
.code { white-space: nowrap; font-family: ui-monospace, Menlo, monospace; font-weight: 600; }
.badge { display: inline-block; white-space: nowrap; padding: 2px 10px; border-radius: 999px; font-size: 12px; color: #fff; background: var(--c, #7f8c8d); }
.muted { color: var(--muted); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: center; margin-top: 16px; }

.detail { grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 8px 12px; margin: 0; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; }
.evidence img, .evidence video { max-width: 100%; max-height: 520px; border-radius: 8px; display: block; margin-bottom: 10px; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 0 0 14px 16px; border-left: 2px solid var(--border); position: relative; margin-left: 4px; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: 4px; width: 10px; height: 10px; border-radius: 50%; background: var(--c, var(--accent)); }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; background: var(--brand-soft); border: 1px solid var(--border); }
.flash.error { color: var(--danger); }
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 380px; }
.login h1 { margin-bottom: 4px; } .login form > div { margin-bottom: 14px; } .login input { width: 100%; }
.errorlist { color: var(--danger); list-style: none; padding: 0; margin: 4px 0 0; font-size: 13px; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 14px; }
  .brand { padding: 0 8px 0 0; } .brand small { display: none; }
  .nav { display: flex; gap: 4px; } .nav a { margin: 0; padding: 8px 10px; }
  .user { margin: 0 0 0 auto; border: 0; padding: 0; }
  .main { padding: 16px 14px 40px; }
  .detail { grid-template-columns: 1fr; }
  .two { grid-template-columns: 1fr; }
}

.card form select, .card form textarea { width: 100%; }
.detail > * { min-width: 0; }

.chip { display: inline-block; padding: 1px 9px; margin: 2px 4px 2px 0; border-radius: 999px; font-size: 12px; background: var(--brand-soft); border: 1px solid var(--border); }
.detail input[readonly] { min-width: 0; }

/* ---- overview board ---- */
.kpi-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 16px; }
.kpi { position: relative; display: flex; flex-direction: column; gap: 4px; color: var(--text); overflow: hidden; transition: transform .15s, box-shadow .15s; }
.kpi::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 4px; background: var(--k, var(--accent)); }
.kpi:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(20,35,60,.12); }
.kpi-label { color: var(--muted); font-size: 13px; }
.kpi b { font-size: 34px; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -.5px; }
.kpi b small { font-size: 18px; font-weight: 600; margin-left: 2px; }
.kpi.alert { background: color-mix(in srgb, #c0392b 9%, var(--surface)); border-color: color-mix(in srgb, #c0392b 40%, var(--border)); }
.kpi.alert b { color: #c0392b; }
.kpi-foot { font-size: 12.5px; }
.delta { font-weight: 600; } .delta.up { color: #c0392b; } .delta.down { color: #27ae60; }

.board { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 16px; }
.board > .card { min-width: 0; }
.s4 { grid-column: span 4; } .s6 { grid-column: span 6; } .s8 { grid-column: span 8; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.chart-box.short { height: 230px; } .chart-box.donut { height: 190px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 6px; text-align: right; }

.seg { display: inline-flex; background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 3px; gap: 2px; }
.seg a, .seg button { font: inherit; font-size: 13px; color: var(--muted); background: none; border: 0; border-radius: 6px; padding: 5px 12px; cursor: pointer; }
.seg a:hover, .seg button:hover { color: var(--text); }
.seg .on { background: var(--accent); color: #fff; }
.seg.small button { font-size: 12px; padding: 3px 10px; }

.legend { list-style: none; margin: 10px 0 0; padding: 0; }
.legend a { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; color: var(--text); }
.legend a:hover { background: var(--brand-soft); }
.legend i { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.legend b { margin-left: auto; font-variant-numeric: tabular-nums; }

.wait { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 10px; align-items: center; padding: 10px 6px; border-bottom: 1px solid var(--border); color: var(--text); border-radius: 8px; }
.wait:last-child { border-bottom: 0; } .wait:hover { background: var(--brand-soft); }
.wait-age { font-size: 12px; font-weight: 700; text-align: center; padding: 6px 4px; border-radius: 8px; background: color-mix(in srgb, #e67e22 16%, transparent); color: #b45f0a; }
.wait-body { display: flex; flex-direction: column; font-size: 13px; min-width: 0; }
.wait-body > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

@media (max-width: 1100px) { .kpi-row { grid-template-columns: repeat(2, minmax(0, 1fr)); } .s4 { grid-column: span 6; } .s8 { grid-column: 1 / -1; } }
@media (max-width: 700px) { .kpi-row { grid-template-columns: 1fr; } .s4, .s6, .s8 { grid-column: 1 / -1; } }
@media (prefers-reduced-motion: reduce) { .kpi { transition: none; } }

/* ---- varied widgets ---- */
.plants { display: flex; flex-direction: column; gap: 14px; margin-top: 6px; }
.plant { display: block; color: var(--text); padding: 8px 10px; border-radius: 10px; }
.plant:hover { background: var(--brand-soft); }
.plant-top { display: flex; justify-content: space-between; margin-bottom: 6px; }
.plant-top span { color: var(--muted); font-variant-numeric: tabular-nums; }
.stack { display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: var(--border); }
.stack i { display: block; height: 100%; }
.plant-legend { display: flex; gap: 12px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.plant-legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }

.heat { display: grid; gap: 5px; margin-top: 10px; align-items: center; }
.heat-h, .heat-d { font-size: 11.5px; color: var(--muted); text-align: center; }
.heat-d { text-align: left; }
.cell { height: 30px; border-radius: 6px; display: grid; place-items: center; font-size: 12px; font-weight: 600; color: #fff; background: var(--border); }
.cell.l1 { background: color-mix(in srgb, var(--accent) 30%, var(--border)); color: var(--text); }
.cell.l2 { background: color-mix(in srgb, var(--accent) 55%, var(--surface)); }
.cell.l3 { background: color-mix(in srgb, var(--accent) 80%, var(--surface)); }
.cell.l4 { background: var(--accent); }

.rank { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.rank a { display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; gap: 12px; align-items: center; padding: 7px 8px; border-radius: 10px; color: var(--text); }
.rank a:hover { background: var(--brand-soft); }
.rank .no { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; background: var(--brand-soft); color: var(--accent); }
.rank li:nth-child(1) .no { background: #f2b134; color: #3b2a00; }
.rank-body { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.rank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.rank-bar { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; }
.rank-bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #8e6bbf); border-radius: 3px; }
.rank b { font-variant-numeric: tabular-nums; }

.tiles { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.tile { flex-basis: 150px; min-height: 74px; padding: 10px 12px; border-radius: 12px; display: flex; flex-direction: column; justify-content: space-between; gap: 4px; color: #fff; }
.tile b { font-size: 22px; line-height: 1; }
.tile span { font-size: 12px; line-height: 1.25; opacity: .95; }
.tile.t1 { background: #3d7ea6; } .tile.t2 { background: #2a9d8f; } .tile.t3 { background: #e08a2c; } .tile.t4 { background: #d9482b; }

/* ---- hero + live ---- */
.hero { position: relative; overflow: hidden; display: flex; flex-wrap: wrap; gap: 18px 28px; align-items: center; justify-content: space-between;
  padding: 22px 26px 46px; margin-bottom: 18px; border-radius: 16px; color: #fff;
  background: radial-gradient(900px 260px at 85% -20%, rgba(110,160,245,.55), transparent 60%), linear-gradient(120deg, #14305a, #1f4b8f 55%, #3b6fd0); }
.hero h1 { color: #fff; margin-top: 4px; font-size: 26px; }
.hero .sub { color: rgba(255,255,255,.75); }
.hero-stats { display: flex; gap: 14px; margin-left: auto; }
.hero-stats div { background: rgba(255,255,255,.12); backdrop-filter: blur(6px); border: 1px solid rgba(255,255,255,.18); border-radius: 12px; padding: 8px 18px; text-align: center; min-width: 84px; }
.hero-stats b { display: block; font-size: 26px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.hero-stats span { font-size: 12px; opacity: .8; }
.hero .seg { background: rgba(255,255,255,.14); border-color: rgba(255,255,255,.22); }
.hero .seg a { color: rgba(255,255,255,.8); } .hero .seg a:hover { color: #fff; } .hero .seg .on { background: #fff; color: #1f4b8f; }
.waves { position: absolute; left: 0; bottom: -1px; width: 200%; height: 38px; pointer-events: none; }
.waves path { fill: var(--bg); }
.waves .w1 { opacity: .35; animation: drift 14s linear infinite; }
.waves .w2 { opacity: 1; animation: drift 22s linear infinite reverse; }
@keyframes drift { from { transform: translateX(0); } to { transform: translateX(-50%); } }

.live-badge { display: inline-flex; align-items: center; gap: 8px; padding: 3px 12px 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: rgba(46,204,113,.18); border: 1px solid rgba(46,204,113,.5); color: #b9f5d0; }
.live-badge small { font-weight: 400; opacity: .8; }
.pulse { width: 9px; height: 9px; border-radius: 50%; background: #2ecc71; position: relative; }
.pulse::after { content: ""; position: absolute; inset: -5px; border-radius: 50%; border: 2px solid #2ecc71; animation: ring 1.8s ease-out infinite; }
@keyframes ring { from { transform: scale(.4); opacity: .9; } to { transform: scale(1.7); opacity: 0; } }
.live-badge.offline { background: rgba(231,76,60,.2); border-color: rgba(231,76,60,.6); color: #ffd0cb; }
.live-badge.offline .pulse, .live-badge.offline .pulse::after { background: #e74c3c; border-color: #e74c3c; animation: none; }
.live-badge.offline span::after { content: " — aloqa yo'q"; font-weight: 400; }

.toast { position: fixed; top: 18px; right: 18px; z-index: 20; display: flex; flex-direction: column; gap: 2px; padding: 12px 18px; border-radius: 12px;
  background: var(--surface); border: 1px solid var(--accent); box-shadow: 0 10px 30px rgba(0,0,0,.35); transform: translateY(-20px); opacity: 0; transition: .3s; }
.toast.show { transform: none; opacity: 1; }
.toast span { color: var(--muted); font-size: 13px; }

.spark { position: absolute; right: 14px; top: 16px; width: 120px; height: 34px; opacity: .9; }
.spark polyline { fill: none; stroke: var(--k, var(--accent)); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.card { transition: border-color .2s, box-shadow .2s; }
.board > .card:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); box-shadow: 0 8px 24px rgba(20,35,60,.14); }
@media (prefers-reduced-motion: reduce) { .waves path, .pulse::after { animation: none; } }
