  :root {
    /* ===== Phase-1 design tokens =====
       Anything below the "legacy" comment is the original palette, kept as
       aliases so existing rules and inline styles keep working during the
       UI/UX migration. New code should reach for the semantic tokens
       (--surface-N, --text-primary/secondary/tertiary, --space-N, --text-N,
       --radius-N, --shadow-N) rather than the raw legacy names.
    */

    /* ----- Surfaces -----
       Semantic wrappers around the existing bg ladder. */
    --surface-0:  #0b1220;   /* page bg */
    --surface-1:  #11192c;   /* cards, panels */
    --surface-2:  #1a2440;   /* nested surface (inputs, headers) */
    --surface-3:  #243056;   /* hover / active state */

    /* ----- Text roles ----- */
    --text-primary:   #e6ecff;
    --text-secondary: #b6bfdb;
    --text-tertiary:  #8a93b3;

    /* ----- Borders ----- */
    --border:        #233055;
    --border-strong: #2d3a64;

    /* ----- Accents ----- */
    --accent:    #6366f1;
    --accent-2:  #818cf8;
    --gradient:  linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #ec4899 100%);

    /* ----- Semantic colors ----- */
    --success:  #10b981;
    --warning:  #f59e0b;
    --danger:   #ef4444;
    --info:     #60a5fa;
    --orange:   #d97706;
    --violet:   #a78bfa;

    /* Soft tinted backgrounds for semantic chips/badges */
    --success-bg: rgba(16, 185, 129, 0.12);
    --warning-bg: rgba(245, 158, 11, 0.12);
    --danger-bg:  rgba(239,  68,  68, 0.12);
    --info-bg:    rgba( 96, 165, 250, 0.12);
    --accent-bg:  rgba( 99, 102, 241, 0.10);
    --neutral-bg: rgba(138, 147, 179, 0.12);

    /* ----- Spacing scale (4px grid) ----- */
    --space-1:   4px;
    --space-2:   8px;
    --space-3:  12px;
    --space-4:  16px;
    --space-5:  20px;
    --space-6:  24px;
    --space-8:  32px;
    --space-10: 40px;
    --space-12: 48px;
    --space-16: 64px;

    /* ----- Type scale ----- */
    --text-xs:   11px;
    --text-sm:   12px;
    --text-base: 13px;
    --text-md:   14px;
    --text-lg:   17px;
    --text-xl:   20px;
    --text-2xl:  24px;
    --text-3xl:  28px;

    --leading-tight:   1.2;
    --leading-normal:  1.5;
    --leading-relaxed: 1.7;

    /* ----- Radius scale ----- */
    --radius-sm:   4px;
    --radius-md:   6px;
    --radius-lg:   8px;
    --radius-xl:   12px;
    --radius-2xl:  16px;
    --radius-3xl:  20px;
    --radius-full: 999px;

    /* ----- Shadow ramp ----- */
    --shadow-sm:   0 1px 2px rgba(0, 0, 0, 0.20);
    --shadow-md:   0 4px 12px rgba(0, 0, 0, 0.25);
    --shadow-lg:   0 10px 30px rgba(0, 0, 0, 0.30);
    --shadow-xl:   0 24px 60px rgba(0, 0, 0, 0.40);
    --shadow-glow: 0 0 0 1px rgba(99, 102, 241, 0.45), 0 8px 24px rgba(99, 102, 241, 0.25);

    /* ----- Motion ----- */
    --transition-fast: 0.10s ease-out;
    --transition-base: 0.15s ease-out;
    --transition-slow: 0.25s ease-out;

    /* ===== Legacy aliases ===== */
    /* Existing rules reference these names — keep them pointing at the
       semantic tokens so the refactor is incremental, not a flag day. */
    --bg-0:        var(--surface-0);
    --bg-1:        var(--surface-1);
    --bg-2:        var(--surface-2);
    --text:        var(--text-primary);
    --text-muted:  var(--text-tertiary);
    --green:       var(--success);
    --red:         var(--danger);
    --amber:       var(--warning);
  }

  /* ===== Phase-1 component primitives =====
     Add these classes to any element instead of writing inline styles.
     They compose: e.g. <button class="btn btn-secondary btn-sm"> or
     <div class="card card-accent is-success">. */

  /* ----- Button variants & sizes -----
     The default .btn (defined further down) is the primary gradient one.
     These modifier classes ride on top of it. */
  .btn.btn-secondary {
    background: var(--surface-2);
    color: var(--text-primary);
    border: 1px solid var(--border);
    box-shadow: none;
  }
  .btn.btn-secondary:hover {
    border-color: var(--accent);
    background: var(--surface-3);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
  }
  .btn.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid transparent;
    box-shadow: none;
  }
  .btn.btn-ghost:hover {
    background: var(--accent-bg);
    color: var(--text-primary);
    transform: none;
    box-shadow: none;
  }
  .btn.btn-danger {
    background: var(--danger);
    color: white;
  }
  .btn.btn-danger:hover { box-shadow: 0 8px 24px rgba(239, 68, 68, 0.4); }
  .btn.btn-sm {
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    border-radius: var(--radius-md);
  }
  .btn.btn-xs {
    padding: 6px 10px;
    font-size: var(--text-xs);
    border-radius: var(--radius-md);
  }
  .btn.btn-lg {
    padding: var(--space-3) var(--space-5);
    font-size: var(--text-md);
  }

  /* ----- Input & select primitives ----- */
  .input, .select {
    background: var(--surface-2);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-2) var(--space-3);
    font-size: var(--text-sm);
    font-family: inherit;
    outline: none;
    transition: border-color var(--transition-base);
  }
  .input:focus, .select:focus { border-color: var(--accent); }
  .select { cursor: pointer; }
  .input::placeholder { color: var(--text-tertiary); }

  /* ----- Card primitive -----
     Used for stat tiles, panels, and any rectangular grouped content. */
  .card {
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-3) var(--space-4);
  }
  .card.card-accent      { border-left: 3px solid var(--border); }
  .card.is-violet        { border-left-color: var(--accent-2); }
  .card.is-info          { border-left-color: var(--info); }
  .card.is-success       { border-left-color: var(--success); }
  .card.is-warning       { border-left-color: var(--warning); }
  .card.is-danger        { border-left-color: var(--danger); }

  /* ----- Stat tile (label / value / hint) -----
     Used by Performance tab + future dashboard widgets. Renamed from
     .stat-card to .stat-tile so it doesn't collide with the existing
     .stat-card used by the Prices tab (which has a different visual
     contract: .ticker/.change/.meta children). */
  .stat-tile {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-left: 3px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-3) var(--space-4);
  }
  .stat-tile.is-info     { border-left: 3px solid var(--info); }
  .stat-tile.is-violet   { border-left: 3px solid var(--accent-2); }
  .stat-tile.is-success  { border-left: 3px solid var(--success); }
  .stat-tile.is-warning  { border-left: 3px solid var(--warning); }
  .stat-tile .stat-label {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
  .stat-tile .stat-value {
    font-size: var(--text-xl);
    font-weight: 600;
    color: var(--text-primary);
    margin-top: var(--space-1);
    line-height: var(--leading-tight);
  }
  .stat-tile .stat-hint {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    margin-top: 2px;
  }

  /* ----- Badge semantic variants -----
     Existing .b-* classes stay; these are the new role-named aliases. */
  .badge.badge-success { background: var(--success-bg); color: var(--success); }
  .badge.badge-warning { background: var(--warning-bg); color: var(--warning); }
  .badge.badge-danger  { background: var(--danger-bg);  color: var(--danger); }
  .badge.badge-info    { background: var(--info-bg);    color: var(--info); }
  .badge.badge-neutral { background: var(--neutral-bg); color: var(--text-tertiary); }

  /* ----- Layout primitives ----- */
  .stack    { display: flex; flex-direction: column; gap: var(--space-3); }
  .cluster  { display: flex; align-items: center; gap: var(--space-2); }
  .grow     { flex: 1; }

  /* ----- Muted uppercase label ----- */
  .muted-label {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }

  /* ----- Empty-state primitives -----
     Two flavours: .empty-inline for one-liners inside panels/popups,
     .empty-block for full-section illustrations with an icon. Replaces
     the inline-style "No data…" patterns that were sprinkled across
     the codebase. */
  .empty-inline {
    color: var(--text-tertiary);
    font-size: var(--text-sm);
    padding: var(--space-3) var(--space-4);
    text-align: center;
  }
  .empty-block {
    color: var(--text-tertiary);
    text-align: center;
    padding: var(--space-12) var(--space-6);
  }
  .empty-block .empty-icon {
    font-size: 48px;
    margin-bottom: var(--space-4);
    opacity: 0.5;
  }
  .empty-block .empty-cta { margin-top: var(--space-4); }

  /* ----- Skeleton loading shimmer -----
     Use .skeleton-row in a <tr> body while data is in flight; the shimmer
     animation matches the .progress-fill ::after one for visual coherence. */
  @keyframes skeleton-shimmer {
    from { background-position: -200% 0; }
    to   { background-position:  200% 0; }
  }
  .skeleton {
    background: linear-gradient(90deg,
      var(--surface-2) 0%,
      var(--surface-3) 50%,
      var(--surface-2) 100%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.6s ease-in-out infinite;
    border-radius: var(--radius-sm);
    color: transparent;
    user-select: none;
  }
  .skeleton-row td { padding-top: var(--space-3); padding-bottom: var(--space-3); }
  .skeleton-row .skeleton {
    display: inline-block;
    height: 12px; min-width: 60px; width: 80%;
  }

  /* ===== Toggle pill (Group by ticker etc.) ===== */
  .toggle-pill {
    display: inline-flex; align-items: center; gap: var(--space-2);
    padding: 6px 12px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-full);
    cursor: pointer;
    user-select: none;
    font-size: var(--text-sm);
    font-family: inherit;
    color: var(--text-secondary);
    transition: background var(--transition-base), color var(--transition-base),
                border-color var(--transition-base);
  }
  .toggle-pill:hover { color: var(--text-primary); border-color: var(--accent); }
  .toggle-pill.is-active {
    background: var(--accent-bg);
    border-color: var(--accent);
    color: var(--accent-2);
  }
  .toggle-pill .toggle-dot {
    width: 8px; height: 8px;
    border-radius: var(--radius-full);
    background: var(--text-tertiary);
    transition: background var(--transition-base);
  }
  .toggle-pill.is-active .toggle-dot { background: var(--accent-2); }

  /* ===== Allocation: clickable tile, sector/account sections ===== */
  .alloc-section + .alloc-section { margin-top: var(--space-4); }
  .alloc-section .alloc-label {
    font-size: var(--text-xs); color: var(--text-tertiary);
    text-transform: uppercase; letter-spacing: 0.05em;
    margin-bottom: var(--space-2);
    display: flex; align-items: center; gap: var(--space-2);
  }
  /* Clickable .stat-tile variant — we render it as a <div> with role=button
     instead of an actual <button>, because the per-tile action icons are
     themselves <button>s and nesting button-in-button is invalid HTML
     (browsers split the outer one and lay them out flat). */
  .stat-tile.is-clickable {
    font-family: inherit;
    cursor: pointer;
    text-align: left;
    transition: border-color var(--transition-base),
                transform var(--transition-fast);
  }
  .stat-tile.is-clickable:hover {
    border-color: var(--accent);
    transform: translateY(-1px);
  }
  .stat-tile.is-clickable:active { transform: translateY(0); }
  .stat-tile.is-clickable.is-selected {
    border-color: var(--accent-2);
    box-shadow: 0 0 0 2px var(--accent-bg);
  }
  .stat-tile.is-clickable:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

  /* ===== Per-tile action icons (rename / delete) =====
     Floats inside the top-right of allocation tiles. Visible on hover.
     Stops click propagation so triggering rename doesn't filter the tile. */
  .stat-tile { position: relative; }
  .stat-tile .tile-actions {
    position: absolute;
    top: 6px; right: 6px;
    display: flex; gap: 2px;
    opacity: 0;
    transition: opacity var(--transition-base);
  }
  .stat-tile:hover .tile-actions,
  .stat-tile:focus-within .tile-actions { opacity: 1; }
  .stat-tile .tile-actions .tile-act {
    background: transparent;
    border: 0;
    cursor: pointer;
    color: var(--text-tertiary);
    padding: 2px 6px;
    font-size: 12px;
    border-radius: var(--radius-sm);
    line-height: 1;
  }
  .stat-tile .tile-actions .tile-act:hover {
    color: var(--accent-2);
    background: var(--surface-2);
  }
  .stat-tile .tile-actions .tile-act.danger:hover { color: var(--danger); }

  /* ===== Active-filter chip strip ===== */
  .pf-active-filters {
    display: flex; gap: var(--space-2); flex-wrap: wrap;
    margin-bottom: var(--space-3);
    align-items: center;
  }
  .pf-filter-chip {
    display: inline-flex; align-items: center; gap: var(--space-2);
    padding: 4px 10px;
    background: var(--accent-bg);
    border: 1px solid var(--accent);
    color: var(--accent-2);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: 600;
  }
  .pf-filter-chip .x {
    cursor: pointer; opacity: 0.7;
    background: transparent; border: 0; color: inherit;
    padding: 0 0 0 4px; font-size: 13px;
  }
  .pf-filter-chip .x:hover { opacity: 1; color: var(--danger); }

  /* ----- Virtualized-row spacers (Performance Phase 7) -----
     Used by the watchlist render() to maintain scroll height while only
     emitting visible rows into the DOM. */
  .virt-spacer { pointer-events: none; }
  .virt-spacer td { padding: 0 !important; border: 0; background: transparent; }
  .virt-spacer:hover td { background: transparent !important; }

  /* ----- Smoother view transitions ----- */
  .view {
    transition: opacity var(--transition-base);
  }
  .view:not(.active) { display: none; }
  .view.active { display: block; opacity: 1; }

  /* ----- Subtler section-header variants ----- */
  .section-header.is-tight { margin-top: var(--space-6); }
  * { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; }
  body {
    font-family: 'Inter', system-ui, -apple-system, sans-serif;
    background:
      radial-gradient(circle at 20% 10%, rgba(99, 102, 241, 0.12) 0%, transparent 40%),
      radial-gradient(circle at 80% 90%, rgba(236, 72, 153, 0.08) 0%, transparent 40%),
      var(--bg-0);
    color: var(--text);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
  }
  /* .container, header, h1, .subtitle —
     removed in Phase-2 of the UI/UX overhaul (left-rail navigation
     superseded the top-stacked layout). Kept the gradient h1 effect on
     .rail-title which is now where the app brand lives. */

  .add-bar {
    display: flex; gap: 8px; align-items: center;
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 6px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.2);
    position: relative;     /* anchor for .ticker-suggest dropdown */
  }
  .add-bar input {
    background: transparent; border: 0;
    color: var(--text); font-size: 14px;
    padding: 10px 14px; min-width: 280px;
    font-family: 'JetBrains Mono', monospace;
    outline: none;
  }
  .add-bar input::placeholder { color: var(--text-muted); }

  /* Yahoo-search-driven autocomplete dropdown */
  .ticker-suggest {
    position: absolute;
    top: calc(100% + 4px); left: 0; right: 0;
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 12px 32px rgba(0,0,0,0.45);
    max-height: 360px; overflow-y: auto;
    z-index: 4000;
    padding: 4px 0;
  }
  .ticker-suggest-item {
    display: flex; justify-content: space-between; align-items: baseline;
    gap: 12px;
    padding: 9px 14px;
    cursor: pointer;
    font-size: 13px;
    line-height: 1.3;
  }
  .ticker-suggest-item:hover,
  .ticker-suggest-item.is-active {
    background: rgba(99,102,241,0.18);
  }
  .ticker-suggest-item .sym {
    font-family: 'JetBrains Mono', monospace;
    font-weight: 700;
    color: var(--text);
  }
  .ticker-suggest-item .nm {
    color: var(--text-muted);
    flex: 1; min-width: 0;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .ticker-suggest-item .exch {
    color: var(--text-muted);
    font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
    flex-shrink: 0;
  }
  .ticker-suggest-empty {
    padding: 12px 14px; color: var(--text-muted);
    font-size: 13px; text-align: center;
  }
  .btn {
    background: var(--gradient);
    color: white; border: 0; cursor: pointer;
    padding: 10px 18px; border-radius: 8px;
    font-weight: 600; font-size: 14px;
    transition: transform 0.1s, box-shadow 0.2s;
  }
  .btn:hover { transform: translateY(-1px); box-shadow: 0 8px 24px rgba(99, 102, 241, 0.4); }
  .btn:active { transform: translateY(0); }
  .btn:disabled { opacity: 0.5; cursor: not-allowed; }

  .table-wrap {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 16px;
    overflow: auto;
    max-height: calc(100vh - 320px);
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
  }
  /* Watchlist table header — sticky so it stays visible on vertical scroll. */
  #tbl th {
    position: sticky; top: 0; z-index: 2;
    background: var(--bg-2);
  }
  table { width: 100%; border-collapse: collapse; font-size: 13px; }
  th {
    text-align: left;
    padding: 14px 16px;
    background: var(--bg-2);
    color: var(--text-muted);
    font-weight: 600; font-size: 11px; text-transform: uppercase;
    letter-spacing: 0.06em;
    cursor: pointer;
    user-select: none;
    border-bottom: 1px solid var(--border);
  }
  th:hover { color: var(--text); }
  td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    font-variant-numeric: tabular-nums;
  }
  tr:last-child td { border-bottom: none; }
  tr:hover td { background: rgba(99, 102, 241, 0.06); }

  .ticker-cell {
    font-family: 'JetBrains Mono', monospace;
    font-weight: 600;
    color: var(--accent-2);
  }
  .name-cell { color: var(--text); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .muted { color: var(--text-muted); }
  .num { text-align: right; }

  .badge {
    display: inline-block;
    padding: 3px 10px; border-radius: 999px;
    font-size: 11px; font-weight: 600;
    letter-spacing: 0.04em;
  }
  .b-buy      { background: rgba(16, 185, 129, 0.15); color: var(--green); }
  .b-oversold { background: rgba(16, 185, 129, 0.15); color: var(--green); }
  .b-overbought { background: rgba(239, 68, 68, 0.15); color: var(--red); }
  .b-downtrend  { background: rgba(239, 68, 68, 0.15); color: var(--red); }
  .b-neutral    { background: rgba(138, 147, 179, 0.15); color: var(--text-muted); }
  .b-stale      { background: rgba(245, 158, 11, 0.15); color: var(--amber); }

  .actions { display: flex; gap: 8px; justify-content: flex-end; }
  .icon-btn {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-muted); cursor: pointer;
    padding: 6px 10px; border-radius: 6px; font-size: 12px;
    transition: all 0.15s;
  }
  .icon-btn:hover { color: var(--text); border-color: var(--accent); background: rgba(99, 102, 241, 0.1); }
  .icon-btn.danger:hover { color: var(--red); border-color: var(--red); background: rgba(239, 68, 68, 0.1); }

  .empty {
    padding: var(--space-12) var(--space-6);
    text-align: center;
    color: var(--text-tertiary);
  }
  .empty-icon {
    font-size: 48px;
    margin-bottom: var(--space-4);
    opacity: 0.5;
  }

  /* ---------------- Loading modal ---------------- */
  .modal-overlay {
    position: fixed; inset: 0;
    background: rgba(11, 18, 32, 0.78);
    backdrop-filter: blur(8px);
    display: none; align-items: center; justify-content: center;
    z-index: 100; opacity: 0;
    transition: opacity 0.2s;
  }
  .modal-overlay.active { display: flex; opacity: 1; }
  .modal {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 32px 36px;
    width: min(440px, calc(100vw - 32px));
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
    text-align: center;
    animation: pop 0.25s cubic-bezier(.2, .9, .3, 1.4);
  }
  @keyframes pop { from { transform: scale(0.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
  .modal h3 { margin: 0 0 6px 0; font-size: 20px; font-weight: 700; }
  .modal-ticker {
    font-family: 'JetBrains Mono', monospace;
    color: var(--accent-2);
  }
  .modal-step { color: var(--text-muted); font-size: 13px; margin: 16px 0 12px 0; min-height: 20px; }

  .progress-track {
    width: 100%; height: 10px;
    background: var(--bg-2);
    border-radius: 999px;
    overflow: hidden;
    position: relative;
    margin-bottom: 8px;
  }
  .progress-fill {
    height: 100%; width: 0;
    background: var(--gradient);
    border-radius: 999px;
    transition: width 0.3s ease-out;
    position: relative;
    overflow: hidden;
  }
  .progress-fill::after {
    content: ''; position: absolute; inset: 0;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.4), transparent);
    animation: shimmer 1.4s infinite;
  }
  @keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
  .progress-pct { font-size: 13px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
  .modal.error .progress-fill { background: var(--red); }

  /* ---------------- Toast ---------------- */
  .toast-wrap {
    position: fixed; bottom: 24px; right: 24px; z-index: 110;
    display: flex; flex-direction: column; gap: 8px;
  }
  .toast {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-left: 4px solid var(--accent);
    color: var(--text);
    padding: 12px 16px; border-radius: 8px;
    font-size: 13px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
    animation: slide-in 0.3s ease-out;
  }
  .toast.success { border-left-color: var(--green); }
  .toast.error   { border-left-color: var(--red); }
  @keyframes slide-in { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

  /* Row-add animation */
  tr.added {
    animation: highlight 1.5s ease-out;
  }
  @keyframes highlight {
    0%   { background: rgba(99, 102, 241, 0.3); }
    100% { background: transparent; }
  }

  .toolbar {
    display: flex; gap: 12px; align-items: center;
    margin-bottom: 12px; padding: 0 4px;
  }
  .toolbar input {
    background: var(--bg-1); border: 1px solid var(--border);
    color: var(--text); padding: 8px 12px; border-radius: 8px;
    font-size: 13px; min-width: 240px; outline: none;
  }
  .toolbar input:focus { border-color: var(--accent); }
  .toolbar .count { color: var(--text-muted); font-size: 13px; margin-left: auto; }

  /* ---------------- Mode selector (global) ---------------- */
  /* .mode-bar, standalone .mode-btn, .refresh-mode — removed in Phase-2.
     The .mode-btn class survives ONLY as a styling hook inside .rail (see
     .rail .mode-btn rules earlier). The spin animation now lives on a
     generic .spin-icon helper used by any button that wants to express
     "loading" feedback. */
  @keyframes spin { to { transform: rotate(360deg); } }
  .spin-icon { display: inline-block; }
  .spinning .spin-icon { animation: spin 0.8s linear infinite; }

  /* ---------------- Market multi-select (All Scanned mode) ---------------- */
  .markets-wrap {
    position: relative;
    display: none;     /* hidden unless mode == 'all' */
    margin-left: 8px;
  }
  .markets-wrap.visible { display: inline-flex; align-items: center; gap: 8px; }
  .markets-btn {
    background: var(--bg-1); border: 1px solid var(--border);
    color: var(--text); padding: 8px 14px; border-radius: 8px;
    cursor: pointer; font-family: inherit; font-size: 13px;
    display: inline-flex; align-items: center; gap: 8px;
    transition: all 0.15s;
  }
  .markets-btn:hover { border-color: var(--accent); }
  .markets-btn .chev { color: var(--text-muted); font-size: 10px; }
  .markets-btn .summary {
    color: var(--text-muted); font-size: 12px;
    font-variant-numeric: tabular-nums;
  }
  .markets-btn .summary.has-selection { color: var(--accent-2); font-weight: 600; }
  .markets-panel {
    position: absolute; top: calc(100% + 6px); left: 0;
    z-index: 50;
    background: var(--bg-2); border: 1px solid var(--border);
    border-radius: 12px; padding: 8px;
    min-width: 280px;
    box-shadow: 0 12px 36px rgba(0,0,0,0.5);
    display: none;
    animation: pop 0.15s ease-out;
  }
  .markets-panel.active { display: block; }
  .markets-panel .panel-row {
    display: flex; align-items: center; gap: 10px;
    padding: 7px 10px; border-radius: 8px;
    cursor: pointer; user-select: none;
    transition: background 0.1s;
  }
  .markets-panel .panel-row:hover { background: var(--bg-1); }
  .markets-panel .panel-row input[type="checkbox"] {
    width: 16px; height: 16px; accent-color: var(--accent);
    cursor: pointer; margin: 0;
  }
  .markets-panel .panel-row .label { flex: 1; color: var(--text); font-size: 13px; }
  .markets-panel .panel-row .cnt {
    color: var(--text-muted); font-size: 11px;
    font-variant-numeric: tabular-nums;
  }
  .markets-panel .panel-footer {
    display: flex; gap: 8px; justify-content: space-between;
    padding: 8px 10px 6px; margin-top: 6px;
    border-top: 1px solid var(--border);
  }
  .markets-panel .panel-footer button {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-muted); padding: 6px 12px; border-radius: 6px;
    cursor: pointer; font-family: inherit; font-size: 12px;
  }
  .markets-panel .panel-footer button:hover { color: var(--text); border-color: var(--accent); }
  .markets-panel .panel-footer .primary {
    background: var(--gradient); color: white; border: 0;
  }

  /* .tabs container, standalone .tab — removed in Phase-2. The .tab
     class survives ONLY as a styling hook inside .rail (see .rail .tab
     rules earlier). View visibility/transition now lives next to the
     other Phase-1 primitives so all view CSS is in one place. */
  @keyframes fade-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
  }
  .view.active { animation: fade-in 0.2s ease-out; }

  /* ---------------- Revenue / EPS sub-tables ---------------- */
  .section-header {
    display: flex; align-items: center; gap: 12px;
    margin: 4px 4px 12px 4px;
  }
  .section-header h2 {
    margin: 0; font-size: 18px; font-weight: 700;
    color: var(--text);
  }
  .section-header .sub { color: var(--text-muted); font-size: 13px; }
  .section-header .spacer { flex: 1; }

  .scroll-x {
    overflow-x: auto;
    overflow-y: auto;
    max-height: calc(100vh - 320px);  /* leaves room for mode bar + toolbar */
    border-radius: 16px;
  }
  .scroll-x::-webkit-scrollbar { height: 10px; width: 10px; }
  .scroll-x::-webkit-scrollbar-track { background: var(--bg-2); }
  .scroll-x::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
  .scroll-x::-webkit-scrollbar-thumb:hover { background: var(--accent); }

  .data-table {
    width: 100%; border-collapse: collapse; font-size: 12.5px;
    min-width: max-content;
  }
  .data-table th {
    background: var(--bg-2); color: var(--text-muted);
    font-weight: 600; font-size: 10.5px; text-transform: uppercase;
    letter-spacing: 0.05em; padding: 10px 12px;
    text-align: left; border-bottom: 1px solid var(--border);
    white-space: nowrap;
    position: sticky; top: 0; z-index: 1;
  }
  .data-table td {
    padding: 9px 12px;
    border-bottom: 1px solid rgba(35, 48, 85, 0.5);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }
  .data-table td.num { text-align: right; }
  .data-table td.tk {
    font-family: 'JetBrains Mono', monospace;
    color: var(--accent-2); font-weight: 600;
  }
  .data-table tr:hover td { background: rgba(99, 102, 241, 0.05); }

  .delta-pos { color: var(--green); font-weight: 600; }
  .delta-neg { color: var(--red);   font-weight: 600; }

  /* ============ Option detail modal ============ */
  .opt-metrics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 8px;
    padding: 12px 18px;
    border-bottom: 1px solid var(--border);
  }
  .opt-metric {
    background: var(--surface-2, rgba(255,255,255,0.04));
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 10px;
    display: flex; flex-direction: column; gap: 2px;
  }
  .opt-metric-label {
    font-size: 10px;
    text-transform: uppercase; letter-spacing: 0.05em;
    color: var(--text-muted);
  }
  .opt-metric-value {
    font-size: 15px; font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text);
  }
  .opt-panel {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
  }
  .opt-panel:last-of-type { border-bottom: 0; }
  .opt-panel-title {
    font-size: 13px; font-weight: 700;
    color: var(--text);
    margin-bottom: 2px;
  }
  .opt-panel-sub {
    font-size: 11.5px;
    color: var(--text-muted);
    margin-bottom: 10px;
    line-height: 1.4;
  }
  .opt-chart-wrap { height: 340px; }
  .opt-chart-wrap.tall { height: 460px; }
  .opt-chart-wrap > div { width: 100%; height: 100%; }
  .opt-detail-loading {
    position: absolute; inset: 0;
    background: rgba(11, 18, 32, 0.7);
    display: none; align-items: center; justify-content: center;
    color: var(--text-muted);
  }
  #opt-detail-overlay.active .opt-detail-loading.is-loading { display: flex; }

  /* Stock / Option toggle inside the Add-position modal */
  .instrument-toggle {
    display: inline-flex;
    gap: 4px;
    padding: 4px;
    background: var(--surface-2, rgba(255,255,255,0.05));
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-top: 8px;
  }
  .instrument-pill {
    background: transparent; border: 0;
    color: var(--text-muted);
    padding: 6px 14px;
    border-radius: 7px;
    cursor: pointer;
    font-size: 13px; font-weight: 600;
    display: inline-flex; align-items: center; gap: 6px;
    transition: background 120ms, color 120ms;
  }
  .instrument-pill:hover { color: var(--text); }
  .instrument-pill.is-active {
    background: rgba(99, 102, 241, 0.25);
    color: var(--accent-2);
    box-shadow: inset 0 0 0 1px rgba(99,102,241,0.5);
  }
  .pill-ico { font-size: 14px; }

  /* Call/Put badges in the positions table */
  .opt-badge {
    display: inline-block;
    font-size: 10px; font-weight: 700;
    letter-spacing: 0.04em;
    padding: 2px 6px;
    border-radius: 4px;
    margin-left: 6px;
    vertical-align: middle;
    text-transform: uppercase;
  }
  .opt-badge.call { background: rgba(16,185,129,0.18); color: #10b981; }
  .opt-badge.put  { background: rgba(220,38,38,0.18);  color: #f87171; }
  .opt-meta {
    display: block;
    font-size: 10.5px;
    color: var(--text-muted);
    margin-top: 2px;
    font-family: 'JetBrains Mono', monospace;
  }

  /* "↔ Drag to compare" passive hint — sits where the old Compare
     button lived. Quietly tells the user that the default chart drag
     gesture is compare (not zoom). */
  .compare-hint {
    display: inline-flex; align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: var(--radius-md);
    background: rgba(99, 102, 241, 0.08);
    border: 1px dashed rgba(99, 102, 241, 0.35);
    color: var(--accent-2);
    font-size: var(--text-xs);
    font-weight: 500;
    user-select: none;
    cursor: help;
  }

  /* Portfolio table — column reorder drag-and-drop affordance */
  #pf-head th[draggable="true"] { cursor: grab; user-select: none; }
  #pf-head th[draggable="true"]:active { cursor: grabbing; }
  #pf-head th.is-dragging { opacity: 0.4; }
  #pf-head th.is-drop-target {
    box-shadow: inset 4px 0 0 var(--accent);
    background: rgba(99, 102, 241, 0.12);
  }

  .ghost-empty {
    padding: 36px; text-align: center; color: var(--text-muted);
    background: var(--bg-1); border: 1px dashed var(--border);
    border-radius: 16px;
  }

  /* Inline progress bar for the revenue/EPS load */
  .inline-progress {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px 20px;
    margin-bottom: 16px;
  }
  .inline-progress .row {
    display: flex; align-items: center; gap: 12px;
    margin-bottom: 8px;
  }
  .inline-progress .step { color: var(--text-muted); font-size: 13px; flex: 1; }
  .inline-progress .pct  { color: var(--text); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }

  /* ---------------- Prices tab (Google Finance-ish) ---------------- */
  .period-bar {
    display: flex; gap: 4px;
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 4px;
    width: fit-content;
  }
  .period-btn {
    background: transparent; border: 0;
    color: var(--text-muted);
    font-family: inherit; font-size: 12px; font-weight: 600;
    padding: 7px 14px; border-radius: 7px;
    cursor: pointer; transition: all 0.12s;
  }
  .period-btn:hover { color: var(--text); }
  .period-btn.active {
    background: var(--bg-2); color: var(--text);
    box-shadow: inset 0 0 0 1px var(--accent);
  }

  .chart-wrap {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: var(--radius-2xl);
    padding: var(--space-4);
    margin-top: var(--space-3);
    box-shadow: var(--shadow-lg);
    position: relative;   /* anchor for the compare overlay */
  }
  #price-chart {
    width: 100%; height: 480px;
  }

  /* ===== Compare-tool button + overlay =====
     Active state shows the button as accent-tinted. The overlay is
     positioned over the chart and shows ΔPrice / Δ% / Δtime / annualized
     return between the two endpoints of a selection rectangle. */
  .compare-btn { white-space: nowrap; }
  .compare-btn.is-active {
    background: var(--accent-bg);
    border-color: var(--accent);
    color: var(--accent-2);
  }
  .compare-overlay {
    position: absolute;
    top: var(--space-3);
    /* Default position is top-right. The renderer toggles .is-left to
       flip it to top-left when the user is selecting in the right half
       of the chart, so the overlay never covers the cursor / selection. */
    right: var(--space-3);
    left: auto;
    z-index: 5;
    pointer-events: none;
    background: rgba(11, 18, 32, 0.94);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    padding: var(--space-3) var(--space-4);
    min-width: 220px;
    max-width: min(360px, 50vw);
    box-shadow: var(--shadow-lg);
    backdrop-filter: blur(8px);
    display: none;
    font-size: var(--text-sm);
    transition: left var(--transition-base), right var(--transition-base);
  }
  .compare-overlay.is-active { display: block; }
  .compare-overlay.is-left  { right: auto;            left: var(--space-3); }
  .compare-overlay.is-right { right: var(--space-3);  left: auto; }
  .compare-overlay .co-range {
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    margin-bottom: var(--space-2);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-variant-numeric: tabular-nums;
  }
  .compare-overlay .co-row {
    display: flex; justify-content: space-between; align-items: baseline;
    padding: 4px 0;
    font-variant-numeric: tabular-nums;
  }
  .compare-overlay .co-row .co-ticker {
    font-family: 'JetBrains Mono', monospace;
    color: var(--accent-2);
    font-weight: 600;
    font-size: var(--text-sm);
  }
  .compare-overlay .co-row .co-delta {
    font-weight: 600;
    font-size: var(--text-sm);
  }
  .compare-overlay .co-row .co-delta.pos { color: var(--success); }
  .compare-overlay .co-row .co-delta.neg { color: var(--danger); }
  .compare-overlay .co-row .co-prices {
    color: var(--text-tertiary);
    font-size: var(--text-xs);
    margin-left: var(--space-2);
  }
  .compare-overlay .co-meta {
    margin-top: var(--space-2);
    padding-top: var(--space-2);
    border-top: 1px solid var(--border);
    font-size: var(--text-xs);
    color: var(--text-tertiary);
    font-variant-numeric: tabular-nums;
  }

  /* ===== Responsive Plotly helpers =====
     Below 768px (tablet) shrink chart heights, hide non-essential meta. */
  .hide-on-mobile { display: inline; }
  @media (max-width: 768px) {
    #price-chart { height: 380px; }
    .chart-wrap  { padding: var(--space-3); border-radius: var(--radius-xl); }
    .hide-on-mobile { display: none; }
    .compare-overlay {
      top: var(--space-2);
      padding: var(--space-2) var(--space-3);
      min-width: 180px;
    }
    /* Tighter side-spacing on mobile, keeping the smart left/right
       positioning intact via the same specificity. */
    .compare-overlay.is-right { right: var(--space-2); left: auto; }
    .compare-overlay.is-left  { left: var(--space-2);  right: auto; }
  }
  @media (max-width: 480px) {
    #price-chart { height: 300px; }
    .toolbar { flex-wrap: wrap; gap: var(--space-2); }
    .period-bar { flex-wrap: wrap; }
  }

  .stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: var(--space-3);
    margin-top: var(--space-4);
  }
  /* Performance tab's stats strip sits ABOVE the table, not below a chart,
     so it needs a bottom margin instead of a top one. */
  .perf-stats-grid { margin-top: 0; margin-bottom: var(--space-4); }
  .stat-card {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px 16px;
    transition: border-color 0.15s, transform 0.1s;
  }
  .stat-card.pos { border-left: 3px solid var(--green); }
  .stat-card.neg { border-left: 3px solid var(--red); }
  .stat-card .ticker {
    font-family: 'JetBrains Mono', monospace; font-weight: 600;
    color: var(--accent-2); font-size: 14px;
  }
  .stat-card .change {
    font-size: 22px; font-weight: 700; margin-top: 4px;
    font-variant-numeric: tabular-nums;
  }
  .stat-card.pos .change { color: var(--green); }
  .stat-card.neg .change { color: var(--red); }
  .stat-card .meta { color: var(--text-muted); font-size: 11px; margin-top: 2px; }
  .stat-card .price-pair { color: var(--text-muted); font-size: 11px; margin-top: 6px;
                           font-variant-numeric: tabular-nums; }

  .range-label {
    display: flex; align-items: center; gap: 8px;
    margin-top: 16px; color: var(--text-muted); font-size: 13px;
  }
  .range-label b { color: var(--text); }
  .range-reset {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-muted); padding: 4px 10px; border-radius: 6px;
    cursor: pointer; font-size: 11px;
  }
  .range-reset:hover { color: var(--text); border-color: var(--accent); }

  /* ---------------- Click-through detail modal ---------------- */
  .detail-overlay {
    position: fixed; inset: 0;
    background: rgba(11, 18, 32, 0.85);
    backdrop-filter: blur(12px);
    display: none; align-items: center; justify-content: center;
    z-index: 200; opacity: 0;
    transition: opacity 0.2s;
  }
  .detail-overlay.active { display: flex; opacity: 1; }
  .detail-modal {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 20px 24px 16px;
    width: 96vw; max-width: 1500px;
    height: 92vh;
    display: flex; flex-direction: column;
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.7);
    animation: pop 0.28s cubic-bezier(.2, .9, .3, 1.4);
  }
  .detail-header {
    display: flex; align-items: flex-start; gap: 24px;
    padding-bottom: 16px; border-bottom: 1px solid var(--border);
    margin-bottom: 12px;
  }
  .detail-header .title { flex: 1; min-width: 0; }
  .detail-header h2 {
    margin: 0;
    font-family: 'JetBrains Mono', monospace; font-size: 26px; font-weight: 700;
    background: var(--gradient);
    -webkit-background-clip: text; background-clip: text; color: transparent;
  }
  .detail-header .name { color: var(--text-muted); font-size: 14px; margin-top: 4px; }
  .detail-header .price-block { text-align: right; }
  .detail-header .price {
    font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums;
    line-height: 1;
  }
  .detail-header .change {
    font-size: 14px; font-weight: 600; margin-top: 6px;
    font-variant-numeric: tabular-nums;
  }
  .detail-header .change.pos { color: var(--green); }
  .detail-header .change.neg { color: var(--red); }

  /* Mobile: shrink the header so the company name fits on one line and
     the price/close column stops crowding the title. The .name text
     gets nowrap + ellipsis so a long company name truncates instead of
     wrapping over three lines. */
  @media (max-width: 768px) {
    .detail-header {
      gap: var(--space-3);
      padding-bottom: var(--space-2);
      margin-bottom: var(--space-2);
    }
    .detail-header h2  { font-size: 18px; }
    .detail-header .name {
      font-size: 12px; margin-top: 2px;
      white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .detail-header .price  { font-size: 18px; }
    .detail-header .change { font-size: 12px; margin-top: 2px; }
    .detail-close { width: 28px; height: 28px; font-size: 14px; }
    .detail-modal {
      padding: var(--space-3);
      width: 100vw; height: 100vh;
      max-width: 100vw;
      border-radius: 0;
    }
    .detail-controls { gap: var(--space-2); flex-wrap: wrap; }
  }

  .detail-controls {
    display: flex; align-items: center; gap: 12px;
    margin-bottom: 8px;
  }
  .detail-close {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-muted);
    width: 36px; height: 36px; border-radius: 10px;
    cursor: pointer; font-size: 18px;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.15s;
  }
  .detail-close:hover { color: var(--red); border-color: var(--red); background: rgba(239, 68, 68, 0.1); }

  .detail-chart {
    flex: 1; min-height: 0;     /* let flex shrink to fit */
    width: 100%;
    position: relative;         /* anchor for .compare-overlay */
  }
  #detail-chart { width: 100%; height: 100%; }

  .detail-loading {
    flex: 1;
    display: flex; align-items: center; justify-content: center;
    color: var(--text-muted); font-size: 14px;
  }

  /* Make ticker cells visibly clickable everywhere */
  .ticker-cell, td.tk { cursor: pointer; }
  .ticker-cell:hover, td.tk:hover { text-decoration: underline; }

  /* ---------------- Sortable headers + filter button ---------------- */
  .data-table th.sortable {
    cursor: pointer; user-select: none;
    transition: color 0.12s;
  }
  .data-table th.sortable:hover { color: var(--text); }
  .data-table th .sort-arrow {
    color: var(--accent); font-weight: 700; margin-left: 4px;
  }
  .data-table th .filter-icon {
    display: inline-block; margin-left: 6px;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 12px; line-height: 1;
    width: 16px; height: 16px;
    text-align: center;
    border-radius: 4px;
    transition: all 0.15s;
    border: 1px solid transparent;
  }
  .data-table th .filter-icon:hover {
    color: var(--accent); background: rgba(99, 102, 241, 0.15);
    border-color: var(--border);
  }
  .data-table th .filter-icon.active {
    color: white;
    background: var(--accent);
    border-color: var(--accent);
    font-weight: bold;
  }

  /* Filter popup */
  #filter-popup {
    position: absolute;
    z-index: 180;
    background: var(--bg-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px 18px 12px;
    width: 260px;
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.6);
    display: none;
    animation: pop 0.15s ease-out;
  }
  #filter-popup.active { display: block; }
  #filter-popup .popup-title {
    color: var(--text); font-size: 13px; font-weight: 600;
    margin-bottom: 10px;
    border-bottom: 1px solid var(--border); padding-bottom: 8px;
  }
  #filter-popup .popup-title b {
    color: var(--accent-2);
    font-family: 'JetBrains Mono', monospace;
  }
  #filter-popup .popup-row {
    display: flex; align-items: center; gap: 10px; margin-bottom: 8px;
  }
  #filter-popup .popup-row label {
    color: var(--text-muted); font-size: 11px; width: 38px;
    text-transform: uppercase; letter-spacing: 0.04em;
  }
  #filter-popup .popup-row input[type="number"] {
    flex: 1;
    background: var(--bg-1); border: 1px solid var(--border);
    color: var(--text); padding: 6px 10px; border-radius: 6px;
    font-size: 13px; outline: none;
    font-variant-numeric: tabular-nums;
  }
  #filter-popup .popup-row input[type="number"]:focus { border-color: var(--accent); }
  #filter-popup .popup-row input[type="range"] {
    width: 100%; accent-color: var(--accent);
  }
  #filter-popup .popup-meta {
    color: var(--text-muted); font-size: 11px;
    margin: 4px 0 10px; font-variant-numeric: tabular-nums;
  }
  #filter-popup .popup-buttons {
    display: flex; gap: 6px; justify-content: flex-end;
    margin-top: 8px;
  }
  #filter-popup .popup-buttons button {
    background: transparent; border: 1px solid var(--border);
    color: var(--text-muted); padding: 6px 12px; border-radius: 6px;
    cursor: pointer; font-size: 12px; font-family: inherit;
    transition: all 0.12s;
  }
  #filter-popup .popup-buttons button:hover { color: var(--text); border-color: var(--accent); }
  #filter-popup .popup-buttons .primary {
    background: var(--gradient); color: white; border: 0;
  }
  #filter-popup .popup-buttons .primary:hover { color: white; }

  /* ---------------- Drag-to-reorder column visuals ---------------- */
  .draggable-col {
    cursor: grab;
    user-select: none;
  }
  .draggable-col:active { cursor: grabbing; }
  .col-dragging {
    opacity: 0.55;
    background: var(--bg-1) !important;
  }
  .col-drop-target {
    box-shadow: inset 3px 0 0 var(--accent);
  }

  /* ---------------- Presets dropdown (reuses markets-panel styles) ---------------- */
  .presets-wrap { position: relative; }
  /* The Presets button lives on the right side of the toolbar, so anchor
     the panel to its right edge and let it grow leftward — otherwise the
     default left:0 anchor pushes the panel off-screen. */
  #presets-panel {
    left: auto;
    right: 0;
    min-width: 320px;
    max-width: min(420px, 96vw);
  }
  /* Preset list rows: name on the left (click to load), ✕ on the right. */
  #presets-list .panel-row {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 10px; border-radius: 8px;
    user-select: none;
    transition: background 0.1s;
  }
  #presets-list .panel-row:hover { background: var(--bg-1); }
  #presets-list .panel-row .label {
    flex: 1; cursor: pointer; color: var(--text); font-size: 13px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  #presets-list .panel-row .label:hover { color: var(--accent-2); }
  #presets-list .panel-row .x {
    cursor: pointer; color: var(--text-muted);
    width: 24px; height: 24px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 6px;
    transition: all 0.12s;
    font-size: 14px;
  }
  #presets-list .panel-row .x:hover {
    color: var(--red);
    background: rgba(239, 68, 68, 0.12);
  }

  /* Active-filter summary bar above each table */
  .active-filters {
    display: flex; flex-wrap: wrap; gap: 6px;
    margin: 8px 4px 0;
    min-height: 0;
  }
  .filter-chip {
    background: rgba(99, 102, 241, 0.15); color: var(--accent-2);
    border: 1px solid rgba(99, 102, 241, 0.4);
    padding: 3px 10px; border-radius: 999px;
    font-size: 11px;
    display: inline-flex; align-items: center; gap: 6px;
  }
  .filter-chip .x { cursor: pointer; opacity: 0.7; }
  .filter-chip .x:hover { opacity: 1; color: var(--red); }

  /* ====== Phase-2 App Shell: left rail + top bar + content area ======
     Replaces the previous top-stacked layout (header → mode bar → tabs).
     Body becomes a 2-column grid; aside.rail is sticky-fixed on the left,
     main.app-main holds the topbar + content area. On mobile (<768px)
     the rail collapses to a slide-in drawer triggered by the hamburger
     button. */
  body.has-rail {
    display: grid;
    grid-template-columns: 240px minmax(0, 1fr);
    /* Inherits the radial-gradient background from the base `body` rule
       so the main content area keeps the violet/pink ambient glow. The
       rail and topbar each set their own opaque surface color, so the
       gradient only shows through behind the content. */
  }
  body.has-rail.rail-collapsed {
    grid-template-columns: 64px minmax(0, 1fr);
  }

  .rail {
    background: var(--surface-1);
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    height: 100vh;
    position: sticky; top: 0;
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 50;
    transition: width var(--transition-slow);
  }
  .rail-header {
    display: flex; align-items: center;
    gap: var(--space-3);
    padding: var(--space-4);
    border-bottom: 1px solid var(--border);
    min-height: 64px;
  }
  .rail-brand { font-size: 22px; flex-shrink: 0; }
  .rail-title {
    font-size: var(--text-md);
    font-weight: 700;
    background: var(--gradient);
    -webkit-background-clip: text; background-clip: text; color: transparent;
    flex: 1; min-width: 0;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .rail-toggle {
    background: transparent; border: 0; cursor: pointer;
    color: var(--text-tertiary);
    padding: 6px; border-radius: var(--radius-sm);
    flex-shrink: 0;
    width: 28px; height: 28px;
    display: flex; align-items: center; justify-content: center;
    transition: background var(--transition-base), color var(--transition-base);
  }
  .rail-toggle:hover { color: var(--text-primary); background: var(--accent-bg); }

  .rail-nav {
    flex: 1;
    padding: var(--space-3);
    overflow-y: auto;
  }
  .rail-group { margin-bottom: var(--space-5); }
  .rail-group-label {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-tertiary);
    padding: var(--space-2) var(--space-3);
    font-weight: 600;
    display: flex; flex-direction: column; gap: 2px;
  }
  .rail-group-hint {
    font-size: 10.5px;
    text-transform: none;
    letter-spacing: 0;
    color: var(--text-muted);
    font-weight: 400;
    font-style: italic;
  }

  /* ============ Workspace card (Portfolio) ============
     Visually distinct from the Explore matrix below so the user can
     tell at a glance "this is its own place, not a view of the lists". */
  .rail-group-workspace {
    margin-bottom: var(--space-4);
    padding-bottom: var(--space-4);
    border-bottom: 1px solid var(--border);
  }
  .rail .tab.rail-card {
    background: linear-gradient(135deg,
      rgba(99,102,241,0.14) 0%,
      rgba(139,92,246,0.10) 100%);
    border: 1px solid rgba(99,102,241,0.28);
    padding: 10px 12px;
    align-items: flex-start;
    border-radius: 10px;
  }
  .rail .tab.rail-card:hover {
    background: linear-gradient(135deg,
      rgba(99,102,241,0.22) 0%,
      rgba(139,92,246,0.16) 100%);
    border-color: rgba(99,102,241,0.5);
  }
  .rail .tab.rail-card.active {
    background: linear-gradient(135deg,
      rgba(99,102,241,0.32) 0%,
      rgba(139,92,246,0.24) 100%);
    border-color: rgba(99,102,241,0.7);
    box-shadow: 0 6px 18px rgba(99,102,241,0.25);
  }
  .rail-card .rail-ico { font-size: 22px; line-height: 1; padding-top: 1px; }
  .rail-card-body { display: flex; flex-direction: column; gap: 1px; min-width: 0; flex: 1; }
  .rail-card-sub {
    font-size: 10.5px;
    color: var(--text-muted);
    font-weight: 400;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .rail .tab.rail-card.active .rail-card-sub { color: var(--text-secondary); }

  /* ============ Explore matrix — two visually grouped sub-steps ============
     The "Show as" sub-step is indented + has a left rail so the user reads
     it as "the second axis of the choice above". */
  .rail-substep {
    padding: 6px 0 4px;
  }
  .rail-substep-label {
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    padding: 2px var(--space-3) 4px;
    font-weight: 600;
  }
  .rail-substep[data-step="view"] {
    margin-left: 6px;
    padding-left: 8px;
    border-left: 2px solid var(--border);
    position: relative;
  }
  .rail-substep[data-step="view"]::before {
    content: "";
    position: absolute;
    left: -2px; top: 0; height: 14px;
    width: 2px; background: var(--accent-2);
    border-radius: 1px;
  }

  /* ============ Mutual-exclusion dim ============
     When the user is in the Portfolio workspace, fade the Explore section
     to ~55% opacity so it's obviously not the active workspace. Same in
     reverse — when in Explore, the Portfolio card is normal-strength but
     unselected. Toggle is driven by body class "in-portfolio" which JS
     sets when Portfolio tab is active. */
  body.in-portfolio .rail-group-explore { opacity: 0.55; }
  body.in-portfolio .rail-group-explore:hover { opacity: 1; }

  /* Make .mode-btn and .tab render as rail items when inside the rail. */
  .rail .mode-btn, .rail .tab {
    width: 100%;
    display: flex; align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    margin: 1px 0;
    background: transparent;
    color: var(--text-secondary);
    border: 0; border-radius: var(--radius-md);
    cursor: pointer; text-align: left;
    font-size: var(--text-sm); font-weight: 500;
    font-family: inherit;
    transition: background var(--transition-base), color var(--transition-base);
  }
  .rail .mode-btn:hover, .rail .tab:hover {
    background: var(--accent-bg);
    color: var(--text-primary);
  }
  .rail .mode-btn.active, .rail .tab.active {
    background: var(--accent-bg);
    color: var(--accent-2);
    font-weight: 600;
  }
  .rail .mode-btn:active, .rail .tab:active, .rail-footer-btn:active {
    transform: translateY(0.5px);
  }
  .rail .rail-ico {
    font-size: 18px; line-height: 1;
    width: 24px; flex-shrink: 0;
    text-align: center;
  }
  .rail .rail-text {
    flex: 1; min-width: 0;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .rail .rail-badge {
    background: var(--surface-2);
    color: var(--text-tertiary);
    padding: 2px 8px;
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    flex-shrink: 0;
  }
  .rail .mode-btn.active .rail-badge,
  .rail .tab.active     .rail-badge {
    background: rgba(129, 140, 248, 0.2);
    color: var(--accent-2);
  }

  .rail-footer {
    padding: var(--space-3);
    border-top: 1px solid var(--border);
    display: flex; flex-direction: column; gap: var(--space-1);
  }
  .rail-footer-btn {
    width: 100%;
    display: flex; align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    background: transparent;
    color: var(--text-secondary);
    border: 0; border-radius: var(--radius-md);
    cursor: pointer; text-align: left;
    font-size: var(--text-sm); font-family: inherit;
    transition: background var(--transition-base), color var(--transition-base);
  }
  .rail-footer-btn:hover {
    background: var(--accent-bg);
    color: var(--text-primary);
  }

  /* ----- Collapsed rail ----- */
  body.has-rail.rail-collapsed .rail-title,
  body.has-rail.rail-collapsed .rail-text,
  body.has-rail.rail-collapsed .rail-badge,
  body.has-rail.rail-collapsed .rail-group-label,
  body.has-rail.rail-collapsed .rail-substep-label,
  body.has-rail.rail-collapsed .rail-card-sub,
  body.has-rail.rail-collapsed .density-pip {
    display: none;
  }
  /* Collapsed rail: drop the Show-as indent guide rail too. */
  body.has-rail.rail-collapsed .rail-substep[data-step="view"] {
    margin-left: 0; padding-left: 0; border-left: 0;
  }
  body.has-rail.rail-collapsed .rail-substep[data-step="view"]::before {
    display: none;
  }
  body.has-rail.rail-collapsed .rail .mode-btn,
  body.has-rail.rail-collapsed .rail .tab,
  body.has-rail.rail-collapsed .rail-footer-btn {
    justify-content: center;
    padding: var(--space-3) 0;
  }
  body.has-rail.rail-collapsed .rail-header { justify-content: center; }
  body.has-rail.rail-collapsed .rail-toggle::before { content: "»"; }
  body.has-rail:not(.rail-collapsed) .rail-toggle::before { content: "«"; }

  /* ----- Main content area ----- */
  .app-main {
    display: flex; flex-direction: column;
    min-width: 0;
    min-height: 100vh;
  }
  .app-topbar {
    display: flex; align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-5);
    border-bottom: 1px solid var(--border);
    background: rgba(11, 18, 32, 0.72);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    position: sticky; top: 0; z-index: 30;
    min-height: 64px;
  }
  .topbar-breadcrumb {
    color: var(--text-tertiary);
    font-size: var(--text-sm);
    display: flex; align-items: center; gap: var(--space-2);
    min-width: 0;
  }
  .topbar-breadcrumb b {
    color: var(--text-primary);
    font-weight: 600;
  }
  .topbar-breadcrumb .crumb-sep { color: var(--text-tertiary); }
  .app-content {
    flex: 1;
    padding: var(--space-5);
    min-width: 0;
  }

  /* ----- Hamburger (mobile only) ----- */
  .rail-hamburger {
    display: none;
    background: var(--surface-1);
    border: 1px solid var(--border);
    color: var(--text-primary);
    width: 40px; height: 40px;
    border-radius: var(--radius-md);
    align-items: center; justify-content: center;
    cursor: pointer;
    font-size: 18px;
    flex-shrink: 0;
  }
  .rail-hamburger:hover { background: var(--surface-2); border-color: var(--accent); }
  .rail-scrim {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    z-index: 49;
  }

  /* ----- Tablet (768-1024px): collapse rail to icons-only by default ----- */
  @media (max-width: 1024px) {
    body.has-rail {
      grid-template-columns: 64px minmax(0, 1fr);
    }
    body.has-rail .rail-title,
    body.has-rail .rail-text,
    body.has-rail .rail-badge,
    body.has-rail .rail-group-label,
    body.has-rail .rail-substep-label,
    body.has-rail .rail-card-sub,
    body.has-rail .density-pip {
      display: none;
    }
    body.has-rail .rail-substep[data-step="view"] {
      margin-left: 0; padding-left: 0; border-left: 0;
    }
    body.has-rail .rail-substep[data-step="view"]::before { display: none; }
    body.has-rail .rail .mode-btn,
    body.has-rail .rail .tab,
    body.has-rail .rail-footer-btn {
      justify-content: center;
      padding: var(--space-3) 0;
    }
    body.has-rail .rail-header { justify-content: center; }
    body.has-rail .rail-toggle { display: none; }
  }

  /* ====== Phase-3 Density toggle ======
     Three modes: Compact, Comfortable (default — no class), Spacious.
     Class on <body> drives the overrides. We touch the highest-impact
     surfaces (tables, toolbars, buttons, stat tiles, charts) so the
     visual change is immediate; secondary chrome stays comfortable.
  */

  /* ----- COMPACT ----- */
  body.density-compact table { font-size: var(--text-sm); }
  body.density-compact th { padding: var(--space-2) var(--space-3); font-size: var(--text-xs); }
  body.density-compact td { padding: 6px var(--space-3); }
  body.density-compact .data-table th,
  body.density-compact .data-table td { padding: 6px var(--space-3); }
  body.density-compact .toolbar { padding: var(--space-2) var(--space-3); margin-bottom: var(--space-2); }
  body.density-compact .toolbar.cluster { gap: var(--space-2); }
  body.density-compact .stat-tile,
  body.density-compact .stat-card { padding: var(--space-2) var(--space-3); }
  body.density-compact .stat-tile .stat-value { font-size: var(--text-lg); }
  body.density-compact .stats-grid { gap: var(--space-2); margin-top: var(--space-2); }
  body.density-compact .section-header h2 { font-size: var(--text-md); }
  body.density-compact .section-header { margin-top: var(--space-3); margin-bottom: var(--space-2); }
  body.density-compact .table-wrap { border-radius: var(--radius-lg); }
  body.density-compact .chart-wrap { padding: var(--space-2); }
  body.density-compact #price-chart { height: 360px; }
  body.density-compact .app-content { padding: var(--space-3); }
  body.density-compact .app-topbar { min-height: 52px; padding-top: var(--space-2); padding-bottom: var(--space-2); }
  body.density-compact .btn { padding: 6px var(--space-3); font-size: var(--text-sm); }
  body.density-compact .btn.btn-sm { padding: 4px var(--space-2); font-size: var(--text-xs); }
  body.density-compact .rail .mode-btn,
  body.density-compact .rail .tab,
  body.density-compact .rail-footer-btn { padding: 6px var(--space-2); font-size: var(--text-xs); }

  /* ----- SPACIOUS ----- */
  body.density-spacious table { font-size: var(--text-md); }
  body.density-spacious th { padding: 18px var(--space-5); font-size: var(--text-sm); }
  body.density-spacious td { padding: var(--space-4) var(--space-5); }
  body.density-spacious .data-table th,
  body.density-spacious .data-table td { padding: var(--space-4) var(--space-5); }
  body.density-spacious .toolbar { padding: var(--space-4) var(--space-5); margin-bottom: var(--space-4); }
  body.density-spacious .toolbar.cluster { gap: var(--space-4); }
  body.density-spacious .stat-tile,
  body.density-spacious .stat-card { padding: var(--space-5) var(--space-6); }
  body.density-spacious .stat-tile .stat-value { font-size: var(--text-2xl); }
  body.density-spacious .stats-grid { gap: var(--space-4); margin-top: var(--space-5); }
  body.density-spacious .section-header h2 { font-size: var(--text-xl); }
  body.density-spacious .section-header { margin-top: var(--space-6); margin-bottom: var(--space-4); }
  body.density-spacious .chart-wrap { padding: var(--space-5); }
  body.density-spacious #price-chart { height: 560px; }
  body.density-spacious .app-content { padding: var(--space-6) var(--space-8); }
  body.density-spacious .app-topbar { min-height: 76px; padding-top: var(--space-4); padding-bottom: var(--space-4); }
  body.density-spacious .btn { padding: var(--space-3) var(--space-5); font-size: var(--text-md); }
  body.density-spacious .btn.btn-sm { padding: var(--space-2) var(--space-4); font-size: var(--text-sm); }
  body.density-spacious .rail .mode-btn,
  body.density-spacious .rail .tab,
  body.density-spacious .rail-footer-btn { padding: var(--space-3) var(--space-4); font-size: var(--text-md); }

  /* Density indicator on the rail-footer button: which icon segment is
     lit reflects the current setting. CSS pseudo-element ::before paints
     a row of three "rungs" and the active one gets the accent color. */
  .density-pip {
    display: inline-flex; flex-direction: column;
    gap: 2px;
    margin-left: auto;
    flex-shrink: 0;
  }
  .density-pip span {
    width: 16px; height: 2px;
    background: var(--text-tertiary);
    border-radius: 1px;
    transition: background var(--transition-base);
  }
  body.density-compact   .density-pip span:nth-child(1),
  body.density-compact   .density-pip span:nth-child(2),
  body.density-compact   .density-pip span:nth-child(3) { background: var(--accent-2); }
  body.density-compact   .density-pip span:nth-child(1) { height: 4px; }
  /* "Comfortable" = no body class — middle rung lights up */
  body:not(.density-compact):not(.density-spacious) .density-pip span:nth-child(2) {
    background: var(--accent-2); height: 4px;
  }
  body.density-spacious  .density-pip span:nth-child(3) { background: var(--accent-2); height: 4px; }

  /* ----- Mobile (<768px): rail becomes slide-in drawer ----- */
  @media (max-width: 768px) {
    body.has-rail {
      grid-template-columns: minmax(0, 1fr);
    }
    .rail {
      position: fixed; top: 0; left: 0;
      width: 240px; height: 100vh;
      transform: translateX(-100%);
      transition: transform var(--transition-slow);
      box-shadow: var(--shadow-xl);
    }
    body.has-rail.rail-open .rail {
      transform: translateX(0);
    }
    body.has-rail.rail-open .rail-scrim { display: block; }
    /* Open state: show full labels even on mobile so the drawer is usable */
    body.has-rail.rail-open .rail-title,
    body.has-rail.rail-open .rail-text,
    body.has-rail.rail-open .rail-badge,
    body.has-rail.rail-open .rail-group-label,
    body.has-rail.rail-open .rail-substep-label,
    body.has-rail.rail-open .rail-card-sub { display: revert; }
    body.has-rail.rail-open .rail-substep[data-step="view"] {
      margin-left: 6px; padding-left: 8px;
      border-left: 2px solid var(--border);
    }
    body.has-rail.rail-open .rail-substep[data-step="view"]::before {
      display: block;
    }
    body.has-rail.rail-open .density-pip { display: inline-flex; }
    body.has-rail.rail-open .rail .mode-btn,
    body.has-rail.rail-open .rail .tab,
    body.has-rail.rail-open .rail-footer-btn {
      justify-content: flex-start;
      padding: var(--space-2) var(--space-3);
    }
    .rail-hamburger { display: flex; }
    .app-topbar { padding-left: var(--space-3); padding-right: var(--space-3); }
    .app-content { padding: var(--space-3); }
  }

/* ============================================================
   MOBILE UX PASS - patch-mobile-css.py
   ============================================================ */
@media (max-width: 640px) {
  /* Stats strip collapses to 2 col instead of 5-7 */
  .stats-grid,
  .perf-stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 6px !important;
  }
  .stat-tile {
    padding: 8px 10px !important;
  }
  .stat-tile .stat-value {
    font-size: 14px !important;
  }
  .stat-tile .stat-label {
    font-size: 9.5px !important;
  }

  /* Detail modal: full-screen, sticky close */
  .detail-overlay.active .detail-modal {
    max-width: 100vw !important;
    max-height: 100vh !important;
    margin: 0 !important;
    border-radius: 0 !important;
  }
  .detail-header {
    position: sticky; top: 0; z-index: 2;
    background: var(--bg-1) !important;
    border-bottom: 1px solid var(--border);
  }
  .detail-close {
    width: 44px !important; height: 44px !important;
    font-size: 22px !important;
  }

  /* Portfolio table -> stacked cards */
  #pf-table {
    display: block;
  }
  #pf-table thead { display: none; }
  #pf-table tbody, #pf-table tr, #pf-table td { display: block; }
  #pf-table tr {
    background: var(--bg-1);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }
  #pf-table tr:hover td { background: transparent !important; }
  #pf-table td {
    padding: 4px 0 !important;
    border-bottom: 0 !important;
    text-align: left !important;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
  }
  #pf-table td::before {
    content: attr(class);
    color: var(--text-muted);
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-right: 12px;
    /* Hide class names that aren't useful */
  }
  #pf-table td.pf-col-ticker  { background: transparent !important; }
  #pf-table td.pf-col-ticker::before  { content: "TICKER";  }
  #pf-table td.pf-col-name::before    { content: "NAME";    }
  #pf-table td.pf-col-sector::before  { content: "SECTOR";  }
  #pf-table td.pf-col-account::before { content: "ACCOUNT"; }
  #pf-table td.pf-col-shares::before  { content: "SHARES";  }
  #pf-table td.pf-col-cost::before    { content: "COST";    }
  #pf-table td.pf-col-costval::before { content: "COST VAL";}
  #pf-table td.pf-col-price::before   { content: "PRICE";   }
  #pf-table td.pf-col-value::before   { content: "VALUE";   }
  #pf-table td.pf-col-pnl::before     { content: "P&L";     }
  #pf-table td.pf-col-pnlpct::before  { content: "P&L %";   }
  #pf-table td.pf-col-daysheld::before{ content: "DAYS";    }
  #pf-table td.pf-col-dte::before     { content: "DTE";     }
  #pf-table td.pf-col-iv::before      { content: "IV";      }
  #pf-table td.pf-col-delta::before   { content: "\u0394"; }
  #pf-table td.pf-col-theta::before   { content: "\u0398/day"; }
  #pf-table td.pf-col-actions {
    justify-content: flex-end !important;
    padding-top: 8px !important;
    margin-top: 6px !important;
    border-top: 1px solid var(--border) !important;
  }
  #pf-table td.pf-col-actions::before { content: ""; }

  /* Bigger tap targets on row icon buttons */
  .icon-btn {
    min-width: 36px !important;
    min-height: 36px !important;
    padding: 6px 10px !important;
    font-size: 14px !important;
  }

  /* Rail collapses to bottom tab strip */
  .rail {
    position: fixed; bottom: 0; left: 0; right: 0;
    top: auto !important;
    width: 100vw !important;
    height: auto !important;
    flex-direction: row !important;
    overflow-x: auto;
    z-index: 50;
    border-top: 1px solid var(--border);
    background: var(--bg-1);
  }
  .rail-header { display: none; }
  .rail-footer { display: none; }
  .rail .mode-btn,
  .rail .tab {
    flex-direction: column;
    gap: 2px !important;
    font-size: 10px !important;
    padding: 6px 8px !important;
    min-width: 64px;
  }
  .app-main {
    padding-bottom: 70px; /* leave room for fixed bottom tab strip */
  }

  /* Add bar: stack vertically on phone */
  .add-bar {
    flex-direction: column !important;
    width: 100% !important;
    gap: 6px !important;
  }
  .add-bar input {
    width: 100% !important;
    min-width: 0 !important;
  }
}

/* Tablet 641-1023 */
@media (max-width: 1023px) and (min-width: 641px) {
  .stats-grid,
  .perf-stats-grid {
    grid-template-columns: repeat(3, 1fr) !important;
  }
}
/* ============================================================ */

/* Alerts modal styles */
.alert-row {
  display: grid;
  grid-template-columns: 1fr 1.4fr 1.6fr auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--surface-2, rgba(255,255,255,0.04));
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 4px;
  font-size: 12px;
}
.alert-ticker {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  color: var(--accent-2);
}
.alert-cond { color: var(--text); }
.alert-status { color: var(--text-muted); font-size: 11px; }

/* ============================================================
   MOBILE UX PASS V2 - patch-mobile-css-v2.py
   ============================================================ */
@media (max-width: 640px) {

  /* ---- Fix Greek-letter labels (was rendering as "U0394") ---- */
  #pf-table td.pf-col-delta::before   { content: "DELTA" !important; }
  #pf-table td.pf-col-theta::before   { content: "THETA/DAY" !important; }

  /* ---- Top-bar reorganization ----
     Old: [☰] [breadcrumb] [search box (huge)] [refresh hidden]
     New: row 1 = [☰] [breadcrumb] [refresh]
          row 2 = [search box full-width]
  */
  .app-topbar {
    flex-wrap: wrap !important;
    padding: 8px 10px !important;
    gap: 6px !important;
    min-height: auto !important;
  }
  .topbar-breadcrumb {
    font-size: 12px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    flex: 1 1 auto !important;
    min-width: 0 !important;
  }
  .topbar-breadcrumb b {
    font-size: 12px !important;
  }
  .rail-hamburger {
    flex-shrink: 0 !important;
    width: 36px !important; height: 36px !important;
    font-size: 18px !important;
  }
  .add-bar {
    order: 99;          /* push to end of flex row -> wraps below */
    width: 100% !important;
    margin-top: 4px !important;
    padding: 4px !important;
  }
  .add-bar input {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    font-size: 13px !important;
    padding: 8px 10px !important;
  }
  .add-bar .btn {
    flex-shrink: 0 !important;
    padding: 8px 14px !important;
  }
  #refresh-mode-btn,
  #run-screener-btn {
    flex-shrink: 0 !important;
    padding: 6px 10px !important;
    font-size: 12px !important;
  }
  /* Don't hide refresh on mobile */
  #refresh-mode-btn { display: inline-flex !important; }

  /* ---- Watchlist (non-portfolio) data tables: visible scroll hint ---- */
  .scroll-x {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }
  .scroll-x::after {
    /* fade-out right edge as scroll affordance */
    content: "";
    position: sticky; right: 0; top: 0;
    width: 20px; height: 100%;
    background: linear-gradient(to right, transparent, var(--bg-0));
    pointer-events: none;
  }
  /* Compact data-table cells on mobile to fit more */
  .scroll-x .data-table th,
  .scroll-x .data-table td {
    padding: 6px 8px !important;
    font-size: 11px !important;
  }
  .scroll-x .data-table th {
    font-size: 9.5px !important;
  }
  /* Portfolio table cards: keep what we had, just tighten spacing */
  #pf-table tr {
    padding: 8px 10px !important;
    margin-bottom: 6px !important;
  }
  #pf-table td {
    padding: 3px 0 !important;
    font-size: 12px !important;
  }
  #pf-table td::before {
    font-size: 10px !important;
    flex-shrink: 0 !important;
    min-width: 70px !important;
  }

  /* ---- Detail chart: shrink legend so chart isn't hidden ---- */
  .detail-modal .detail-header h2 { font-size: 18px !important; }
  .detail-modal .detail-header .name { font-size: 11px !important; }
  .detail-modal .price-block .price { font-size: 16px !important; }
  .detail-modal .price-block .change { font-size: 11px !important; }
  .detail-modal .period-bar {
    flex-wrap: wrap !important;
    gap: 4px !important;
  }
  .detail-modal .period-bar .period-btn {
    padding: 4px 8px !important;
    font-size: 11px !important;
    min-width: 36px !important;
  }
  #detail-chart .legend text,
  #pf-history-chart .legend text,
  #price-chart .legend text {
    font-size: 9px !important;
  }

  /* ---- Section headers: remove orphan-text overlap ---- */
  .section-header {
    flex-wrap: wrap !important;
    gap: 4px !important;
    margin-top: 12px !important;
    margin-bottom: 6px !important;
  }
  .section-header h2 {
    font-size: 15px !important;
    margin: 0 !important;
  }
  .section-header .sub {
    font-size: 10.5px !important;
    flex-basis: 100% !important;
    margin-left: 0 !important;
    color: var(--text-muted) !important;
  }

  /* ---- Tiny: number-format tightening ---- */
  .stat-tile .stat-value {
    font-size: 15px !important;
    word-break: break-word;
  }
}
/* ============================================================ */

/* Rev/EPS modal - inherits .detail-overlay/.detail-modal/.opt-* styles */
#reveps-overlay .price-block .price {
  font-size: 11px !important;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.tk.is-rev-eps-link {
  cursor: pointer;
  text-decoration: underline dotted;
  text-decoration-color: rgba(99,102,241,0.4);
  text-underline-offset: 3px;
}
.tk.is-rev-eps-link:hover {
  color: var(--accent) !important;
}

.reveps-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 18px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.reveps-tab {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  padding: 6px 14px;
  font-size: 12.5px;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  transition: background 120ms, color 120ms;
}
.reveps-tab:hover {
  background: rgba(99, 102, 241, 0.10);
  color: var(--text);
}
.reveps-tab.is-active {
  background: rgba(99, 102, 241, 0.20);
  color: var(--accent-2);
  border-color: rgba(99, 102, 241, 0.4);
}

/* ============ Alerts modal — polished ============ */
.alerts-modal { padding: 20px 22px 22px; }
.alerts-header {
  display: flex; justify-content: space-between; align-items: center;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
  margin-bottom: 12px;
}
.alerts-header .detail-close {
  width: 32px; height: 32px; font-size: 16px;
  background: transparent; border: 0; color: var(--text-muted);
  cursor: pointer; border-radius: 6px;
}
.alerts-header .detail-close:hover {
  background: rgba(99,102,241,0.15); color: var(--text);
}
.alerts-intro {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.45;
  margin-bottom: 14px;
}

.alerts-perm-warn {
  display: flex; align-items: center; gap: 12px;
  background: rgba(245,158,11,.10);
  border: 1px solid rgba(245,158,11,.35);
  padding: 12px 14px;
  border-radius: 10px;
  margin-bottom: 16px;
}

.alerts-section {
  margin-top: 16px;
}
.alerts-section h4 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin: 0 0 8px;
  font-weight: 700;
}

#alerts-list .alert-row {
  display: grid;
  grid-template-columns: 1fr 1.5fr 1.4fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface-2, rgba(255,255,255,0.04));
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 6px;
  font-size: 12.5px;
  transition: background 100ms;
}
#alerts-list .alert-row:hover { background: rgba(99,102,241,0.08); }
#alerts-list .alert-ticker {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700; color: var(--accent-2); font-size: 13px;
}
#alerts-list .alert-cond { color: var(--text); }
#alerts-list .alert-status {
  color: var(--text-muted); font-size: 11px;
}
#alerts-list .icon-btn.danger {
  width: 28px; height: 28px;
  font-size: 12px;
  padding: 0;
  background: transparent; border: 0;
  color: var(--text-muted); cursor: pointer;
  border-radius: 6px;
}
#alerts-list .icon-btn.danger:hover {
  background: rgba(220,38,38,0.15); color: #dc2626;
}
#alerts-list > div.muted {
  text-align: center;
  padding: 22px 12px;
  background: var(--surface-2, rgba(255,255,255,0.03));
  border: 1px dashed var(--border);
  border-radius: 10px;
  color: var(--text-muted);
  font-size: 12.5px;
}

.alerts-add-form {
  background: var(--surface-2, rgba(255,255,255,0.03));
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.alerts-add-form label {
  display: block;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 4px;
  font-weight: 600;
}
.alerts-add-form .input,
.alerts-add-form .select {
  font-size: 13px;
  padding: 8px 10px;
}
.alerts-add-row { margin-bottom: 10px; }
.alerts-add-row-condition {
  display: flex; gap: 10px; align-items: flex-end;
}
.alerts-add-row-condition .btn {
  height: 36px;
  padding: 0 18px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Mobile: stack condition row */
@media (max-width: 640px) {
  .alerts-add-row-condition { flex-direction: column; align-items: stretch; }
  .alerts-add-row-condition .btn { width: 100%; }
  #alerts-list .alert-row {
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
  }
  #alerts-list .alert-ticker { grid-column: 1; grid-row: 1; }
  #alerts-list .icon-btn.danger { grid-column: 2; grid-row: 1; }
  #alerts-list .alert-cond { grid-column: 1 / -1; grid-row: 2; }
  #alerts-list .alert-status { grid-column: 1 / -1; grid-row: 3;
    font-size: 10.5px; padding-top: 2px; }
}

/* News feed modal */
.news-list { display: flex; flex-direction: column; gap: 8px; }
.news-item {
  display: flex; gap: 12px;
  padding: 12px 14px;
  background: var(--surface-2, rgba(255,255,255,0.03));
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: background 100ms;
  text-decoration: none; color: inherit;
}
.news-item:hover {
  background: rgba(99,102,241,0.08);
  border-color: rgba(99,102,241,0.35);
}
.news-thumb {
  width: 96px; height: 72px; flex-shrink: 0;
  background: var(--bg-1);
  border-radius: 6px;
  object-fit: cover;
}
.news-body { flex: 1; min-width: 0; }
.news-title {
  font-size: 13.5px; font-weight: 600; color: var(--text);
  margin-bottom: 4px; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.news-meta {
  font-size: 11px; color: var(--text-muted);
  display: flex; gap: 8px; align-items: center;
}
.news-meta .publisher { font-weight: 600; color: var(--accent-2); }
.news-summary {
  font-size: 12px; color: var(--text-muted);
  margin-top: 6px; line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}

/* Position notes popover */
.note-popover {
  position: fixed;
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  max-width: 400px;
  max-height: 300px;
  overflow-y: auto;
  box-shadow: 0 12px 32px rgba(0,0,0,0.5);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text);
  z-index: 5000;
}
.note-popover h1, .note-popover h2, .note-popover h3 {
  font-size: 14px; margin: 4px 0 6px; color: var(--accent-2);
}
.note-popover p { margin: 4px 0; }
.note-popover code {
  background: rgba(255,255,255,0.06);
  padding: 1px 4px; border-radius: 3px;
  font-family: 'JetBrains Mono', monospace; font-size: 11.5px;
}
.note-popover pre {
  background: rgba(0,0,0,0.3);
  padding: 8px 10px; border-radius: 6px;
  overflow-x: auto; margin: 6px 0;
}
.note-popover ul, .note-popover ol { margin: 4px 0; padding-left: 20px; }
.note-popover a { color: var(--accent-2); text-decoration: underline; }
.note-popover a:hover { color: var(--accent); }
.note-popover-close {
  position: absolute; top: 4px; right: 6px;
  background: transparent; border: 0; color: var(--text-muted);
  font-size: 14px; cursor: pointer; padding: 2px 6px;
}
.note-icon {
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
  border-radius: 4px;
}
.note-icon:hover { background: rgba(99,102,241,0.15); }

/* Concentration warnings banner */
.concentration-banner {
  background: linear-gradient(90deg,
    rgba(245,158,11,0.10),
    rgba(245,158,11,0.04));
  border: 1px solid rgba(245,158,11,0.35);
  border-left: 3px solid #f59e0b;
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}
.concentration-banner .icon {
  font-size: 18px;
  flex-shrink: 0;
}
.concentration-banner .body {
  flex: 1;
  font-size: 12.5px;
  color: var(--text);
  line-height: 1.45;
}
.concentration-banner .body b {
  color: #f59e0b;
}
.concentration-banner .dismiss {
  background: transparent; border: 0; color: var(--text-muted);
  cursor: pointer; padding: 2px 8px; border-radius: 4px;
  font-size: 12px;
}
.concentration-banner .dismiss:hover {
  background: rgba(255,255,255,0.06); color: var(--text);
}

/* News overlay must stack ABOVE the detail modal that opened it */
#news-overlay { z-index: 6000 !important; }


/* Attribution panel */
.attr-list { display: flex; flex-direction: column; gap: 6px; }
.attr-row {
  display: grid;
  grid-template-columns: 60px 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.03);
  font-size: 12.5px;
}
.attr-row:hover { background: rgba(255, 255, 255, 0.06); cursor: pointer; }
.attr-tk { font-weight: 600; color: var(--accent, #a78bfa); }
.attr-name { opacity: 0.75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attr-pnl { text-align: right; font-variant-numeric: tabular-nums; }
.attr-pnl.pos { color: #4ade80; }
.attr-pnl.neg { color: #f87171; }
.attr-share {
  text-align: right;
  font-size: 11px;
  opacity: 0.65;
  min-width: 42px;
  font-variant-numeric: tabular-nums;
}


/* Advanced Analysis tab */
.adv-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  padding: 14px 16px;
}
.adv-card-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.adv-card-hint {
  font-weight: 400;
  opacity: 0.55;
  font-size: 11px;
}
.adv-pair-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  padding: 5px 8px;
  border-radius: 4px;
  font-size: 12.5px;
  background: transparent;
  cursor: pointer;
}
.adv-pair-row:hover { background: rgba(255, 255, 255, 0.05); }
.adv-pair-tks { color: var(--accent, #a78bfa); font-weight: 600; }
.adv-pair-corr { font-variant-numeric: tabular-nums; }
.adv-pair-corr.pos { color: #4ade80; }
.adv-pair-corr.neg { color: #f87171; }
.adv-pair-lag {
  font-size: 11px;
  opacity: 0.65;
  font-variant-numeric: tabular-nums;
  min-width: 42px;
  text-align: right;
}
/* Correlation heatmap cells */
.adv-heatmap {
  display: inline-grid;
  gap: 1px;
  font-size: 10px;
}
.adv-heatmap-cell {
  width: 34px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
  font-variant-numeric: tabular-nums;
}
.adv-heatmap-head {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 10px;
  padding: 2px 0;
  font-weight: 600;
  color: var(--accent, #a78bfa);
}
.adv-heatmap-rowhead {
  padding: 0 6px;
  font-size: 10px;
  font-weight: 600;
  color: var(--accent, #a78bfa);
  text-align: right;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}


/* Advanced Analysis panel: fixed overlay over the main content area.
   Sidebar is ~240px wide; leave that free on the left. */
#tab-advanced {
  position: fixed !important;
  left: 240px;
  top: 0;
  right: 0;
  bottom: 0;
  width: auto !important;
  height: auto !important;
  overflow: auto;
  background: var(--bg, #0f1218);
  z-index: 100;
}
/* On narrow screens (mobile), sidebar collapses so use full width */
@media (max-width: 900px) {
  #tab-advanced { left: 0; }
}


/* =========================================================
   MARKETS FILTER POPOVER - polished styling
   Overrides existing .markets-panel defaults.
   ========================================================= */
.markets-panel {
  min-width: 320px !important;
  padding: 8px 8px !important;
  border-radius: 10px !important;
  background: rgba(22, 22, 30, 0.98) !important;
  border: 1px solid rgba(255, 255, 255, 0.1) !important;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5) !important;
}

/* Rows in the market list */
#markets-list {
  max-height: 380px;
  overflow-y: auto;
  padding-right: 4px;
}
#markets-list > * {
  display: grid !important;
  grid-template-columns: 22px 1fr auto !important;
  gap: 10px !important;
  align-items: center !important;
  padding: 4px 8px !important;
  border-radius: 5px !important;
  font-size: 13px !important;
  cursor: pointer !important;
  transition: background 0.12s ease;
  margin: 1px 0 !important;
}
#markets-list > *:hover {
  background: rgba(255, 255, 255, 0.05) !important;
}
/* Selected rows - subtle background only, NO outline (was creating a
   box that intersected the flag emoji at row edges). */
#markets-list > *:has(input[type="checkbox"]:checked) {
  background: rgba(99, 102, 241, 0.18) !important;
  border-left: 2px solid #6366f1 !important;
  padding-left: 6px !important;
}

/* Custom checkbox - constrained so it doesn't stretch to fill grid cell */
#markets-list input[type="checkbox"] {
  appearance: none !important;
  -webkit-appearance: none !important;
  width: 16px !important;
  height: 16px !important;
  min-width: 16px !important;
  max-width: 16px !important;
  min-height: 16px !important;
  max-height: 16px !important;
  flex: 0 0 16px !important;
  justify-self: start !important;
  align-self: center !important;
  border: 1.5px solid rgba(255, 255, 255, 0.3) !important;
  border-radius: 3px !important;
  background: transparent !important;
  cursor: pointer !important;
  position: relative !important;
  transition: all 0.15s ease;
  margin: 0 !important;
  padding: 0 !important;
  box-sizing: border-box !important;
}
#markets-list input[type="checkbox"]:hover {
  border-color: rgba(99, 102, 241, 0.7) !important;
}
#markets-list input[type="checkbox"]:checked {
  background: #6366f1 !important;
  border-color: #6366f1 !important;
}
#markets-list input[type="checkbox"]:checked::after {
  content: "" !important;
  position: absolute !important;
  left: 4px !important;
  top: 0px !important;
  width: 5px !important;
  height: 10px !important;
  border: solid #fff !important;
  border-width: 0 2px 2px 0 !important;
  transform: rotate(45deg) !important;
  background: transparent !important;
}

/* Flag emojis + labels: consistent line-height */
#markets-list > * > span,
#markets-list > * > label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  line-height: 1.3;
  color: var(--text, #e5e7eb);
}

/* Count badge (right-aligned) */
#markets-list > * > *:last-child {
  color: var(--text-muted, rgba(255,255,255,0.5));
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  font-weight: 500;
  min-width: 32px;
  text-align: right;
}

/* Action buttons row (Clear / Apply) */
.markets-panel > div:last-child,
.markets-panel .actions {
  padding-top: 10px !important;
  margin-top: 8px !important;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
#markets-clear {
  padding: 6px 14px !important;
  background: transparent !important;
  color: var(--text-muted, rgba(255,255,255,0.6)) !important;
  border: 1px solid rgba(255, 255, 255, 0.15) !important;
  border-radius: 6px !important;
  cursor: pointer !important;
  font-size: 12px !important;
  transition: all 0.12s ease;
}
#markets-clear:hover {
  color: var(--text, #fff) !important;
  border-color: rgba(255, 255, 255, 0.3) !important;
}
#markets-apply {
  padding: 6px 18px !important;
  background: #6366f1 !important;
  color: #fff !important;
  border: none !important;
  border-radius: 6px !important;
  cursor: pointer !important;
  font-size: 12px !important;
  font-weight: 600 !important;
  transition: background 0.12s ease;
}
#markets-apply:hover {
  background: #4f46e5 !important;
}

/* Scrollbar polish */
#markets-list::-webkit-scrollbar {
  width: 6px;
}
#markets-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 3px;
}
#markets-list::-webkit-scrollbar-track {
  background: transparent;
}
