:root {
  --bg-dark: #0f172a;
  --bg-card: #1e293b;
  --bg-hover: #334155;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --accent: #10b981;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --danger: #ef4444;
  --border: #334155;
  --radius: 12px;
  --shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
  background-color: var(--bg-dark);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: auto;
}

.app-container {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  padding: 16px;
  gap: 16px;
}

/* Navbar */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-card);
  padding: 12px 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  flex-wrap: nowrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.brand-text h1 {
  font-size: 1.1rem;
  font-weight: 700;
  background: linear-gradient(135deg, #818cf8, #c084fc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}

.subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.status-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.05);
  font-size: 0.82rem;
  font-weight: 500;
  border: 1px solid var(--border);
  white-space: nowrap;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--text-muted);
  transition: background-color 0.3s;
}

.status-badge.connected .status-dot {
  background-color: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}

.status-badge.disconnected .status-dot {
  background-color: var(--danger);
}

/* Main Content */
.main-content {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 16px;
  flex: 1;
  min-height: 0;
}

/* Screen Panel */
.screen-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 16px;
  gap: 16px;
}

.screen-wrapper {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  width: 100%;
  max-height: calc(100vh - 180px);
  background: #000;
  border-radius: var(--radius);
  border: 2px solid var(--border);
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6);
  touch-action: none;
}

video#screenVideo {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  z-index: 1;
}

canvas#screenCanvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  z-index: 2;
}

.screen-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: rgba(15, 23, 42, 0.95);
  gap: 16px;
  text-align: center;
  padding: 24px;
  z-index: 5;
}

.screen-placeholder.hidden {
  display: none;
}

/* Asleep: the video element would happily keep showing the last decoded frame,
   so the placeholder covers it and the spinner is hidden (nothing is loading). */
.screen-placeholder.asleep .loader-spinner {
  display: none;
}

.screen-wrapper.asleep {
  cursor: not-allowed;
}

.screen-wrapper.nudge {
  animation: screen-nudge 0.32s ease-in-out;
}

@keyframes screen-nudge {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}

.loader-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s infinite linear;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Instant Touch Ripple Effect on Canvas */
.tap-ripple {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(99, 102, 241, 0.5);
  border: 2px solid #818cf8;
  transform: translate(-50%, -50%) scale(0.3);
  animation: rippleAnim 0.4s ease-out forwards;
  pointer-events: none;
  z-index: 10;
}

@keyframes rippleAnim {
  0% {
    transform: translate(-50%, -50%) scale(0.3);
    opacity: 1;
  }
  100% {
    transform: translate(-50%, -50%) scale(2.2);
    opacity: 0;
  }
}

/* Control Bar */
.control-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  background: rgba(15, 23, 42, 0.6);
  padding: 10px 16px;
  border-radius: 30px;
  border: 1px solid var(--border);
  touch-action: auto;
}

.ctrl-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg-card);
  color: var(--text-main);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.2s;
  white-space: nowrap;
}

.ctrl-btn:hover {
  background: var(--bg-hover);
  border-color: var(--primary);
  transform: translateY(-2px);
}

.ctrl-btn:active {
  transform: translateY(0);
}

/* POWER button while the phone is really suspended (stream stopped, input
   dropped). It must be obvious at a glance that the button is now a "wake"
   button — the original complaint was that pressing it changed nothing visible. */
.ctrl-btn.sleeping {
  border-color: var(--danger);
  color: var(--danger);
}

.ctrl-btn.sleeping:hover {
  border-color: var(--danger);
  background: rgba(239, 68, 68, 0.12);
}

/* A suspend/wake transition is in flight (~2-5s): don't let a second press
   queue up behind it in the wrong direction. */
.ctrl-btn.busy {
  opacity: 0.5;
  pointer-events: none;
}

/* Side Panel & Cards */
.side-panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
  touch-action: auto;
}

.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 20px;
  box-shadow: var(--shadow);
}

.card h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 14px;
  color: var(--text-main);
}

/* Access-address card: title + refresh button on one line */
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
}

.card-head h3 {
  margin-bottom: 0;
}

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 3px 10px;
  font-size: 0.72rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  font-family: inherit;
}

.btn-icon:hover {
  color: var(--text-main);
  border-color: var(--primary);
}

.access-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.access-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.access-group-label {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  text-transform: uppercase;
}

.access-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.access-url {
  flex: 1;
  min-width: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.74rem;
  color: var(--text-main);
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 9px;
  overflow-x: auto;
  white-space: nowrap;
}

.access-copy {
  flex: 0 0 auto;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  color: var(--text-main);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.72rem;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
}

.access-copy:hover {
  background: var(--primary);
  border-color: var(--primary);
}

.access-copy.copied {
  background: var(--accent);
  border-color: var(--accent);
  color: #05231a;
}

.access-note {
  font-size: 0.76rem;
  color: var(--text-muted);
}

.access-warn {
  font-size: 0.76rem;
  color: #fbbf24;
}

/* Verdict of the server's own public-path check (see lib/funnel-guard.js). */
.access-state {
  font-size: 0.76rem;
  margin-top: 4px;
}
.access-state.ok {
  color: #34d399;
}
.access-state.fail {
  color: #f87171;
}
.access-state.unknown {
  color: var(--text-muted);
}

.access-badge {
  display: inline-block;
  font-size: 0.62rem;
  padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  margin-left: 6px;
  vertical-align: middle;
}

.input-group {
  display: flex;
  gap: 8px;
}

.input-group input {
  flex: 1;
  background: var(--bg-dark);
  border: 1px solid var(--border);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: 8px;
  outline: none;
  font-size: 0.9rem;
}

.input-group input:focus {
  border-color: var(--primary);
}

.hint-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 8px;
}

.info-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 0.88rem;
}

.info-list li {
  display: flex;
  justify-content: space-between;
  padding-bottom: 6px;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.08);
}

.info-label {
  color: var(--text-muted);
}

/* Buttons */
.btn {
  padding: 8px 14px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.85rem;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.btn-text {
  white-space: nowrap;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-primary-outline {
  background: rgba(99, 102, 241, 0.1);
  color: #818cf8;
  border: 1px solid var(--primary);
}

.btn-primary-outline:hover {
  background: var(--primary);
  color: #fff;
}

.btn-secondary {
  background: var(--bg-dark);
  color: var(--text-main);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--bg-hover);
}

.btn-block {
  width: 100%;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  touch-action: auto;
}

.modal.active {
  display: flex;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
}

.modal-card {
  position: relative;
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  width: 90%;
  max-width: 460px;
  padding: 24px;
  box-shadow: var(--shadow);
  z-index: 101;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 16px;
}

.form-group label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.form-group input {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: 8px;
  outline: none;
}

.form-group input:focus {
  border-color: var(--primary);
}

.form-select {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  color: var(--text-main);
  padding: 10px 14px;
  border-radius: 8px;
  outline: none;
  font-size: 0.9rem;
  width: 100%;
}

.form-select option {
  background: var(--bg-card);
  color: var(--text-main);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 20px;
}

.gate-intro {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 16px;
  line-height: 1.5;
}

/* Auto-detected device hint (shown when the server finds the phone by itself) */
.gate-hint {
  font-size: 0.82rem;
  color: #16a34a;
  min-height: 18px;
  margin: -6px 0 10px 0;
  line-height: 1.4;
}

.checkbox-group {
  margin-top: 12px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
}

.error-msg {
  color: var(--danger);
  font-size: 0.85rem;
  margin-top: 10px;
  display: none;
}

.error-msg.visible {
  display: block;
}

.mobile-quick-text-bar {
  display: none;
}

.mobile-only {
  display: none;
}

.device-info-compact {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Mobile responsive adjustments (width <= 768px) */
@media (max-width: 768px) {

  /* ─── Reset body/html ─── */
  html {
    height: -webkit-fill-available;
  }

  body {
    min-height: 100vh;
    min-height: -webkit-fill-available;
    overflow: hidden;
    touch-action: none;
    display: block;
  }

  /* ─── App container: fills exactly the visual viewport ─── */
  .app-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    /* 4px outer padding, 0 gap — gaps managed per-section below */
    padding: 4px;
    gap: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-sizing: border-box;
  }

  /* ─── Navbar: thin, no brand title ─── */
  .brand {
    display: none !important;
  }

  .navbar {
    flex-shrink: 0;
    height: 36px;
    min-height: 36px;
    padding: 0 8px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
  }

  .status-bar {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    width: 100%;
  }

  .status-badge {
    padding: 3px 8px;
    font-size: 0.72rem;
    border-radius: 20px;
    flex-shrink: 0;
  }

  .status-dot {
    width: 6px;
    height: 6px;
  }

  .status-text-full {
    font-size: 0.72rem;
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .navbar .btn,
  .status-bar .btn {
    padding: 3px 8px;
    font-size: 0.72rem;
    height: 28px;
    border-radius: 6px;
    flex-shrink: 0;
  }

  .btn-text {
    display: inline !important;
    font-size: 0.72rem;
    white-space: nowrap;
  }

  .mobile-only {
    display: inline-flex !important;
  }

  /* ─── Main content: takes all remaining height, no gap (screen-panel fills it) ─── */
  .main-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    gap: 0;
    overflow: hidden;
    margin-top: 4px;
  }

  .side-panel {
    display: none !important;
  }

  /* ─── Screen panel: flex column, fills height ─── */
  .screen-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    /* Override desktop justify-content:center & align-items:center */
    justify-content: flex-start;
    align-items: stretch;
    padding: 0;
    gap: 6px;     /* gap between video area and control bar */
    background: transparent;
    border: none;
    border-radius: 0;
    overflow: hidden;
  }

  /* ─── Video wrapper: takes all available space ─── */
  .screen-wrapper {
    flex: 1;
    min-height: 0;
    width: 100%;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: #000;
    overflow: hidden;
    touch-action: none;
    position: relative;
  }

  video#screenVideo {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    pointer-events: none;
    z-index: 1;
  }

  canvas#screenCanvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    cursor: pointer;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    z-index: 2;
  }

  /* ─── Quick text bar: hidden on mobile (use modal instead) ─── */
  .mobile-quick-text-bar {
    display: none !important;
  }

  /* ─── Control bar: 6 equal buttons in one row, fixed height ─── */
  .control-bar {
    flex-shrink: 0;
    height: 52px;   /* more room for icon + label */
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 5px;
    padding: 5px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    touch-action: auto;
    box-sizing: border-box;
  }

  .ctrl-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    height: 100%;
    width: 100%;
    padding: 3px 2px;
    border-radius: 7px;
    border: 1px solid var(--border);
    background: rgba(255,255,255,0.04);
    cursor: pointer;
    transition: background 0.15s;
    min-width: 0;
    overflow: hidden;
  }

  .ctrl-btn:active {
    background: var(--bg-hover);
  }

  .ctrl-btn svg {
    width: 17px;
    height: 17px;
    flex-shrink: 0;
  }

  .ctrl-btn span {
    font-size: 0.65rem;
    font-weight: 500;
    line-height: 1;
    white-space: nowrap;
    color: var(--text-muted);
  }
}

/* ─── Paused stream button indicator ─── */
#toggleStreamBtn.paused {
  background: rgba(239, 68, 68, 0.15);
  border-color: rgba(239, 68, 68, 0.4);
  color: #f87171;
}

