:root {
  --bg: #f6f4f0;
  --card: #fff;
  --line: #e5e7eb;
  --text: #1a1a1a;
  --dim: #6b7280;
  --faint: #9ca3af;
  --green: #3d6b45;
  --green-dark: #2f5437;
  --green-bg: #f0fdf4;
  --ok: #16a34a;
  --err: #dc2626;
  --err-bg: #fef2f2;
  --warn: #b45309;
  --warn-bg: #fffbeb;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  min-height: 100vh;
}
[hidden] { display: none !important; }

header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 24px; height: 56px; background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.header-left { display: flex; align-items: center; gap: 12px; }
.logo { height: 18px; display: block; }
.header-title { font-size: 14px; color: var(--dim); font-weight: 500; letter-spacing: .02em; }
.header-right { display: flex; align-items: center; gap: 8px; }
.conn { display: flex; align-items: center; gap: 6px; margin-right: 8px; font-size: 12px; color: var(--dim); }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.live-dot.off { background: var(--err); }
.hdr-btn {
  background: var(--card); border: 1px solid var(--line); color: var(--text);
  font-size: 13px; padding: 6px 14px; border-radius: 8px; cursor: pointer;
}
.hdr-btn:hover { border-color: var(--green); color: var(--green); }
.icon-btn {
  background: none; border: none; color: var(--dim); font-size: 16px;
  cursor: pointer; padding: 4px 8px; border-radius: 8px;
}
.icon-btn:hover { color: var(--text); }

.homes-grid {
  display: grid; gap: 16px; padding: 24px;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  max-width: 1400px; margin: 0 auto;
}
.empty { grid-column: 1 / -1; text-align: center; color: var(--faint); padding: 80px 20px; font-size: 14px; }

.home-card {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px; display: flex; flex-direction: column; gap: 10px;
}
.home-head { display: flex; justify-content: space-between; align-items: center; padding-bottom: 4px; }
.home-name { font-size: 15px; font-weight: 700; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; font-weight: 600; }
.badge.live { background: var(--green-bg); color: var(--ok); }
.badge.off { background: var(--err-bg); color: var(--err); }
.badge.wait { background: var(--warn-bg); color: var(--warn); }

.device {
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 8px;
  transition: opacity .25s;
}
.device.is-off .device-body { opacity: .35; }
.device-top { display: flex; justify-content: space-between; align-items: center; }
.device-title { font-size: 13px; font-weight: 600; }
.device-title .type { font-weight: 400; color: var(--faint); font-size: 12px; margin-left: 6px; }

.power-badge {
  font-size: 11px; font-weight: 700; padding: 3px 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); color: var(--faint);
  cursor: pointer; letter-spacing: .05em;
}
.power-badge.on { background: var(--green); border-color: var(--green); color: #fff; }
.power-badge:hover { border-color: var(--green); }
.power-badge:disabled { cursor: wait; opacity: .6; }

.device-body { display: flex; justify-content: space-between; align-items: center; transition: opacity .25s; }
.cur { display: flex; align-items: baseline; gap: 2px; }
.cur .val { font-size: 34px; font-weight: 800; letter-spacing: -1px; line-height: 1; }
.cur .unit { font-size: 14px; color: var(--dim); }
.cur.flash .val { animation: flash 1.2s ease; }
@keyframes flash { 0% { color: var(--green); } 100% {} }
.sub { font-size: 12px; color: var(--dim); text-align: right; line-height: 1.5; }

.target-ctl { display: flex; align-items: center; gap: 4px; }
.t-label { font-size: 11px; color: var(--faint); margin-right: 2px; }
.target-ctl button {
  width: 26px; height: 26px; border-radius: 6px; border: 1px solid var(--line);
  background: var(--card); color: var(--dim); font-size: 15px; cursor: pointer; line-height: 1;
}
.target-ctl button:hover { border-color: var(--green); color: var(--green); }
.target-val { min-width: 44px; text-align: center; font-size: 13px; font-weight: 700; color: var(--text); }
.device-foot { display: flex; justify-content: space-between; align-items: center; font-size: 11px; color: var(--faint); }
.err-msg { color: var(--err); font-size: 11px; }
.device.error-state { border-color: #fca5a5; }

/* ---------- 모달 ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.35);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 48px 16px; z-index: 20; overflow-y: auto;
}
.modal {
  background: var(--card); border-radius: 12px; width: 100%; max-width: 560px;
  padding: 20px; display: flex; flex-direction: column; gap: 16px;
  box-shadow: 0 10px 40px rgba(0,0,0,.12);
}
.modal.wide { max-width: 760px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head-right { display: flex; align-items: center; gap: 8px; }
.modal h2 { font-size: 15px; }
.modal h3 { font-size: 13px; color: var(--dim); margin-bottom: 8px; font-weight: 600; }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; }
.form-row input, .form-row select, #logFilter {
  flex: 1; min-width: 110px; padding: 8px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--card); color: var(--text); font-size: 13px;
}
#logFilter { flex: none; min-width: 90px; padding: 6px 10px; }
button.primary {
  background: var(--green); color: #fff; border: none; padding: 8px 16px;
  border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 13px;
}
button.primary:hover { background: var(--green-dark); }
button.primary:disabled { opacity: .5; cursor: wait; }
button.ghost {
  background: none; border: 1px solid var(--line); color: var(--dim);
  padding: 5px 12px; border-radius: 8px; cursor: pointer; font-size: 12px;
}
button.ghost:hover { color: var(--text); border-color: var(--faint); }
button.danger { color: var(--err); }
button.danger:hover { color: var(--err); border-color: var(--err); }

.s-home { border: 1px solid var(--line); border-radius: 10px; padding: 14px; margin-bottom: 12px; display: flex; flex-direction: column; gap: 10px; }
.s-home-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.s-home-head .nm { font-weight: 700; font-size: 14px; }
.s-dev {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--bg); border-radius: 8px; padding: 7px 12px; font-size: 12px;
}
.s-dev .did { color: var(--faint); font-family: monospace; font-size: 10px; }
.discover-list { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.discover-item {
  display: flex; align-items: center; gap: 8px; font-size: 12px;
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; cursor: pointer;
}
.discover-item .tag { font-size: 10px; padding: 2px 6px; border-radius: 6px; background: var(--bg); color: var(--dim); }
.discover-item .tag.hit { background: var(--green-bg); color: var(--green); }
.muted { color: var(--faint); font-size: 12px; }

/* ---------- 로그 ---------- */
.log-list { display: flex; flex-direction: column; max-height: 65vh; overflow-y: auto; }
.log-row {
  display: grid; grid-template-columns: 110px 70px 90px 1fr; gap: 10px;
  padding: 7px 4px; font-size: 12px; border-bottom: 1px solid var(--line);
  align-items: baseline;
}
.log-row .ts { color: var(--faint); font-variant-numeric: tabular-nums; }
.log-row .kind { font-weight: 600; }
.log-row .kind.temp { color: var(--green); }
.log-row .kind.power { color: var(--warn); }
.log-row .kind.control { color: #1d4ed8; }
.log-row .kind.conn { color: var(--faint); }
.log-row .home { color: var(--dim); }

/* ---------- 로그 온도 그래프 ---------- */
.log-chart { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px 6px; margin: 6px 0; }
.log-chart-title { font-size: 13px; font-weight: 600; margin-bottom: 4px; }
.log-chart-body { position: relative; }
.log-chart svg { width: 100%; display: block; }
.log-chart .grid { stroke: var(--line); stroke-width: 1; }
.log-chart .tick-label { fill: var(--faint); font-size: 10px; font-variant-numeric: tabular-nums; }
.log-chart .line { fill: none; stroke: var(--green); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.log-chart .area { fill: rgba(61,107,69,.08); stroke: none; }
.log-chart .end-dot, .log-chart .hover-dot { fill: var(--green); stroke: var(--card); stroke-width: 2; }
.log-chart .end-label { fill: var(--text); font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; }
.log-chart .xhair { stroke: var(--faint); stroke-width: 1; }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 2;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,.1); padding: 5px 9px;
  display: flex; align-items: baseline; gap: 6px; white-space: nowrap;
}
.chart-tip strong { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.chart-tip span { font-size: 11px; color: var(--dim); font-variant-numeric: tabular-nums; }

/* ---------- 스파크라인 ---------- */
.spark { width: 100%; height: 44px; display: block; margin-top: 2px; }
.spark polyline { fill: none; stroke: var(--green); stroke-width: 1.5; }
.spark path.area { fill: rgba(61,107,69,.08); stroke: none; }
.spark circle { fill: var(--green); }
.spark-cap { font-size: 10px; color: var(--faint); display: flex; justify-content: space-between; }

/* ---------- 로그인 ---------- */
.login-overlay {
  position: fixed; inset: 0; background: var(--bg); z-index: 100;
  display: flex; align-items: center; justify-content: center;
}
.login-box {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 36px 32px; display: flex; flex-direction: column; gap: 14px;
  width: 300px; align-items: center;
  box-shadow: 0 10px 40px rgba(0,0,0,.08);
}
.login-box .logo { height: 20px; }
.login-box p { font-size: 12px; color: var(--dim); margin-top: -6px; letter-spacing: .04em; }
.login-box input {
  width: 100%; padding: 10px 12px; border-radius: 8px; text-align: center;
  border: 1px solid var(--line); font-size: 14px;
}
.login-box button { width: 100%; }

#logHomeFilter {
  flex: none; min-width: 90px; padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--card); color: var(--text); font-size: 13px;
}

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; border-radius: 10px;
  padding: 10px 18px; font-size: 13px; z-index: 50; max-width: 90vw;
}
.toast.err { background: var(--err); }
