:root {
  --bg: #efece4;
  --surface: #f8f6f0;
  --stage-bg: #e5e1d6;
  --ink: #171614;
  --muted: #666158;
  --line: #d4cec1;
  --line-strong: #b6afa0;
  --accent: #d9431f;
  --accent-hover: #bf3615;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Monaco, Consolas, monospace;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Helvetica, sans-serif;
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
}

/* Top instrument bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

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

.brand-mark {
  width: 24px;
  height: 24px;
  border: 1px solid var(--ink);
  background-image:
    linear-gradient(45deg, var(--ink) 25%, transparent 25%),
    linear-gradient(-45deg, var(--ink) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--ink) 75%),
    linear-gradient(-45deg, transparent 75%, var(--ink) 75%);
  background-size: 8px 8px;
  background-position: 0 0, 0 4px, 4px -4px, -4px 0;
}

.brand h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.subtitle {
  margin: 2px 0 0;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

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

.live-strip {
  width: 184px;
  height: 33px;
  padding: 2px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 120ms ease;
}

.live-strip:hover {
  border-color: var(--ink);
}

#liveRibbon {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.select-control {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  padding: 8px 10px;
  cursor: pointer;
  outline: none;
}

.select-control:hover {
  border-color: var(--ink);
}

/* Main workspace grid */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  min-height: calc(100vh - 65px);
}

.stage-wrap {
  display: flex;
  flex-direction: column;
  padding: 22px;
  gap: 14px;
  position: relative;
}

.stage-frame {
  position: relative;
  flex: 1;
  min-height: 420px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  background-color: var(--stage-bg);
  background-image: radial-gradient(rgba(23, 22, 20, 0.14) 1px, transparent 1px);
  background-size: 18px 18px;
  border: 1px solid var(--line);
  overflow: hidden;
}

/* Corner registration marks */
.reg {
  position: absolute;
  width: 12px;
  height: 12px;
  pointer-events: none;
}

.reg-tl { top: 10px; left: 10px; border-top: 1px solid var(--muted); border-left: 1px solid var(--muted); }
.reg-tr { top: 10px; right: 10px; border-top: 1px solid var(--muted); border-right: 1px solid var(--muted); }
.reg-bl { bottom: 10px; left: 10px; border-bottom: 1px solid var(--muted); border-left: 1px solid var(--muted); }
.reg-br { bottom: 10px; right: 10px; border-bottom: 1px solid var(--muted); border-right: 1px solid var(--muted); }

#outputCanvas {
  max-width: 100%;
  max-height: calc(100vh - 190px);
  width: auto;
  height: auto;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  box-shadow: 0 10px 30px rgba(23, 22, 20, 0.14);
  border: 1px solid rgba(23, 22, 20, 0.18);
  background: #fff;
}

.drop-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(239, 236, 228, 0.9);
  border: 2px dashed var(--accent);
  z-index: 10;
}

.stage-wrap.drag-active .drop-overlay {
  display: flex;
}

.drop-card {
  font-family: var(--mono);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 14px 20px;
  background: var(--surface);
  border: 1px solid var(--ink);
}

/* Telemetry bar */
.telemetry {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  padding: 2px 2px 0;
}

.telemetry .sep {
  opacity: 0.5;
}

/* Right control deck */
.deck {
  background: var(--surface);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.deck-section {
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 12px;
}

.section-head h2 {
  margin: 0;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink);
}

.section-code {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
}

/* Algorithm segmented grid */
.algo-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.seg {
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 11.5px;
  padding: 9px 10px;
  text-align: left;
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease, color 120ms ease;
}

.seg:hover {
  border-color: var(--ink);
}

.seg.active {
  background: var(--ink);
  color: var(--surface);
  border-color: var(--ink);
}

/* Palettes */
.palette-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 8px;
}

.pal {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 11px;
  padding: 8px 9px;
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease;
}

.pal:hover {
  border-color: var(--ink);
}

.pal.active {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.swatch {
  display: inline-flex;
  border: 1px solid rgba(23, 22, 20, 0.25);
  flex-shrink: 0;
}

.swatch i {
  display: block;
  width: 9px;
  height: 14px;
}

.custom-ink {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 6px;
}

.pal-custom {
  flex: 1;
  justify-content: center;
}

.color-pickers {
  display: flex;
  gap: 6px;
}

.color-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
  padding: 4px 6px;
  border: 1px solid var(--line);
  background: var(--bg);
  cursor: pointer;
}

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

/* Sliders */
.slider-row {
  margin-bottom: 13px;
}

.slider-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 5px;
  font-size: 12px;
}

.slider-meta label {
  color: var(--ink);
}

.slider-meta output {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 2px;
  background: var(--line-strong);
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 12px;
  height: 16px;
  background: var(--ink);
  border: 1px solid var(--surface);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 16px;
  border-radius: 0;
  background: var(--ink);
  border: 1px solid var(--surface);
  cursor: pointer;
}

.toggle-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink);
  cursor: pointer;
  user-select: none;
}

.toggle input {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 11.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 9px 14px;
  border: 1px solid var(--ink);
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
  user-select: none;
}

.btn-primary {
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: 600;
  padding: 12px 16px;
}

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

.btn-secondary {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line-strong);
}

.btn-secondary:hover {
  border-color: var(--ink);
  background: var(--bg);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--line);
}

.btn-ghost:hover {
  color: var(--ink);
  border-color: var(--ink);
}

.deck-footer {
  margin-top: auto;
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.footer-sub {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* Responsive */
@media (max-width: 900px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .deck {
    border-left: none;
    border-top: 1px solid var(--line);
  }

  .stage-frame {
    min-height: 320px;
  }
}
