:root {
  /* 송란건설 브랜드: 청록 #054a4f / 베이지 #f0eee1 */
  --brand-teal: #054a4f;
  --brand-cream: #f0eee1;
  --bg: #f6f4ec;
  --surface: #ffffff;
  --surface-2: #efede3;
  --text: #142326;
  --muted: #6c7471;
  --border: #e3e0d4;
  --primary: var(--brand-teal);
  --primary-text: var(--brand-cream);
  --on: #16a34a;
  --danger: #d93025;
  --warn-bg: #fff4e5;
  --info-bg: #e3eeec;
  --error-bg: #fdecea;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --tabbar-h: 60px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1415;
    --surface: #142021;
    --surface-2: #1c2b2c;
    --text: #e9ece6;
    --muted: #9aa8a6;
    --border: #263738;
    /* 어두운 배경에서는 밝은 청록 + 진한 청록 글자 */
    --primary: #5fb3aa;
    --primary-text: #062a2d;
    --on: #22c55e;
    --danger: #ff6b5e;
    --warn-bg: #3a2a12;
    --info-bg: #12302f;
    --error-bg: #3b1714;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Malgun Gothic", sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
body.page-login { padding-bottom: 0; }
a { color: var(--primary); text-decoration: none; }
code { font-size: .85em; background: var(--surface-2); padding: 1px 5px; border-radius: 6px; word-break: break-all; }

/* ── 상단 로고 (송란건설) + 제목 줄: 함께 항상 위에 고정 ── */
.topbar { position: sticky; top: 0; z-index: 10; }
.brandbar {
  /* 로고 이미지의 색 구분(왼쪽 32% 청록 / 오른쪽 크림)과 맞춰 노치 영역까지 자연스럽게 이어지도록 */
  background: linear-gradient(90deg, var(--brand-teal) 32%, var(--brand-cream) 32%);
  padding-top: env(safe-area-inset-top);
}
.brandbar picture, .brandbar img { display: block; width: 100%; height: auto; }
@media (min-width: 721px) {
  /* 넓은 화면: 로고는 720px 로 가운데, 양옆은 각 색으로 채움 */
  .brandbar { background: linear-gradient(90deg, var(--brand-teal) 50%, var(--brand-cream) 50%); }
  .brandbar picture { max-width: 720px; margin: 0 auto; }
}

/* ── 상단 바 ── */
.appbar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.appbar-title { flex: 1; margin: 0; font-size: 20px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  display: inline-grid; place-items: center;
  min-width: 40px; height: 40px; padding: 0 8px;
  border: 0; border-radius: 12px; background: transparent; color: var(--text);
  font-size: 24px; line-height: 1; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

.content { max-width: 720px; margin: 0 auto; padding: 4px 16px 24px; }

/* ── 하단 탭 ── */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px; gap: 2px;
}
.tabbar a span { font-size: 22px; filter: grayscale(1); opacity: .7; }
.tabbar a.active { color: var(--primary); font-weight: 600; }
.tabbar a.active span { filter: none; opacity: 1; }

/* ── 공통 ── */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 12px 0; }
.section-title { font-size: 15px; font-weight: 700; color: var(--muted); margin: 20px 4px 8px; }
.card .section-title { margin: 0 0 12px; color: var(--text); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hint { color: var(--muted); font-size: 13px; margin: 12px 0 0; }
.alert { border-radius: 12px; padding: 10px 14px; margin: 12px 0; font-size: 14px; }
.alert.error { background: var(--error-bg); color: var(--danger); }
.alert.info { background: var(--info-bg); }
.error-text { color: var(--danger); }
.badge { display: inline-block; font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); vertical-align: 2px; margin-left: 4px; }
.badge.ok { background: color-mix(in srgb, var(--on) 15%, transparent); color: var(--on); }
.badge.bad { background: var(--error-bg); color: var(--danger); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 46px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn.block { display: flex; width: 100%; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; }

/* ── 폼 ── */
.form label { display: block; margin: 0 0 14px; font-size: 14px; color: var(--muted); }
.form input:not([type=checkbox]), .form select, .search input {
  display: block; width: 100%; margin-top: 6px;
  min-height: 48px; padding: 0 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 16px; /* iOS 확대 방지 */
}
.form input:focus, .form select:focus, .search input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.form label.check { display: flex; align-items: center; gap: 10px; color: var(--text); font-size: 15px; margin: 0; padding: 10px 0; border-bottom: 1px solid var(--border); }
.form label.check input { width: 20px; height: 20px; accent-color: var(--primary); }
fieldset { border: 0; padding: 0; margin: 8px 0 16px; }
legend { font-size: 14px; color: var(--muted); margin-bottom: 4px; }
details.card > summary { cursor: pointer; list-style: none; margin: 0; }
details.card > summary::after { content: "＋"; float: right; }
details.card[open] > summary { margin-bottom: 12px; }
details.card[open] > summary::after { content: "－"; }
.search { display: flex; gap: 8px; margin: 12px 0; }
.search input { margin: 0; }
.search .btn { flex: none; white-space: nowrap; }
.logout { margin: 24px 0; }

/* ── 로그인 ── */
.login-wrap { max-width: 400px; margin: 0 auto; padding: 32px 20px 32px; text-align: center; }
.login-wrap .form { text-align: left; margin-top: 24px; }
.login-logo { border-radius: 18px; }
.login-title { font-size: 24px; margin: 12px 0 4px; }

/* ── 장치 카드 ── */
.device-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 560px) { .device-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.device-card {
  position: relative; min-height: 132px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  transition: background .2s;
}
.device-card.is-on { background: color-mix(in srgb, var(--primary) 12%, var(--surface)); }
.device-card.is-offline { opacity: .55; }
.device-link { display: flex; flex-direction: column; height: 100%; padding: 14px; color: inherit; }
.device-icon { font-size: 30px; line-height: 1; }
.device-icon.big { font-size: 44px; }
.device-name { margin-top: auto; font-weight: 700; font-size: 15px; line-height: 1.3; word-break: keep-all; padding-right: 4px; }
.device-sub { font-size: 13px; color: var(--muted); }
.power {
  position: absolute; top: 10px; right: 10px;
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: var(--surface-2); cursor: pointer;
  display: grid; place-items: center;
}
.power-icon { width: 22px; height: 22px; fill: none; stroke: var(--muted); stroke-width: 2.4; stroke-linecap: round; }
.power[aria-pressed=true] { background: var(--primary); }
.power[aria-pressed=true] .power-icon { stroke: var(--primary-text); }
.power.busy .power-icon { animation: pulse .8s ease-in-out infinite; }
.power:disabled { cursor: default; }
@keyframes pulse { 50% { opacity: .3; } }

/* ── 장치 상세 ── */
.device-hero { display: flex; align-items: center; gap: 16px; }
.online { font-weight: 700; }
.online.yes { color: var(--on); }
.online.no { color: var(--danger); }
.controls { padding: 4px 16px; }
.control-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border); }
.control-row:last-child { border-bottom: 0; }
.control-row.column { flex-direction: column; align-items: stretch; }
.row-head { display: flex; justify-content: space-between; }
.row-head output { font-weight: 700; }
input[type=range] { width: 100%; accent-color: var(--primary); height: 32px; }
.segmented { display: flex; flex-wrap: wrap; gap: 6px; }
.segmented button {
  flex: 1 0 auto; min-height: 42px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font: inherit; cursor: pointer;
}
.segmented button.selected { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }

.switch { position: relative; width: 56px; height: 32px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch .slider { position: absolute; inset: 0; border-radius: 999px; background: var(--border); transition: .2s; cursor: pointer; }
.switch .slider::before { content: ""; position: absolute; width: 26px; height: 26px; left: 3px; top: 3px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: .2s; }
.switch input:checked + .slider { background: var(--primary); }
.switch input:checked + .slider::before { transform: translateX(24px); }
.switch input:disabled + .slider { opacity: .5; cursor: default; }
.switch input:focus-visible + .slider { outline: 2px solid var(--primary); outline-offset: 2px; }

.readings, .kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; }
.readings dt, .kv dt { color: var(--muted); }
.readings dd, .kv dd { margin: 0; text-align: right; font-weight: 600; }

/* ── 홈 화면 설치 안내 ── */
.install { display: flex; align-items: center; gap: 10px; background: var(--info-bg); }
.install > div { flex: 1; font-size: 14px; }

/* ── 목록 / 관리 ── */
.subnav { display: flex; gap: 6px; overflow-x: auto; margin: 4px -16px 0; padding: 0 16px 4px; scrollbar-width: none; }
.subnav a { flex: none; padding: 8px 14px; border-radius: 999px; background: var(--surface); color: var(--muted); font-size: 14px; font-weight: 600; }
.subnav a.active { background: var(--primary); color: var(--primary-text); }
.list { list-style: none; margin: 0; padding: 0; }
.list.card { padding: 0 16px; }
.list-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 0; border-bottom: 1px solid var(--border); color: inherit; }
.list li:last-child .list-item, .list li.list-item:last-child { border-bottom: 0; }
.chev { color: var(--muted); font-size: 22px; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-top: 12px; }
.stat { margin: 0; display: flex; flex-direction: column; color: inherit; }
.stat b { font-size: 26px; }
.stat span { color: var(--muted); font-size: 13px; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.pager { display: flex; justify-content: space-between; gap: 8px; }
.danger-zone { margin: 24px 0; }

.empty { text-align: center; padding: 64px 16px; }
.empty-icon { font-size: 48px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 14px; transition: .25s; z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
