
/* ── Layout ── */
html, body {
    height: 100%;
    overflow: hidden;
}
body {
    background: #f1f5f9;
    display: flex;
    flex-direction: column;
}

.app-header {
    background: var(--navy);
    padding: 0 20px;
    height: 48px;
    border-bottom: 1px solid rgba(255,255,255,0.07);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
}
body.fullscreen-mode .app-header { display: none; }
.app-header__back {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    color: #64748b;
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: color 0.15s;
    width: 80px;
}
.app-header__back:hover { color: #e2e8f0; }
.app-header__title {
    font-size: 15px;
    font-weight: 800;
    color: #e2e8f0;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    white-space: nowrap;
}
.app-header__right {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 80px;
    justify-content: flex-end;
    position: relative;
}
.settings-btn {
    background: none;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 6px;
    color: #94a3b8;
    cursor: pointer;
    padding: 5px 9px;
    font-size: 15px;
    line-height: 1;
    transition: all 0.12s;
}
.settings-btn:hover { color: #e2e8f0; border-color: rgba(255,255,255,0.25); }
.settings-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: #1e293b;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 10px;
    padding: 6px;
    min-width: 200px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
    z-index: 200;
}
.settings-dropdown.open { display: block; }
.settings-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 13px;
    color: #cbd5e1;
    cursor: pointer;
    transition: background 0.12s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}
.settings-item:hover { background: rgba(255,255,255,0.07); color: #e2e8f0; }
.settings-item__icon { font-size: 14px; width: 18px; text-align: center; }
.settings-divider {
    height: 1px;
    background: rgba(255,255,255,0.07);
    margin: 4px 0;
}

.menu-btn {
    background: none;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 6px;
    color: #94a3b8;
    cursor: pointer;
    padding: 5px 9px;
    font-size: 15px;
    line-height: 1;
    transition: all 0.12s;
}
.menu-btn:hover { color: #e2e8f0; border-color: rgba(255,255,255,0.25); }
.menu-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    left: 18px;
    background: #1e293b;
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: 10px;
    padding: 6px;
    min-width: 200px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
    z-index: 200;
}
.menu-dropdown.open { display: block; }
.menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 13px;
    color: #cbd5e1;
    cursor: pointer;
    transition: background 0.12s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
}
.menu-item:hover { background: rgba(255,255,255,0.07); color: #e2e8f0; }
.menu-item__icon { font-size: 14px; width: 18px; text-align: center; }
.menu-divider {
    height: 1px;
    background: rgba(255,255,255,0.07);
    margin: 4px 0;
}

.workspace {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: 1fr 400px;
    grid-template-rows: 3fr 2fr;
    gap: 10px;
    padding: 10px;
    overflow: hidden;
}

/* ── Panels ── */
.panel {
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-height: 0;
}
.panel--circuit  { grid-column: 1; grid-row: 1; overflow: visible; }
.panel--topology { grid-column: 2; grid-row: 1; }
.panel--results  { grid-column: 1; grid-row: 2; }
.panel--analysis { grid-column: 2; grid-row: 2; }

.panel__head {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    column-gap: 8px;
    padding: 11px 18px;
    border-bottom: 1px solid #e2e8f0;
    background: #f8fafc;
    position: relative;
}
.panel__head h3 {
    grid-column: 2;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: #64748b;
    text-align: center;
    margin: 0;
}
.panel__head .menu-btn {
    grid-column: 1;
    justify-self: start;
}
.panel__head-badge {
    grid-column: 3;
    justify-self: end;
    font-size: 12px;
    font-weight: 600;
    color: #94a3b8;
}
.panel__body {
    padding: 14px 18px;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ── Gate palette ── */
.gate-palette {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 14px;
}
.g-btn {
    padding: 5px 13px;
    border: 1.5px solid #e2e8f0;
    border-radius: 6px;
    background: white;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    font-weight: 700;
    color: #334155;
    cursor: pointer;
    transition: all 0.12s;
}
.g-btn:hover { border-color: #3b82f6; color: #3b82f6; }
.g-btn.active { background: #3b82f6; border-color: #3b82f6; color: white; }
.g-btn.two-q { border-style: dashed; }

/* ── Gate add row ── */
.gate-add-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
    min-height: 34px;
}
.gate-add-row select {
    padding: 5px 8px;
    border: 1.5px solid #e2e8f0;
    border-radius: 6px;
    background: white;
    font-size: 13px;
    font-family: 'JetBrains Mono', monospace;
    color: #1e293b;
    cursor: pointer;
}
.gate-add-row select:focus { outline: none; border-color: #3b82f6; }
.gate-add-row .hint { font-size: 13px; color: #94a3b8; }
.add-btn {
    padding: 5px 14px;
    background: #3b82f6;
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.12s;
}
.add-btn:hover { background: #2563eb; }

/* ── Circuit diagram ── */
.circuit-wrap {
    flex: 1;
    min-height: 0;
    overflow: auto;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    display: flex;
    align-items: flex-start;
}
.circuit-empty {
    width: 100%;
    text-align: center;
    color: #94a3b8;
    font-size: 14px;
    padding: 36px 20px;
    line-height: 1.6;
}

/* ── Topology ── */
.topo-hint {
    font-size: 12px;
    color: #94a3b8;
    line-height: 1.4;
    margin-bottom: 8px;
    flex-shrink: 0;
}
.topo-hint strong { color: #64748b; }
.topo-svg-wrap {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    overflow: hidden;
    cursor: crosshair;
    flex-shrink: 0;
}

.section-label {
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: #94a3b8;
    margin: 10px 0 6px;
    flex-shrink: 0;
}
.preset-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
.preset-btn {
    padding: 4px 11px;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    background: #f8fafc;
    font-size: 12px;
    font-weight: 600;
    color: #64748b;
    cursor: pointer;
    transition: all 0.12s;
}
.preset-btn:hover { border-color: #3b82f6; color: #3b82f6; background: #eff6ff; }

/* ── Analyze bar ── */
.analyze-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 24px;
    background: var(--navy);
    flex-wrap: wrap;
    flex-shrink: 0;
}
.a-btn {
    padding: 9px 18px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    border: none;
    transition: all 0.12s;
}
.a-btn--primary { background: #3b82f6; color: white; }
.a-btn--primary:hover { background: #2563eb; }
.a-btn--secondary {
    background: rgba(255,255,255,0.08);
    color: #e2e8f0;
    border: 1px solid rgba(255,255,255,0.12);
}
.a-btn--secondary:hover { background: rgba(255,255,255,0.14); }
.a-btn--ghost {
    background: transparent;
    color: #f87171;
    border: 1px solid rgba(248,113,113,0.25);
    margin-left: auto;
}
.a-btn--ghost:hover { background: rgba(248,113,113,0.08); }

/* ── Results / Analysis ── */
#results-wrap, #analysis-wrap {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
}
.results-body { padding: 14px 18px; }
.results-placeholder { font-size: 14px; color: #94a3b8; }

/* ── Probabilities ── */
.prob-toggle {
    display: flex;
    gap: 6px;
    margin-bottom: 12px;
}
.prob-toggle__btn {
    padding: 4px 11px;
    border: 1px solid #e2e8f0;
    border-radius: 5px;
    background: #f8fafc;
    font-size: 12px;
    font-weight: 600;
    color: #64748b;
    cursor: pointer;
    transition: all 0.12s;
}
.prob-toggle__btn:hover { border-color: #3b82f6; color: #3b82f6; }
.prob-toggle__btn.active { background: #3b82f6; border-color: #3b82f6; color: white; }

.prob-list { display: flex; flex-direction: column; gap: 5px; }
.prob-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
}
.prob-row__label { color: #334155; min-width: 0; flex-shrink: 0; }
.prob-row__bar {
    flex: 1;
    height: 8px;
    background: #e2e8f0;
    border-radius: 4px;
    overflow: hidden;
}
.prob-row__fill { display: block; height: 100%; background: #3b82f6; border-radius: 4px; }
.prob-row__pct { color: #64748b; min-width: 46px; text-align: right; flex-shrink: 0; }
.prob-row__amp { margin-left: auto; color: #64748b; }

.compat-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 9px 16px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 700;
    margin-bottom: 12px;
}
.compat-badge--ok  { background: #f0fdf4; color: #16a34a; border: 1px solid #bbf7d0; }
.compat-badge--err { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }

.violation-list { list-style: none; display: flex; flex-direction: column; gap: 5px; }
.violation-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 6px;
    font-size: 12px;
    font-family: 'JetBrains Mono', monospace;
    color: #1e293b;
}

.gate-list { display: flex; flex-direction: column; gap: 5px; }
.gate-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
}
.gate-item__idx { color: #cbd5e1; min-width: 22px; font-size: 11px; }
.gate-item__type { font-weight: 700; color: #3b82f6; min-width: 42px; }
.gate-item__qubits { color: #334155; }
.gate-item__tag {
    margin-left: auto;
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 2px 6px;
    border-radius: 3px;
}
.tag--inserted { background: #fffbeb; color: #d97706; border: 1px solid #fde68a; }
.tag--original { background: #eff6ff; color: #3b82f6; border: 1px solid #bfdbfe; }
.tag--error    { background: #fef2f2; color: #dc2626; border: 1px solid #fecaca; }

.small-btn {
    padding: 3px 8px;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    background: white;
    font-size: 11px;
    color: #94a3b8;
    cursor: pointer;
    transition: all 0.12s;
}
.small-btn:hover { color: #dc2626; border-color: #fca5a5; }

/* ── QASM modal ── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 400;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
}
.modal-overlay.open { display: flex; }
.modal {
    background: white;
    border-radius: 14px;
    padding: 22px 24px;
    width: min(580px, 92vw);
    max-height: 82vh;
    display: flex;
    flex-direction: column;
    gap: 14px;
    box-shadow: 0 24px 64px rgba(0,0,0,0.35);
}
body.dark .modal {
    background: #1e293b;
    border: 1px solid rgba(255,255,255,0.08);
}
.modal__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
.modal__title { font-size: 15px; font-weight: 700; color: #1e293b; }
body.dark .modal__title { color: #e2e8f0; }
.modal__close-btn {
    background: none; border: none; cursor: pointer;
    color: #94a3b8; font-size: 18px; line-height: 1; padding: 2px 6px;
    border-radius: 4px; transition: color 0.12s;
}
.modal__close-btn:hover { color: #1e293b; }
body.dark .modal__close-btn:hover { color: #e2e8f0; }
.modal__hint {
    font-size: 12px; color: #64748b; line-height: 1.6; flex-shrink: 0;
}
body.dark .modal__hint { color: #475569; }
.modal__hint code {
    font-family: 'JetBrains Mono', monospace;
    background: #f1f5f9; padding: 1px 5px; border-radius: 3px; font-size: 11px;
}
body.dark .modal__hint code { background: #0f172a; color: #94a3b8; }
.modal__code {
    flex: 1; min-height: 0; overflow-y: auto;
    background: #f8fafc; border: 1px solid #e2e8f0; border-radius: 8px;
    padding: 14px 16px;
    font-family: 'JetBrains Mono', monospace; font-size: 12px;
    color: #1e293b; white-space: pre; line-height: 1.7;
}
body.dark .modal__code {
    background: #0f172a; border-color: rgba(255,255,255,0.07); color: #e2e8f0;
}
.modal__textarea {
    width: 100%; box-sizing: border-box; resize: vertical;
    font-weight: 400;
}
.modal__textarea:focus { outline: none; border-color: #3b82f6; }
.modal__actions { display: flex; gap: 8px; flex-shrink: 0; align-items: center; }
.modal__dl-btn {
    padding: 8px 16px; border-radius: 8px; font-size: 13px; font-weight: 700;
    cursor: pointer; border: 1px solid #e2e8f0; background: #f8fafc; color: #334155;
    transition: all 0.12s;
}
.modal__dl-btn:hover { border-color: #3b82f6; color: #3b82f6; }
body.dark .modal__dl-btn { background: #0f172a; border-color: rgba(255,255,255,0.1); color: #94a3b8; }
body.dark .modal__dl-btn:hover { border-color: #3b82f6; color: #3b82f6; }

/* ── Drag & drop ── */
.g-btn[draggable="true"] { cursor: grab; }
.g-btn[draggable="true"]:active { cursor: grabbing; }
.g-btn[draggable="true"]:active, svg [draggable="true"]:active { cursor: grabbing; }
#circuit-status {
    display: none;
    margin-top: 6px;
    padding: 6px 12px;
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: 6px;
    font-size: 12px;
    color: #1d4ed8;
    font-weight: 500;
    flex-shrink: 0;
}

/* ── Dark mode ── */
body.dark { background: #0f172a; }
body.dark .panel { background: #1e293b; border-color: rgba(255,255,255,0.07); }
body.dark .panel__head { background: #0f172a; border-color: rgba(255,255,255,0.07); }
body.dark .panel__head h3 { color: #64748b; }
body.dark .panel__head-badge { color: #475569; }
body.dark .g-btn { background: #1e293b; border-color: rgba(255,255,255,0.1); color: #cbd5e1; }
body.dark .g-btn:hover { border-color: #3b82f6; color: #3b82f6; }
body.dark .g-btn.active { background: #3b82f6; border-color: #3b82f6; color: white; }
body.dark .gate-add-row select { background: #1e293b; border-color: rgba(255,255,255,0.1); color: #e2e8f0; color-scheme: dark; }
body.dark .gate-add-row .hint { color: #475569; }
body.dark .circuit-wrap { background: #0f172a; border-color: rgba(255,255,255,0.07); }
body.dark .circuit-empty { color: #475569; }
body.dark .topo-hint { color: #475569; }
body.dark .topo-hint strong { color: #64748b; }
body.dark .topo-svg-wrap { background: #0f172a; border-color: rgba(255,255,255,0.07); }
body.dark .section-label { color: #475569; }
body.dark .preset-btn { background: #1e293b; border-color: rgba(255,255,255,0.1); color: #64748b; }
body.dark .preset-btn:hover { border-color: #3b82f6; color: #3b82f6; background: rgba(59,130,246,0.08); }
body.dark .results-placeholder { color: #475569; }
body.dark .compat-badge--ok { background: rgba(22,163,74,0.1); border-color: rgba(22,163,74,0.2); }
body.dark .compat-badge--err { background: rgba(220,38,38,0.1); border-color: rgba(220,38,38,0.2); }
body.dark .violation-item { background: rgba(220,38,38,0.05); border-color: rgba(220,38,38,0.15); color: #e2e8f0; }
body.dark .gate-item { background: #0f172a; border-color: rgba(255,255,255,0.07); }
body.dark .gate-item__qubits { color: #cbd5e1; }
body.dark .gate-item__idx { color: #334155; }
body.dark .tag--inserted { background: rgba(217,119,6,0.1); border-color: rgba(217,119,6,0.25); }
body.dark .tag--original { background: rgba(59,130,246,0.1); border-color: rgba(59,130,246,0.2); }
body.dark .tag--error { background: rgba(220,38,38,0.1); border-color: rgba(220,38,38,0.2); }
body.dark .small-btn { background: #1e293b; border-color: rgba(255,255,255,0.1); color: #475569; }
body.dark #circuit-status { background: rgba(59,130,246,0.1); border-color: rgba(59,130,246,0.2); color: #93c5fd; }
body.dark .prob-toggle__btn { background: #0f172a; border-color: rgba(255,255,255,0.1); color: #64748b; }
body.dark .prob-toggle__btn:hover { border-color: #3b82f6; color: #3b82f6; }
body.dark .prob-row { background: #0f172a; border-color: rgba(255,255,255,0.07); }
body.dark .prob-row__label { color: #cbd5e1; }
body.dark .prob-row__bar { background: rgba(255,255,255,0.08); }
body.dark .prob-row__pct, body.dark .prob-row__amp { color: #94a3b8; }

@media (max-width: 860px) {
    .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto auto;
    }
    .panel--topology { grid-column: 1; grid-row: 2; }
    .panel--analysis { grid-column: 1; grid-row: 3; }
    .panel--results  { grid-column: 1; grid-row: 4; }
}