:root {
  --bg: #eef2f6;
  --card: #ffffff;
  --ink: #13212e;
  --muted: #5d6b79;
  --line: #dbe2ea;
  --brand: #0e2438;
  --accent: #1667c4;
  --accent-ink: #ffffff;
  --ok: #138a4b;
  --ok-bg: #e5f5ec;
  --warn: #b25e00;
  --warn-bg: #fff2df;
  --bad: #c62828;
  --bad-bg: #fdeaea;
  --off: #6b7785;
  --off-bg: #eceff3;
  --cold: #2a7fd4;
  --shadow: 0 1px 2px rgba(16, 32, 48, .06), 0 2px 8px rgba(16, 32, 48, .05);
  --r: 14px;
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1520;
    --card: #132231;
    --ink: #e6edf4;
    --muted: #93a3b3;
    --line: #23374a;
    --brand: #07121c;
    --accent: #4b9bf0;
    --accent-ink: #06121e;
    --ok: #3ccf82;
    --ok-bg: #11301f;
    --warn: #ffb04a;
    --warn-bg: #3a2a12;
    --bad: #ff6b6b;
    --bad-bg: #3d1717;
    --off: #8e9cab;
    --off-bg: #1c2b3a;
    --cold: #6cb2ff;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
small, .muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }

/* --- yläpalkki ---------------------------------------------------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: #fff;
}
.brand { display: flex; gap: 10px; align-items: center; }
.brand img { border-radius: 8px; }
.brand b { display: block; font-size: 16px; }
.brand small { display: block; color: #a9bdd0; font-size: 12px; }
.icon-btn { background: none; border: 0; color: inherit; padding: 8px; border-radius: 10px; cursor: pointer; }
.icon-btn:active { background: rgba(255,255,255,.12); }
.spin { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { max-width: 1100px; margin: 0 auto; padding: 14px 16px 24px; }

/* --- alapalkki ---------------------------------------------------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs a {
  position: relative; flex: 1; max-width: 140px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 11.5px; font-weight: 600;
}
.tabs svg { width: 24px; height: 24px; fill: currentColor; }
.tabs a.on { color: var(--accent); }
.badge {
  position: absolute; top: 7px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--bad); color: #fff; font: 700 11px/18px system-ui; font-style: normal; text-align: center;
}

/* --- kortit ------------------------------------------------------- */
.card { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow); border: 1px solid var(--line); padding: 14px; }
.stack > * + * { margin-top: 12px; }
.section-h { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin: 20px 2px 10px; }
.section-h:first-child { margin-top: 4px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 250px), 1fr)); }

.sensor {
  display: block; text-decoration: none; color: inherit; position: relative; overflow: hidden;
  border-left: 5px solid var(--off);
}
.sensor.s-ok { border-left-color: var(--ok); }
.sensor.s-warn { border-left-color: var(--warn); }
.sensor.s-alarm { border-left-color: var(--bad); background: linear-gradient(0deg, var(--card), var(--card)), var(--bad-bg); }
.sensor.s-alarm { background: var(--bad-bg); }
.sensor-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.sensor-name { font-weight: 650; font-size: 15px; }
.sensor-temp { font-size: 34px; font-weight: 700; letter-spacing: -.5px; margin: 4px 0 0; }
.sensor-temp .u { font-size: 18px; font-weight: 600; color: var(--muted); margin-left: 2px; }
.sensor-meta { display: flex; justify-content: space-between; color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.spark { display: block; width: 100%; height: 40px; margin-top: 8px; }

.pill {
  display: inline-flex; align-items: center; gap: 5px; white-space: nowrap;
  padding: 3px 9px; border-radius: 99px; font-size: 12px; font-weight: 650;
  background: var(--off-bg); color: var(--off);
}
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.alarm { background: var(--bad-bg); color: var(--bad); }

.banner { border-radius: var(--r); padding: 12px 14px; display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.banner.bad { background: #c62828; color: #fff; }
.banner.bad a:not(.btn) { color: #fff; font-weight: 700; }
.banner.bad .btn { background: #fff; color: #c62828; border-color: #fff; }
.banner.info { background: var(--card); border: 1px dashed var(--accent); }
.banner p { margin: 0; flex: 1 1 220px; }

/* --- painikkeet ja lomakkeet --------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; min-height: 42px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 6px 12px; min-height: 34px; font-size: 13.5px; }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

label.f { display: block; margin-top: 12px; }
label.f > span { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; padding: 10px 12px; min-height: 42px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink); font: inherit; font-size: 16px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.check { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-weight: 600; }
.check input { width: 22px; height: 22px; min-height: 0; accent-color: var(--accent); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.err { color: var(--bad); font-weight: 600; margin-top: 10px; }

.seg { display: inline-flex; background: var(--off-bg); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; color: var(--muted); font: inherit; font-weight: 650; font-size: 13.5px; padding: 6px 12px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chips button { border: 1px solid var(--line); background: var(--bg); color: var(--ink); border-radius: 99px; padding: 6px 11px; font: inherit; font-size: 13px; cursor: pointer; }

/* --- anturinäkymä --------------------------------------------------- */
.hero { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.hero .big { font-size: 48px; font-weight: 750; letter-spacing: -1px; line-height: 1; }
.hero .big .u { font-size: 22px; color: var(--muted); }
.back { display: inline-flex; align-items: center; gap: 4px; text-decoration: none; font-weight: 600; margin-bottom: 10px; }
.chart { position: relative; width: 100%; height: 260px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; }
.chart .tip {
  position: absolute; top: 4px; pointer-events: none; background: var(--brand); color: #fff;
  padding: 5px 9px; border-radius: 8px; font-size: 12.5px; white-space: nowrap; transform: translateX(-50%);
}
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.stat { background: var(--bg); border-radius: 10px; padding: 10px 12px; }
.stat b { display: block; font-size: 18px; }
.stat small { font-size: 12px; }

/* --- listat ---------------------------------------------------------- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { padding: 12px 0; border-top: 1px solid var(--line); }
.list > li:first-child { border-top: 0; padding-top: 2px; }
.alarm-h { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.alarm-h b { font-size: 15px; }
.alarm-ack { margin-top: 8px; padding: 8px 10px; background: var(--bg); border-radius: 8px; font-size: 13.5px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; color: var(--muted); font-weight: 650; text-transform: uppercase; letter-spacing: .3px; }
td.r, th.r { text-align: right; }
table.num td, table.num th { white-space: nowrap; }
@media (max-width: 480px) { th, td { padding: 6px 5px; } table { font-size: 13.5px; } }
tr.out td { background: var(--warn-bg); }
.scroll-x { overflow-x: auto; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
pre.code { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px; font-size: 12.5px; white-space: pre-wrap; word-break: break-all; }

/* --- kirjautuminen --------------------------------------------------- */
.login { max-width: 400px; margin: 7vh auto 0; }
.login h1 { font-size: 22px; margin-bottom: 4px; }

/* --- dialogi & ilmoitus ---------------------------------------------- */
dialog {
  border: 0; border-radius: 16px; padding: 0; width: min(520px, calc(100vw - 24px));
  background: var(--card); color: var(--ink); box-shadow: 0 10px 40px rgba(0,0,0,.3);
  max-height: calc(100vh - 40px);
}
dialog::backdrop { background: rgba(8, 18, 28, .55); }
dialog form { padding: 18px; }
dialog h2 { margin-bottom: 4px; }
dialog .btn-row { margin-top: 18px; justify-content: flex-end; }
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--brand); color: #fff; padding: 10px 16px; border-radius: 10px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.empty { text-align: center; padding: 30px 10px; color: var(--muted); }
.loading { text-align: center; padding: 40px; color: var(--muted); }

/* --- tulostus (omavalvontaraportti) --------------------------------- */
.print-only { display: none; }
@media print {
  :root { --bg: #fff; --card: #fff; --ink: #000; --muted: #444; --line: #bbb; --warn-bg: #f3f3f3; color-scheme: light; }
  body { padding: 0; font-size: 11pt; }
  .top, .tabs, .no-print, #toast { display: none !important; }
  .print-only { display: block; }
  main { padding: 0; max-width: none; }
  .card { box-shadow: none; border: 0; padding: 0; break-inside: avoid; }
  .report-sensor { break-inside: avoid; margin-top: 14pt; }
  table { font-size: 9.5pt; }
  th, td { padding: 3px 5px; }
}
