/* ================================================================
   m-box Logic Builder — Design System
   ================================================================ */

:root {
    /* Colour palette — dark industrial theme */
    --bg-base: #0d1117;
    --bg-surface: #161b22;
    --bg-elevated: #1c2333;
    --bg-hover: #232d3f;
    --bg-active: #2a3a52;

    --border-subtle: #21262d;
    --border-default: #30363d;
    --border-accent: rgba(88, 166, 255, 0.25);

    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;
    --text-link: #58a6ff;

    --accent-blue: #58a6ff;
    --accent-green: #3fb950;
    --accent-orange: #d29922;
    --accent-red: #f85149;
    --accent-purple: #bc8cff;

    --gradient-accent: linear-gradient(135deg, #58a6ff 0%, #bc8cff 100%);
    --gradient-surface: linear-gradient(180deg, #1c2333 0%, #161b22 100%);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
    --shadow-glow: 0 0 20px rgba(88, 166, 255, 0.15);

    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;

    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    --transition: 150ms cubic-bezier(0.4, 0, 0.2, 1);
}

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

/* ---- Light theme ---- */
[data-theme="light"] {
    --bg-base: #f6f8fa;
    --bg-surface: #ffffff;
    --bg-elevated: #f0f2f5;
    --bg-hover: #e8ecf0;
    --bg-active: #dce3eb;

    --border-subtle: #d8dee4;
    --border-default: #c9d1d9;
    --border-accent: rgba(9, 105, 218, 0.3);

    --text-primary: #1f2328;
    --text-secondary: #57606a;
    --text-muted: #8b949e;
    --text-link: #0969da;

    --accent-blue: #0969da;
    --accent-green: #1a7f37;
    --accent-orange: #bf8700;
    --accent-red: #cf222e;
    --accent-purple: #8250df;

    --gradient-accent: linear-gradient(135deg, #0969da 0%, #8250df 100%);
    --gradient-surface: linear-gradient(180deg, #ffffff 0%, #f6f8fa 100%);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
    --shadow-glow: 0 0 12px rgba(9, 105, 218, 0.12);
}

/* Theme toggle button */
.theme-toggle {
    font-size: 16px;
    padding: 4px 8px;
}

html,
body {
    height: 100%;
    font-family: var(--font-sans);
    background: var(--bg-base);
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.5;
    overflow: hidden;
}

/* ================================================================
   Header
   ================================================================ */

#app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 56px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-subtle);
    z-index: 100;
}

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

.logo {
    color: var(--accent-blue);
    display: flex;
    align-items: center;
}

.brand-logo {
    height: 24px;
    width: auto;
    opacity: 0.5;
    transition: opacity 0.2s;
}

.brand-logo:hover {
    opacity: 0.8;
}

[data-theme="light"] .brand-logo {
    filter: brightness(0) saturate(100%) invert(13%) sepia(60%) saturate(1200%) hue-rotate(178deg) brightness(95%) contrast(95%);
}

.header-left h1 {
    font-size: 16px;
    font-weight: 600;
    letter-spacing: -0.3px;
}

.sequence-name-editable {
    display: block;
    font-size: 12px;
    color: var(--text-secondary);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 3px;
    padding: 1px 4px;
    margin-top: 1px;
    font-family: var(--font-sans);
    width: 200px;
    transition: all var(--transition);
}

.sequence-name-editable:hover {
    border-color: var(--border-default);
    color: var(--text-primary);
}

.sequence-name-editable:focus {
    outline: none;
    border-color: var(--accent-blue);
    color: var(--text-primary);
    background: var(--bg-elevated);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

/* Connection panel */
.connection-panel {
    display: flex;
    align-items: center;
    gap: 10px;
}

.conn-mode-toggle {
    display: flex;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.conn-mode-btn {
    padding: 4px 10px;
    font-size: 12px;
    font-family: var(--font-sans);
    font-weight: 500;
    background: var(--bg-elevated);
    color: var(--text-secondary);
    border: none;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

.conn-mode-btn:first-child {
    border-right: 1px solid var(--border-default);
}

.conn-mode-btn:hover {
    color: var(--text-primary);
}

.conn-mode-btn.active {
    background: var(--accent-blue);
    color: #fff;
}

.conn-section {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--text-secondary);
}

.conn-ip-input {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font-sans);
    width: 140px;
}

.conn-status {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
}

.conn-status.connected {
    color: var(--accent-green);
    font-weight: 500;
}

.conn-status.error {
    color: var(--accent-red);
}

.conn-section select {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font-sans);
}

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

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

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

.btn:active {
    transform: scale(0.97);
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
}

.btn:disabled:hover {
    background: var(--bg-elevated);
    border-color: var(--border-default);
    transform: none;
    box-shadow: none;
}

.btn-sm {
    padding: 4px 10px;
    font-size: 12px;
}

.btn-primary {
    background: var(--accent-blue);
    border-color: var(--accent-blue);
    color: #fff;
}

.btn-primary:hover {
    background: #4d96e8;
    border-color: #4d96e8;
}

.btn-accent {
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-purple));
    border: none;
    color: #fff;
}

.btn-accent:hover {
    opacity: 0.9;
    box-shadow: var(--shadow-glow);
}

.btn-deploy {
    background: linear-gradient(135deg, var(--accent-green), #2ea043);
    border: none;
    color: #fff;
    font-weight: 600;
}

.btn-deploy:hover:not(:disabled) {
    opacity: 0.9;
    box-shadow: 0 0 16px rgba(63, 185, 80, 0.3);
}

.btn-danger {
    border-color: var(--accent-red);
    color: var(--accent-red);
    background: transparent;
}

.btn-danger:hover {
    background: rgba(248, 81, 73, 0.1);
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

/* ================================================================
   Main Layout — 3-column
   ================================================================ */

#app-main {
    display: grid;
    grid-template-columns: 240px 1fr 280px;
    height: calc(100vh - 56px);
    overflow: hidden;
}

/* ================================================================
   Step List Panel (left sidebar)
   ================================================================ */

#step-list-panel {
    background: var(--bg-surface);
    border-right: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

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

.panel-header h2 {
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.step-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.step-list::-webkit-scrollbar {
    width: 6px;
}

.step-list::-webkit-scrollbar-track {
    background: transparent;
}

.step-list::-webkit-scrollbar-thumb {
    background: var(--border-default);
    border-radius: 3px;
}

.step-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all var(--transition);
    margin-bottom: 4px;
    border: 1px solid transparent;
    position: relative;
}

.step-item:hover {
    background: var(--bg-hover);
}

.step-item.active {
    background: var(--bg-active);
    border-color: var(--accent-blue);
    box-shadow: var(--shadow-glow);
}

.step-number {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: var(--bg-elevated);
    border: 2px solid var(--border-default);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    flex-shrink: 0;
    transition: all var(--transition);
}

.step-item.active .step-number {
    background: var(--accent-blue);
    border-color: var(--accent-blue);
    color: #fff;
}

.step-item-info {
    flex: 1;
    min-width: 0;
}

.step-item-name {
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.step-item-meta {
    font-size: 11px;
    color: var(--text-muted);
}

/* Connection line between steps */
.step-item::after {
    content: '';
    position: absolute;
    left: 20px;
    bottom: -6px;
    width: 2px;
    height: 8px;
    background: var(--border-default);
}

.step-item:last-child::after {
    display: none;
}

.panel-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.template-menu {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.template-menu label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.template-menu select {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 6px 8px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font-sans);
    width: 100%;
}

.file-actions {
    display: flex;
    gap: 4px;
}

/* ================================================================
   Resources Panel (variables / timers)
   ================================================================ */

.resources-panel {
    border-top: 1px solid var(--border-subtle);
    padding: 4px 8px;
}

.resource-details {
    border-radius: var(--radius-sm);
    margin-bottom: 2px;
}

.resource-details summary {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 8px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
    user-select: none;
    transition: all var(--transition);
}

.resource-details summary:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}

.res-count {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    padding: 0 6px;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 600;
    min-width: 18px;
    text-align: center;
    color: var(--text-muted);
}

.resource-list {
    padding: 2px 0 2px 8px;
}

.resource-row {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 3px 4px;
    border-radius: 4px;
    transition: background var(--transition);
}

.resource-row:hover {
    background: var(--bg-hover);
}

.res-idx {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--text-muted);
    min-width: 20px;
    text-align: right;
}

.res-name-input {
    flex: 1;
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-primary);
    font-size: 12px;
    font-family: var(--font-sans);
    padding: 2px 4px;
    border-radius: 3px;
    min-width: 0;
    transition: all var(--transition);
}

.res-name-input:hover {
    border-color: var(--border-default);
}

.res-name-input:focus {
    outline: none;
    border-color: var(--accent-blue);
    background: var(--bg-elevated);
}

.res-name-input::placeholder {
    color: var(--text-muted);
    font-style: italic;
}

.res-remove {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0 4px;
    font-size: 12px;
    opacity: 0;
    transition: all var(--transition);
}

.resource-row:hover .res-remove {
    opacity: 1;
}

.res-remove:hover {
    color: var(--accent-red);
}

.res-retain-btn {
    background: none;
    border: 1px solid var(--border-subtle);
    color: var(--text-muted);
    cursor: pointer;
    padding: 1px 5px;
    font-size: 10px;
    font-weight: 700;
    font-family: var(--font-mono);
    border-radius: var(--radius-sm);
    transition: all var(--transition);
    line-height: 1;
}

.res-retain-btn:hover {
    border-color: var(--border-accent);
    color: var(--text-secondary);
}

.res-retain-btn.active {
    background: rgba(227, 155, 35, 0.15);
    border-color: #e39b23;
    color: #e39b23;
}

.res-add-btn {
    width: 100%;
    justify-content: center;
    margin: 4px 0;
    font-size: 11px;
}

/* ================================================================
   Interlocks
   ================================================================ */

.interlock-details summary {
    color: var(--accent-red);
}

.interlock-badge {
    background: rgba(255, 80, 80, 0.15) !important;
    color: var(--accent-red) !important;
}

.interlock-row {
    padding: 6px 8px;
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--accent-red);
    margin-bottom: 4px;
    background: var(--bg-elevated);
    cursor: pointer;
    transition: all var(--transition);
}

.interlock-row:hover {
    background: var(--bg-hover);
}

.interlock-row.expanded {
    background: var(--bg-hover);
    border-left-color: var(--accent-orange);
}

.interlock-row.dragging {
    opacity: 0.4;
}

.interlock-row.drag-over {
    border-top: 2px solid var(--accent-blue);
}

.interlock-header {
    display: flex;
    align-items: center;
    gap: 6px;
}

.interlock-drag-handle {
    cursor: grab;
    color: var(--text-muted);
    font-size: 12px;
    user-select: none;
    opacity: 0.5;
    transition: opacity var(--transition);
}

.interlock-row:hover .interlock-drag-handle {
    opacity: 1;
}

.interlock-num {
    font-family: var(--font-mono);
    font-size: 10px;
    font-weight: 700;
    color: var(--accent-red);
    min-width: 14px;
}

.interlock-name {
    flex: 1;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.interlock-meta {
    font-size: 10px;
    color: var(--text-muted);
    padding: 2px 0 0 28px;
    font-family: var(--font-mono);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.interlock-editor {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border-subtle);
}

.interlock-editor .form-group {
    margin-bottom: 6px;
}

.interlock-editor label {
    font-size: 10px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 2px;
}

.interlock-editor select,
.interlock-editor input[type="text"],
.interlock-editor input[type="number"] {
    font-size: 12px;
    padding: 4px 6px;
}

.interlock-header .res-remove {
    opacity: 0;
}

.interlock-row:hover .interlock-header .res-remove {
    opacity: 1;
}

/* ================================================================
   Step Editor (center)
   ================================================================ */

#step-editor-panel {
    overflow-y: auto;
    padding: 24px 32px;
    background: var(--bg-base);
}

#step-editor-panel::-webkit-scrollbar {
    width: 8px;
}

#step-editor-panel::-webkit-scrollbar-track {
    background: transparent;
}

#step-editor-panel::-webkit-scrollbar-thumb {
    background: var(--border-default);
    border-radius: 4px;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
    text-align: center;
    gap: 16px;
}

.empty-icon {
    color: var(--text-muted);
    opacity: 0.5;
}

.empty-state h3 {
    font-size: 18px;
    color: var(--text-primary);
}

.empty-state p {
    max-width: 320px;
    font-size: 14px;
}

.editor-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
}

.step-name-input {
    font-size: 22px;
    font-weight: 600;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-primary);
    font-family: var(--font-sans);
    padding: 4px 0;
    width: 100%;
    max-width: 400px;
    transition: border-color var(--transition);
}

.step-name-input:focus {
    outline: none;
    border-bottom-color: var(--accent-blue);
}

.editor-sections {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.editor-section {
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

.section-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--gradient-surface);
    border-bottom: 1px solid var(--border-subtle);
}

.section-header h3 {
    font-size: 13px;
    font-weight: 600;
}

.section-hint {
    font-size: 12px;
    color: var(--text-muted);
    flex: 1;
}

.add-action-btn {
    margin-left: auto;
}

.action-list {
    padding: 8px;
    min-height: 48px;
}

.action-list:empty::after {
    content: 'No actions — click + Add';
    display: block;
    padding: 12px 16px;
    color: var(--text-muted);
    font-size: 13px;
    font-style: italic;
    text-align: center;
}

.action-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    margin-bottom: 4px;
    font-size: 13px;
    transition: all var(--transition);
    border: 1px solid transparent;
    cursor: pointer;
}

.action-item:hover {
    border-color: var(--accent-blue);
    background: var(--bg-hover);
}

.action-badge {
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.action-badge.output {
    background: rgba(88, 166, 255, 0.15);
    color: var(--accent-blue);
}

.action-badge.led {
    background: rgba(210, 153, 34, 0.15);
    color: var(--accent-orange);
}

.action-badge.timer {
    background: rgba(188, 140, 255, 0.15);
    color: var(--accent-purple);
}

.action-badge.variable {
    background: rgba(63, 185, 80, 0.15);
    color: var(--accent-green);
}

.action-desc {
    flex: 1;
}

.action-remove {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 14px;
    transition: all var(--transition);
}

.action-remove:hover {
    color: var(--accent-red);
    background: rgba(248, 81, 73, 0.1);
}

/* Hold condition */
.hold-editor {
    padding: 12px 16px;
}

.hold-form {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: end;
}

.hold-form .form-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.hold-form label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.hold-form select,
.hold-form input {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font-sans);
}

.hold-form input[type="number"] {
    width: 100px;
}

/* Compound conditions (AND/OR) */
.compound-condition-list {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 0;
    margin-top: 4px;
}

.compound-condition-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    background: var(--bg-base);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    flex-wrap: wrap;
}

.compound-type-select {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: 12px;
    font-family: var(--font-sans);
    min-width: 120px;
}

.compound-params {
    display: flex;
    gap: 8px;
    align-items: end;
    flex-wrap: wrap;
}

.compound-params .form-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.compound-params label {
    font-size: 10px;
}

.compound-params select,
.compound-params input {
    padding: 4px 8px;
    font-size: 12px;
}

.compound-params input[type="number"] {
    width: 80px;
}

.compound-condition-row .res-remove {
    opacity: 1;
    margin-left: auto;
}

.compound-operator-label {
    text-align: center;
    font-size: 10px;
    font-weight: 700;
    font-family: var(--font-mono);
    color: var(--accent-blue);
    padding: 2px 0;
    letter-spacing: 1px;
}

.transition-editor {
    padding: 12px 16px;
}

.transition-editor .hold-form {
    margin-bottom: 8px;
}

.transition-branch {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    margin-top: 8px;
    font-size: 13px;
}

.transition-branch-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    padding: 2px 8px;
    border-radius: 4px;
    white-space: nowrap;
}

.branch-true {
    background: rgba(63, 185, 80, 0.15);
    color: var(--accent-green);
}

.branch-false {
    background: rgba(248, 81, 73, 0.15);
    color: var(--accent-red);
}

.branch-always {
    background: rgba(88, 166, 255, 0.15);
    color: var(--accent-blue);
}

.transition-branch select {
    flex: 1;
    background: var(--bg-base);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 4px 8px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font-sans);
}

/* ================================================================
   Compile Panel (right sidebar)
   ================================================================ */

#compile-panel {
    background: var(--bg-surface);
    border-left: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.compile-status {
    padding: 12px 16px;
    font-size: 13px;
}

.status-idle {
    color: var(--text-muted);
}

.status-success {
    color: var(--accent-green);
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.status-error {
    color: var(--accent-red);
    white-space: pre-wrap;
    font-family: var(--font-mono);
    font-size: 12px;
}

.compile-stat {
    display: flex;
    justify-content: space-between;
    padding: 2px 0;
    font-size: 12px;
}

.compile-stat-label {
    color: var(--text-secondary);
}

.compile-stat-value {
    color: var(--text-primary);
    font-weight: 500;
    font-family: var(--font-mono);
}

.forth-viewer {
    border-top: 1px solid var(--border-subtle);
    display: flex;
    flex-direction: column;
}

.forth-viewer summary {
    padding: 10px 16px;
    cursor: pointer;
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    user-select: none;
}

.forth-viewer summary:hover {
    color: var(--text-primary);
}

.forth-code {
    overflow: auto;
    padding: 16px 20px;
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 1.6;
    color: var(--text-secondary);
    background: var(--bg-base);
    white-space: pre;
    margin: 0;
    flex: 1;
}

/* Compile actions button row */
.compile-actions {
    display: flex;
    gap: 4px;
    padding: 8px;
    border-top: 1px solid var(--border-subtle);
}

.compile-action-btn {
    flex: 1;
    justify-content: center;
}

/* Source modal */
.source-modal {
    max-width: 700px;
    width: 90vw;
    max-height: 80vh;
    height: auto;
}

.source-modal .forth-code {
    max-height: 60vh;
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.deploy-status {
    padding: 12px 16px;
    border-top: 1px solid var(--border-subtle);
    font-size: 13px;
}

/* Utilization bar */
.util-bar-wrap {
    margin-top: 6px;
    padding: 2px 0;
}

.util-bar-label {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 3px;
}

.util-bar {
    height: 6px;
    background: var(--bg-elevated);
    border-radius: 3px;
    overflow: hidden;
}

.util-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease, background 0.3s ease;
}

.util-bar-fill.low {
    background: var(--accent-green);
}

.util-bar-fill.mid {
    background: var(--accent-orange);
}

.util-bar-fill.high {
    background: var(--accent-red);
}

/* Device config panel */
.device-config-panel {
    border-top: 1px solid var(--border-subtle);
    padding: 4px 8px;
}

.device-config-section {
    padding: 4px 8px 8px;
}

.device-config-section h4 {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin: 6px 0 4px;
}

#compile-panel::-webkit-scrollbar {
    width: 6px;
}

#compile-panel::-webkit-scrollbar-track {
    background: transparent;
}

#compile-panel::-webkit-scrollbar-thumb {
    background: var(--border-default);
    border-radius: 3px;
}

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

.modal {
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    background: var(--bg-surface);
    color: var(--text-primary);
    padding: 0;
    max-width: 420px;
    width: 90%;
    box-shadow: var(--shadow-lg);
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    margin: 0;
}

.modal::backdrop {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
}

.modal-content {
    padding: 24px;
}

.modal-content h3 {
    margin-bottom: 16px;
    font-size: 16px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 16px;
}

.form-group label {
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 500;
}

.form-group select,
.form-group input {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-family: var(--font-sans);
}

.form-group select:focus,
.form-group input:focus {
    outline: none;
    border-color: var(--accent-blue);
    box-shadow: 0 0 0 3px rgba(88, 166, 255, 0.15);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 24px;
}

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

/* -- Tab bar: hidden on desktop -- */
.mobile-tab-bar {
    display: none;
}

@media (max-width: 900px) {
    #app-main {
        grid-template-columns: 200px 1fr 240px;
    }
}

@media (max-width: 768px) {

    /* ---- Header: compact ---- */
    #app-header {
        flex-wrap: wrap;
        height: auto;
        padding: 8px 12px;
        gap: 6px;
    }

    .header-left {
        gap: 8px;
        flex: 1;
        min-width: 0;
    }

    .header-left h1 {
        font-size: 14px;
    }

    .sequence-name-editable {
        display: none;
    }

    .header-right {
        width: 100%;
        justify-content: space-between;
        gap: 8px;
    }

    .connection-panel {
        flex-wrap: wrap;
        gap: 6px;
    }

    .conn-ip-input {
        width: 120px;
        min-width: 0;
    }

    .btn-deploy {
        padding: 6px 12px;
        font-size: 12px;
    }

    /* ---- Main: single panel with tab switching ---- */
    #app-main {
        display: flex;
        flex-direction: column;
        height: calc(100vh - 96px - 48px); /* header rows + tab bar */
        overflow: hidden;
    }

    #step-list-panel,
    #step-editor-panel,
    #compile-panel {
        display: none;
        flex: 1;
        overflow-y: auto;
        border: none;
        max-height: none;
        width: 100%;
    }

    #step-list-panel.mobile-active,
    #step-editor-panel.mobile-active,
    #compile-panel.mobile-active {
        display: flex;
        flex-direction: column;
    }

    #step-editor-panel.mobile-active {
        display: block;
        overflow-y: auto;
    }

    /* ---- Bottom Tab Bar ---- */
    .mobile-tab-bar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 48px;
        background: var(--bg-surface);
        border-top: 1px solid var(--border-subtle);
        z-index: 9001;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .mobile-tab {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        background: none;
        border: none;
        color: var(--text-muted);
        font-family: var(--font-sans);
        font-size: 12px;
        font-weight: 500;
        cursor: pointer;
        transition: color 0.15s, background 0.15s;
        padding: 0;
    }

    .mobile-tab:hover {
        color: var(--text-secondary);
        background: var(--bg-hover);
    }

    .mobile-tab.active {
        color: var(--accent-blue);
        font-weight: 600;
        background: var(--bg-hover);
    }

    /* ---- Touch targets ---- */
    .btn-sm {
        padding: 8px 12px;
        font-size: 12px;
        min-height: 36px;
    }

    .step-item {
        padding: 12px 14px;
        min-height: 48px;
    }

    .action-item {
        padding: 10px 12px;
        min-height: 44px;
    }

    .res-remove {
        min-width: 32px;
        min-height: 32px;
    }

    /* ---- Modals: scale down ---- */
    .modal {
        max-width: 95vw;
        width: 95vw;
    }

    .diagram-modal,
    .source-modal {
        max-width: 100vw;
        width: 100vw;
        max-height: 100vh;
        height: 100vh;
        border-radius: 0;
    }

    .help-modal {
        max-width: 95vw;
        width: 95vw;
    }

    /* ---- AI: show as tab panel on mobile, hide FAB ---- */
    .ai-fab {
        display: none !important;
    }

    .ai-chat-panel {
        position: static;
        display: none;
        width: 100%;
        max-width: none;
        border: none;
        border-radius: 0;
        box-shadow: none;
        flex: 1;
        overflow-y: auto;
        animation: none;
    }

    .ai-chat-panel.mobile-active {
        display: block !important;
    }

    .mobile-tab[data-panel="ai"] {
        color: var(--accent-purple);
    }

    .mobile-tab[data-panel="ai"].active {
        color: var(--accent-purple);
        font-weight: 600;
    }

    /* ---- Resources panel: full width ---- */
    .resources-panel {
        border-top: 1px solid var(--border-subtle);
    }

    /* ---- Editor sections: more padding ---- */
    .editor-sections {
        padding: 0 4px;
    }

    .section-header {
        flex-wrap: wrap;
        gap: 4px;
    }

    .section-header h3 {
        font-size: 13px;
    }

    .section-hint {
        font-size: 11px;
    }

    /* ---- Compile panel: scroll friendly ---- */
    #compile-panel.mobile-active {
        padding-bottom: 60px; /* space above tab bar */
    }
}

/* ================================================================
   Animations
   ================================================================ */

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.step-item {
    animation: fadeIn 0.2s ease-out;
}

.action-item {
    animation: fadeIn 0.15s ease-out;
}

.editor-section {
    animation: fadeIn 0.2s ease-out;
}

/* ================================================================
   State Diagram Modal
   ================================================================ */

.diagram-modal {
    max-width: 90vw;
    max-height: 90vh;
    width: 90vw;
    height: 85vh;
    border-radius: var(--radius-lg);
    padding: 0;
}

.diagram-modal-content {
    display: flex;
    flex-direction: column;
    height: 100%;
}

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

.diagram-header h3 {
    font-size: 16px;
    font-weight: 600;
}

.diagram-canvas {
    flex: 1;
    overflow: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-base);
    padding: 24px;
}

.diagram-canvas svg {
    max-width: 100%;
    max-height: 100%;
}

/* Compile section (templates in right panel) */
.compile-section {
    padding: 8px;
    border-top: 1px solid var(--border-subtle);
}

.compile-section .template-menu {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.compile-section .template-menu label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.compile-section .template-menu select {
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    padding: 6px 8px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: var(--font-sans);
    width: 100%;
}

.compile-section .file-actions {
    display: flex;
    gap: 4px;
    margin-top: 6px;
}

/* ================================================================
   Print Styles
   ================================================================ */

@media print {

    #app-header,
    #step-list-panel,
    #compile-panel,
    .modal::backdrop,
    .btn,
    .conn-mode-toggle {
        display: none !important;
    }

    .modal,
    .diagram-modal,
    .source-modal {
        position: static !important;
        transform: none !important;
        max-width: 100% !important;
        width: 100% !important;
        max-height: none !important;
        height: auto !important;
        border: none !important;
        box-shadow: none !important;
        display: block !important;
        page-break-inside: avoid;
    }

    .modal[open] {
        display: block !important;
    }

    .diagram-modal-content {
        height: auto !important;
    }

    .diagram-header .btn {
        display: none !important;
    }

    .diagram-canvas {
        overflow: visible !important;
        padding: 0 !important;
    }

    .diagram-canvas svg {
        max-width: 100% !important;
        height: auto !important;
    }

    .forth-code {
        max-height: none !important;
        overflow: visible !important;
        white-space: pre-wrap !important;
        word-break: break-all !important;
        font-size: 10px !important;
        border: 1px solid #ccc !important;
        padding: 12px !important;
    }

    #app-main {
        display: block !important;
        height: auto !important;
        overflow: visible !important;
    }

    #step-editor-panel {
        overflow: visible !important;
        padding: 12px !important;
    }

    body {
        background: #fff !important;
        color: #000 !important;
        overflow: visible !important;
    }
}

/* ================================================================
   Help System
   ================================================================ */

.help-icon {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 10px;
    padding: 0 1px;
    opacity: 0.25;
    transition: opacity 0.2s;
    vertical-align: middle;
    line-height: 1;
}

/* Brighten on parent hover */
.section-header:hover .help-icon,
summary:hover .help-icon {
    opacity: 0.5;
}

.help-icon:hover {
    opacity: 1 !important;
    color: var(--accent-blue);
}

.section-header .help-icon {
    margin-left: auto;
    margin-right: 4px;
    flex-shrink: 0;
}

summary .help-icon {
    margin-left: 4px;
}

/* Popover */

.help-popover {
    position: fixed;
    z-index: 10000;
    max-width: 260px;
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 32px rgba(0,0,0,0.25);
    padding: 12px 14px;
    font-size: 12px;
    line-height: 1.6;
    color: var(--text-secondary);
    pointer-events: auto;
    animation: fadeIn 0.12s ease-out;
}

.help-popover::before {
    content: '';
    position: absolute;
    top: -5px;
    left: var(--arrow-left, 20px);
    width: 10px;
    height: 10px;
    background: var(--bg-surface);
    border-left: 1px solid var(--border-default);
    border-top: 1px solid var(--border-default);
    transform: rotate(45deg);
}

.help-popover-content p {
    margin: 0 0 6px;
}

.help-popover-content p:last-child {
    margin-bottom: 0;
}

.help-popover-content strong {
    color: var(--text-primary);
    font-weight: 600;
}

.help-popover-content ul {
    margin: 4px 0 0;
    padding-left: 16px;
}

.help-popover-content li {
    margin-bottom: 2px;
}

/* Quick Reference Card */

.help-modal {
    backdrop-filter: blur(4px);
    max-width: 620px;
    width: calc(100vw - 40px);
    padding: 0;
    border: none;
    background: transparent;
    border-radius: var(--radius-lg);
    overflow: visible;
}

.help-ref-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    width: 100%;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 16px 64px rgba(0,0,0,0.3);
}

.help-ref-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px 12px;
    border-bottom: 1px solid var(--border-subtle);
}

.help-ref-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.help-ref-body {
    padding: 16px 20px 20px;
}

.help-ref-section {
    margin-bottom: 16px;
}

.help-ref-section:last-child {
    margin-bottom: 0;
}

.help-ref-section h4 {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-muted);
    margin: 0 0 8px;
    padding-bottom: 4px;
    border-bottom: 1px solid var(--border-subtle);
}

.help-ref-section dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 4px 12px;
    margin: 0;
}

.help-ref-section dt {
    font-weight: 600;
    font-size: 12px;
    color: var(--text-primary);
    font-family: var(--font-mono);
}

.help-ref-section dd {
    margin: 0;
    font-size: 12px;
    color: var(--text-secondary);
}

.help-ref-section ol,
.help-ref-section ul {
    margin: 0;
    padding-left: 20px;
}

.help-ref-section li {
    font-size: 12px;
    color: var(--text-secondary);
    margin-bottom: 3px;
}

.help-ref-note {
    color: var(--text-muted);
    font-size: 11px;
}

kbd {
    display: inline-block;
    padding: 2px 6px;
    font-family: var(--font-mono);
    font-size: 11px;
    font-weight: 600;
    color: var(--text-primary);
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-sm);
    box-shadow: 0 1px 0 var(--border-subtle);
    box-shadow: 0 1px 0 var(--border-subtle);
    line-height: 1;
}

/* ================================================================
   AI Chat (Coming Soon)
   ================================================================ */

.ai-fab {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9000;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: var(--gradient-accent);
    color: #fff;
    font-size: 22px;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

.ai-fab:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 24px rgba(0,0,0,0.3);
}

.ai-fab.open {
    transform: rotate(45deg) scale(1.1);
}

.ai-chat-panel {
    position: fixed;
    bottom: 80px;
    right: 20px;
    z-index: 9000;
    width: 320px;
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    box-shadow: 0 16px 48px rgba(0,0,0,0.2);
    overflow: hidden;
    animation: slideUp 0.2s ease-out;
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: translateY(0); }
}

.ai-chat-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--bg-elevated);
}

.ai-chat-header .ai-spark {
    font-size: 18px;
    animation: sparkle 2s ease-in-out infinite;
}

@keyframes sparkle {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.6; transform: scale(1.15); }
}

.ai-chat-header h4 {
    margin: 0;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
}

.ai-chat-header span.ai-badge {
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--accent-purple);
    background: rgba(130, 80, 223, 0.12);
    padding: 2px 6px;
    border-radius: 4px;
}

.ai-chat-body {
    padding: 24px 20px;
    text-align: center;
}

.ai-chat-body p {
    margin: 0 0 6px;
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.6;
}

.ai-chat-body .ai-coming-icon {
    font-size: 32px;
    margin-bottom: 12px;
    display: block;
}

.ai-chat-body .ai-feature-list {
    text-align: left;
    margin: 12px 0 0;
    padding: 0;
    list-style: none;
}

.ai-chat-body .ai-feature-list li {
    font-size: 12px;
    color: var(--text-muted);
    padding: 4px 0;
    display: flex;
    align-items: center;
    gap: 6px;
}

.ai-chat-body .ai-feature-list li::before {
    content: '→';
    color: var(--accent-purple);
    font-weight: 600;
}