﻿/* ============================================================
   CapitalPilot AI — Mobile layer (iPhone 15: 393x852pt, Dynamic Island,
   Home Indicator, iOS Safari quirks)

   Breakpoint contract (mirrors Tailwind's `md`):
     phone   : max-width 767.98px   -> everything in this file
     desktop : min-width 768px      -> untouched (styles.css rules only)

   Loaded AFTER styles.css. Every rule that changes layout lives inside the
   phone media query; the only rules outside it hide phone-only chrome on
   desktop, so the existing desktop layout is preserved exactly.
   ============================================================ */

/* ----- Tokens ----- */
:root {
  --mobile-chrome-bg: #0b1120;      /* top bar / bottom tabs */
  --mobile-sheet-bg: #0b1120;       /* bottom sheets (dark navy) */
  --mobile-topbar-h: 52px;
  --mobile-tab-h: 56px;
  --mobile-sheet-radius: 16px;      /* rounded-t-2xl */
}

[data-theme="light"] {
  --mobile-chrome-bg: #ffffff;
  --mobile-sheet-bg: #ffffff;
}

@keyframes mobileSheetUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

/* ----- Phone-only chrome is invisible by default (desktop) ----- */
.mobile-topbar,
.mobile-bottom-nav,
.mobile-sheet,
.mobile-sheet-backdrop {
  display: none;
}

.mobile-sheet-backdrop[hidden] {
  display: none !important;
}

/* `md:hidden` / `hidden md:block` helpers (>= 768px) */
@media (min-width: 768px) {
  .mobile-only {
    display: none !important;
  }
}

/* ============================================================
   PHONE  (< 768px)
   ============================================================ */
@media (max-width: 767.98px) {

  /* `hidden md:block` */
  .desktop-only {
    display: none !important;
  }

  /* ----- Touch targets: every button-like control is at least 44px tall -- */
  .btn.btn-xs,
  .btn.btn-sm {
    min-height: 44px;
  }

  /* ----- iOS Safari quirks -------------------------------------------- */
  html {
    -webkit-tap-highlight-color: transparent;
  }

  a, button, input, select, textarea, label, summary,
  [role="button"], .m-card {
    touch-action: manipulation; /* no double-tap zoom / 300ms delay */
  }

  body.mobile-sheet-open {
    overflow: hidden;
  }

  /*
   * Stop Safari auto-zoom on focus: any form control under 16px triggers it.
   * (text-base md:text-sm) -> 16px here, desktop keeps its own sizes.
   */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="file"]):not([type="color"]):not([type="hidden"]),
  select,
  textarea,
  .input,
  .select,
  .textarea,
  .search-container .search-input {
    font-size: 16px !important;
  }

  input[type="text"],
  input[type="number"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="date"],
  input[type="tel"],
  input[type="url"],
  select,
  .input,
  .select {
    min-height: 44px;
  }

  /* ----- Layout shell: sidebar out, sticky top bar + fixed bottom tabs - */
  .app-layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "header"
      "main";
    min-height: 100vh;
    min-height: 100dvh;
  }

  .sidebar {
    display: none; /* `hidden md:flex` — desktop sidebar is not used on phones */
  }

  /* Desktop header becomes a slim, non-sticky tool row (search · refresh · add). */
  .header {
    position: static;
    height: auto;
    min-height: 0;
    padding: 8px 12px;
    gap: 8px;
    background: transparent;
    border-bottom: none;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    width: 100% !important;
    box-sizing: border-box !important;
  }

  .header-left,
  .header-left #searchbar-container,
  #searchbar-container {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    max-width: none !important;
    width: 100% !important;
    display: flex !important;
    align-items: center !important;
  }

  .header-search-trigger {
    width: 100% !important;
    min-height: 44px !important;
    height: 44px !important;
    padding: 0 12px !important;
    gap: 8px !important;
    background: #161f33 !important;
    border: 1px solid rgba(148, 163, 184, 0.25) !important;
    border-radius: 8px !important;
    display: flex !important;
    align-items: center !important;
    box-sizing: border-box !important;
    cursor: pointer !important;
    touch-action: manipulation;
    transition: all 0.2s ease;
  }

  .header-search-trigger:hover,
  .header-search-trigger:focus-visible {
    border-color: var(--accent, #38bdf8) !important;
    background: #1e293b !important;
    box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.2) !important;
    outline: none !important;
  }

  .header-search-trigger .search-icon {
    font-size: 15px !important;
    line-height: 1 !important;
    flex-shrink: 0 !important;
  }

  .header-search-placeholder {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    font-size: 0.88rem !important;
    color: #94a3b8 !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
    line-height: 1.2 !important;
  }

  .header-search-kbd {
    display: none !important;
  }

  .header-right {
    flex: 0 0 auto !important;
    flex-shrink: 0 !important;
    background: transparent !important;
    padding-left: 0 !important;
    display: flex !important;
    align-items: center !important;
    gap: 6px !important;
  }

  /* Items that moved into the mobile top bar / More sheet */
  #mobile-menu-btn,
  .market-status-indicator,
  #navbar-regime-container,
  #navbar-privacy-container,
  #header-logout-btn,
  #header-context-help-btn,
  .last-refreshed-badge,
  .active-portfolio-badge,
  .header-btn-label,
  .header-toolbar-divider,
  .ai-cmd-trigger-btn {
    display: none !important;
  }

  #manual-refresh-btn,
  #header-add-stock-btn,
  #header-guest-reset-sandbox {
    min-width: 44px !important;
    min-height: 44px !important;
    height: 44px !important;
    padding: 0 12px !important;
    font-size: 1.05rem !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    box-sizing: border-box !important;
    border-radius: 8px !important;
  }

  .main-content {
    padding: 8px 16px;
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    /* clear the fixed bottom tab bar + Home Indicator */
    padding-bottom: calc(var(--mobile-tab-h) + env(safe-area-inset-bottom) + 20px);
    min-height: 0;
  }

  /* ----- Sticky compact top bar ---------------------------------------- */
  .mobile-topbar {
    display: block;
    position: sticky;
    top: 0;
    z-index: 900;
    background: var(--mobile-chrome-bg);
    border-bottom: 1px solid var(--border-color);
    /* Dynamic Island / status bar (viewport-fit=cover + black-translucent) */
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    /* NOTE: no backdrop-filter here — it would trap the fixed regime popover */
  }

  .mobile-topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: var(--mobile-topbar-h);
    padding: 0 8px 0 14px;
  }

  .mobile-topbar-brand {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
  }

  .mobile-topbar-logo {
    font-size: 1.25rem;
    line-height: 1;
  }

  .mobile-topbar-title {
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: -0.01em;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-topbar-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }

  .mobile-topbar-slot {
    position: relative;
    display: inline-flex;
  }

  .mobile-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--bg-hover);
    color: var(--accent);
    font-family: var(--font-family);
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
  }

  .mobile-icon-btn:active {
    transform: scale(0.94);
  }

  /* Regime pill -> compact colored icon button (details stay in the popover) */
  .mobile-topbar .market-regime-pill {
    min-width: 44px;
    min-height: 44px;
    padding: 0 10px;
    justify-content: center;
    gap: 0;
  }

  .mobile-topbar .regime-text {
    font-size: 0;
    display: inline-flex;
    align-items: center;
  }

  .mobile-topbar .regime-pill-icon {
    font-size: 1.2rem;
  }

  .mobile-topbar .regime-pill-title,
  .mobile-topbar .regime-pill-sep,
  .mobile-topbar .regime-pill-vix,
  .mobile-topbar .regime-dot {
    display: none;
  }

  .mobile-topbar .market-regime-pill.regime-pill-loading::after {
    content: "\1F6A6"; /* traffic light while loading */
    font-size: 1.2rem;
  }

  /* Popover spans the screen under the top bar instead of a 340px absolute box */
  .mobile-topbar .market-regime-popover {
    position: fixed;
    top: calc(env(safe-area-inset-top) + var(--mobile-topbar-h) + 8px);
    left: 12px;
    right: 12px;
    width: auto;
    max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - var(--mobile-topbar-h) - var(--mobile-tab-h) - 24px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  /* Privacy toggle -> icon-only (Eye / Lock) */
  .mobile-topbar .privacy-toggle-pill {
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    justify-content: center;
    gap: 0;
    border-radius: 50%;
  }

  .mobile-topbar .privacy-toggle-pill .privacy-text {
    display: none;
  }

  .mobile-topbar .privacy-toggle-pill .privacy-icon {
    font-size: 1.1rem;
    line-height: 1;
  }

  /* ----- Fixed bottom tab bar ------------------------------------------ */
  .mobile-bottom-nav {
    display: flex;
    align-items: stretch;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 950;
    background: var(--mobile-chrome-bg);
    border-top: 1px solid var(--border-color);
    /* Home Indicator */
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  .mobile-tab {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
    min-height: var(--mobile-tab-h);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 6px 2px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-family: var(--font-family);
    font-size: 0.68rem;
    font-weight: 600;
    line-height: 1.1;
    cursor: pointer;
    transition: color 0.15s ease;
  }

  .mobile-tab::before {
    content: "";
    position: absolute;
    top: 0;
    left: 28%;
    right: 28%;
    height: 2px;
    border-radius: 0 0 2px 2px;
    background: transparent;
    transition: background 0.15s ease;
  }

  .mobile-tab.active {
    color: var(--accent);
  }

  .mobile-tab.active::before {
    background: var(--accent);
  }

  .mobile-tab:active .mobile-tab-icon {
    transform: scale(0.9);
  }

  .mobile-tab-icon {
    position: relative;
    font-size: 1.3rem;
    line-height: 1;
    transition: transform 0.12s ease;
  }

  .mobile-tab-label {
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-tab-badge {
    position: absolute;
    top: -6px;
    right: -12px;
    min-width: 17px;
    height: 17px;
    padding: 0 5px;
    border-radius: 9px;
    background: var(--negative);
    color: #fff;
    font-size: 0.62rem;
    font-weight: 700;
    line-height: 17px;
    text-align: center;
  }

  .mobile-tab-badge[hidden] {
    display: none;
  }

  /* ----- "More" bottom sheet ------------------------------------------- */
  .mobile-sheet-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 1250;
    background: rgba(2, 6, 23, 0.68);
    opacity: 0;
    transition: opacity 0.25s ease;
  }

  .mobile-sheet-backdrop.open {
    opacity: 1;
  }

  .mobile-sheet {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1251;
    max-height: 88vh;
    max-height: 88dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    background: var(--mobile-sheet-bg);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: var(--mobile-sheet-radius) var(--mobile-sheet-radius) 0 0;
    box-shadow: 0 -16px 48px rgba(0, 0, 0, 0.6);
    padding-bottom: env(safe-area-inset-bottom);
    transform: translateY(100%);
    visibility: hidden;
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), visibility 0s linear 0.3s;
  }

  .mobile-sheet.open {
    transform: translateY(0);
    visibility: visible;
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), visibility 0s;
  }

  .mobile-sheet-handle {
    display: block;
    width: 100%;
    height: 28px;
    background: none;
    border: none;
    cursor: pointer;
    position: relative;
  }

  .mobile-sheet-handle::after {
    content: "";
    position: absolute;
    top: 10px;
    left: 50%;
    width: 40px;
    height: 5px;
    margin-left: -20px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.22);
  }

  .mobile-sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 8px 4px 20px;
  }

  .mobile-sheet-head h3 {
    margin: 0;
    font-size: 1.15rem;
  }

  .mobile-sheet-profile {
    padding: 8px 16px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  }

  .mobile-sheet-profile-label {
    display: block;
    margin-bottom: 6px;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-secondary);
  }

  .mobile-sheet-profile-row {
    display: flex;
    gap: 8px;
  }

  .mobile-sheet-profile-row .select {
    flex: 1;
    min-width: 0;
  }

  .mobile-sheet-profile-row .btn {
    min-width: 48px;
    min-height: 48px;
    font-size: 1.2rem;
  }

  .mobile-sheet-list {
    list-style: none;
    margin: 0;
    padding: 6px 8px 12px;
  }

  .mobile-sheet-item {
    display: flex;
    align-items: center;
    gap: 14px;
    width: 100%;
    min-height: 52px;
    padding: 0 12px;
    background: none;
    border: none;
    border-radius: 12px;
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: 1rem;
    font-weight: 500;
    text-align: left;
    cursor: pointer;
  }

  .mobile-sheet-item:active {
    background: rgba(255, 255, 255, 0.07);
  }

  .mobile-sheet-item-icon {
    width: 28px;
    text-align: center;
    font-size: 1.25rem;
    line-height: 1;
  }

  .mobile-sheet-item-label {
    flex: 1;
  }

  .mobile-sheet-item-chevron {
    color: var(--text-muted);
    font-size: 1.4rem;
    line-height: 1;
  }

  .mobile-sheet-item--danger {
    margin-top: 6px;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 0 0 12px 12px;
    color: var(--negative);
  }

  /* ----- Touch targets (>= 44px) --------------------------------------- */
  /* Catch-all for every button (dashboard text links, reset links, chips...) */
  button:not(.mobile-sheet-handle),
  [role="button"],
  summary {
    min-height: 44px;
  }

  .btn,
  .filter-btn,
  .scanner-preset-pill,
  .sector-pill,
  .scanner-scope-btn,
  .scanner-tf-btn,
  .stc-dir-btn,
  .stc-tab-btn,
  .tab-item,
  .guide-tab,
  .guide-lang-pill,
  .trade-filter-pill,
  .outcome-pill,
  .journal-tag-chip,
  .alert-mode-btn,
  .alert-filter-pill,
  .alert-cat-pill,
  .alert-action-chip,
  .ai-quick-chip,
  .ai-lang-toggle-btn,
  .ai-action-btn,
  .ai-provider-tab-btn,
  .target-toggle-btn,
  .cal-pill-btn,
  .pyr-target-r-btn,
  .btn-icon-toggle,
  .btn-toggle-mask,
  .ai-btn-thesis,
  .ai-sanity-btn,
  .ai-scanner-btn-ask,
  .modal-close,
  .close-modal-btn {
    min-height: 44px;
  }

  .filter-btn,
  .scanner-preset-pill,
  .sector-pill,
  .scanner-scope-btn,
  .scanner-tf-btn,
  .guide-lang-pill,
  .trade-filter-pill,
  .outcome-pill,
  .journal-tag-chip,
  .alert-mode-btn,
  .alert-filter-pill,
  .alert-cat-pill,
  .alert-action-chip,
  .ai-quick-chip,
  .target-toggle-btn,
  .cal-pill-btn,
  .tab-item,
  .guide-tab {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .page-help-btn,
  .page-help-header-btn {
    width: 44px;
    height: 44px;
  }

  .modal-close,
  .close-modal-btn {
    min-width: 44px;
  }

  /* Toggle switches: keep the 40x22 look, enlarge the tap area to >= 44px */
  .toggle::after {
    content: "";
    position: absolute;
    inset: -12px -6px;
  }

  /* ----- Sliders: bigger track + thumb, 44px hit-area ------------------ */
  input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    height: 44px;
    margin: 0;
    padding: 0;
    background: transparent;
    cursor: pointer;
    touch-action: pan-y; /* horizontal drags belong to the slider */
  }

  input[type="range"]::-webkit-slider-runnable-track {
    height: 6px;
    border-radius: 3px;
    background: var(--border-color);
  }

  input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 30px;
    height: 30px;
    margin-top: -12px; /* centre on the 6px track */
    border-radius: 50%;
    border: 3px solid #ffffff;
    background: var(--accent);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 0 10px rgba(59, 130, 246, 0.0);
    transition: box-shadow 0.15s ease;
  }

  input[type="range"]:active::-webkit-slider-thumb {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 0 10px rgba(59, 130, 246, 0.22);
  }

  input[type="range"]::-moz-range-track {
    height: 6px;
    border-radius: 3px;
    background: var(--border-color);
  }

  input[type="range"]::-moz-range-thumb {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 3px solid #ffffff;
    background: var(--accent);
  }

  /* SL Calculator: slider + number + % */
  .stc-risk-row {
    gap: 12px;
  }

  /* (higher specificity than `input[type="number"] { width: 100% }`) */
  .stc-risk-row .stc-risk-num {
    flex: 0 0 84px;
    width: 84px;
    text-align: center;
  }

  .stc-risk-row input[type="range"] {
    flex: 1 1 auto;
    min-width: 0;
  }

  /* Strategy Guide sandbox: label + value on one line, full-width slider below */
  .guide-sandbox-sliders label {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0 10px;
    margin-bottom: 6px;
    font-size: 0.9rem;
  }

  .guide-sandbox-sliders label > span {
    order: 2;
    font-weight: 600;
    font-family: var(--font-mono);
  }

  .guide-sandbox-sliders label > input[type="range"] {
    order: 3;
    flex: 1 0 100%;
  }

  /* ----- Slide-over drawers -> full-width bottom sheets ---------------- */
  .scanner-drawer,
  .quick-help-drawer {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: auto;
    max-height: 88vh;
    max-height: 88dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    background: var(--mobile-sheet-bg);
    border-left: none;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: var(--mobile-sheet-radius) var(--mobile-sheet-radius) 0 0;
    box-shadow: 0 -16px 48px rgba(0, 0, 0, 0.6);
    padding-bottom: env(safe-area-inset-bottom);
    animation: mobileSheetUp 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  }

  /* grab handle */
  .scanner-drawer::before,
  .quick-help-drawer::before {
    content: "";
    display: block;
    flex-shrink: 0;
    width: 40px;
    height: 5px;
    margin: 10px auto 2px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.22);
  }

  /* keep the close button reachable while scrolling */
  .scanner-drawer-head,
  .quick-help-drawer-head {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--mobile-sheet-bg);
  }

  .scanner-drawer-head .btn,
  .quick-help-drawer-head .btn {
    min-width: 44px;
    font-size: 1.4rem;
  }

  .scanner-drawer-body {
    padding: 16px;
  }

  /* EN / हिंदी toggle: shrink-wrap instead of stretching across the sheet */
  .quick-help-lang {
    align-self: flex-start;
    margin: 12px 18px 0;
    padding: 3px;
    gap: 2px;
  }

  .guide-lang-pill {
    min-width: 64px;
    padding: 0 16px;
  }

  /* Playbook & AI Command OS Guide Mobile (iPhone 15 393px) */
  .guide-tools-subgrid,
  .guide-safety-grid,
  .guide-workflow-grid,
  .guide-prompts-grid {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  .guide-prompt-actions {
    flex-direction: column !important;
  }

  .guide-prompt-run-btn,
  .guide-prompt-copy-btn {
    width: 100% !important;
    min-height: 44px !important;
  }

  /* ----- Centered modals -> bottom sheets ------------------------------ */
  .modal-overlay,
  #scanner-radar-modal.modal-overlay,
  .radar-modal-overlay {
    align-items: flex-end !important;
    justify-content: center !important;
    padding: 0 !important;
  }

  .modal-overlay .modal-content,
  .modal-overlay .modal-content.modal-sm,
  .modal-overlay .modal-content.modal-lg,
  .modal-overlay .modal-content.modal-xl,
  #scanner-radar-modal .radar-modal-card,
  .radar-modal-overlay .radar-modal-card {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 88vh !important;
    max-height: 88dvh !important;
    margin: 0 !important;
    border-radius: var(--mobile-sheet-radius) var(--mobile-sheet-radius) 0 0 !important;
    background: var(--mobile-sheet-bg) !important;
    padding: 16px !important;
    padding-bottom: calc(16px + env(safe-area-inset-bottom)) !important;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    animation: mobileSheetUp 0.3s cubic-bezier(0.32, 0.72, 0, 1) !important;
  }

  .modal-overlay .modal-content > .modal-header {
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--mobile-sheet-bg);
  }

  .modal-overlay .modal-footer {
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    /* Sticky action bar: Save / Cancel stay reachable while a long form scrolls inside the sheet */
    position: sticky;
    bottom: 0;
    z-index: 2;
    background: var(--mobile-sheet-bg);
    border-top: 1px solid var(--border-color);
  }

  .modal {
    max-width: 100%;
    margin: 0;
    border-radius: var(--mobile-sheet-radius) var(--mobile-sheet-radius) 0 0;
    max-height: 88vh;
    max-height: 88dvh;
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Trade Journal / Event Calendar dialogs (fixed + centered on desktop) */
  #journal-modal,
  .modal-dialog {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    transform: none;
    width: 100%;
    max-width: 100% !important; /* beats the inline max-width on #journal-modal */
    max-height: 88vh;
    max-height: 88dvh;
    border-radius: var(--mobile-sheet-radius) var(--mobile-sheet-radius) 0 0;
    background: var(--mobile-sheet-bg);
    padding: 18px 16px;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    animation: mobileSheetUp 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  }

  /* Toasts must clear the Dynamic Island */
  .toast-container {
    top: calc(env(safe-area-inset-top) + 8px);
  }

  /* ----- Mobile cards (tables -> stacked cards) ------------------------ */
  .m-card-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .m-card-empty {
    padding: 20px 8px;
    text-align: center;
  }

  .m-card {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
  }

  .scanner-card {
    cursor: pointer;
  }

  .scanner-card:active {
    background: var(--bg-hover);
  }

  .m-card-top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
  }

  .m-card-id {
    min-width: 0;
  }

  .m-card-symbol-row {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
  }

  .m-card-symbol {
    font-size: 1.1rem;
    line-height: 1.2;
  }

  .m-card-symbol-row .scanner-exch-badge {
    margin-left: 0;
  }

  .m-card-sub {
    margin-top: 2px;
    font-size: 0.74rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .m-card-price {
    flex-shrink: 0;
    text-align: right;
  }

  .m-card-ltp {
    font-size: 1.1rem;
    font-weight: 700;
    line-height: 1.2;
  }

  .m-card-chg {
    font-size: 0.82rem;
    font-weight: 600;
  }

  .m-card-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .m-card-badges:empty {
    display: none;
  }

  .m-badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 999px;
    border: 1px solid var(--border-color);
    background: var(--bg-hover);
    color: var(--text-secondary);
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1.4;
    white-space: nowrap;
  }

  .m-badge--rs,
  .m-badge--good {
    background: rgba(16, 185, 129, 0.16);
    border-color: rgba(16, 185, 129, 0.4);
    color: #34d399;
  }

  .m-badge--vcp,
  .m-badge--warn,
  .m-badge--fail {
    background: rgba(245, 158, 11, 0.16);
    border-color: rgba(245, 158, 11, 0.4);
    color: #fbbf24;
  }

  .m-badge--sl,
  .m-badge--bad {
    background: rgba(239, 68, 68, 0.16);
    border-color: rgba(239, 68, 68, 0.4);
    color: #f87171;
  }

  .m-badge--target,
  .m-badge--trend {
    background: rgba(59, 130, 246, 0.16);
    border-color: rgba(59, 130, 246, 0.4);
    color: #60a5fa;
  }

  .m-card-metrics {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
    margin: 0;
  }

  .m-card-metrics--3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .m-card-metrics > div {
    min-width: 0;
    padding: 6px 8px;
    border-radius: 8px;
    background: rgba(15, 23, 42, 0.45);
  }

  .m-card-metrics dt {
    font-size: 0.62rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  .m-card-metrics dd {
    margin: 0;
    font-size: 0.82rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* expandable "Details" panel (Portfolio / Backtester) */
  .m-card-details {
    display: none;
  }

  .m-card.is-expanded .m-card-details {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .m-card-action-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .m-card-action-grid .btn {
    width: 100%;
    min-height: 44px;
  }

  .scanner-watch-alert-btn {
    min-height: 44px;
  }

  /* odd button out (Delete) spans the full row instead of leaving a gap */
  .m-card-action-grid > .btn:last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  /* full-width tap action */
  .m-card-action {
    width: 100%;
    min-height: 48px;
    font-size: 0.95rem;
  }

  .m-card-action-row {
    display: grid;
    grid-template-columns: 1fr 48px;
    gap: 8px;
  }

  .m-card-action-icon {
    min-width: 48px;
    min-height: 48px;
    padding: 0;
  }

  .m-danger {
    color: var(--negative);
  }

  /* SL Calculator market-regime banner: stack text above a full-width action
     (desktop row layout squeezes the copy into a ~60px column on a phone) */
  .stc-regime-banner {
    flex-direction: column;
    align-items: stretch !important;
    gap: 10px;
  }

  .stc-regime-banner .btn {
    width: 100%;
    min-height: 44px;
    white-space: normal;
  }

  .stc-regime-banner .font-bold {
    flex-wrap: wrap;
  }

  /* ----- Filter rows & toolbars: horizontal scroll, never wrap-clip ----- */
  .filter-bar {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .filter-bar::-webkit-scrollbar {
    display: none;
  }

  .filter-bar .search-input {
    flex: 1 0 200px;
  }

  .journal-outcome-pills,
  .trade-filter-pills {
    overflow-x: auto;
    max-width: 100%;
    scrollbar-width: none;
  }

  .journal-outcome-pills::-webkit-scrollbar,
  .trade-filter-pills::-webkit-scrollbar {
    display: none;
  }

  /* ----- Mutual Funds Mobile Layout (iPhone 15 393px) ----- */
  .mf-desktop-table-card {
    display: none !important;
  }

  .mf-mobile-cards-grid {
    display: grid !important;
  }

  .mf-mobile-card {
    border-radius: var(--radius-md, 12px);
    background: var(--bg-card, #131d31);
    border: 1px solid rgba(255, 255, 255, 0.08);
  }

  .mf-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .mf-header-actions {
    width: 100%;
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .mf-header-actions .mf-header-btn {
    width: 100%;
    font-size: 0.75rem;
    padding: 0.5rem 0.6rem;
  }

  .mf-header-actions .mf-btn-label {
    display: none;
  }

  .mf-unified-header-card {
    padding: 1rem !important;
  }

  .mf-onboard-actions {
    grid-template-columns: 1fr !important;
  }

  .mf-import-modal {
    width: calc(100vw - 16px) !important;
    max-height: 92vh;
    overflow-y: auto;
  }

  /* ----- AI Command OS & Action Bar Mobile (iPhone 15 393px) ----- */
  .ai-cmd-trigger-btn {
    display: none !important;
  }

  .mobile-floating-ai-btn {
    display: flex !important;
    position: fixed;
    bottom: calc(4.5rem + env(safe-area-inset-bottom, 0px));
    right: 1rem;
    z-index: 35;
    min-height: 44px;
    padding: 0.625rem 1.1rem;
    border-radius: 9999px;
    background: linear-gradient(135deg, #3b82f6, #7c3aed);
    color: #ffffff;
    font-weight: 700;
    font-size: 0.875rem;
    box-shadow: 0 4px 18px rgba(79, 70, 229, 0.45);
    border: 1px solid rgba(255, 255, 255, 0.25);
    align-items: center;
    gap: 0.45rem;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
  }

  .mobile-floating-ai-btn:active {
    transform: scale(0.94);
  }

  /* Center AI Command OS modal on mobile instead of docking to bottom */
  .ai-cmd-sheet {
    top: 50% !important;
    bottom: auto !important;
    left: 50% !important;
    right: auto !important;
    width: calc(100vw - 24px) !important;
    max-width: 660px !important;
    max-height: min(88vh, calc(100dvh - 32px)) !important;
    max-height: min(88vh, calc(100vh - 32px)) !important;
    border-radius: 16px !important;
    border: 1px solid rgba(99, 102, 241, 0.35) !important;
    transform: translate(-50%, -50%) scale(0.98) !important;
    margin: 0 !important;
    box-shadow: 0 25px 60px -15px rgba(0, 0, 0, 0.85), 0 0 35px rgba(99, 102, 241, 0.18) !important;
  }

  .ai-cmd-sheet.open {
    display: flex !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    transform: translate(-50%, -50%) scale(1) !important;
  }

  .ai-cmd-drag-handle {
    display: none !important;
  }

  .dash-battle-plan-grid {
    grid-template-columns: 1fr !important;
  }

  .dash-battle-plan-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .dash-battle-ai-btn {
    width: 100%;
    justify-content: center;
  }
}

/* iPhone landscape / very small phones */
@media (max-width: 380px) {
  .mobile-tab-label {
    font-size: 0.64rem;
  }

  .m-card-metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* ============================================================
   UI AUDIT — touch targets, focus, loading states, safe wrapping
   (dark-navy theme untouched: layout / interaction rules only)
   ============================================================ */

/* Checkbox / radio rows: whole label row is the 44px tap target on phones & touch tablets */
@media (max-width: 1024px), (pointer: coarse) {
  .checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    padding-block: 6px;
    cursor: pointer;
  }

  .checkbox-label > input[type="checkbox"],
  .checkbox-label > input[type="radio"] {
    flex: 0 0 auto;
    width: 20px;
    height: 20px;
    margin: 0;
  }

  .checkbox-label > span {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .ai-cmd-close-btn,
  .toast-close-btn {
    min-width: 44px;
    min-height: 44px;
  }

  select,
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="hidden"]),
  textarea {
    min-height: 44px;
  }
}

/* Visible keyboard focus everywhere (WCAG 2.4.7) without changing the dark theme */
:where(button, a[href], input, select, textarea, summary, [role="button"], [role="tab"]):focus-visible {
  outline: 2px solid #38bdf8;
  outline-offset: 2px;
}

/* Async-action feedback: disabled / busy controls look inert and cannot be double-tapped */
.btn:disabled,
button:disabled,
[aria-busy="true"] {
  cursor: not-allowed;
  pointer-events: none;
}

button:disabled,
.btn:disabled {
  opacity: 0.6;
}

/* Long helper text, endpoints and channel-status strings must wrap instead of overflowing */
.settings-page,
.settings-page .card,
.form-group,
.form-row {
  min-width: 0;
}

.form-group p,
.form-group small,
.form-group code,
.form-group .text-secondary,
.text-secondary code {
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Range sliders (position-sizing sandbox): full-height touch area */
@media (max-width: 1024px), (pointer: coarse) {
  input[type="range"],
  .guide-sandbox-sliders input[type="range"] {
    min-height: 44px !important;
    height: 44px !important;
    margin-block: 0;
  }
}

/* AI-First Swing Trading Lab Backtester (iPhone 15: 393px) */
@media (max-width: 767.98px) {
  .backtester-page {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  .bt-preset-pills-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
  }

  .bt-preset-pill {
    flex: 1 1 calc(50% - 6px);
    font-size: 0.76rem;
    padding: 8px 10px;
    white-space: normal;
    text-align: center;
    line-height: 1.25;
    min-height: 44px !important;
  }

  .backtester-config-grid {
    grid-template-columns: 1fr !important;
    gap: 14px;
    margin-top: 10px;
  }

  .backtester-charts-grid {
    grid-template-columns: 1fr !important;
    gap: 12px;
  }

  .equity-chart-card,
  .r-dist-card {
    min-height: 320px;
    width: 100%;
    overflow: hidden;
  }

  .equity-chart-body {
    padding: 8px 6px;
    min-height: 220px;
    overflow: hidden;
  }

  .equity-tooltip {
    position: static;
    margin-top: 8px;
    width: 100%;
  }

  .bt-card {
    border-radius: 8px;
    margin-bottom: 10px;
  }
}

/* Tablet & touch (iPad 768/1024 and any coarse pointer): every button-like control reaches 44px */
@media (pointer: coarse), (max-width: 1024px) {
  button,
  [role="button"],
  [role="tab"],
  summary,
  select,
  .select,
  .header-search-trigger,
  textarea,
  input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="range"]) {
    min-height: 44px !important;
  }

  /* Icon-only buttons also get a 44px-wide target */
  .hamburger-btn,
  .page-help-btn,
  #header-context-help-btn,
  .modal-close {
    min-width: 44px;
  }

  /* Inline text-links rendered as buttons: keep the text inline but give them a real tap area */
  .dash-text-link,
  .dash-kpi-link,
  .pl-chip-btn,
  .pl-copy-btn,
  .mf-legend-pill,
  .cal-pill-btn {
    display: inline-flex;
    align-items: center;
  }

  /* Range sliders & drag handle */
  .mobile-sheet-handle {
    min-height: 44px !important;
  }
}
