:root {
  --accent: #2a78d6;
  --radius: 10px;
  --font-size: 14px;
  --density: 1;
  --font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --green: #1baf7a;
  --amber: #eda100;
  --red: #e24b4a;
}

html[data-theme='dark'] {
  --bg: #16171a; --surface: #1e1f23; --surface-2: #26282d;
  --border: #33353b; --text: #e8e8ea; --text-muted: #9a9ba3; --chart-grid: #33353b;
}
html[data-theme='light'] {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #eef0f3;
  --border: #d7dae0; --text: #1d2129; --text-muted: #6b7280; --chart-grid: #e2e5ea;
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font-family); font-size: var(--font-size); }

.login-screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); z-index: 100; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 36px; width: 340px; text-align: center; }
.login-logo { width: 84px; height: 84px; border-radius: 50%; }
.login-card h1 { font-size: 18px; margin: 14px 0 2px; }
.login-sub { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }
.login-card input { width: 100%; margin-bottom: 10px; padding: 10px 12px; border-radius: calc(var(--radius) * 0.7); border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: var(--font-size); }
.login-error { color: var(--red); font-size: 13px; min-height: 18px; margin: 10px 0 0; }

header { display: flex; align-items: center; justify-content: space-between; padding: calc(12px * var(--density)) 24px; border-bottom: 1px solid var(--border); background: var(--surface); }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 36px; height: 36px; border-radius: 50%; }
.brand-name { font-weight: 600; font-size: calc(var(--font-size) + 2px); }
.header-actions { display: flex; align-items: center; gap: 10px; }
.user-badge { font-size: 13px; color: var(--text-muted); }
.conn-status { font-size: 13px; color: var(--text-muted); display: flex; align-items: center; gap: 6px; }
.conn-status.connected::before, .conn-status.disconnected::before { content: ''; width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.conn-status.connected::before { background: var(--green); }
.conn-status.disconnected::before { background: var(--red); }

.page-nav { display: flex; gap: 4px; background: var(--surface-2); border-radius: calc(var(--radius) * 0.7); padding: 3px; }
.page-nav-btn { background: none; border: none; color: var(--text-muted); padding: 6px 16px; border-radius: calc(var(--radius) * 0.55); cursor: pointer; font-size: var(--font-size); font-family: var(--font-family); }
.page-nav-btn.active { background: var(--surface); color: var(--text); font-weight: 600; }

.btn { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: calc(var(--radius) * 0.7); padding: calc(8px * var(--density)) 14px; font-size: var(--font-size); cursor: pointer; font-family: var(--font-family); }
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-danger { color: var(--red); border-color: var(--red); background: transparent; }
.btn-small { padding: calc(5px * var(--density)) 10px; font-size: calc(var(--font-size) - 1px); }
.btn-block { width: 100%; }
.icon-btn { background: none; border: 1px solid var(--border); border-radius: calc(var(--radius) * 0.7); color: var(--text); cursor: pointer; padding: 5px 9px; font-size: 15px; }
.icon-btn:hover { border-color: var(--accent); }
.file-btn { display: inline-block; }

.layout { display: flex; min-height: calc(100vh - 61px); }
.device-list { width: 270px; border-right: 1px solid var(--border); padding: 16px; flex-shrink: 0; background: var(--surface); }
.device-list-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.section-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin: 0; }
.device-item { padding: calc(10px * var(--density)) 12px; border-radius: calc(var(--radius) * 0.8); cursor: pointer; margin-bottom: 6px; border: 1px solid transparent; }
.device-item:hover { background: var(--surface-2); }
.device-item.active { background: var(--surface-2); border-color: var(--accent); }
.device-item-name { font-weight: 500; }
.device-item-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.device-item-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; margin-right: 6px; }
.dot-live { background: var(--green); }
.dot-mock { background: var(--amber); }
.dot-stale { background: var(--red); }

main { flex: 1; padding: 24px; overflow-x: hidden; }
.empty-state { color: var(--text-muted); padding: 60px 0; text-align: center; }
.device-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; flex-wrap: wrap; gap: 10px; }
.device-header h1 { margin: 0; font-size: calc(var(--font-size) + 6px); font-weight: 600; }
.device-meta { margin: 4px 0 0; color: var(--text-muted); font-size: 13px; }
.device-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.badge { font-size: 12px; padding: 4px 10px; border-radius: calc(var(--radius) * 0.6); background: var(--surface-2); border: 1px solid var(--border); }
.badge.mock { color: var(--amber); border-color: var(--amber); }
.badge.live { color: var(--green); border-color: var(--green); }

.cards-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: calc(12px * var(--density)); margin-bottom: 28px; }
.reg-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: calc(12px * var(--density)) 14px; cursor: pointer; }
.reg-card:hover { border-color: var(--accent); }
.reg-card.selected { border-color: var(--accent); background: var(--surface-2); }
.reg-card-label { font-size: 12px; color: var(--text-muted); margin: 0 0 6px; }
.reg-card-value { font-size: calc(var(--font-size) + 6px); font-weight: 500; margin: 0; }
.reg-card-unit { font-size: 13px; color: var(--text-muted); font-weight: 400; }
.reg-card-error { font-size: 11px; color: var(--red); margin-top: 4px; }

.chart-section { margin-bottom: 28px; }
.chart-header { display: flex; align-items: center; justify-content: space-between; }
select, input { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: calc(var(--radius) * 0.6); padding: 6px 10px; font-size: calc(var(--font-size) - 1px); font-family: var(--font-family); }
.chart-wrap { position: relative; height: 240px; margin-top: 10px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.logs-header { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.btn-log-on { color: var(--green); border-color: var(--green); }
.btn-log-off { color: var(--red); border-color: var(--red); }
.logs-list { display: flex; flex-direction: column; gap: 6px; }
.log-link { color: var(--accent); font-size: 13px; text-decoration: none; }
.log-link:hover { text-decoration: underline; }
.logs-off-note { color: var(--text-muted); font-size: 13px; }

/* ---------- harmonic spectrum ---------- */
.harmonic-charts { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 10px; }
.harmonic-charts .chart-wrap { height: 220px; margin-top: 0; }
.harmonic-title { font-size: 12px; color: var(--text-muted); margin: 0 0 8px; }
@media (max-width: 800px) { .harmonic-charts { grid-template-columns: 1fr; } }

/* ---------- events & alarms ---------- */
.events-section { margin-bottom: 28px; }
.events-list { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.event-row { display: flex; align-items: center; gap: 10px; padding: 8px 12px; border: 1px solid var(--border); border-radius: calc(var(--radius) * 0.6); background: var(--surface); font-size: 13px; flex-wrap: wrap; }
.event-badge { font-size: 11px; padding: 2px 8px; border-radius: 5px; text-transform: uppercase; font-weight: 600; }
.event-sag { color: var(--red); border: 1px solid var(--red); }
.event-swell { color: var(--amber); border: 1px solid var(--amber); }
.event-transient { color: var(--accent); border: 1px solid var(--accent); }

.modal-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; }
.modal-wide { max-width: 1080px; }
.modal h2 { margin: 0 0 16px; font-size: calc(var(--font-size) + 3px); }
.modal h3 { font-size: var(--font-size); margin: 18px 0 10px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); }
.form-grid input, .form-grid select { width: 100%; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.modal-error { color: var(--red); font-size: 13px; min-height: 16px; margin: 8px 0 0; }

.reg-toolbar { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.reg-hint { font-size: 12px; color: var(--text-muted); }
.reg-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: calc(var(--radius) * 0.6); }
.reg-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.reg-table th { text-align: left; padding: 8px; background: var(--surface-2); color: var(--text-muted); font-weight: 500; white-space: nowrap; }
.reg-table td { padding: 4px; border-top: 1px solid var(--border); }
.reg-table input, .reg-table select { width: 100%; min-width: 60px; padding: 4px 6px; font-size: 12px; }
.reg-table .col-key input { min-width: 90px; }
.reg-table .col-param input { min-width: 130px; }
.reg-del { color: var(--red); cursor: pointer; background: none; border: none; font-size: 14px; }

.users-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-bottom: 6px; }
.users-table th { text-align: left; color: var(--text-muted); font-weight: 500; padding: 6px 8px; }
.users-table td { padding: 6px 8px; border-top: 1px solid var(--border); }

.an-export-btn { margin-left: auto; color: var(--accent); border-color: var(--accent); }
.an-flow-header { display: flex; align-items: center; justify-content: space-between; margin-top: 22px; }

.analytics-page { padding: 20px 24px 40px; max-width: 1100px; margin: 0 auto; }
.an-tabbar { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.an-tab { background: var(--surface); border: 1px solid var(--border); color: var(--text); border-radius: calc(var(--radius) * 0.7); padding: calc(6px * var(--density)) 14px; cursor: pointer; font-size: var(--font-size); font-family: var(--font-family); }
.an-tab.active { border-color: var(--accent); background: var(--surface-2); font-weight: 600; }
.an-demo-note { font-size: 12px; color: var(--text-muted); margin: 4px 0 18px; }
.an-title { font-weight: 600; margin: 22px 0 8px; }
.an-sample { font-size: 10px; font-weight: 500; color: var(--amber); border: 1px solid var(--amber); border-radius: 4px; padding: 1px 5px; vertical-align: middle; margin-left: 6px; text-transform: uppercase; letter-spacing: .03em; }
.an-chart { position: relative; height: 240px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.an-chart-sm { height: 190px; }
.an-two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 800px) { .an-two-col { grid-template-columns: 1fr; } }
.an-site-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.an-site-card { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.an-site-card-top { height: 64px; display: flex; align-items: center; justify-content: center; background: color-mix(in srgb, var(--accent) 18%, var(--surface)); font-size: 26px; }
.an-site-card-body { padding: 8px 12px; }
.an-site-card-body p { margin: 0; font-size: 12px; color: var(--text-muted); }
.an-site-card-body h3 { margin: 2px 0 0; font-size: calc(var(--font-size) + 3px); }
.an-live-tag { font-size: 10px; color: var(--green); border: 1px solid var(--green); border-radius: 4px; padding: 1px 5px; margin-left: 6px; vertical-align: middle; }
.an-kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.an-kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.an-kpi p { margin: 0; font-size: 12px; color: var(--text-muted); }
.an-kpi h3 { margin: 4px 0 0; font-size: calc(var(--font-size) + 5px); font-weight: 600; }
.an-red { color: var(--red); }
.an-heatmap { display: grid; grid-template-columns: repeat(24, 1fr); gap: 2px; }
.an-heatmap div { aspect-ratio: 1; border-radius: 2px; }
.an-heatmap-legend { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 12px; color: var(--text-muted); }
.an-heatmap-scale { flex: 1; height: 8px; border-radius: 4px; background: linear-gradient(to right, #e24b4a, #eda100, #1baf7a); }
.an-table { width: 100%; border-collapse: collapse; font-size: 13px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.an-table th { text-align: left; padding: 8px 12px; color: var(--text-muted); font-weight: 500; background: var(--surface-2); }
.an-table td { padding: 8px 12px; border-top: 1px solid var(--border); }
.ta-r { text-align: right; }
.an-status { font-size: 11px; padding: 2px 8px; border-radius: 5px; }
.an-status-ok { color: var(--green); border: 1px solid var(--green); }
.an-status-warn { color: var(--amber); border: 1px solid var(--amber); }
.an-status-bad { color: var(--red); border: 1px solid var(--red); }
.an-kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.an-kpi-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.an-kpi-card p { margin: 0 0 4px; font-size: 12px; color: var(--text-muted); }
.an-kpi-card h3 { margin: 0 0 4px; font-size: calc(var(--font-size) + 4px); }
.an-delta { font-size: 12px; }
.an-delta.up { color: var(--green); }
.an-delta.down { color: var(--red); }
.an-source-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.an-source { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.an-source h4 { margin: 0 0 6px; }
.an-source p { margin: 0 0 10px; font-size: 13px; color: var(--text-muted); }
.an-chip { font-size: 11px; padding: 2px 8px; border-radius: 5px; border: 1px solid var(--border); color: var(--text-muted); }
.an-chip-green { color: var(--green); border-color: var(--green); }
.an-chip-amber { color: var(--amber); border-color: var(--amber); }
.an-sankey { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.an-node { fill: var(--surface-2); stroke: var(--border); }
.an-node-h { fill: var(--text); font-size: 14px; font-weight: 600; }
.an-node-s { fill: var(--text-muted); font-size: 12px; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .layout { flex-direction: column; min-height: auto; }
  .device-list { width: 100%; border-right: none; border-bottom: 1px solid var(--border); padding: 12px; }
  .device-list-header { margin-bottom: 8px; }
  #deviceItems { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; -webkit-overflow-scrolling: touch; }
  .device-item { flex: 0 0 auto; min-width: 170px; margin-bottom: 0; white-space: nowrap; }
  main { padding: 16px; }
  .analytics-page { padding: 16px; }
  .an-two-col { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  header { flex-wrap: wrap; gap: 8px; padding: 10px 14px; }
  .brand-name { display: none; }
  .page-nav { order: 3; width: 100%; justify-content: center; }
  .header-actions { gap: 6px; }
  .user-badge { display: none; }
  main { padding: 12px; }
  .device-header { flex-direction: column; align-items: flex-start; gap: 8px; }
  .device-actions { width: 100%; }
  .cards-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
  .reg-card { padding: 10px 12px; }
  .reg-card-value { font-size: calc(var(--font-size) + 4px); }
  .chart-wrap { height: 200px; padding: 8px; }
  .harmonic-charts .chart-wrap { height: 180px; }
  .form-grid { grid-template-columns: 1fr; }
  .modal { padding: 16px; max-height: 85vh; }
  .an-tabbar { gap: 4px; }
  .an-tab { padding: 6px 10px; font-size: calc(var(--font-size) - 1px); }
  .an-kpis, .an-kpi-grid, .an-site-cards, .an-source-grid { grid-template-columns: 1fr 1fr; }
  .an-heatmap { grid-template-columns: repeat(12, 1fr); }
}

@media (max-width: 420px) {
  .an-kpis, .an-kpi-grid, .an-site-cards, .an-source-grid { grid-template-columns: 1fr; }
  .cards-grid { grid-template-columns: repeat(auto-fill, minmax(115px, 1fr)); }
}
