:root {
    --bg:     #0d1117;
    --bg2:    #161b22;
    --bg3:    #1c2128;
    --bg4:    #252c35;
    --border: #30363d;
    --text:   #c9d1d9;
    --muted:  #8b949e;
    --white:  #e6edf3;
    --green:  #00e676;
    --red:    #ff3d5a;
    --blue:   #3b7fff;
    --amber:  #ffaa20;
    --purple: #9d6fff;
    --font:   'Inter', 'Segoe UI', system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; margin: 0; overflow: hidden; }

body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
    display: flex;
    flex-direction: column;
}

/* Topbar */
.topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 16px;
    background: var(--bg3);
    border-bottom: 1px solid var(--border);
    height: 48px;
}

.topbar .logo {
    font-size: 18px;
    font-weight: 700;
    color: var(--white);
    letter-spacing: 1px;
}

.live-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: var(--green);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.env-chip {
    padding: 2px 10px;
    border-radius: 4px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 1px;
    cursor: pointer;
    text-transform: uppercase;
}
.env-chip.demo { background: var(--green); color: #000; }
.env-chip.live { background: var(--red); color: #fff; animation: pulse 1.5s infinite; }

.spacer { flex: 1; }

.topbar-btn {
    background: var(--bg4);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 4px 12px;
    font-family: var(--font);
    font-size: 13px;
    cursor: pointer;
}
.topbar-btn:hover { border-color: var(--blue); color: var(--white); }
.topbar-btn.active { border-color: var(--purple); color: var(--purple); }

/* Close modal steppers */
.close-step-btn {
    display: flex; align-items: center; justify-content: center;
    border-radius: 6px; background: var(--bg4); color: var(--white);
    border: 1px solid var(--border); cursor: pointer; transition: all 0.15s;
}
.close-step-btn:hover { border-color: var(--blue); color: var(--blue); }
.close-step-btn:active { background: var(--blue); color: #fff; }

/* Instrument browser */
.inst-row {
    display: flex; align-items: center; gap: 6px;
    padding: 5px 8px; cursor: pointer; border-bottom: 1px solid var(--border);
    transition: background 0.1s;
}
.inst-row:hover { background: var(--bg3); }
.inst-row.active { background: var(--bg4); border-left: 2px solid var(--blue); }
.inst-row .inst-name { font-weight: 600; color: var(--white); font-size: 14px; flex: 1; }
.inst-row .inst-desc { font-size: 11px; color: var(--muted); flex: 2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inst-row .inst-type { font-size: 11px; color: var(--muted); }
.inst-fav { cursor: pointer; font-size: 16px; color: var(--muted); }
.inst-fav.on { color: var(--amber); }
.inst-cat-btn.active { border-color: var(--blue); color: var(--blue); }

/* Profile panel slide-out */
.profile-backdrop {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5); z-index: 999; display: none;
}
.profile-panel {
    position: fixed; top: 0; right: -320px; width: 320px; height: 100vh;
    background: var(--bg2); border-left: 1px solid var(--border);
    z-index: 1000; display: none; flex-direction: column;
    transition: right 0.3s ease;
    overflow-y: auto;
}
.profile-panel.open { right: 0; display: flex; }
.pp-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 16px; background: var(--bg3); border-bottom: 1px solid var(--border);
}
.pp-user { text-align: center; padding: 16px; border-bottom: 1px solid var(--border); }
.pp-tabs {
    display: flex; flex-wrap: wrap; gap: 2px; padding: 6px 8px;
    border-bottom: 1px solid var(--border);
}
.pp-tab {
    background: var(--bg4); border: 1px solid var(--border); border-radius: 3px;
    padding: 3px 8px; font-size: 12px; color: var(--muted); cursor: pointer;
    font-family: var(--font);
}
.pp-tab:hover { color: var(--text); }
.pp-tab.active { color: var(--blue); border-color: var(--blue); }
.pp-content { flex: 1; padding: 12px 16px; overflow-y: auto; min-height: 0; }
.pp-footer { padding: 12px 16px; border-top: 1px solid var(--border); }

/* Sleeping state */
.topbar.sleeping { opacity: 0.6; }
.topbar.sleeping .topbar-btn:not(#sleep-btn) { pointer-events: none; opacity: 0.4; }
body.sleeping .order-panel-body { opacity: 0.3; pointer-events: none; }

/* Aggregate bar */
.aggregate-bar {
    background: var(--bg2);
    border-bottom: 1px solid var(--border);
    padding: 6px 16px;
    font-size: 14px;
}
.agg-top {
    display: flex;
    align-items: center;
    gap: 24px;
}
.agg-top .agg-item { display: flex; flex-direction: column; gap: 1px; }
.agg-top .agg-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }
.agg-top .agg-value { font-size: 16px; font-weight: 600; color: var(--white); }
.agg-top .agg-divider { width: 1px; height: 28px; background: var(--border); }
.agg-selected {
    font-size: 13px;
    color: var(--muted);
    padding: 4px 0 2px;
    border-top: 1px solid var(--border);
    margin-top: 4px;
}
.agg-accounts {
    display: flex;
    gap: 16px;
    padding-top: 4px;
    border-top: 1px solid var(--border);
    margin-top: 4px;
    flex-wrap: wrap;
}
.agg-acct {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    padding: 3px 0;
}
.agg-acct .agg-gain-bar {
    width: 60px; height: 4px;
    background: var(--bg4);
    border-radius: 2px;
    overflow: hidden;
}
.agg-acct .agg-gain-bar-fill { height: 100%; border-radius: 2px; }

/* Main layout */
.main-grid {
    display: flex;
    flex-direction: row;
    gap: 0;
    padding: 0;
    overflow: hidden;
    min-height: 0;
    min-width: 0;
    flex: 1;
}

/* Panels */
.panel {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
}

.panel-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg3);
    border-bottom: 1px solid var(--border);
    font-size: 13px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--muted);
}

.panel-body { padding: 8px; overflow-y: auto; }

/* Right sidebar */
.right-sidebar {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 0;
    min-width: 0;
    overflow-y: auto;
    overflow-x: hidden;
    flex-shrink: 1;
}

/* Left column */
.left-column {
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow: hidden;
    min-height: 0;
    min-width: 0;
    flex-shrink: 1;
    position: relative;
    grid-column: 1;
    grid-row: 1 / -1;
    min-height: 0;
}

/* ── Chart panel (yellow zone — layout_manager sizes this) ── */
.chart-area,
#chart-panel {
    flex-shrink: 1;
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}
.chart-area .panel-body { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.chart-area canvas,
#chart-container canvas { display: block; }

/* ── #chart-container = LWC host (the ONE clipping boundary for LWC) ── */
/* overflow:hidden clips LWC wrapper at its right/bottom edges — prevents */
/* the price axis from bleeding into the orange sidebar on shrink.        */
#chart-container {
    overflow: hidden;
    position: relative;
    min-width: 0;
    min-height: 0;
}
/* LWC wrapper (direct child div) MUST NOT have overflow:hidden — its price */
/* (right) and time (bottom) axes render at the wrapper's own edges and need */
/* visible overflow to appear. This rule explicitly reverses any prior       */
/* `overflow:hidden !important` we (or third-party CSS) may have had.        */
#chart-container > div.tv-lightweight-charts,
#chart-container > div:first-child {
    overflow: visible;
    position: relative;
    width: 100% !important;
    height: 100% !important;
}

/* ── Positions panel (lime zone) ── */
.positions-area,
#positions-panel { flex-shrink: 1; min-height: 0; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.positions-area .panel-body { flex: 1; min-height: 0; overflow-y: auto; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td {
    padding: 4px 8px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
    white-space: nowrap;
}
th { color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: 12px; letter-spacing: 0.5px; }

/* Inputs */
input, select {
    background: var(--bg4);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text);
    font-family: var(--font);
    font-size: 14px;
    padding: 4px 8px;
}
input:focus, select:focus { outline: none; border-color: var(--blue); }
#order-sl::-webkit-inner-spin-button, #order-sl::-webkit-outer-spin-button,
#order-tp::-webkit-inner-spin-button, #order-tp::-webkit-outer-spin-button,
td input[type="number"]::-webkit-inner-spin-button, td input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
#order-sl, #order-tp, td input[type="number"] { -moz-appearance: textfield; }

/* Buttons */
.btn {
    background: var(--bg4);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 6px 14px;
    font-family: var(--font);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.15s;
}
.btn:hover { border-color: var(--blue); color: var(--white); }
.btn-buy { background: var(--green); color: #000; border-color: var(--green); font-weight: 700; }
.btn-buy:hover { filter: brightness(1.1); }
.btn-sell { background: var(--red); color: #fff; border-color: var(--red); font-weight: 700; }
.btn-sell:hover { filter: brightness(1.1); }

/* Profit/loss colors */
.pnl-pos { color: var(--green); }
.pnl-neg { color: var(--red); }

/* Tabs */
.tabs {
    display: flex;
    gap: 0;
    border-bottom: 1px solid var(--border);
}
.tab {
    padding: 8px 16px;
    font-size: 14px;
    color: var(--muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.15s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--blue); border-bottom-color: var(--blue); }

/* Debug panel */
.debug-panel {
    background: var(--bg2);
    border-top: 2px solid var(--purple);
    max-height: 300px;
    overflow-y: auto;
    display: none;
}
.debug-panel.open { display: block; }
.debug-panel th { color: var(--purple); }

/* Status indicators */
.status-dot {
    display: inline-block;
    width: 6px; height: 6px;
    border-radius: 50%;
}
.status-dot.on { background: var(--green); }
.status-dot.off { background: var(--red); }

/* Multiplier badge */
.mult-badge {
    background: var(--amber);
    color: #000;
    font-size: 12px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 3px;
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.7);
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
}
.modal {
    background: var(--bg2);
    border: 1px solid var(--border);
    border-radius: 8px;
    min-width: 500px;
    max-width: 650px;
    max-height: 80vh;
    overflow-y: auto;
}
.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg3);
    border-bottom: 1px solid var(--border);
    font-weight: 700;
    font-size: 15px;
    color: var(--white);
}
.modal-body { padding: 16px; }
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

/* Health bar */
.health-bar {
    width: 100%;
    height: 4px;
    background: var(--bg4);
    border-radius: 2px;
    overflow: hidden;
    margin-top: 3px;
}
.health-bar-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.5s ease;
}

/* Blocked badge */
.blocked-badge {
    background: var(--red);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 3px;
    letter-spacing: 0.5px;
}

/* Toast notifications */
.toast-container {
    position: fixed;
    bottom: 16px;
    right: 16px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 10px 16px;
    border-radius: 6px;
    font-size: 14px;
    color: var(--white);
    animation: slideIn 0.3s ease;
}
.toast.success { background: var(--green); color: #000; }
.toast.error { background: var(--red); }
.toast.warning { background: var(--amber); color: #000; }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* Spinner */
.spinner {
    display: inline-block;
    width: 12px; height: 12px;
    border: 2px solid var(--border);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ─── Mobile Responsive ─── */
@media (max-width: 1024px) {
    .main-grid { /* layout manager controls widths */ }
    .aggregate-bar { font-size: 13px; }
    .agg-top .agg-value { font-size: 14px; }
}

@media (max-width: 768px) {
    .topbar { flex-wrap: wrap; height: auto; padding: 6px 10px; gap: 6px; }
    .topbar #poll-bar { display: none; }
    .topbar .logo { font-size: 16px; }

    .aggregate-bar { padding: 4px 10px; }
    .agg-top { flex-wrap: wrap; gap: 12px; }
    .agg-top .agg-divider { display: none; }
    .agg-accounts { flex-direction: column; gap: 4px; }

    .main-grid {
        flex-direction: column;
        flex: 1;
        min-height: 0;
    }
    .right-sidebar {
        grid-column: 1;
        grid-row: auto;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 8px;
    }
    .right-sidebar > .panel { flex: 1 1 280px; min-width: 0; }

    .chart-area { min-height: 250px; }
    .positions-area { min-height: 150px; }

    .modal { min-width: auto; width: 95vw; max-width: 95vw; }

    /* Hide less important table columns */
    table th:nth-child(3), table td:nth-child(3),
    table th:nth-child(10), table td:nth-child(10) { display: none; }

    .profile-panel { width: 100vw; }
}

/* Drawing toolbar */
.drawing-group { position: relative; }
.drawing-tool-btn {
    width: 36px; height: 36px; display: flex; align-items: center; justify-content: center;
    background: var(--bg3); border: 1px solid var(--border); border-radius: 4px;
    color: var(--muted); font-size: 15px; cursor: pointer; padding: 0;
    font-family: var(--font); transition: all 0.1s;
}
.drawing-tool-btn:hover { border-color: var(--blue); color: var(--text); background: var(--bg4); }
.drawing-tool-btn.active { border-color: var(--blue); background: rgba(59,127,255,0.15); color: var(--blue); }
.drawing-divider { height: 1px; background: var(--border); margin: 4px 2px; }

/* Flyout submenu */
.drawing-flyout {
    position: absolute; left: 42px; top: 0; z-index: 200;
    background: var(--bg2); border: 1px solid var(--border); border-radius: 6px;
    min-width: 220px; box-shadow: 0 8px 24px rgba(0,0,0,0.5);
    padding: 4px 0;
    display: none;
}
.drawing-flyout.open { display: block; }
.flyout-header {
    padding: 6px 12px; font-size: 10px; font-weight: 600; color: var(--muted);
    letter-spacing: 0.5px; border-bottom: 1px solid var(--border);
}
.flyout-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 7px 12px; cursor: pointer; font-size: 12px; color: var(--text);
    transition: background 0.1s;
}
.flyout-item:hover { background: var(--bg4); }
.flyout-item.active { color: var(--blue); background: rgba(59,127,255,0.1); }
.flyout-label { flex: 1; }
.flyout-shortcut { font-size: 10px; color: var(--muted); margin-left: 16px; }

/* Handle dots (R10) */
.drawing-handle {
    width: 10px; height: 10px; border-radius: 50%;
    background: #ffffff; border: 2px solid #3b7fff;
    position: absolute; transform: translate(-50%, -50%);
    z-index: 300; box-shadow: 0 0 0 1px rgba(0,0,0,0.4);
    transition: transform 0.08s, background 0.08s;
}
.drawing-handle.endpoint { cursor: crosshair; }
.drawing-handle.midpoint { cursor: move; }
.drawing-handle:hover {
    background: #3b7fff; border-color: #ffffff;
    transform: translate(-50%, -50%) scale(1.35);
}

/* Floating toolbar on selected drawing (R4) */
.drawing-floating-toolbar {
    position: absolute; z-index: 350; display: none;
    align-items: center; gap: 6px; padding: 4px 8px;
    background: var(--bg2); border: 1px solid var(--border); border-radius: 6px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
    pointer-events: auto;
}
.dft-colors { display: flex; gap: 3px; }
.dft-swatch {
    width: 16px; height: 16px; border-radius: 50%; cursor: pointer;
    border: 1px solid transparent; transition: transform 0.1s;
}
.dft-swatch:hover { transform: scale(1.3); }
.dft-width {
    background: var(--bg3); color: var(--text); border: 1px solid var(--border);
    border-radius: 3px; font-size: 11px; padding: 2px 4px; cursor: pointer;
}
.dft-btn {
    background: transparent; border: 1px solid var(--border); border-radius: 3px;
    color: var(--muted); font-size: 14px; cursor: pointer; padding: 2px 6px;
}
.dft-btn:hover { color: var(--red); border-color: var(--red); }

/* Right-click context menu */
.drawing-context-menu {
    position: fixed; z-index: 400;
    background: var(--bg2); border: 1px solid var(--border); border-radius: 6px;
    min-width: 180px; box-shadow: 0 8px 24px rgba(0,0,0,0.6);
    padding: 4px 0;
}
.ctx-section { padding: 6px 10px; }
.ctx-label { font-size: 9px; color: var(--muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.ctx-colors { display: flex; gap: 4px; }
.ctx-swatch { width: 18px; height: 18px; border-radius: 50%; cursor: pointer; transition: transform 0.1s; }
.ctx-swatch:hover { transform: scale(1.3); }
.ctx-widths { display: flex; gap: 3px; }
.ctx-width-btn { background: var(--bg3); border: 1px solid var(--border); border-radius: 3px; color: var(--text); font-size: 10px; padding: 2px 8px; cursor: pointer; }
.ctx-width-btn:hover { border-color: var(--blue); }
.ctx-divider { height: 1px; background: var(--border); margin: 4px 0; }
.ctx-item { padding: 6px 12px; font-size: 12px; color: var(--text); cursor: pointer; }
.ctx-item:hover { background: var(--bg4); }
.ctx-danger { color: var(--red); }
.ctx-danger:hover { background: rgba(255,61,90,0.1); }

/* History panel dropdown */
.history-panel {
    position: absolute; top: 100%; left: 0; z-index: 300;
    background: var(--bg2); border: 1px solid var(--border); border-radius: 6px;
    min-width: 260px; max-height: 320px; overflow-y: auto;
    box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}
.hp-header { padding: 6px 10px; font-size: 9px; font-weight: 600; color: var(--muted); letter-spacing: 0.5px; border-bottom: 1px solid var(--border); }
.hp-item { display: flex; justify-content: space-between; padding: 5px 10px; font-size: 11px; cursor: pointer; color: var(--text); }
.hp-item:hover { background: var(--bg4); }
.hp-item.hp-current { color: var(--blue); font-weight: 600; }
.hp-desc { flex: 1; }
.hp-time { color: var(--muted); font-size: 10px; margin-left: 12px; }
.hp-empty { padding: 12px; text-align: center; color: var(--muted); font-size: 11px; }
.hp-footer { padding: 6px; border-top: 1px solid var(--border); }

/* Resizable dividers */
.divider { background: var(--border); position: relative; flex-shrink: 0; z-index: 10; transition: background 0.15s; }
.divider:hover, .divider.dragging { background: var(--blue); }
.divider-vertical { height: 6px; width: 100%; cursor: row-resize; }
.divider-horizontal { width: 6px; height: 100%; cursor: col-resize; }
.divider-handle { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: grid; gap: 2px; opacity: 0.4; transition: opacity 0.15s; pointer-events: none; }
.divider:hover .divider-handle, .divider.dragging .divider-handle { opacity: 1; }
.divider-vertical .divider-handle { grid-template-columns: repeat(3, 3px); grid-template-rows: repeat(2, 3px); }
.divider-horizontal .divider-handle { grid-template-columns: repeat(2, 3px); grid-template-rows: repeat(3, 3px); }
.divider-dot { width: 3px; height: 3px; border-radius: 50%; background: var(--white); }

/* Sidebar collapsed state */
#right-sidebar.collapsed > .panel { display: none; }
#right-sidebar.collapsed { width: 48px !important; min-width: 48px; padding: 4px 0; display: flex; flex-direction: column; align-items: center; gap: 4px; }

/* Reset layout button */
#btn-reset-layout.flash { border-color: var(--green); color: var(--green); animation: reset-flash 0.6s ease; }
@keyframes reset-flash { 0% { background: rgba(0,212,160,0.2); } 100% { background: var(--bg3); } }

.indicator-chip { border-radius: 3px; }
.indicator-chip.active { border-color: var(--blue); color: var(--blue); }

@media (max-width: 480px) {
    .topbar { font-size: 13px; }
    .topbar .env-chip { font-size: 11px; padding: 1px 6px; }
    .topbar-btn { font-size: 12px; padding: 3px 8px; }

    .chart-area { min-height: 200px; }

    .right-sidebar { flex-direction: column; }
    .right-sidebar > .panel { flex: none; }

    /* Show only essential columns on small screens */
    table th:nth-child(5), table td:nth-child(5),
    table th:nth-child(7), table td:nth-child(7),
    table th:nth-child(8), table td:nth-child(8) { display: none; }
}
