/* =========================================================
   RESPONSIVE — App Athena
   Mobile-first overrides. Loaded after style.css + ux-improvements.css
   Breakpoints: 1024px (tablet), 768px (drawer), 480px (phone)
   ========================================================= */

/* --- Sidebar Toggle Button (hidden on desktop) --- */
.sidebar-toggle-btn {
    display: none;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-default);
    background: var(--bg-card);
    color: var(--text-secondary);
    font-size: 16px;
    cursor: pointer;
    transition: all 0.2s ease;
    flex-shrink: 0;
}
.sidebar-toggle-btn:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
}

/* --- Sidebar Overlay (backdrop for drawer) --- */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(2, 8, 23, 0.65);
    z-index: 15;
    opacity: 0;
    transition: opacity 0.3s ease;
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
}
.sidebar-overlay.active {
    display: block;
    opacity: 1;
}

/* --- iOS Safari fixes --- */
@supports (-webkit-touch-callout: none) {
    .app-container {
        height: -webkit-fill-available;
    }
}

/* =========================================================
   BREAKPOINT: 1024px — Tablet landscape / small laptops
   ========================================================= */
@media (max-width: 1024px) {

    /* Sidebar narrows */
    .sidebar {
        width: 240px;
        min-width: 240px;
    }

    /* Topbar compress */
    .topbar {
        padding: 0 20px;
    }
    #header-company-name {
        max-width: 280px;
        font-size: 16px;
    }

    /* Dashboard padding reduce */
    .dashboard-scroll-area {
        padding: 20px 20px;
    }

    /* KPI values slightly smaller */
    .kpi-value {
        font-size: 26px;
    }

    /* Touch-friendly tap targets */
    .action-btn {
        min-height: 40px;
    }
    .tab-btn {
        min-height: 38px;
    }
    .company-item {
        min-height: 48px;
    }
    .ew-filter-btn {
        min-height: 36px;
        padding: 6px 14px;
    }

    /* Reduce hover jank on touch */
    .kpi-card:hover {
        transform: none !important;
    }
    .company-item:hover {
        padding-left: 12px !important;
    }
}

/* =========================================================
   BREAKPOINT: 768px — Tablet portrait / Sidebar → Drawer
   ========================================================= */
@media (max-width: 768px) {

    /* --- Portfolio Summary --- */
    .ps-kpi-row {
        grid-template-columns: repeat(2, 1fr);
    }
    .ps-kpi-value { font-size: 20px; }
    .ps-table { min-width: 800px; }

    /* --- Show hamburger button --- */
    .sidebar-toggle-btn {
        display: flex;
    }

    /* --- Sidebar as off-canvas drawer --- */
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 300px;
        min-width: 300px;
        transform: translateX(-100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 20;
    }
    .sidebar.sidebar-open {
        transform: translateX(0);
    }

    /* --- Topbar --- */
    .topbar {
        height: auto;
        min-height: 52px;
        padding: 10px 16px;
        gap: 8px;
        flex-wrap: wrap;
    }
    .topbar-left {
        gap: 8px;
    }
    .topbar-right {
        gap: 6px;
    }
    #header-company-name {
        max-width: 200px;
        font-size: 15px;
    }

    /* Hide button text labels, keep icons */
    .topbar-right .action-btn span,
    .topbar-right #btn-portfolio-analytics span,
    #btn-delete-company span {
        display: none;
    }
    /* But topbar buttons use text nodes, not spans — hide text via font-size trick */
    #btn-portfolio-analytics {
        font-size: 0;
    }
    #btn-portfolio-analytics i {
        font-size: 14px;
    }
    #btn-add-company {
        font-size: 0;
    }
    #btn-add-company i {
        font-size: 14px;
    }
    #btn-delete-company {
        font-size: 0;
    }
    #btn-delete-company i {
        font-size: 12px;
    }

    /* Hide FG sub-indices on mobile (tap-to-toggle still works) */
    .fg-info .fg-title {
        display: none;
    }
    .fg-details {
        position: fixed;
        top: 60px;
        right: 12px;
        left: auto;
        z-index: 100;
    }

    /* --- Notification dropdown --- */
    .notif-dropdown {
        position: fixed !important;
        top: 56px !important;
        right: 12px !important;
        left: 12px !important;
        width: auto !important;
        max-width: none;
    }

    /* --- Dashboard scroll --- */
    .dashboard-scroll-area {
        padding: 16px 16px;
    }

    /* --- KPI row: 4 → 2 columns --- */
    .kpi-row {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 12px;
    }
    .kpi-value {
        font-size: 24px;
    }

    /* --- Main panels: side-by-side → stacked --- */
    .main-panels-row {
        flex-direction: column;
    }
    .main-panels-row .flex-2,
    .main-panels-row .flex-1 {
        flex: unset !important;
        width: 100%;
    }

    /* Metrics grid: 2 → 1 column */
    .metrics-grid {
        grid-template-columns: 1fr !important;
    }

    /* Projection chart */
    .proj-chart-wrap {
        height: 180px;
    }

    /* --- Global Dashboard --- */
    .analytics-grid {
        grid-template-columns: 1fr !important;
    }
    .analytics-chart-card {
        height: 280px;
    }
    .rebalanceo-chart-wrap {
        max-height: 300px;
    }

    /* Earnings watch compact */
    .ew-item {
        grid-template-columns: 50px 1fr auto;
    }

    /* Rebalanceo sort bar wrap */
    .rebalanceo-sort-bar {
        flex-wrap: wrap;
        gap: 6px;
    }

    /* GD section headers */
    .gds-header-right {
        flex-wrap: wrap;
        gap: 6px;
    }
    .gds-header-right .action-btn {
        font-size: 0;
        padding: 6px 10px;
    }
    .gds-header-right .action-btn i {
        font-size: 12px;
    }

    /* --- Modals --- */
    .modal-content {
        width: calc(100vw - 32px) !important;
        max-width: 400px;
    }
    .modal-bulk {
        width: calc(100vw - 16px) !important;
        max-width: none !important;
        max-height: calc(100vh - 32px) !important;
    }
    .modal-body {
        padding: 16px;
    }

    /* --- Master DB View --- */
    .master-db-header-row {
        flex-wrap: wrap;
        gap: 8px;
    }
    .master-category-tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: 2px;
    }
    .master-category-tabs::-webkit-scrollbar {
        display: none;
    }
    .master-category-tabs .tab-btn {
        white-space: nowrap;
        flex-shrink: 0;
    }
    .smart-filter-bar {
        flex-wrap: wrap;
    }
    .sort-bar {
        flex-wrap: wrap;
        gap: 6px;
    }

    /* Sticky column narrower */
    .metric-cell:first-child {
        min-width: 160px !important;
        max-width: 200px !important;
    }

    /* Table scroll shadow affordance */
    .master-table-wrapper {
        position: relative;
    }
    .master-table-wrapper::after {
        content: '';
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 24px;
        pointer-events: none;
        background: linear-gradient(to left, rgba(2, 8, 23, 0.7), transparent);
        z-index: 5;
    }
    .horizontal-scroll-container {
        position: relative;
    }
    .horizontal-scroll-container::after {
        content: '';
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 24px;
        pointer-events: none;
        background: linear-gradient(to left, rgba(2, 8, 23, 0.7), transparent);
        z-index: 5;
    }

    /* Prevent iOS zoom on input focus (inputs < 16px trigger zoom) */
    input, select, textarea {
        font-size: 16px !important;
    }

    /* Master DB full height adjust */
    #master-db-view {
        height: calc(100vh - 80px) !important;
    }

    /* --- Login Screen Responsive --- */
    .login-card {
        width: calc(100vw - 48px);
        max-width: 380px;
        padding: 40px 32px;
    }
    .login-logo i {
        font-size: 40px;
    }
    .login-logo h1 {
        font-size: 24px;
        letter-spacing: 4px;
    }

    /* --- User Badge in Drawer --- */
    .user-badge {
        padding: 12px 20px;
    }

    /* --- Benchmark / Fundamental panels --- */
    .benchmark-grid {
        grid-template-columns: 1fr !important;
    }
    .forward-multiples-grid {
        grid-template-columns: 1fr !important;
    }

    /* --- Modal footer buttons stack --- */
    .modal-footer {
        flex-wrap: wrap;
        gap: 8px;
    }
    .modal-footer .primary-btn,
    .modal-footer .secondary-btn,
    .modal-footer .submit-btn {
        flex: 1 1 auto;
        min-width: 120px;
    }
}

/* =========================================================
   BREAKPOINT: 480px — Phones portrait
   ========================================================= */
@media (max-width: 480px) {

    /* --- Portfolio Summary --- */
    .ps-kpi-row { grid-template-columns: 1fr; }
    .ps-kpi-value { font-size: 18px; }
    .ps-kpi-card { padding: 14px 16px; }
    .ps-table-header { flex-direction: column; gap: 8px; align-items: flex-start; }

    /* Topbar ultra-compact */
    .topbar {
        padding: 8px 12px;
    }
    #header-company-name {
        max-width: 150px;
        font-size: 14px;
    }

    /* Hide earnings badge on phones */
    .earnings-badge {
        display: none !important;
    }

    /* Dashboard padding minimal */
    .dashboard-scroll-area {
        padding: 12px 10px;
    }

    /* KPI: 2 → 1 column */
    .kpi-row {
        grid-template-columns: 1fr !important;
    }
    .kpi-value {
        font-size: 22px;
    }
    .kpi-card {
        padding: 14px 16px;
    }

    /* Glass card padding reduce */
    .glass-card {
        padding: 14px;
    }
    .gd-section .gd-section-body {
        padding: 12px;
    }

    /* Panel titles smaller */
    .panel-title {
        font-size: 14px;
    }

    /* Earnings watch: simpler grid */
    .ew-item {
        grid-template-columns: 1fr auto;
        gap: 4px;
    }
    .ew-item .ew-name {
        display: none;
    }

    /* Analytics chart smaller */
    .analytics-chart-card {
        height: 240px;
    }

    /* Modal padding tighter */
    .modal-body {
        padding: 12px;
    }
    .modal-header h3 {
        font-size: 15px;
    }

    /* Master DB table font smaller */
    #master-table-body td {
        font-size: 11px;
        padding: 8px 10px;
    }
    #master-table-head th {
        font-size: 11px;
        padding: 8px 10px;
    }

    /* Sort bar compact */
    .sort-bar {
        gap: 4px;
    }
    .sort-bar-label {
        font-size: 11px;
    }

    /* Bulk table columns narrower on phone */
    .bulk-table th:nth-child(1), .bulk-table td:nth-child(1) { min-width: 120px; }
    .bulk-table th:nth-child(6), .bulk-table td:nth-child(6) { min-width: 100px; }

    /* Sidebar drawer slightly narrower on very small phones */
    .sidebar {
        width: 280px;
        min-width: 280px;
    }

    /* Empty state icon smaller */
    .empty-icon-wrapper {
        width: 72px;
        height: 72px;
    }
    .empty-state-wrapper h3 {
        font-size: 16px;
    }
    .empty-state-wrapper p {
        font-size: 13px;
    }

    /* Projection chart even shorter */
    .proj-chart-wrap {
        height: 160px;
    }

    /* --- Login Screen Phone --- */
    .login-card {
        width: calc(100vw - 32px);
        padding: 32px 24px;
    }
    .login-logo i {
        font-size: 36px;
        margin-bottom: 12px;
    }
    .login-logo h1 {
        font-size: 22px;
        letter-spacing: 4px;
    }
    .login-logo p {
        font-size: 12px;
    }
    .login-divider {
        margin: 20px 0;
    }
    .login-google-btn {
        width: 100%;
        padding: 11px 24px;
        font-size: 13px;
    }

    /* --- User Badge Phone --- */
    .user-badge {
        padding: 10px 16px;
        gap: 8px;
    }
    .user-avatar {
        width: 28px;
        height: 28px;
    }
    .user-name {
        font-size: 12px;
        max-width: 120px;
    }

    /* --- Topbar Phone Refinements --- */
    #header-ticker-badge {
        display: none;
    }
    #header-company-name {
        max-width: 120px;
    }

    /* --- Score Breakdown Phone --- */
    .score-cat-name {
        font-size: 10px;
    }
    .score-cat-val {
        font-size: 11px;
    }
    .score-breakdown-container {
        gap: 10px;
    }

    /* --- Metrics grid tighter --- */
    .metrics-group-title {
        font-size: 11px;
    }
    .metric-cell {
        padding: 6px 8px !important;
        font-size: 11px;
    }

    /* --- Valoración diagnostics --- */
    .diagnostico-valoracion {
        gap: 10px;
    }

    /* --- Rebalanceo chart shorter --- */
    .rebalanceo-chart-wrap {
        max-height: 250px;
    }
}

/* =========================================================
   GLOBAL TOUCH IMPROVEMENTS
   ========================================================= */
@media (pointer: coarse) {
    button, .action-btn, .tab-btn, .company-item, .ew-filter-btn,
    .sort-pill, .filter-chip, .master-category-tabs .tab-btn {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }
}

/* =========================================================
   PERFORMANCE: Reduce blur on low-end devices
   ========================================================= */
@media (prefers-reduced-motion: reduce) {
    .glass-card,
    .sidebar,
    .sidebar-overlay {
        -webkit-backdrop-filter: none !important;
        backdrop-filter: none !important;
    }
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
