/* Responsive */
@media (max-width: 768px) {
    .dashboard {
        --page-gutter: 1rem;
        padding: 0 var(--page-gutter) 1rem;
    }

    header {
        overflow: visible;
        height: var(--nav-height);
        flex-wrap: nowrap;
    }

    .header-menu-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        width: 44px;
        height: 44px;
        padding: 0;
        background: var(--bg-hover);
        border: 1px solid var(--border-color);
        border-radius: 8px;
        color: var(--text-secondary);
        cursor: pointer;
    }

    .header-menu-toggle:hover,
    header.header-menu-open .header-menu-toggle {
        color: var(--text-primary);
        border-color: var(--border-highlight);
    }

    .user-info {
        display: none;
        position: absolute;
        top: calc(100% - 0.35rem);
        right: var(--page-gutter);
        flex-direction: column;
        align-items: stretch;
        gap: 0.35rem;
        min-width: 12.5rem;
        max-width: calc(100vw - 2 * var(--page-gutter));
        padding: 0.5rem;
        background: var(--bg-dark);
        border: 1px solid var(--border-color);
        border-radius: 10px;
        box-shadow: var(--shadow-dropdown);
        z-index: 120;
    }

    header.header-menu-open .user-info {
        display: flex;
    }

    .user-info .lang-picker,
    .user-info .btn-help-header,
    .user-info .community-header-link,
    .user-info .billing-header-manage,
    .user-info .btn-logout {
        width: 100%;
        justify-content: center;
        margin: 0;
        min-height: 44px;
    }

    .user-info #username-display {
        display: block;
        text-align: center;
        padding: 0.35rem 0.5rem;
        word-break: break-all;
    }

    /* Contained horizontal scroll — do not set width:100% on base .tabs (desktop shrink-to-fit) */
    .tabs {
        display: flex;
        width: 100%;
        max-width: 100%;
        min-width: 0;
        flex-wrap: nowrap;
    }

    .tab {
        flex-shrink: 0;
    }

    .matches-grid {
        grid-template-columns: 1fr;
    }

    .toolbar-top,
    .toolbar-bottom {
        flex-direction: column;
        align-items: stretch;
    }

    .toolbar-actions {
        justify-content: stretch;
    }

    .btn-refresh,
    .btn-scan {
        flex: 1;
    }

    .category-tabs {
        display: none;
    }

    .filter-select-mobile {
        display: block !important;
        width: 100%;
        padding: 0.4rem 0.8rem;
        background: var(--bg-input);
        border: 1px solid var(--border-color);
        border-radius: 8px;
        color: var(--text-primary);
        font-size: var(--text-caption);
    }

    .gallery-profile-select {
        min-width: 0;
        max-width: none;
        width: 100%;
    }

    .sort-select {
        width: 100%;
    }

    .toolbar-filters {
        width: 100%;
        min-width: 0;
    }

    .trade-plan-toggle {
        white-space: normal;
        max-width: 100%;
    }

    .modal {
        padding-top: 50px;
        /* Space for close button if needed, but height is 100% */
    }

    .modal-layout {
        flex-direction: column;
        width: 100%;
        height: 100%;
        max-width: none;
        border-radius: 0;
        overflow-y: auto;
    }

    .chart-modal-body {
        width: 100%;
        height: 55vh;
        min-height: 400px;
        flex: none;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        border-radius: 0;
    }

    .modal-sidebar {
        width: 100%;
        height: auto;
        flex: none;
        overflow-y: visible;
        padding-bottom: 2rem;
    }

    .modal-nav {
        padding: 0.5rem;
        font-size: var(--text-title);
        background: rgba(0, 0, 0, 0.7);
    }

    .modal-prev {
        left: 5px;
    }

    .modal-next {
        right: 5px;
    }

    #modal-caption {
        width: 95%;
        font-size: var(--text-heading);
    }

    /* Gallery card elements on mobile */
    .match-company {
        max-width: 140px;
    }

    .gallery-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.5rem;
    }

    .scan-status-line {
        flex-wrap: wrap;
        font-size: var(--text-caption);
    }

    .portfolio-table {
        font-size: var(--text-caption);
    }

    .portfolio-table th,
    .portfolio-table td {
        padding: 0.4rem;
    }

    /* Increase touch targets on mobile */
    .card-actions a,
    .card-actions button {
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .btn-icon {
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* Tablet breakpoint — override gallery-profile-select clamp */
@media (min-width: 769px) and (max-width: 1024px) {
    .gallery-profile-select {
        min-width: 0;
        max-width: none;
    }
}

/* Small phone breakpoint */
@media (max-width: 480px) {
    .dashboard {
        /* Keep 1rem gutter (not 0.5rem) so bleed chrome and header edges stay readable */
        --page-gutter: 1rem;
        padding: 0 var(--page-gutter) var(--space-md);
    }

    .logo {
        font-size: var(--text-heading);
        flex-shrink: 0;
        min-width: 0;
    }

    .logo span {
        overflow: visible;
        text-overflow: unset;
        white-space: nowrap;
    }

    .tabs {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        overflow-x: hidden;
    }

    .tab {
        flex: 1 1 0;
        min-width: 0;
        min-height: 44px;
        padding: 0.5rem 0.2rem;
        font-size: var(--text-caption);
    }

    .tab-label {
        display: none;
    }

    .tab-icon {
        font-size: 1.15rem;
        line-height: 1;
    }

    .trade-plan-toggle {
        font-size: var(--text-caption);
        padding: 0.35rem 0.6rem;
    }

    /* Compact gallery chrome so empty state stays above the fold on 375×667 */
    #macro-pulse-bar,
    #traffic-light-bar {
        display: none;
    }

    .gallery-chrome-secondary {
        display: flex;
        flex: 0 0 auto;
        gap: 0.35rem;
        align-items: center;
    }

    .gallery-chrome-secondary .toggle-label {
        font-size: var(--text-caption);
        white-space: nowrap;
    }

    .gallery-chrome-secondary .trade-plan-toggle {
        padding: 0.35rem 0.5rem;
    }

    /* Pair selects instead of 100% stacking (≤768 width:100%) so empty-state stays above the fold */
    .toolbar-filters {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        align-items: center;
    }

    .sort-select,
    .gallery-profile-select,
    .filter-select-mobile {
        width: auto;
        flex: 1 1 calc(50% - 0.25rem);
        min-width: 0;
    }

    .empty-state {
        padding: 1rem 0.75rem;
    }

    .empty-state .empty-icon {
        font-size: 2rem;
        margin-bottom: 0.5rem;
    }

    #help-modal.modal {
        padding: 0;
        overflow: hidden;
        align-items: stretch;
        justify-content: stretch;
    }

    .help-modal-body {
        width: 100%;
        max-width: none;
        max-height: 100%;
        height: 100%;
        border-radius: 0;
        padding: var(--space-lg);
    }

    .match-score {
        padding: 0.3rem 0.55rem;
    }

    .preset-cards {
        grid-template-columns: 1fr;
    }

    .pnl-card {
        padding: var(--space-md);
    }

    .pnl-value {
        font-size: var(--text-title);
    }

    .toast {
        min-width: auto;
        max-width: calc(100vw - 2rem);
        margin: 0 var(--space-md);
    }
}

/* Modals General Formatting - Assuming default modals outside of media queries aren't fully styled here, but added just in case */
.modal {
    background: var(--bg-overlay-heavy) !important;
    backdrop-filter: var(--glass-blur) !important;
    -webkit-backdrop-filter: var(--glass-blur) !important;
}

.chart-modal-body {
    background: var(--bg-card) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-card) !important;
    border-radius: var(--radius);
    overflow: hidden;
}

/* Progress Bar */
.progress-container {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 1rem 1.2rem;
    margin-bottom: 1rem;
}

.progress-phase {
    color: var(--text-primary);
    font-weight: 600;
    margin-bottom: 0.5rem;
    font-size: var(--text-body);
}

.progress-track {
    background: var(--bg-input);
    border-radius: 4px;
    height: 8px;
    overflow: hidden;
}

.progress-fill {
    background: linear-gradient(90deg, var(--accent-blue), #1d4ed8);
    height: 100%;
    width: 0%;
    transition: width 0.3s ease;
    border-radius: 4px;
}

.progress-msg {
    color: var(--text-secondary);
    font-size: var(--text-caption);
    margin-top: 0.4rem;
}

/* Scan Warning Banner */
.scan-warning {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
    background: var(--accent-warning-bg);
    border: 1px solid var(--accent-warning-border-strong);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: var(--text-body);
    line-height: 1.4;
    animation: fadeIn 0.3s ease;
}

.scan-warning-icon {
    font-size: var(--text-heading);
    flex-shrink: 0;
}

.scan-warning-text {
    flex: 1;
}

.scan-warning-tickers {
    display: block;
    font-size: var(--text-caption);
    color: var(--text-secondary);
    margin-top: 0.2rem;
    font-family: var(--font-mono);
}

.scan-warning-dismiss {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: var(--text-heading);
    cursor: pointer;
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.scan-warning-dismiss:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* Empty / Onboarding State */
.empty-state {
    text-align: center;
    padding: 3rem 2rem;
    color: var(--text-secondary);
}

.empty-state .empty-icon {
    /* Decorative — excluded from type scale */
    font-size: 3rem;
    margin-bottom: 1rem;
}

.empty-state h3 {
    color: var(--text-primary);
    margin-bottom: 0.5rem;
}

.empty-state p {
    margin-bottom: 1.5rem;
    max-width: 400px;
    margin-left: auto;
    margin-right: auto;
    line-height: 1.5;
}

.empty-state .btn-onboard {
    padding: 0.7rem 1.5rem;
    background: linear-gradient(135deg, var(--accent-blue), #1d4ed8);
    border: none;
    border-radius: 8px;
    color: white;
    font-size: var(--text-body);
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.empty-state .btn-onboard:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--accent-blue-glow);
}

/* Modal */
.modal {
    display: flex;
    /* Changed from default block to flex for centering */
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0, 0, 0, 0.9);
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.modal.hidden {
    display: none !important;
}

.modal-layout {
    display: flex;
    width: 95vw;
    height: 85vh;
    max-width: 1600px;
    background: var(--bg-modal);
    border-radius: var(--radius);
    box-shadow: var(--shadow-modal);
    animation: zoom 0.3s;
    overflow: hidden;
}

.chart-modal-body {
    position: relative;
    flex: 1;
    min-width: 0;
    border-right: 1px solid var(--border-subtle);
}

.modal-caption-overlay {
    position: absolute;
    bottom: 15px;
    left: 15px;
    background: var(--bg-overlay);
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: var(--text-body);
    z-index: 20;
    pointer-events: none;
    border: 1px solid var(--border-subtle);
}

.modal-caption-overlay:empty {
    display: none;
}

.modal-sidebar {
    width: 350px;
    background: var(--bg-card);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.modal-sidebar-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-hover);
}

.modal-sidebar-header h3 {
    margin: 0 0 0.5rem 0;
    font-size: var(--text-title);
    color: var(--text-primary);
}

.modal-sidebar-content {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.sidebar-details {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.sidebar-details span {
    display: flex;
    justify-content: space-between;
    padding: 0.5rem;
    background: var(--bg-hover);
    border-radius: 6px;
    font-size: var(--text-body);
    color: var(--text-secondary);
}

#chart-container {
    width: 100%;
    height: 100%;
}

.chart-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--text-secondary);
    font-size: var(--text-heading);
    z-index: 10;
}

.chart-loading.hidden {
    display: none;
}

.chart-legend {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 20;
    background: var(--chart-legend-bg);
    border: 1px solid var(--border-subtle);
    border-radius: 6px;
    padding: 10px 15px;
    font-size: var(--text-caption);
    color: var(--chart-legend-text);
    pointer-events: none;
}

.legend-title {
    font-size: var(--text-heading);
    font-weight: bold;
    margin-bottom: 8px;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-subtle);
    padding-bottom: 4px;
}

.legend-item {
    display: flex;
    align-items: center;
    margin-bottom: 4px;
}

.legend-color {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    display: inline-block;
    margin-right: 8px;
}

#modal-caption {
    margin: 1rem auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: var(--text-secondary);
    padding: 10px 0;
    font-size: var(--text-title);
}

.modal-close {
    position: absolute;
    top: 15px;
    right: 35px;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    margin: 0;
    background: none;
    border: none;
    color: var(--text-primary);
    /* Decorative — excluded from type scale */
    font-size: 1.75rem;
    font-weight: bold;
    line-height: 1;
    transition: 0.3s;
    cursor: pointer;
    z-index: 1001;
    border-radius: 50%;
}

.modal-close:hover,
.modal-close:focus {
    color: var(--text-secondary);
    text-decoration: none;
    cursor: pointer;
}

.modal-close:focus-visible {
    outline: 2px solid var(--accent-blue);
    outline-offset: 2px;
}

/* In-header close (MDH): stay in flex flow, not overlay-absolute */
.mdh-header-actions .modal-close {
    position: static;
    top: auto;
    right: auto;
    flex-shrink: 0;
}

/* Mobile glass circle — must follow base .modal-close so background/z-index win */
@media (max-width: 768px) {
    .modal-close {
        top: 10px;
        right: 15px;
        background: rgba(0, 0, 0, 0.6);
        border-radius: 50%;
        min-width: 44px;
        min-height: 44px;
        backdrop-filter: var(--glass-blur);
        -webkit-backdrop-filter: var(--glass-blur);
        z-index: 2000;
    }

    .mdh-header-actions .modal-close {
        background: transparent;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }
}

@keyframes zoom {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* News & Special Situations — keep badge layout in sync with base .match-badges */
.match-badges {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
    justify-content: flex-end;
    min-width: 0;
}

.badge-special {
    background: var(--accent-red);
    color: white;
    padding: 0.3rem 0.6rem;
    border-radius: 20px;
    font-size: var(--text-caption);
    font-weight: 600;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0.7;
    }

    100% {
        opacity: 1;
    }
}

.news-section {
    margin-top: 0.8rem;
    padding-top: 0.8rem;
    border-top: 1px solid var(--border-color);
}

.news-header {
    font-size: var(--text-caption);
    font-weight: 600;
    color: var(--text-dim);
    margin-bottom: 0.4rem;
}

.news-item {
    display: block;
    font-size: var(--text-caption);
    color: var(--accent-blue);
    text-decoration: none;
    margin-bottom: 0.3rem;
    line-height: 1.3;
}

.news-item:hover {
    text-decoration: underline;
}

