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

:root {
    --primary-color: #2aa89a;
    --primary-dark: #1d7a70;
    --background: #0b1019;
    --surface: #141c28;
    --surface-light: #1e2a3a;
    --text-primary: #eef3f8;
    --text-secondary: #b7c4d4;
    --text-muted: #8191a5;
    --border-color: #2c3a4f;
    --success: #34d399;
    --danger: #f87171;
    --warning: #fbbf24;
    --info: #38bdf8;
    --wire-high: #3ecf8e;
    --wire-low: #5b6b82;
    --wire-x: #eab308;
    --shadow: rgba(0, 0, 0, 0.45);
    --header-height: 64px;
    --sidebar-width: 248px;
    --right-width: 268px;
    /* Schematic-specific — must contrast with canvas */
    --schematic-bg: #070b12;
    --schematic-grid: rgba(120, 140, 170, 0.09);
    --comp-fill: #1a2433;
    --comp-stroke: #9aafc7;
    --comp-text: #e8f0fa;
    --seg-on: #ff7043;
    --seg-off: #152030;
    --switch-on-bg: #143528;
    --switch-track: #0c1420;
}

/* Keep classical distinctive even when site theme swaps tokens */
body.classical-sim {
    --primary-color: #2aa89a;
    --primary-dark: #1d7a70;
    --background: #0b1019;
    --surface: #141c28;
    --surface-light: #1e2a3a;
    --border-color: #2c3a4f;
    --text-primary: #eef3f8;
    --text-secondary: #b7c4d4;
    --text-muted: #8191a5;
    --schematic-bg: #070b12;
    --schematic-grid: rgba(120, 140, 170, 0.09);
    --comp-fill: #1a2433;
    --comp-stroke: #9aafc7;
    --comp-text: #e8f0fa;
    --seg-on: #ff7043;
    --seg-off: #152030;
    --switch-on-bg: #143528;
    --switch-track: #0c1420;
    --wire-high: #3ecf8e;
    --wire-low: #5b6b82;
}

html[data-theme="light"] body.classical-sim,
body.classical-sim[data-theme="light"] {
    --background: #e8eef6;
    --surface: #f7fafc;
    --surface-light: #dce5f0;
    --border-color: #b7c4d6;
    --text-primary: #1a2332;
    --text-secondary: #3d4f66;
    --text-muted: #6b7c91;
    --schematic-bg: #d9e2ee;
    --schematic-grid: rgba(40, 60, 90, 0.12);
    --comp-fill: #ffffff;
    --comp-stroke: #3d4f66;
    --comp-text: #1a2332;
    --seg-on: #e85d2c;
    --seg-off: #c5d0de;
    --switch-on-bg: #d8f3e4;
    --switch-track: #c8d4e2;
    --wire-high: #15803d;
    --wire-low: #7a8ba0;
}

/* Fill gaps when themes.css maps core tokens but omits surface-light */
html[data-theme] {
    --surface-light: color-mix(in srgb, var(--surface) 80%, var(--text-primary) 20%);
}

html, body {
    height: 100%;
    font-family: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
    background: var(--background);
    color: var(--text-primary);
}

body { overflow: hidden; }

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    min-height: 100dvh;
}

.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.65rem 1.25rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border-color);
    min-height: var(--header-height);
    z-index: 40;
}

.title-section {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    min-width: 0;
}

.logo {
    height: 36px;
    width: auto;
    display: block;
}

.title { font-size: 1.1rem; font-weight: 700; line-height: 1.2; }
.subtitle { font-size: 0.75rem; color: var(--text-muted); }

.header-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    justify-content: flex-end;
}

.main-content {
    flex: 1;
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr var(--right-width);
    min-height: 0;
    overflow: hidden;
}

.sidebar, .right-pane {
    background: var(--surface);
    border-right: 1px solid var(--border-color);
    overflow: auto;
    padding: 0.75rem;
    min-height: 0;
}

.right-pane { border-right: none; border-left: 1px solid var(--border-color); }

.center-pane {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--background);
}

.sidebar-panel-title, .right-sub {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    margin-bottom: 0.6rem;
}

.right-sub { margin-top: 1rem; }

.mobile-hint {
    display: none;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 0.6rem;
}

.tab-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--surface);
}

.tab-btn {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-secondary);
    padding: 0.4rem 0.75rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
}

.tab-btn:hover { background: var(--surface-light); }
.tab-btn.active {
    color: var(--text-primary);
    border-color: var(--border-color);
    background: var(--background);
}

.tab-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    position: relative;
}

.tab-panel[hidden] { display: none !important; }

.canvas-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.4rem 0.75rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--surface);
    flex-wrap: wrap;
}

.doc-tabs {
    display: flex;
    gap: 0.25rem;
    flex-wrap: wrap;
    padding: 0.35rem 0.6rem 0;
    border-bottom: 1px solid var(--border-color);
    background: var(--surface);
}

.doc-tab {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    max-width: 11rem;
    border: 1px solid var(--border-color);
    border-bottom: none;
    border-radius: 0.5rem 0.5rem 0 0;
    background: var(--surface-light);
    color: var(--text-secondary);
    padding: 0.35rem 0.5rem 0.35rem 0.65rem;
    font: 600 0.75rem/1.2 "IBM Plex Sans", sans-serif;
    cursor: pointer;
}

.doc-tab.active {
    color: var(--primary-color);
    box-shadow: inset 0 -2px 0 var(--primary-color);
}

.doc-tab-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.doc-tab-close {
    border: none;
    background: transparent;
    color: inherit;
    opacity: 0.7;
    cursor: pointer;
    padding: 0 0.1rem;
    font-size: 0.9rem;
    line-height: 1;
}

.doc-tab-close:hover { opacity: 1; }

.examples-search-wrap,
.library-search-wrap {
    padding: 0.65rem 0.75rem 0;
}

.toolbar-spacer { flex: 1; }
.hint-text { font-size: 0.75rem; color: var(--text-muted); }
.toolbar-check { font-size: 0.8rem; color: var(--text-secondary); display: flex; align-items: center; gap: 0.3rem; }

.status-pill {
    font-size: 0.72rem;
    font-weight: 600;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    background: var(--surface-light);
    color: var(--text-secondary);
}

.status-pill.running { background: rgba(34, 197, 94, 0.2); color: var(--wire-high); }
.status-pill.warn { background: rgba(245, 158, 11, 0.2); color: var(--warning); }
.status-pill.error { background: rgba(239, 68, 68, 0.2); color: var(--danger); }

.canvas-wrap {
    flex: 1;
    min-height: 240px;
    position: relative;
    overflow: hidden;
    background-color: var(--schematic-bg);
    background-image:
        radial-gradient(ellipse 80% 60% at 50% 40%, rgba(42, 168, 154, 0.05), transparent 70%),
        linear-gradient(var(--schematic-grid) 1px, transparent 1px),
        linear-gradient(90deg, var(--schematic-grid) 1px, transparent 1px);
    background-size: auto, 20px 20px, 20px 20px;
}

.canvas-wrap.no-grid {
    background-image: none;
}

#circuitSvg {
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
    cursor: default;
}

.inspector {
    position: absolute;
    right: 10px;
    top: 52px;
    width: 220px;
    max-height: calc(100% - 70px);
    overflow: auto;
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 0.75rem;
    box-shadow: 0 8px 24px var(--shadow);
    z-index: 5;
}

.inspector h3 { font-size: 0.9rem; margin-bottom: 0.5rem; }
.inspector-empty, .muted { color: var(--text-muted); font-size: 0.82rem; }

.analysis-layout {
    padding: 0.75rem;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-height: 0;
    flex: 1;
}

.analysis-controls { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.analysis-out {
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 0.75rem;
    overflow: auto;
    max-height: 50vh;
}

.timing-wrap {
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    overflow: auto;
    min-height: 180px;
}

#timingSvg { min-width: 100%; height: 220px; display: block; }

.examples-grid, .library-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 0.75rem;
    padding: 0.75rem;
    overflow: auto;
}

.library-toolbar {
    padding: 0.75rem 0.75rem 0;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.library-hint { max-width: 52rem; }

.modal-card-wide { width: min(900px, 100%); max-height: 90vh; overflow: auto; }

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-bottom: 0.4rem;
}

.modal-head h2 { margin-bottom: 0; }

.internals-canvas-wrap {
    margin-top: 0.6rem;
    height: min(52vh, 420px);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    overflow: hidden;
    background-color: var(--schematic-bg);
    background-image:
        linear-gradient(var(--schematic-grid) 1px, transparent 1px),
        linear-gradient(90deg, var(--schematic-grid) 1px, transparent 1px);
    background-size: 20px 20px;
}

#internalsSvg { width: 100%; height: 100%; display: block; }

.sidebar-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border-color);
}

.project-actions {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.netlist-pre, .messages {
    background: var(--background);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.5rem;
    font-size: 0.72rem;
    max-height: 180px;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
}

.qc-status {
    display: block;
    font-size: 0.7rem;
    margin: 0 0 0.35rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.qc-status[hidden] {
    display: none;
}

.qc-status.err {
    color: var(--danger);
}

.qc-editor {
    width: 100%;
    min-height: 220px;
    max-height: 42vh;
    resize: vertical;
    background: var(--background);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.5rem;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.72rem;
    line-height: 1.35;
    color: var(--text-primary);
    tab-size: 2;
}

.qc-cheatsheet {
    margin: 0.4rem 0 0.6rem;
    font-size: 0.72rem;
    color: var(--text-secondary);
}

.qc-cheatsheet summary {
    cursor: pointer;
    user-select: none;
}

.qc-cheat-pre {
    margin: 0.35rem 0 0;
    padding: 0.45rem;
    background: var(--background);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 0.68rem;
    white-space: pre-wrap;
    overflow: auto;
    max-height: 140px;
}

.messages { color: var(--text-secondary); max-height: 140px; }
.messages .err { color: var(--danger); }
.messages .warn { color: var(--warning); }

@media (max-width: 1024px) {
    .main-content {
        grid-template-columns: 180px 1fr minmax(220px, 280px);
    }
    .inspector { width: 180px; }
}

@media (max-width: 768px) {
    body { overflow: auto; }
    .app-container { height: auto; min-height: 100dvh; }
    .main-content {
        display: flex;
        flex-direction: column;
        overflow: visible;
    }
    .sidebar, .right-pane {
        display: block;
        border: none;
        border-bottom: 1px solid var(--border-color);
        max-height: none;
        width: 100%;
    }
    .right-pane { display: block; }
    .center-pane { min-height: 60vh; }
    .canvas-wrap { min-height: 360px; height: 55vh; }
    .mobile-hint { display: block; }
    .inspector {
        position: static;
        width: auto;
        max-height: none;
        margin: 0.5rem;
    }
    .header { flex-direction: column; align-items: flex-start; }
    .title { font-size: 1rem; }
}

@media (max-width: 480px) {
    .header-controls .btn { padding: 0.35rem 0.55rem; font-size: 0.78rem; }
    .tab-btn { font-size: 0.78rem; padding: 0.35rem 0.5rem; }
}
