:root {
    --bg-body:             #0e1018;
    --bg-sidebar:          #161a26;
    --bg-card:             #1e2334;
    --bg-card-hover:       #252b40;
    --bg-input:            #11141f;
    --border-main:         #2b3147;
    --border-strong:       #394264;
    --text-main:           #eef2f8;
    --text-muted:          #9aa6bd;
    --text-label:          #cdd6e6;
    --accent:              #4a8bf7;
    --accent-strong:       #2563eb;
    --accent-soft:         rgba(59, 130, 246, 0.14);
    --success:             #10b981;
    --danger:              #ef4444;
    --btn-secondary:       #2c3450;
    --btn-secondary-hover: #3a456a;
    --shadow-card:         0 1px 3px rgba(0, 0, 0, 0.22);

    --sidebar-w:  296px;
    --card-pad:   12px;
    --radius:     10px;
    --fs-title:   15.5px;
    --fs-sub:     11.5px;
    --fs-trigger: 11px;
    --fs-label:   12px;
    --fs-input:   13px;
    --fs-hint:    11px;
    --fs-mono:    11.5px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg-body);
    color: var(--text-main);
    display: flex;
    height: 100dvh;
}

/* ─── SIDEBAR ─────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-main);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
}

.sidebar-inner {
    padding: 14px 10px 20px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* ─── HEADER ──────────────────────────────────── */
.sim-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-bottom: 4px;
}

.sim-logo {
    width: 38px;
    height: 38px;
    border-radius: 9px;
    background: var(--accent-soft);
    border: 1.5px solid var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 19px;
    flex-shrink: 0;
}

.sim-title    { font-size: var(--fs-title); font-weight: 700; line-height: 1.25; }
.sim-subtitle { font-size: var(--fs-sub);   color: var(--text-muted); margin-top: 2px; }

/* ─── CARDS ───────────────────────────────────── */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border-main);
    border-radius: var(--radius);
    box-shadow: var(--shadow-card);
    overflow: hidden;
}

/* Card with collapsible trigger (mode, info) */
.card-trigger {
    width: 100%;
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px var(--card-pad);
    color: var(--text-muted);
    font-size: var(--fs-trigger);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    transition: color 0.15s;
    font-family: inherit;
}
.card-trigger:hover { color: var(--text-main); }

.card-chevron { font-size: 10px; transition: transform 0.2s; }
.card.is-expanded .card-chevron { transform: rotate(180deg); }

.card-body { display: none; padding: 0 var(--card-pad) var(--card-pad); }
.card.is-expanded .card-body { display: block; }

/* Non-collapsible body (atom cards) */
.card-body-static {
    padding: 0 var(--card-pad) var(--card-pad);
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* Atom card section label */
.atom-card-label {
    padding: 8px var(--card-pad) 2px;
    font-size: var(--fs-trigger);
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.07em;
}

/* ─── RESULT CARD ─────────────────────────────── */
.result-header {
    padding: 9px var(--card-pad);
    font-size: var(--fs-trigger);
    font-weight: 700;
    color: var(--success);
    text-transform: uppercase;
    letter-spacing: 0.07em;
    border-bottom: 1px solid var(--border-main);
}

.compound-formula {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-main);
    text-align: center;
    margin-bottom: 3px;
    letter-spacing: 0.03em;
}

.result-detail {
    font-size: 11px;
    color: var(--text-muted);
    text-align: center;
}

.result-ions {
    font-size: 11.5px;
    color: var(--text-muted);
    text-align: center;
    margin-top: 6px;
}

/* ─── SELECT ──────────────────────────────────── */
select {
    width: 100%;
    padding: 6px 8px;
    background: var(--bg-input);
    color: var(--text-main);
    border: 1px solid var(--border-strong);
    border-radius: 6px;
    font-family: inherit;
    font-size: var(--fs-input);
    cursor: pointer;
    outline: none;
}
select:focus { border-color: var(--accent); }

/* ─── STATUS BOX ──────────────────────────────── */
.status-box {
    background: var(--bg-input);
    border: 1px solid var(--border-main);
    border-radius: 7px;
    padding: 7px 9px;
    font-size: var(--fs-mono);
    font-family: 'Consolas', 'Fira Mono', monospace;
    line-height: 1.6;
    color: var(--text-label);
    min-height: 60px;
}

.ui-empty {
    color: var(--text-muted);
    font-style: italic;
    font-size: 11px;
}

/* ─── BUTTONS ─────────────────────────────────── */
button {
    background: var(--btn-secondary);
    color: var(--text-label);
    border: 1px solid var(--border-main);
    border-radius: 7px;
    padding: 6px 10px;
    font-family: inherit;
    font-size: 11.5px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
button:hover:not(:disabled) {
    background: var(--btn-secondary-hover);
    color: var(--text-main);
    border-color: var(--border-strong);
}
button:disabled { opacity: 0.28; cursor: not-allowed; }

/* Fondo un tono más oscuro que el acento para que el texto blanco se lea bien (contraste 4,5:1) */
.btn-primary {
    background: var(--accent-strong);
    color: #fff;
    border-color: var(--accent-strong);
}
.btn-primary:hover:not(:disabled) {
    background: #1d4ed8;
    color: #fff;
    border-color: var(--accent-strong);
}

.btn-group          { display: flex; gap: 5px; }
.btn-group button   { flex: 1; font-size: 10.5px; padding: 5px 3px; }

.reset-row        { display: flex; }
.reset-row button { width: 100%; }

/* ─── CHECKS ──────────────────────────────────── */
.check-pass { color: var(--success); font-weight: 600; }
.check-fail { color: var(--danger); font-weight: 600; }
.check-todo { color: var(--text-muted); }


/* ─── MODE CARD (selector always visible) ────── */
.mode-card {
    padding: 10px var(--card-pad);
}

/* ─── INFO SECTION ────────────────────────────── */
.info-section p {
    font-size: var(--fs-hint);
    color: var(--text-muted);
    line-height: 1.65;
    margin-bottom: 6px;
}
.info-section p:last-child { margin-bottom: 0; }
.info-section b  { color: var(--text-label); }
.info-section em { color: var(--accent); font-style: normal; }
.info-section .crystal-note { color: #FBBF24; font-size: 10.5px; line-height: 1.5; }
.info-section .crystal-props {
    margin: 0 0 6px 16px;
    padding: 0;
    font-size: var(--fs-hint);
    color: var(--text-muted);
    line-height: 1.55;
}

/* ─── CANVAS HOLDER ───────────────────────────── */
#canvas-holder {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    padding: 14px;
    gap: 10px;
    background: var(--bg-body);
    overflow: hidden;
}

/* Frame de simulación — canvas contenido con borde */
#sim-frame {
    flex: 1;
    min-height: 0;
    position: relative;
    background: #0F172A;
    border-radius: 14px;
    overflow: hidden;
    border: 1px solid var(--border-strong);
    box-shadow: 0 0 0 1px rgba(59,130,246,0.06), 0 4px 24px rgba(0,0,0,0.45);
}

/* El lienzo ocupa el ancho del marco; si es más estrecho que el tamaño lógico
   mínimo (720 px, ver frameCanvasSize en sketch.js) se escala visualmente. */
#sim-frame canvas { display: block; width: 100% !important; height: auto !important; }

/* ─── ATOM CONTROLS ROW ───────────────────────── */
#atom-controls-row {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    height: 170px;
}

.atom-ctrl {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: var(--bg-card);
    border: 1px solid var(--border-main);
    border-radius: var(--radius);
    padding: 8px;
    overflow: hidden;
    box-shadow: var(--shadow-card);
}

.atom-ctrl-label {
    font-size: var(--fs-trigger);
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.07em;
    padding-bottom: 4px;
    border-bottom: 1px solid var(--border-main);
    flex-shrink: 0;
}

.atom-ctrl select {
    font-size: 11px;
    padding: 4px 6px;
    flex-shrink: 0;
}

.atom-ctrl .status-box {
    flex: 1;
    min-height: 0;
    font-size: 10.5px;
    line-height: 1.4;
    overflow: hidden;
    padding: 5px 7px;
}

.atom-ctrl .btn-group button {
    font-size: 10.5px;
    padding: 5px 3px;
}

/* ─── BOND CONNECTOR (covalent) ───────────────── */
.bond-connector {
    width: 54px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
}

.bond-connector::before,
.bond-connector::after {
    content: '';
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    width: 1px;
    background: var(--border-strong);
    opacity: 0.4;
}
.bond-connector::before { top: 0;    height: calc(50% - 30px); }
.bond-connector::after  { bottom: 0; height: calc(50% - 30px); }

.bond-connector-btn {
    width: 46px;
    padding: 8px 2px;
    font-size: 9px;
    font-weight: 700;
    line-height: 1.5;
    text-align: center;
    border-radius: 10px;
    letter-spacing: 0.03em;
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(59, 130, 246, 0.08);
}

.bond-dative-btn {
    border-color: #E879F9;
    color: #E879F9;
    background: rgba(232, 121, 249, 0.08);
    margin-top: 4px;
}
.bond-dative-btn:hover:not(:disabled) {
    background: rgba(232, 121, 249, 0.16);
}

.bond-recover-btn {
    border-color: var(--border-strong);
    color: var(--text-muted);
    background: transparent;
    margin-top: 4px;
}
.bond-recover-btn:hover:not(:disabled) {
    border-color: var(--danger);
    color: var(--danger);
    background: rgba(239, 68, 68, 0.08);
}

/* ─── PANTALLAS ESTRECHAS (móvil, tablet en vertical) ───
   La simulación pasa arriba a todo el ancho, con proporción 4:3, y los
   controles debajo. En ordenador no cambia nada. */
@media (max-width: 960px) {
    html, body { height: auto; overflow: auto; }
    body { flex-direction: column; height: auto; }
    #canvas-holder { order: -1; padding: 10px; overflow: visible; }
    #sim-frame { flex: none; aspect-ratio: 4 / 3; }
    .sidebar { width: 100%; overflow: visible; border-right: none; border-top: 1px solid var(--border-main); }
    #atom-controls-row { height: auto; min-height: 170px; }
}

/* En móvil los tres átomos se apilan; los conectores de enlace quedan entre ellos. */
@media (max-width: 600px) {
    #atom-controls-row { flex-direction: column; min-height: 0; }
    .atom-ctrl { flex: none; min-height: 150px; }
    .bond-connector { width: 100%; flex-direction: row; gap: 6px; padding: 4px 0; }
    .bond-connector::before, .bond-connector::after { display: none; }
}
