/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #0a0a0a;
  --surface: #141414;
  --surface2: #1e1e1e;
  --border: #2a2a2a;
  --text: #e4e4e7;
  --text-dim: #71717a;
  --accent: #8b5cf6;
  --accent-hover: #7c3aed;
  --accent-glow: rgba(139, 92, 246, 0.25);
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #eab308;
  --radius: 12px;
  --radius-sm: 8px;
  --toolbar-bg: rgba(20, 20, 20, 0.92);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  position: fixed;
  top: 0;
  left: 0;
}

/* ===== Pages ===== */
.page {
  display: none;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}

.page.active {
  display: flex;
}

/* ===== Connection Page ===== */
#connect-page {
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.connect-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 90%;
  max-width: 420px;
  text-align: center;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
}

.logo {
  font-size: 36px;
  font-weight: 700;
  margin-bottom: 8px;
  letter-spacing: -0.5px;
}

.logo-screen {
  color: var(--text);
}

.logo-link {
  color: var(--accent);
}

.subtitle {
  color: var(--text-dim);
  font-size: 15px;
  margin-bottom: 36px;
}

.code-input-wrap {
  margin-bottom: 16px;
  position: relative;
}

.password-input-wrap {
  margin-bottom: 24px;
  position: relative;
}

.input-label {
  display: block;
  text-align: left;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#room-code {
  width: 100%;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 36px;
  font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
  font-weight: 600;
  text-align: center;
  letter-spacing: 12px;
  padding: 16px 20px;
  outline: none;
  transition: border-color 0.2s;
  caret-color: var(--accent);
}

#room-code::placeholder {
  color: var(--border);
  letter-spacing: 12px;
}

#room-code:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
}

#room-password {
  width: 100%;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 22px;
  font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
  font-weight: 600;
  text-align: center;
  letter-spacing: 6px;
  padding: 14px 20px;
  outline: none;
  transition: border-color 0.2s;
  caret-color: var(--accent);
}

#room-password::placeholder {
  color: var(--border);
  letter-spacing: 6px;
  font-size: 18px;
}

#room-password:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-glow);
}

.btn-primary {
  width: 100%;
  padding: 16px;
  background: var(--accent);
  color: white;
  font-size: 17px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s, transform 0.1s;
  min-height: 54px;
}

.btn-primary:active {
  transform: scale(0.98);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-secondary {
  width: 100%;
  padding: 14px;
  background: transparent;
  color: var(--text-dim);
  font-size: 15px;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, color 0.2s, transform 0.1s;
  min-height: 48px;
  margin-top: 12px;
}

.btn-secondary:active {
  transform: scale(0.98);
}

.btn-secondary:hover {
  background: var(--surface2);
  color: var(--text);
}

.status-msg {
  margin-top: 20px;
  font-size: 14px;
  color: var(--text-dim);
  min-height: 20px;
  transition: color 0.2s;
}

.status-msg.error {
  color: var(--danger);
}

.status-msg.success {
  color: var(--success);
}

.status-msg.connecting {
  color: var(--accent);
}

/* ===== Approval Waiting Page ===== */
#approval-page,
#rejected-page {
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.approval-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  width: 90%;
  max-width: 420px;
  text-align: center;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
}

.approval-spinner {
  width: 80px;
  height: 80px;
  margin: 24px auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.spinner-ring {
  position: absolute;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 1.2s linear infinite;
}

.spinner-lock {
  color: var(--accent);
  z-index: 1;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.approval-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 8px;
}

.approval-subtitle {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 20px;
}

.approval-countdown {
  font-size: 13px;
  color: var(--warning);
  font-family: 'SF Mono', 'Cascadia Code', monospace;
  margin-bottom: 24px;
  animation: pulse 2s ease-in-out infinite;
}

.rejection-icon {
  margin: 24px auto;
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(239, 68, 68, 0.1);
  border-radius: 50%;
}

/* ===== Viewer Page ===== */
#viewer-page {
  background: #000;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#video-container {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  touch-action: none;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

#remote-video {
  max-width: 100%;
  max-height: 100%;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  transform-origin: 0 0;
  will-change: transform;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* LAN mode canvas */
#lan-frame-canvas {
  display: block;
  background: #000;
}

/* ===== Security Badge ===== */
.security-badge {
  position: fixed;
  top: 12px;
  right: 12px;
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 20px;
  padding: 6px 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--success);
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}

.security-badge.visible {
  opacity: 1;
}

.security-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.security-sep {
  color: var(--border);
  font-size: 10px;
}

.session-timer {
  color: var(--text-dim);
  font-family: 'SF Mono', 'Cascadia Code', monospace;
  font-size: 11px;
}

/* ===== Connection Quality ===== */
.quality-indicator {
  position: fixed;
  top: 42px;
  right: 12px;
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 20px;
  padding: 6px 14px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}

.quality-indicator.visible {
  opacity: 1;
}

.quality-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--success);
  transition: background 0.3s;
}

.quality-dot.good { background: var(--success); }
.quality-dot.fair { background: var(--warning); }
.quality-dot.poor { background: var(--danger); }

/* ===== Session Expiry Warning ===== */
.session-expiry-warning {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(234, 179, 8, 0.15);
  border: 1px solid rgba(234, 179, 8, 0.3);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 20px;
  padding: 8px 18px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--warning);
  z-index: 150;
  animation: pulse 2s ease-in-out infinite;
}

.session-expiry-warning.hidden {
  display: none;
}

/* ===== Input Paused Overlay ===== */
.input-paused-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 72px;
  z-index: 50;
  pointer-events: none;
}

.input-paused-overlay.hidden {
  display: none;
}

.input-paused-content {
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--warning);
  font-size: 14px;
  font-weight: 500;
}

/* ===== Connection Lost Overlay ===== */
.connection-lost-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 90;
}

.connection-lost-overlay.hidden {
  display: none;
}

.connection-lost-content {
  text-align: center;
  color: var(--text);
}

.connection-lost-content p {
  font-size: 18px;
  font-weight: 600;
  margin-top: 16px;
}

.connection-lost-content .connection-lost-sub {
  font-size: 14px;
  font-weight: 400;
  color: var(--text-dim);
  margin-top: 6px;
}

.connection-lost-spinner {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--danger);
  animation: spin 1s linear infinite;
  margin: 0 auto;
}

/* ===== Floating Buttons ===== */
.floating-btn {
  position: fixed;
  z-index: 200;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, opacity 0.3s;
  opacity: 0;
  pointer-events: none;
  touch-action: manipulation;
}

.floating-btn.visible {
  opacity: 1;
  pointer-events: auto;
}

.floating-btn:active {
  transform: scale(0.92);
  background: var(--surface2);
}

#toolbar-toggle {
  bottom: calc(16px + var(--safe-bottom));
  left: 16px;
}

.kb-btn {
  bottom: calc(16px + var(--safe-bottom));
  left: 76px;
}

.fs-btn {
  top: 12px;
  left: 12px;
}

.dc-btn {
  top: 12px;
  left: 72px;
}

.dc-btn:active {
  background: rgba(239, 68, 68, 0.3);
}

/* ===== Hidden Keyboard Input ===== */
#hidden-keyboard-input {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 40px;
  font-size: 16px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  border: none;
  border-top: 1px solid rgba(139, 92, 246, 0.5);
  outline: none;
  z-index: 9998;
  display: none; /* shown when keyboard toggle is active */
  font-size: 16px; /* prevent iOS zoom */
}

/* ===== Virtual Toolbar ===== */
.toolbar {
  position: fixed;
  bottom: calc(0px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--toolbar-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 16px 16px 0 0;
  padding: 6px 10px 10px;
  z-index: 300;
  width: 96%;
  max-width: 780px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  touch-action: none;
}

.toolbar.visible {
  opacity: 1;
  pointer-events: auto;
}

.toolbar.collapsed .toolbar-content {
  display: none;
}

.toolbar.collapsed {
  padding: 6px 10px;
}

.toolbar-drag-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 8px;
  cursor: grab;
}

.toolbar.collapsed .toolbar-drag-handle {
  margin-bottom: 0;
}

.toolbar-collapse-btn {
  position: absolute;
  top: 4px;
  right: 12px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text-dim);
  transition: transform 0.2s;
}

.toolbar.collapsed .toolbar-collapse-btn {
  transform: rotate(180deg);
}

.toolbar-content {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toolbar-row {
  display: flex;
  gap: 5px;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  align-items: center;
  padding: 2px 0;
}

.toolbar-row::-webkit-scrollbar {
  display: none;
}

.toolbar-sep {
  width: 1px;
  height: 28px;
  background: var(--border);
  flex-shrink: 0;
  margin: 0 4px;
}

.tkey {
  min-width: 48px;
  min-height: 40px;
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  touch-action: manipulation;
  white-space: nowrap;
}

.tkey:active {
  transform: scale(0.94);
  background: var(--accent);
  border-color: var(--accent);
}

.tkey.modifier.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  box-shadow: 0 0 8px var(--accent-glow);
}

.tkey.arrow {
  min-width: 44px;
  font-size: 18px;
  padding: 6px 8px;
}

.tkey.combo {
  font-size: 12px;
  font-family: 'SF Mono', 'Cascadia Code', monospace;
}

.fkeys {
  display: none;
}

.fkeys.expanded {
  display: flex;
}

.fkey {
  min-width: 44px;
  font-size: 12px;
}

.fkey-expand-btn {
  width: 100%;
  min-height: 36px;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  touch-action: manipulation;
}

.fkey-expand-btn:active,
.fkey-expand-btn.expanded {
  background: var(--surface2);
  color: var(--text);
}

/* ===== Responsive ===== */

/* Portrait phone */
@media (max-width: 480px) {
  .connect-card,
  .approval-card {
    padding: 36px 24px;
  }

  #room-code {
    font-size: 28px;
    letter-spacing: 8px;
  }

  #room-password {
    font-size: 18px;
    letter-spacing: 4px;
  }

  .toolbar {
    width: 100%;
    border-radius: 12px 12px 0 0;
  }

  .tkey {
    min-width: 40px;
    min-height: 36px;
    font-size: 12px;
    padding: 4px 6px;
  }
}

/* Landscape */
@media (orientation: landscape) {
  .toolbar {
    max-width: 90%;
  }

  .connect-card,
  .approval-card {
    padding: 32px 40px;
    max-width: 380px;
  }

  #room-code {
    font-size: 30px;
    padding: 12px 16px;
  }

  #room-password {
    font-size: 20px;
    padding: 10px 16px;
  }
}

/* Tablet landscape */
@media (min-width: 768px) and (orientation: landscape) {
  .toolbar {
    max-width: 720px;
  }

  .tkey {
    min-width: 52px;
    min-height: 42px;
    font-size: 14px;
  }
}

/* Large tablet */
@media (min-width: 1024px) {
  .connect-card,
  .approval-card {
    padding: 56px 48px;
    max-width: 460px;
  }

  .logo {
    font-size: 42px;
  }

  #room-code {
    font-size: 40px;
    letter-spacing: 16px;
    padding: 20px 24px;
  }

  #room-password {
    font-size: 24px;
    letter-spacing: 8px;
    padding: 16px 20px;
  }
}

/* ===== Animations ===== */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.status-msg.connecting::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 8px;
  animation: pulse 1.2s ease-in-out infinite;
  vertical-align: middle;
}

/* ===== Network Stats Overlay (F3 toggle) ===== */
.stats-overlay {
  position: fixed;
  top: 12px;
  right: 44px;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(34, 197, 94, 0.3);
  border-radius: 8px;
  padding: 8px 14px;
  z-index: 1000;
  font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
  font-size: 12px;
  color: #22c55e;
  pointer-events: none;
  min-width: 140px;
}

.stats-overlay.hidden {
  display: none;
}

.stats-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  line-height: 1.6;
}

.stats-label {
  color: rgba(34, 197, 94, 0.6);
  font-weight: 500;
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.5px;
}

/* ===== Gamepad Indicator ===== */
.gamepad-indicator {
  position: fixed;
  top: 12px;
  right: 200px;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  color: var(--accent);
  pointer-events: none;
}

.gamepad-indicator.hidden {
  display: none;
}
