:root {
  --bg: #0d0d0f;
  --bg2: #17171c;
  --bg3: #1f1f27;
  --border: #2a2a38;
  --accent: #7c6aff;
  --accent2: #5b4de0;
  --text: #e8e8f0;
  --text2: #9090a8;
  --green: #4caf7d;
  --red: #e05555;
  --radius: 12px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ── Login ── */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px;
  width: 360px;
}

.login-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
}

.login-logo svg { width: 32px; height: 32px; }

.login-card h2 { font-size: 18px; margin-bottom: 24px; color: var(--text2); }

/* ── Nav ── */
#navbar {
  display: flex;
  align-items: center;
  padding: 0 24px;
  height: 56px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  gap: 24px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
  font-weight: 700;
  color: var(--accent);
}
.nav-brand svg { width: 28px; height: 28px; }

.nav-tabs { display: flex; gap: 4px; }
.nav-tab {
  background: none;
  border: none;
  color: var(--text2);
  padding: 6px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all .15s;
}
.nav-tab:hover { background: var(--bg3); color: var(--text); }
.nav-tab.active { background: var(--accent); color: #fff; }

.nav-user { margin-left: auto; display: flex; align-items: center; gap: 12px; }
#navUsername { font-size: 13px; color: var(--text2); }

/* ── Views ── */
.view { display: none; padding: 32px 24px; }
.view.active { display: block; }

/* ── Host Grid ── */
.host-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 20px;
  max-width: 1100px;
}

.host-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  cursor: pointer;
  transition: all .15s;
  position: relative;
  overflow: hidden;
}

.host-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 8px 24px rgba(124,106,255,.15); }

.host-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  opacity: 0;
  transition: opacity .15s;
}
.host-card:hover::before { opacity: 1; }

.host-icon {
  width: 48px; height: 48px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  margin-bottom: 16px;
}

.host-name { font-size: 16px; font-weight: 600; margin-bottom: 4px; }
.host-addr { font-size: 12px; color: var(--text2); }

.host-actions { margin-top: 20px; display: flex; gap: 8px; }

/* ── Stream Overlay ── */
.stream-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.stream-overlay.hidden { display: none; }

.stream-container { position: relative; width: 100%; height: 100%; }

#streamVideo {
  width: 100%; height: 100%;
  object-fit: contain;
  background: #000;
}

.stream-debug {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  background: rgba(0,0,0,.95);
  color: #0f0;
  font-family: monospace;
  font-size: 11px;
  padding: 8px 12px 8px 12px;
  max-height: 220px;
  overflow-y: auto;
  display: none;
  white-space: pre-wrap;
  word-break: break-all;
}
.stream-debug.visible { display: block; }

.debug-header {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  display: none;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  padding: 4px 8px;
  background: rgba(0,0,0,.5);
  z-index: 10;
}
.debug-header.visible { display: flex; }

.stream-controls {
  position: absolute;
  top: 12px; left: 12px; right: 12px;
  display: flex; gap: 8px;
  align-items: center;
  background: rgba(13,13,15,.82);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 10px;
  padding: 8px 12px;
  opacity: 0;
  transition: opacity .2s;
}
.stream-container:hover .stream-controls { opacity: 1; }
.stream-controls { z-index: 30; }
.stream-controls select { background: var(--bg3); color: var(--text); border: 1px solid var(--border); }
.stream-controls select option { background: var(--bg2); color: var(--text); }
/* Controls always visible when pointer is NOT locked */
.stream-container.unlocked .stream-controls { opacity: 1; }

.stream-status {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(0,0,0,.8);
}
.stream-status .spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Admin ── */
.admin-container { display: flex; gap: 32px; max-width: 1100px; }
.admin-section { flex: 1; }
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.section-header h2 { font-size: 17px; }

.admin-list { display: flex; flex-direction: column; gap: 8px; }

.admin-item {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.admin-item-info h3 { font-size: 14px; margin-bottom: 2px; }
.admin-item-info p { font-size: 12px; color: var(--text2); }

.admin-item-actions { display: flex; gap: 8px; }

/* ── Forms ── */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; color: var(--text2); margin-bottom: 6px; }
.field input, .field select {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color .15s;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.field select option { background: var(--bg3); }

/* ── Buttons ── */
.btn {
  border: none;
  border-radius: 8px;
  padding: 9px 18px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent2); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { background: #c44; }
.btn-ghost { background: var(--bg3); border: 1px solid var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Modal ── */
.modal { position: fixed; inset: 0; z-index: 500; display: flex; align-items: center; justify-content: center; }
.modal.hidden { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.6); backdrop-filter: blur(4px); }
.modal-card {
  position: relative;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 400px;
  z-index: 1;
}
.modal-card h3 { font-size: 17px; margin-bottom: 24px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }

/* ── Misc ── */
.error { color: var(--red); font-size: 13px; margin-bottom: 12px; }
.hidden { display: none !important; }
.loading { color: var(--text2); font-size: 14px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; }
.badge-admin { background: rgba(124,106,255,.2); color: var(--accent); }
.badge-user { background: rgba(144,144,168,.15); color: var(--text2); }

/* Chrome only hint */
.chrome-hint {
  background: rgba(255,200,50,.08);
  border: 1px solid rgba(255,200,50,.2);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 12px;
  color: #c8a040;
  margin-bottom: 24px;
}

.lock-indicator {
  position: absolute;
  top: 12px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.75);
  color: #fff; font-size: 13px; padding: 6px 14px;
  border-radius: 8px; pointer-events: none;
  z-index: 20; white-space: nowrap;
}
.lock-indicator kbd {
  background: rgba(255,255,255,.15); border-radius: 4px;
  padding: 1px 5px; font-family: inherit;
}
.lock-hint {
  position: absolute;
  inset: 0; display: flex; align-items: center; justify-content: center;
  color: rgba(255,255,255,.7); font-size: 18px;
  pointer-events: none; z-index: 20;
  background: rgba(0,0,0,.35);
  text-shadow: 0 1px 4px rgba(0,0,0,.8);
}

.ctrl-label {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  color: rgba(255,255,255,.75);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.stats-overlay {
  position: absolute;
  top: 60px; left: 12px;
  background: rgba(13,13,15,.88);
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 10px;
  padding: 0;
  backdrop-filter: blur(8px);
  z-index: 25;
  min-width: 220px;
  font-size: 12px;
  pointer-events: auto;
  user-select: none;
}
.stats-overlay.hidden { display: none; }
.stats-handle {
  display: flex; align-items: center;
  padding: 6px 14px;
  color: var(--text2);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .5px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  cursor: move;
}
.stats-handle:hover { color: var(--text); }
.stats-row {
  display: flex; justify-content: space-between; gap: 16px;
  padding: 4px 14px;
  border-bottom: 1px solid rgba(255,255,255,.05);
}
.stats-row:last-child { border-bottom: none; padding-bottom: 8px; }
.stats-label { color: var(--text2); }
.stats-val { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 500; text-decoration: underline dotted rgba(255,255,255,.3); }
.stats-clickable { cursor: pointer; }
.stats-clickable:hover .stats-label { color: var(--accent); }
.stats-clickable .stats-val { text-decoration: underline dotted; }

/* ── Mobile / Touch (Samsung S24 & similar) ── */
@media (hover: none), (max-width: 768px) {
  /* Nav: compact on mobile */
  #navbar { padding: 0 12px; height: 48px; }
  .nav-brand span { display: none; }
  .nav-user { gap: 4px; }
  #navUsername { display: none; }
  .btn { padding: 8px 12px; }

  /* Host grid: single column */
  .host-grid { padding: 12px !important; gap: 10px !important; }

  /* Stream controls: always visible, scrollable */
  .stream-controls {
    opacity: 1 !important;
    pointer-events: auto !important;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 10px;
    max-height: 120px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Larger tap targets */
  .stream-controls .btn,
  .stream-controls .ctrl-label { min-height: 36px; font-size: 13px; }

  /* Lock hint: show on mobile (touch instructions) */
  .touch-mode #lockHint { display: flex !important; }
  .touch-mode #lockIndicator { display: none !important; }

  /* Modal: full-width on mobile */
  .modal-card { width: calc(100vw - 24px); max-width: 400px; padding: 20px; }

  /* Login card: full-width */
  .login-card { width: calc(100vw - 32px); padding: 24px; }
}

/* Touch-visible: controls show when touch detected */
.stream-controls.touch-visible {
  opacity: 1 !important;
  pointer-events: auto !important;
}

/* Safe area padding for notch/cutout (e.g. Samsung S24) */
@supports (padding: env(safe-area-inset-top)) {
  #navbar { padding-top: env(safe-area-inset-top); }
  .stream-controls { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
}
