/* AST dashboard styles. Phone-first; works from 320 px wide and at 200 % zoom.
   Colours: Okabe-Ito palette for data series; every text/background pair
   below meets WCAG 1.4.3 (>= 4.5:1), checked in tests/check_contrast.py. */

:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --text: #15191e;
  --muted: #48515c;
  --border: #c9d0d8;
  --control-border: #6b7480;
  --grid: #e1e5ea;
  --accent: #0b4f79;
  --on-accent: #ffffff;
  --link: #005a8e;
  --focus: #0b4f79;
  --warn-bg: #fff3e6;
  --warn-edge: #d55e00;
  --ok-edge: #009e73;
  /* Okabe-Ito series colours (>= 3:1 against --surface) */
  --s-trk: #0072b2;   /* blue: tracked panel, solid line, circle */
  --s-fix: #d55e00;   /* vermillion: fixed panel, dashed, triangle */
  --s-use: #000000;   /* black: own use, dotted, square */
  --s-net: #009e73;   /* bluish green: tracked minus own use, dash-dot, diamond */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #1a2027;
    --text: #e9edf1;
    --muted: #b7c0ca;
    --border: #3a444f;
    --control-border: #8a95a1;
    --grid: #2c343d;
    --accent: #0b3a59;
    --on-accent: #ffffff;
    --link: #7cc4ee;
    --focus: #f0e442;
    --warn-bg: #3a2410;
    --warn-edge: #e69f00;
    --ok-edge: #009e73;
    --s-trk: #56b4e9;
    --s-fix: #e69f00;
    --s-use: #f0e442;
    --s-net: #009e73;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.wrap { max-width: 72rem; margin: 0 auto; padding: 0 0.75rem; }

h1 { font-size: 1.35rem; line-height: 1.2; margin: 0; }
h2 { font-size: 1.15rem; line-height: 1.3; margin: 0 0 0.6rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 0.25rem; }
p { margin: 0 0 0.6rem; }
a { color: var(--link); }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 2px; }
/* the date field keeps its ring while its inner parts (day, month, year, calendar button) have focus */
input[type="date"]:focus, .day-field:focus-within input[type="date"] { outline: 3px solid var(--focus); outline-offset: 2px; }
main:focus { outline: none; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

.skip-link {
  position: absolute; left: 0.5rem; top: -4rem; z-index: 10;
  background: var(--surface); color: var(--link); padding: 0.6rem 0.9rem; border: 2px solid var(--link); border-radius: 4px;
}
.skip-link:focus { top: 0.5rem; }

.site-header { background: var(--accent); color: var(--on-accent); padding: 0.75rem 0; }
.header-row { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; align-items: center; justify-content: space-between; }
.tagline { margin: 0.15rem 0 0; font-size: 0.9rem; }
.badge {
  margin: 0; padding: 0.2rem 0.6rem; border: 2px solid var(--on-accent); border-radius: 999px;
  font-size: 0.9rem; font-weight: 600;
}

main { padding-top: 0.75rem; padding-bottom: 1rem; }
section, .card { margin-bottom: 1rem; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 0.85rem;
}
.hint { color: var(--muted); font-size: 0.9rem; }

/* ---- status ---- */
.freshness {
  display: flex; gap: 0.5rem; align-items: flex-start; margin: 0 0 0.75rem; padding: 0.55rem 0.7rem;
  border: 1px solid var(--border); border-left: 6px solid var(--control-border); border-radius: 6px; font-weight: 600;
}
.freshness[data-fresh="fresh"] { border-left-color: var(--ok-edge); }
.freshness[data-fresh="stale"], .freshness[data-fresh="empty"] {
  border-left-color: var(--warn-edge); border-left-style: double; border-left-width: 9px; background: var(--warn-bg);
}
.fresh-icon { font-size: 1.2rem; line-height: 1.25; }

.status-grid, .kpi-grid { display: grid; gap: 0.6rem; margin: 0; }
.status-grid { grid-template-columns: repeat(auto-fit, minmax(min(8.5rem, 100%), 1fr)); }
.status-grid .wide { grid-column: 1 / -1; }
.status-grid div { min-width: 0; }
dt { color: var(--muted); font-size: 0.85rem; }
dd { margin: 0; overflow-wrap: anywhere; }
.status-grid dd { font-weight: 600; }

/* ---- messages ---- */
.msg {
  margin: 0 0 1rem; padding: 0.7rem 0.85rem; background: var(--warn-bg); color: var(--text);
  border: 2px solid var(--warn-edge); border-radius: 8px;
}
.msg-error { border-style: solid; border-width: 2px 2px 2px 9px; }

/* ---- day picker ---- */
.day-picker { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: flex-end; margin-bottom: 0.75rem; }
.day-field { display: flex; flex-direction: column; min-width: 0; }
.day-field label { font-size: 0.85rem; color: var(--muted); }
input[type="date"] {
  font: inherit; color: var(--text); background: var(--surface); min-height: 2.75rem; padding: 0.3rem 0.5rem;
  border: 2px solid var(--control-border); border-radius: 6px; max-width: 100%;
}
.btn {
  font: inherit; font-weight: 600; color: var(--text); background: var(--surface); min-height: 2.75rem; padding: 0.4rem 0.8rem;
  border: 2px solid var(--control-border); border-radius: 6px; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--text); }
.btn:disabled { color: var(--muted); border-style: dashed; cursor: not-allowed; }
.btn-small { min-height: 2.75rem; }
.summary { font-size: 1.05rem; margin-bottom: 0.4rem; }

/* ---- KPI cards ---- */
.kpi-grid { grid-template-columns: repeat(auto-fit, minmax(min(9.5rem, 100%), 1fr)); }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 0.65rem 0.75rem; min-width: 0;
}
.kpi-main { border: 2px solid var(--text); }
.kpi dt { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.dt-sub { font-size: 0.8rem; }
.kpi-value { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.unit { color: var(--muted); font-weight: 600; }
.kpi-note { color: var(--muted); font-size: 0.85rem; }
.key { display: inline-block; width: 1.5rem; height: 0; border-top: 3px solid; flex: none; }
.key-trk { border-color: var(--s-trk); }
.key-fix { border-color: var(--s-fix); border-top-style: dashed; }
.key-use { border-color: var(--s-use); border-top-style: dotted; }
.quality { margin: 0.6rem 0 0; padding: 0; list-style: none; font-size: 0.9rem; }
.quality li { padding: 0.15rem 0; }

/* ---- charts ---- */
figure { margin: 0 0 1rem; }
figcaption { margin-bottom: 0.5rem; }
.canvas-box { position: relative; height: 18rem; max-height: 75vh; min-width: 0; }
.legend { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; list-style: none; margin: 0 0 0.4rem; padding: 0; font-size: 0.9rem; }
.legend li { display: inline-flex; align-items: center; gap: 0.4rem; }
.swatch { display: inline-block; width: 1.1rem; height: 0.9rem; flex: none; }
.swatch-gain { background: var(--s-trk); }
.swatch-loss {
  border: 2px solid var(--s-fix);
  background: repeating-linear-gradient(135deg, var(--s-fix) 0 2px, var(--surface) 2px 6px);
}
details.data-table { margin-top: 0.5rem; }
summary { cursor: pointer; font-weight: 600; color: var(--link); padding: 0.6rem 0; min-height: 2.75rem; }

/* ---- tables ---- */
.table-scroll { overflow-x: auto; max-width: 100%; }
table { border-collapse: collapse; width: 100%; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
caption { text-align: left; color: var(--muted); padding: 0.3rem 0; }
th, td { padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { border-bottom: 2px solid var(--control-border); white-space: normal; vertical-align: bottom; }
td.empty { text-align: left; color: var(--muted); white-space: normal; }
td[data-col="matlab"] { text-align: left; white-space: normal; min-width: 11rem; }

/* ---- export + footer ---- */
.export-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.5rem; }
.export-list li { display: flex; flex-wrap: wrap; gap: 0.25rem 0.75rem; align-items: center; }
.site-footer { padding: 0.5rem 0 2rem; color: var(--muted); font-size: 0.9rem; }

@media (min-width: 60rem) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
  #charts { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
  #charts > h2 { grid-column: 1 / -1; }
  #fig-power, #fig-daily-gain { grid-column: 1 / -1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}

/* CSV export feedback (download or clipboard) */
.export-status { margin: 0.75rem 0 0; font-weight: 600; }
.export-status:empty { display: none; }

/* Status timeline (taller: seven labelled rows) */
.canvas-box-timeline { height: 300px; }

/* MATLAB charts embedded from ThingSpeak */
.matlab-grid { display: grid; gap: 0.75rem; grid-template-columns: 1fr; }
@media (min-width: 900px) { .matlab-grid { grid-template-columns: 1fr 1fr; } }
.matlab-grid figure { margin: 0; }
.matlab-grid figcaption { font-weight: 600; margin-bottom: 0.35rem; }
/* MATLAB draws the figure at the frame's size when the page loads (width=auto&height=auto),
   so a taller frame on phones keeps the titles and axis labels readable. */
.matlab-grid iframe { display: block; width: 100%; height: auto; aspect-ratio: 4 / 3; min-height: 300px; max-height: 520px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
@media (max-width: 480px) { .matlab-grid iframe { aspect-ratio: 1 / 1; } }
.matlab-grid .hint { margin: 0.25rem 0 0; }
