:root {
  --bg: #000;
  --panel: #111214;
  --panel-2: #1c1d21;
  --line: #2a2c31;
  --text: #f2f3f5;
  --muted: #9a9ca3;
  --accent: #4f8cff;
  --ok: #34c759;
  --warn: #ffb020;
  --bad: #ff453a;
  --off: #6b6e76;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --app-h: 100dvh;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }

#app { position: fixed; inset: 0; height: var(--app-h); overflow: hidden; }
.view { position: absolute; inset: 0; display: flex; flex-direction: column; }

/* ---------- device list ---------- */
#list-view { padding: calc(var(--safe-top) + 12px) 16px calc(var(--safe-bottom) + 16px); overflow-y: auto; }
.list-header { display: flex; align-items: baseline; justify-content: space-between; margin: 8px 4px 16px; }
.list-header h1 { font-size: 34px; margin: 0; letter-spacing: -0.02em; }
.bridge-status { font-size: 13px; color: var(--muted); }
.bridge-status.bad { color: var(--bad); }
.device-list { display: flex; flex-direction: column; gap: 10px; }
.device-card {
  display: flex; align-items: center; gap: 14px; width: 100%; text-align: left;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 14px 16px;
}
.device-card:active { background: var(--panel-2); }
.device-card .glyph {
  width: 30px; height: 46px; border: 2px solid var(--muted); border-radius: 7px; flex: none; position: relative;
}
.device-card.online .glyph { border-color: var(--text); }
.device-card .meta { flex: 1; min-width: 0; }
.device-card .name { font-weight: 600; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-card .sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.chip { font-size: 13px; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--panel-2); color: var(--muted); white-space: nowrap; }
.chip.online { color: var(--ok); background: rgba(52,199,89,.12); }
.chip.warn { color: var(--warn); background: rgba(255,176,32,.12); }
.chip.offline { color: var(--muted); }
.list-foot { color: var(--muted); font-size: 13px; text-align: center; margin-top: 24px; }
.empty { color: var(--muted); text-align: center; padding: 40px 16px; }
.device-list.compact .device-card { background: var(--panel-2); }

/* ---------- device view ---------- */
.topbar {
  flex: none; display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 4px) 6px 4px; background: var(--bg); z-index: 2;
}
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; color: var(--text); }
.icon-btn:active { background: var(--panel-2); }
#btn-more svg circle { fill: currentColor; stroke: none; }
.title-btn { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 16px; padding: 8px 12px; border-radius: 12px; max-width: 65%; }
.title-btn:active { background: var(--panel-2); }
.title-btn #dev-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-btn .chev { width: 16px; height: 16px; color: var(--muted); flex: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); flex: none; }
.dot.live { background: var(--ok); box-shadow: 0 0 8px rgba(52,199,89,.7); }
.dot.connecting { background: var(--warn); animation: pulse 1s infinite alternate; }
.dot.bad { background: var(--bad); }
@keyframes pulse { to { opacity: .35; } }

.stage { flex: 1; min-height: 0; position: relative; display: grid; place-items: center; overflow: hidden; }
#screen { display: block; touch-action: none; background: #050505; border-radius: 10px; box-shadow: 0 0 0 1px var(--line); }
.stage.live #screen { box-shadow: none; }

.overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; padding: 24px; text-align: center; background: rgba(0,0,0,.55); backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px); transition: opacity .2s; pointer-events: none;
}
.overlay.show { pointer-events: auto; }
.overlay:not(.show) { opacity: 0; }
.ov-title { font-size: 18px; font-weight: 600; }
.ov-sub { color: var(--muted); font-size: 14px; max-width: 300px; }
.ov-actions { display: flex; gap: 10px; margin-top: 8px; }
.ov-actions button { background: var(--panel-2); border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; font-weight: 600; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid rgba(255,255,255,.2); border-top-color: var(--text); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: absolute; left: 50%; bottom: 14px; transform: translate(-50%, 20px); opacity: 0;
  background: rgba(40,42,48,.95); padding: 8px 14px; border-radius: 999px; font-size: 14px;
  transition: all .2s; pointer-events: none; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.navbar {
  flex: none; display: grid; grid-template-columns: repeat(4, 1fr); padding: 2px 8px calc(var(--safe-bottom) + 2px);
  background: var(--bg);
}
.navbar button { height: 50px; display: grid; place-items: center; border-radius: 14px; color: var(--text); }
.navbar button:active, .navbar button.active { background: var(--panel-2); }
.navbar svg { width: 26px; height: 26px; }
.navbar button[data-key="back"] svg { fill: currentColor; stroke-width: 1.5; }

.compose { flex: none; background: var(--panel); border-top: 1px solid var(--line); padding: 8px 10px 6px; }
.compose-row { display: flex; gap: 8px; align-items: center; }
.compose-row input {
  flex: 1; min-width: 0; height: 42px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg);
  color: var(--text); padding: 0 12px; font-size: 16px; outline: none; -webkit-user-select: text; user-select: text;
}
.compose-row input:focus { border-color: var(--accent); }
.mini-btn { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 12px; color: var(--muted); }
.mini-btn.on { color: var(--accent); }
.send-btn { height: 42px; padding: 0 16px; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600; }
.compose-keys { display: flex; gap: 6px; margin-top: 8px; overflow-x: auto; }
.compose-keys button { flex: none; background: var(--panel-2); padding: 8px 12px; border-radius: 10px; font-size: 14px; }
.compose-keys button:active { background: var(--line); }
#btn-compose-close { margin-left: auto; color: var(--accent); font-weight: 600; background: none; }

/* ---------- sheets ---------- */
.scrim { position: absolute; inset: 0; background: rgba(0,0,0,.5); z-index: 10; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 11; max-height: 85%; overflow-y: auto;
  background: var(--panel); border-radius: 20px 20px 0 0; padding: 8px 16px calc(var(--safe-bottom) + 16px);
  animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 14px; }
.action-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.action-grid button {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 4px; border-radius: 14px;
  background: var(--panel-2); font-size: 12px; color: var(--text);
}
.action-grid button:active { background: var(--line); }
.sheet-section { margin-top: 16px; }
.sheet-label { color: var(--muted); font-size: 13px; margin: 0 4px 8px; }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--panel-2); border-radius: 12px; padding: 3px; }
.segmented button { padding: 9px 4px; border-radius: 9px; font-size: 14px; color: var(--muted); }
.segmented button.on { background: var(--line); color: var(--text); font-weight: 600; }
.row-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.wide-btn { padding: 12px; border-radius: 12px; background: var(--panel-2); font-weight: 600; font-size: 15px; }
.wide-btn:active { background: var(--line); }
.stats { color: var(--muted); font-size: 12px; text-align: center; margin-top: 14px; font-variant-numeric: tabular-nums; }

@media (hover: hover) {
  .device-card:hover, .icon-btn:hover, .navbar button:hover { background: var(--panel-2); }
}

/* ---------- login ---------- */
.login { align-items: center; justify-content: center; padding: calc(var(--safe-top) + 24px) 24px calc(var(--safe-bottom) + 24px); text-align: center; overflow-y: auto; }
.login-icon { width: 76px; height: 76px; border-radius: 18px; }
.login h1 { font-size: 28px; margin: 14px 0 28px; letter-spacing: -0.01em; }
.login-panel { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.login-hint { color: var(--muted); font-size: 14px; margin: 0; }
.login-hint code { color: var(--text); font-size: 12px; background: var(--panel-2); padding: 2px 5px; border-radius: 6px; -webkit-user-select: text; user-select: text; }
.primary-btn { height: 50px; border-radius: 14px; background: var(--accent); color: #fff; font-weight: 600; font-size: 17px; }
.primary-btn:disabled { opacity: .6; }
.primary-btn.busy::after { content: "…"; }
.link-btn { color: var(--accent); font-size: 15px; padding: 8px; }
.code-input {
  height: 52px; border-radius: 14px; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  font-size: 22px; letter-spacing: .12em; text-align: center; text-transform: uppercase; outline: none;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; -webkit-user-select: text; user-select: text;
}
.code-input:focus { border-color: var(--accent); }
.login-msg { min-height: 20px; font-size: 14px; color: var(--muted); max-width: 340px; }
.login-msg.bad { color: var(--bad); }

/* ---------- account (list footer) ---------- */
.account { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 16px; margin-top: 8px; }
.account button { color: var(--accent); font-size: 14px; padding: 6px; }
.setup-code-box { margin: 14px auto 0; max-width: 340px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px; text-align: center; }
.setup-code-box .code { font: 600 24px ui-monospace, "SF Mono", Menlo, Consolas, monospace; letter-spacing: .1em; margin: 6px 0; -webkit-user-select: text; user-select: text; }
.setup-code-box p { color: var(--muted); font-size: 13px; margin: 0; }
