/* ===================================
   Analytics Page Specific Styles
   =================================== */

/* Analytics Header */
.analytics-header {
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-xl);
    padding: var(--space-xl);
    margin-bottom: var(--space-xl);
    box-shadow: var(--shadow-md);
}

.analytics-header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-lg);
}

.analytics-header h1 {
    color: var(--color-text-primary);
    font-size: clamp(1.75rem, 3vw, 2.25rem);
    font-weight: var(--font-weight-bold);
    margin: 0;
    letter-spacing: -0.02em;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    flex-wrap: wrap;
}

/* Time Range Selector */
.time-range-selector {
    display: inline-flex;
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-md);
    padding: var(--space-xs);
    gap: var(--space-xs);
}

.time-range-selector button {
    background: transparent;
    border: none;
    color: var(--color-text-secondary);
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-sm);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-medium);
    font-family: var(--font-family-base);
    cursor: pointer;
    transition: all var(--transition-fast);
    white-space: nowrap;
}

.time-range-selector button:hover {
    color: var(--color-text-primary);
    background: var(--color-bg-hover);
}

.time-range-selector button.active {
    background: var(--color-primary);
    color: var(--color-bg-base);
}

/* Metrics Row */
.metrics-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: var(--space-lg);
    margin-bottom: var(--space-xl);
}

.metric-card {
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
    box-shadow: var(--shadow-sm);
    transition: all var(--transition-base);
    position: relative;
    overflow: hidden;
}

.metric-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: linear-gradient(90deg, var(--color-primary), var(--color-secondary));
    opacity: 0;
    transition: opacity var(--transition-base);
}

.metric-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: var(--color-border-emphasis);
}

.metric-card:hover::before {
    opacity: 1;
}

.metric-label {
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-tertiary);
}

.metric-value {
    font-size: clamp(1.5rem, 2.5vw, 2rem);
    font-weight: var(--font-weight-bold);
    font-variant-numeric: tabular-nums;
    color: var(--color-text-primary);
    line-height: var(--line-height-tight);
}

.metric-change {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-secondary);
}

/* Analytics Grid */
.analytics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: var(--space-xl);
    width: 100%;
}

.chart-container {
    background: var(--color-bg-surface);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-xl);
    padding: var(--space-xl);
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
}

.span-full {
    grid-column: 1 / -1;
}

/* Chart Header */
.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-lg);
}

.chart-header h3 {
    color: var(--color-text-primary);
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    margin: 0;
}

.chart-action {
    background: transparent;
    border: 1px solid var(--color-border-subtle);
    color: var(--color-text-secondary);
    padding: var(--space-sm);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}

.chart-action:hover {
    background: var(--color-bg-elevated);
    color: var(--color-text-primary);
    border-color: var(--color-border-emphasis);
}

/* Chart Canvas */
.chart-canvas-wrapper {
    flex: 1;
    position: relative;
    min-height: 300px;
}

.chart-canvas-wrapper canvas {
    max-height: 400px;
    width: 100% !important;
    height: auto !important;
}

/* Chart Legend */
.chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-md);
    margin-top: var(--space-lg);
    padding-top: var(--space-lg);
    border-top: 1px solid var(--color-border-subtle);
}

.legend-item {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    font-size: var(--font-size-sm);
}

.legend-color {
    width: 16px;
    height: 16px;
    border-radius: 4px;
}

.legend-label {
    color: var(--color-text-secondary);
    font-weight: var(--font-weight-medium);
}

.legend-value {
    color: var(--color-text-primary);
    font-weight: var(--font-weight-semibold);
    margin-left: var(--space-xs);
}

/* Performers Table */
.performers-table-wrapper {
    overflow-x: auto;
}

.performers-table {
    width: 100%;
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
}

.performers-table th {
    background: transparent;
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-xs);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-tertiary);
    padding: var(--space-md);
    text-align: left;
    border-bottom: 1px solid var(--color-border-subtle);
}

.performers-table th.text-right {
    text-align: right;
}

.performers-table td {
    padding: var(--space-md);
    border-bottom: 1px solid rgba(41, 53, 72, 0.3);
    color: var(--color-text-secondary);
    font-size: var(--font-size-sm);
}

.performers-table td.text-right {
    text-align: right;
}

.performers-table tbody tr {
    transition: background-color var(--transition-fast);
}

.performers-table tbody tr:hover {
    background: rgba(30, 39, 64, 0.5);
}

.performers-table tbody tr:last-child td {
    border-bottom: none;
}

/* Rank Badge */
.rank-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border-subtle);
    font-weight: var(--font-weight-bold);
    font-size: var(--font-size-xs);
    color: var(--color-text-primary);
}

.rank-badge.top-1 {
    background: linear-gradient(135deg, #FFD700, #FFA500);
    border-color: #FFD700;
    color: var(--color-bg-base);
}

.rank-badge.top-2 {
    background: linear-gradient(135deg, #C0C0C0, #A8A8A8);
    border-color: #C0C0C0;
    color: var(--color-bg-base);
}

.rank-badge.top-3 {
    background: linear-gradient(135deg, #CD7F32, #B87333);
    border-color: #CD7F32;
    color: white;
}

/* Performance Bar */
.performance-bar-wrapper {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    min-width: 100px;
}

.performance-bar {
    flex: 1;
    height: 8px;
    background: var(--color-bg-elevated);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.performance-bar-fill {
    height: 100%;
    border-radius: var(--radius-full);
    transition: width var(--transition-slow);
}

.performance-bar-fill.gain {
    background: linear-gradient(90deg, var(--color-gain), #00E5BF);
}

.performance-bar-fill.loss {
    background: linear-gradient(90deg, var(--color-loss), #FF8A80);
}

/* Risk Metrics */
.risk-metrics-grid {
    display: flex;
    flex-direction: column;
    gap: var(--space-xl);
}

.risk-metric {
    display: flex;
    flex-direction: column;
    gap: var(--space-sm);
}

.risk-label {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-secondary);
}

.risk-gauge {
    height: 32px;
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-full);
    overflow: hidden;
    position: relative;
}

.risk-gauge-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--color-primary), var(--color-secondary));
    border-radius: var(--radius-full);
    transition: width var(--transition-slow);
}

.risk-gauge.danger .risk-gauge-fill {
    background: linear-gradient(90deg, var(--color-danger), #FF8A80);
}

.risk-value {
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-bold);
    color: var(--color-text-primary);
    font-variant-numeric: tabular-nums;
}

/* Analytics Footer */
.analytics-footer {
    margin-top: var(--space-2xl);
    padding-top: var(--space-xl);
    border-top: 1px solid var(--color-border-subtle);
}

.last-updated {
    text-align: center;
    margin-bottom: var(--space-md);
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
}

.last-updated-label {
    font-weight: var(--font-weight-semibold);
    color: var(--color-text-tertiary);
    margin-right: var(--space-xs);
}

.last-updated-time {
    font-variant-numeric: tabular-nums;
    color: var(--color-text-primary);
    font-weight: var(--font-weight-medium);
}

.analytics-note {
    text-align: center;
    color: var(--color-text-tertiary);
    font-size: var(--font-size-sm);
    font-style: italic;
}

/* Export Button */
.export-btn {
    white-space: nowrap;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .analytics-grid {
        grid-template-columns: 1fr;
    }

    .span-full {
        grid-column: 1;
    }
}

@media (max-width: 768px) {
    .analytics-header-content {
        flex-direction: column;
        align-items: stretch;
    }

    .header-controls {
        flex-direction: column;
        width: 100%;
    }

    .time-range-selector {
        width: 100%;
        overflow-x: auto;
    }

    .export-btn {
        width: 100%;
    }

    .metrics-row {
        grid-template-columns: 1fr;
    }

    .chart-canvas-wrapper {
        min-height: 250px;
    }

    .performers-table {
        font-size: var(--font-size-xs);
    }

    .performers-table th,
    .performers-table td {
        padding: var(--space-sm);
    }
}

@media (max-width: 480px) {
    .chart-container {
        padding: var(--space-md);
    }

    .metric-card {
        padding: var(--space-lg);
    }

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

    .time-range-selector button {
        padding: var(--space-xs) var(--space-sm);
        font-size: 0.7rem;
    }
}

/* Sold Stocks Performance */
.sold-stocks-metrics {
    display: flex;
    flex-direction: column;
    gap: var(--space-lg);
}

.sold-metric {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);
    padding: var(--space-md);
    background: var(--color-bg-elevated);
    border: 1px solid var(--color-border-subtle);
    border-radius: var(--radius-md);
    transition: all var(--transition-fast);
}

.sold-metric:hover {
    border-color: var(--color-border-emphasis);
    box-shadow: var(--shadow-sm);
}

.sold-metric-label {
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-tertiary);
}

.sold-metric-value {
    font-size: var(--font-size-xl);
    font-weight: var(--font-weight-bold);
    font-variant-numeric: tabular-nums;
    color: var(--color-text-primary);
    line-height: var(--line-height-tight);
}

.sold-metric-change {
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-semibold);
}

.sold-metric-detail {
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    font-weight: var(--font-weight-medium);
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--space-2xl);
    text-align: center;
    color: var(--color-text-tertiary);
}

.empty-state h3 {
    color: var(--color-text-secondary);
    font-size: var(--font-size-lg);
    font-weight: var(--font-weight-semibold);
    margin: 0 0 var(--space-sm) 0;
}

.empty-state p {
    color: var(--color-text-tertiary);
    font-size: var(--font-size-sm);
    margin: 0;
}

/* ===================================
   Analytics Page Responsive Design
   =================================== */

/* Tablet & Mobile (< 1024px) */
@media (max-width: 1023px) {
    .analytics-header {
        padding: var(--space-lg);
    }

    .analytics-header-content {
        flex-direction: column;
        align-items: stretch;
    }

    .header-controls {
        flex-direction: column;
        width: 100%;
    }

    .time-range-selector {
        width: 100%;
        justify-content: space-between;
    }

    .time-range-selector button {
        flex: 1;
        padding: var(--space-sm);
        font-size: 0.75rem;
    }

    /* Metrics row - 2 columns on tablet */
    .metrics-row {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--space-md);
    }

    /* Analytics grid - single column */
    .analytics-grid {
        grid-template-columns: 1fr;
    }

    .chart-container {
        padding: var(--space-lg);
    }

    /* Performers table wrapper */
    .performers-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .performers-table {
        min-width: 700px;
        font-size: var(--font-size-xs);
    }

    .performers-table th,
    .performers-table td {
        padding: var(--space-sm);
    }
}

/* Mobile (< 768px) */
@media (max-width: 767px) {
    .analytics-header {
        padding: var(--space-md);
        margin-bottom: var(--space-lg);
    }

    .analytics-header h1 {
        font-size: 1.5rem;
    }

    /* Hide export button on mobile */
    .export-btn {
        display: none;
    }

    /* Simplify time range selector */
    .time-range-selector {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        justify-content: flex-start;
        gap: var(--space-xs);
    }

    .time-range-selector button {
        min-width: 44px;
        padding: var(--space-xs) var(--space-sm);
        font-size: 0.75rem;
    }

    /* Mobile-optimized metrics - show only priority metrics */
    .metrics-row {
        display: flex;
        flex-direction: column;
        gap: var(--space-sm);
        margin-bottom: var(--space-lg);
        width: 100%;
    }

    /* Hide secondary metrics on mobile */
    .metric-card:nth-child(2), /* Money-Weighted Return */
    .metric-card:nth-child(4), /* Best Performer */
    .metric-card:nth-child(5) { /* Worst Performer */
        display: none;
    }

    /* Enhanced metric cards for mobile */
    .metric-card {
        padding: var(--space-lg);
        border-radius: var(--radius-xl);
        box-shadow: var(--shadow-lg);
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    .metric-label {
        font-size: 0.7rem;
        letter-spacing: 0.08em;
    }

    .metric-value {
        font-size: 1.75rem;
        margin: var(--space-xs) 0;
    }

    .metric-change {
        font-size: var(--font-size-sm);
        font-weight: var(--font-weight-bold);
    }

    /* Chart containers - app-like cards */
    .chart-container {
        padding: var(--space-lg);
        border-radius: var(--radius-xl);
        margin-bottom: var(--space-md);
        box-shadow: var(--shadow-lg);
    }

    .chart-header {
        margin-bottom: var(--space-md);
    }

    .chart-header h3 {
        font-size: 1rem;
        font-weight: var(--font-weight-bold);
    }

    /* Hide chart action buttons on mobile */
    .chart-action {
        display: none;
    }

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

    /* Hide secondary charts on mobile */
    .chart-container:has(#performance-bar),
    .chart-container:has(#distribution-chart) {
        display: none;
    }

    /* Simplify allocation legend on mobile */
    .chart-legend {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: var(--space-sm);
        margin-top: var(--space-md);
        padding-top: var(--space-md);
    }

    .legend-item {
        font-size: 0.75rem;
    }

    .legend-color {
        width: 12px;
        height: 12px;
    }

    /* Hide risk metrics section on mobile */
    .chart-container:has(.risk-metrics-grid) {
        display: none;
    }

    /* Sold stocks - compact view */
    .sold-stocks-metrics {
        gap: var(--space-sm);
    }

    .sold-metric {
        padding: var(--space-md);
    }

    .sold-metric-label {
        font-size: 0.65rem;
    }

    .sold-metric-value {
        font-size: var(--font-size-lg);
    }

    /* Top performers table - show only top 3 on mobile */
    .chart-container:has(.performers-table) {
        padding: var(--space-lg);
    }

    .performers-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .performers-table {
        min-width: 100%;
        font-size: 0.75rem;
    }

    .performers-table th,
    .performers-table td {
        padding: var(--space-sm) var(--space-xs);
    }

    /* Hide rows after 3rd on mobile */
    .performers-table tbody tr:nth-child(n+4) {
        display: none;
    }

    /* Analytics footer - simplified */
    .analytics-footer {
        padding: var(--space-lg) var(--space-md);
        margin-top: var(--space-xl);
        margin-bottom: 80px; /* Space for bottom nav */
    }

    .analytics-note {
        font-size: 0.75rem;
        line-height: 1.5;
        margin-top: var(--space-md);
    }

    .last-updated {
        font-size: 0.75rem;
    }
}

/* Small Mobile (< 480px) */
@media (max-width: 479px) {
    .analytics-header {
        padding: var(--space-sm) var(--space-md);
    }

    .analytics-header h1 {
        font-size: 1.25rem;
    }

    /* Compact time range selector */
    .time-range-selector button {
        min-width: 40px;
        padding: 6px 8px;
        font-size: 0.7rem;
    }

    /* Compact metric cards */
    .metric-card {
        padding: var(--space-md);
        border-radius: var(--radius-lg);
    }

    .metric-label {
        font-size: 0.65rem;
    }

    .metric-value {
        font-size: 1.5rem;
    }

    .metric-change {
        font-size: 0.75rem;
    }

    /* Compact chart containers */
    .chart-container {
        padding: var(--space-md);
        border-radius: var(--radius-lg);
    }

    .chart-header h3 {
        font-size: 0.9rem;
    }

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

    /* Compact allocation legend */
    .chart-legend {
        grid-template-columns: 1fr;
        gap: var(--space-xs);
    }

    .legend-item {
        font-size: 0.7rem;
    }

    /* Compact performers table */
    .performers-table {
        font-size: 0.7rem;
    }

    .performers-table th,
    .performers-table td {
        padding: var(--space-xs);
    }

    /* Hide some table columns on very small screens */
    .performers-table th:nth-child(3), /* Name */
    .performers-table td:nth-child(3),
    .performers-table th:nth-child(6), /* Weight */
    .performers-table td:nth-child(6) {
        display: none;
    }

    /* Compact sold stocks metrics */
    .sold-metric {
        padding: var(--space-sm) var(--space-md);
    }

    .sold-metric-label {
        font-size: 0.6rem;
    }

    .sold-metric-value {
        font-size: 1rem;
    }

    .sold-metric-change,
    .sold-metric-detail {
        font-size: 0.7rem;
    }
}

/* Mobile-specific enhancements */
@media (max-width: 767px) {
    /* Add app-like padding to container */
    .container {
        padding: var(--space-md);
        padding-bottom: 100px; /* Extra space for bottom nav */
        max-width: 100%;
    }

    /* Ensure all cards are consistent width */
    .analytics-header,
    .metric-card,
    .chart-container {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    /* Make charts responsive on mobile */
    .chart-canvas-wrapper canvas {
        max-height: none !important;
        height: 100% !important;
    }

    /* Sold stocks metrics on mobile */
    .sold-stocks-metrics {
        padding: 0;
        width: 100%;
    }

    .sold-metric {
        background: var(--color-bg-elevated);
        border-radius: var(--radius-lg);
        width: 100%;
        box-sizing: border-box;
    }

    /* Add visual hierarchy with larger first metric card */
    .metrics-row .metric-card:first-child {
        background: linear-gradient(135deg, rgba(0, 201, 167, 0.15), rgba(132, 94, 247, 0.15));
        border: 2px solid var(--color-primary);
    }

    .metrics-row .metric-card:first-child .metric-value {
        font-size: 2rem;
        color: var(--color-primary);
    }

    /* Improve analytics grid spacing - force single column */
    .analytics-grid {
        display: flex;
        flex-direction: column;
        gap: var(--space-md);
        width: 100%;
    }

    /* Override span-full on mobile since we're single column */
    .span-full {
        grid-column: auto;
    }

    /* Add gradient overlay to header for depth */
    .analytics-header {
        background: linear-gradient(135deg, var(--color-bg-surface), rgba(132, 94, 247, 0.05));
        border: 1px solid rgba(132, 94, 247, 0.2);
    }
}
