:root {
  color-scheme: light;
  --bg: #f6f7f8;
  --panel: #ffffff;
  --ink: #172026;
  --muted: #687680;
  --line: #dce3e8;
  --accent: #006d77;
  --accent-strong: #00545c;
  --warning: #b45309;
  --ok: #0f766e;
  --shadow: 0 12px 32px rgba(23, 32, 38, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.shell {
  width: min(1180px, calc(100vw - 32px));
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 0 max(40px, env(safe-area-inset-bottom));
}

.topbar,
.panel-head,
.actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar {
  margin-bottom: 22px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.board-panel {
  margin-bottom: 18px;
}

.board-head {
  align-items: flex-start;
}

.board-actions,
.quick-actions,
.control-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 32px;
  line-height: 1.1;
}

h2 {
  font-size: 17px;
}

.setup,
.content {
  display: grid;
  gap: 18px;
}

.setup {
  grid-template-columns: minmax(0, 1.05fr) minmax(280px, 0.95fr);
  margin-bottom: 18px;
}

.content {
  grid-template-columns: minmax(0, 1.1fr) minmax(320px, 0.9fr);
}

.camera-panel {
  grid-column: 1 / -1;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

label {
  display: grid;
  gap: 7px;
  margin-top: 14px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input,
select,
button {
  min-height: 42px;
  border-radius: 7px;
  font: inherit;
}

input,
select {
  width: 100%;
  border: 1px solid var(--line);
  padding: 0 12px;
  color: var(--ink);
  background: #fff;
}

button {
  border: 0;
  padding: 0 16px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
}

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

button:disabled,
input:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

button.secondary {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
}

button.secondary:hover {
  background: #eef4f5;
}

.link-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 14px;
  background: #fff;
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  text-decoration: none;
}

.link-button:hover {
  background: #eef4f5;
}

.icon-button {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 999px;
  font-size: 22px;
}

.icon-button.small {
  width: 36px;
  height: 36px;
  min-height: 36px;
  font-size: 18px;
}

.icon-button.danger {
  background: #fff;
  border: 1px solid var(--line);
  color: #9f1239;
}

.icon-button.danger:hover {
  background: #fff1f2;
}

.quick-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 12px;
  margin-top: 16px;
}

.quick-grid.empty {
  min-height: 128px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 18px;
}

.quick-card {
  display: grid;
  gap: 14px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: #fff;
}

.quick-card.is-on {
  border-color: rgba(0, 109, 119, 0.42);
  background: #f4fbfb;
}

.quick-title {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.quick-title strong,
.quick-title span,
.quick-title code {
  overflow-wrap: anywhere;
}

.quick-title strong {
  font-size: 15px;
  line-height: 1.25;
}

.quick-title span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.quick-title code {
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 11px;
  font-weight: 800;
}

.quick-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.quick-state {
  color: var(--muted);
  font-size: 24px;
  font-weight: 900;
  line-height: 1;
}

.quick-card.is-on .quick-state {
  color: var(--accent);
}

.quick-actions {
  justify-content: flex-end;
}

.quick-action,
.pin-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  min-height: 34px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font-size: 16px;
  font-weight: 900;
}

.quick-action:hover,
.pin-action:hover,
.pin-action.active {
  background: #eef4f5;
  color: var(--accent);
}

.checklist ol {
  margin: 14px 0 0;
  padding-left: 22px;
  color: var(--muted);
  line-height: 1.55;
}

.devices {
  display: grid;
  gap: 10px;
  margin-top: 14px;
  max-height: 680px;
  overflow: auto;
  padding-right: 4px;
}

.devices.empty {
  min-height: 160px;
  place-items: center;
  color: var(--muted);
  border: 1px dashed var(--line);
  border-radius: 8px;
}

.devices-panel {
  align-self: start;
  position: sticky;
  top: 18px;
}

.device {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 12px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 13px;
  background: #fff;
  color: var(--ink);
  text-align: left;
}

.function-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.function-list.empty {
  min-height: 88px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
}

.control-row {
  display: grid;
  grid-template-columns: minmax(110px, 0.8fr) minmax(0, 1.3fr) auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px;
  background: #fafcfc;
}

.control-label {
  display: grid;
  gap: 3px;
  min-width: 0;
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
}

.control-label span,
.control-label code {
  overflow-wrap: anywhere;
}

.control-label code {
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 11px;
  font-weight: 800;
}

.control-field {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.control-field input[type="range"] {
  flex: 1 1 auto;
  min-width: 90px;
}

.control-field select,
.control-field input:not([type="range"]):not([type="checkbox"]) {
  min-height: 38px;
}

.hsv-control {
  align-items: center;
  flex-wrap: wrap;
}

.color-wheel {
  position: relative;
  flex: 0 0 132px;
  width: 132px;
  height: 132px;
  touch-action: none;
}

.color-wheel-canvas {
  width: 132px;
  height: 132px;
  border-radius: 50%;
  cursor: crosshair;
  display: block;
}

.color-wheel-pointer {
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--swatch);
  box-shadow: 0 0 0 1px rgba(23, 32, 38, 0.35), 0 2px 8px rgba(23, 32, 38, 0.28);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.hsv-preview,
.status-swatch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.hsv-preview::before,
.status-swatch {
  content: "";
  width: 18px;
  height: 18px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--swatch);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.55);
}

.hsv-preview {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.hsv-sliders {
  display: grid;
  flex: 1 1 140px;
  gap: 4px;
  min-width: 120px;
}

.saturation-range {
  flex: 1 1 160px;
  max-width: 260px;
}

.mini-range {
  display: grid;
  grid-template-columns: 18px 1fr;
  gap: 6px;
  align-items: center;
  margin: 0;
  color: var(--muted);
  font-size: 11px;
}

.mini-range input[type="range"] {
  width: 100%;
  min-width: 0;
}

.number-control {
  flex: 0 0 82px;
  padding: 0 8px;
}

.control-apply {
  min-height: 38px;
  padding: 0 12px;
}

.control-actions {
  justify-content: flex-end;
}

.switch-control {
  position: relative;
  display: inline-flex;
  align-items: center;
  width: 52px;
  height: 30px;
  margin: 0;
}

.switch-control input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-control span {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #cbd5dc;
  transition: background 160ms ease;
}

.switch-control span::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(23, 32, 38, 0.2);
  transition: transform 160ms ease;
}

.switch-control input:checked + span {
  background: var(--accent);
}

.switch-control input:checked + span::after {
  transform: translateX(22px);
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
  gap: 10px;
  margin-top: 16px;
}

.status-grid.empty {
  min-height: 96px;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
}

.status-card {
  display: grid;
  gap: 6px;
  min-width: 0;
  min-height: 94px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.status-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  line-height: 1.2;
}

.status-card strong {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-size: 20px;
  line-height: 1.15;
  max-width: 100%;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.status-card code {
  align-self: end;
  max-width: 100%;
  min-width: 0;
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 11px;
  font-weight: 800;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.debug-json {
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.debug-json summary {
  cursor: pointer;
}

.camera-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}

.camera-frame {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  margin-top: 14px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  aspect-ratio: 16 / 9;
  background: #111820;
}

.camera-frame video {
  width: 100%;
  height: 100%;
  background: #111820;
}

.camera-frame.empty video {
  display: none;
}

#cameraPlaceholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  color: #d9e5ea;
  text-align: center;
}

.camera-frame:not(.empty) #cameraPlaceholder {
  display: none;
}

.device:hover,
.device.selected {
  border-color: var(--accent);
}

.device-name {
  display: block;
  color: var(--ink);
  font-size: 15px;
  font-weight: 800;
  line-height: 1.3;
}

.device-meta {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.online {
  color: var(--ok);
}

.offline {
  color: var(--warning);
}

pre {
  min-height: 160px;
  overflow: auto;
  margin: 10px 0 0;
  padding: 14px;
  border-radius: 8px;
  background: #101820;
  color: #d8f3dc;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
}

#toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  padding: 12px 14px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 160ms ease, transform 160ms ease;
  pointer-events: none;
}

#toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 820px) {
  .shell {
    width: min(100vw - 24px, 560px);
  }

  .setup,
  .content {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: flex-start;
  }

  h1 {
    font-size: 27px;
  }

  .control-row {
    grid-template-columns: 1fr;
  }

  .control-actions {
    justify-content: stretch;
  }

  .control-apply {
    width: 100%;
  }

  .pin-action {
    flex: 0 0 42px;
    width: 42px;
    height: 42px;
  }

  .quick-grid {
    grid-template-columns: 1fr;
  }
}
