:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --ink: #1c2530;
  --muted: #6b7785;
  --line: #e3e8ee;
  --brand: #16324f;
  --accent: #2f7bd6;
  --ok: #1f9d55;
  --warn: #d6902f;
  --bad: #cc3b3b;
  --s1: #e6820c;
  --s2: #2557c9;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
}
header.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 20px; background: var(--brand); color: #fff;
}
header.topbar .brand { font-weight: 700; font-size: 1.1rem; flex: 1; }
header.topbar button { background: transparent; color: #fff; border: 1px solid rgba(255,255,255,.4); border-radius: 6px; padding: 6px 12px; cursor: pointer; }
header.topbar button:hover { background: rgba(255,255,255,.15); }
main { max-width: 1100px; margin: 0 auto; padding: 20px; }
.hidden { display: none !important; }

/* login */
#loginView { max-width: 360px; margin: 80px auto; }
#loginView .card { padding: 24px; }
#loginView h1 { font-size: 1.3rem; margin: 0 0 16px; }
#loginView input { width: 100%; margin-bottom: 10px; }
#loginView .error { color: var(--bad); font-size: .9rem; min-height: 1.2em; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; }
input, select, button { font: inherit; }
input, select { border: 1px solid var(--line); border-radius: 6px; padding: 7px 9px; }
button.primary { background: var(--accent); color: #fff; border: none; border-radius: 6px; padding: 8px 16px; cursor: pointer; }
button.primary:hover { filter: brightness(1.08); }
button.secondary { background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: 8px 14px; cursor: pointer; }
button.secondary:hover { background: #f0f2f5; }
button:disabled { opacity: .5; cursor: not-allowed; }

/* home */
.home-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.home-header h1 { font-size: 1.3rem; margin: 0; }
.device-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 14px; }
.device-card { padding: 14px; cursor: pointer; transition: box-shadow .15s; }
.device-card:hover { box-shadow: 0 3px 12px rgba(0,0,0,.08); }
.device-card .row1 { display: flex; justify-content: space-between; align-items: start; }
.device-card .name { font-weight: 600; }
.device-card .temp { font-size: 1.6rem; font-weight: 700; margin: 6px 0 2px; }
.device-card .sub { color: var(--muted); font-size: .82rem; }
.badge { font-size: .72rem; padding: 2px 8px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.badge.online { background: #e5f7ec; color: var(--ok); }
.badge.alarm { background: #fdeee0; color: var(--warn); }
.badge.offline { background: #f1f2f4; color: var(--muted); }
.sparkline { width: 100%; height: 40px; margin-top: 8px; }
.empty-state { color: var(--muted); padding: 40px; text-align: center; }

/* device page */
.device-header { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.device-header h1 { font-size: 1.3rem; margin: 0; flex: 1; min-width: 160px; }
.range-buttons { display: flex; gap: 6px; }
.range-buttons button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.chart-card { padding: 16px; margin-bottom: 16px; }
.chart-card canvas { max-height: 320px; }
.stats-row { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 10px; color: var(--muted); font-size: .88rem; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 800px) { .grid-2 { grid-template-columns: 1fr; } }

.section-card { padding: 16px; margin-bottom: 16px; }
.section-card h2 { font-size: 1rem; margin: 0 0 12px; }
.field-row { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.field-row label { width: 130px; color: var(--muted); font-size: .88rem; }
.field-row input, .field-row select { flex: 1; min-width: 90px; }

.relay-card { border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin-bottom: 10px; }
.relay-card .field-row label { width: 90px; }
.relay-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.relay-title input[type=checkbox] { width: auto; }

.report-form .field-row label { width: 150px; }
.save-bar { display: flex; justify-content: flex-end; gap: 10px; align-items: center; }
.save-bar .msg { color: var(--ok); font-size: .88rem; }
.save-bar .msg.error { color: var(--bad); }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 8px; font-size: .9rem; z-index: 50; }
