/* ==========================================================================
   PLC Alarm Analytics - Prasad Group POC
   Design tokens follow a validated categorical + status palette.
   Light and dark are each selected (not an automatic flip).
   ========================================================================== */

:root {
  color-scheme: light;

  --page:            #f9f9f7;
  --surface-1:       #fcfcfb;
  --surface-2:       #f3f2ee;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --grid:            #e1e0d9;
  --axis:            #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);
  --shadow:          0 1px 2px rgba(11, 11, 11, .04), 0 4px 16px rgba(11, 11, 11, .04);
  --ghost:           rgba(11, 11, 11, 0.045);

  /* Categorical - fixed slot order, never cycled, never rank-assigned. */
  --series-1: #2a78d6;  /* blue    */
  --series-2: #eb6834;  /* orange  */
  --series-3: #1baf7a;  /* aqua    */
  --series-4: #eda100;  /* yellow  */
  --series-5: #e87ba4;  /* magenta */
  --series-6: #008300;  /* green   */
  --series-7: #4a3aa7;  /* violet  */
  --series-8: #e34948;  /* red     */

  /* Status - reserved for severity only, never reused as a series colour. */
  --sev-major:   #d03b3b;  /* critical */
  --sev-minor:   #fab219;  /* warning  */
  --sev-uncat:   #898781;  /* no severity recorded by the PLC */
  --status-good: #0ca30c;

  --radius:   10px;
  --radius-s: 6px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page:           #0d0d0d;
    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --axis:           #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --shadow:         0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    --ghost:          rgba(255, 255, 255, 0.06);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page:           #0d0d0d;
  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --axis:           #383835;
  --border:         rgba(255, 255, 255, 0.10);
  --shadow:         0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  --ghost:          rgba(255, 255, 255, 0.06);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------------- header */

.masthead {
  border-bottom: 1px solid var(--border);
  background: var(--surface-1);
  padding: 18px 24px 16px;
  display: flex;
  align-items: flex-start;
  gap: 24px;
  flex-wrap: wrap;
}
.brand {
  display: flex;
  align-items: center;
  flex: none;
  padding-right: 20px;
  margin-right: 2px;
  border-right: 1px solid var(--border);
  align-self: stretch;
}
.brand img { height: 30px; width: auto; display: block; }
.brand .brand-dark { display: none; }

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .brand .brand-light { display: none; }
  :root:where(:not([data-theme="light"])) .brand .brand-dark { display: block; }
}
:root[data-theme="dark"] .brand .brand-light { display: none; }
:root[data-theme="dark"] .brand .brand-dark { display: block; }

.masthead h1 {
  margin: 0 0 3px;
  font-size: 19px;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.masthead p {
  margin: 0;
  color: var(--text-secondary);
  font-size: 13px;
  max-width: 74ch;
}
.masthead .spacer { flex: 1 1 auto; }
.provenance {
  color: var(--text-muted);
  font-size: 11.5px;
  text-align: right;
  line-height: 1.6;
  white-space: nowrap;
}

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  border-radius: var(--radius-s);
  padding: 6px 12px;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  min-height: 32px;
}
.theme-toggle:hover { background: var(--ghost); }

/* ---------------------------------------------------------- data sources */
/* Sits above the filter row: what is loaded, and how to load more. */

.datasrc {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 18px;
  padding: 12px 24px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.datasrc-main { flex: 1 1 420px; min-width: 0; }
.datasrc-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 12.5px;
}
.datasrc-title strong { color: var(--text-primary); }
.datasrc-title span { color: var(--text-secondary); }

.srcfiles {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}
.srcfiles li {
  font-size: 11px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 9px;
  white-space: nowrap;
}
.srcfiles li b { color: var(--text-primary); font-weight: 600; }

.dropzone {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 9px 16px;
  background: var(--surface-1);
  border: 1px dashed var(--border);
  border-radius: var(--radius-s);
  cursor: pointer;
  font-size: 12px;
  color: var(--text-secondary);
  transition: border-color .12s, background .12s;
}
.dropzone strong { font-size: 12.5px; color: var(--text-primary); }
.dropzone code {
  font-size: 11px;
  background: var(--ghost);
  border-radius: 3px;
  padding: 0 3px;
}
.dropzone:hover:not([aria-disabled="true"]),
.dropzone.is-over {
  border-color: var(--series-1);
  background: var(--ghost);
}
.dropzone.is-over { border-style: solid; }
.dropzone[aria-disabled="true"] { cursor: default; opacity: .65; }
.dropzone.is-busy { cursor: progress; opacity: .7; }

.upload-status {
  flex: 1 1 100%;
  font-size: 12px;
  line-height: 1.55;
  border-radius: var(--radius-s);
  border: 1px solid var(--border);
  background: var(--surface-1);
  padding: 8px 11px;
  color: var(--text-secondary);
}
.upload-status.is-ok    { border-color: var(--sev-minor); }
.upload-status.is-error { border-color: var(--sev-major); }
.upload-status b { color: var(--text-primary); }
.upload-status ul { margin: 4px 0 0; padding-left: 18px; }

@media (max-width: 720px) {
  .datasrc { padding: 12px 16px; }
  .dropzone { flex: 1 1 100%; }
}

/* --------------------------------------------------------------- filters */
/* One row, above everything it scopes. Never per-chart. */

.filters {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 14px;
}
.filters.is-stuck { box-shadow: var(--shadow); }

.field { display: flex; flex-direction: column; gap: 4px; }
.field > label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  font-weight: 600;
}
.field select,
.field input {
  font: inherit;
  font-size: 13px;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  padding: 6px 8px;
  min-height: 32px;
  min-width: 132px;
}
.field input[type="date"] { min-width: 138px; }
.field input[type="search"] { min-width: 210px; }
.field select:focus-visible,
.field input:focus-visible,
button:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--series-1);
  outline-offset: 1px;
}

.presets { display: flex; gap: 6px; }
.preset-btn, .btn {
  font: inherit;
  font-size: 12.5px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  border-radius: var(--radius-s);
  padding: 6px 11px;
  min-height: 32px;
  cursor: pointer;
  white-space: nowrap;
}
.preset-btn:hover, .btn:hover { background: var(--ghost); }
.preset-btn[aria-pressed="true"] {
  border-color: var(--series-1);
  color: var(--series-1);
  font-weight: 600;
}
.btn-primary {
  border-color: var(--series-1);
  color: var(--series-1);
  font-weight: 600;
}

.filter-summary {
  width: 100%;
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 6px 2px 10px;
  font-size: 12px;
}
.chip button {
  border: 0;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font: inherit;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 999px;
}
.chip button:hover { color: var(--text-primary); background: var(--ghost); }

/* ----------------------------------------------------------------- layout */

main { padding: 20px 24px 64px; max-width: 1680px; margin: 0 auto; }

.section-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin: 30px 0 12px;
}
.section-head:first-child { margin-top: 4px; }
.section-head h2 {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--text-muted);
}
.section-head .rule { flex: 1; height: 1px; background: var(--border); }

/* align-items:start lets each card size to its own content - stretching them
   to a shared row height leaves a large dead band under the shorter chart. */
.grid { display: grid; gap: 14px; align-items: start; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1100px) {
  .grid.cols-2, .grid.cols-3 { grid-template-columns: minmax(0, 1fr); }
}

/* ------------------------------------------------------------- stat tiles */

.kpi-row {
  display: grid;
  grid-template-columns: minmax(230px, 1.25fr) repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
}
@media (max-width: 900px) { .kpi-row { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.tile .label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-muted);
  font-weight: 600;
}
.tile .value {
  font-size: 26px;
  font-weight: 650;
  letter-spacing: -0.02em;
  line-height: 1.15;
  /* Proportional figures - tabular-nums only in aligned columns. */
}
.tile .value .unit {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0;
  margin-left: 2px;
}
.tile .sub {
  font-size: 12px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tile.hero .value { font-size: 52px; font-weight: 600; }
.tile.hero { justify-content: space-between; }

.sev-dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 3px;
  vertical-align: baseline;
  margin-right: 5px;
  flex: none;
}

/* ------------------------------------------------------------------ cards */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px 16px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.card-head {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 2px;
}
.card-head h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 620;
  letter-spacing: -0.005em;
}
.card-head .why {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-secondary);
}
.card-head .grow { flex: 1; }

.view-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  overflow: hidden;
  flex: none;
}
.view-toggle button {
  font: inherit;
  font-size: 11.5px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 4px 9px;
  min-height: 26px;
  cursor: pointer;
}
.view-toggle button[aria-pressed="true"] {
  background: var(--surface-2);
  color: var(--text-primary);
  font-weight: 600;
}
.view-toggle button:hover { background: var(--ghost); }

.card-body { margin-top: 10px; flex: 1; min-width: 0; }
.card-body[hidden] { display: none; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  margin: 0 0 8px;
  font-size: 12px;
  color: var(--text-secondary);
}
.legend .item { display: inline-flex; align-items: center; gap: 6px; }
.legend .key { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend .key.line { height: 2px; width: 14px; border-radius: 2px; }

.chart-scroll { overflow-x: auto; overflow-y: hidden; }
svg.chart { display: block; max-width: 100%; }
svg.chart text { font-family: var(--font); }

.empty {
  padding: 34px 12px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* ------------------------------------------------------------------ table */

.table-wrap { overflow-x: auto; max-height: 420px; overflow-y: auto; }
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;  /* aligned columns - correct here */
}
table.data th, table.data td {
  text-align: left;
  padding: 6px 10px;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}
table.data th {
  position: sticky;
  top: 0;
  background: var(--surface-1);
  color: var(--text-muted);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .05em;
  font-weight: 700;
  z-index: 2;
  border-bottom: 1px solid var(--axis);
}
table.data th.sortable { cursor: pointer; user-select: none; }
table.data th.sortable:hover { color: var(--text-primary); }
table.data th .arrow { color: var(--series-1); margin-left: 3px; }
table.data td.num { text-align: right; }
table.data td.wide { white-space: normal; min-width: 260px; max-width: 460px; }
table.data tbody tr:hover { background: var(--ghost); }
table.data tbody tr.clickable { cursor: pointer; }
table.data .muted { color: var(--text-muted); }

.pill {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  background: var(--surface-2);
}
.pill .sev-dot { margin-right: 4px; }

.table-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 10px;
  font-size: 12px;
  color: var(--text-secondary);
  flex-wrap: wrap;
}
.table-foot .grow { flex: 1; }

/* --------------------------------------------------------------- tooltip */

#tooltip {
  position: fixed;
  z-index: 100;
  pointer-events: none;
  opacity: 0;
  transition: opacity .1s ease;
  background: var(--surface-1);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-s);
  box-shadow: 0 4px 20px rgba(0, 0, 0, .16);
  padding: 8px 10px;
  font-size: 12.5px;
  max-width: 320px;
  line-height: 1.45;
}
#tooltip[data-show="1"] { opacity: 1; }
#tooltip .tt-title {
  font-weight: 650;
  margin-bottom: 4px;
  font-size: 12.5px;
  white-space: normal;
}
#tooltip .tt-row {
  display: flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
}
/* Values lead, labels follow. */
#tooltip .tt-val { font-weight: 650; font-variant-numeric: tabular-nums; }
#tooltip .tt-name { color: var(--text-secondary); }
#tooltip .tt-key { width: 12px; height: 2px; border-radius: 2px; flex: none; }
#tooltip .tt-note {
  margin-top: 5px;
  color: var(--text-muted);
  font-size: 11.5px;
  white-space: normal;
}

/* --------------------------------------------------------------- sundries */

.callout {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--series-1);
  border-radius: var(--radius-s);
  padding: 10px 14px;
  font-size: 12.5px;
  color: var(--text-secondary);
  margin-bottom: 14px;
}
.callout strong { color: var(--text-primary); }

.seq-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.seq-list li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 7px 8px;
  border-radius: var(--radius-s);
  border-bottom: 1px solid var(--grid);
}
.seq-list li:hover { background: var(--ghost); }
.seq-chain { font-size: 12.5px; min-width: 0; }
.seq-chain .arrow { color: var(--text-muted); margin: 0 6px; }
.seq-chain .code { font-weight: 650; }
.seq-chain .desc { color: var(--text-secondary); display: block; font-size: 11.5px; }
.seq-count { font-variant-numeric: tabular-nums; font-weight: 650; white-space: nowrap; }

footer.page-foot {
  border-top: 1px solid var(--border);
  margin-top: 40px;
  padding-top: 16px;
  color: var(--text-muted);
  font-size: 11.5px;
  line-height: 1.7;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

@media print {
  .filters, .theme-toggle, .view-toggle { display: none !important; }
  .card, .tile { box-shadow: none; break-inside: avoid; }
}
