:root {
  color-scheme: dark;
  --bg: #0a0e13;
  --panel: #111a23;
  --panel-2: #16222d;
  --border: #263544;
  --text: #e7eef6;
  --muted: #8ea2b5;
  --accent: #3ba7ff;
  --accent-strong: #0d84e6;
  --danger: #e05b5b;
  --ok: #47c980;
  --warn: #e0b45b;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
  touch-action: manipulation;
}

button:hover {
  background: #1d2c3a;
  border-color: #3a4f63;
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  opacity: 0.55;
  cursor: default;
}

button.active {
  border-color: var(--accent);
  background: #123b5c;
}

button.danger {
  border-color: #5a2d2d;
  color: #ffb7b7;
}

button.danger:hover {
  background: #3a2020;
}

select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 8px;
}

input,
textarea {
  width: 100%;
  background: #0d151d;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  outline: none;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
}

label {
  display: block;
  margin: 12px 0 6px;
  color: var(--muted);
  font-size: 12px;
}

.muted {
  color: var(--muted);
  line-height: 1.6;
}

.error {
  color: #ff9a9a;
  margin: 10px 0 0;
}

.hint {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

/* Login */

.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 10% 0%, #12324b 0%, transparent 60%),
    radial-gradient(700px 400px at 100% 100%, #1a1530 0%, transparent 55%),
    var(--bg);
}

.login-card {
  width: min(420px, 100%);
  background: rgba(17, 26, 35, 0.92);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.45);
}

.login-card h1 {
  margin: 0 0 8px;
  font-size: 22px;
}

.login-card button[type="submit"] {
  width: 100%;
  margin-top: 18px;
  padding: 11px;
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  font-weight: 600;
}

.login-card button[type="submit"]:hover {
  background: var(--accent);
}

.login-card .hint {
  margin: 18px 0 0;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}

/* Devices page */

body.devices-page {
  overflow-y: auto;
}

#devicesApp {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

#devicesHeader {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: linear-gradient(180deg, rgba(17, 26, 35, 0.98), rgba(13, 21, 29, 0.96));
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
}

#devicesHeader .brand {
  flex: 1 1 auto;
  min-width: 120px;
  font-size: 16px;
}

#devicesHeader .toolbar-actions {
  margin-left: auto;
}

.devices-main {
  flex: 1 1 auto;
  width: min(920px, 100%);
  margin: 0 auto;
  padding: 18px 16px calc(28px + env(safe-area-inset-bottom));
}

.devices-sub {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.device-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 12px;
}

.device-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.device-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.device-title {
  flex: 1 1 auto;
  min-width: 0;
}

.device-name {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-address {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  font-family: Consolas, "Courier New", monospace;
  overflow-wrap: anywhere;
}

.state-badge {
  flex: 0 0 auto;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.state-badge.ok {
  color: #a8efc7;
  border-color: #2c6b49;
  background: rgba(71, 201, 128, 0.12);
}

.state-badge.warn {
  color: #f4d79a;
  border-color: #6b5a2c;
  background: rgba(224, 180, 91, 0.12);
}

.state-badge.off {
  color: #c7d3df;
}

.device-meta {
  min-height: 18px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.device-card-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.device-card-actions button {
  flex: 1 1 0;
  min-height: 40px;
}

button.primary {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  font-weight: 600;
}

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

.empty-state {
  border: 1px dashed var(--border);
  border-radius: 14px;
  padding: 34px 20px;
  text-align: center;
  color: var(--muted);
}

.empty-state h2 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 18px;
}

.empty-state p {
  margin: 0;
  line-height: 1.7;
}

.segmented {
  display: flex;
  gap: 6px;
  margin: 4px 0 8px;
}

.segmented button {
  flex: 1 1 0;
}

.segmented button.active {
  border-color: var(--accent);
  background: #123b5c;
  color: #eaf4ff;
}

/* App shell */

.app-body {
  overflow: hidden;
}

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

#toolbar {
  background: linear-gradient(180deg, #111a23, #0d151d);
  border-bottom: 1px solid var(--border);
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  min-width: 140px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  box-shadow: 0 0 0 3px rgba(142, 162, 181, 0.15);
  flex: 0 0 auto;
}

.dot.connecting,
.dot.starting {
  background: var(--warn);
  box-shadow: 0 0 0 3px rgba(224, 180, 91, 0.18);
  animation: pulse 1.4s infinite;
}

.dot.running {
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(71, 201, 128, 0.18);
}

.dot.error,
.dot.offline {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(224, 91, 91, 0.18);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

.stats {
  display: flex;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  margin-left: auto;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  position: relative;
}

.extra-actions {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  min-width: 244px;
  max-height: calc(100dvh - 72px);
  overflow-y: auto;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.55);
  z-index: 40;
}

.extra-actions.open {
  display: flex;
}

.extra-actions button,
.extra-actions select {
  width: 100%;
  min-height: 40px;
}

.extra-actions .quality-label {
  margin: 2px 0 0;
  font-size: 11px;
}

.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.menu-sep {
  height: 1px;
  background: var(--border);
  margin: 2px 0;
  flex: 0 0 auto;
}

.quality-label {
  margin: 0 0 0 4px;
}

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(600px 300px at 50% 0%, #0f1c28 0%, transparent 70%),
    #05080b;
  overflow: hidden;
}

#screen {
  display: block;
  background: #000;
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
  touch-action: none;
  cursor: crosshair;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.exit-immersive {
  position: absolute;
  top: calc(10px + env(safe-area-inset-top));
  right: 10px;
  z-index: 8;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(17, 26, 35, 0.72);
  border-color: rgba(78, 104, 128, 0.6);
  color: var(--text);
  backdrop-filter: blur(8px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

body.immersive #toolbar,
body.immersive .hint-bar {
  display: none;
}

body.immersive #app {
  padding-bottom: 0;
}

.video-sink {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(5, 8, 11, 0.82);
  backdrop-filter: blur(3px);
  text-align: center;
  padding: 24px;
  z-index: 5;
}

.overlay[hidden] {
  display: none;
}

#overlayText {
  max-width: 680px;
  line-height: 1.7;
  white-space: pre-wrap;
  color: #cfe0ef;
}

#retryBtn {
  padding: 10px 20px;
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}

.hint-bar {
  flex: 0 0 auto;
  padding: 6px 14px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  background: #0b1218;
}

.keyboard-hint {
  color: var(--accent);
}

.device-actions button {
  padding: 6px 10px;
  font-size: 13px;
}

.hidden-input {
  position: fixed;
  left: -1000px;
  top: 0;
  width: 10px;
  height: 10px;
  opacity: 0;
  pointer-events: none;
}

/* Dialog */

.dialog {
  width: min(520px, calc(100vw - 32px));
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
}

.dialog::backdrop {
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(2px);
}

.dialog h2 {
  margin: 0 0 14px;
  font-size: 18px;
}

.dialog textarea {
  resize: vertical;
  min-height: 120px;
}

.dialog-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 16px;
}

.dialog form {
  display: block;
}

/* Toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: #1b2a38;
  border: 1px solid #33506a;
  color: #eaf4ff;
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  z-index: 50;
  max-width: calc(100vw - 32px);
}

.toast[hidden] {
  display: none;
}

@media (max-width: 720px), (max-height: 520px) {
  #app {
    height: 100dvh;
    padding-bottom: calc(56px + env(safe-area-inset-bottom));
  }

  #toolbar {
    padding: calc(6px + env(safe-area-inset-top)) 8px 6px;
    gap: 0;
    position: relative;
    z-index: 20;
  }

  .toolbar-row {
    flex-wrap: nowrap;
    gap: 6px;
  }

  .brand {
    flex: 1 1 auto;
    min-width: 0;
    gap: 6px;
    font-size: 13px;
  }

  #deviceName {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .stats {
    flex: 0 0 auto;
    margin-left: 0;
    gap: 6px;
    font-size: 11px;
  }

  #resolution {
    display: none;
  }

  .toolbar-actions {
    flex: 0 0 auto;
    flex-wrap: nowrap;
    gap: 6px;
    position: relative;
  }

  .toolbar-actions > button {
    min-height: 38px;
    padding: 0 10px;
    border-radius: 9px;
    font-size: 13px;
  }

  .extra-actions {
    top: calc(100% + 6px);
  }

  .device-actions {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 25;
    margin: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: rgba(13, 21, 29, 0.96);
    border-top: 1px solid var(--border);
    backdrop-filter: blur(8px);
    flex-wrap: nowrap;
    gap: 6px;
    -webkit-overflow-scrolling: touch;
  }

  .device-actions button {
    min-height: 42px;
    padding: 0 13px;
    font-size: 14px;
    border-radius: 9px;
    flex: 1 1 0;
  }

  .hint-bar {
    display: none;
  }

  #stage {
    padding: 4px;
  }

  #screen {
    border-radius: 4px;
    box-shadow: none;
  }

  .toast {
    bottom: calc(66px + env(safe-area-inset-bottom));
  }

  .exit-immersive {
    top: calc(8px + env(safe-area-inset-top));
  }

  /* 对话框改为底部弹出式，方便手机操作 */
  .dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 88dvh;
    margin: auto 0 0;
    border-radius: 16px 16px 0 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
    overflow-y: auto;
  }

  .dialog h2 {
    font-size: 17px;
  }

  .dialog textarea {
    min-height: 100px;
  }

  .dialog-actions {
    gap: 8px;
  }

  .dialog-actions button {
    flex: 1 1 auto;
    min-height: 42px;
  }

  .login-body {
    padding:
      calc(16px + env(safe-area-inset-top))
      16px
      calc(16px + env(safe-area-inset-bottom));
  }

  .login-card {
    padding: 22px 18px;
  }

  #devicesHeader {
    padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
    gap: 8px;
  }

  #devicesHeader .brand {
    min-width: 0;
    font-size: 15px;
  }

  #devicesHeader .toolbar-actions {
    width: 100%;
    margin-left: 0;
    gap: 6px;
  }

  #devicesHeader .toolbar-actions > button {
    flex: 1 1 0;
    min-height: 40px;
    padding: 0 8px;
    font-size: 13px;
  }

  .devices-main {
    padding: 14px 10px calc(20px + env(safe-area-inset-bottom));
  }

  .device-list {
    grid-template-columns: 1fr;
  }

  .device-card-actions button {
    min-height: 44px;
  }

  body.devices-page .toast {
    bottom: calc(24px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 360px) {
  .stats {
    display: none;
  }

  .toolbar-actions > button {
    padding: 0 8px;
  }
}
