:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --text: #1d1f22;
  --muted: #6b6f76;
  --line: #e3e1db;
  --track: #ecebe6;
  --ok: #2f9e5a;
  --warn: #d68a00;
  --bad: #d23f31;
  --accent: #e8743b;
  --s1: #e8743b;
  --s2: #2f7fd6;
  --s3: #2f9e5a;
  --s4: #9b59b6;
  --s5: #c9a227;
  --s6: #d23f73;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214;
    --surface: #1a1c1f;
    --text: #ecebe7;
    --muted: #9a9ea6;
    --line: #2a2d31;
    --track: #24272b;
    --s2: #5aa2f0;
    --s3: #49c07a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
header, main, footer { max-width: 1160px; margin: 0 auto; padding: 0 16px; }
header {
  display: flex; justify-content: space-between; align-items: flex-end;
  gap: 12px; flex-wrap: wrap; padding-top: 28px; padding-bottom: 18px;
}
h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; display: flex; align-items: center; gap: 10px; }
h2 { margin: 0; font-size: 17px; display: flex; align-items: center; gap: 8px; }
h3 { margin: 18px 0 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.sub, .muted { color: var(--muted); }
.sub { margin: 2px 0 0; font-size: 14px; }

.live {
  display: inline-flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 700;
  letter-spacing: .08em; color: var(--bad); border: 1px solid currentColor; border-radius: 6px; padding: 2px 7px;
}
.live-dot, .dot {
  position: relative; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; display: inline-block;
}
.live .live-dot { background: var(--bad); }
.live .live-dot::after, .dot.ok::after, .card.pulse .dot::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%; background: inherit;
  animation: ping 1.8s cubic-bezier(0, 0, .2, 1) infinite;
}
@keyframes ping { 0% { transform: scale(1); opacity: .7; } 80%, 100% { transform: scale(2.8); opacity: 0; } }

.head-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.clock { font-size: 22px; font-weight: 600; letter-spacing: .02em; }
.status {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 999px; padding: 6px 12px; font-size: 13px;
}
.ok .dot, .dot.ok { background: var(--ok); }
.warn .dot, .dot.warn { background: var(--warn); }
.bad .dot, .dot.bad { background: var(--bad); }

.summary {
  display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); margin-bottom: 14px;
}
.stat {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px;
}
.stat > span { display: block; font-size: 12px; color: var(--muted); }
.stat b { font-size: 22px; font-weight: 650; }
.stat small { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 2px; }

.cards {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
.card {
  position: relative; overflow: hidden;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 16px 18px; border-top: 4px solid var(--c, var(--accent));
  transition: opacity .4s, transform .4s;
}
.card::before {
  content: ""; position: absolute; inset: 0; pointer-events: none; opacity: 0;
  background: radial-gradient(120% 80% at 20% 0%, var(--c), transparent 60%);
}
.card.flash::before { animation: flash 1.4s ease-out; }
@keyframes flash { 0% { opacity: .22; } 100% { opacity: 0; } }
.card.stale { opacity: 0.7; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.loc { font-weight: 650; font-size: 16px; }
.meta { font-size: 12px; color: var(--muted); }
.age { font-size: 12px; display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; color: var(--muted); }
.head-side { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; }
.signal { font-size: 12px; display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; color: var(--muted); cursor: help; }
.sig { display: inline-flex; align-items: flex-end; gap: 2px; height: 12px; }
.sig i { width: 3px; border-radius: 1px; background: var(--track); transition: background .6s; }
.sig i:nth-child(1) { height: 25%; }
.sig i:nth-child(2) { height: 50%; }
.sig i:nth-child(3) { height: 75%; }
.sig i:nth-child(4) { height: 100%; }
.sig.ok { --sig: var(--ok); }
.sig.warn { --sig: var(--warn); }
.sig.bad { --sig: var(--bad); }
.sig.b1 i:nth-child(-n+1), .sig.b2 i:nth-child(-n+2),
.sig.b3 i:nth-child(-n+3), .sig.b4 i:nth-child(-n+4) { background: var(--sig); }

.temp-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 8px 0 0; }
.temp { font-size: 50px; font-weight: 650; letter-spacing: -0.03em; line-height: 1.05; }
.temp small { font-size: 20px; font-weight: 500; color: var(--muted); margin-left: 2px; }
.trend { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; background: var(--track); color: var(--muted); white-space: nowrap; }
.trend.up { color: var(--bad); }
.trend.down { color: var(--s2); }

.spark { position: relative; height: 54px; margin: 8px 0 10px; }
.spark canvas { width: 100%; height: 100%; display: block; }
.spark-dot {
  position: absolute; width: 8px; height: 8px; margin: -4px 0 0 -4px; border-radius: 50%;
  background: var(--c); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 30%, transparent);
  transition: left .8s ease, top .8s ease;
}
.spark-dot::after {
  content: ""; position: absolute; inset: 0; border-radius: 50%; background: var(--c);
  animation: ping 2s cubic-bezier(0, 0, .2, 1) infinite;
}
.spark-label { position: absolute; right: 0; top: -2px; font-size: 11px; color: var(--muted); }

.hum-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.hum-head b { color: var(--text); font-size: 15px; }
.bar { height: 6px; background: var(--track); border-radius: 999px; overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; width: 0; background: var(--c); border-radius: inherit; transition: width 1s cubic-bezier(.2, .8, .2, 1); }

.row { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--line); }
.kv > span { display: block; font-size: 12px; color: var(--muted); }
.kv b { font-size: 16px; font-weight: 600; }
.range { font-size: 12px; color: var(--muted); text-align: right; }

.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 16px 18px; margin-top: 18px;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.chip { font-size: 11px; font-weight: 600; color: var(--ok); border: 1px solid currentColor; border-radius: 999px; padding: 1px 8px; }
.chip[hidden] { display: none; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; flex-wrap: wrap; }
.seg button {
  border: 0; background: transparent; color: var(--text); font: inherit; font-size: 13px;
  padding: 6px 11px; cursor: pointer; transition: background .2s, color .2s;
}
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--text); color: var(--surface); }
.chart { width: 100%; min-height: 240px; }
.chart .u-legend { font-size: 13px; color: var(--text); text-align: left; margin-top: 4px; }
.chart .u-legend th { font-weight: 500; }
.chart .u-legend .u-marker {
  width: 14px; height: 18px; margin-right: 5px; border: 0 !important; border-radius: 0;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}
#chart-temp { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M14 14.76V4.5a2 2 0 0 0-4 0v10.26a4.5 4.5 0 1 0 4 0zM12 3.9a.6.6 0 0 1 .6.6V12h-1.2V4.5a.6.6 0 0 1 .6-.6z' fill-rule='evenodd'/%3E%3C/svg%3E"); }
#chart-rh { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2.5S5.5 9.6 5.5 14.5a6.5 6.5 0 0 0 13 0C18.5 9.6 12 2.5 12 2.5z'/%3E%3C/svg%3E"); }
.uplot, .uplot * { font-family: inherit; }

footer { padding-top: 18px; padding-bottom: 32px; font-size: 12px; }

@media (max-width: 600px) {
  header { padding-top: 20px; }
  .temp { font-size: 42px; }
  .clock { font-size: 18px; }

  .summary { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .stat { padding: 8px 10px; }
  .stat > span { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .stat b { font-size: 18px; }

  .panel { padding: 14px 12px; }
  .seg { display: grid; grid-template-columns: repeat(6, 1fr); width: 100%; }
  .seg button { padding: 7px 0; }
  .chart { min-height: 0; }

  /* Selite allekkain: kuvake + nimi vasemmalla, arvo oikealla */
  .chart .u-legend, .chart .u-legend tbody { display: block; width: 100%; }
  .chart .u-legend { margin-top: 8px; }
  .chart .u-legend tr.u-series {
    display: flex !important; justify-content: space-between; align-items: center;
    padding: 6px 2px; border-top: 1px solid var(--line);
  }
  .chart .u-legend tr.u-series:first-child { border-top: 0; color: var(--muted); font-size: 12px; padding-top: 0; }
  .chart .u-legend tr.u-series:first-child .u-marker { display: none; }
  .chart .u-legend th { display: flex; align-items: center; min-width: 0; padding: 0; }
  .chart .u-legend .u-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .chart .u-legend td.u-value { padding: 0 0 0 10px; white-space: nowrap; font-weight: 600; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
