/* Comparison Panel - 4th Panel System */

/* Panel Structure - CSS Grid Integration */
.comparison-panel {
    grid-area: comparisons;
    background: var(--color-bg-secondary, #1a1a1a);
    border-right: 1px solid var(--color-border, #333);
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
    position: relative;
    width: 12px; /* Default minimized width - acts as border */
    display: flex;
}

/* Authentication-based visibility control */
.comparison-panel.auth-hidden {
    display: none !important;
}

/* Panel States */
.comparison-panel.minimized {
    width: 12px;
    cursor: pointer; /* Entire thin bar is clickable */
}

.comparison-panel.expanded {
    width: 350px;
    cursor: default;
}

/* Toggle Bar - Acts as left border when expanded */
.comparison-header {
    width: 12px;
    background: var(--color-bg-secondary, #1a1a1a);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%; /* Fill available grid height */
}

/* Subtle breathing animation for thin bar when minimized */
.comparison-panel.minimized .comparison-header {
    animation: subtleBreathing 3s ease-in-out infinite;
}

@keyframes subtleBreathing {
    0%, 100% { 
        box-shadow: 0 0 0 rgba(0, 122, 255, 0);
        background: var(--color-bg-secondary, #1a1a1a);
    }
    50% { 
        box-shadow: 0 0 6px rgba(0, 122, 255, 0.15);
        background: rgba(0, 122, 255, 0.02);
    }
}

/* Content Area - Only visible when expanded */
.comparison-content {
    flex: 1;
    background: var(--color-bg-secondary, #1a1a1a);
    display: none; /* Hidden by default */
    height: 100%; /* Fill available grid height */
    overflow-y: auto; /* Scroll if content overflows */
}

.comparison-panel.expanded .comparison-content {
    display: flex;
    flex-direction: column;
}

.comparison-toggle-btn {
    background: transparent;
    border: none;
    font-size: 10px;
    color: var(--color-accent, #007aff);
    cursor: pointer;
    padding: 0;
    border-radius: 0;
    transition: all 0.2s ease;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.8;
    writing-mode: vertical-lr;
    text-orientation: mixed;
}

.comparison-toggle-btn:hover {
    background: var(--color-bg-tertiary, rgba(255,255,255,0.08));
    color: var(--color-accent-bright, #40a9ff);
    opacity: 1;
    text-shadow: 0 0 8px var(--color-accent, #007aff);
}

.toggle-icon {
    font-size: 14px;
    font-weight: bold;
    line-height: 1;
    font-family: 'Lucida Console', 'Courier New', monospace;
    transform: rotate(90deg); /* Always vertical in the thin bar */
    text-shadow: 0 0 4px rgba(0, 122, 255, 0.3); /* Subtle base glow */
}

/* Expanded Content Area Styling */
.comparison-panel.expanded .comparison-content {
    padding: 0;
    background: var(--color-bg-secondary, #1a1a1a);
    border-left: 1px solid var(--color-border, #444);
}

/* Panel Header with Title and Collapse Button */
.comparison-panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid var(--color-border, #333);
    background: var(--color-bg-secondary, #1a1a1a);
}

.comparison-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--color-text-primary, #fff);
    font-family: inherit; /* Match other panel fonts */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
}

.comparison-collapse-btn {
    background: transparent;
    border: none;
    color: var(--color-text-secondary, #999);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 2px;
    font-size: 16px;
    font-weight: normal;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    height: 24px;
}

.comparison-collapse-btn:hover {
    background: var(--color-bg-tertiary, rgba(255,255,255,0.08));
    color: var(--color-accent, #007aff);
    transform: translateX(2px);
}

.comparison-refresh-btn {
    background: transparent;
    border: none;
    color: var(--color-text-secondary, #999);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 2px;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    height: 24px;
    margin-right: 8px;
}

.comparison-refresh-btn:hover {
    background: var(--color-bg-tertiary, rgba(255,255,255,0.08));
    color: var(--color-accent, #007aff);
    transform: rotate(180deg);
}

.comparison-refresh-btn svg {
    transition: inherit;
}

.comparison-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 200px;
    color: var(--text-secondary, #666);
}

.loading-spinner {
    width: 24px;
    height: 24px;
    border: 2px solid var(--color-border, #333);
    border-top: 2px solid var(--color-accent, #007aff);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 12px;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Ensure loading spinner is visible in mobile */
#panel4 .loading-spinner,
.mobile-comparison .loading-spinner,
.comparison-placeholder .loading-spinner {
    width: 24px;
    height: 24px;
    border: 2px solid var(--color-border, #333);
    border-top: 2px solid var(--color-accent, #007aff);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 16px;
}

/* Tab System Styling */
.comparison-tabs {
    background: var(--color-bg-secondary, #1a1a1a);
}

.tab-headers {
    display: flex;
    border-bottom: 1px solid var(--color-border, #333);
    background: var(--color-bg-secondary, #1a1a1a);
}

.tab-header {
    flex: 1;
    padding: 12px 16px;
    cursor: pointer;
    font-size: 14px; /* Increased from 12px */
    font-weight: 500;
    font-family: inherit; /* Match other panel fonts */
    color: var(--color-text-secondary, #999);
    border-bottom: 2px solid transparent;
    background: transparent;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-align: center;
    border: none;
    outline: none;
}

.tab-header:hover {
    background: var(--color-bg-tertiary, rgba(255,255,255,0.05));
    color: var(--color-text-primary, #fff);
}

.tab-header.active {
    color: var(--color-accent, #007aff);
    font-weight: 600;
    border-bottom-color: var(--color-accent, #007aff);
    background: rgba(0, 122, 255, 0.05);
}

.tab-content-area {
    padding: 16px;
    background: var(--color-bg-secondary, #1a1a1a);
}

.tab-content {
    display: none;
    animation: fadeInUp 0.3s ease;
}

.tab-content.active {
    display: block;
}

/* Section Collapse/Expand Animation */
.collapsible-section {
    margin-bottom: 8px;
    border-radius: 6px;
    overflow: hidden;
    border: 1px solid var(--color-border, #333);
}

.section-header {
    padding: 12px 16px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--color-bg-tertiary, rgba(255,255,255,0.03));
    border-left: 3px solid transparent;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    font-family: inherit; /* Match other panel fonts */
}

.section-header:hover {
    background: var(--color-bg-tertiary, rgba(255,255,255,0.08));
}

.section-header.expanded {
    border-left-color: var(--color-accent, #007aff);
    background: rgba(0, 122, 255, 0.08);
}

.section-header h3 {
    color: var(--color-text-primary, #fff);
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    font-family: inherit; /* Match other panel fonts */
}

.section-arrow {
    color: var(--color-accent, #007aff);
    font-size: 12px;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    font-weight: bold;
}

.section-content {
    background: var(--color-bg-secondary, #1a1a1a);
    border-left: 3px solid transparent;
    max-height: 0;
    overflow: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    opacity: 0;
}

.section-content.expanded {
    max-height: 2000px; /* Large enough for content */
    padding: 16px;
    border-left-color: var(--color-accent, #007aff);
    opacity: 1;
}

/* Calculation Item Animations */
.compact-calc-item {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.calc-details-expanded {
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.calc-details-expanded.expanded {
    max-height: 200px; /* Enough for content */
    opacity: 1;
}

/* Load button fade animation */
.calc-load-btn {
    opacity: 1;
    transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.calc-load-btn.hidden {
    opacity: 0;
    pointer-events: none;
}

/* Arrow rotation animation */
.expand-arrow {
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Animations */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideDown {
    from {
        max-height: 0;
        padding: 0;
        opacity: 0;
    }
    to {
        max-height: 2000px;
        padding: 16px;
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        max-height: 2000px;
        padding: 16px;
        opacity: 1;
    }
    to {
        max-height: 0;
        padding: 0;
        opacity: 0;
    }
}

/* Mobile - Integrated with mobile panel navigation */
@media (max-width: 768px) {
    /* When comparison panel is moved into mobile navigation system */
    .comparison-panel.mobile-integrated {
        display: block !important;
        position: static !important;
        width: 100% !important;
        height: 100% !important;
        padding: 0 !important; /* Remove padding for full screen utilization */
        margin: 0 !important; /* Remove any margins */
        overflow-y: auto;
        background: var(--color-bg-primary, #000);
        grid-area: unset !important;
        border: none !important;
        /* Remove desktop toggle functionality */
        transition: none !important;
        box-sizing: border-box;
    }
    
    /* Force content to show on mobile regardless of expanded state */
    .comparison-panel.mobile-integrated .comparison-content {
        display: flex !important;
        flex-direction: column !important;
        padding: 0 !important; /* Remove content padding */
        margin: 0 !important;
        width: 100% !important;
        height: 100% !important;
    }
    
    /* Hide the toggle button on mobile since we don't need it */
    .comparison-panel.mobile-integrated .comparison-header {
        display: none !important;
    }
    
    /* Mobile panel adjustments - hide redundant header */
    .comparison-panel.mobile-integrated .comparison-panel-header {
        display: none !important; /* Hide entire header section on mobile */
    }
    
    .comparison-panel.mobile-integrated .comparison-collapse-btn {
        display: none; /* Hide collapse button on mobile */
    }
    
    /* Mobile tabs - simplified for integrated architecture */
    .comparison-panel.mobile-integrated .comparison-tabs {
        background: var(--color-bg-secondary, #1a1a1a);
        margin-bottom: 0 !important; /* Remove margin for seamless design */
        width: 100%;
    }
    
    .comparison-panel.mobile-integrated .tab-headers {
        display: flex !important;
        border-bottom: 1px solid var(--color-border, #333);
        margin-bottom: 0;
    }
    
    .comparison-panel.mobile-integrated .tab-header {
        flex: 1;
        padding: 12px 20px;
        font-size: 16px;
        cursor: pointer;
        border: none;
        background: transparent;
        color: var(--color-text-secondary, #999);
        border-bottom: 2px solid transparent;
        transition: all 0.3s ease;
    }
    
    .comparison-panel.mobile-integrated .tab-header:hover {
        background: var(--color-bg-tertiary, rgba(255,255,255,0.05));
        color: var(--color-text-primary, #fff);
    }
    
    .comparison-panel.mobile-integrated .tab-header.active {
        color: var(--color-accent, #007aff);
        border-bottom-color: var(--color-accent, #007aff);
        background: rgba(0, 122, 255, 0.05);
        font-weight: 600;
    }
    
    /* Mobile tab content */
    #panel4 .tab-content-area,
    .mobile-comparison .tab-content-area {
        padding: 0 !important;
        width: 100%;
        height: 100%;
    }
    
    #panel4 .tab-content,
    .mobile-comparison .tab-content {
        display: none;
        padding: 16px; /* Internal padding for content readability */
        min-height: 200px; /* Ensure minimum height */
        box-sizing: border-box;
    }
    
    #panel4 .tab-content.active,
    .mobile-comparison .tab-content.active {
        display: block !important;
    }
    
    /* Make charts mobile-friendly - Higher specificity */
    #panel4 .scatter-plot,
    #panel4 .tax-rate-lines,
    #panel4 .horizontal-chart,
    .mobile-comparison .scatter-plot,
    .mobile-comparison .tax-rate-lines,
    .mobile-comparison .horizontal-chart {
        height: 180px !important; /* Compact height for mobile */
        margin-bottom: 8px !important; /* Minimal spacing */
        min-height: 180px !important;
        overflow: visible;
        padding: 8px !important; /* Minimal internal padding */
        box-sizing: border-box; /* Include padding in height calculation */
        width: 100% !important; /* Full width on mobile */
        max-width: 100% !important;
        position: relative;
        clear: both; /* Prevent floating overlap */
    }
    
    /* Ensure charts don't overlap by adding container spacing */
    #panel4 .collapsible-section,
    .mobile-comparison .collapsible-section {
        margin-bottom: 6px !important; /* Minimal space between sections */
        border-radius: 8px;
        background: var(--color-bg-tertiary, rgba(255, 255, 255, 0.02));
        padding-bottom: 2px !important; /* Minimal internal bottom padding */
    }
    
    /* Mobile section content expanded state */
    #panel4 .section-content.expanded,
    .mobile-comparison .section-content.expanded {
        max-height: none !important; /* Remove height restrictions */
        padding: 12px !important; /* Compact padding */
        margin-bottom: 4px !important; /* Minimal bottom margin */
        overflow-y: visible !important; /* Ensure charts are fully visible */
    }
    
    /* Ensure chart content areas stay within container bounds on mobile */
    #panel4 .scatter-plot > div:first-of-type,
    #panel4 .tax-rate-lines > div:first-of-type,
    .mobile-comparison .scatter-plot > div:first-of-type,
    .mobile-comparison .tax-rate-lines > div:first-of-type {
        /* Override hardcoded positioning to fit mobile container */
        left: 40px !important; /* Reduced from 50px to fit 8px padding */
        right: 12px !important; /* Adjusted right margin */
        top: 12px !important; /* Adjusted for mobile height */
        bottom: 35px !important; /* Adjusted bottom margin */
    }
    
    /* Mobile Y-axis label positioning improvements */
    #panel4 .scatter-plot div[style*="left: 2px"][style*="rotate(-90deg)"],
    #panel4 .tax-rate-lines div[style*="left: 2px"][style*="rotate(-90deg)"],
    .mobile-comparison .scatter-plot div[style*="left: 2px"][style*="rotate(-90deg)"],
    .mobile-comparison .tax-rate-lines div[style*="left: 2px"][style*="rotate(-90deg)"] {
        left: 1px !important; /* Move even closer to edge on mobile */
        font-size: 9px !important; /* Smaller font for mobile */
        transform: translateY(-50%) rotate(-90deg) !important;
        transform-origin: center center !important;
    }
    
    /* Mobile Y-axis values positioning adjustments */
    #panel4 .scatter-plot div[style*="left: 20px"],
    #panel4 .scatter-plot div[style*="left: 18px"],
    #panel4 .tax-rate-lines div[style*="left: 20px"],
    #panel4 .tax-rate-lines div[style*="left: 18px"],
    .mobile-comparison .scatter-plot div[style*="left: 20px"],
    .mobile-comparison .scatter-plot div[style*="left: 18px"],
    .mobile-comparison .tax-rate-lines div[style*="left: 20px"],
    .mobile-comparison .tax-rate-lines div[style*="left: 18px"] {
        left: 15px !important; /* Move Y-axis values slightly inward on mobile */
        font-size: 8px !important; /* Smaller font for mobile axis values */
    }
    
    /* Horizontal chart content area - different structure */
    #panel4 .horizontal-chart .chart-content-area,
    .mobile-comparison .horizontal-chart .chart-content-area {
        height: 160px !important; /* Compact height for main chart area only */
        position: relative;
        width: 100% !important;
        overflow: visible !important; /* Prevent clipping */
    }
    
    /* Ensure chart legends display at natural height */
    #panel4 .chart-legends,
    .mobile-comparison .chart-legends {
        height: auto !important; /* Natural height for legends */
        min-height: auto !important;
        max-height: none !important;
    }
    
    /* Mobile chart text sizing */
    #panel4 .scatter-plot [style*="font-size"],
    #panel4 .tax-rate-lines [style*="font-size"],
    #panel4 .horizontal-chart [style*="font-size"],
    .mobile-comparison .scatter-plot [style*="font-size"],
    .mobile-comparison .tax-rate-lines [style*="font-size"],
    .mobile-comparison .horizontal-chart [style*="font-size"] {
        font-size: 12px !important;
    }
    
    /* Mobile chart responsive adjustments */
    #panel4 .quick-stats,
    .mobile-comparison .quick-stats {
        grid-template-columns: 1fr 1fr !important;
        gap: 12px;
        font-size: 12px;
    }
    
    /* Chart row spacing - mobile responsive */
    #panel4 .chart-row,
    .mobile-comparison .chart-row {
        margin-bottom: 2px !important; /* Compact mobile spacing */
    }
    
    #panel4 .calc-label,
    .mobile-comparison .calc-label {
        font-size: 11px !important;
        color: var(--color-text-secondary, #999) !important;
        margin-bottom: 1px !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    #panel4 .bars-container,
    .mobile-comparison .bars-container {
        margin-bottom: 1px !important;
    }
    
    #panel4 .bar-row,
    .mobile-comparison .bar-row {
        margin-bottom: 1px !important;
        display: flex !important; /* Mobile: flex layout puts percentage to right of bar */
        align-items: center !important;
        gap: 4px !important;
    }
    
    #panel4 .tax-percentage,
    .mobile-comparison .tax-percentage {
        display: inline !important; /* Mobile: Keep percentage inline with bar */
        margin-top: 0 !important;
    }
    
    #panel4 .composition-row,
    .mobile-comparison .composition-row {
        margin-bottom: 4px !important; /* Compact composition chart spacing */
    }
    
    /* Bar chart visual styles - default desktop layout */
    .chart-row {
        margin-bottom: 8px;
    }
    
    .calc-label {
        font-size: 11px;
        color: var(--color-text-secondary, #999);
        margin-bottom: 2px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .bars-container {
        margin-bottom: 2px;
    }
    
    .bar-row {
        margin-bottom: 3px;
        /* Inline styles in JavaScript handle the flex layout */
    }
    
    .take-home-bar {
        height: 16px;
        background: var(--color-accent, #007aff);
        border-radius: 3px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        margin-bottom: 2px;
    }
    
    .tax-rate-bar {
        height: 16px;
        background: var(--color-warning, #ff6384);
        border-radius: 3px;
        /* Inline styles in JavaScript handle the display properties */
    }
    
    .composition-row {
        margin-bottom: 12px;
    }
    
    #panel4 .stat-item,
    .mobile-comparison .stat-item {
        padding: 12px !important;
        text-align: center;
    }
    
    /* Adjust mobile section content */
    #panel4 .section-content.expanded,
    .mobile-comparison .section-content.expanded {
        max-height: none; /* Remove height restrictions on mobile */
        padding: 16px;
    }
    
    /* Mobile section headers */
    #panel4 .section-header,
    .mobile-comparison .section-header {
        padding: 16px;
        font-size: 16px;
    }
    
    /* Mobile insights */
    #panel4 .insight-item,
    .mobile-comparison .insight-item {
        padding: 16px;
        margin-bottom: 16px;
    }
    
    /* Mobile calculations list */
    #panel4 .compact-calc-item,
    .mobile-comparison .compact-calc-item {
        margin-bottom: 12px;
        border-radius: 8px;
    }
}

/* Chart Modal Styles */
.chart-modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    backdrop-filter: blur(4px);
}

.chart-modal-backdrop.show {
    opacity: 1;
    visibility: visible;
}

.chart-modal-container {
    background: var(--color-bg-primary, #000);
    border: 1px solid var(--color-border, #333);
    border-radius: 12px;
    max-width: 90vw;
    max-height: 85vh;
    width: 900px;
    height: auto;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    transform: scale(0.9);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chart-modal-backdrop.show .chart-modal-container {
    transform: scale(1);
}

.chart-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--color-border, #333);
    background: var(--color-bg-secondary, #1a1a1a);
    border-radius: 12px 12px 0 0;
}

.chart-modal-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--color-text-primary, #fff);
    margin: 0;
}

.chart-modal-close {
    background: transparent;
    border: none;
    color: var(--color-text-secondary, #999);
    font-size: 24px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    transition: all 0.2s ease;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
}

.chart-modal-close:hover {
    background: var(--color-bg-tertiary, rgba(255,255,255,0.08));
    color: var(--color-text-primary, #fff);
    transform: rotate(90deg);
}

.chart-modal-content {
    padding: 20px 24px;
    overflow: auto;
    flex: 1;
    background: var(--color-bg-primary, #000);
    border-radius: 0 0 12px 12px;
}

/* Chart Expand Button Styles */
.chart-expand-btn {
    opacity: 0.3;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chart-expand-btn:hover {
    background: rgba(0, 122, 255, 0.8) !important;
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 122, 255, 0.3);
    opacity: 1;
}

/* Show expand button on container hover */
.scatter-plot:hover .chart-expand-btn,
.tax-rate-lines:hover .chart-expand-btn,
.horizontal-chart:hover .chart-expand-btn {
    opacity: 1;
}

/* Mobile Modal Adjustments */
@media (max-width: 768px) {
    .chart-modal-container {
        width: 95vw;
        height: 80vh;
        max-width: none;
        max-height: 85vh;
        border-radius: 8px;
    }
    
    .chart-modal-header {
        padding: 16px 20px 12px;
        border-radius: 8px 8px 0 0;
    }
    
    .chart-modal-title {
        font-size: 16px;
    }
    
    .chart-modal-close {
        font-size: 20px;
        min-width: 28px;
        height: 28px;
    }
    
    .chart-modal-content {
        padding: 16px 20px;
        border-radius: 0 0 8px 8px;
    }
    
    /* Always show expand button on mobile - more visible for touch */
    .chart-expand-btn {
        opacity: 0.7;
    }
    
    .chart-expand-btn:hover {
        opacity: 1;
    }
}