:root {
  --bg: #0b0f14;
  --panel: #121821;
  --panel-2: #0e141c;
  --border: #1f2a37;
  --text: #e6edf3;
  --muted: #8a97a6;
  --accent: #f5a623;
  --accent-2: #38d39f;
  --danger: #ff5d5d;
  --radius: 12px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { display: flex; flex-direction: column; }
h2 { margin: 0; font-size: 14px; font-weight: 600; letter-spacing: 0.2px; }

.muted { color: var(--muted); }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, "SF Mono", Menlo, monospace; }

.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #ffd27a, var(--accent));
  box-shadow: 0 0 10px rgba(245, 166, 35, 0.6); display: inline-block;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; }

.chip {
  display: inline-flex; align-items: center; gap: 7px; padding: 4px 10px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--border); font-size: 13px; color: var(--muted);
}
.chip-status .status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); transition: background 0.3s; }
.chip-status[data-state="connected"] .status-dot { background: var(--accent-2); box-shadow: 0 0 8px var(--accent-2); }
.chip-status[data-state="connecting"] .status-dot { background: var(--accent); animation: pulse 1.2s infinite; }
.chip-status[data-state="disconnected"] .status-dot { background: var(--danger); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.btn-ghost { background: transparent; color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.btn-ghost:hover { border-color: var(--muted); }

/* ---------- Layout ---------- */
.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 380px; gap: 12px; padding: 12px; }
.panel { position: relative; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; min-height: 0; }
.side { display: grid; grid-template-rows: minmax(140px, 0.9fr) 1.4fr; gap: 12px; min-height: 0; }
.panel-head { padding: 10px 14px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }

/* ---------- Map ---------- */
.map-panel { display: flex; }
#map { position: absolute; inset: 0; }
.map-btn {
  position: absolute; top: 12px; right: 12px; z-index: 5; width: 38px; height: 38px; border-radius: 9px;
  cursor: pointer; background: var(--panel); color: var(--text); border: 1px solid var(--border);
  font-size: 20px; line-height: 1; box-shadow: var(--shadow);
}
.map-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- Device list ---------- */
.list-panel { display: flex; flex-direction: column; }
.device-list { list-style: none; margin: 0; padding: 8px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.device-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 9px;
  border: 1px solid transparent; cursor: pointer; background: var(--panel-2);
}
.device-item:hover { border-color: var(--border); }
.device-item.selected { border-color: var(--accent); background: rgba(245, 166, 35, 0.08); }
.device-item .swatch { width: 12px; height: 12px; border-radius: 50%; flex: none; box-shadow: 0 0 8px currentColor; }
.device-item .dmeta { display: flex; flex-direction: column; min-width: 0; gap: 1px; flex: 1; }
.device-item .dname { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-item .dsub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.device-item .dstate { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); flex: none; box-shadow: 0 0 6px var(--accent-2); }
.device-item.offline .dstate { background: var(--muted); box-shadow: none; }
.device-item.offline { opacity: 0.6; }

/* ---------- Detail / video ---------- */
.detail-panel { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; }
.detail-head h2 { font-size: 15px; }
.video-frame { position: relative; width: 100%; height: 100%; min-height: 0; background: #000; }
#video { width: 100%; height: 100%; object-fit: contain; display: block; background: #000; }
.video-placeholder {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  background: repeating-linear-gradient(45deg, #0a0e13, #0a0e13 12px, #0c1117 12px, #0c1117 24px);
  color: var(--muted); text-align: center; padding: 16px;
}
.video-placeholder .ph-icon { font-size: 40px; opacity: 0.6; }
.video-placeholder[hidden] { display: none; }
.video-osd { position: absolute; top: 10px; left: 10px; display: flex; gap: 8px; pointer-events: none; }
.osd-item {
  display: inline-flex; align-items: baseline; gap: 6px; background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.08); padding: 3px 8px; border-radius: 7px;
  font-variant-numeric: tabular-nums; backdrop-filter: blur(3px); font-size: 13px;
}
.osd-key { color: var(--accent); font-size: 11px; font-weight: 700; letter-spacing: 0.5px; }

.readouts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.readout { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.readout-key { font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.readout-val { font-size: 16px; font-variant-numeric: tabular-nums; display: flex; gap: 6px; align-items: baseline; }
.readout-val.mono { font-size: 13px; }

/* ---------- Map markers ---------- */
.dev-dot {
  width: 15px; height: 15px; border-radius: 50%; background: var(--accent); border: 2px solid #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.6); cursor: pointer; transition: transform 0.15s;
}
.dev-dot:hover { transform: scale(1.2); }
.dev-dot.selected { outline: 3px solid rgba(255, 255, 255, 0.85); outline-offset: 2px; }
.dev-dot.offline { filter: grayscale(1) brightness(0.7); border-color: #9aa5b1; }
.range-chip {
  transform: translateY(-50%); background: rgba(11, 15, 20, 0.9); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 7px; padding: 2px 8px; font-size: 13px; font-weight: 700;
  font-variant-numeric: tabular-nums; white-space: nowrap; box-shadow: var(--shadow);
}

/* ---------- Auth ---------- */
.auth-body { min-height: 100%; display: grid; place-items: center; padding: 24px; background: radial-gradient(1200px 600px at 50% -10%, rgba(245, 166, 35, 0.08), transparent), var(--bg); }
.auth-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.auth-brand { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.auth-card h1 { margin: 14px 0 20px; font-size: 22px; }
.auth-card label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.auth-card input { width: 100%; padding: 12px; border-radius: 9px; background: var(--panel-2); border: 1px solid var(--border); color: var(--text); font-size: 16px; }
.auth-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(245, 166, 35, 0.15); }
.auth-card button { width: 100%; margin-top: 20px; padding: 12px; border: none; border-radius: 9px; background: var(--accent); color: #1a1204; font-size: 15px; font-weight: 700; cursor: pointer; }
.auth-card button:hover { filter: brightness(1.05); }
.auth-card button:disabled { opacity: 0.6; cursor: default; }
.form-error { margin-top: 14px; padding: 9px 12px; border-radius: 8px; font-size: 13px; background: rgba(255, 93, 93, 0.12); border: 1px solid rgba(255, 93, 93, 0.4); color: var(--danger); }
.form-error[hidden] { display: none; }

/* ---------- Buttons ---------- */
.btn-primary { background: var(--accent); color: #1a1204; border: none; border-radius: 8px; padding: 9px 16px; font-weight: 700; cursor: pointer; font-size: 14px; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-mini { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 7px; padding: 5px 9px; font-size: 12px; cursor: pointer; }
.btn-mini:hover { border-color: var(--muted); }
.btn-mini.danger { color: var(--danger); border-color: rgba(255, 93, 93, 0.4); }
.btn-mini:disabled { opacity: 0.4; cursor: default; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 20px; background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(2px); }
.modal[hidden] { display: none; }
.modal-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 22px; box-shadow: var(--shadow); }
.modal-card h2 { font-size: 18px; margin-bottom: 8px; }
.modal-card label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.modal-card input { width: 100%; padding: 11px 12px; border-radius: 9px; background: var(--panel-2); border: 1px solid var(--border); color: var(--text); font-size: 16px; }
.modal-card input:focus { outline: none; border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* ---------- Admin ---------- */
.admin-body { display: block; overflow-y: auto; }
.admin-wrap { max-width: 960px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.admin-section { padding-bottom: 12px; }
.create-form { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 14px; align-items: center; }
.create-form input, .create-form select { padding: 9px 11px; border-radius: 8px; background: var(--panel-2); border: 1px solid var(--border); color: var(--text); font-size: 14px; }
.create-form input { flex: 1; min-width: 140px; }
.inline-check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.create-form + .form-error { margin: 0 14px 12px; }

.table-scroll { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.admin-table th { text-align: left; padding: 10px 14px; color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px; border-bottom: 1px solid var(--border); }
.admin-table td { padding: 9px 14px; border-bottom: 1px solid var(--panel-2); vertical-align: middle; }
.admin-table select { padding: 5px 8px; border-radius: 7px; background: var(--panel-2); border: 1px solid var(--border); color: var(--text); }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); }
.badge.ok { color: var(--accent-2); border-color: rgba(56, 211, 159, 0.4); }
.badge.off { color: var(--muted); }
.badge.warn { color: var(--accent); border-color: rgba(245, 166, 35, 0.4); }

/* ---------- Provisioning QR ---------- */
.prov-qr { display: flex; justify-content: center; margin: 14px 0 4px; }
.prov-qr svg { width: 240px; height: 240px; background: #fff; padding: 10px; border-radius: 10px; }
.modal-card input[readonly] { color: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: 13px; }

/* ---------- Sender PWA ---------- */
.sender-body { min-height: 100%; display: grid; place-items: start center; padding: 20px; background: radial-gradient(1000px 500px at 50% -10%, rgba(245, 166, 35, 0.08), transparent), var(--bg); }
.sender-card { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 22px; box-shadow: var(--shadow); margin-top: 6vh; }
.sender-head { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.sender-error { margin-top: 16px; padding: 14px; border-radius: 10px; background: rgba(255, 93, 93, 0.1); border: 1px solid rgba(255, 93, 93, 0.35); color: var(--text); font-size: 14px; }
.sender-device { display: flex; flex-direction: column; gap: 2px; margin: 16px 0 12px; }
.sender-device strong { font-size: 18px; }
.sender-status { display: inline-flex; align-items: center; gap: 9px; padding: 8px 14px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--border); font-size: 14px; }
.sender-status .status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.sender-status[data-state="connecting"] .status-dot { background: var(--accent); animation: pulse 1.2s infinite; }
.sender-status[data-state="sending"] .status-dot { background: var(--accent-2); box-shadow: 0 0 10px var(--accent-2); }
.sender-status[data-state="error"] .status-dot { background: var(--danger); }
.sender-readouts { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 20px 0; }
.sender-btn { width: 100%; padding: 16px; font-size: 16px; }
.cam-btn { margin-top: 10px; }
.cam-preview { width: 100%; margin-top: 12px; border-radius: 12px; background: #000; aspect-ratio: 16 / 9; object-fit: cover; }
.sender-note { font-size: 12px; margin-top: 12px; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  /* Let the two side panels drop into the main grid so the page stacks: map, list, detail. */
  .layout { grid-template-columns: 1fr; grid-template-rows: 50vh auto auto; padding: 8px; gap: 8px; }
  .side { display: contents; }
  .device-list { max-height: 34vh; }
  .detail-panel { grid-template-rows: auto minmax(220px, 42vh) auto; }
  .topbar { flex-wrap: wrap; row-gap: 8px; padding: 8px 12px; }
  .topbar strong { font-size: 15px; }
  #user { display: none; }
  .readouts { grid-template-columns: repeat(2, 1fr); }
}

/* Scope control panel (palette / zoom / range) */
.scope-controls { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.ctl-row { display: flex; align-items: center; gap: 10px; }
.ctl-title { flex: 0 0 62px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.ctl-body { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.ctl-btn {
  padding: 5px 10px; font-size: 13px; cursor: pointer;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.ctl-btn:hover { border-color: var(--accent); }
.ctl-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.ctl-btn:disabled { opacity: .5; cursor: default; }
.ctl-zoom { width: 160px; accent-color: var(--accent); }
.ctl-zoom-label { font-variant-numeric: tabular-nums; min-width: 40px; color: var(--text); }
.ctl-range-msg { font-size: 12px; color: var(--muted); }
