:root {
  color-scheme: dark;
  --bg: #111418;
  --surface: #1b1f26;
  --surface-2: #232833;
  --border: #2d333d;
  --text: #e8eaed;
  --text-2: #b4bac4;
  --muted: #8a919c;
  --accent: #3987e5;
  --accent-ink: #ffffff;
  --danger: #e66767;
  --warn: #d9a400;
  --good: #3fb950;
  --rate-a: #3987e5;
  --rate-b: #d95926;
  --rate-c: #199e70;
  --radius: 12px;
  font-family: "Segoe UI", "Noto Sans Thai", "Sarabun", system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 14px; line-height: 1.45; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; font-size: 12px; word-break: break-all; }
a { color: var(--accent); }

/* buttons & inputs */
.btn { border: 1px solid var(--border); background: var(--surface-2); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: 12px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type=checkbox] { width: auto; }
label { display: flex; flex-direction: column; gap: 4px; color: var(--text-2); font-size: 13px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.error { color: var(--danger); min-height: 18px; font-size: 13px; margin-top: 8px; }

/* auth */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px; }
.auth-card form { margin-top: 20px; }
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 6px; }
.brand.big { font-size: 22px; }
.bolt { color: var(--accent); }
.step-title { font-weight: 600; font-size: 15px; }
.steps { margin: 0; padding-left: 18px; }
.qr { width: 220px; height: 220px; align-self: center; background: #fff; border-radius: 8px; padding: 6px; }

/* app layout */
.topbar { display: flex; align-items: center; gap: 24px; padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface); flex-wrap: wrap; }
.topbar nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: var(--text-2); text-decoration: none; padding: 6px 12px; border-radius: 8px; }
.topbar nav a.active, .topbar nav a:hover { background: var(--surface-2); color: var(--text); }
.user-menu { display: flex; align-items: center; gap: 8px; }
main { padding: 20px; max-width: 1280px; margin: 0 auto; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.page-head h2 { margin: 0; font-size: 20px; }

.selectors { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 16px; }
.selectors label { min-width: 200px; }
.status { margin-left: auto; display: flex; align-items: center; gap: 8px; color: var(--muted); flex-wrap: wrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); }
.dot.on { background: var(--good); }
.dot.stale { background: var(--warn); }
.dot.off { background: var(--danger); }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--border); font-size: 12px; color: var(--text-2); }
.badge.warn { border-color: var(--warn); }
.badge.bad { border-color: var(--danger); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tab { background: none; border: none; color: var(--muted); padding: 10px 14px; cursor: pointer; font: inherit; border-bottom: 2px solid transparent; white-space: nowrap; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-height: 104px; }
.card .lbl { color: var(--text-2); font-size: 13px; display: flex; align-items: center; gap: 6px; }
.card .val { font-size: 26px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.card .unit { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 2px; }
.card .sub { font-size: 12px; color: var(--muted); margin-top: 4px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

h3.section { text-transform: uppercase; letter-spacing: .06em; font-size: 13px; color: var(--text-2); margin: 8px 0 10px; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 20px; overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.tbl th { color: var(--text-2); font-weight: 600; font-size: 13px; text-align: left; padding: 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.tbl td { padding: 10px; border-bottom: 1px solid var(--border); }
.tbl tr:last-child td { border-bottom: none; }
.tbl .r { text-align: right; }
.tbl .c { text-align: center; }
.tbl .code { color: var(--muted); margin-right: 8px; }
.tbl .unit { color: var(--muted); }
.tbl .actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.stale-row td { opacity: .55; }

.energy-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.date-nav { display: flex; gap: 6px; align-items: center; }
.date-nav input { width: auto; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg-btn { background: none; border: none; color: var(--muted); padding: 6px 14px; cursor: pointer; font: inherit; }
.seg-btn.active { background: var(--surface-2); color: var(--text); }
.chart-title { font-weight: 600; margin-bottom: 6px; }
.legend { display: flex; gap: 16px; color: var(--text-2); font-size: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-box { position: relative; height: 320px; }
.table-scroll { max-height: 480px; overflow: auto; }
.empty { color: var(--muted); padding: 28px; text-align: center; }

/* structure tree */
.org { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 16px; }
.org-head, .site-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; flex-wrap: wrap; }
.org-head { border-bottom: 1px solid var(--border); font-weight: 700; font-size: 15px; }
.site { margin: 12px 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); }
.site-head { font-weight: 600; }
.site .tbl { margin: 0 0 4px; }
.kv { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 8px; }

/* access editor */
.access-org { font-weight: 700; margin-top: 6px; }
.access-site { padding: 6px 0 6px 6px; }
.access-site > label, .access-meter { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.access-meters { padding-left: 26px; display: flex; flex-direction: column; gap: 4px; }

/* modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 20px; width: 100%; max-width: 480px; max-height: 90vh; overflow: auto; }
.modal-title { font-weight: 700; font-size: 16px; margin-bottom: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.check-row { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }

@media (max-width: 640px) {
  main { padding: 16px; }
  .topbar { gap: 10px; padding: 10px 16px; }
  .user-menu { width: 100%; justify-content: flex-end; }
  .selectors label { min-width: 0; flex: 1 1 140px; }
  .status { margin-left: 0; }
  .card .val { font-size: 22px; }
  .chart-box { height: 260px; }
}
