/* =========================================================================
   Sungrow Energy Monitoring — dashboard styles
   Dark-first energy-monitoring theme with a light mode; tokens follow the
   validated reference palette (chrome/ink/status roles).
   ========================================================================= */

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --accent: #3987e5;
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;
  --status-neutral: #898781;
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --accent: #2a78d6;
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;
  --status-neutral: #898781;
}

* { box-sizing: border-box; }

/* The hidden attribute must always win over component display rules */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.9rem; color: var(--ink-2); font-weight: 600; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  clip: rect(0 0 0 0); overflow: hidden; white-space: nowrap;
}

/* ------------------------------- Brand -------------------------------- */
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 14px; height: 14px; border-radius: 4px; flex: none;
  background: linear-gradient(135deg, var(--series-2), var(--series-1));
}
.brand-title { font-weight: 650; white-space: nowrap; }
.brand-meta { font-size: 0.75rem; color: var(--muted); }

/* ---------------------------- Login view ------------------------------- */
.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
}
.login-card h1 { font-size: 1.15rem; }
.login-sub { color: var(--ink-2); font-size: 0.85rem; margin: 12px 0 20px; }
.login-card label {
  display: block; font-size: 0.78rem; color: var(--ink-2);
  margin: 14px 0 4px; font-weight: 600;
}
.login-card input {
  width: 100%; padding: 10px 12px;
  background: var(--surface-2); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 0.95rem;
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: 0; }

.form-error {
  margin-top: 14px; padding: 10px 12px;
  border-radius: 8px; font-size: 0.85rem;
  background: color-mix(in srgb, var(--status-critical) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--status-critical) 45%, transparent);
  color: var(--ink);
}

/* ------------------------------ Buttons -------------------------------- */
.btn {
  font: inherit; cursor: pointer;
  padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2); color: var(--ink);
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: 0.55; cursor: wait; }
.btn-primary { background: var(--accent); border-color: transparent; color: #fff; font-weight: 600; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-block { width: 100%; margin-top: 18px; padding: 11px; }
.btn-ghost { background: transparent; }
.btn-danger-text { color: var(--status-critical); }

/* ------------------------------- Topbar -------------------------------- */
.topbar {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.topbar-status { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.topbar-stat { font-size: 0.78rem; color: var(--muted); }
.topbar-stat strong { color: var(--ink-2); font-weight: 600; }
.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }

.pill {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 0.78rem; font-weight: 600;
  padding: 4px 11px; border-radius: 999px;
  border: 1px solid var(--border);
}
.pill-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.pill-on { color: var(--status-good); }
.pill-off { color: var(--status-critical); }

select, input[type="search"] {
  font: inherit;
  padding: 8px 10px;
  background: var(--surface-2); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
}
select:focus, input[type="search"]:focus { outline: 2px solid var(--accent); }
.plant-select-wrap select { max-width: 300px; }

/* ------------------------------ Layout --------------------------------- */
.container { max-width: 1400px; margin: 0 auto; padding: 20px; }
.section { margin-top: 28px; }
.section-head {
  display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap;
  margin-bottom: 12px;
}
.tag { font-size: 0.72rem; color: var(--muted); }

.global-alert {
  margin-bottom: 16px; padding: 12px 14px;
  border-radius: 10px; font-size: 0.88rem;
  background: color-mix(in srgb, var(--status-warning) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--status-warning) 40%, transparent);
}
.global-alert.error {
  background: color-mix(in srgb, var(--status-critical) 12%, transparent);
  border-color: color-mix(in srgb, var(--status-critical) 40%, transparent);
}

.plant-info { font-size: 0.82rem; color: var(--ink-2); margin-bottom: 14px; }

/* --------------------------- Clients overview -------------------------- */
.clients-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}
.client-card {
  font: inherit;
  text-align: left;
  cursor: pointer;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  color: var(--ink);
  transition: border-color 0.15s, transform 0.1s;
}
.client-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.client-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 4px;
}
.client-name { font-size: 1.05rem; font-weight: 650; }
.client-loc { font-size: 0.78rem; color: var(--muted); margin-bottom: 12px; }
.client-stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px;
  margin-bottom: 12px;
}
.client-stats .k {
  display: block;
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted);
}
.client-stats .v { font-weight: 650; font-variant-numeric: tabular-nums; }
.client-open { font-size: 0.8rem; font-weight: 600; color: var(--accent); }

.plant-bar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  margin-bottom: 16px;
}
.plant-bar .plant-info { margin-bottom: 0; }

/* --------------------------- Summary cards ----------------------------- */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 12px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  min-height: 84px;
}
.card-label {
  font-size: 0.72rem; font-weight: 600; letter-spacing: 0.03em;
  text-transform: uppercase; color: var(--muted);
}
.card-value { font-size: 1.5rem; font-weight: 650; margin-top: 6px; }
.card-value .unit { font-size: 0.85rem; color: var(--ink-2); font-weight: 500; margin-left: 4px; }
.card-sub { font-size: 0.72rem; color: var(--muted); margin-top: 2px; }
.card-accent { border-left: 3px solid var(--series-1); }
.card-accent-warn { border-left: 3px solid var(--status-warning); }
.card-accent-crit { border-left: 3px solid var(--status-critical); }
.card-accent-good { border-left: 3px solid var(--status-good); }

/* ------------------------------- Charts -------------------------------- */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 12px;
}
.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px 10px;
}
.chart-box { position: relative; height: 210px; margin-top: 8px; }

/* ------------------------------- Table --------------------------------- */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1 1 260px; }

.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.device-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; min-width: 980px; }
.device-table th, .device-table td {
  padding: 10px 12px; text-align: left;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}
.device-table th {
  font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); font-weight: 600;
  position: sticky; top: 0; background: var(--surface);
}
.device-table tbody tr:hover { background: var(--surface-2); }
.device-table tbody tr:last-child td { border-bottom: none; }
.device-table .num { text-align: right; font-variant-numeric: tabular-nums; }
/* Hide per-device measurement columns when the app has no device-level data */
.device-table.no-devrt th:nth-child(n+5):nth-child(-n+8),
.device-table.no-devrt td:nth-child(n+5):nth-child(-n+8) { display: none; }
.device-table.no-devrt { min-width: 640px; }

.client-stale { font-size: 0.72rem; color: var(--status-warning); margin-bottom: 10px; }
.data-note {
  margin-bottom: 14px; padding: 10px 14px;
  border-radius: 10px; font-size: 0.85rem;
  background: color-mix(in srgb, var(--status-warning) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--status-warning) 35%, transparent);
  color: var(--ink-2);
}
.device-name { font-weight: 600; }
.device-sub { font-size: 0.72rem; color: var(--muted); }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.74rem; font-weight: 600;
  padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--ink-2);
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--status-neutral); }
.badge-online::before, .badge-normal::before { background: var(--status-good); }
.badge-warning::before { background: var(--status-warning); }
.badge-fault::before { background: var(--status-critical); }
.badge-offline::before { background: var(--status-neutral); }

.empty-note { padding: 24px; text-align: center; color: var(--muted); font-size: 0.85rem; }

.link-btn {
  background: none; border: none; padding: 0; font: inherit;
  color: var(--accent); cursor: pointer; font-weight: 600;
}
.link-btn:hover { text-decoration: underline; }

/* ------------------------------- Modal --------------------------------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, 0.55);
  display: grid; place-items: center;
  padding: 20px;
}
.modal {
  width: 100%; max-width: 760px; max-height: 88vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--grid);
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.modal-body { padding: 18px 20px 24px; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 10px 18px;
  margin-bottom: 18px;
}
.detail-item { font-size: 0.85rem; }
.detail-item .k { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.detail-item .v { margin-top: 2px; color: var(--ink); word-break: break-all; }

.points-group { margin-top: 16px; }
.points-group h4 {
  margin: 0 0 8px; font-size: 0.78rem; font-weight: 650;
  text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-2);
}
.points-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 8px;
}
.point-tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px;
}
.point-tile .pt-name { font-size: 0.7rem; color: var(--muted); }
.point-tile .pt-value { font-size: 0.95rem; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.point-tile .pt-value .unit { font-size: 0.72rem; color: var(--ink-2); font-weight: 500; margin-left: 3px; }

.modal-note { font-size: 0.75rem; color: var(--muted); margin: 10px 0 0; }
.modal-actions { margin-top: 18px; }

/* ----------------------------- Responsive ------------------------------ */
@media (max-width: 900px) {
  .topbar { gap: 10px; }
  .topbar-actions { margin-left: 0; width: 100%; }
  .container { padding: 14px; }
  .cards-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .charts-grid { grid-template-columns: 1fr; }
}
