:root {
  color-scheme: dark;
  --bg: #080d17;
  --panel: rgba(18, 27, 45, 0.92);
  --panel-strong: #162238;
  --line: rgba(148, 163, 184, 0.18);
  --text: #f6f8fc;
  --muted: #9aa8bd;
  --accent: #69a7ff;
  --accent-strong: #3b82f6;
  --green: #45d29a;
  --amber: #f2b84b;
  --red: #fb7185;
  font-family: Inter, "SF Pro Display", "Segoe UI", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; }
button { touch-action: manipulation; }

.offline-banner {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  text-align: center;
  background: #9f1239;
  color: white;
  font-size: 13px;
  font-weight: 700;
}

.offline-banner:not([hidden]) + .shell { padding-top: calc(38px + env(safe-area-inset-top)); }

.shell {
  width: auto;
  max-width: 680px;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 16px calc(28px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  gap: 11px;
  min-height: 48px;
  margin-bottom: 18px;
}

.topbar > div:nth-child(2) { display: grid; gap: 2px; }
.topbar strong { font-size: 15px; }
.topbar span { color: var(--muted); font-size: 12px; }

.brand-mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  background: linear-gradient(145deg, #65a7ff, #2563eb);
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.32);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.08em;
}

.icon-button {
  margin-left: auto;
  width: 42px;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: rgba(22, 34, 56, 0.86);
  font-size: 24px;
}

.icon-button:active { transform: scale(0.96); }

.pair-card,
.status-card,
.control-card,
.section-card,
.device-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(16px);
}

.pair-card { padding: 26px 22px; }
.pair-card h1 { margin: 6px 0 10px; font-size: 25px; }
.pair-card p { margin: 0 0 24px; color: var(--muted); line-height: 1.65; }
.eyebrow { color: var(--accent); font-size: 12px; font-weight: 800; letter-spacing: 0.12em; }
.pair-card form { display: grid; gap: 10px; }
.pair-card label { margin-top: 4px; color: #c9d2df; font-size: 13px; font-weight: 700; }
.pair-card input {
  width: 100%;
  min-height: 51px;
  padding: 0 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  color: var(--text);
  background: rgba(6, 11, 21, 0.7);
}
.pair-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(105, 167, 255, 0.14); }
.pair-code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; letter-spacing: 0.12em; }

.primary-button {
  min-height: 53px;
  margin-top: 9px;
  border: 0;
  border-radius: 8px;
  color: white;
  background: linear-gradient(135deg, #4f92f7, #2563eb);
  font-weight: 800;
}

.security-note { margin-top: 18px; color: var(--muted); font-size: 12px; line-height: 1.5; }

#app-view { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; min-width: 0; }
#app-view[hidden] { display: none; }
#app-view > * { min-width: 0; }

.status-card { padding: 20px; }
.status-heading { display: flex; align-items: center; gap: 8px; }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); box-shadow: 0 0 0 5px rgba(154, 168, 189, 0.08); }
.status-dot.running { background: var(--green); box-shadow: 0 0 0 5px rgba(69, 210, 154, 0.1); }
.status-dot.paused { background: var(--amber); box-shadow: 0 0 0 5px rgba(242, 184, 75, 0.1); }
.status-dot.attention { background: var(--red); box-shadow: 0 0 0 5px rgba(251, 113, 133, 0.1); }
.status-pill { color: #cbd5e1; font-size: 13px; font-weight: 800; }
.last-updated { margin-left: auto; color: var(--muted); font-size: 11px; }
.status-card h1 { margin: 18px 0 7px; font-size: clamp(22px, 6vw, 29px); line-height: 1.18; }
.status-card p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }
.progress-track { height: 6px; margin-top: 20px; overflow: hidden; border-radius: 999px; background: rgba(148, 163, 184, 0.13); }
.progress-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #3b82f6, #6ee7b7); transition: width 300ms ease; }
.progress-label { margin-top: 8px; color: var(--muted); font-size: 11px; }

.control-card { padding: 14px; }
.control-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.pause-button,
.resume-button {
  position: relative;
  min-height: 126px;
  overflow: hidden;
  border: 0;
  border-radius: 8px;
  color: white;
}
.pause-button { background: linear-gradient(145deg, #c2415d, #9f1239); }
.resume-button { background: linear-gradient(145deg, #2173dc, #1d4ed8); }
.pause-button > *, .control-content > * { position: relative; z-index: 2; display: block; }
.control-content { position: relative; z-index: 2; display: block; }
.control-icon { margin-bottom: 9px; font-size: 25px; font-weight: 900; }
.pause-button strong, .resume-button strong { font-size: 17px; }
.pause-button small, .resume-button small { margin-top: 5px; color: rgba(255, 255, 255, 0.75); font-size: 11px; }
.hold-fill { position: absolute; inset: 0; z-index: 1; transform: scaleX(0); transform-origin: left; background: rgba(69, 210, 154, 0.42); }
.resume-button.holding .hold-fill { transform: scaleX(1); transition: transform 1.2s linear; }
.pause-button:active { transform: scale(0.98); }
button:disabled, input:disabled + .switch { opacity: 0.42; filter: grayscale(0.35); }

.switch-row { display: flex; align-items: center; gap: 12px; margin-top: 12px; padding: 13px 7px 4px; border-top: 1px solid var(--line); }
.switch-row > span:first-child { display: grid; gap: 3px; flex: 1; }
.switch-row strong { font-size: 13px; }
.switch-row small { color: var(--muted); font-size: 11px; }
.switch-row input { position: absolute; opacity: 0; pointer-events: none; }
.switch { position: relative; width: 48px; height: 28px; flex: 0 0 auto; border-radius: 999px; background: #354157; transition: background 160ms ease; }
.switch::after { content: ""; position: absolute; top: 4px; left: 4px; width: 20px; height: 20px; border-radius: 50%; background: white; transition: transform 160ms ease; }
.switch-row input:checked + .switch { background: var(--accent-strong); }
.switch-row input:checked + .switch::after { transform: translateX(20px); }
.dispatch-open-button {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  min-height: 58px;
  margin-top: 12px;
  padding: 10px 12px;
  border: 1px solid rgba(69, 210, 154, 0.36);
  border-radius: 8px;
  color: var(--text);
  background: #173329;
  text-align: left;
}
.dispatch-open-button > span:last-child { display: grid; gap: 3px; min-width: 0; }
.dispatch-open-button strong { font-size: 13px; }
.dispatch-open-button small { color: #9fcfbd; font-size: 10px; }
.dispatch-icon { display: grid; place-items: center; width: 32px; height: 32px; flex: 0 0 auto; color: #07150f; background: var(--green); border-radius: 50%; font-size: 22px; line-height: 1; }

.section-card {
  padding: 10px 0 2px;
  border: 0;
  background: transparent;
  box-shadow: none;
  backdrop-filter: none;
}
.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.section-title h2 { margin: 0; font-size: 15px; }
.section-title span { color: var(--muted); font-size: 11px; }
.task-filter {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 3px;
  margin-bottom: 10px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(5, 10, 19, 0.45);
}
.task-filter button {
  min-width: 0;
  min-height: 34px;
  padding: 0 4px;
  border: 0;
  border-radius: 5px;
  color: var(--muted);
  background: transparent;
  font-size: 11px;
  font-weight: 700;
}
.task-filter button.active { color: var(--text); background: #243147; }
.task-list, .incident-list { display: grid; gap: 8px; }
.task-item, .incident-item { padding: 13px; border: 1px solid rgba(148, 163, 184, 0.12); border-radius: 8px; background: rgba(18, 27, 45, 0.72); }
.task-item { width: 100%; min-width: 0; max-width: 100%; overflow: hidden; color: var(--text); text-align: left; }
.task-item.active { border-color: rgba(105, 167, 255, 0.48); background: rgba(59, 130, 246, 0.09); }
.item-top { display: flex; align-items: center; gap: 10px; }
.item-top strong { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.item-state { padding: 3px 7px; border-radius: 999px; color: #c8d3e2; background: rgba(148, 163, 184, 0.11); font-size: 10px; }
.item-meta { margin-top: 7px; color: var(--muted); font-size: 11px; line-height: 1.45; }
.task-progress { display: grid; grid-template-columns: minmax(72px, 1fr) auto auto; align-items: center; gap: 8px; margin-top: 10px; }
.task-progress > span { height: 5px; overflow: hidden; border-radius: 3px; background: rgba(148, 163, 184, 0.14); }
.task-progress i { display: block; height: 100%; border-radius: inherit; background: var(--green); }
.task-progress small { color: var(--muted); font-size: 10px; white-space: nowrap; }
.health-label { color: var(--muted); }
.health-active, .health-complete { color: var(--green) !important; }
.health-quiet, .health-paused, .health-recovering { color: var(--amber) !important; }
.health-stalled, .health-attention { color: var(--red) !important; }
.incident-item strong { display: block; font-size: 13px; line-height: 1.4; }
.incident-item p { margin: 6px 0 0; color: var(--muted); font-size: 11px; line-height: 1.5; }
.incident-item.user-action { border-color: rgba(251, 113, 133, 0.42); }
.empty-message { padding: 12px 2px; color: var(--muted); font-size: 12px; }

.device-card { display: flex; align-items: center; gap: 12px; padding: 16px 18px; }
.device-card > div { display: grid; gap: 3px; flex: 1; }
.device-card strong { font-size: 12px; }
.device-card span { color: var(--muted); font-size: 10px; }
.text-button { border: 0; color: #a9c9f8; background: transparent; font-size: 11px; }

body.detail-open, body.dispatch-open { overflow: hidden; }
.task-detail-layer,
.dispatch-layer {
  position: fixed;
  z-index: 25;
  inset: 0;
  display: grid;
  align-items: end;
  justify-items: center;
  padding: 12px;
}
.task-detail-layer[hidden], .dispatch-layer[hidden] { display: none; }
.detail-backdrop {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: rgba(2, 6, 12, 0.78);
}
.task-detail-panel,
.dispatch-panel {
  position: relative;
  width: min(100%, 648px);
  max-height: calc(100dvh - 24px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #111a2a;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.48);
}
.detail-header {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 18px;
  border-bottom: 1px solid var(--line);
}
.detail-header > div { min-width: 0; flex: 1; }
.detail-kicker { display: block; margin-bottom: 5px; color: var(--accent); font-size: 10px; font-weight: 800; }
.detail-header h2 { margin: 0; overflow-wrap: anywhere; font-size: 18px; line-height: 1.35; }
.detail-close {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: #202c40;
  font-size: 24px;
  line-height: 1;
}
.detail-body {
  max-height: calc(100dvh - 104px);
  overflow-y: auto;
  padding: 18px;
  overscroll-behavior: contain;
}
.detail-message { padding: 28px 4px; color: var(--muted); text-align: center; font-size: 12px; }
.detail-message.error { color: var(--red); }
.detail-summary { padding-bottom: 16px; border-bottom: 1px solid var(--line); }
.detail-status-line { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.detail-status-line strong { font-size: 14px; }
.detail-status-line .health-label { font-size: 11px; font-weight: 700; text-align: right; }
.detail-progress-track { height: 7px; margin-top: 14px; overflow: hidden; border-radius: 4px; background: rgba(148, 163, 184, 0.14); }
.detail-progress-track span { display: block; height: 100%; border-radius: inherit; background: var(--green); }
.detail-summary p { margin: 8px 0 0; color: var(--muted); font-size: 11px; }
.detail-metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; margin: 14px 0; overflow: hidden; border: 1px solid var(--line); border-radius: 8px; background: var(--line); }
.detail-metric { display: grid; gap: 5px; min-width: 0; padding: 12px; background: #172235; }
.detail-metric span { color: var(--muted); font-size: 10px; }
.detail-metric strong { overflow-wrap: anywhere; font-size: 12px; }
.detail-metric.warning strong { color: var(--red); }
.current-stage { padding: 14px; border-left: 3px solid var(--accent); background: #172235; }
.current-stage > span { color: var(--accent); font-size: 10px; font-weight: 800; }
.current-stage h3 { margin: 6px 0; overflow-wrap: anywhere; font-size: 14px; line-height: 1.4; }
.current-stage p { margin: 0; color: var(--muted); font-size: 11px; line-height: 1.5; }
.timeline-section { margin-top: 20px; }
.timeline-section > h3 { margin: 0 0 12px; font-size: 14px; }
.stage-timeline { display: grid; gap: 0; margin: 0; padding: 0; list-style: none; }
.stage-timeline li { position: relative; display: flex; align-items: center; gap: 10px; min-height: 58px; padding: 10px 0 10px 28px; border-bottom: 1px solid rgba(148, 163, 184, 0.1); }
.stage-timeline li::before { content: ""; position: absolute; top: 22px; left: 5px; width: 9px; height: 9px; border: 2px solid #69768a; border-radius: 50%; background: #111a2a; }
.stage-timeline li::after { content: ""; position: absolute; top: 34px; bottom: -22px; left: 10px; width: 1px; background: rgba(148, 163, 184, 0.2); }
.stage-timeline li:last-child::after { display: none; }
.stage-timeline li.stage-complete::before { border-color: var(--green); background: var(--green); }
.stage-timeline li.stage-running::before { border-color: var(--accent); background: var(--accent); }
.stage-timeline li.stage-attention::before, .stage-timeline li.stage-failed::before { border-color: var(--red); background: var(--red); }
.stage-timeline li > div { display: grid; gap: 4px; min-width: 0; flex: 1; }
.stage-timeline strong { overflow-wrap: anywhere; font-size: 12px; line-height: 1.4; }
.stage-timeline div span { color: var(--muted); font-size: 10px; }
.timeline-state { flex: 0 0 auto; color: var(--muted); font-size: 10px; }

.dispatch-form {
  display: grid;
  gap: 15px;
  max-height: calc(100dvh - 104px);
  overflow-y: auto;
  padding: 18px;
  overscroll-behavior: contain;
}
.workflow-selector { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; margin: 0; padding: 3px; border: 1px solid var(--line); border-radius: 8px; background: #0b1321; }
.workflow-selector legend { width: 100%; margin-bottom: 7px; padding: 0; color: var(--muted); font-size: 11px; font-weight: 700; }
.workflow-selector label { min-width: 0; }
.workflow-selector input { position: absolute; opacity: 0; pointer-events: none; }
.workflow-selector span { display: grid; place-items: center; min-height: 40px; padding: 0 8px; border-radius: 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.workflow-selector input:checked + span { color: var(--text); background: #26364e; }
.form-field { display: grid; gap: 7px; min-width: 0; }
.form-field > span { color: #cbd5e1; font-size: 11px; font-weight: 700; }
.form-field input,
.form-field textarea {
  width: 100%;
  min-width: 0;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  color: var(--text);
  background: #0a111d;
  line-height: 1.55;
}
.form-field input { min-height: 46px; }
.form-field textarea { min-height: 164px; resize: vertical; }
.form-field input:focus, .form-field textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(105, 167, 255, 0.12); }
.form-field small { color: var(--muted); font-size: 10px; text-align: right; }
.form-field small.over-limit { color: var(--red); font-weight: 700; }
.dispatch-switch { margin: 0; padding: 13px 1px 2px; }
.dispatch-actions { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr); gap: 8px; }
.dispatch-actions button { min-height: 48px; margin: 0; border-radius: 8px; }
.secondary-button { border: 1px solid var(--line); color: var(--text); background: #202c40; font-weight: 700; }
.dispatch-actions .primary-button { background: #2563eb; }

.toast {
  position: fixed;
  z-index: 30;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 12px 15px;
  transform: translateX(-50%);
  border: 1px solid var(--line);
  border-radius: 13px;
  color: white;
  background: #21304a;
  box-shadow: 0 10px 35px rgba(0,0,0,.35);
  font-size: 12px;
  text-align: center;
}
.toast.error { background: #9f1239; }

@media (max-width: 360px) {
  .control-grid { grid-template-columns: 1fr; }
  .pause-button, .resume-button { min-height: 98px; }
  .task-progress { grid-template-columns: minmax(54px, 1fr) auto; }
  .task-progress .health-label { grid-column: 1 / -1; }
  .detail-metrics { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
