:root {
  --bg: #070a0f;
  --bg-soft: #0b1118;
  --surface: #101821;
  --surface-2: #15202b;
  --surface-3: #1b2836;
  --border: #1d2b39;
  --border-2: #2a3d51;
  --text: #e9f0f7;
  --muted: #8798aa;
  --muted-2: #5f7183;
  --accent: #2dd4bf;
  --accent-2: #22d3ee;
  --green: #34d399;
  --amber: #fbbf24;
  --red: #f87171;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
  --radius: 16px;
  --topbar: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: radial-gradient(1200px 800px at 15% -10%, #10202a 0%, var(--bg) 55%) fixed;
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; display: block; }
.hidden { display: none !important; }

#app { height: 100%; display: flex; flex-direction: column; }

/* ---------- top bar ---------- */
#topbar {
  height: var(--topbar);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(16, 24, 33, 0.92), rgba(10, 15, 21, 0.86));
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-logo {
  width: 34px; height: 34px; flex: 0 0 auto;
  display: grid; place-items: center;
  border-radius: 10px;
  color: #041014;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  box-shadow: 0 4px 18px rgba(45, 212, 191, 0.35);
}
.brand-logo svg { width: 20px; height: 20px; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-text strong { font-size: 15px; letter-spacing: 0.2px; }
.brand-text small { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; align-items: center; gap: 8px; }

.pill {
  display: inline-flex; align-items: center; gap: 7px;
  height: 32px; padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px; font-weight: 600;
  white-space: nowrap;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted-2); }
button.pill:hover { border-color: var(--border-2); color: var(--text); }
.relay.connected .dot { background: var(--green); box-shadow: 0 0 10px var(--green); }
.relay.connected { color: var(--green); border-color: rgba(52, 211, 153, 0.35); }
.relay.connecting .dot { background: var(--amber); }
.relay.connecting { color: var(--amber); }
.relay.offline .dot, .relay.error .dot { background: var(--red); }
.relay.offline, .relay.error { color: var(--red); }

.icon-btn {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  transition: 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 19px; height: 19px; }

/* ---------- main ---------- */
#main { flex: 1 1 auto; min-height: 0; position: relative; }

#view-grid { height: 100%; overflow-y: auto; padding: 20px; }
#view-grid::-webkit-scrollbar { width: 10px; }
#view-grid::-webkit-scrollbar-thumb { background: #1d2b39; border-radius: 10px; border: 3px solid transparent; background-clip: content-box; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 18px;
  align-content: start;
}

.card {
  position: relative;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.16s ease, border-color 0.16s, box-shadow 0.16s;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.card:hover { transform: translateY(-2px); border-color: var(--border-2); box-shadow: 0 16px 34px rgba(0, 0, 0, 0.42); }
.card:active { transform: scale(0.995); }
.card.offline { opacity: 0.65; }

.card .thumb {
  position: relative;
  aspect-ratio: var(--ar, 0.5);
  max-height: 320px;
  background: #04070a;
  display: grid; place-items: center;
  overflow: hidden;
}
.card .thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.card .thumb .placeholder { color: var(--muted-2); display: flex; flex-direction: column; align-items: center; gap: 8px; font-size: 12px; }
.card .thumb .placeholder svg { width: 34px; height: 34px; opacity: 0.5; }
.card .thumb .stream-badge {
  position: absolute; top: 10px; left: 10px;
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 8px; border-radius: 999px;
  font-size: 10px; font-weight: 700; letter-spacing: 0.4px;
  background: rgba(8, 14, 20, 0.78);
  border: 1px solid rgba(45, 212, 191, 0.3);
  color: var(--accent);
  backdrop-filter: blur(6px);
}
.card .thumb .stream-badge i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 1.6s infinite; }
.card .thumb .off-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 4px;
  background: rgba(5, 8, 12, 0.55);
  color: var(--muted); font-size: 12px; font-weight: 600;
}
.card .thumb .off-overlay svg { width: 26px; height: 26px; opacity: 0.7; }
.card .badges { position: absolute; top: 10px; right: 10px; display: flex; gap: 6px; }
.badge {
  height: 24px; padding: 0 8px;
  display: inline-flex; align-items: center; gap: 5px;
  border-radius: 8px;
  background: rgba(6, 11, 16, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.07);
  color: var(--muted);
  font-size: 11px; font-weight: 600;
  backdrop-filter: blur(8px);
}
.badge svg { width: 13px; height: 13px; }
.badge.low { color: var(--red); }
.badge.mid { color: var(--amber); }

.card-foot { padding: 12px 14px 14px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-name { font-size: 14px; font-weight: 650; letter-spacing: 0.1px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { color: var(--muted-2); font-size: 11.5px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-actions { display: flex; gap: 2px; opacity: 0; transition: 0.15s; }
.card:hover .card-actions { opacity: 1; }
.card-actions .icon-btn { width: 30px; height: 30px; }
.card-actions .icon-btn svg { width: 16px; height: 16px; }

.empty { max-width: 520px; margin: 8vh auto 0; text-align: center; padding: 32px 26px; border: 1px dashed var(--border-2); border-radius: 22px; background: rgba(16, 24, 33, 0.55); }
.empty-icon { width: 64px; height: 64px; margin: 0 auto 18px; display: grid; place-items: center; border-radius: 18px; background: var(--surface-3); color: var(--accent); }
.empty-icon svg { width: 32px; height: 32px; }
.empty h2 { margin: 0 0 8px; font-size: 20px; }
.empty p { color: var(--muted); margin: 0 0 18px; font-size: 13.5px; }
.empty .hint { font-size: 12px; color: var(--muted-2); margin: 18px 0 0; }
.empty-actions { display: flex; gap: 10px; justify-content: center; }

.banner {
  margin: 0 0 16px;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid rgba(251, 191, 36, 0.3);
  background: rgba(251, 191, 36, 0.08);
  color: var(--amber);
  font-size: 13px;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 38px; padding: 0 16px;
  border-radius: 11px;
  border: 1px solid var(--border-2);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px; font-weight: 600;
  transition: 0.15s;
}
.btn:hover { background: var(--surface-3); border-color: #35506a; }
.btn:active { transform: scale(0.98); }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent; color: #04211e; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.danger { color: var(--red); border-color: rgba(248, 113, 113, 0.35); background: rgba(248, 113, 113, 0.08); }
.btn svg { width: 17px; height: 17px; }
.btn:disabled { opacity: 0.45; pointer-events: none; }

/* ---------- control view ---------- */
#view-control { height: 100%; display: flex; flex-direction: column; min-height: 0; }
.control-head {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 14px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: rgba(10, 15, 21, 0.7);
}
.control-title { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.control-title strong { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.control-title span { color: var(--muted-2); font-size: 11.5px; }
.control-stats { margin-left: auto; display: flex; gap: 8px; }
.stat { font-size: 11.5px; color: var(--muted); padding: 4px 9px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); font-variant-numeric: tabular-nums; }

.control-body { flex: 1 1 auto; min-height: 0; display: flex; gap: 14px; padding: 14px; }
#screen-wrap {
  flex: 1 1 auto; min-width: 0; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 20px;
  background: radial-gradient(600px 500px at 50% 40%, #0d151d 0%, #060a0e 70%);
  overflow: hidden;
}
.screen {
  position: relative;
  background: #000;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.75), 0 0 0 1px rgba(255, 255, 255, 0.06);
  touch-action: none;
  user-select: none;
}
.screen img { display: block; width: 100%; height: 100%; user-select: none; pointer-events: none; }
.screen.off img { opacity: 0.35; filter: grayscale(0.6); }
.screen-off {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: rgba(4, 7, 10, 0.65);
  font-size: 13px; color: var(--muted);
  z-index: 4;
}
.screen-loading { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(4, 7, 10, 0.6); z-index: 3; }
.screen-loading.hidden { display: none; }
.spinner {
  width: 26px; height: 26px;
  border: 2.5px solid rgba(45, 212, 191, 0.18);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.spinner.small { width: 15px; height: 15px; border-width: 2px; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

#toolbar {
  flex: 0 0 auto;
  width: 204px;
  display: flex; flex-direction: column; gap: 10px;
  overflow-y: auto;
  padding-right: 2px;
}
.tool-group {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
  padding: 10px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(16, 24, 33, 0.65);
}
.tool {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px;
  min-height: 58px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: 10.5px; font-weight: 600;
  transition: 0.14s;
}
.tool svg { width: 20px; height: 20px; }
.tool:hover { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.tool:active { transform: scale(0.95); background: var(--surface-3); }
.tool.active { color: var(--accent); border-color: rgba(45, 212, 191, 0.35); background: rgba(45, 212, 191, 0.08); }
.tool-selects { display: flex; flex-direction: column; gap: 8px; }
.mini-label { display: flex; flex-direction: column; gap: 4px; font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted-2); font-weight: 700; }
.mini-label select {
  height: 32px; padding: 0 6px;
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text);
  font-size: 10.5px;
  font-weight: 500;
  text-overflow: ellipsis;
}
.mini-label select:focus { outline: none; border-color: var(--accent); }

#textbar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px calc(10px + var(--safe-b));
  border-top: 1px solid var(--border);
  background: var(--bg-soft);
}
#text-input {
  flex: 1 1 auto;
  height: 40px;
  padding: 0 14px;
  border-radius: 11px;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
#text-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(45, 212, 191, 0.12); }

/* ---------- toasts / modal ---------- */
#toasts { position: fixed; left: 50%; bottom: calc(22px + var(--safe-b)); transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 120; pointer-events: none; }
.toast {
  padding: 11px 16px;
  border-radius: 12px;
  background: rgba(19, 28, 38, 0.96);
  border: 1px solid var(--border-2);
  color: var(--text);
  font-size: 13px;
  box-shadow: var(--shadow);
  animation: toast-in 0.18s ease;
  max-width: 80vw;
}
.toast.error { border-color: rgba(248, 113, 113, 0.4); color: #ffd7d7; }
.toast.success { border-color: rgba(52, 211, 153, 0.4); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

#modal-root { position: fixed; inset: 0; z-index: 100; display: none; }
#modal-root.open { display: block; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(3, 6, 9, 0.66); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }
.modal {
  position: relative;
  margin: min(7vh, 60px) auto;
  width: min(620px, calc(100vw - 28px));
  max-height: calc(100vh - 80px);
  display: flex; flex-direction: column;
  border-radius: 20px;
  border: 1px solid var(--border-2);
  background: linear-gradient(180deg, #131c26, #0d141c);
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: modal-in 0.16s ease;
}
@keyframes modal-in { from { opacity: 0; transform: translateY(10px) scale(0.99); } }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 18px 20px 14px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 16px; font-weight: 650; }
.modal-head .icon-btn { margin-left: auto; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px calc(16px + var(--safe-b)); border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.7px; }
.field input, .field select, .field textarea {
  height: 40px; padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border-2);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(45, 212, 191, 0.1); }
.field .hint { font-size: 11.5px; color: var(--muted-2); }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1 1 0; }
.tabs { display: flex; gap: 6px; padding: 4px; background: var(--bg-soft); border: 1px solid var(--border); border-radius: 12px; margin-bottom: 16px; }
.tab { flex: 1 1 0; height: 34px; border: none; border-radius: 9px; background: transparent; color: var(--muted); font-size: 12.5px; font-weight: 650; }
.tab.active { background: var(--surface-2); color: var(--text); box-shadow: 0 1px 0 rgba(255, 255, 255, 0.04) inset; }
.section { border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; margin-bottom: 14px; background: rgba(16, 24, 33, 0.45); }
.section h4 { margin: 0 0 10px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 7px 0; }
.switch-row .label { font-size: 13px; font-weight: 550; }
.switch-row .desc { font-size: 11.5px; color: var(--muted-2); margin-top: 2px; }
.switch { position: relative; width: 42px; height: 24px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--surface-3); border: 1px solid var(--border-2); transition: 0.18s; }
.switch span::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: 0.18s; }
.switch input:checked + span { background: rgba(45, 212, 191, 0.25); border-color: rgba(45, 212, 191, 0.55); }
.switch input:checked + span::after { left: 21px; background: var(--accent); }
.code-box { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: var(--bg-soft); border: 1px solid var(--border); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.code-box span { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.room-display { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 22px; letter-spacing: 5px; color: var(--accent); }
.status-line { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); padding: 8px 0 0; }
.status-line .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted-2); }
.status-line.ok .dot { background: var(--green); }
.status-line.bad .dot { background: var(--red); }
.status-line.warn .dot { background: var(--amber); }
.list { display: flex; flex-direction: column; gap: 8px; }
.list-item { display: flex; align-items: center; gap: 12px; padding: 11px 13px; border-radius: 11px; border: 1px solid var(--border); background: var(--surface); }
.list-item .grow { flex: 1 1 auto; min-width: 0; }
.list-item .name { font-size: 13px; font-weight: 600; }
.list-item .sub { font-size: 11.5px; color: var(--muted-2); margin-top: 2px; word-break: break-all; }

/* connect / lock gate */
.gate { position: fixed; inset: 0; z-index: 150; display: flex; align-items: center; justify-content: center; padding: 24px; background: radial-gradient(800px 600px at 50% 0%, #10202a 0%, var(--bg) 60%); }
.gate-card { width: min(420px, 100%); border-radius: 22px; border: 1px solid var(--border-2); background: linear-gradient(180deg, #131c26, #0d141c); box-shadow: var(--shadow); padding: 28px 26px calc(26px + var(--safe-b)); }
.gate-logo { width: 52px; height: 52px; border-radius: 15px; display: grid; place-items: center; background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #04211e; margin-bottom: 16px; }
.gate-logo svg { width: 28px; height: 28px; }
.gate-card h1 { margin: 0 0 6px; font-size: 22px; }
.gate-card p { margin: 0 0 20px; color: var(--muted); font-size: 13px; }
.gate-card .field input { height: 46px; font-size: 16px; }
.gate-error { color: var(--red); font-size: 12.5px; margin-top: 4px; min-height: 17px; }
.gate .btn { width: 100%; height: 44px; }
.gate-foot { margin-top: 16px; font-size: 11.5px; color: var(--muted-2); text-align: center; }

.conn-banner {
  position: fixed; top: calc(var(--topbar) + 10px); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 9px;
  padding: 8px 14px; border-radius: 999px;
  background: rgba(19, 28, 38, 0.97); border: 1px solid var(--border-2);
  font-size: 12px; color: var(--muted); z-index: 90; box-shadow: var(--shadow);
}

/* QR setup */
.qr-wrap { display: flex; align-items: center; justify-content: center; padding: 14px; background: #fff; border-radius: 14px; }
.qr-wrap img { width: min(320px, 70vw); height: auto; display: block; image-rendering: pixelated; }

/* screenshot preview */
.shot-wrap { display: flex; align-items: center; justify-content: center; background: #04070a; border-radius: 14px; padding: 10px; }
.shot-wrap img { max-width: 100%; max-height: 62vh; border-radius: 8px; display: block; }

/* ---------- mobile ---------- */
@media (max-width: 560px) {
  .brand-text { display: none; }
  .top-actions { gap: 5px; }
  .pill { padding: 0 9px; }
  #topbar { padding: 0 10px; }
}

@media (max-width: 860px) {
  :root { --topbar: 56px; }
  #topbar { padding: 0 12px; gap: 8px; }
  .brand-text small { display: none; }
  .pill { height: 30px; padding: 0 10px; font-size: 11.5px; }
  #view-grid { padding: 14px; }
  .grid { grid-template-columns: 1fr; gap: 14px; }
  .card .thumb { max-height: 46vh; }
  .card-actions { opacity: 1; }
  .control-body { flex-direction: column; padding: 10px; gap: 10px; }
  #screen-wrap { flex: 1 1 auto; min-height: 0; }
  #toolbar {
    width: 100%;
    flex-direction: row;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 2px;
  }
  .tool-group {
    display: flex;
    flex-wrap: nowrap;
    gap: 4px;
    padding: 6px;
    flex: 0 0 auto;
  }
  .tool { min-width: 56px; min-height: 52px; padding: 4px 6px; }
  .tool svg { width: 18px; height: 18px; }
  .tool span { font-size: 9.5px; }
  .tool-selects { flex-direction: row; align-items: center; gap: 10px; }
  .mini-label { flex-direction: column; }
  .mini-label select { width: 108px; }
  .control-head { padding: 8px 10px; gap: 8px; }
  .control-title span { display: none; }
  .stat { padding: 3px 7px; }
  .modal { margin: 12px auto; max-height: calc(100vh - 24px); }
  .modal-head, .modal-body, .modal-foot { padding-left: 14px; padding-right: 14px; }
}
