:root {
  color-scheme: dark;
  --bg-void: #000000;
  --bg-surface: rgba(16, 20, 28, 0.88);
  --bg-surface-solid: #121722;
  --bg-elevated: #1b2232;
  --border-subtle: rgba(255, 255, 255, 0.14);
  --border-strong: rgba(255, 255, 255, 0.28);
  --text-primary: #f4f7fb;
  --text-secondary: #a4b0c4;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-contrast: #ffffff;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --radius-pill: 999px;
  --radius-card: 16px;
  --shadow-float: 0 14px 40px rgba(0, 0, 0, 0.65);
  accent-color: var(--accent);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background-color: var(--bg-void);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* Full-viewport video stage */
.stage {
  position: fixed;
  inset: 0;
  background: var(--bg-void);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

#video-canvas,
#video-mse {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

#video-mse {
  object-fit: contain;
  background: var(--bg-void);
}

#video-canvas[hidden],
#video-mse[hidden] {
  display: none;
}

/* Remote Bluetooth mouse cursor overlay */
.cursor-overlay {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 20;
  pointer-events: none;
  will-change: transform;
  /* Align SVG tip (4, 3) with exact coordinate */
  margin-left: -4px;
  margin-top: -3px;
}

.cursor-overlay[hidden] {
  display: none;
}

.cursor-arrow {
  display: block;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.7));
}

.cursor-ring {
  position: absolute;
  left: -10px;
  top: -11px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2.5px solid rgba(96, 165, 250, 0.95);
  background: rgba(59, 130, 246, 0.24);
  opacity: 0;
  transform: scale(0.45);
  pointer-events: none;
}

.cursor-overlay.is-pressed .cursor-ring {
  opacity: 1;
  transform: scale(1);
}

@media (prefers-reduced-motion: no-preference) {
  .cursor-ring {
    transition: opacity 90ms ease-out, transform 110ms ease-out;
  }
}

/* Center connection / status overlay */
.status-banner {
  position: fixed;
  inset: 0;
  z-index: 15;
  display: grid;
  place-items: center;
  padding: 20px;
  pointer-events: none;
}

.status-banner[hidden] {
  display: none;
}

.status-card {
  pointer-events: auto;
  width: min(420px, 100%);
  padding: 22px 24px;
  border-radius: var(--radius-card);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow-float);
}

.status-header {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-header h2 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.status-detail {
  margin: 10px 0 18px;
  font-size: 0.92rem;
  line-height: 1.45;
  color: var(--text-secondary);
  word-break: break-word;
}

.status-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.status-dot {
  inline-size: 10px;
  block-size: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--warn);
}

.status-dot[data-state="live"] {
  background: var(--ok);
  box-shadow: 0 0 10px rgba(34, 197, 94, 0.7);
}

.status-dot[data-state="connecting"] {
  background: var(--warn);
}

.status-dot[data-state="error"],
.status-dot[data-state="offline"] {
  background: var(--err);
}

/* Floating HUD pill */
.hud {
  position: fixed;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  max-width: calc(100vw - 24px);
}

.hud-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 46px;
  min-block-size: 26px;
  padding: 2px 14px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-subtle);
  background: rgba(16, 20, 28, 0.72);
  color: var(--text-secondary);
  backdrop-filter: blur(12px);
  cursor: pointer;
}

.hud[data-collapsed="true"] .hud-toggle {
  min-block-size: 36px;
  min-inline-size: 58px;
  color: var(--text-primary);
  border-color: var(--border-strong);
  box-shadow: var(--shadow-float);
}

.hud[data-collapsed="true"] .hud-toggle svg {
  transform: rotate(180deg);
}

.hud-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: var(--radius-pill);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow-float);
  overflow-x: auto;
  scrollbar-width: none;
  max-width: 100%;
}

.hud-toolbar::-webkit-scrollbar {
  display: none;
}

.hud-toolbar[hidden] {
  display: none;
}

.hud-meta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px 0 6px;
  white-space: nowrap;
  font-size: 0.84rem;
  font-weight: 600;
  color: var(--text-primary);
}

.stats-chip {
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: var(--text-secondary);
  font-size: 0.76rem;
  font-variant-numeric: tabular-nums;
}

.stats-chip[hidden] {
  display: none;
}

.hud-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.hud-toolbar [role="separator"] {
  display: inline-block;
  inline-size: 1px;
  block-size: 24px;
  margin: 0 2px;
  border-left: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.hud-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0;
  border-radius: var(--radius-pill);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  flex-shrink: 0;
}

.hud-btn[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: rgba(255, 255, 255, 0.35);
}

.hud-btn:focus-visible,
.hud-toggle:focus-visible,
.btn:focus-visible,
.dialog-close:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 2px;
}

@media (hover: hover) {
  .hud-btn:hover {
    background: rgba(255, 255, 255, 0.11);
  }
  .hud-btn[aria-pressed="true"]:hover {
    background: var(--accent-hover);
  }
  .hud-toggle:hover {
    background: rgba(30, 38, 54, 0.92);
    color: var(--text-primary);
  }
}

.hud-btn:active {
  transform: scale(0.94);
}

@media (pointer: coarse) {
  .hud-btn {
    min-inline-size: 50px;
    min-block-size: 50px;
  }
  .hud-toggle {
    min-inline-size: 58px;
    min-block-size: 32px;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .hud-btn,
  .hud-toggle,
  .hud-toggle svg,
  .btn {
    transition: background-color 140ms ease, color 140ms ease, transform 120ms ease, border-color 140ms ease;
  }
}

@media (forced-colors: active) {
  .hud-btn[aria-pressed="true"] {
    background: Highlight;
    color: HighlightText;
    border-color: ButtonText;
  }
}

/* Native Settings Dialog */
dialog {
  margin: auto;
  padding: 0;
  width: min(480px, calc(100vw - 24px));
  max-height: calc(100dvh - 28px);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-card);
  background: var(--bg-surface-solid);
  color: var(--text-primary);
  box-shadow: var(--shadow-float);
}

dialog[open] {
  display: flex;
  flex-direction: column;
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(4, 7, 12, 0.74);
  backdrop-filter: blur(8px);
  overflow: hidden;
  overscroll-behavior: contain;
}

.dialog-form {
  display: flex;
  flex-direction: column;
  max-height: calc(100dvh - 30px);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border-subtle);
}

.dialog-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 650;
}

.dialog-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 40px;
  min-block-size: 40px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}

@media (hover: hover) {
  .dialog-close:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text-primary);
  }
}

.dialog-body {
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.field label {
  font-size: 0.86rem;
  font-weight: 600;
  color: var(--text-primary);
}

.field input,
.field select {
  min-block-size: 44px;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 0.96rem;
  font-family: inherit;
}

.field-hint {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 12px;
}

@media (max-width: 440px) {
  .field-row {
    grid-template-columns: 1fr;
  }
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  cursor: pointer;
}

.toggle-row span {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toggle-row strong {
  font-size: 0.92rem;
  font-weight: 600;
}

.toggle-row small {
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.toggle-row input[type="checkbox"] {
  inline-size: 22px;
  block-size: 22px;
  flex-shrink: 0;
  cursor: pointer;
}

.warn-box {
  margin: 0;
  padding: 11px 13px;
  border-radius: 10px;
  background: rgba(245, 158, 11, 0.14);
  border: 1px solid rgba(245, 158, 11, 0.45);
  color: #fde68a;
  font-size: 0.83rem;
  line-height: 1.4;
}

.warn-box[hidden] {
  display: none;
}

.diag-summary {
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  font-size: 0.78rem;
  line-height: 1.5;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border-subtle);
}

.footer-right {
  display: flex;
  gap: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-block-size: 44px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
}

.btn-secondary {
  background: var(--bg-elevated);
  border-color: var(--border-subtle);
  color: var(--text-primary);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

@media (hover: hover) {
  .btn-primary:hover {
    background: var(--accent-hover);
  }
  .btn-secondary:hover {
    border-color: var(--border-strong);
  }
  .btn-ghost:hover {
    background: rgba(255, 255, 255, 0.07);
    color: var(--text-primary);
  }
}

/* Car Keyboard Floating IME Bar */
.ime-bar {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 8px;
  width: min(640px, calc(100vw - 24px));
  padding: 8px 10px;
  border-radius: 14px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow-float);
}

.ime-bar[hidden] {
  display: none;
}

.ime-bar input {
  flex: 1 1 180px;
  min-inline-size: 0;
  min-block-size: 44px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 1rem;
  font-family: inherit;
}

.ime-btn {
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0 14px;
  flex-shrink: 0;
}

@media (pointer: coarse) {
  .ime-bar input,
  .ime-btn {
    min-block-size: 48px;
  }
}

/* Right-Edge Pie Control */
.pie-overlay {
  position: fixed;
  inset: 0;
  z-index: 45;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.pie-overlay[hidden] {
  display: none;
}

.pie-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 6, 14, 0.38);
  backdrop-filter: blur(3px);
}

.pie-wheel {
  position: fixed;
  right: 0;
  top: var(--pie-y, 50%);
  width: 0;
  height: 0;
  transform: scale(var(--pie-scale, 1));
  transform-origin: right center;
}

.pie-arc-ring {
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
  border: 1px solid var(--border-subtle);
  box-shadow: var(--shadow-float);
}

.pie-arc-nav {
  width: 276px;
  height: 276px;
  right: -138px;
  top: -138px;
  background: rgba(16, 20, 28, 0.84);
  backdrop-filter: blur(18px);
}

.pie-arc-apps {
  width: 468px;
  height: 468px;
  right: -234px;
  top: -234px;
  background: rgba(16, 20, 28, 0.62);
  backdrop-filter: blur(14px);
}

.pie-arc-apps[hidden] {
  display: none;
}

.pie-hub {
  position: absolute;
  right: 8px;
  top: 0;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 44px;
  min-block-size: 44px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  cursor: pointer;
  z-index: 2;
}

.pie-items {
  position: absolute;
  inset: 0;
}

.pie-item {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(calc(-50% + var(--pie-x, 0px)), calc(-50% + var(--pie-y-off, 0px)));
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-inline-size: 58px;
  min-block-size: 58px;
  padding: 6px;
  border-radius: 50%;
  border: 1px solid var(--border-subtle);
  background: rgba(27, 34, 50, 0.9);
  color: var(--text-primary);
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

.pie-item--app {
  min-inline-size: 66px;
  min-block-size: 66px;
  border-radius: 18px;
  padding: 6px 5px;
}

.pie-item.is-hovered,
.pie-item:focus-visible {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: rgba(255, 255, 255, 0.65);
  transform: translate(calc(-50% + var(--pie-x, 0px)), calc(-50% + var(--pie-y-off, 0px))) scale(1.1);
  outline: none;
  z-index: 3;
}

@media (hover: hover) {
  .pie-item:hover {
    background: var(--accent);
    color: var(--accent-contrast);
    border-color: rgba(255, 255, 255, 0.65);
    transform: translate(calc(-50% + var(--pie-x, 0px)), calc(-50% + var(--pie-y-off, 0px))) scale(1.1);
  }
  .pie-hub:hover {
    background: rgba(255, 255, 255, 0.16);
  }
}

.pie-item-label {
  font-size: 0.68rem;
  font-weight: 600;
  line-height: 1.1;
  max-width: 58px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pie-app-icon {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  object-fit: cover;
  pointer-events: none;
}

.pie-app-fallback {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: rgba(59, 130, 246, 0.25);
  border: 1px solid rgba(96, 165, 250, 0.45);
  font-size: 0.8rem;
  font-weight: 700;
}

@media (prefers-reduced-motion: no-preference) {
  .pie-item,
  .pie-hub {
    transition: transform 110ms ease, background-color 110ms ease, border-color 110ms ease;
  }
}

@media (forced-colors: active) {
  .pie-item.is-hovered,
  .pie-item:focus-visible {
    background: Highlight;
    color: HighlightText;
    border-color: ButtonText;
  }
}

/* HUD Toast Notification (popover="manual") */
.toast-region {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}

.hud-toast {
  position: fixed;
  inset: auto;
  left: 50%;
  bottom: max(92px, calc(env(safe-area-inset-bottom) + 84px));
  transform: translateX(-50%);
  margin: 0;
  padding: 10px 18px;
  max-width: min(440px, calc(100vw - 24px));
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-strong);
  background: rgba(16, 20, 28, 0.92);
  color: var(--text-primary);
  font-size: 0.92rem;
  font-weight: 600;
  text-align: center;
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow-float);
  pointer-events: none;
  opacity: 0;
}

.hud-toast:where(:popover-open, .is-open) {
  display: block;
  opacity: 1;
}

.hud-toast[hidden] {
  display: none;
}

@supports not selector(:popover-open) {
  .hud-toast:not(.is-open) {
    display: none;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .hud-toast {
    transition: opacity 150ms ease, display 150ms ease;
    transition-behavior: allow-discrete;
  }
}
