:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --card: #ffffff;
  --line: #d5dce6;
  --text: #1b2430;
  --muted: #5d6b7c;
  --accent: #111111;
  --on-accent: #ffffff;
  --field: #ffffff;
  --code: #eef2f7;
  --log: #f7f9fb;
  --log-text: #1b2430;
  --ok: #17693a;
  --ok-line: #b7e0c4;
  --error: #9f1d1d;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #10141c;
  --card: #181e2a;
  --line: #2c3548;
  --text: #eef2f8;
  --muted: #9aa6ba;
  --accent: #ffffff;
  --on-accent: #111111;
  --field: #121722;
  --code: #121722;
  --log: #0b0e14;
  --log-text: #d5def6;
  --ok: #b7ebc9;
  --ok-line: #2d5a40;
  --error: #ffb4b4;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
}

main {
  width: calc(100% - 2rem);
  height: 100vh;
  margin: 0 auto;
  padding: 0.8rem 0 0.6rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.6rem;
}

body.device-view #panel-device,
body.device-view #journal,
body.device-view #support,
body.device-view #error {
  width: min(760px, 100%);
  margin-inline: auto;
}

.header-side {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.eyebrow {
  margin: 0;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.72rem;
  color: var(--muted);
}

h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 620;
  letter-spacing: -0.04em;
}

.brand-logo {
  display: block;
  height: 4.2rem;
  width: auto;
}

:root[data-theme="dark"] .brand-logo {
  filter: invert(1);
}

h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 620;
}

.pill {
  margin: 0;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.85rem;
}

.pill.on {
  color: var(--ok);
  border-color: var(--ok-line);
}

.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#panel-screens,
#panel-library,
#panel-device,
#panel-relay {
  flex: 1;
  min-height: 0;
}

#panel-screens:not([hidden]),
#panel-library:not([hidden]) {
  display: flex;
}

#panel-device:not([hidden]) {
  overflow: auto;
}

#panel-relay:not([hidden]) {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  min-height: 0;
  overflow: auto;
}

#panel-relay .relay-editor {
  flex: 1;
  min-height: 14rem;
  resize: none;
}

.relay-count {
  font-size: 2rem;
  color: var(--text);
  margin: 0;
}

.tabs {
  display: flex;
  gap: 0.4rem;
  margin: 0;
}

.tab {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--line);
  font-weight: 500;
}

.tab.on {
  color: var(--on-accent);
  border-color: transparent;
  background: var(--accent);
}

.row, .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: end;
}

.relay-actions {
  align-items: center;
}

.relay-actions .hint {
  margin: 0;
  line-height: 1;
}

.spread { justify-content: space-between; align-items: center; margin-bottom: 0.8rem; }

label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  color: var(--muted);
  font-size: 0.82rem;
}

select, input[type="text"], input[type="search"], input[type="password"], textarea, button {
  font: inherit;
  color: var(--text);
  background: var(--field);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.7rem;
}

textarea {
  width: 100%;
  min-height: 5.5rem;
  resize: vertical;
}

textarea.masked {
  -webkit-text-security: disc;
  text-security: disc;
}

button {
  background: var(--accent);
  border-color: transparent;
  color: var(--on-accent);
  font-weight: 650;
  cursor: pointer;
}

button.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--line);
  font-weight: 500;
}

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

.hint, #support { color: var(--muted); font-size: 0.9rem; }
code {
  color: var(--text);
  background: var(--code);
  padding: 0.05rem 0.3rem;
  border-radius: 6px;
}

.screen-list, .fn-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.7rem 0;
}

.chip { font-weight: 550; }
.chip.on { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.chip.square {
  width: 2.45rem;
  height: 2.45rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.chip.missing { opacity: 0.28; }

button.mini {
  padding: 0.28rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 550;
}

.card {
  position: relative;
  display: grid;
  gap: 0.7rem;
  padding: 0.9rem;
  margin-top: 0.7rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.card > button.trash {
  position: absolute;
  top: 0.3rem;
  right: 0.3rem;
}

.card-head {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.8rem;
  align-items: center;
  padding-right: 1.4rem;
}

input:disabled {
  opacity: 0.35;
}

.card input[type="color"] {
  width: 46px;
  height: 46px;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
}

.fn-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 0.7rem;
  align-items: end;
}

.fn-grid > button.open-fn {
  box-sizing: border-box;
  align-self: end;
  width: 2.15rem;
  height: auto;
  margin: 0;
  padding: 0.5rem 0;
  font-size: 0.82rem;
  line-height: 1.2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.fn-grid > button.open-fn svg {
  width: 1.15rem;
  height: 1.15rem;
  display: block;
}

.grid-2 label, .fn-grid label, .stack { min-width: 0; }
.grid-2 input, .grid-2 select, .grid-2 form,
.fn-grid input, .fn-grid select, .fn-grid form { width: 100%; }
.grid-2 form, .fn-grid form { margin: 0; }

.learn img {
  display: block;
  max-width: 100%;
  margin-top: 0.6rem;
  border-radius: 10px;
  cursor: crosshair;
  background: #000;
}

.learn img[hidden] { display: none; }

.error { color: var(--error); }

details { margin-top: 1.2rem; color: var(--muted); }
.log-tools {
  display: flex;
  gap: 0.45rem;
  margin: 0.85rem 0 0.45rem;
}
#log {
  min-height: 7rem;
  margin: 0.6rem 0 0;
  padding: 0.8rem;
  border-radius: 12px;
  background: var(--log);
  color: var(--log-text);
  overflow: auto;
  white-space: pre-wrap;
}

.screens-layout,
.lib-layout {
  display: grid;
  grid-template-columns: minmax(210px, 280px) minmax(0, 1fr);
  flex: 1;
  width: 100%;
  min-height: 0;
}

body.nav-collapsed .screens-layout,
body.nav-collapsed .lib-layout {
  grid-template-columns: 2.15rem minmax(0, 1fr);
}

.nav-col,
.pane {
  min-height: 0;
  overflow: auto;
}

.nav-col {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  padding: 0.55rem 0.7rem 0.8rem 0.15rem;
  margin-right: 0.8rem;
  background: var(--card);
  border-right: 3px solid var(--line);
}

.nav-col.collapsed {
  overflow: hidden;
  margin-right: 0;
  padding-right: 0.25rem;
  padding-left: 0.35rem;
}

.nav-col.collapsed > :not(.nav-toggle) { display: none; }

button.nav-toggle {
  position: sticky;
  top: 0;
  z-index: 1;
  align-self: flex-start;
  width: 1.15rem;
  height: 1.15rem;
  margin: 0 0 0.15rem;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
}

button.nav-toggle svg,
button.twist svg,
button.trash svg {
  width: 0.95rem;
  height: 0.95rem;
  display: block;
}

button.nav-toggle svg { transform: rotate(90deg); }
body.nav-collapsed button.nav-toggle svg { transform: rotate(-90deg); }

button.twist {
  width: 1.15rem;
  height: 1.15rem;
  flex: 0 0 auto;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.twist.closed svg { transform: rotate(-90deg); }

button.trash {
  width: 1.7rem;
  height: 1.7rem;
  padding: 0.2rem;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.trash:hover { color: var(--error); border-color: var(--line); }

input[type="search"] {
  appearance: none;
  -webkit-appearance: none;
}

input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button {
  -webkit-appearance: none;
}

.fn-search { width: 100%; }

.label-row {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.label-row input[type="text"] { flex: 1; min-width: 0; }
.label-row .check { flex: 0 0 auto; white-space: nowrap; }

.pane { padding: 0.15rem 0.15rem 1rem 0.35rem; }

.screen-map { gap: 0.35rem; }

.map-screen.drop,
.map-btn.drop { outline: 2px solid var(--accent); }

button.handle {
  background: transparent;
  border: 0;
  color: var(--muted);
  padding: 0.15rem 0;
}

button.map-label,
input.map-title {
  background: transparent;
  border: 0;
  padding: 0.1rem 0;
  text-align: left;
}

button.map-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
}

input.map-title {
  flex: 1;
  min-width: 0;
  color: var(--text);
  font-weight: 650;
  padding: 0.2rem 0.15rem;
}

.map-btn.on button.map-label {
  color: var(--text);
  font-weight: 650;
}

.map-screen {
  padding: 0.35rem 0.45rem 0.45rem;
  border: 1px solid transparent;
  border-radius: 12px;
}

.map-screen.on {
  border-color: var(--line);
  background: var(--card);
}

.map-screen.slot {
  opacity: 0.55;
}

.map-screen.far {
  opacity: 0.28;
}

.map-head, .map-btn {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 0;
}

.map-title {
  background: transparent;
  border: 0;
  color: var(--text);
  font-weight: 650;
  padding: 0.2rem 0.15rem;
  text-align: left;
}

.map-title:disabled {
  color: var(--muted);
  font-weight: 550;
}

.handle {
  width: 1.15rem;
  flex: 0 0 auto;
  padding: 0.15rem 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: grab;
  letter-spacing: -0.12em;
  font-weight: 700;
}

.handle:active { cursor: grabbing; }

.map-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  margin: 0.1rem 0 0 0.2rem;
}

.map-label {
  color: var(--muted);
  font-size: 0.78rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drop-gap {
  height: 0.65rem;
  margin: 0.12rem 0.2rem;
  border-radius: 99px;
  background: var(--accent);
}

.map-buttons .drop-gap {
  height: 0.45rem;
  margin-left: 1.15rem;
}

.map-btn.lifting,
.map-screen.lifting {
  height: 0;
  min-height: 0;
  padding: 0;
  margin: 0;
  overflow: hidden;
  opacity: 0;
  border-width: 0;
  pointer-events: none;
}

button.icon {
  width: 2.1rem;
  height: 2.1rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

button.icon svg {
  width: 1rem;
  height: 1rem;
  display: block;
}

.seq-block { display: grid; gap: 0.3rem; }

.seq-builder { display: grid; gap: 0.35rem; }

.seq-toggle {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}

.seq-chevron {
  display: inline-block;
  width: 0.7rem;
  font-size: 1rem;
  line-height: 1;
  transition: transform 0.15s ease;
}

.seq-toggle[aria-expanded="true"] .seq-chevron { transform: rotate(90deg); }

.seq-tools[hidden] { display: none; }

.seq-tools {
  display: grid;
  gap: 0.45rem;
  padding: 0.55rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--field);
}

.seq-group { display: grid; gap: 0.25rem; }

.seq-group > span,
.seq-presets {
  color: var(--muted);
  font-size: 0.75rem;
}

.seq-chips,
.seq-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
}

button.seq-chip {
  padding: 0.28rem 0.5rem;
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--line);
  font-size: 0.78rem;
  font-weight: 600;
}

#seq-ask .row { justify-content: flex-end; }

#seq-ask-fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
  gap: 0.6rem;
}

.seq-head {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  font-size: 0.82rem;
}

button.info {
  width: 1.25rem;
  height: 1.25rem;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-size: 0.72rem;
  font-style: italic;
  font-weight: 700;
  line-height: 1;
}

label.check {
  flex-direction: row;
  align-items: center;
  gap: 0.45rem;
  color: var(--text);
  font-size: 0.85rem;
}

dialog {
  width: min(640px, calc(100% - 2rem));
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1.1rem 1.2rem 1rem;
}

dialog::backdrop { background: rgba(0, 0, 0, 0.55); }

dialog h2 { margin-bottom: 0.6rem; }

dialog p, dialog li { color: var(--muted); font-size: 0.92rem; }

dialog ul { padding-left: 1.1rem; display: grid; gap: 0.45rem; }

dialog form { display: grid; gap: 0.7rem; }

#relay-arm p { margin: 0; }

#relay-setup {
  display: grid;
  gap: 1.35rem;
  margin-top: 0.25rem;
}

#relay-ticking {
  display: grid;
  gap: 1.8rem;
  justify-items: center;
}

#relay-setup[hidden],
#relay-ticking[hidden] {
  display: none;
}

dialog button { justify-self: end; }

@media (max-width: 800px) {
  .screens-layout,
  .lib-layout,
  body.nav-collapsed .screens-layout,
  body.nav-collapsed .lib-layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(8rem, 38%) minmax(0, 1fr);
  }
  .nav-col { margin-right: 0; border-right: 0; border-bottom: 3px solid var(--line); }
}

@media (max-width: 700px) {
  .grid-2 { grid-template-columns: 1fr; }
  .fn-grid { grid-template-columns: minmax(0, 1fr) auto; }
  .fn-grid > label:last-child { grid-column: 1 / -1; }
  .fn-grid > button.open-fn { grid-column: 2; grid-row: 1; }
}
