.btn {
    appearance: none;
    border: 1px solid var(--border-color);
    background: var(--surface-light);
    color: var(--text-primary);
    border-radius: 8px;
    padding: 0.45rem 0.85rem;
    font-size: 0.85rem;
    cursor: pointer;
    font-weight: 600;
}

.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

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

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

.btn-sm { padding: 0.3rem 0.55rem; font-size: 0.78rem; }

.file-label { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }

.gate-palette { display: flex; flex-direction: column; gap: 0.55rem; }

.gate-category {
    border: 1px solid var(--border-color);
    border-radius: 10px;
    overflow: hidden;
    background: var(--background);
}

.gate-category-toggle {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0.55rem;
    background: var(--surface-light);
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 600;
}

.gate-category-toggle[aria-expanded="false"] + .gate-list { display: none; }
.gate-category-chevron { opacity: 0.7; }

.gate-list {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem;
    padding: 0.45rem;
}

.gate-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    padding: 0.35rem 0.2rem 0.4rem;
    border-radius: 8px;
    border: 1px solid transparent;
    background: var(--surface);
    cursor: grab;
    user-select: none;
    touch-action: manipulation;
}

.gate-item:hover, .gate-item.selected-place {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 1px var(--primary-color);
}

.gate-item:active { cursor: grabbing; }

.gate-symbol-svg {
    width: 52px;
    height: 36px;
    display: block;
    background: var(--schematic-bg, var(--background));
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 2px;
}

.gate-symbol {
    width: 36px;
    height: 28px;
    border-radius: 6px;
    display: grid;
    place-items: center;
    font-size: 0.72rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: #fff;
}

.gate-symbol.cat-io { background: linear-gradient(145deg, #0ea5e9, #0369a1); }
.gate-symbol.cat-basic { background: linear-gradient(145deg, #2aa89a, #1d7a70); }
.gate-symbol.cat-arith { background: linear-gradient(145deg, #d97706, #92400e); }
.gate-symbol.cat-seq { background: linear-gradient(145deg, #e11d48, #9f1239); }
.gate-symbol.cat-display { background: linear-gradient(145deg, #f97316, #c2410c); }
.gate-symbol.cat-sensor { background: linear-gradient(145deg, #22c55e, #15803d); }
.gate-symbol.cat-device { background: linear-gradient(145deg, #a78bfa, #6d28d9); }
.gate-symbol.cat-play { background: linear-gradient(145deg, #f43f5e, #be123c); }
.gate-symbol.cat-annotate { background: linear-gradient(145deg, #64748b, #334155); }
.gate-symbol.cat-custom { background: linear-gradient(145deg, #0d9488, #115e59); }

.palette-search-wrap {
    display: block;
    padding: 0 10px 10px;
}
.palette-search {
    width: 100%;
    box-sizing: border-box;
    border: 1px solid var(--border-color, #334155);
    border-radius: 8px;
    background: var(--surface-light, #1e293b);
    color: var(--text-color, #e2e8f0);
    padding: 8px 10px;
    font: 500 13px/1.2 "IBM Plex Sans", sans-serif;
}
.palette-search:focus {
    outline: 2px solid var(--primary-color, #2aa89a);
    outline-offset: 1px;
}
.palette-empty {
    padding: 8px 12px 16px;
    margin: 0;
}
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.lcd-cursor-blink {
    animation: lcdBlink 0.9s step-end infinite;
}
@keyframes lcdBlink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}
.buzzer-wave {
    animation: buzzWave 0.35s ease-in-out infinite alternate;
}
@keyframes buzzWave {
    from { opacity: 0.35; }
    to { opacity: 1; }
}
.gate-symbol.cat-annotate { background: linear-gradient(145deg, #64748b, #334155); }
.gate-symbol.cat-custom { background: linear-gradient(145deg, #38bdf8, #0284c7); }

.gate-name { font-size: 0.68rem; color: var(--text-secondary); text-align: center; }

.example-card, .library-card {
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
}

.example-card h3, .library-card h3 { font-size: 0.95rem; }
.example-card p, .library-card p { font-size: 0.8rem; color: var(--text-muted); line-height: 1.35; }

.truth-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}

.truth-table th, .truth-table td {
    border: 1px solid var(--border-color);
    padding: 0.28rem 0.45rem;
    text-align: center;
}

.truth-table th { background: var(--surface-light); }
.truth-table td.hi { color: var(--wire-high); font-weight: 700; }
.truth-table td.lo { color: var(--text-muted); }

.kmap {
    display: inline-grid;
    gap: 2px;
    margin-top: 0.5rem;
}

.kmap-cell {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border: 1px solid var(--border-color);
    background: var(--background);
    font-weight: 700;
}

.code-block {
    font-family: ui-monospace, Consolas, monospace;
    font-size: 0.75rem;
    white-space: pre-wrap;
    background: var(--background);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 0.65rem;
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: grid;
    place-items: center;
    z-index: 100;
    padding: 1rem;
}

.modal[hidden] { display: none !important; }

.modal-card {
    width: min(420px, 100%);
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 1.1rem;
    box-shadow: 0 16px 40px var(--shadow);
}

.modal-card h2 { margin-bottom: 0.85rem; font-size: 1.1rem; }

.field {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 0.7rem;
}

.field input[type="number"] {
    background: var(--background);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 8px;
    padding: 0.45rem 0.6rem;
}

.field.check { flex-direction: row; align-items: center; gap: 0.45rem; }

.modal-actions { display: flex; gap: 0.45rem; justify-content: flex-end; margin-top: 0.5rem; }
.modal-actions.stacked { flex-direction: column; }

.inspector .field input[type="text"],
.inspector .field input[type="number"],
.inspector .field select {
    background: var(--background);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 6px;
    padding: 0.35rem 0.45rem;
}

/* SVG component styling */
.comp-body, .sym-body {
    fill: var(--comp-fill);
    stroke: var(--comp-stroke);
    stroke-width: 1.6;
}

.comp-body.selected, .sym-body.selected {
    stroke: var(--primary-color);
    stroke-width: 2.25;
}

.comp-label, .sym-caption {
    fill: var(--comp-text);
    font-size: 11px;
    font-weight: 700;
    font-family: "IBM Plex Sans", sans-serif;
    pointer-events: none;
    text-anchor: middle;
    dominant-baseline: middle;
    letter-spacing: 0.02em;
}

.sym-caption.tiny { font-size: 9px; font-weight: 600; }
.sym-caption.large { font-size: 18px; font-family: "IBM Plex Mono", ui-monospace, monospace; }

.schematic-label {
    fill: var(--comp-text);
    font-size: 13px;
    font-weight: 600;
    font-family: "IBM Plex Sans", sans-serif;
    pointer-events: none;
    dominant-baseline: middle;
    letter-spacing: 0.01em;
}

.schematic-label.selected { fill: var(--primary-color); }

.comp-nameplate {
    fill: var(--text-muted);
    font-size: 9px;
    font-weight: 600;
    font-family: "IBM Plex Sans", sans-serif;
    pointer-events: none;
    text-anchor: middle;
    dominant-baseline: hanging;
}

.pin-hit { fill: transparent; cursor: crosshair; }
.pin-prong { pointer-events: none; }
.pin-dot { fill: var(--text-muted); stroke: var(--schematic-bg); stroke-width: 1; }
.pin-dot.high { fill: var(--wire-high); }
.pin-dot.low { fill: var(--wire-low); }
.pin-dot.x { fill: var(--wire-x); }

.wire-hit { cursor: crosshair; pointer-events: stroke; }
.wire-path {
    fill: none;
    stroke: var(--wire-low);
    stroke-width: 2.25;
    stroke-linecap: round;
    stroke-linejoin: round;
    pointer-events: stroke;
    cursor: crosshair;
}

.wire-path.high { stroke: var(--wire-high); }
.wire-path.x { stroke: var(--wire-x); stroke-dasharray: 4 3; }
.wire-path.selected { stroke: var(--primary-color); stroke-width: 3.25; }

.marquee-rect {
    fill: color-mix(in srgb, var(--primary-color) 12%, transparent);
    stroke: var(--primary-color);
    stroke-width: 1;
    stroke-dasharray: 4 3;
    pointer-events: none;
}

.rubber-band {
    fill: none;
    stroke: var(--primary-color);
    stroke-width: 1.5;
    stroke-dasharray: 5 4;
    pointer-events: none;
}

.junction-dot { fill: var(--wire-low); }
.junction-dot.high { fill: var(--wire-high); }
.junction-dot.low { fill: var(--wire-low); }
.junction-dot.x { fill: var(--wire-x); }

.led-on { fill: #ff6b6b; filter: drop-shadow(0 0 7px rgba(248, 113, 113, 0.75)); }
.led-off { fill: #2a3548; stroke: var(--comp-stroke); stroke-width: 1; }

html[data-theme="light"] {
    --wire-high: #15803d;
    --wire-low: #94a3b8;
    --wire-x: #d97706;
}

.library-card .lib-cat {
    display: inline-block;
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--primary-color);
    margin-bottom: 0.2rem;
}

.library-card.has-module {
    border-color: color-mix(in srgb, var(--primary-color) 45%, var(--border-color));
}

/* Context menu */
.ctx-menu {
    position: fixed;
    z-index: 200;
    min-width: 200px;
    padding: 0.35rem 0;
    margin: 0;
    background: var(--surface);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: 0 12px 32px var(--shadow);
    list-style: none;
}

.ctx-menu[hidden] { display: none !important; }

.ctx-item {
    display: block;
    width: 100%;
    text-align: left;
    padding: 0.45rem 0.85rem;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 0.82rem;
    cursor: pointer;
    font-family: inherit;
}

.ctx-item:hover, .ctx-item:focus {
    background: var(--surface-light);
    outline: none;
}

.ctx-item.danger { color: var(--danger); }
.ctx-item:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.ctx-sep {
    height: 1px;
    margin: 0.3rem 0.5rem;
    background: var(--border-color);
}

.ctx-kbd {
    float: right;
    opacity: 0.55;
    font-size: 0.72rem;
    font-family: "IBM Plex Mono", monospace;
    margin-left: 1rem;
}

/* Physics modal */
.modal-card-physics {
    width: min(1100px, calc(100vw - 2rem));
    max-height: min(92vh, 900px);
    overflow: auto;
    display: flex;
    flex-direction: column;
}

.physics-layout {
    display: grid;
    grid-template-columns: minmax(0, 1.35fr) minmax(280px, 0.9fr);
    gap: 1.1rem;
    margin-top: 0.55rem;
    align-items: stretch;
    min-height: 0;
}

.physics-stage {
    min-width: 0;
    min-height: 280px;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    background: #0a1018;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

#physicsSvg {
    width: 100%;
    height: auto;
    max-height: min(52vh, 480px);
    display: block;
}

.physics-body {
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.5;
    overflow: auto;
    max-height: min(52vh, 480px);
    padding-right: 0.25rem;
}

.physics-body h3,
.physics-body h4.phys-section {
    color: var(--text-primary);
    margin: 0.85rem 0 0.4rem;
    font-size: 0.95rem;
    font-weight: 650;
}

.phys-eyebrow {
    font-size: 0.75rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--primary-color);
    margin: 0 0 0.55rem;
    font-weight: 650;
}

.phys-lead {
    color: var(--text-primary);
    margin: 0 0 0.75rem;
    font-size: 0.95rem;
}

.phys-how {
    margin: 0 0 0.85rem;
    padding: 0.65rem 0.75rem;
    border-radius: 8px;
    background: var(--background);
    border: 1px solid var(--border-color);
}

.phys-facts {
    display: grid;
    gap: 0.45rem;
    margin-bottom: 0.35rem;
}

.phys-facts > div {
    display: grid;
    grid-template-columns: 7.5rem 1fr;
    gap: 0.5rem;
    font-size: 0.82rem;
}

.phys-fact-k {
    color: var(--text-muted);
    font-weight: 600;
}

.phys-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    margin: 0.35rem 0 0.5rem;
}

.phys-table th, .phys-table td {
    border: 1px solid var(--border-color);
    padding: 0.35rem 0.45rem;
    text-align: left;
    vertical-align: top;
}

.phys-table th { background: var(--surface-light); }

/* SVG figure text */
.phys-svg-title {
    fill: #eef3f8;
    font-size: 18px;
    font-weight: 700;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-sub {
    fill: #8b9bb4;
    font-size: 12px;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-label {
    fill: #f1f5f9;
    font-size: 15px;
    font-weight: 700;
    text-anchor: middle;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-pin {
    fill: #e2e8f0;
    font-size: 14px;
    font-weight: 700;
    text-anchor: middle;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-caption {
    fill: #cbd5e1;
    font-size: 12px;
    font-weight: 600;
    text-anchor: middle;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-tiny {
    fill: #94a3b8;
    font-size: 11px;
    font-weight: 600;
    text-anchor: middle;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-legend-text {
    fill: #cbd5e1;
    font-size: 11px;
    font-weight: 600;
    text-anchor: start;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-svg-hint {
    fill: #a7f3d0;
    font-size: 13px;
    font-weight: 600;
    font-family: "IBM Plex Sans", sans-serif;
    text-anchor: start;
}

.phys-badge-text {
    fill: var(--text-primary);
    font-size: 9px;
    font-weight: 700;
    font-family: "IBM Plex Sans", sans-serif;
}
.phys-badge-text.muted { fill: var(--text-muted); font-weight: 500; }

.phys-layer {
    animation: physFadeIn 0.55s ease both;
}
.phys-gate-block {
    animation: physGateGlow 2.4s ease-in-out infinite;
}
.phys-channel {
    animation: physChannelPulse 1.6s ease-in-out infinite;
}
.phys-pulse {
    animation: physSoftPulse 2.8s ease-in-out infinite;
}
.phys-depletion {
    animation: physSoftPulse 2.2s ease-in-out infinite;
}
.phys-ray {
    animation: physRayBlink 1.1s ease-in-out infinite;
}
.phys-lead, .phys-rail {
    opacity: 0.9;
}
.phys-junction {
    animation: physSoftPulse 2s ease-in-out infinite;
}

@keyframes physFadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: none; }
}
@keyframes physGateGlow {
    0%, 100% { filter: drop-shadow(0 0 0 rgba(42, 168, 154, 0)); }
    50% { filter: drop-shadow(0 0 8px rgba(42, 168, 154, 0.55)); }
}
@keyframes physChannelPulse {
    0%, 100% { opacity: 0.55; }
    50% { opacity: 1; }
}
@keyframes physSoftPulse {
    0%, 100% { opacity: 0.92; }
    50% { opacity: 1; }
}
@keyframes physRayBlink {
    0%, 100% { opacity: 0.35; }
    50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    .phys-layer, .phys-gate-block, .phys-channel, .phys-pulse,
    .phys-depletion, .phys-ray, .phys-junction, .phys-carrier {
        animation: none !important;
    }
}

@media (max-width: 900px) {
    .physics-layout {
        grid-template-columns: 1fr;
    }
    #physicsSvg {
        max-height: min(42vh, 360px);
    }
    .physics-body {
        max-height: none;
    }
}
