/* ===== RESET & BASE ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #1a73e8;
    --primary-dark: #1557b0;
    --success: #0f9d58;
    --danger: #d93025;
    --warning: #f9ab00;
    --dark: #202124;
    --gray-900: #3c4043;
    --gray-700: #5f6368;
    --gray-500: #80868b;
    --gray-300: #dadce0;
    --gray-100: #f1f3f4;
    --gray-50: #f8f9fa;
    --white: #ffffff;
    --border-radius: 8px;
    --shadow-sm: 0 1px 2px 0 rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);
    --shadow-md: 0 1px 3px 0 rgba(60, 64, 67, 0.3), 0 4px 8px 3px rgba(60, 64, 67, 0.15);
    --transition: all 0.2s ease;
}

[data-theme="dark"] {
    --primary: #8ab4f8;
    --primary-dark: #aecbfa;
    --success: #81c995;
    --danger: #f28b82;
    --warning: #fdd663;
    --dark: #e8eaed;
    --gray-900: #f1f3f4;
    --gray-700: #bdc1c6;
    --gray-500: #9aa0a6;
    --gray-300: #3c4043;
    --gray-100: #202124;
    --gray-50: #171717;
    /* Very dark background */
    --white: #202124;
    /* Dark card background */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3), 0 1px 3px 1px rgba(0, 0, 0, 0.15);
    --shadow-md: 0 1px 3px 0 rgba(0, 0, 0, 0.3), 0 4px 8px 3px rgba(0, 0, 0, 0.15);
}

.theme-toggle {
    background: none;
    border: none;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    color: var(--gray-700);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.theme-toggle:hover {
    background-color: var(--gray-100);
    color: var(--dark);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    line-height: 1.6;
    color: var(--dark);
    background: var(--gray-50);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    zoom: 90%;
    /* Scale down to 90% by default */
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 24px;
}

/* ===== HEADER ===== */
.header {
    background: var(--white);
    border-bottom: 1px solid var(--gray-300);
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64px;
}

.logo {
    font-size: 24px;
    font-weight: 700;
    color: var(--dark);
    letter-spacing: -0.5px;
}

.logo span {
    color: var(--primary);
    font-weight: 600;
}

.nav {
    display: flex;
    gap: 8px;
}

.nav-link {
    padding: 8px 16px;
    color: var(--gray-700);
    text-decoration: none;
    border-radius: var(--border-radius);
    font-weight: 500;
    transition: var(--transition);
}

.nav-link:hover {
    background: var(--gray-100);
    color: var(--dark);
}

.nav-link.active {
    background: var(--primary);
    color: var(--white);
}

/* ===== MAIN CONTENT ===== */
.main {
    padding: 32px 0;
    min-height: calc(100vh - 64px - 80px);
}

/* ===== SEARCH ===== */
.search-section {
    margin-bottom: 32px;
}

.search-box {
    position: relative;
    max-width: 600px;
}

.search-icon {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--gray-500);
}

.search-input {
    width: 100%;
    padding: 12px 16px 12px 48px;
    border: 1px solid var(--gray-300);
    border-radius: var(--border-radius);
    font-size: 15px;
    font-family: inherit;
    transition: var(--transition);
    background: var(--white);
    color: var(--dark);
}

.search-input::placeholder {
    color: var(--gray-500);
    opacity: 1;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.1);
}

/* ===== STATS GRID ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}

.stat-card {
    background: var(--white);
    padding: 24px;
    border-radius: var(--border-radius);
    border: 1px solid var(--gray-300);
    transition: var(--transition);
}

.stat-card:hover {
    box-shadow: var(--shadow-md);
    transform: translateY(-2px);
}

.stat-label {
    font-size: 13px;
    color: var(--gray-700);
    font-weight: 500;
    margin-bottom: 8px;
}

.stat-value {
    font-size: 28px;
    font-weight: 700;
    color: var(--dark);
    margin-bottom: 4px;
    font-variant-numeric: tabular-nums;
}

.stat-change {
    font-size: 13px;
    font-weight: 500;
}

.stat-change.positive {
    color: var(--success);
}

.stat-change.negative {
    color: var(--danger);
}

.stat-change.neutral {
    color: var(--gray-500);
}

/* ===== SECTION ===== */
.section {
    background: var(--white);
    border-radius: var(--border-radius);
    border: 1px solid var(--gray-300);
}

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 24px;
    border-bottom: 1px solid var(--gray-300);
}

.section-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--dark);
}

.section-actions {
    display: flex;
    gap: 12px;
}

/* ===== TABLE ===== */
.table-container {
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
}

.data-table thead {
    background: var(--gray-50);
    border-bottom: 2px solid var(--gray-300);
}

.data-table th {
    padding: 12px 16px;
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--gray-700);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.data-table th.sortable {
    cursor: pointer;
    user-select: none;
}

.data-table th.sortable:hover {
    background: var(--gray-100);
}

.data-table th.sortable {
    cursor: pointer;
    user-select: none;
    position: relative;
    padding-right: 24px;
    /* Space for icon */
}

.data-table th.sortable::after {
    content: '↕';
    position: absolute;
    right: 8px;
    color: var(--gray-500);
    font-size: 12px;
    opacity: 0.5;
}

.data-table th.sortable.asc::after {
    content: '↑';
    color: var(--primary);
    opacity: 1;
}

.data-table th.sortable.desc::after {
    content: '↓';
    color: var(--primary);
    opacity: 1;
}

.data-table td {
    padding: 16px;
    border-bottom: 1px solid var(--gray-300);
    font-size: 14px;
}

.data-table tbody tr {
    transition: var(--transition);
}

.data-table tbody tr:hover {
    background: var(--gray-50);
}

.data-table td.loading {
    text-align: center;
    color: var(--gray-500);
    padding: 40px;
}

.fund-code {
    font-weight: 600;
    color: var(--primary);
    cursor: pointer;
}

.fund-code:hover {
    text-decoration: underline;
}

.fund-name {
    color: var(--dark);
    max-width: 400px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.price {
    font-weight: 600;
    color: var(--dark);
}

.market-cap {
    color: var(--gray-700);
    white-space: nowrap;
}

.investors {
    color: var(--gray-700);
}

.date {
    color: var(--gray-500);
    font-size: 13px;
}

/* ===== PAGINATION ===== */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 20px;
    border-top: 1px solid var(--gray-300);
}

.page-info {
    color: var(--gray-700);
    font-size: 14px;
}

/* ===== BUTTONS ===== */
.btn {
    padding: 10px 20px;
    border: none;
    border-radius: var(--border-radius);
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    display: inline-block;
}

.btn-primary {
    background: var(--primary);
    color: var(--white);
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-dark);
}

.btn-secondary {
    background: var(--white);
    color: var(--gray-700);
    border: 1px solid var(--gray-300);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--gray-50);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ===== SELECT ===== */
.select-input {
    padding: 8px 32px 8px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--border-radius);
    font-size: 14px;
    font-family: inherit;
    background: var(--white);
    color: var(--dark);
    cursor: pointer;
    transition: var(--transition);
}

.select-input:focus {
    outline: none;
    border-color: var(--primary);
}

/* ===== FOOTER ===== */
.footer {
    background: var(--white);
    border-top: 1px solid var(--gray-300);
    padding: 24px 0;
    margin-top: 48px;
}

.footer p {
    text-align: center;
    color: var(--gray-500);
    font-size: 13px;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        height: auto;
        padding: 12px 0;
        gap: 12px;
    }

    .nav {
        width: 100%;
        overflow-x: auto;
        justify-content: flex-start;
        /* Allow scrolling if needed */
        padding-bottom: 4px;
        /* Space for scrollbar */
        -webkit-overflow-scrolling: touch;
    }

    .nav-link {
        white-space: nowrap;
        padding: 8px 12px;
        font-size: 13px;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        /* 2 columns on mobile */
        gap: 12px;
    }

    .section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 12px;
    }

    .section-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .select-input,
    .text-input,
    .search-input {
        font-size: 16px;
        /* Prevent iOS zoom */
    }

    .data-table {
        font-size: 12px;
    }

    .data-table th,
    .data-table td {
        padding: 8px 6px;
        /* Tighter padding */
    }

    .hide-mobile {
        display: none;
    }

    /* Make buttons full width on very small screens */
    .btn {
        width: 100%;
        text-align: center;
        margin-top: 8px;
    }

    /* Specific fix for calc type row */
    #top10View .compare-selector>div {
        flex-wrap: wrap;
    }

    #top10CalcType {
        max-width: 100% !important;
        margin-bottom: 8px;
    }

    #calculateTop10Btn {
        width: 100%;
        margin: 0;
    }
}

/* ===== VIEWS ===== */
.view {
    display: none;
}

.view.active {
    display: block;
}

/* ===== MODAL ===== */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    animation: fadeIn 0.2s ease;
}

.modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.modal-content {
    background: var(--white);
    border-radius: var(--border-radius);
    width: 90%;
    max-width: 800px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    animation: slideUp 0.3s ease;
}

.modal-content.modal-sm {
    max-width: 500px;
}

.modal-content.modal-lg {
    max-width: 1000px;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 24px;
    border-bottom: 1px solid var(--gray-300);
}

.modal-header h2 {
    font-size: 20px;
    font-weight: 600;
    color: var(--dark);
}

.modal-close {
    background: none;
    border: none;
    font-size: 28px;
    color: var(--gray-500);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: var(--transition);
}

.modal-close:hover {
    background: var(--gray-100);
    color: var(--dark);
}

.modal-body {
    padding: 24px;
}

/* ===== TABS ===== */
.tabs {
    display: flex;
    gap: 8px;
    border-bottom: 2px solid var(--gray-300);
    margin-bottom: 24px;
}

.tab-btn {
    padding: 12px 20px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    font-size: 14px;
    font-weight: 500;
    color: var(--gray-700);
    cursor: pointer;
    transition: var(--transition);
}

.tab-btn:hover {
    color: var(--primary);
}

.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* ===== METRICS GRID ===== */
.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.metric-card {
    background: var(--gray-50);
    padding: 20px;
    border-radius: var(--border-radius);
    border: 1px solid var(--gray-300);
}

.metric-label {
    font-size: 13px;
    color: var(--gray-700);
    font-weight: 500;
    margin-bottom: 8px;
}

.metric-value {
    font-size: 24px;
    font-weight: 700;
    color: var(--dark);
    font-variant-numeric: tabular-nums;
}

.metric-explanation {
    padding: 16px;
    background: var(--gray-50);
    border-radius: var(--border-radius);
    margin-top: 16px;
}

.metric-explanation p {
    margin-bottom: 8px;
    font-size: 13px;
    color: var(--gray-700);
}

.metric-explanation p:last-child {
    margin-bottom: 0;
}


/* ===== FUND STATS ===== */
.fund-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.fund-stat {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.fund-stat .label {
    font-size: 13px;
    color: var(--gray-700);
    font-weight: 500;
}

.fund-stat .value {
    font-size: 18px;
    font-weight: 600;
    color: var(--dark);
}

/* ===== CHART CONTAINER ===== */
.chart-container {
    position: relative;
    height: 300px;
    margin-bottom: 24px;
}

/* ===== FORM ===== */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
    color: var(--dark);
}

.text-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--gray-300);
    border-radius: var(--border-radius);
    font-size: 14px;
    font-family: inherit;
    transition: var(--transition);
}

.text-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.1);
}

/* ===== AUTOCOMPLETE ===== */
.search-wrapper {
    position: relative;
    width: 100%;
}

.search-results {
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    max-height: 250px;
    overflow-y: auto;
    background: var(--white);
    border: 1px solid var(--gray-300);
    border-radius: 0 0 var(--border-radius) var(--border-radius);
    box-shadow: var(--shadow-md);
    z-index: 9999;
    /* Increased z-index significantly */
    display: none;
}

.search-results.active {
    display: block;
}

.search-result-item {
    padding: 10px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--gray-100);
    transition: var(--transition);
}

.search-result-item:last-child {
    border-bottom: none;
}

.search-result-item:hover {
    background-color: var(--gray-50);
}

.search-result-item .code {
    font-weight: 600;
    color: var(--primary);
    margin-right: 8px;
}

.search-result-item .title {
    color: var(--dark);
    font-size: 13px;
}

.btn-block {
    width: 100%;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
}

/* ===== SECTION BODY ===== */
.section-body {
    padding: 24px;
}

/* ===== COMPARE ===== */
.compare-selector {
    margin-bottom: 24px;
}

.search-wrapper {
    position: relative;
}

.compare-selector label {
    display: block;
    margin-bottom: 12px;
    font-weight: 500;
}

.fund-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 12px;
    min-height: 32px;
}

.fund-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: var(--primary);
    color: var(--white);
    border-radius: 16px;
    font-size: 13px;
    font-weight: 500;
}

.fund-chip-remove {
    background: none;
    border: none;
    color: var(--white);
    cursor: pointer;
    padding: 0;
    font-size: 18px;
    line-height: 1;
}

.suggestions {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--white);
    border: 1px solid var(--gray-300);
    border-top: none;
    border-radius: 0 0 var(--border-radius) var(--border-radius);
    max-height: 400px;
    overflow-y: auto;
    z-index: 10;
    display: none;
}

.suggestions.active {
    display: block;
}

.suggestion-item {
    padding: 12px 16px;
    cursor: pointer;
    transition: var(--transition);
    color: var(--dark);
}

.suggestion-item:hover {
    background: var(--gray-50);
}

.comparison-table {
    margin-top: 24px;
}

/* ===== PORTFOLIO ===== */
.portfolio-summary {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
    padding: 24px;
    background: var(--gray-50);
    border-radius: var(--border-radius);
}

.portfolio-stat {
    text-align: center;
}

.portfolio-stat .label {
    font-size: 13px;
    color: var(--gray-700);
    margin-bottom: 4px;
}

.portfolio-stat .value {
    font-size: 24px;
    font-weight: 700;
    color: var(--dark);
}

.portfolio-stat .value.positive {
    color: var(--success);
}

.portfolio-stat .value.negative {
    color: var(--danger);
}

/* ===== TOOLTIPS ===== */
.tooltip-container {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    cursor: help;
}

.tooltip-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    background-color: var(--gray-300);
    color: var(--gray-700);
    border-radius: 50%;
    font-size: 11px;
    font-weight: 700;
    transition: var(--transition);
    flex-shrink: 0;
    /* Prevent shrinking */
}

.tooltip-container:hover .tooltip-icon {
    background-color: var(--primary);
    color: var(--white);
}

.tooltip-text {
    visibility: hidden;
    width: 240px;
    background-color: var(--dark);
    color: var(--white);
    text-align: center;
    border-radius: var(--border-radius);
    padding: 8px 12px;
    position: absolute;
    z-index: 1000;
    bottom: 125%;
    left: 50%;
    transform: translateX(-50%);
    /* Start centered */
    opacity: 0;
    transition: opacity 0.2s;
    font-size: 12px;
    font-weight: 400;
    line-height: 1.4;
    box-shadow: var(--shadow-md);
    pointer-events: none;
    text-transform: none;
}

.tooltip-text::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: var(--dark) transparent transparent transparent;
}

.tooltip-container:hover .tooltip-text {
    visibility: visible;
    opacity: 1;
}

/* Tooltip Right Variation */
.tooltip-right .tooltip-text {
    left: 110%;
    bottom: auto;
    top: 50%;
    transform: translateY(-50%);
}

.tooltip-right .tooltip-text::after {
    top: 50%;
    right: 100%;
    left: auto;
    margin-top: -5px;
    margin-left: 0;
    border-color: transparent var(--dark) transparent transparent;
}

/* ===== MOBILE OPTIMIZATIONS ===== */
@media (max-width: 768px) {

    /* Mobile Tooltips (Toast Style) */
    .tooltip-text {
        position: fixed;
        width: 90%;
        bottom: 20px;
        left: 50%;
        top: auto;
        transform: translateX(-50%);
        z-index: 2000;
        /* Ensure above modal */
        text-align: center;
        border-radius: 8px;
        padding: 12px 16px;
        font-size: 13px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .tooltip-text::after {
        display: none;
        /* Remove arrow on mobile */
    }

    .tooltip-right .tooltip-text {
        left: 50%;
        bottom: 20px;
        top: auto;
        transform: translateX(-50%);
    }

    /* Scrollable Tabs */
    .tabs {
        overflow-x: auto;
        white-space: nowrap;
        padding-bottom: 4px;
        /* Space for scrollbar */
        -webkit-overflow-scrolling: touch;
    }

    .tab-btn {
        flex-shrink: 0;
        padding: 12px 16px;
    }

    /* Modal Adjustments */
    .modal-content {
        width: 95%;
        max-height: 85vh;
        margin: 10px;
    }

    .modal-header {
        padding: 16px;
    }

    .modal-body {
        padding: 16px;
    }

    .fund-stats {
        gap: 12px;
    }

    .chart-container {
        height: 250px;
    }

    .hide-mobile {
        display: none !important;
    }

    .desktop-only {
        display: none !important;
    }

    .mobile-only {
        display: inline !important;
    }
}

/* Default state (Desktop) */
.mobile-only {
    display: none;
}

/* ===== PROMPT CONTAINER ===== */
.perplexity-section {
    background: var(--gray-50);
    padding: 20px;
    border-radius: var(--border-radius);
    border: 1px solid var(--gray-300);
}

[data-theme="dark"] .perplexity-section {
    background: var(--gray-100);
    /* Darker background for dark mode */
    border-color: var(--gray-700);
}

.prompt-container {
    margin-top: 16px;
    animation: fadeIn 0.3s ease;
}

.prompt-label {
    font-size: 14px;
    font-weight: 600;
    color: var(--gray-700);
    margin-bottom: 8px;
}

.prompt-box {
    background: var(--white);
    padding: 12px;
    border-radius: var(--border-radius);
    border: 1px solid var(--gray-300);
    max-height: 150px;
    overflow-y: auto;
    font-size: 13px;
    font-family: monospace;
    color: var(--dark);
    white-space: pre-wrap;
    /* Preserve line breaks */
}

[data-theme="dark"] .prompt-box {
    background: #2d2d30;
    border-color: var(--gray-700);
}

.btn-success {
    background-color: var(--success) !important;
    color: var(--white) !important;
    border-color: var(--success) !important;
}