/* Tema scuro caldo (ambra/miele) di default; chiaro quando il sistema lo
   chiede. I colori delle serie sono token condivisi dai grafici Chart.js
   (letti via getComputedStyle in app.js), dalle sparkline e dalla heatmap. */
:root {
  --bg: #16130d; --card: #201b12; --card2: #2a2316;
  --text: #f3ead6; --muted: #a69a82; --line: #3a3121;
  --accent: #f0b429; --err: #ff7b6b; --ok: #8fd18f;
  --c-weight: #f0b429; --c-temp: #ff8c42; --c-hum: #5bb8ff;
  --c-act: #b68cff; --c-batt: #7bd389;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f3ea; --card: #fffdf8; --card2: #fbf5e8;
    --text: #1c1810; --muted: #6e6454; --line: #e6dcc6;
    --accent: #c8901a; --err: #b3261e; --ok: #2e7d32;
    /* Serie piu' scure: le stesse tinte del tema scuro sul fondo chiaro
       stanno a 1,8-2,5:1 di contrasto; queste passano il validatore (>= 3:1). */
    --c-weight: #b07a0a; --c-temp: #d9541e; --c-hum: #1f7fd1;
    --c-act: #7a4fd6; --c-batt: #2f8f46;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; padding: 16px; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow-x: hidden;
}

/* ---- header ------------------------------------------------------------ */
#top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px;
  margin-bottom: 16px;
}
#brand { display: flex; align-items: center; gap: 8px; }
#brand h1 { margin: 0; font-size: 1.35rem; letter-spacing: .01em; }
#brand .bee { font-size: 1.4rem; }
.status {
  flex: 1 1 260px; color: var(--muted); font-size: .88rem;
  font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis;
}
.status .sep { margin: 0 .5em; opacity: .6; }
#period { margin-left: auto; }

.banner {
  background: var(--err); color: #1a0a08; padding: 10px 14px;
  border-radius: 8px; margin-bottom: 16px; font-size: .9rem;
}
@media (prefers-color-scheme: light) { .banner { color: #fff; } }

/* ---- segmented control ------------------------------------------------- */
.seg {
  display: inline-flex; border: 1px solid var(--line); border-radius: 999px;
  padding: 2px; background: var(--card); gap: 2px;
}
.seg button {
  border: 0; background: transparent; color: var(--muted); font: inherit;
  font-size: .78rem; padding: 3px 10px; border-radius: 999px; cursor: pointer;
  line-height: 1.4; min-height: 26px;
}
.seg button[aria-pressed="true"] { background: var(--accent); color: #1a1205; font-weight: 600; }
.seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- griglie ----------------------------------------------------------- */
.kpis {
  display: grid; gap: 12px; margin-bottom: 14px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
}
@media (max-width: 600px) { .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.main {
  display: grid; gap: 14px; margin-bottom: 14px;
  /* 420 px: due colonne fino a ~1300 px (come il mockup), una sola su telefono. */
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr));
}
.main > #events { grid-column: 1 / -1; }
.secondary {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

/* ---- card -------------------------------------------------------------- */
.card {
  background: linear-gradient(160deg, var(--card2), var(--card));
  border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px;
  min-width: 0; animation: rise .3s ease-out both;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: none; }
}
.card h2 {
  margin: 0 0 10px; font-size: .76rem; font-weight: 600;
  letter-spacing: .07em; text-transform: uppercase; color: var(--muted);
}
.card.stale { opacity: .55; }
.card.card-error { border-color: var(--err); }
.card.card-error .body { color: var(--err); }
.secondary .card { padding: 10px 14px; }
.secondary .card h2 { font-size: .7rem; margin-bottom: 6px; }
.value { font-size: 1.9rem; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.unit { font-size: .95rem; color: var(--muted); margin-left: 3px; }
.row { display: flex; justify-content: space-between; gap: 8px; padding: 3px 0; }
.row span:last-child { font-variant-numeric: tabular-nums; text-align: right; }
.note { margin-top: 8px; font-size: .76rem; color: var(--muted); }
.note:empty { display: none; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; vertical-align: 0; }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--err); }
.dot.off { background: var(--muted); }
a { color: var(--accent); }

/* ---- KPI tile ---------------------------------------------------------- */
.kpi { padding: 12px 14px; }
.kpi-label { font-size: .74rem; font-weight: 600; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.kpi-value { font-size: 1.75rem; font-weight: 650; font-variant-numeric: tabular-nums; line-height: 1.25; margin-top: 2px; }
.kpi-delta { font-size: .8rem; font-variant-numeric: tabular-nums; color: var(--muted); }
.kpi-delta.up { color: var(--ok); }
.kpi-delta.down { color: var(--err); }
.spark { display: block; width: 100%; height: 32px; margin-top: 8px; overflow: visible; }
.spark path {
  fill: none; stroke: var(--spark, var(--accent)); stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke;
}
.spark.empty { opacity: .25; }
.kpi-span { font-size: .7rem; color: var(--muted); margin-top: 2px; }
.kpi-span:empty { display: none; }
#kpi-weight { --spark: var(--c-weight); }
#kpi-temp   { --spark: var(--c-temp); }
#kpi-hum    { --spark: var(--c-hum); }
#kpi-act    { --spark: var(--c-act); }
@media (max-width: 600px) { .kpi-value { font-size: 1.45rem; } }

/* ---- card con toggle Valore | Grafico | Tabella -------------------------- */
.panel .head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.panel .head h2 { margin: 0; }
.panel > .body, .panel > .chart, .panel > .table { display: none; }
.panel[data-view="value"] > .body { display: block; }
.panel[data-view="chart"] > .chart { display: block; }
.panel[data-view="table"] > .table { display: block; }
.panel .chart { position: relative; height: 220px; }
.panel .chart canvas { width: 100% !important; height: 100% !important; }
.panel .table { max-height: 260px; overflow-y: auto; }
table { width: 100%; border-collapse: collapse; font-size: .86rem; font-variant-numeric: tabular-nums; }
th, td { padding: 4px 6px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: .74rem; letter-spacing: .04em; text-transform: uppercase; position: sticky; top: 0; background: var(--card); }
th:first-child, td:first-child { text-align: left; }

/* ---- heatmap ----------------------------------------------------------- */
.heat { display: grid; grid-template-columns: 3.2em repeat(24, minmax(0, 1fr)); gap: 2px; align-items: center; }
.heat .lbl { font-size: .72rem; color: var(--muted); white-space: nowrap; padding-right: 4px; }
.heat .hr { font-size: .66rem; color: var(--muted); text-align: left; line-height: 1; }
.heat .cell { display: block; background: var(--c-act); border-radius: 2px; aspect-ratio: 1; min-height: 6px; }
.heat.dense .cell { aspect-ratio: auto; height: 6px; }
.heat.dense .lbl { font-size: .6rem; line-height: 1; }
.heat.dense { gap: 1px 2px; }

/* ---- timeline eventi --------------------------------------------------- */
.events { list-style: none; margin: 0; padding: 0; }
.events li { display: flex; align-items: baseline; gap: 10px; padding: 5px 0; border-bottom: 1px solid var(--line); font-size: .9rem; }
.events li:last-child { border-bottom: 0; }
.events time { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 3.4em; font-size: .82rem; }
.events .ico { width: 1.4em; text-align: center; }

@media (prefers-reduced-motion: reduce) {
  .card { animation: none; }
}
