/* Web shell: navigation, workspace, and the layout primitives the screens compose with.
 *
 * Loaded after app.css. app.css owns the base primitives (buttons, fields, cards, tables); this file
 * owns the frame around them, plus the components that arrived with the P0 shell: the navigation
 * rail / bottom bar, the tile grid, empty and loading states, and toasts.
 *
 * Rules:
 *   - One tablist element, positioned left on wide screens and bottom on narrow ones. Every tab is a
 *     real link (`href="#id"`) so a section is deep-linkable and testable.
 *   - Keyboard: focus moves with ArrowLeft/ArrowRight/ArrowUp/ArrowDown, Home and End. Because the
 *     rail becomes a horizontal bar on a phone, all four arrows navigate - orientation is not fixed.
 *   - Nothing animates when `prefers-reduced-motion: reduce` (see the block at the end).
 */

/* ── frame ───────────────────────────────────────────────────────────────────── */

.trader-shell {
    display: grid;
    grid-template-columns: var(--rail-w) minmax(0, 1fr);
    gap: var(--space-6);
    align-items: start;
    margin-top: var(--space-4);
}

.workspace {
    min-width: 0; /* lets tables and charts shrink instead of forcing the page wide */
}

.workspace > [role="tabpanel"] { min-width: 0; }
.workspace > [role="tabpanel"]:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: var(--radius-sm); }

/* ── navigation rail (nav landmark wrapping a tablist) ───────────────────────── */

.rail-wrap {
    position: sticky;
    top: calc(var(--space-8) + var(--space-3));
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-width: 0;
}

.rail-label {
    padding: 0 var(--space-1);
    font-size: var(--text-xs);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.rail {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-2);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

[role="tab"] {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--row-h);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: var(--text-base);
    font-weight: 500;
    line-height: 1.2;
}

[role="tab"]:hover { background: var(--bg-card-hover); color: var(--text-primary); }
[role="tab"]:focus-visible { outline: none; box-shadow: var(--focus-ring); }

[role="tab"][aria-selected="true"] {
    background: var(--bg-card);
    border-color: var(--border-strong);
    color: var(--gold);
}

/* A tab that a later phase will fill: present, visibly inert, and honest about it. It is not
   focusable and not a link, so nothing dangles. */
/* Read by a screen reader, invisible on screen: the reason a dependent control is disabled, wired with
   `aria-describedby`. `display:none`/`visibility:hidden` would remove it from the accessibility tree too. */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    border: 0;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    overflow: hidden;
    white-space: nowrap;
}

/* The product's rule for a dependent setting (`web/spa/deps.js`): still readable, still saved, not
   editable. Dimmed rather than hidden - a hidden field looks like a lost one. */
.is-inactive {
    opacity: 0.55;
}
.field.is-inactive label,
.field.is-inactive input,
.field.is-inactive select {
    cursor: not-allowed;
}

[role="tab"][aria-disabled="true"] {
    color: var(--text-faint);
    cursor: default;
}

[role="tab"] .tab-key {
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-faint);
}

/* ── tiles (the dashboard's KPI cards, P1) ───────────────────────────────────── */

.tiles {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: var(--space-3);
    margin: var(--space-4) 0;
}

.tile {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    font-variant-numeric: tabular-nums;
    min-width: 0;
}

.tile-label {
    font-size: var(--text-xs);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.tile-value {
    font-size: var(--text-xl);
    font-weight: 600;
    line-height: 1.15;
    overflow-wrap: anywhere;
}

.tile-delta { font-size: var(--text-sm); }
.tile-delta.up { color: var(--profit); }
.tile-delta.down { color: var(--loss); }
.tile-foot { margin-top: auto; font-size: var(--text-xs); color: var(--text-faint); }

/* A number that is older than its poll interval says so rather than lying. */
.tile[data-stale="yes"] .tile-value { color: var(--text-muted); }

/* ── dashboard: two-up cards, engine rows, scan rows ─────────────────────────── */

.grid-two {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-4);
    margin: var(--space-4) 0;
}

.engine-head {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
    margin-bottom: var(--space-2);
}

.engine-name {
    font-family: var(--font-mono);
    font-size: var(--text-base);
    color: var(--text-primary);
}

.engine-market {
    font-size: var(--text-xs);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.scan-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: var(--space-1) var(--space-3);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--border);
    font-size: var(--text-sm);
}
.scan-row:last-child { border-bottom: none; }
.scan-row .scan-when { color: var(--text-muted); font-variant-numeric: tabular-nums; }

.breadth-row {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--border);
    font-size: var(--text-sm);
}
.breadth-row:last-child { border-bottom: none; }
.breadth-market {
    font-size: var(--text-xs);
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
    min-width: 64px;
}

@media (max-width: 1023px) {
    .grid-two { grid-template-columns: minmax(0, 1fr); }
}

/* The ladder: price levels as absolute percentages of the configured range. Percent-of-range keeps it
   honest when the card resizes, which a pixel position would not. */
.ladder {
    position: relative;
    height: 180px;
    margin: var(--space-4) 0 var(--space-6);
    border-left: 2px solid var(--border-strong);
}

.ladder-rung {
    position: absolute;
    left: 0;
    right: 18%;
    height: 2px;
    background: var(--text-muted);
    opacity: 0.75;
}
.ladder-rung.buy { background: var(--profit); }
.ladder-rung.sell { background: var(--loss); }
.ladder-last { position: absolute; left: -5px; right: 0; height: 2px; background: var(--gold); }

.ladder-bound {
    position: absolute;
    right: 0;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}
.ladder-bottom { bottom: -18px; }
.ladder-top { top: -18px; }

/* ── tables that become cards on a phone ────────────────────────────────────── */

.table-wrap { overflow-x: auto; }

/* A nine-column table on a 390px screen is not a table, it is a lie about what fits. The card list
   below carries the same fields; only one of the two is ever displayed. */
@media (max-width: 719px) {
    /* Only the tables with a card alternative (`#positions-cards`, `#spot-cards`) belong behind this rule.
       It used the generic `.table-wrap`, so a phone hid **every** table on the page - monitors, activity,
       backtests - and a dashboard saying "1 managed" showed nothing underneath (reported 2026-11-21).
       The other tables scroll inside their own card (`overflow-x: auto`). */
    #positions-table-wrap,
    #spot-table-wrap { display: none; }
}

@media (min-width: 720px) {
    /* The tables take over above the phone breakpoint; the card lists are the phone's alternative, so
       leaving `#spot-cards` visible rendered every holding twice (the positions list already did this). */
    #positions-cards,
    #spot-cards { display: none; }
}

/* Direction and action colouring: the phone's signal, never colour alone. Every value is also signed and
   every action carries its symbol, so the meaning survives a monochrome screen or a colour-blind reader.
   `.up`/`.down` are kept because the tiles and Activity already emit them. */
.up, .profit { color: var(--profit); }
.down, .loss { color: var(--loss); }
.flat { color: var(--text-muted); }
.sym { margin-right: 0.35em; }
.sym-emoji { font-family: 'NotoEmoji', 'Segoe UI Emoji', 'Apple Color Emoji', 'Noto Color Emoji', sans-serif; }
.tag { font-size: 0.78em; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; }
.tag-grid { color: var(--gold); }
.tag-stop { color: var(--warn); }
.tag-dust { color: var(--warn); }
.tag-limit { color: var(--text-muted); }
/* The phone's gold star for the strategy that is running. */
.active-flag { color: var(--gold); font-weight: 600; }

/* ── confirmation dialog ────────────────────────────────────────────────────── */

.dialog-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.62);
    display: grid;
    place-items: center;
    padding: var(--space-4);
    z-index: var(--z-dialog);
}

.dialog {
    background: var(--bg-card);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: var(--space-5);
    max-width: 460px;
    width: 100%;
}
.dialog h2 { margin-top: 0; font-size: var(--text-lg); }

/* The command palette is a dialog with a list, not a confirmation: it must not look like a decision. */
.palette-list {
    max-height: 50vh;
    overflow-y: auto;
    margin-top: var(--space-3);
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}
:root[data-font-size="small"] { font-size: 14px; }
:root[data-font-size="medium"] { font-size: 16px; }
:root[data-font-size="large"] { font-size: 18px; }
.force-reduce-motion *,
.force-reduce-motion *::before,
.force-reduce-motion *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
}

/* Split view: two real destinations side by side, with a draggable and keyboard-resizable divider.
   The ratio is a custom property so the drag handler only writes one number. Narrow screens keep one
   destination at a time; the toolbar itself is a desktop control. */
.split-toolbar { display: flex; gap: var(--space-3); align-items: center; margin-bottom: var(--space-3); }
@media (max-width: 1199px) {
    .split-toolbar { display: none; }
}
@media (min-width: 1200px) {
    #trader-workspace[data-split] {
        display: grid;
        grid-template-columns: var(--split-left, 55%) minmax(0, 1fr);
        gap: var(--space-4);
        position: relative;
        align-items: start;
    }
    #trader-workspace[data-split] .split-toolbar { grid-column: 1 / -1; }
    .split-divider {
        position: absolute;
        top: 0;
        bottom: 0;
        left: var(--split-left, 55%);
        width: 12px;
        margin-left: -6px;
        cursor: col-resize;
        z-index: 2;
        background: transparent;
    }
    .split-divider:hover,
    .split-divider:focus-visible { background: var(--border-strong); }
    /* A touch drag must not scroll the page instead of moving the divider. */
    .split-divider { touch-action: none; }
}

/* A sortable header is a control, and it is reachable by Tab: it has to look and behave like one. */
.table-wrap th[tabindex]:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
    border-radius: var(--radius-sm);
}
.table-wrap th[tabindex] { cursor: pointer; }
.table-wrap th.sort-asc::after { content: ' \2191'; }
.table-wrap th.sort-desc::after { content: ' \2193'; }

/* Wide screens: the dashboard is a set of independent cards, so a single long column wastes the width.
   `data-wide` is the operator's choice, not a breakpoint surprise. */
@media (min-width: 1200px) {
    :root[data-wide="1"] #dash-engines .cards,
    :root[data-wide="1"] #dash-other-engines .cards,
    :root[data-wide="1"] #dash-grids .cards,
    :root[data-wide="1"] #dash-monitors .table-wrap {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
        gap: var(--space-4);
        align-items: start;
    }
}

.palette-row {
    background: transparent;
    color: var(--text-primary);
    border: 1px solid transparent;
    text-align: left;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    cursor: pointer;
    font: inherit;
}
.palette-row:hover,
.palette-row:focus-visible {
    background: var(--bg-elevated);
    border-color: var(--border);
}
#palette-input { width: 100%; }
.dialog .dialog-detail {
    font-size: var(--text-sm);
    color: var(--text-muted);
    margin-bottom: var(--space-2);
}

/* ── states ─────────────────────────────────────────────────────────────────── */.empty-state {
    border: 1px dashed var(--border-strong);
    border-radius: var(--radius);
    padding: var(--space-5);
    text-align: center;
    color: var(--text-secondary);
}
.empty-state strong { display: block; color: var(--text-primary); margin-bottom: var(--space-1); }

.skeleton {
    display: block;
    background: linear-gradient(90deg, var(--bg-card) 0%, var(--bg-card-hover) 50%, var(--bg-card) 100%);
    background-size: 200% 100%;
    border-radius: var(--radius-sm);
    min-height: 1rem;
    animation: skeleton-sweep 1.4s ease-in-out infinite;
}
.skeleton.tile-skeleton { min-height: 92px; }

@keyframes skeleton-sweep {
    from { background-position: 200% 0; }
    to { background-position: -200% 0; }
}

/* ── toasts (per-action outcome, not a global spinner) ───────────────────────── */

.toast-host {
    position: fixed;
    right: var(--space-4);
    bottom: var(--space-4);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    max-width: min(420px, calc(100vw - var(--space-6)));
}

.toast {
    background: var(--bg-card);
    border: 1px solid var(--border-strong);
    border-left: 3px solid var(--gold);
    border-radius: var(--radius-sm);
    padding: var(--space-3) var(--space-4);
    box-shadow: var(--shadow);
    font-size: var(--text-sm);
    color: var(--text-primary);
}
.toast[data-kind="error"] { border-left-color: var(--loss); }
.toast[data-kind="ok"] { border-left-color: var(--profit); }

/* ── density ────────────────────────────────────────────────────────────────── */

:root[data-density="compact"] table th,
:root[data-density="compact"] table td { padding-top: var(--space-1); padding-bottom: var(--space-1); }

/* ── responsive ─────────────────────────────────────────────────────────────── */

@media (max-width: 1199px) {
    .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 1023px) {
    /* The rail becomes a bottom bar: one row, horizontally scrollable, always reachable. */
    .trader-shell { grid-template-columns: minmax(0, 1fr); }
    .rail-wrap {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        top: auto;
        z-index: var(--z-rail);
        gap: 0;
    }
    .rail-label { display: none; }
    .rail {
        flex-direction: row;
        gap: var(--space-1);
        padding: var(--space-1) var(--space-2);
        border-radius: 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
        overflow-x: auto;
        background: rgba(5, 5, 5, 0.94);
        backdrop-filter: blur(12px);
    }
    [role="tab"] {
        flex: 0 0 auto;
        flex-direction: column;
        gap: 2px;
        min-width: 72px;
        padding: var(--space-1) var(--space-2);
        font-size: var(--text-xs);
        text-align: center;
    }
    [role="tab"] .tab-key { display: none; }
    /* The bar is fixed, so the page needs room under it. */
    body { padding-bottom: calc(var(--row-h) + var(--space-4)); }
}

@media (max-width: 719px) {
    .tiles { grid-template-columns: minmax(0, 1fr); }
    .toast-host { left: var(--space-4); right: var(--space-4); }
}

/* ── motion ─────────────────────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
