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

:root {
  --bg: #000; --fg: #fff; --dim: #888; --border: #333;
  --surface: #111;
  --green: #0f0; --red: #f00; --orange: #f80;
  --mono: "SF Mono", "Cascadia Code", "Fira Code", Consolas, monospace;
  --toolbar-h: 52px;
}

body { font-family: var(--mono); background: var(--bg); color: var(--fg); line-height: 1.5; min-height: 100vh; font-size: 13px; }
body.modal-open { overflow: hidden; }
input, select, button, textarea { font-family: inherit; background: var(--bg); color: var(--fg); border: 1px solid var(--border); outline: none; }
input:focus, select:focus { border-color: var(--fg); }
collapsible-card { display: block; }

/* ==========================================================================
   Sticky Toolbar
   ========================================================================== */

.toolbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  height: var(--toolbar-h);
  padding: 0 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

.toolbar-left {
  flex-shrink: 0;
}

.toolbar-left h1 {
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  white-space: nowrap;
}

.toolbar-left h1 a { color: var(--fg); text-decoration: none; }
.toolbar-left h1 a:hover { color: var(--dim); }
.toolbar-left h1 span { color: var(--dim); font-weight: 400; }

.toolbar-center {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.toolbar-status {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--dim);
  white-space: nowrap;
}

.toolbar-status.unsupported {
  color: var(--red);
}

.toolbar-ref-btn {
  font-size: 11px;
  padding: 4px 12px;
}

/* Connect button: bright green when disconnected (action needed) */
.btn-connect {
  background: #0a0; color: #000;
  border-color: #0a0;
  font-weight: 700;
}
.btn-connect:not(:disabled):hover {
  background: #0f0; color: #000;
  border-color: #0f0;
}
.btn-connect:disabled { opacity: 0.3; cursor: not-allowed; }

/* Once connected, the button becomes a subtle disconnect button */
.btn-connect:not(.btn-connect-active) {
  background: var(--bg); color: var(--dim);
  border-color: var(--border);
}
.btn-connect:not(.btn-connect-active):not(:disabled):hover {
  background: var(--surface); color: var(--fg);
}

/* Save button: muted when disabled, bright orange when active (pending changes) */
.btn-save {
  background: var(--bg); color: var(--dim);
  border-color: var(--border);
  font-weight: 700;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.btn-save:disabled {
  opacity: 0.3; cursor: not-allowed;
}
.btn-save.btn-save-active {
  background: var(--orange); color: #000;
  border-color: var(--orange);
  opacity: 1;
}
.btn-save.btn-save-active:not(:disabled):hover {
  background: #fa0; color: #000;
  border-color: #fa0;
}
.btn-save.btn-save-active:disabled {
  opacity: 0.5; cursor: not-allowed;
}

/* ==========================================================================
   Layout (single column, full width)
   ========================================================================== */

.layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding-top: var(--toolbar-h);
}

.layout-main {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.layout-main > config-panel {
  flex: 1 1 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 20px 40px;
  scroll-behavior: smooth;
}

/* ==========================================================================
   Buttons
   ========================================================================== */

.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 14px;
  font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em;
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--fg); color: var(--bg); }
a.btn { text-decoration: none; }
.btn:disabled { opacity: 0.3; cursor: not-allowed; }
.btn:disabled:hover { background: var(--bg); color: var(--fg); }
.btn-primary { background: var(--fg); color: var(--bg); }
.btn-primary:hover { background: var(--dim); }
.btn-primary:disabled:hover { background: var(--fg); color: var(--bg); }
.btn-sm { padding: 3px 10px; font-size: 11px; }

/* ==========================================================================
   Cards
   ========================================================================== */

.card { border: 1px solid var(--border); margin-bottom: 20px; }
.card-header { display: flex; align-items: center; justify-content: space-between; padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer; user-select: none; }
.card-header:hover { background: var(--surface); }
.card-header h2 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; display: flex; align-items: center; gap: 8px; }
.card-header .badge { font-size: 11px; padding: 1px 6px; border: 1px solid var(--border); color: var(--dim); }
.card-header .chevron { font-size: 12px; color: var(--dim); transition: transform 0.15s; }
.card-header.collapsed .chevron { transform: rotate(-90deg); }
.card-body { padding: 16px; }
.card-body.collapsed { display: none; }

/* ==========================================================================
   Fields
   ========================================================================== */

.field { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field label { color: var(--dim); min-width: 100px; flex-shrink: 0; }
.field input, .field select { padding: 5px 8px; font-size: 13px; width: 80px; }
.field input[type="number"] { -moz-appearance: textfield; }
.field input::-webkit-outer-spin-button, .field input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* ==========================================================================
   Item Rows
   ========================================================================== */

.item-row { display: flex; align-items: center; gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; margin-bottom: 6px; }
.item-row:last-of-type { border-bottom: none; margin-bottom: 0; }
.item-row .index { font-size: 11px; color: var(--dim); min-width: 20px; }
.item-row input { padding: 4px 8px; font-size: 13px; width: 64px; }
.item-row label { font-size: 11px; color: var(--dim); min-width: 28px; }
.pin-label { font-size: 11px; color: var(--dim); min-width: 32px; flex-shrink: 0; }
.pin-select { padding: 3px 6px; font-size: 11px; min-width: 56px; cursor: pointer; }
.pin-select option:disabled { color: #555; }

.btn-remove { border-color: #500; color: #888; padding: 2px 8px; font-size: 13px; line-height: 1; margin-left: auto; flex-shrink: 0; }
.btn-remove:hover { background: var(--red); color: var(--bg); border-color: var(--red); }
.add-item-btn { margin-top: 10px; }

.expr-group { display: flex; align-items: center; gap: 8px; flex: 1 1 100%; min-width: 0; }
.expr-input { padding: 6px 10px; font-size: 13px; flex: 1 1 0; min-width: 120px; }
.expr-input.expr-invalid { border-color: var(--red); }
.expr-input::placeholder { color: #444; }
.expr-error { font-size: 10px; color: var(--red); max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex-shrink: 0; }

/* ==========================================================================
   Toggle Switch
   ========================================================================== */

.toggle-row { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.toggle-row label { color: var(--dim); }
.toggle { position: relative; width: 36px; height: 20px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .slider { position: absolute; inset: 0; background: #222; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; transition: background 0.15s; }
.toggle .slider::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; background: var(--dim); border-radius: 50%; transition: transform 0.15s, background 0.15s; }
.toggle input:checked + .slider { background: var(--fg); border-color: var(--fg); }
.toggle input:checked + .slider::after { transform: translateX(16px); background: var(--bg); }

.note-hint { font-size: 11px; color: var(--dim); min-width: 32px; }

/* ==========================================================================
   Toast Notification
   ========================================================================== */

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(80px); padding: 10px 20px; font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; opacity: 0; transition: transform 0.2s, opacity 0.2s; z-index: 300; pointer-events: none; }
.toast.visible { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.success { background: var(--green); color: var(--bg); }
.toast.error { background: var(--red); color: var(--bg); }
.toast.info { background: var(--fg); color: var(--bg); }

/* ==========================================================================
   Monitor Elements
   ========================================================================== */

.monitor-row { display: flex; align-items: center; gap: 10px; height: 28px; margin-top: 6px; }
.monitor-dot { width: 16px; height: 16px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface); flex-shrink: 0; }
.monitor-indicator { transition: background 0.08s, border-color 0.08s; }
.monitor-indicator.active { background: var(--green); border-color: var(--green); }
.monitor-bar-track { flex: 1; height: 6px; background: var(--surface); border: 1px solid var(--border); overflow: hidden; min-width: 60px; }
.monitor-bar-fill { height: 100%; width: 0%; background: var(--fg); transition: width 0.06s linear; }
.monitor-value { font-size: 12px; color: var(--fg); min-width: 28px; text-align: right; flex-shrink: 0; }
.monitor-tap { transition: background 0.06s, border-color 0.06s; }
.monitor-tap.active { background: var(--orange); border-color: var(--orange); }
.monitor-label { font-size: 12px; color: var(--dim); min-width: 56px; flex-shrink: 0; }

/* ==========================================================================
   Landing Page
   ========================================================================== */

.container { max-width: 860px; margin: 0 auto; padding: 24px 16px 64px; }

.landing-intro { color: var(--dim); margin-bottom: 32px; line-height: 1.7; }
.landing-intro a { color: var(--fg); }
.landing-intro a:hover { color: var(--dim); }
.landing-section { margin-bottom: 32px; }
.landing-section h2 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 16px; }
.landing-steps { padding-left: 20px; color: var(--dim); }
.landing-steps li { margin-bottom: 16px; line-height: 1.7; }
.landing-steps li:last-child { margin-bottom: 0; }
.landing-steps strong { color: var(--fg); }
.landing-steps ul { list-style: disc; padding-left: 18px; margin-top: 6px; }
.landing-steps code { color: var(--fg); }
.landing-steps a { color: var(--fg); }
.landing-steps a:hover { color: var(--dim); }
.landing-cta { display: flex; gap: 10px; padding-top: 8px; }

/* Download section */
.download-list { color: var(--dim); }
.download-loading { color: var(--dim); }
.download-error { color: var(--red); }
.download-error a { color: var(--fg); }
.download-error a:hover { color: var(--dim); }
.download-version { margin-bottom: 12px; }
.download-version a { color: var(--fg); }
.download-version a:hover { color: var(--dim); }
.download-items { display: flex; gap: 10px; flex-wrap: wrap; }
.download-item { display: flex; flex-direction: column; gap: 4px; padding: 12px 20px; border: 1px solid var(--border); text-decoration: none; color: var(--fg); min-width: 200px; }
.download-item:hover { background: var(--fg); color: var(--bg); }
.download-item:hover .download-desc { color: var(--bg); }
.download-name { font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; }
.download-desc { font-size: 11px; color: var(--dim); }

/* ==========================================================================
   Reference Sections (shared by expr-ref and pinout wiring ref)
   ========================================================================== */

.ref-section p { color: var(--dim); margin-bottom: 12px; line-height: 1.7; }
.ref-section strong { color: var(--fg); }
.ref-section h3 { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; margin: 16px 0 8px; color: var(--fg); }
.ref-section h3:first-child { margin-top: 0; }
.ref-section table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.ref-section th { text-align: left; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--dim); padding: 4px 10px 4px 0; border-bottom: 1px solid var(--border); }
.ref-section td { font-size: 12px; padding: 4px 10px 4px 0; border-bottom: 1px solid var(--border); color: var(--dim); }

/* Expression reference specifics */
.expr-ref td code, .expr-ref p code { color: var(--fg); }
.expr-ref .expr-examples { list-style: none; margin-bottom: 12px; }
.expr-ref .expr-examples li { padding: 3px 0; color: var(--dim); font-size: 12px; }
.expr-ref .expr-examples code { color: var(--fg); }
.expr-ref .expr-notes { list-style: disc; padding-left: 18px; color: var(--dim); font-size: 12px; }
.expr-ref .expr-notes li { margin-bottom: 4px; line-height: 1.6; }
.expr-ref .expr-notes code { color: var(--fg); }

/* Pinout wiring reference (below SVG diagram) */
.pinout-wiring-ref { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 12px; }
.pinout-wiring-ref h3 { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; margin: 16px 0 8px; color: var(--fg); }
.pinout-wiring-ref h3:first-child { margin-top: 0; }
.pinout-wiring-ref strong { color: var(--fg); }

.wiring-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.wiring-table th { text-align: left; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--dim); padding: 6px 10px 6px 0; border-bottom: 1px solid var(--border); }
.wiring-table td { font-size: 12px; padding: 6px 10px 6px 0; border-bottom: 1px solid var(--border); color: var(--dim); line-height: 1.5; vertical-align: top; }
.wiring-table td:first-child { white-space: nowrap; font-weight: 700; }
.wiring-table td:nth-child(3) { white-space: nowrap; }

.wiring-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: middle; flex-shrink: 0; }

/* ==========================================================================
   Project Actions (inside Settings card)
   ========================================================================== */

.project-actions-inline { border-top: 1px solid var(--border); margin-top: 16px; padding-top: 16px; }
.project-actions-inline h3 { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--dim); margin-bottom: 10px; }
.project-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ==========================================================================
   Disabled config panel
   ========================================================================== */

config-panel.disabled input,
config-panel.disabled select,
config-panel.disabled .toggle { opacity: 0.35; pointer-events: none; }
config-panel.disabled .project-actions-inline { opacity: 0.35; pointer-events: none; }
config-panel.disabled .monitor-indicator,
config-panel.disabled .monitor-bar-track,
config-panel.disabled .monitor-value,
config-panel.disabled .monitor-tap { opacity: 0.35; }

/* ==========================================================================
   Pinout Guide
   ========================================================================== */

.pinout-guide-body { display: flex; flex-direction: column; align-items: center; }
.pinout-legend { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 12px; justify-content: center; }
.pinout-legend-item { display: inline-flex; align-items: center; gap: 5px; font-size: 11px; color: var(--dim); }
.pinout-legend-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ==========================================================================
   Modal
   ========================================================================== */

ref-modal {
  display: none;
}

ref-modal.open {
  display: block;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal-dialog {
  background: var(--bg);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 640px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.modal-close {
  border-color: var(--border);
  color: var(--dim);
  padding: 2px 8px;
  font-size: 15px;
  line-height: 1;
}

.modal-close:hover {
  background: var(--fg);
  color: var(--bg);
}

.modal-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
}

/* Pinout modal needs extra width for the wiring table */
#modalPinout .modal-dialog {
  max-width: 760px;
}

/* ==========================================================================
   Responsive
   ========================================================================== */

@media (max-width: 768px) {
  .toolbar {
    flex-wrap: wrap;
    height: auto;
    padding: 8px 12px;
    gap: 8px;
  }

  .toolbar-left h1 {
    font-size: 12px;
  }

  .toolbar-center {
    order: 3;
    margin-left: 0;
    flex-basis: 100%;
    justify-content: center;
  }

  .toolbar-right {
    margin-left: auto;
  }

  .toolbar-ref-btn {
    font-size: 10px;
    padding: 3px 8px;
  }

  /* Toolbar wraps to two rows on mobile; use JS-free approach
     with a spacer that matches the toolbar's rendered height */
  .layout {
    padding-top: 0;
  }

  .layout-main {
    /* Extra top padding to clear the two-row toolbar */
    padding-top: calc(var(--toolbar-h) + 44px);
  }

  .modal-dialog {
    max-width: 100%;
    max-height: 90vh;
  }
}

@media (max-width: 480px) {
  .toolbar-status { display: none; }
  .toolbar-left h1 span { display: none; }
}
