/* ---- Diagram Toolbar & Sparkline Controls (M8) ---- */
#diagram-toolbar, #diagram3d-toolbar {
    position: absolute; top: 8px; right: 8px;
    display: flex; gap: 4px; z-index: 5; align-items: center;
}

#stale-badge {
    background: #fef3c7;
    color: #92400e;
    border: 1px solid rgba(217, 119, 6, 0.4);
    border-radius: 10px;
    padding: 1px 8px;
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
}
.view-controls {
    display: inline-flex; gap: 2px;
    border-left: 1px solid var(--border); padding-left: 4px; margin-left: 2px;
}
/* ---- Variable Search ---- */
.var-search-wrap {
    position: relative; display: inline-block; vertical-align: middle;
}
#var-search {
    width: 160px; padding: 3px 8px; font-size: 12px;
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    outline: none; font-family: var(--font-sans);
}
#var-search:focus { border-color: var(--accent); }
.var-search-dropdown {
    display: none; position: absolute; top: 100%; left: 0; right: 0;
    max-height: 200px; overflow-y: auto; background: var(--surface);
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    list-style: none; z-index: 100; box-shadow: var(--shadow-sm);
    margin-top: 2px;
}
.var-search-dropdown.show { display: block; }
.var-search-dropdown li {
    padding: 4px 8px; font-size: 12px; cursor: pointer;
}
.var-search-dropdown li:hover, .var-search-dropdown li.active {
    background: var(--accent-light); color: var(--accent);
}
.var-search-dropdown li .var-type {
    font-size: 10px; color: var(--text-muted); margin-left: 4px;
}
/* ---- Variable Info Popup ---- */
.var-popup {
    position: fixed; z-index: 200;
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    min-width: 280px; max-width: 420px; font-size: 13px;
    font-family: var(--font-sans);
}
.var-popup-header {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 12px; border-bottom: 1px solid var(--border-light);
}
.var-popup-title { font-weight: 600; flex: 1; }
.var-popup-type {
    font-size: 10px; text-transform: uppercase; color: var(--text-muted);
    background: var(--surface-hover); padding: 2px 6px; border-radius: 4px;
}
.var-popup-close {
    background: none; border: none; font-size: 18px; cursor: pointer;
    color: var(--text-muted); line-height: 1;
}
.var-popup-close:hover { color: var(--text); }
.var-popup-body { padding: 8px 12px; }
.var-popup-row { margin-bottom: 6px; }
.var-popup-row label {
    display: block; font-size: 10px; text-transform: uppercase;
    color: var(--text-muted); margin-bottom: 2px;
}
.var-popup-row code {
    display: block; font-family: var(--font-mono); font-size: 12px;
    background: var(--surface-hover); padding: 4px 8px; border-radius: 4px;
    white-space: pre-wrap; word-break: break-all;
}
.var-popup-row span { font-size: 12px; }
.spark-controls {
    display: inline-flex; align-items: center; gap: 8px;
    font-size: 10px; color: #666; margin-right: 8px;
}
.spark-controls label {
    display: inline-flex; align-items: center; gap: 2px; cursor: pointer;
}
.spark-controls input[type="checkbox"] { margin: 0; }

/* ---- Reset & Base ---- */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #ffffff;
    --surface: #ffffff;
    --surface-hover: #f0f0f2;
    --surface-active: #e8e8ec;
    --border: #e0e0e4;
    --border-light: #ebebef;
    --text: #1d1d1f;
    --text-secondary: #6e6e73;
    --text-muted: #aeaeb2;
    --accent: #0071e3;
    --accent-light: #e8f2fc;
    --accent-hover: #0077ED;
    --user-bg: #e8f2fc;
    --assistant-bg: #f9f9fb;
    --error-bg: #fef2f2;
    --error-text: #dc2626;
    --success: #34c759;
    --success-bg: #f0fdf4;
    --warning: #ff9500;
    --stock-color: #0071e3;
    --flow-color: #34c759;
    --aux-color: #ff9500;
    --connector-color: #aeaeb2;
    --font-mono: 'SF Mono', 'Menlo', 'Consolas', 'Courier New', monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    --radius: 8px;
    --radius-sm: 6px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.03);
    --topbar-h: 48px;
    --panel-header-h: 40px;
    --panel-left-w: 230px;
}

html, body {
    height: 100%;
    font-family: var(--font-sans);
    font-size: 13px;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ---- Top Bar ---- */
#topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--topbar-h);
    padding: 0 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    box-shadow: var(--shadow-sm);
    z-index: 10;
    position: relative;
}

.topbar-left {
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.logo {
    font-size: 17px;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 1.5px;
}

.logo-sub {
    font-size: 11px;
    color: var(--text-muted);
    letter-spacing: 0.3px;
}

.topbar-center {
    flex: 1;
    text-align: center;
}

.model-name {
    color: var(--text-secondary);
    font-family: var(--font-mono);
    background: var(--bg);
    padding: 3px 12px;
    border-radius: 12px;
    font-size: 11px;
}

.topbar-right {
    display: flex;
    gap: 8px;
}

.topbar-right button,
.small-btn {
    padding: 5px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.3px;
    line-height: 1;
    transition: all 0.15s;
}

.topbar-right button:hover,
.small-btn:hover {
    background: var(--surface-hover);
    color: var(--text);
    border-color: var(--accent);
}

/* ---- Three-Panel Layout ---- */
#panels {
    display: flex;
    height: calc(100vh - var(--topbar-h));
    overflow: hidden;
}

/* ---- Left Panel: Resources ---- */
#panel-left {
    width: var(--panel-left-w);
    min-width: 180px;
    max-width: 400px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
}

#panel-left .panel-header {
    display: flex;
    align-items: center;
    height: var(--panel-header-h);
    padding: 0 14px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

#panel-left .panel-sections {
    flex: 1;
    overflow-y: auto;
}

.panel-section {
    border-bottom: 1px solid var(--border-light);
}

.section-title {
    padding: 9px 14px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
    cursor: pointer;
    transition: color 0.15s;
    user-select: none;
}

.section-title:hover {
    color: var(--text);
    background: var(--surface-hover);
}

.section-title::before {
    content: '\25B8';
    display: inline-block;
    margin-right: 6px;
    font-size: 10px;
    transition: transform 0.15s;
    color: var(--text-muted);
}

.section-title.expanded::before {
    transform: rotate(90deg);
}

.resource-list {
    list-style: none;
    padding: 0 0 6px 0;
    display: none;
}

.resource-list.show {
    display: block;
}

.resource-item {
    padding: 5px 14px 5px 28px;
    font-size: 12px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.1s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border-radius: 4px;
    margin: 0 6px;
}

.resource-item:hover {
    background: var(--accent-light);
    color: var(--accent);
}

.resource-item.active {
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 500;
}

.resource-item.placeholder {
    font-style: italic;
    cursor: default;
    font-size: 11px;
    color: var(--text-muted);
}

.resource-item.placeholder:hover {
    background: transparent;
    color: var(--text-muted);
}

.resource-icon {
    margin-right: 6px;
    font-size: 10px;
    opacity: 0.5;
}

/* ---- Resize Handles ---- */
.resize-handle {
    width: 4px;
    cursor: col-resize;
    background: transparent;
    transition: background 0.15s;
    flex-shrink: 0;
    position: relative;
}

.resize-handle::after {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    width: 2px;
    transform: translateX(-50%);
    background: var(--border);
    transition: background 0.15s, width 0.15s;
}

.resize-handle:hover::after,
.resize-handle.dragging::after {
    background: var(--accent);
    width: 3px;
}

/* ---- Center Panel: Canvas ---- */
#panel-center {
    flex: 1;
    min-width: 300px;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    overflow: hidden;
}

.canvas-tabs {
    display: flex;
    align-items: center;
    height: var(--panel-header-h);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    padding: 0 8px;
    gap: 2px;
}

.canvas-tab {
    padding: 0 18px;
    height: 100%;
    display: inline-flex;
    align-items: center;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.15s;
}

.canvas-tab:hover {
    color: var(--text);
}

.canvas-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.canvas-view {
    flex: 1;
    display: none;
    position: relative;
    overflow: hidden;
    background: #ffffff;
}

.canvas-view.active {
    display: flex;
    flex-direction: column;
}

/* The 3D canvas fills its container via CSS so the browser scales it instantly
   during a resize (no JS). The expensive WebGL drawing-buffer reallocation is
   decoupled: it happens once per settled size in JS (renderer.setSize(_, _, false)),
   not on every drag tick. See FlatPlanDiagram3D.resize(). */
#diagram3d-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: block;
}

.canvas-empty {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
}

.canvas-empty p {
    font-size: 15px;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.canvas-empty .hint {
    font-size: 12px;
    color: var(--border);
}

/* SFD Canvas */
#sfd-canvas {
    width: 100%;
    height: 100%;
    background: var(--bg);
}

#sfd-canvas .stock {
    fill: var(--surface);
    stroke: var(--stock-color);
    stroke-width: 2;
    cursor: pointer;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.08));
}

#sfd-canvas .stock:hover {
    stroke-width: 2.5;
    filter: drop-shadow(0 2px 6px rgba(0,113,227,0.2));
}

#sfd-canvas .stock.selected {
    stroke: var(--accent);
    stroke-width: 3;
    filter: drop-shadow(0 2px 8px rgba(0,113,227,0.3));
}

#sfd-canvas .flow-line {
    stroke: var(--flow-color);
    stroke-width: 2;
    fill: none;
}

#sfd-canvas .flow-valve {
    fill: var(--surface);
    stroke: var(--flow-color);
    stroke-width: 1.5;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.06));
}

#sfd-canvas .aux-circle {
    fill: var(--surface);
    stroke: var(--aux-color);
    stroke-width: 1.5;
    cursor: pointer;
    filter: drop-shadow(0 1px 2px rgba(0,0,0,0.06));
}

#sfd-canvas .aux-circle:hover {
    stroke-width: 2;
    filter: drop-shadow(0 2px 6px rgba(255,149,0,0.2));
}

#sfd-canvas .connector {
    stroke: var(--connector-color);
    stroke-width: 1;
    stroke-dasharray: 5 3;
    fill: none;
    marker-end: url(#arrowhead);
}

#sfd-canvas .label {
    fill: var(--text);
    font-size: 11px;
    font-family: var(--font-sans);
    text-anchor: middle;
    pointer-events: none;
}

#sfd-canvas .label-stock {
    font-weight: 600;
    font-size: 12px;
    fill: var(--text);
}

/* Chart canvas */
#canvas-chart {
    width: 100%;
    height: 100%;
}

/* View toolbar (shared by Parameters, Chart, Table) */
.view-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
    flex-shrink: 0;
}
.view-toolbar .accent {
    background: var(--accent);
    color: #fff;
    border: none;
}

/* Parameters table */
#params-table-wrap table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
#params-table-wrap th {
    text-align: left;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    padding: 6px 8px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--surface);
}
#params-table-wrap td {
    padding: 4px 8px;
    border-bottom: 1px solid var(--border-light);
}
#params-table-wrap input[type="number"] {
    width: 80px;
    padding: 3px 6px;
    font-size: 13px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg);
    color: var(--text);
}
#params-table-wrap input[type="checkbox"] {
    margin: 0;
    cursor: pointer;
}

/* Chart panels */
.chart-panel {
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 12px;
    background: var(--surface);
}
.chart-panel-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--border-light);
}
.chart-panel-header input[type="text"] {
    flex: 1;
    padding: 4px 8px;
    font-size: 13px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg);
    color: var(--text);
}
.chart-var-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 4px 12px;
}
.chart-var-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    font-size: 12px;
    background: var(--accent-light);
    color: var(--accent);
    border-radius: 12px;
}
.chart-var-tag button {
    background: none;
    border: none;
    color: var(--accent);
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    padding: 0;
}

/* Variable selector modal list */
#var-selector-list label {
    display: block;
    padding: 4px 8px;
    font-size: 13px;
    cursor: pointer;
    border-radius: 4px;
}
#var-selector-list label:hover {
    background: var(--surface-hover);
}
#var-selector-list input[type="checkbox"] {
    margin-right: 8px;
}

/* Table canvas */
.table-scroll {
    flex: 1;
    overflow: auto;
    padding: 12px;
}

.result-table {
    border-collapse: collapse;
    font-family: var(--font-mono);
    font-size: 11px;
    width: 100%;
    background: var(--surface);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.result-table th,
.result-table td {
    padding: 6px 10px;
    border-bottom: 1px solid var(--border-light);
    text-align: right;
    white-space: nowrap;
}

.result-table th {
    background: var(--bg);
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    position: sticky;
    top: 0;
    z-index: 1;
}

.result-table td {
    color: var(--text);
}

.result-table tr:hover td {
    background: var(--surface-hover);
}

/* ---- Scrollbars ---- */
#panel-left .panel-sections::-webkit-scrollbar,
.table-scroll::-webkit-scrollbar {
    width: 6px;
}

#panel-left .panel-sections::-webkit-scrollbar-track,
.table-scroll::-webkit-scrollbar-track {
    background: transparent;
}

#panel-left .panel-sections::-webkit-scrollbar-thumb,
.table-scroll::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

#panel-left .panel-sections::-webkit-scrollbar-thumb:hover,
.table-scroll::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ---- Upload Badge ---- */
.upload-badge {
    display: inline-block;
    background: var(--accent);
    color: white;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 11px;
    margin: 1px;
}

/* ---- Canvas action buttons in tab bar ---- */
.canvas-tab-spacer {
    flex: 1;
}

.canvas-action-btn {
    font-size: 10px !important;
    padding: 3px 10px !important;
    border-radius: 4px !important;
}

/* Simulate: primary action, colored to match the FlatPlan logo (accent). */
#btn-simulate {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

#btn-simulate:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    color: #fff;
}

#btn-simulate.is-busy,
#btn-simulate:disabled {
    opacity: 0.6;
    cursor: wait;
}

/* ---- Mobile navigation bar (hidden on desktop) ---- */
#mobile-nav {
    display: none;
}

/* ---- Responsive: mobile (< 768px) ----
   Single breakpoint: above 768px the left panel is always visible; below it,
   the left panel and mobile-nav swap (no dead zone where neither shows). */
@media (max-width: 768px) {
    :root {
        --topbar-h: 42px;
        --panel-header-h: 36px;
    }

    /* Show mobile nav */
    #mobile-nav {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        height: 36px;
        background: var(--surface);
        border-bottom: 1px solid var(--border);
        padding: 0 8px;
        flex-shrink: 0;
    }

    .mobile-nav-btn {
        flex: 1;
        padding: 5px 0;
        border: 1px solid var(--border);
        border-radius: var(--radius-sm);
        background: var(--surface);
        color: var(--text-secondary);
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        cursor: pointer;
        transition: all 0.15s;
        text-align: center;
    }

    .mobile-nav-btn.active {
        background: var(--accent);
        color: white;
        border-color: var(--accent);
    }

    /* Hide resize handles */
    .resize-handle {
        display: none !important;
    }

    /* Panels: single-panel mode */
    #panels {
        flex-direction: column;
        height: calc(100vh - var(--topbar-h) - 36px); /* subtract mobile-nav height */
        height: calc(100dvh - var(--topbar-h) - 36px);
    }

    #panel-left,
    #panel-center {
        position: absolute;
        top: 0;
        left: 0;
        width: 100% !important;
        min-width: 0 !important;
        max-width: 100% !important;
        height: 100%;
        border: none !important;
        display: none;
    }

    #panels {
        position: relative;
    }

    /* Active mobile panel */
    #panel-left.mobile-active,
    #panel-center.mobile-active {
        display: flex;
    }

    /* Topbar: compact */
    .topbar-center { display: none; }
    .logo-sub { display: none; }
    .logo { font-size: 15px; }
}

.src-chip { font-size: 10px; padding: 1px 6px; border-radius: 8px; margin-left: 4px; white-space: nowrap; }
.src-pinned { background: rgba(10,132,255,.12); color: #0a84ff; }
.src-data { background: rgba(255,149,0,.14); color: #b25000; }
.src-builtin { background: rgba(60,60,67,.10); color: #6e6e73; }

.chart-zero { display: inline-flex; align-items: center; gap: 3px; font-size: 11px; color: var(--text-secondary); cursor: pointer; white-space: nowrap; margin: 0 6px; }

/* ── Landing gate (login-free entry) ─────────────────────────── */
/* Hidden by default; the boot script adds html.gate-open when there is no
   valid token, so the gate paints before the app shell. */
#gate-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    z-index: 12000; display: none;
    align-items: center; justify-content: center;
    background: linear-gradient(180deg, #f5f7fa 0%, #eef1f6 100%);
    padding: 24px;
}
html.gate-open #gate-overlay { display: flex; }
html.gate-open #topbar,
html.gate-open #mobile-nav,
html.gate-open #panels { visibility: hidden; }
.gate-card {
    width: 460px; max-width: 100%;
    background: var(--surface, #fff);
    border: 1px solid var(--border, #e0e0e4);
    border-radius: 18px;
    padding: 44px 40px 30px;
    box-shadow: 0 16px 48px rgba(0,0,0,0.10);
    text-align: center;
}
.gate-brand {
    /* Same wordmark treatment as the top-bar .logo, at landing size. */
    font-size: 30px; font-weight: 700; letter-spacing: 1.5px;
    font-style: italic;
    color: var(--accent, #0071e3); margin-bottom: 8px;
}
.gate-tagline {
    margin: 0 0 26px; font-size: 15px; line-height: 1.5;
    color: var(--text-secondary, #6e6e73);
}
.gate-cta {
    width: 100%; padding: 14px 20px; border: none; cursor: pointer;
    border-radius: 12px; font-size: 17px; font-weight: 600;
    background: var(--accent, #0071e3); color: #fff;
    transition: background 0.15s ease;
}
.gate-cta:hover { background: var(--accent-hover, #0077ED); }
.gate-cta:disabled { opacity: 0.6; cursor: default; }
.gate-note {
    margin: 14px 0 0; min-height: 18px; font-size: 13px;
    color: var(--error-text, #dc2626);
}
.gate-note:empty { display: none; }
.gate-disclaim {
    margin: 20px 0 0; font-size: 12px; line-height: 1.5;
    color: var(--text-muted, #aeaeb2);
}
.gate-links {
    margin: 22px 0 0; padding-top: 18px;
    border-top: 1px solid var(--border-light, #ebebef);
    font-size: 13px; color: var(--text-secondary, #6e6e73);
}
.gate-links a { color: var(--accent, #0071e3); text-decoration: none; }
.gate-links a:hover { text-decoration: underline; }
.gate-links span { margin: 0 10px; color: var(--text-muted, #aeaeb2); }

/* Guest countdown chip in the top bar */
#user-display.guest-badge {
    color: var(--warning, #ff9500); font-weight: 600;
}
