:root {
  --bg: #0a0e14;
  --panel: rgba(18, 24, 36, 0.92);
  --border: #2a3548;
  --text: #e8edf5;
  --muted: #8a96a8;
  --accent: #4d9fff;
  --accent2: #2d6a9f;
  --danger: #e74c3c;
  --nav-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  /* Stream itself is exactly 1/3 of the viewport */
  --video-preview-h: 33.333dvh;
  --poi-chrome-h: 7.75rem; /* title + status + action buttons */
  --chat-sheet-h: 30vh;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom));
}

a { color: var(--accent); text-decoration: none; }

.boot-status {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--muted);
  font-size: 1rem;
  padding: 1.5rem;
  text-align: center;
}
.boot-status--err {
  color: #ffb4b4;
  background: #1a1010;
}

/* --- Bottom nav --- */
.bottomnav {
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding: 0 0.35rem var(--safe-bottom);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  align-items: center;
  gap: 0.25rem;
  background: #121a28;
  border-top: 1px solid var(--border);
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
}

.bottomnav .tab-btn {
  justify-self: stretch;
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  color: var(--muted);
}

.bottomnav a.tab-btn:hover,
.bottomnav a.tab-btn:focus {
  color: var(--text);
}

/* Admin still uses topnav */
.topnav {
  height: var(--nav-h);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 1rem;
  background: #121a28;
  border-bottom: 1px solid var(--border);
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 1000;
}

.topnav h1 { font-size: 1.1rem; margin-right: auto; letter-spacing: 0.04em; }

.tab-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--muted);
  padding: 0.45rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.95rem;
}

.tab-btn.active {
  color: var(--text);
  background: var(--accent2);
  border-color: #3d7ab8;
}

.tab-btn.admin-link {
  margin-left: 0.5rem;
  border-color: var(--border);
  color: var(--accent);
}

.tab-label { pointer-events: none; }

.view {
  display: none;
  min-height: calc(100vh - var(--nav-h) - var(--safe-bottom));
}
.view.active { display: block; }

#mapView {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: calc(var(--nav-h) + var(--safe-bottom));
  padding-top: 0;
  min-height: 0;
  z-index: 1;
  background: #dce6f0;
}
#mapView.active { display: block; }
#mapView:not(.active) { display: none !important; }
#map {
  width: 100%;
  height: 100%;
  min-height: 240px;
  z-index: 1;
  background: #dce6f0;
}
#map .leaflet-container {
  width: 100%;
  height: 100%;
  background: #dce6f0;
}

.map-status {
  position: absolute;
  top: calc(var(--safe-top) + 0.75rem);
  left: 50%;
  transform: translateX(-50%);
  z-index: 1200;
  max-width: min(520px, calc(100% - 2rem));
  padding: 0.65rem 1rem;
  border-radius: 10px;
  background: rgba(20, 35, 56, 0.92);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  text-align: center;
  pointer-events: none;
}
.map-status--err {
  background: rgba(42, 18, 18, 0.94);
  border-color: #8b3a3a;
  color: #ffb4b4;
}

/* Video sheet: stream = exactly 1/3 screen; chrome sits under it */
#poiPanel {
  position: absolute;
  top: auto;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: calc(var(--video-preview-h) + var(--poi-chrome-h));
  min-height: calc(var(--video-preview-h) + 140px);
  max-height: calc(var(--video-preview-h) + var(--poi-chrome-h) + 1.5rem);
  background: var(--panel);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  border-bottom: none;
  border-left: none;
  padding: 0.45rem 0.75rem 0.55rem;
  display: none;
  flex-direction: column;
  z-index: 900;
  overflow: hidden;
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.35);
  transition: height 0.22s ease, max-height 0.22s ease;
}

#poiPanel.open { display: flex; }

/* Chat open: sheet grows upward over the map; video stays 1/3 */
#poiPanel.open.chat-open {
  height: calc(var(--video-preview-h) + var(--poi-chrome-h) + var(--chat-sheet-h));
  max-height: min(88dvh, calc(var(--video-preview-h) + var(--poi-chrome-h) + var(--chat-sheet-h)));
}

.poi-panel-meta {
  flex: 0 0 auto;
  padding-right: 2rem;
  margin-bottom: 0.25rem;
}

#poiPanel h2 {
  font-size: 0.9rem;
  margin-bottom: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#poiPanel .addr {
  font-size: 0.7rem;
  color: var(--muted);
  margin-bottom: 0.1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#poiPanel .comment { display: none; }

.preview-wrap {
  position: relative;
  width: 100%;
  flex: 0 0 var(--video-preview-h);
  height: var(--video-preview-h);
  min-height: var(--video-preview-h);
  max-height: var(--video-preview-h);
  aspect-ratio: auto;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 0.3rem;
}

.preview-wrap video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #000;
}

/* When showing raw device camera (no mask canvas), keep video visible */
.preview-wrap video:not(.live-source-hidden) {
  position: relative;
  z-index: 1;
}

video.live-source-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0 !important;
  pointer-events: none;
  z-index: -1;
}

.privacy-composite .mask-overlay-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  background: #000;
}

.preview-wrap .mask-overlay-canvas,
.video-box .mask-overlay-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.poi-panel-footer { flex: 0 0 auto; }

.panel-actions { display: flex; flex-direction: column; gap: 0.35rem; }
.panel-actions .primary { padding: 0.45rem 0.75rem; font-size: 0.85rem; }

button.primary {
  background: var(--accent);
  color: #0a0e14;
  border: none;
  padding: 0.65rem 1rem;
  border-radius: 8px;
  font-weight: 700;
  cursor: pointer;
}

button.secondary {
  background: #2a3548;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.55rem 1rem;
  border-radius: 8px;
  cursor: pointer;
}

button.close-panel {
  position: absolute;
  top: 0.35rem;
  right: 0.45rem;
  z-index: 2;
  background: rgba(0, 0, 0, 0.45);
  border: none;
  color: var(--text);
  cursor: pointer;
  font-size: 1.25rem;
  width: 36px;
  height: 36px;
  border-radius: 18px;
  margin-bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#accountView {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: calc(var(--nav-h) + var(--safe-bottom));
  z-index: 2;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  padding-top: calc(var(--safe-top) + 1rem);
  padding-left: 1rem;
  padding-right: 1rem;
  padding-bottom: 2rem;
  max-width: none;
}
#accountView.active { display: block; }
#accountView:not(.active) { display: none !important; }
#accountView > * {
  max-width: 420px;
  margin-left: auto;
  margin-right: auto;
}

.account-extra-links {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 1.25rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
}

.account-extra-links a {
  display: block;
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #121a28;
  font-weight: 600;
}

@media (min-width: 900px) {
  #poiPanel {
    top: 0;
    bottom: 0;
    right: 0;
    left: auto;
    width: min(360px, 32vw);
    height: 100%;
    min-height: 0;
    max-height: none;
    border-top: none;
    border-bottom: none;
    border-left: 1px solid var(--border);
    padding: 1rem;
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.25);
  }
  #poiPanel.open.chat-open {
    height: 100%;
    max-height: none;
  }
  #poiPanel .comment { display: block; font-size: 0.85rem; margin-bottom: 0.75rem; line-height: 1.4; }
  .preview-wrap { flex: 0 0 auto; aspect-ratio: 16/9; height: auto; max-height: none; min-height: 0; }
  #poiPanel.chat-open .poi-chat {
    height: auto;
    flex: 1 1 auto;
  }
  button.close-panel {
    position: static;
    align-self: flex-end;
    background: transparent;
    color: var(--muted);
    margin-bottom: 0.25rem;
  }
  .poi-panel-meta { padding-right: 0; }
  .panel-actions { margin-top: auto; }
  .panel-actions .primary { font-size: 1rem; padding: 0.65rem 1rem; }
}


.auth-tabs {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.25rem;
}

.auth-tabs button {
  flex: 1;
  padding: 0.6rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #141c2a;
  color: var(--muted);
  cursor: pointer;
  font-weight: 600;
}

.auth-tabs button.active {
  background: var(--accent2);
  color: #fff;
  border-color: #3d7ab8;
}

form input {
  width: 100%;
  padding: 0.65rem;
  margin-bottom: 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #0f1419;
  color: var(--text);
}

.msg { font-size: 0.85rem; margin-top: 0.75rem; color: var(--muted); white-space: pre-wrap; }
.msg.error { color: #ff8a80; }
.msg.ok { color: #81c784; }

.user-card {
  background: #141c2a;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.25rem;
}

.user-card p { margin-bottom: 0.5rem; font-size: 0.9rem; }
.profile-form label { display: block; font-size: 0.85rem; margin: 0.65rem 0 0.25rem; opacity: 0.9; }
.profile-form input,
.profile-form select { width: 100%; max-width: 360px; }

/* --- Admin --- */
.admin-page {
  padding-top: var(--nav-h);
  min-height: 100vh;
}

.admin-tabs {
  display: flex;
  gap: 0.35rem;
  padding: 0 1rem;
  background: #121a28;
  border-bottom: 1px solid var(--border);
  height: 48px;
  align-items: center;
  position: sticky;
  top: var(--nav-h);
  z-index: 950;
}

.admin-tabs .tab-btn {
  margin: 0;
  font-size: 0.9rem;
}

.admin-tab-panel {
  display: none;
  height: calc(100vh - var(--nav-h) - 48px);
  overflow: hidden;
}

.admin-tab-panel.active {
  display: block;
}

.admin-layout {
  display: grid;
  grid-template-columns: 1fr 380px;
  height: 100%;
  min-height: 0;
}

.admin-map-wrap {
  position: relative;
  height: 100%;
  min-height: 0;
}

.admin-map {
  height: 100%;
  min-height: 320px;
  width: 100%;
  background: #1a2332;
  z-index: 0;
}

.admin-map-wrap .map-hint {
  position: absolute;
  top: 0.75rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 500;
  background: rgba(18, 26, 40, 0.92);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.85rem;
  font-size: 0.8rem;
  color: var(--muted);
  pointer-events: none;
  white-space: nowrap;
}

.admin-map .leaflet-container {
  height: 100% !important;
  width: 100% !important;
  background: #1a2332;
}

.admin-side.is-readonly {
  opacity: 0.55;
}

.admin-side.is-readonly input:not([type="file"]),
.admin-side.is-readonly textarea,
.admin-side.is-readonly button:not(#btnAddPoi) {
  pointer-events: none;
}

.admin-tab-panel--scroll {
  height: calc(100vh - var(--nav-h) - 48px);
  overflow-y: auto;
  padding: 1rem 2rem;
  max-width: 560px;
  margin: 0 auto;
}

.admin-layout-quality {
  padding: 1rem 2rem;
  max-width: 900px;
  margin: 0 auto;
}

.admin-side {
  border-left: 1px solid var(--border);
  background: #121a28;
  overflow-y: auto;
  padding: 1rem;
}

.admin-section { margin-bottom: 1.5rem; }
.admin-section h3 {
  font-size: 0.9rem;
  color: var(--accent);
  margin-bottom: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.admin-side input, .admin-side select, .admin-side textarea {
  width: 100%;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #0f1419;
  color: var(--text);
  font-size: 0.85rem;
}

.admin-side textarea { min-height: 60px; resize: vertical; }

.row-btns { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.35rem; }

table.users {
  width: 100%;
  font-size: 0.75rem;
  border-collapse: collapse;
}

table.users th, table.users td {
  border-bottom: 1px solid var(--border);
  padding: 0.35rem 0.25rem;
  text-align: left;
}

table.users tr { cursor: pointer; }
table.users tr.selected { background: #1e2d44; }

.mask-preview {
  width: 100%;
  max-height: 120px;
  object-fit: contain;
  background: #000;
  border-radius: 8px;
  margin: 0.5rem 0;
}

.stream-sample {
  position: relative;
  width: 100%;
  aspect-ratio: 16/9;
  background: #111;
  border-radius: 8px;
  overflow: hidden;
  margin: 0.5rem 0;
}

.stream-sample video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.mask-overlay-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.admin-section.preview-inactive .stream-sample {
  opacity: 0.55;
}

.admin-section.preview-inactive input,
.admin-section.preview-inactive button {
  opacity: 1;
  pointer-events: auto;
}

.admin-section .hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

/* --- Modal --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 1rem;
}

.modal-backdrop.open { display: flex; }

.modal {
  background: #141c2a;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.25rem;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

.modal h2 {
  font-size: 1.1rem;
  margin-bottom: 1rem;
  color: var(--accent);
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 0.75rem;
}

.log { font-size: 0.75rem; color: var(--muted); white-space: pre-wrap; max-height: 100px; overflow-y: auto; }

.quality-score {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--accent);
  margin: 0.5rem 0;
}

.camera-slot {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.65rem;
  margin-bottom: 0.5rem;
  background: #0f1419;
}

.camera-slot label {
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-top: 0.35rem;
}

.camera-slot .slot-remove {
  margin-top: 0.5rem;
  width: 100%;
  font-size: 0.8rem;
  color: #ff8a80;
  background: transparent;
  border: 1px solid #5a3030;
  border-radius: 6px;
  padding: 0.35rem;
  cursor: pointer;
}

.env-badge {
  font-size: 0.7rem;
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
  background: #2a3548;
  color: var(--muted);
  margin-left: 0.5rem;
}

/* Map FAB + POI chat */
.map-fab {
  position: absolute;
  right: 12px;
  bottom: calc(var(--bottom-nav-h, 56px) + 16px);
  z-index: 850;
  border: 0;
  border-radius: 999px;
  padding: 0.65rem 1rem;
  background: #4d9fff;
  color: #0a0e14;
  font-weight: 700;
  font-size: 0.9rem;
  box-shadow: 0 6px 18px rgba(0,0,0,.35);
}
.poi-chat {
  display: none;
  margin-top: 0.45rem;
  border-top: 1px solid var(--border);
  padding-top: 0.45rem;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  height: calc(var(--chat-sheet-h) - 0.5rem);
}
#poiPanel.chat-open .poi-chat {
  display: flex;
}
.poi-chat[hidden] {
  display: none !important;
}
.poi-chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
  font-size: 0.78rem;
  line-height: 1.35;
}
.poi-chat-msg {
  padding: 0.2rem 0;
  border-bottom: 1px solid rgba(255,255,255,.04);
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  align-items: baseline;
}
.poi-chat-msg strong { color: #9ec5ff; }
.poi-chat-del, .poi-chat-mute {
  margin-left: auto;
  border: 0;
  background: transparent;
  color: #ff8a80;
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0 0.2rem;
}
.poi-chat-form {
  display: flex;
  gap: 0.35rem;
  margin-top: 0.35rem;
}
.poi-chat-form input {
  flex: 1;
  min-width: 0;
  font-size: 0.85rem;
  padding: 0.4rem 0.5rem;
}
.poi-chat-form button {
  width: auto;
  padding: 0.4rem 0.65rem;
}

.check-row {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  font-size: 0.88rem;
  color: var(--text);
  margin: 0.75rem 0;
  line-height: 1.35;
}
.check-row input { width: auto; margin-top: 0.15rem; }

.event-badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  margin: 0.25rem 0 0.35rem;
}
.event-badge.open { background: #163524; color: #8dffb8; }
.event-badge.closed { background: #3a1c1c; color: #ffb4b4; }

.poi-pin-wrap { background: transparent; border: 0; }
.poi-pin {
  min-width: 92px;
  max-width: 140px;
  padding: 4px 8px 5px;
  border-radius: 10px;
  color: #fff;
  font-size: 11px;
  line-height: 1.2;
  box-shadow: 0 4px 12px rgba(0,0,0,.35);
}
.poi-pin span { display: block; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.poi-pin small { opacity: 0.9; font-size: 10px; }
.poi-pin--open { background: #1f6b45; }
.poi-pin--closed { background: #8a2f2f; }

.kiosk-cta {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 44px;
  border-radius: 10px;
  font-weight: 700;
  text-decoration: none;
  background: var(--accent);
  color: #0a0e14;
  margin-top: 0.5rem;
}

.warn-note {
  background: #3a2410;
  border: 1px solid #7a4a16;
  color: #ffd9a0;
  padding: 0.75rem 0.85rem;
  border-radius: 10px;
  font-size: 0.88rem;
  line-height: 1.4;
  margin-bottom: 0.75rem;
}

.modal-wide { max-width: 480px; }

.selfie-stage {
  position: relative;
  width: 100%;
  aspect-ratio: 3/4;
  max-height: 42vh;
  background: #0b1220;
  border-radius: 12px;
  overflow: hidden;
  margin: 0.5rem 0;
  border: 1px solid var(--border);
}
.selfie-stage video, .selfie-stage img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.guest-streams, .host-requests {
  margin-top: 0.65rem;
  max-height: 28vh;
  overflow-y: auto;
}
.host-request-card {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 0.55rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}
.host-request-card img {
  width: 64px;
  height: 80px;
  object-fit: cover;
  border-radius: 8px;
  background: #0b1220;
}
.pass-code {
  font-family: ui-monospace, Menlo, monospace;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #8dffb8;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
  transform: translateX(-50%);
  z-index: 2500;
  max-width: min(92vw, 420px);
  background: #1a2740;
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 0.75rem 1rem;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.35;
  box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.toast[hidden] { display: none !important; }

