/* =============================================================
   ChatMMM Beta Trading page (trading.html) — one-page dashboard.

   Card + table language mirrors performance.css / the News shell
   (surface-card sections, sunken table heads, tabular numerals);
   form controls and toggles reuse settings-* component classes.
   ============================================================= */

@layer components {

    .trading-surface {
        display: flex;
        flex-direction: column;
        gap: 18px;
    }
    .trading-stack {
        display: flex;
        flex-direction: column;
        gap: 18px;
    }

    /* Header chips: paper/live mode + broker account number. */
    .trading-chips {
        display: inline-flex;
        gap: 8px;
        align-items: center;
        flex-wrap: wrap;
    }
    .trading-chip {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 4px 12px;
        border-radius: 999px;
        font-size: var(--fs-xs);
        font-weight: 700;
        letter-spacing: 0.03em;
        text-transform: uppercase;
        color: var(--brand-700, #6d28d9);
        background: var(--brand-50, rgba(124, 58, 237, 0.12));
        border: 1px solid var(--brand-200, rgba(124, 58, 237, 0.28));
        white-space: nowrap;
    }
    .trading-chip--muted {
        color: var(--text-secondary);
        background: var(--surface-sunken);
        border-color: var(--border-color);
        text-transform: none;
        letter-spacing: 0;
        font-weight: 600;
    }
    .trading-chip--live {
        color: #b45309;
        background: rgba(245, 158, 11, 0.14);
        border-color: rgba(245, 158, 11, 0.35);
    }
    /* Dark-mode chip backgrounds/borders (colors live in the unlayered
       "Dark-mode color survival" block at the bottom of this file — an
       unlayered theme-dark reset beats any color declared in here). */
    html.theme-dark .trading-chip { background: rgba(124, 58, 237, 0.18); border-color: rgba(124, 58, 237, 0.4); }
    html.theme-dark .trading-chip--live { background: rgba(245, 158, 11, 0.14); border-color: rgba(245, 158, 11, 0.35); }

    /* ── Cards ─────────────────────────────────────────────── */
    .trading-card {
        background: var(--surface-card);
        border: 1px solid var(--border-color);
        border-radius: 12px;
        overflow: hidden;
    }
    .trading-card__head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        flex-wrap: wrap;
        padding: 14px 18px;
        border-bottom: 1px solid var(--border-color);
        background: var(--surface-sunken);
    }
    .trading-card__title {
        margin: 0;
        font-size: var(--fs-md, 1rem);
        font-weight: 700;
        color: var(--text-primary);
        display: inline-flex;
        align-items: center;
        gap: 9px;
    }
    .trading-card__title i {
        color: var(--brand-600, #7c3aed);
        font-size: 0.9em;
    }
    .trading-card__count {
        color: var(--text-muted);
        font-weight: 600;
        font-size: var(--fs-sm);
    }
    .trading-card__aside {
        font-size: var(--fs-xs);
        color: var(--text-muted);
        font-variant-numeric: tabular-nums;
    }
    .trading-card__body {
        padding: 16px 18px;
        display: flex;
        flex-direction: column;
        gap: 14px;
    }
    .trading-card__desc {
        margin: 0;
        color: var(--text-secondary);
        font-size: var(--fs-sm);
        line-height: 1.5;
    }
    .trading-card__hint {
        margin: 0;
        color: var(--text-muted);
        font-size: var(--fs-xs);
        line-height: 1.5;
    }
    .trading-card__actions {
        display: flex;
        justify-content: flex-end;
        gap: 10px;
        flex-wrap: wrap;
    }

    /* ── Independent execution / research states ──────────── */
    .trading-state-list {
        display: flex;
        flex-direction: column;
        border: 1px solid var(--border-color);
        border-radius: 10px;
        overflow: hidden;
    }
    .trading-state-row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 16px;
        padding: 13px 14px;
        background: var(--surface-card);
        border-bottom: 1px solid var(--border-color);
    }
    .trading-state-row:last-child { border-bottom: 0; }
    .trading-state-row__copy {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 3px;
        min-width: 0;
    }
    .trading-state-row__title {
        color: var(--text-primary);
        font-size: var(--fs-sm);
    }
    .trading-state-row__desc {
        color: var(--text-muted);
        font-size: var(--fs-xs);
        line-height: 1.45;
    }
    .trading-state-row__version {
        color: var(--text-secondary);
        font-size: var(--fs-xs);
    }
    .trading-research {
        display: flex;
        flex-direction: column;
        gap: 12px;
        padding-top: 2px;
    }
    .trading-research__head {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 12px;
        flex-wrap: wrap;
    }
    .trading-research__head > div {
        display: flex;
        flex-direction: column;
        gap: 2px;
    }
    .trading-research__title {
        color: var(--text-primary);
        font-size: var(--fs-sm);
    }
    .trading-research__subtitle,
    .trading-research__heartbeat {
        color: var(--text-muted);
        font-size: var(--fs-xs);
    }
    .trading-cluster-progress {
        display: flex;
        flex-direction: column;
        gap: 7px;
        padding: 2px 4px 0;
    }
    .trading-cluster-progress__labels {
        display: flex;
        justify-content: space-between;
        align-items: baseline;
        gap: 12px;
        color: var(--text-muted);
        font-size: var(--fs-xs);
    }
    .trading-cluster-progress__labels strong { color: var(--text-primary); }
    .trading-cluster-progress .trading-deploybar { max-width: none; }
    .trading-recovery {
        display: flex;
        flex-direction: column;
        gap: 3px;
        padding: 11px 13px;
        border: 1px dashed var(--border-color);
        border-radius: 9px;
        color: var(--text-muted);
        background: var(--surface-sunken);
        font-size: var(--fs-xs);
        line-height: 1.45;
    }
    .trading-recovery strong { color: var(--text-secondary); }

    /* ── Key form ──────────────────────────────────────────── */
    .trading-keyform {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }
    .trading-keyform__grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
        gap: 12px;
    }
    .trading-field {
        display: flex;
        flex-direction: column;
        gap: 6px;
    }
    .trading-field__label {
        font-size: var(--fs-xs);
        font-weight: 600;
        color: var(--text-secondary);
    }

    /* ── Stat grid (account + performance) ─────────────────── */
    .trading-statgrid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(128px, 1fr));
        gap: 10px;
    }
    .trading-stat {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        text-align: center;
        padding: 14px 10px;
        border-radius: 10px;
        background: var(--surface-sunken);
        border: 1px solid var(--border-color);
        min-width: 0;
    }
    .trading-stat--wide { grid-column: span 2; }
    .trading-stat__num {
        font-size: var(--fs-lg, 1.15rem);
        font-weight: 800;
        color: var(--text-primary);
        font-variant-numeric: tabular-nums;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .trading-stat__num.is-pos { color: var(--bull-strong, #16a34a); }
    .trading-stat__num.is-neg { color: var(--bear-strong, #dc2626); }
    .trading-stat__label {
        font-size: var(--fs-xs);
        font-weight: 600;
        color: var(--text-muted);
        white-space: nowrap;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Deployment progress vs the 70% cap. */
    .trading-deploybar {
        display: block;
        width: 100%;
        max-width: 220px;
        height: 5px;
        margin-top: 4px;
        border-radius: 999px;
        background: var(--border-color);
        overflow: hidden;
    }
    .trading-deploybar__fill {
        display: block;
        height: 100%;
        width: 0%;
        border-radius: 999px;
        background: var(--brand-600, #7c3aed);
        transition: width var(--dur-med, 0.25s) var(--ease-out, ease-out);
    }
    .trading-deploybar__fill.is-warn { background: #f59e0b; }
    .trading-deploybar__fill.is-over { background: var(--bear-strong, #dc2626); }

    /* ── Positions tables ──────────────────────────────────── */
    .trading-tablewrap { overflow-x: auto; }
    .trading-table {
        width: 100%;
        border-collapse: collapse;
        font-size: var(--fs-sm);
    }
    .trading-table thead th {
        text-align: left;
        font-size: var(--fs-xs);
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--text-muted);
        padding: 10px 14px;
        background: var(--surface-sunken);
        border-bottom: 1px solid var(--border-color);
        font-weight: 600;
        white-space: nowrap;
    }
    .trading-table th.is-num, .trading-table td.is-num {
        text-align: right;
        font-variant-numeric: tabular-nums;
    }
    .trading-table th.is-action, .trading-table td.is-action { text-align: right; }
    .trading-table tbody tr { border-bottom: 1px solid var(--border-color); }
    .trading-table tbody tr:last-child { border-bottom: none; }
    .trading-table tbody tr:hover { background: var(--surface-sunken); }
    .trading-table td {
        padding: 11px 14px;
        color: var(--text-primary);
        white-space: nowrap;
    }
    .trading-table td.is-reason {
        white-space: normal;
        max-width: 260px;
        color: var(--text-muted);
        font-size: var(--fs-xs);
    }
    .trading-table__empty {
        padding: 22px 14px !important;
        text-align: center;
        color: var(--text-muted);
        white-space: normal !important;
    }

    .trading-ticker {
        font-weight: 700;
        color: var(--brand-700, #6d28d9);
        background: var(--brand-50, rgba(124, 58, 237, 0.12));
        padding: 2px 8px;
        border-radius: 6px;
        font-size: var(--fs-xs);
    }
    html.theme-dark .trading-ticker { background: rgba(124, 58, 237, 0.16); }

    .trading-side {
        display: inline-block;
        padding: 2px 8px;
        border-radius: 999px;
        font-size: 10px;
        font-weight: 700;
        letter-spacing: 0.04em;
    }
    .trading-side--long { color: var(--bull-strong, #16a34a); background: rgba(34, 197, 94, 0.12); }
    .trading-side--short { color: var(--bear-strong, #dc2626); background: rgba(239, 68, 68, 0.12); }
    .trading-side--pending { color: #b45309; background: rgba(245, 158, 11, 0.14); }

    .trading-pl { font-weight: 700; font-variant-numeric: tabular-nums; }
    .trading-pl--up { color: var(--bull-strong, #16a34a); }
    .trading-pl--down { color: var(--bear-strong, #dc2626); }
    .trading-pl--flat { color: var(--text-muted); }

    .trading-closebtn {
        padding: 5px 12px;
        border-radius: 8px;
        border: 1px solid rgba(239, 68, 68, 0.35);
        background: transparent;
        color: var(--bear-strong, #dc2626);
        font: inherit;
        font-size: var(--fs-xs);
        font-weight: 600;
        cursor: pointer;
        transition: background var(--dur-fast, 0.15s) var(--ease-out, ease-out);
    }
    .trading-closebtn:hover:not(:disabled) { background: rgba(239, 68, 68, 0.1); }
    .trading-closebtn:disabled { opacity: 0.55; cursor: default; }

    /* ── Danger zone ───────────────────────────────────────── */
    .trading-danger {
        margin-top: 4px;
        border: 1px solid rgba(239, 68, 68, 0.3);
        border-radius: 10px;
        padding: 12px 14px;
        display: flex;
        flex-direction: column;
        gap: 10px;
    }
    .trading-danger__title {
        margin: 0;
        font-size: var(--fs-xs);
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--bear-strong, #dc2626);
    }
    .trading-danger__row {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 14px;
        flex-wrap: wrap;
    }
    .trading-danger__meta { display: flex; flex-direction: column; gap: 2px; min-width: 200px; flex: 1; }
    .trading-danger__label { font-size: var(--fs-sm); font-weight: 600; color: var(--text-primary); }
    .trading-danger__hint { font-size: var(--fs-xs); color: var(--text-muted); line-height: 1.45; }
    .trading-btn--danger { color: var(--bear-strong, #dc2626) !important; border-color: rgba(239, 68, 68, 0.4) !important; }
    .trading-btn--danger:hover { background: rgba(239, 68, 68, 0.08) !important; }

    /* Compact button variant for card-header actions (e.g. Sync). */
    .settings-btn--sm {
        padding: 6px 12px !important;
        font-size: var(--fs-xs) !important;
    }

    /* Screen-reader-only helper (table action header). */
    .trading-table .sr-only {
        position: absolute;
        width: 1px; height: 1px;
        padding: 0; margin: -1px;
        overflow: hidden;
        clip: rect(0 0 0 0);
        white-space: nowrap;
        border: 0;
    }

    @media (max-width: 640px) {
        .trading-stat--wide { grid-column: span 1; }
        .trading-card__head, .trading-card__body { padding-left: 14px; padding-right: 14px; }
        .trading-state-row { align-items: flex-start; }
    }
}

/* =============================================================
   Dark-mode color survival — UNLAYERED on purpose.

   theme-dark.css carries an unlayered "text-color sanity" reset
   (`html.theme-dark span/p/label/h3 { color: inherit }`), and
   unlayered author styles beat ANY rule inside @layer regardless of
   specificity. Every span/p-borne color above therefore needs an
   unlayered `html.theme-dark` twin here or P/L greens/reds, muted
   labels and chips all flatten to plain body text in dark mode.
   (Same pattern as the .trading-beta-pill dark override below.)
   ============================================================= */
html.theme-dark .trading-chip { color: #c4b5fd; }
html.theme-dark .trading-chip--muted { color: var(--text-secondary); }
html.theme-dark .trading-chip--live { color: #fbbf24; }
html.theme-dark .trading-card__title { color: var(--text-primary); }
html.theme-dark .trading-card__aside,
html.theme-dark .trading-card__count,
html.theme-dark .trading-card__hint,
html.theme-dark .trading-stat__label,
html.theme-dark .trading-danger__hint { color: var(--text-muted); }
html.theme-dark .trading-card__desc,
html.theme-dark .trading-field__label { color: var(--text-secondary); }
html.theme-dark .trading-stat__num,
html.theme-dark .trading-danger__label,
html.theme-dark .trading-state-row__title,
html.theme-dark .trading-research__title,
html.theme-dark .trading-cluster-progress__labels strong { color: var(--text-primary); }
html.theme-dark .trading-state-row__desc,
html.theme-dark .trading-research__subtitle,
html.theme-dark .trading-research__heartbeat,
html.theme-dark .trading-cluster-progress__labels,
html.theme-dark .trading-recovery { color: var(--text-muted); }
html.theme-dark .trading-state-row__version,
html.theme-dark .trading-recovery strong { color: var(--text-secondary); }
html.theme-dark .trading-stat__num.is-pos { color: #4ade80; }
html.theme-dark .trading-stat__num.is-neg { color: #f87171; }
html.theme-dark .trading-ticker { color: #c4b5fd; }
html.theme-dark .trading-side--long { color: #4ade80; }
html.theme-dark .trading-side--short { color: #f87171; }
html.theme-dark .trading-side--pending { color: #fbbf24; }
html.theme-dark .trading-pl--up { color: #4ade80; }
html.theme-dark .trading-pl--down { color: #f87171; }
html.theme-dark .trading-pl--flat { color: var(--text-muted); }
html.theme-dark .trading-table__empty { color: var(--text-muted); }
html.theme-dark .trading-danger__title { color: #f87171; }
html.theme-dark .trading-closebtn { color: #f87171; }

/* =============================================================
   Legacy page states (loading / beta pill / empty) below.
   ============================================================= */

/* Small "Beta" pill next to the page title. */
.trading-beta-pill {
    display: inline-block;
    vertical-align: middle;
    margin-left: 8px;
    padding: 2px 9px;
    border-radius: 999px;
    font-size: 0.62em;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--brand-600, #6d28d9);
    background: var(--brand-50, rgba(124, 58, 237, 0.12));
    border: 1px solid var(--brand-200, rgba(124, 58, 237, 0.28));
}
html.theme-dark .trading-beta-pill {
    color: #c4b5fd;
    background: rgba(124, 58, 237, 0.18);
    border-color: rgba(124, 58, 237, 0.4);
}

/* Loading placeholder while beta membership resolves. */
.trading-loading .settings-row__hint i { margin-right: 6px; }

/* "Not available" empty state (non-beta users hitting the URL directly). */
.trading-empty .settings-section__body {
    text-align: center;
    padding: 12px 8px 20px;
}
.trading-empty__icon {
    width: 56px;
    height: 56px;
    margin: 6px auto 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 16px;
    font-size: 24px;
    color: var(--brand-600, #6d28d9);
    background: var(--brand-50, rgba(124, 58, 237, 0.12));
}
html.theme-dark .trading-empty__icon {
    color: #c4b5fd;
    background: rgba(124, 58, 237, 0.16);
}
.trading-empty__title {
    font-size: var(--fs-lg, 1.15rem);
    font-weight: 700;
    margin: 0 0 6px;
}
.trading-empty__desc {
    max-width: 460px;
    margin: 0 auto 18px;
    color: var(--text-muted);
    font-size: var(--fs-sm);
    line-height: 1.5;
}
.trading-empty__actions {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}
