:root {
    --bg: #0d0f0e;
    --panel: #151917;
    --panel-2: #1b201d;
    --line: #2a312d;
    --line-strong: #3a443e;
    --text: #e6ece8;
    --muted: #8a978f;
    --faint: #5b6660;
    --phosphor: #7cffb2;
    --phosphor-dim: rgba(124, 255, 178, 0.14);
    --amber: #ffb454;
    --amber-dim: rgba(255, 180, 84, 0.14);
    --danger: #ff6b5e;
    --radius: 10px;
    --display: 'Chakra Petch', 'IBM Plex Sans', sans-serif;
    --sans: 'IBM Plex Sans', system-ui, sans-serif;
    --mono: 'IBM Plex Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: var(--sans);
    font-size: 14px;
    color: var(--text);
    background:
        radial-gradient(1200px 600px at 70% -10%, rgba(124, 255, 178, 0.06), transparent 60%),
        radial-gradient(900px 500px at -10% 110%, rgba(255, 180, 84, 0.05), transparent 60%),
        var(--bg);
}

svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
svg .fill { fill: currentColor; stroke: none; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.app {
    height: 100vh;
    display: grid;
    grid-template-columns: 84px minmax(0, 1fr) 400px;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
        "top top top"
        "rail stage side"
        "status status status";
    gap: 12px;
    padding: 12px;
}

/* ---------- Top bar ---------- */
.topbar {
    grid-area: top;
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 14px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    animation: rise .5s ease both;
}

.brand { display: flex; align-items: center; gap: 12px; margin-right: auto; }
.brand-mark {
    width: 38px; height: 38px;
    display: grid; place-items: center;
    border-radius: 9px;
    background: var(--phosphor-dim);
    border: 1px solid rgba(124, 255, 178, 0.35);
    color: var(--phosphor);
    box-shadow: 0 0 24px rgba(124, 255, 178, 0.15);
}
.brand-mark svg { width: 26px; height: 26px; stroke-width: 2.2; }
.brand h1 { margin: 0; font-family: var(--display); font-size: 1.2rem; letter-spacing: .04em; text-transform: uppercase; }
.brand p { margin: 0; font-family: var(--mono); font-size: .7rem; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; }

.topbar-group, .transport { display: flex; align-items: center; gap: 8px; }
.transport { padding-left: 16px; border-left: 1px solid var(--line); }

.btn, select {
    display: inline-flex; align-items: center; gap: 7px;
    height: 36px;
    padding: 0 13px;
    font: 500 .85rem var(--sans);
    color: var(--text);
    background: var(--panel-2);
    border: 1px solid var(--line-strong);
    border-radius: 8px;
    cursor: pointer;
    transition: background .15s, border-color .15s, transform .08s, box-shadow .15s;
}
.btn:hover, select:hover { background: #232a26; border-color: #4a564f; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, select:focus-visible, .tool:focus-visible, input:focus-visible { outline: 2px solid var(--phosphor); outline-offset: 2px; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.run { background: var(--phosphor); color: #06140c; border-color: var(--phosphor); font-weight: 600; box-shadow: 0 0 18px rgba(124, 255, 178, 0.25); }
.btn.run:hover { background: #9dffc6; }
.btn.danger:hover { color: var(--danger); border-color: rgba(255, 107, 94, 0.5); background: rgba(255, 107, 94, 0.08); }
.btn.active-pause { color: var(--amber); border-color: rgba(255, 180, 84, .5); background: var(--amber-dim); }

select {
    appearance: none;
    padding-right: 32px;
    background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position: calc(100% - 17px) 15px, calc(100% - 12px) 15px;
    background-size: 5px 5px;
    background-repeat: no-repeat;
}

/* ---------- Tool rail ---------- */
.rail {
    grid-area: rail;
    display: flex; flex-direction: column; gap: 6px;
    padding: 8px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    animation: rise .5s .05s ease both;
}
.rail-sep { height: 1px; background: var(--line); margin: 4px 6px; }

.tool {
    position: relative;
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    padding: 10px 4px 8px;
    color: var(--muted);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    font: 500 .68rem var(--sans);
    cursor: pointer;
    transition: color .15s, background .15s, border-color .15s;
}
.tool svg { width: 24px; height: 24px; stroke-width: 1.6; }
.tool:hover { color: var(--text); background: var(--panel-2); }
.tool.active { color: var(--phosphor); background: var(--phosphor-dim); border-color: rgba(124, 255, 178, 0.35); }
.tool kbd {
    position: absolute; top: 4px; right: 5px;
    font: 500 .58rem var(--mono);
    color: var(--faint);
}
.tool.active kbd { color: var(--phosphor); }

/* ---------- Stage ---------- */
.stage {
    grid-area: stage;
    position: relative;
    min-height: 420px;
    overflow: hidden;
    background: #101412;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: inset 0 0 80px rgba(0, 0, 0, 0.5);
    animation: rise .5s .1s ease both;
}
#circuitCanvas { position: absolute; inset: 0; display: block; cursor: crosshair; }
#circuitCanvas.tool-select { cursor: default; }
#circuitCanvas.hover-pin { cursor: pointer; }
#circuitCanvas.hover-part { cursor: grab; }
#circuitCanvas.dragging { cursor: grabbing; }

.stage-label {
    position: absolute; top: 12px; left: 14px; z-index: 2;
    display: flex; align-items: center; gap: 8px;
    font: 600 .7rem var(--mono); letter-spacing: .12em; text-transform: uppercase;
    color: var(--muted);
    pointer-events: none;
}
.stage-label .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--phosphor); box-shadow: 0 0 8px var(--phosphor); }
.stage-hint { margin-left: 8px; padding-left: 10px; border-left: 1px solid var(--line-strong); color: var(--faint); letter-spacing: .02em; text-transform: none; font-weight: 400; }

.empty {
    position: absolute; inset: 0; z-index: 3;
    display: grid; place-items: center;
    pointer-events: none;
    transition: opacity .25s;
}
.empty.hidden { opacity: 0; }
.empty.hidden .empty-card { pointer-events: none; }
.empty-card {
    pointer-events: auto;
    max-width: 360px;
    padding: 26px 28px;
    text-align: center;
    background: rgba(21, 25, 23, 0.88);
    backdrop-filter: blur(6px);
    border: 1px dashed var(--line-strong);
    border-radius: 14px;
}
.empty-card svg { width: 96px; height: 48px; color: var(--phosphor); stroke-width: 2; filter: drop-shadow(0 0 6px rgba(124, 255, 178, .5)); }
.empty-card h2 { margin: 8px 0 6px; font-family: var(--display); font-size: 1.15rem; letter-spacing: .03em; }
.empty-card p { margin: 0 0 16px; color: var(--muted); line-height: 1.5; }
.empty-actions { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }

/* ---------- Side panels ---------- */
.side {
    grid-area: side;
    display: flex; flex-direction: column; gap: 12px;
    min-height: 0;
}
.card {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 12px 14px 14px;
    animation: rise .5s ease both;
}
.scope { animation-delay: .15s; }
.inspector { animation-delay: .2s; }
.log { animation-delay: .25s; flex: 1; min-height: 120px; display: flex; flex-direction: column; }

.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head h3 { margin: 0; font: 600 .72rem var(--mono); letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }

.badge {
    font: 600 .66rem var(--mono); letter-spacing: .08em; text-transform: uppercase;
    padding: 3px 8px; border-radius: 99px;
    color: var(--muted); background: var(--panel-2); border: 1px solid var(--line-strong);
}
.badge.live { color: var(--phosphor); border-color: rgba(124, 255, 178, .4); background: var(--phosphor-dim); }
.badge.paused, .badge.stale { color: var(--amber); border-color: rgba(255, 180, 84, .4); background: var(--amber-dim); }
.badge.error { color: var(--danger); border-color: rgba(255, 107, 94, .4); background: rgba(255, 107, 94, .1); }

.sim-settings { display: flex; gap: 8px; margin-bottom: 10px; }
.sim-settings label {
    flex: 1; display: flex; align-items: center; gap: 6px;
    padding: 0 10px; height: 32px;
    font-size: .72rem; color: var(--muted);
    background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px;
}
.sim-settings input { flex: 1; min-width: 0; text-align: right; }
.sim-settings em { font: normal .75rem var(--mono); color: var(--faint); }

input {
    font: 500 .85rem var(--mono);
    color: var(--text);
    background: transparent;
    border: none;
}

.plot {
    position: relative;
    height: 260px;
    padding: 8px 6px 4px;
    background:
        linear-gradient(rgba(124, 255, 178, 0.04) 1px, transparent 1px) 0 0 / 100% 20%,
        #0b0e0c;
    border: 1px solid var(--line);
    border-radius: 8px;
}
.plot-empty {
    position: absolute; inset: 0;
    display: grid; place-items: center;
    font: .78rem var(--mono); color: var(--faint);
    pointer-events: none;
}
.plot-empty.hidden { display: none; }

/* Inspector */
.inspector-body .muted { margin: 0; color: var(--muted); line-height: 1.5; font-size: .85rem; }
.insp-title { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.insp-chip {
    width: 34px; height: 34px; display: grid; place-items: center;
    border-radius: 8px; color: var(--phosphor); background: var(--phosphor-dim);
    font: 700 .9rem var(--display);
}
.insp-title strong { display: block; font-family: var(--display); font-size: 1rem; letter-spacing: .02em; }
.insp-title small { color: var(--muted); font: .74rem var(--mono); }
.insp-field {
    display: flex; align-items: center; gap: 8px;
    padding: 0 10px; height: 38px;
    background: var(--panel-2); border: 1px solid var(--line-strong); border-radius: 8px;
    transition: border-color .15s;
}
.insp-field:focus-within { border-color: var(--phosphor); }
.insp-field.invalid { border-color: var(--danger); }
.insp-field input { flex: 1; min-width: 0; outline: none; }
.insp-field em { font: normal .8rem var(--mono); color: var(--faint); }
.insp-help { margin: 6px 0 0; font-size: .74rem; color: var(--faint); line-height: 1.45; }
.insp-help.err { color: var(--danger); }
.insp-actions { display: flex; gap: 8px; margin-top: 12px; }
.insp-actions .btn { height: 32px; font-size: .8rem; }

/* Log */
.console {
    flex: 1; min-height: 0; overflow-y: auto;
    margin: 0; padding: 0; list-style: none;
    font: .76rem/1.6 var(--mono);
}
.console li { display: flex; gap: 10px; padding: 1px 0; color: var(--muted); }
.console time { color: var(--faint); flex-shrink: 0; }
.console li.ok span { color: var(--phosphor); }
.console li.err span { color: var(--danger); }
.console li.warn span { color: var(--amber); }
.console li:last-child { animation: fadein .3s ease; }

/* Status bar */
.statusbar {
    grid-area: status;
    display: flex; gap: 22px; align-items: center;
    padding: 0 4px;
    font: .72rem var(--mono); color: var(--faint);
}
.statusbar .kbd-help { margin-left: auto; }
kbd {
    font: .68rem var(--mono);
    padding: 1px 5px;
    border: 1px solid var(--line-strong);
    border-radius: 4px;
    color: var(--muted);
}
.tool kbd { border: none; padding: 0; }

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 8px; }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
    * { animation: none !important; transition: none !important; }
}

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
    .app { grid-template-columns: 84px minmax(0, 1fr) 340px; }
    .stage-hint { display: none; }
}

@media (max-width: 960px) {
    .app {
        height: auto;
        grid-template-columns: 1fr;
        grid-template-rows: auto auto 60vh auto auto;
        grid-template-areas: "top" "rail" "stage" "side" "status";
    }
    .topbar { flex-wrap: wrap; }
    .brand { width: 100%; }
    .transport { padding-left: 0; border-left: none; }
    .rail { flex-direction: row; overflow-x: auto; }
    .rail-sep { width: 1px; height: auto; margin: 6px 2px; }
    .tool { min-width: 64px; }
    .log { min-height: 200px; }
    .statusbar { flex-wrap: wrap; gap: 10px 18px; padding-bottom: 8px; }
    .statusbar .kbd-help { margin-left: 0; }
}
