:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --line: #e2e5ea;
  --text: #15171c;
  --text-2: #5b6170;
  --text-3: #8b91a0;
  --accent: #1f9d6f;
  --accent-ink: #ffffff;
  --accent-soft: #e2f5ec;
  --danger: #d0453a;
  --warn: #b7791f;
  --bubble-in: #ffffff;
  --bubble-out: #1f9d6f;
  --bubble-out-ink: #ffffff;
  --shadow: 0 1px 2px rgba(16, 20, 30, 0.06), 0 1px 1px rgba(16, 20, 30, 0.04);
  --radius: 14px;
  --tab-h: 56px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101114;
    --surface: #1a1c21;
    --surface-2: #23262d;
    --line: #2b2f37;
    --text: #eceef2;
    --text-2: #a3a9b6;
    --text-3: #727887;
    --accent: #36c48d;
    --accent-ink: #06231a;
    --accent-soft: #16362a;
    --danger: #ef6b60;
    --warn: #e0a84a;
    --bubble-in: #23262d;
    --bubble-out: #2a9a70;
    --bubble-out-ink: #ffffff;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.app { min-height: 100%; display: flex; flex-direction: column; }
.boot { margin: auto; color: var(--text-3); padding: 40px; }

/* ── 상단 바 ───────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid transparent;
}
.topbar.scrolled { border-bottom-color: var(--line); }
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -0.01em; flex: 1; min-width: 0; }
.topbar .sub { display: block; font-size: 12px; font-weight: 500; color: var(--text-3); }
.topbar .title-wrap { flex: 1; min-width: 0; }
.topbar .title-wrap h1 { font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  border: 0; background: transparent; padding: 8px; margin: -8px; border-radius: 10px;
  display: inline-flex; align-items: center; justify-content: center; color: var(--accent);
  min-width: 40px; min-height: 40px;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600; color: var(--text-2);
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px;
  white-space: nowrap;
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-3); flex: none; }
.dot.on { background: var(--accent); }
.dot.off { background: var(--danger); }

/* ── 본문 ─────────────────────────────── */
.view { flex: 1; padding: 4px 16px calc(var(--tab-h) + var(--safe-b) + 20px); }
.view.chat { padding: 0; display: flex; flex-direction: column; }
.section-title { font-size: 13px; font-weight: 600; color: var(--text-3); margin: 22px 4px 8px; }

.chips { display: flex; gap: 6px; margin: 4px 0 12px; overflow-x: auto; scrollbar-width: none; }
.chip {
  border: 1px solid var(--line); background: var(--surface); color: var(--text-2);
  border-radius: 999px; padding: 6px 12px; font-size: 14px; font-weight: 500; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.list { list-style: none; margin: 0; padding: 0; }

.conv {
  display: flex; gap: 12px; align-items: center; padding: 12px 14px; text-decoration: none; color: inherit;
  border-bottom: 1px solid var(--line);
}
.conv:last-child { border-bottom: 0; }
.conv:active { background: var(--surface-2); }
.avatar {
  width: 44px; height: 44px; border-radius: 13px; flex: none;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 17px; color: #fff;
}
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; align-items: baseline; gap: 8px; }
.conv-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.conv-time { font-size: 12px; color: var(--text-3); flex: none; }
.conv-bottom { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.conv-text { font-size: 14px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.conv-app { font-size: 12px; color: var(--text-3); }
.badge {
  background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.muted-mark { font-size: 12px; color: var(--text-3); flex: none; }

.empty { text-align: center; color: var(--text-3); padding: 48px 20px; font-size: 15px; }
.empty strong { display: block; color: var(--text-2); font-size: 17px; margin-bottom: 6px; }

/* ── 대화 화면 ────────────────────────── */
.messages { flex: 1; padding: 12px 14px 8px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 12px; color: var(--text-3); margin: 10px 0 4px; }
.msg { max-width: 82%; display: flex; flex-direction: column; align-items: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg .who { font-size: 12px; color: var(--text-3); margin: 6px 4px 2px; }
.bubble {
  background: var(--bubble-in); padding: 8px 12px; border-radius: 18px; border-top-left-radius: 6px;
  white-space: pre-wrap; word-break: break-word; box-shadow: var(--shadow);
}
.msg.out .bubble { background: var(--bubble-out); color: var(--bubble-out-ink); border-radius: 18px; border-top-right-radius: 6px; }
.msg .meta { font-size: 11px; color: var(--text-3); margin: 2px 6px 0; }
.msg.pending .bubble { opacity: 0.6; }
.msg.failed .meta { color: var(--danger); }
.otp {
  margin-top: 6px; display: inline-flex; align-items: center; gap: 8px;
  background: var(--accent-soft); color: var(--accent); border: 0; border-radius: 10px;
  padding: 6px 10px; font-weight: 700; font-size: 15px; letter-spacing: 0.06em;
}

.composer {
  position: sticky; bottom: 0; display: flex; gap: 8px; align-items: flex-end;
  padding: 8px 12px calc(var(--safe-b) + 8px);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); background: var(--surface);
  border-radius: 20px; padding: 9px 14px; max-height: 140px; min-height: 40px; outline: none; line-height: 1.35;
}
.composer textarea:focus { border-color: var(--accent); }
.send {
  border: 0; background: var(--accent); color: var(--accent-ink); width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; flex: none;
}
.send:disabled { opacity: 0.4; }
.send svg { width: 20px; height: 20px; }
.notice {
  margin: 8px 12px calc(var(--safe-b) + 10px); padding: 10px 12px; border-radius: 12px;
  background: var(--surface-2); color: var(--text-2); font-size: 14px;
}

/* ── 제어 화면 ────────────────────────── */
.status-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; background: var(--line); }
.status-cell { background: var(--surface); padding: 12px 14px; min-width: 0; }
.status-cell .k { font-size: 12px; color: var(--text-3); }
.status-cell .v { font-size: 17px; font-weight: 600; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-cell .sub { font-size: 12px; color: var(--text-3); margin-top: 1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.loc-body { padding: 14px; display: grid; gap: 12px; }
.loc-head { display: flex; gap: 10px; align-items: flex-start; }
.loc-head svg { width: 22px; height: 22px; color: var(--accent); flex: none; margin-top: 1px; }
.loc-text { min-width: 0; }
.loc-text b { display: block; font-weight: 600; overflow-wrap: anywhere; }
.loc-text span { font-size: 13px; color: var(--text-3); }
.loc-empty { color: var(--text-2); font-size: 14px; }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.device-head { display: flex; align-items: center; gap: 10px; padding: 14px; border-bottom: 1px solid var(--line); }
.device-head .name { font-weight: 600; flex: 1; }
.device-head .seen { font-size: 12px; color: var(--text-3); }

.controls { display: grid; gap: 10px; }
.control {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: var(--surface);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.control .label { flex: 1; min-width: 0; }
.control .label b { display: block; font-weight: 600; }
.control .label span { font-size: 13px; color: var(--text-3); }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; flex: none; }
.seg button {
  border: 0; background: transparent; border-radius: 8px; padding: 7px 14px; font-size: 14px; font-weight: 600; color: var(--text-2);
}
.seg button:active { background: var(--surface); }
.seg button.primary { background: var(--accent); color: var(--accent-ink); }

.btn {
  border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 600; background: var(--accent); color: var(--accent-ink);
  width: 100%;
}
.btn.secondary { background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--line); }
.btn:disabled { opacity: 0.5; }
.btn-row { display: flex; gap: 8px; }

.log { font-size: 13px; }
.log li { display: flex; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); align-items: baseline; }
.log li:last-child { border-bottom: 0; }
.log .what { flex: 1; min-width: 0; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log .st { font-weight: 600; flex: none; }
.st-done { color: var(--accent); }
.st-failed, .st-expired { color: var(--danger); }
.st-pending, .st-sent { color: var(--warn); }
.log time { color: var(--text-3); flex: none; font-size: 12px; }

/* ── 설정 ─────────────────────────────── */
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--line); min-height: 52px; }
.row:last-child { border-bottom: 0; }
.row .label { flex: 1; min-width: 0; }
.row .label span { display: block; font-size: 13px; color: var(--text-3); }
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch .track { position: absolute; inset: 0; background: var(--surface-2); border-radius: 16px; transition: background 0.2s; border: 1px solid var(--line); }
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 25px; height: 25px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); transition: transform 0.2s;
}
.switch input:checked + .track { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .track::after { transform: translateX(20px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.help { font-size: 13px; color: var(--text-3); margin: 8px 4px 0; }
.steps { margin: 0; padding: 12px 14px 12px 34px; font-size: 14px; color: var(--text-2); }
.steps li { margin: 4px 0; }
.stack { display: grid; gap: 10px; padding: 14px; }

/* ── 로그인 ───────────────────────────── */
.login { margin: auto; width: min(360px, 100%); padding: 24px 16px; text-align: center; }
.login img { width: 72px; height: 72px; border-radius: 18px; }
.login h1 { font-size: 24px; margin: 14px 0 4px; }
.login p { color: var(--text-3); margin: 0 0 22px; }
.login input {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 12px;
  padding: 12px 14px; margin-bottom: 10px; outline: none;
}
.login input:focus { border-color: var(--accent); }
.error { color: var(--danger); font-size: 14px; min-height: 20px; margin-top: 8px; }

/* ── 하단 탭 ──────────────────────────── */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  padding-bottom: var(--safe-b); height: calc(var(--tab-h) + var(--safe-b));
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: saturate(1.6) blur(16px); -webkit-backdrop-filter: saturate(1.6) blur(16px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; color: var(--text-3); font-size: 11px; font-weight: 600; position: relative;
}
.tabbar a[aria-current="page"] { color: var(--accent); }
.tabbar svg { width: 24px; height: 24px; }
.tabbar .badge { position: absolute; top: 5px; left: calc(50% + 6px); min-width: 18px; height: 18px; font-size: 11px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-b) + 16px); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; max-width: calc(100% - 32px); text-align: center;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
