/**
 * Mobile / phone web-browser overrides.
 *
 * Loaded AFTER style.css and layout.css so these rules win at the same
 * specificity. The shell pivots from "always-visible icon sidebar + hover
 * expand" (desktop) to "off-canvas drawer + hamburger top bar" (phones).
 *
 * Breakpoints used:
 *   ≤ 900px : tablets / phones — drawer pattern
 *   ≤ 480px : small phones — extra typography + spacing tightening
 */

/* ==========================================================================
   Mobile top bar (hamburger + brand). Hidden by default on desktop.
   ========================================================================== */
.mobile-topbar {
    display: none;
}

.mobile-sidebar-overlay {
    display: none;
}

/* ==========================================================================
   ≤ 900px: phones + small tablets — switch to off-canvas drawer
   ========================================================================== */
@media (max-width: 900px) {
    /* Show the top bar (overrides default display:none above) */
    .mobile-topbar {
        display: flex;
        align-items: center;
        gap: 12px;
        position: sticky;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1002;
        padding: 8px 14px;
        height: 56px;
        background: rgba(15, 23, 42, 0.92);
        backdrop-filter: blur(18px) saturate(180%);
        -webkit-backdrop-filter: blur(18px) saturate(180%);
        border-bottom: 1px solid var(--border-color);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
        box-sizing: border-box;
    }

    .mobile-topbar::after {
        content: '';
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        height: 2px;
        background: var(--rainbow-gradient);
        background-size: 200% 100%;
        animation: rainbowBorder 3s linear infinite;
        opacity: 0.75;
    }

    .mobile-topbar-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        padding: 0;
        border: 1px solid var(--border-color);
        border-radius: 10px;
        background: rgba(30, 41, 59, 0.7);
        color: var(--text-primary);
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        transition: background 0.2s ease, transform 0.2s ease;
        flex-shrink: 0;
    }

    .mobile-topbar-toggle:active {
        transform: scale(0.96);
        background: rgba(30, 41, 59, 0.9);
    }

    .mobile-topbar-toggle svg {
        display: block;
    }

    .mobile-topbar-brand {
        display: flex;
        align-items: center;
        gap: 8px;
        text-decoration: none;
        color: var(--text-primary);
        font-weight: 600;
        font-size: 0.95rem;
        letter-spacing: 0.01em;
        min-width: 0;
        flex: 1 1 auto;
    }

    .mobile-topbar-brand img {
        width: 28px;
        height: 28px;
        flex-shrink: 0;
        border-radius: 6px;
    }

    .mobile-topbar-title {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* App-layout switches from flex-row to stacked: top bar + main column.
       Sidebar floats above as a fixed drawer. */
    .app-layout {
        flex-direction: column;
    }

    /* --- Sidebar: off-canvas drawer --- */
    .sidebar,
    html[data-sidebar-mode="pinned"] .sidebar,
    html[data-sidebar-mode="icons"] .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        height: 100dvh;
        width: min(86vw, 320px) !important;
        max-width: 320px;
        z-index: 1100;
        transform: translateX(-100%);
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 6px 0 32px rgba(0, 0, 0, 0.5);
        background: rgba(15, 23, 42, 0.97);
        backdrop-filter: blur(22px) saturate(180%);
        -webkit-backdrop-filter: blur(22px) saturate(180%);
    }

    /* No hover-expand on touch: tapping should not toggle width */
    .sidebar:hover,
    html[data-sidebar-mode="pinned"] .sidebar:hover,
    html[data-sidebar-mode="icons"] .sidebar:hover {
        width: min(86vw, 320px) !important;
    }

    .sidebar.is-mobile-open {
        transform: translateX(0);
    }

    /* When drawer is open we always show full labels — overrides the
       "collapsed icon-only" rules from style.css. */
    .sidebar .sidebar-link-text,
    .sidebar:not(:hover) .sidebar-link-text {
        opacity: 1;
        width: auto;
        overflow: visible;
    }

    .sidebar .sidebar-link,
    .sidebar:not(:hover) .sidebar-link {
        justify-content: flex-start;
        padding: 14px 16px;
        min-height: 48px;
    }

    .sidebar:hover .logo-text,
    .logo-text {
        opacity: 1;
        max-height: none;
        width: auto;
    }

    .sidebar:hover .logo-icon,
    .logo-icon {
        width: 56px;
        height: 56px;
    }

    /* Backdrop scrim */
    .mobile-sidebar-overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(2, 6, 23, 0.6);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
        z-index: 1099;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.25s ease, visibility 0.25s ease;
    }

    .mobile-sidebar-overlay.is-active {
        opacity: 1;
        visibility: visible;
    }

    /* Lock background scroll while drawer is open */
    body.mobile-nav-open {
        overflow: hidden;
        touch-action: none;
    }

    /* Avoid the layout FAB overlapping the open drawer / its scrim */
    body.mobile-nav-open .layout-controls-fab,
    body.mobile-nav-open .layout-controls-panel {
        display: none !important;
    }

    /* --- Main column takes full viewport width --- */
    .main-wrapper {
        width: 100%;
        min-width: 0;
        margin-left: 0 !important;
    }

    .container {
        padding: 14px 12px;
        max-width: 100% !important;
    }

    /* Force single-column dashboard layout, regardless of saved preference */
    .main-content,
    html[data-content-columns="2"] .main-content {
        grid-template-columns: 1fr !important;
        gap: 16px;
    }

    /* Long horizontal pieces should wrap */
    .page-header,
    .page-header-content,
    .page-header-actions,
    .insights-controls,
    .charts-section-header {
        flex-wrap: wrap;
        gap: 10px;
    }

    .page-header {
        padding: 22px 16px;
    }

    /* --- Layout-controls FAB: shrink + sit clear of mobile UI --- */
    .layout-controls-fab {
        bottom: 16px;
        right: 12px;
        padding: 8px 10px;
        font-size: 0.72rem;
    }

    .layout-controls-fab-label {
        display: none; /* icon-only on small screens to save space */
    }

    .layout-controls-panel {
        right: 12px;
        left: 12px;
        width: auto;
        bottom: 72px;
        max-height: 70vh;
    }

    /* --- Charts: shorter on phones (560px is taller than most viewports) --- */
    .chart-wrapper {
        height: 320px;
        min-height: 280px;
    }

    .chart-card,
    .chart-card-interactive {
        min-height: 0;
    }

    .chart-iframe {
        min-height: 280px;
    }

    /* --- Analysis output: fit phone screens --- */
    #output-container {
        min-height: 280px;
        max-height: 70vh;
        padding: 16px;
        font-size: 0.9rem;
    }

    /* --- Tables: horizontal scroll instead of overflowing the page --- */
    .main-content table,
    section table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }

    /* --- Forms: full-width controls with comfortable touch targets --- */
    input[type="text"],
    input[type="number"],
    input[type="email"],
    input[type="password"],
    input[type="search"],
    input[type="date"],
    input[type="url"],
    textarea,
    select {
        width: 100%;
        max-width: 100%;
        min-height: 44px;
        font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
    }

    /* --- Footer extras: stack to avoid horizontal scroll --- */
    .footer-bar {
        flex-direction: column;
        align-items: center;
        text-align: center;
        gap: 8px;
    }

    .footer-extras {
        gap: 2px 6px;
    }

    /* --- Sidebar search: always show full field in drawer (not icon-only) --- */
    .sidebar-search-block {
        width: 100%;
    }

    .sidebar-search-icon-btn {
        display: none !important;
    }

    .sidebar-global-search-input {
        display: block !important;
        opacity: 1 !important;
        width: 100% !important;
        font-size: 16px; /* prevent iOS zoom on focus */
    }

    .search-results-dropdown {
        max-width: calc(100% - 8px);
        position: absolute;
        left: 0;
        right: 0;
    }
}

/* ==========================================================================
   ≤ 480px: small phones — extra tightening
   ========================================================================== */
@media (max-width: 480px) {
    .mobile-topbar {
        height: 52px;
        padding: 6px 10px;
    }

    .mobile-topbar-toggle {
        width: 40px;
        height: 40px;
    }

    .mobile-topbar-brand {
        font-size: 0.88rem;
    }

    .mobile-topbar-brand img {
        width: 24px;
        height: 24px;
    }

    .container {
        padding: 10px 10px;
    }

    .page-header {
        padding: 16px 12px;
    }

    .chart-wrapper {
        height: 260px;
        min-height: 240px;
    }

    #output-container {
        min-height: 220px;
        padding: 12px;
    }

    /* Smaller layout FAB on small phones */
    .layout-controls-fab {
        min-width: 40px;
        height: 40px;
        padding: 6px 8px;
    }
}

/* ==========================================================================
   Landscape phones: shrink fixed elements + cards so the chart row fits
   without the topbar eating half the viewport height.
   ========================================================================== */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
    .mobile-topbar {
        height: 48px;
    }

    .chart-wrapper {
        height: 220px;
        min-height: 200px;
    }
}

/* ==========================================================================
   Reduced motion: skip drawer slide animation
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
    .sidebar,
    .mobile-sidebar-overlay {
        transition: none !important;
    }
}

/* ==========================================================================
   Stock Analysis page (≤ 900px): phones / small tablets
   - Stop "Analysis Configuration" + "Analysis Results" from rendering as
     two half-width columns. The 1024px breakpoint in style.css uses a
     `:has()` selector (specificity 0,3,0) that out-ranks the 768px
     override (0,1,0), so on phones the two-column rule kept winning.
   - Tighten padding/typography across the three tabs so more content
     fits without the page feeling zoomed-in.
   - Fix the History tab where the per-item title row + action buttons
     were overlapping because the inline `display:flex; justify-content:
     space-between` row never wrapped.
   ========================================================================== */
@media (max-width: 900px) {
    .analysis-page .dashboard-grid,
    .analysis-page-grid,
    .dashboard-grid:has(.control-panel):has(.results-section) {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    .analysis-page .dashboard-grid .control-panel,
    .analysis-page-grid .control-panel,
    .dashboard-grid:has(.control-panel):has(.results-section) .control-panel,
    .analysis-page .dashboard-grid .results-section,
    .analysis-page-grid .results-section,
    .dashboard-grid:has(.control-panel):has(.results-section) .results-section {
        grid-column: 1 !important;
        width: 100% !important;
        max-width: 100% !important;
    }

    /* --- Analysis Configuration + Analysis Results cards: tighter --- */
    .control-panel,
    section.control-panel,
    .results-section,
    section.results-section {
        padding: 16px !important;
    }

    .results-section {
        min-height: 0;
    }

    .control-panel h2,
    section.control-panel h2,
    .results-section h2 {
        font-size: 1.15rem;
        margin: 0 0 14px 0;
    }

    .control-panel .form-group,
    section.control-panel .form-group {
        margin: 0 0 14px 0;
    }

    /* --- Analysis-type cards: fit two per row, smaller text --- */
    .analysis-types-grid {
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
        gap: 0.4rem;
    }

    .analysis-type-card {
        padding: 0.5rem;
        min-height: 62px;
    }

    .analysis-type-icon {
        font-size: 1rem;
    }

    .analysis-type-content h3 {
        font-size: 0.72rem;
    }

    .analysis-type-content p {
        font-size: 0.62rem;
    }

    .analysis-type-badge {
        font-size: 0.5rem;
        padding: 0.0625rem 0.3rem;
    }

    /* --- Date preset buttons --- */
    .date-preset-btn {
        padding: 0.4rem 0.7rem;
        font-size: 0.8rem;
    }

    /* Run Analysis: full-width primary action */
    #run-btn.btn-primary {
        width: 100%;
    }

    /* --- Tabs nav (Analysis / Charts / History): tighter so all three
       fit on a single line on phones --- */
    .analysis-tab-btn {
        padding: 10px 14px;
        font-size: 0.92rem;
    }

    /* --- History tab: unified controls layout --- */
    .history-controls .analysis-controls-row--primary {
        display: grid;
        grid-template-columns: 1fr 1fr;
    }

    .history-controls .analysis-controls-search-wrap {
        grid-column: 1 / -1;
    }

    .history-item-card__actions {
        width: 100%;
    }

    .history-item-card__actions .btn-small {
        flex: 1 1 auto;
    }

    .history-actions .btn-refresh-charts {
        padding: 8px 14px !important;
        justify-content: center;
    }

    /* --- History items: stop the title + buttons from overlapping ---
       The inline `display:flex; justify-content:space-between` row never
       wrapped, so the 4-button cluster on the right squeezed the title.
       Force the row into a stacked column on phones. */
    .history-item {
        padding: 14px !important;
        margin: 0 0 12px 0 !important;
    }

    .history-item > div:first-child {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }

    .history-item > div:first-child > div:last-child {
        flex-wrap: wrap !important;
        gap: 6px !important;
    }

    .history-item h3 {
        font-size: 1rem;
        line-height: 1.25;
    }

    .history-item .btn-small {
        padding: 6px 10px !important;
        font-size: 0.82rem;
    }

    .history-item .status-badge {
        padding: 5px 10px !important;
        font-size: 0.75rem !important;
    }
}

/* ==========================================================================
   Stock Analysis page (≤ 480px): small phones — extra tightening
   ========================================================================== */
@media (max-width: 480px) {
    .page-title {
        font-size: 1.5rem;
        gap: 10px;
    }

    .page-title-icon > .fas,
    .page-title-icon > .far {
        font-size: 1.4rem;
    }

    .page-subtitle {
        font-size: 0.9rem;
    }

    .analysis-tab-btn {
        padding: 8px 10px;
        font-size: 0.85rem;
        flex: 1 1 auto;
        text-align: center;
    }

    .control-panel,
    section.control-panel,
    .results-section,
    section.results-section,
    .history-section,
    section.history-section {
        padding: 12px !important;
    }

    /* Two columns of analysis-type cards on tiny screens */
    .analysis-types-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    /* History filters collapse to a single column at this size */
    .history-actions {
        grid-template-columns: 1fr !important;
    }

    .history-item {
        padding: 12px !important;
    }

    .history-item .btn-small {
        padding: 5px 9px !important;
        font-size: 0.78rem;
    }

    .history-item .status-badge {
        padding: 4px 8px !important;
        font-size: 0.7rem !important;
    }
}
