/* Task 1.2: CSS Foundation & Variables */
:root {
    /* Color Scheme */
    --bg-primary: #0b0e11;
    --bg-secondary: #1a1d25;
    --bg-panel: #2b3139;
    --border-color: #3a3f4a;
    --accent-color: #f0b90b;
    --success-color: #0ecb81;
    --error-color: #f6465d;
    --text-primary: #f0f0f0;
    --text-secondary: #999;

    /* Spacing - Increased for better touch targets */
    --padding-sm: 10px;
    --padding-md: 16px;
    --padding-lg: 24px;

    /* Transitions */
    --transition-fast: 0.2s ease;
    --transition-slow: 0.3s ease;

    /* Font */
    --font-family: "Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif;
}

/* Global Reset & Base Typography */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 18px; /* Increased from 16px */
    /* Mobile Chrome viewport fix */
    height: 100%;
    height: 100dvh; /* Dynamic viewport height for newer browsers */
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    overflow: hidden; /* Prevent body scroll */
    /* Mobile Chrome viewport fix */
    height: 100%;
    height: 100dvh; /* Dynamic viewport height for newer browsers */
    /* Safe area handling for notched devices */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
    background-color: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--text-secondary);
}

::-webkit-scrollbar-corner {
    background-color: var(--bg-secondary);
}

/* Task 2: Desktop Container Layout */
.desktop-container {
    display: grid;
    grid-template-columns: 1fr 420px;
    grid-template-areas: "chart coins";
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile browsers */
    width: 100vw;
    width: 100dvw; /* Dynamic viewport width for mobile browsers */
    gap: 1px; /* Minimal gap between panels */
    background-color: var(--border-color); /* Gap background */
    /* Additional mobile fixes */
    min-height: 100vh;
    min-height: 100dvh;
    position: relative;
}

/* Chart Panel - Takes remaining space */
.chart-panel {
    grid-area: chart;
    background-color: var(--bg-primary);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

/* Coins/Alerts Panel - Fixed width on the right */
.coins-panel {
    grid-area: coins;
    background-color: var(--bg-secondary);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    /* Allow normal touch interactions */
    touch-action: auto;
}

/* Mobile Layout - Stack chart on top, tabs below */
@media screen and (max-width: 768px) {
    .desktop-container {
        grid-template-columns: 1fr;
        grid-template-rows: 1fr auto;
        grid-template-areas: 
            "chart"
            "coins";
        gap: 0;
        /* Improved mobile viewport handling */
        height: 100vh;
        height: 100dvh;
        min-height: -webkit-fill-available; /* Safari mobile fix */
        min-height: 100dvh;
    }
    
    .chart-panel {
        min-height: 60vh;
        min-height: 60dvh;
        max-height: 60vh;
        max-height: 60dvh;
        /* Additional Safari fixes */
        height: -webkit-fill-available;
    }
    
    .coins-panel {
        height: 40vh;
        height: 40dvh;
        max-height: 40vh;
        max-height: 40dvh;
        border-top: 1px solid var(--border-color);
        flex-shrink: 0;
    }
}

/* Smaller mobile phones */
@media screen and (max-width: 480px) {
    .chart-panel {
        min-height: 55vh;
        min-height: 55dvh;
        max-height: 55vh;
        max-height: 55dvh;
    }
    
    .coins-panel {
        height: 45vh;
        height: 45dvh;
        max-height: 45vh;
        max-height: 45dvh;
    }
}

/* Very small phones in portrait */
@media screen and (max-width: 360px) {
    .chart-panel {
        min-height: 50vh;
        min-height: 50dvh;
        max-height: 50vh;
        max-height: 50dvh;
    }
    
    .coins-panel {
        height: 50vh;
        height: 50dvh;
        max-height: 50vh;
        max-height: 50dvh;
    }
}

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

/* Fallback for browsers without dvh support */
@supports not (height: 100dvh) {
    .desktop-container {
        height: calc(var(--vh, 1vh) * 100);
    }
    
    @media screen and (max-width: 768px) {
        .chart-panel {
            min-height: calc(var(--vh, 1vh) * 60);
            max-height: calc(var(--vh, 1vh) * 60);
        }
        
        .coins-panel {
            height: calc(var(--vh, 1vh) * 40);
            max-height: calc(var(--vh, 1vh) * 40);
        }
    }
}

/* Chrome mobile specific fixes */
@media screen and (max-width: 768px) {
    body {
        /* Prevent Chrome mobile address bar issues */
        height: -webkit-fill-available;
        min-height: -webkit-fill-available;
    }
    
    .desktop-container {
        /* Additional Chrome mobile viewport fixes */
        height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
        height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
        min-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
        min-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    }
}

/* Mobile Chart Header Optimizations */
@media screen and (max-width: 768px) {
    .chart-header {
        height: 56px; /* Increased from 48px */
        min-height: 56px;
        padding: 12px 16px; /* Increased from 8px 12px */
        flex-wrap: wrap;
        gap: 10px; /* Increased from 8px */
    }
    
    .chart-title {
        font-size: 18px; /* Increased from 16px */
        flex: 1;
        min-width: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .chart-controls {
        gap: 10px; /* Increased from 8px */
        flex-shrink: 0;
    }
    
    .chart-actions {
        gap: 6px; /* Increased from 4px */
    }
    
    .chart-btn {
        padding: 6px 8px; /* Increased from 4px 6px */
        min-width: 36px; /* Increased from 28px */
        height: 36px; /* Increased from 28px */
        font-size: 14px; /* Increased from 11px */
    }
    
    #chart-interval {
        padding: 6px 8px; /* Increased from 4px 6px */
        font-size: 14px; /* Increased from 11px */
        min-width: 70px; /* Increased from 50px */
    }
    
    /* Mobile Chart Container */
    .chart-container {
        position: relative;
    }
    
    /* Adjust zoom controls for mobile */
    .zoom-controls {
        top: calc(16px + env(safe-area-inset-top)); /* Increased from 12px */
        left: calc(16px + env(safe-area-inset-left)); /* Increased from 12px */
        gap: 8px; /* Increased from 6px */
        z-index: 150; /* Higher z-index for mobile */
    }
    
    .zoom-btn {
        width: 48px; /* Increased from 36px */
        height: 48px; /* Increased from 36px */
        font-size: 24px; /* Increased from 18px */
        background-color: rgba(43, 49, 57, 0.95);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        border-radius: 10px; /* Increased from 8px */
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    }
    
    /* Mobile volume chart adjustments */
    .volume-chart-container {
        min-height: 60px;
        flex: 1;
    }
    
    .price-chart-container {
        flex: 4;
    }
    
    /* Scroll to latest button for mobile */
    .scroll-to-latest-btn {
        bottom: 16px; /* Increased from 12px */
        right: 16px; /* Increased from 12px */
        padding: 8px 16px; /* Increased from 6px 12px */
        font-size: 14px; /* Increased from 11px */
        border-radius: 20px; /* Increased from 16px */
    }
}

/* Extra small mobile optimizations */
@media screen and (max-width: 480px) {
    .chart-header {
        height: 44px;
        min-height: 44px;
        padding: 6px 8px;
    }
    
    .chart-title {
        font-size: 14px;
    }
    
    .chart-btn {
        min-width: 24px;
        height: 24px;
        font-size: 10px;
        padding: 3px 4px;
    }
    
    .zoom-btn {
        width: 32px;
        height: 32px;
        font-size: 16px;
    }
}

/* Mobile touch optimizations */
@media (max-width: 768px) {
    .chart-container canvas {
        /* Larger touch targets for mobile charts */
        touch-action: pan-x pan-y;
    }
    
    /* Increase manual line dot radius on mobile */
    .manual-line-drawer {
        --dot-radius: 10px;
    }
    
    /* Tab system optimizations for mobile */
    .tab-container {
        flex-wrap: nowrap;
        min-height: 60px; /* Increased from 48px */
    }
    
    .tab-button {
        padding: 16px 10px; /* Increased from 12px 8px */
        font-size: 15px; /* Increased from 13px */
        min-width: 0;
        flex: 1;
        min-height: 60px; /* Added for consistency */
    }
    
    /* Tab content adjustments */
    .tab-content {
        height: calc(100% - 60px); /* Updated to match new tab height */
    }
    
    /* Make coins list scrollable on mobile */
    .coins-list-container {
        height: calc(40vh - 140px); /* Adjusted for larger elements */
        max-height: calc(40vh - 140px);
        min-height: 200px;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        /* Ensure proper scrolling behavior */
        overscroll-behavior: contain;
        position: relative;
    }
    
    /* Ensure coins tab allows normal scrolling */
    #coins-tab {
        touch-action: auto;
    }
    
    #coins-tab .coins-list-container {
        touch-action: pan-y;
    }
}

/* Small mobile phones specific */
@media screen and (max-width: 480px) {
    .tab-button {
        padding: 10px 4px;
        font-size: 12px;
        gap: 4px;
    }
    
    .tab-alert-count {
        min-width: 16px;
        height: 16px;
        font-size: 9px;
        padding: 1px 4px;
    }
    
    .coins-list-container {
        height: calc(45vh - 130px); /* Adjusted for larger elements */
        max-height: calc(45vh - 130px);
        min-height: 180px;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
}

/* Landscape mode adjustments for mobile */
@media screen and (max-width: 768px) and (orientation: landscape) {
    .desktop-container {
        grid-template-rows: 1fr 35vh;
        grid-template-rows: 1fr 35dvh;
    }
    
    .chart-panel {
        min-height: 65vh;
        min-height: 65dvh;
        max-height: 65vh;
        max-height: 65dvh;
    }
    
    .coins-panel {
        height: 35vh;
        height: 35dvh;
        max-height: 35vh;
        max-height: 35dvh;
    }
    
    .coins-list-container {
        height: calc(35vh - 120px); /* Adjusted for larger elements */
        max-height: calc(35vh - 120px);
        min-height: 160px;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    
    /* Ensure zoom buttons are visible in landscape */
    .zoom-controls {
        top: calc(8px + env(safe-area-inset-top));
        left: calc(8px + env(safe-area-inset-left));
    }
}

/* Very small screens - stack everything more compactly */
@media screen and (max-width: 360px) {
    .chart-header {
        height: 40px;
        min-height: 40px;
        padding: 4px 6px;
    }
    
    .chart-title {
        font-size: 13px;
    }
    
    .chart-controls {
        gap: 4px;
    }
    
    .chart-btn {
        min-width: 22px;
        height: 22px;
        font-size: 9px;
        padding: 2px 3px;
    }
    
    .tab-button {
        padding: 8px 2px;
        font-size: 11px;
    }
    
    .coins-list-container {
        height: calc(50vh - 110px); /* Adjusted for larger elements */
        max-height: calc(50vh - 110px);
        min-height: 150px;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    
    .zoom-controls {
        top: calc(8px + env(safe-area-inset-top));
        left: calc(8px + env(safe-area-inset-left));
    }
    
    .zoom-btn {
        width: 28px;
        height: 28px;
        font-size: 14px;
    }
}

/* Additional mobile Chrome fixes */
@media screen and (max-width: 768px) and (-webkit-min-device-pixel-ratio: 1) {
    /* Specific Chrome mobile detection */
    html, body {
        position: fixed;
        overflow: hidden;
        width: 100%;
        height: 100%;
    }
    
    .desktop-container {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        height: 100%;
        width: 100%;
    }
}

/* iOS Safari specific fixes */
@supports (-webkit-appearance: none) {
    @media screen and (max-width: 768px) {
        .desktop-container {
            /* iOS Safari viewport fix */
            height: -webkit-fill-available;
        }
        
        .chart-panel {
            height: -webkit-fill-available;
        }
    }
}

/* Task 3: Chart Header */
.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--padding-md) var(--padding-lg);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
    height: 64px; /* Increased from 56px */
    min-height: 64px;
}

.chart-title {
    font-size: 20px; /* Increased from 18px */
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.chart-controls {
    display: flex;
    align-items: center;
    gap: var(--padding-md);
    flex-shrink: 0;
}

.chart-actions {
    display: flex;
    gap: 8px; /* Increased from 6px */
}

.chart-btn {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 10px 12px; /* Increased from 6px 8px */
    border-radius: 6px; /* Increased from 4px */
    cursor: pointer;
    font-size: 14px; /* Increased from 12px */
    transition: all var(--transition-fast);
    min-width: 36px; /* Increased from 28px */
    height: 36px; /* Increased from 28px */
    display: flex;
    align-items: center;
    justify-content: center;
}

.chart-btn:hover {
    background: var(--bg-primary);
    border-color: var(--accent-color);
}

.chart-btn.active {
    background: var(--accent-color);
    color: var(--bg-primary);
    border-color: var(--accent-color);
}

.chart-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#chart-interval {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 8px 12px; /* Increased from 4px 8px */
    border-radius: 6px; /* Increased from 4px */
    font-size: 14px; /* Increased from 12px */
    cursor: pointer;
    min-width: 80px; /* Increased from 60px */
}

#chart-interval:focus {
    outline: none;
    border-color: var(--accent-color);
}

/* Zoom Controls */
.zoom-controls {
    position: absolute;
    top: var(--padding-md);
    left: var(--padding-md);
    display: flex;
    gap: 8px; /* Increased from 6px */
    z-index: 100; /* Above canvases */
}

.zoom-btn {
    width: 42px; /* Increased from 32px */
    height: 42px; /* Increased from 32px */
    background-color: rgba(43, 49, 57, 0.9);
    border: 1px solid var(--border-color);
    border-radius: 8px; /* Increased from 6px */
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px; /* Increased from 18px */
    font-weight: bold;
    transition: all var(--transition-fast);
    user-select: none;
    /* Improve visibility on all devices */
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.zoom-btn:hover {
    background-color: var(--bg-panel);
    border-color: var(--accent-color);
    color: var(--accent-color);
    transform: scale(1.05);
}

.zoom-btn:active {
    transform: scale(0.95);
}

.zoom-btn:disabled {
    cursor: not-allowed;
    opacity: 0.5 !important;
    background-color: rgba(43, 49, 57, 0.5);
}

.zoom-btn:disabled:hover {
    background-color: rgba(43, 49, 57, 0.5);
    border-color: var(--border-color);
    color: var(--text-primary);
    transform: none;
}

/* Chart Container */
.chart-container {
    flex: 1;
    position: relative;
    background: var(--bg-primary);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    /* Mobile fixes */
    width: 100%;
    height: 100%;
}

/* Price Chart Container */
.price-chart-container {
    flex: 5; /* Main chart takes more space */
    position: relative;
}

.price-chart-container canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10; /* Ensure main chart is above volume chart */
}

/* Volume Chart Container */
.volume-chart-container {
    flex: 1; /* Volume takes less space */
    position: relative;
    border-top: 1px solid var(--border-color);
    min-height: 100px; /* Ensure minimum height for volume chart */
}

.volume-chart-container canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none; /* Prevent volume canvas from blocking interactions */
    z-index: 1; /* Keep volume chart behind main chart */
}

/* Canvas Cursor States */
.chart-container canvas {
    cursor: grab;
    /* Disable text selection and touch callouts for better touch interaction */
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    /* Disable touch callouts on iOS */
    -webkit-touch-callout: none;
    /* Improve touch interaction for charts only */
    touch-action: none;
}

.chart-container canvas.dragging {
    cursor: grabbing;
}

.chart-container canvas.drawing-mode {
    cursor: crosshair;
}

.chart-container canvas.dragging-point {
    cursor: move;
}

/* iPad and touch device optimizations */
@media (pointer: coarse) {
    /* Increase touch target sizes for easier interaction */
    .chart-btn {
        min-width: 32px;
        height: 32px;
        padding: 8px 10px;
    }
    
    .zoom-btn {
        width: 28px;
        height: 28px;
        font-size: 16px;
    }
    
    /* Increase dot radius for manual lines on touch devices */
    .manual-line-drawer {
        --dot-radius: 8px; /* Larger touch targets */
    }
}

/* Prevent zoom on double tap for iOS Safari - chart area only */
@media screen and (max-device-width: 1200px) {
    .chart-container canvas {
        touch-action: manipulation;
    }
}

/* Task 4: Coins Panel Structure - Tab System */
.tab-container {
    display: flex;
    background-color: var(--bg-panel);
    border-bottom: 1px solid var(--border-color);
}

.tab-button {
    flex: 1;
    padding: 18px var(--padding-md); /* Increased from 12px */
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 16px; /* Increased from 14px */
    font-weight: 500;
    position: relative;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px; /* Increased from 6px */
    min-height: 56px; /* Added for better touch targets */
}

.tab-button:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.05);
}

.tab-button.active {
    color: var(--accent-color);
    background: rgba(240, 185, 11, 0.1);
}

.tab-button.active::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 3px; /* Increased from 2px */
    background: var(--accent-color);
}

/* Alert count badge */
.tab-alert-count {
    background: var(--error-color);
    color: white;
    border-radius: 12px; /* Increased from 10px */
    padding: 4px 8px; /* Increased from 2px 6px */
    font-size: 12px; /* Increased from 10px */
    font-weight: 600;
    min-width: 22px; /* Increased from 18px */
    height: 22px; /* Increased from 18px */
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.tab-content {
    flex: 1;
    display: none;
    flex-direction: column;
    overflow: hidden;
    /* Enable touch scrolling for mobile */
    touch-action: auto;
}

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

/* Task 5: Coins Tab Implementation */

/* Filters Bar */
.filters-bar {
    display: flex;
    align-items: center;
    gap: 12px; /* Increased from 8px */
    padding: 12px var(--padding-lg); /* Increased from 8px */
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border-color);
    flex-wrap: nowrap; /* Force single row */
    min-height: 56px; /* Increased from 44px */
    max-height: 56px;
}

.filter-group {
    display: flex;
    align-items: center;
    gap: 6px; /* Increased from 4px */
    flex-shrink: 0;
}

/* Hide Exchange label, show only dropdown */
.filter-group:first-child label {
    display: none;
}

.filter-group label {
    font-size: 13px; /* Increased from 11px */
    color: var(--text-secondary);
    font-weight: 500;
    min-width: fit-content;
    white-space: nowrap;
}

.filter-group select,
.filter-group input {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 6px 10px; /* Increased from 3px 6px */
    border-radius: 6px; /* Increased from 4px */
    font-size: 13px; /* Increased from 11px */
    min-width: 60px; /* Increased from 50px */
    height: 36px; /* Added explicit height */
}

.filter-group select:focus,
.filter-group input:focus {
    outline: none;
    border-color: var(--accent-color);
}

/* Exchange dropdown - no label */
#stock-select {
    min-width: 90px; /* Increased from 70px */
}

.symbol-filter-container {
    position: relative;
    display: flex;
    align-items: center;
}

/* Shorter symbol input */
#symbol-filter {
    min-width: 70px; /* Increased from 50px */
    width: 70px;
    padding-right: 24px; /* Increased from 20px */
}

/* Shorter volume input */
#volume-filter {
    min-width: 60px; /* Increased from 40px */
    width: 60px;
}

.clear-symbol-btn {
    position: absolute;
    right: 4px; /* Increased from 3px */
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 14px; /* Increased from 12px */
    padding: 2px; /* Increased from 1px */
    border-radius: 3px; /* Increased from 2px */
    line-height: 1;
    width: 20px; /* Added explicit size */
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.clear-symbol-btn:hover {
    color: var(--text-primary);
    background: var(--bg-secondary);
}

.refresh-btn {
    background: var(--accent-color);
    color: var(--bg-primary);
    border: none;
    padding: 8px 12px; /* Increased from 6px 8px */
    border-radius: 6px; /* Increased from 4px */
    cursor: pointer;
    font-size: 16px; /* Increased from 14px */
    font-weight: 600;
    transition: all var(--transition-fast);
    min-width: 42px; /* Increased from 32px */
    height: 36px; /* Increased from 28px */
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.refresh-btn:hover {
    background: #d4a109;
    transform: translateY(-1px);
}

.refresh-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Coins List */
.coins-list-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--bg-primary);
    /* Enhanced scrolling for better mobile support */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    /* Ensure minimum height for content */
    min-height: 200px;
    position: relative;
    /* Enable single finger touch scrolling */
    touch-action: pan-y;
}

/* Mobile scrolling enhancements */
@media (max-width: 768px) {
    .coins-list-container {
        /* Enable momentum scrolling on iOS */
        -webkit-overflow-scrolling: touch;
        /* Prevent overscroll bounce */
        overscroll-behavior-y: contain;
        /* Explicitly allow vertical scrolling with single finger */
        touch-action: pan-y;
    }
    
    /* Ensure table takes full width and allows touch scrolling */
    .coins-table {
        width: 100%;
        min-width: 100%;
        /* Allow touch events to pass through for scrolling */
        touch-action: pan-y;
    }
    
    /* Ensure table rows don't interfere with scrolling */
    .coins-table tbody tr {
        touch-action: pan-y;
    }
    
    /* Ensure table cells allow scrolling */
    .coins-table td {
        touch-action: pan-y;
    }
}

.coins-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 15px; /* Increased from 13px */
}

.coins-table thead {
    background: var(--bg-secondary);
    position: sticky;
    top: 0;
    z-index: 10;
}

.coins-table thead tr {
    border-bottom: 1px solid var(--border-color);
}

.coins-table th {
    padding: 18px var(--padding-md); /* Increased from 12px */
    text-align: left;
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 14px; /* Increased from 12px */
    cursor: pointer;
    user-select: none;
    position: relative;
    transition: color var(--transition-fast);
    height: 52px; /* Added explicit height for better touch targets */
}

.coins-table th:hover {
    color: var(--text-primary);
}

.coins-table th.sortable:after {
    content: "↕";
    position: absolute;
    right: 8px;
    opacity: 0.3;
    font-size: 10px;
}

.coins-table th.sort-asc:after {
    content: "↑";
    opacity: 1;
    color: var(--accent-color);
}

.coins-table th.sort-desc:after {
    content: "↓";
    opacity: 1;
    color: var(--accent-color);
}

.coins-table tbody tr {
    border-bottom: 1px solid rgba(58, 63, 74, 0.3);
    transition: background-color var(--transition-fast);
    cursor: pointer;
}

.coins-table tbody tr:hover {
    background: rgba(240, 185, 11, 0.05);
}

.coins-table tbody tr.selected {
    background: rgba(240, 185, 11, 0.1);
}

.coins-table td {
    padding: 14px var(--padding-md); /* Increased from 10px */
    color: var(--text-primary);
    border-bottom: 1px solid rgba(58, 63, 74, 0.2);
    height: 48px; /* Added explicit height for better touch targets */
    vertical-align: middle; /* Center content vertically */
}

.coins-table .symbol {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 15px; /* Added explicit size */
}

.coins-table .price {
    font-family: "Monaco", "Consolas", "Courier New", monospace;
    font-weight: 500;
    font-size: 15px; /* Added explicit size */
}

.coins-table .change {
    font-weight: 600;
    font-family: "Monaco", "Consolas", "Courier New", monospace;
    font-size: 15px; /* Added explicit size */
}

.coins-table .change.positive {
    color: var(--success-color);
}

.coins-table .change.negative {
    color: var(--error-color);
}

.coins-table .volume {
    color: var(--text-secondary);
    font-size: 14px; /* Increased from 12px */
}

/* Loading and Error States */
.loading-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 200px;
    color: var(--text-secondary);
}

.spinner {
    border: 2px solid var(--border-color);
    border-top: 2px solid var(--accent-color);
    border-radius: 50%;
    width: 24px;
    height: 24px;
    animation: spin 1s linear infinite;
    margin-right: var(--padding-md);
}

.error-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 200px;
    color: var(--error-color);
    text-align: center;
    padding: var(--padding-lg);
}

.error-container h3 {
    margin-bottom: var(--padding-md);
    color: var(--error-color);
}

.error-container p {
    color: var(--text-secondary);
    max-width: 300px;
    line-height: 1.4;
}

/* Scroll to Latest Button */
.scroll-to-latest-btn {
    position: absolute;
    bottom: 20px;
    right: 20px;
    background: var(--accent-color);
    color: var(--bg-primary);
    border: none;
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    z-index: 100; /* Above canvases */
    opacity: 0.9;
    transition: all var(--transition-fast);
}

.scroll-to-latest-btn:hover {
    opacity: 1;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(240, 185, 11, 0.3);
}

.chart-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(11, 14, 17, 0.7);
    z-index: 200; /* Above everything including canvases and other UI */
    color: var(--text-secondary);
}

.chart-overlay .placeholder-content {
    text-align: center;
    max-width: 300px;
    padding: var(--padding-lg);
}

.chart-overlay .placeholder-content span {
    font-size: 48px;
    display: block;
    margin-bottom: var(--padding-md);
    opacity: 0.5;
}

.chart-overlay .placeholder-content p {
    font-size: 16px;
    line-height: 1.5;
    margin: 0;
}

.chart-overlay .spinner {
    border-color: var(--border-color);
    border-top-color: var(--accent-color);
    width: 32px;
    height: 32px;
}

/* Line Status Display */
.line-status {
    position: absolute;
    bottom: 20px;
    left: 10px;
    background: rgba(11, 14, 17, 0.9);
    color: var(--text-primary);
    padding: 10px 14px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    font-size: 12px;
    color: var(--text-primary);
    z-index: 100; /* Above canvases */
    pointer-events: auto; /* Allow interactions with buttons inside */
    min-width: 200px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Mobile optimizations for line status */
@media screen and (max-width: 768px) {
    .line-status {
        bottom: 10px;
        left: 8px;
        right: 8px;
        min-width: auto;
        max-width: calc(100% - 16px);
        padding: 8px 12px;
        font-size: 12px;
        border-radius: 8px;
        /* Mobile layout optimizations */
        min-height: 60px;
        max-height: 80px;
        overflow: hidden;
        display: flex;
        align-items: center;
    }
    
    /* Button styling for mobile line status */
    #line-status-buttons {
        width: 100% !important;
    }
    
    #delete-manual-line-btn,
    .create-line-alert-btn {
        min-height: 32px !important;
        padding: 6px 12px !important;
        font-size: 11px !important;
        border-radius: 6px !important;
        font-weight: 600 !important;
        transition: all 0.2s ease !important;
        flex: 1 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 4px !important;
        cursor: pointer !important;
        border: none !important;
    }
    
    #delete-manual-line-btn {
        background: #f6465d !important;
        color: white !important;
    }
    
    #delete-manual-line-btn:hover,
    #delete-manual-line-btn:active {
        background: #e53e3e !important;
        transform: scale(0.98);
    }
    
    .create-line-alert-btn {
        background: #0ecb81 !important;
        color: white !important;
    }
    
    .create-line-alert-btn:hover,
    .create-line-alert-btn:active {
        background: #0bb574 !important;
        transform: scale(0.98);
    }
}

/* Very small mobile phones */
@media screen and (max-width: 480px) {
    .line-status {
        bottom: 8px;
        left: 6px;
        right: 6px;
        padding: 6px 10px;
        font-size: 11px;
        min-height: 55px;
        max-height: 70px;
    }
    
    #delete-manual-line-btn,
    .create-line-alert-btn {
        min-height: 28px !important;
        padding: 4px 8px !important;
        font-size: 10px !important;
    }
}

/* Ensure consistent button sizing on touch devices */
@media (pointer: coarse) {
    #delete-manual-line-btn,
    .create-line-alert-btn {
        min-height: 36px !important;
        min-width: 60px !important;
        touch-action: manipulation !important;
    }
    
    .line-status {
        /* Ensure container accommodates larger touch targets */
        min-height: 65px;
    }
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(240, 185, 11, 0.7);
    }
    70% {
        box-shadow: 0 0 0 10px rgba(240, 185, 11, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(240, 185, 11, 0);
    }
}

/* Alert System Loading State */
.alerts-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-secondary);
}

.loading-text {
    font-size: 14px;
}

/* Chart Line Status Enhancement for Alerts */
#chart-line-status {
    position: absolute;
    bottom: 60px;
    left: 10px;
    background: rgba(11, 14, 17, 0.9);
    color: var(--text-primary);
    padding: 8px 12px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    font-size: 12px;
    max-width: 300px;
    z-index: 100; /* Above canvases */
    pointer-events: auto; /* Allow button interactions */
}

/* Alert Line Styles */
.alert-line-indicator {
    position: absolute;
    z-index: 90; /* Below line status but above chart content */
    pointer-events: none;
}

.alert-line-label {
    background: rgba(33, 150, 243, 0.9);
    color: white;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 600;
    white-space: nowrap;
    border: 1px solid rgba(33, 150, 243, 0.3);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.alert-line-label.selected {
    background: rgba(100, 181, 246, 0.9);
    border-color: rgba(100, 181, 246, 0.5);
    box-shadow: 0 0 8px rgba(33, 150, 243, 0.4);
}

/* Alert button in chart controls */
.chart-btn.alert-btn {
    color: var(--accent-color);
}

.chart-btn.alert-btn:hover {
    background: rgba(240, 185, 11, 0.1);
    border-color: var(--accent-color);
}

/* Animation for alert line selection */
@keyframes alertLineHighlight {
    0% {
        box-shadow: 0 0 0 0 rgba(33, 150, 243, 0.8);
    }
    50% {
        box-shadow: 0 0 0 6px rgba(33, 150, 243, 0.4);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(33, 150, 243, 0);
    }
}

.alert-line-highlight {
    animation: alertLineHighlight 1s ease-out;
}

/* iPad and Tablet Landscape Responsive Fixes */
@media screen and (max-height: 800px) and (min-width: 1000px) {
    /* Reduce header height for better space utilization */
    .chart-header {
        height: 48px;
        min-height: 48px;
        padding: 8px var(--padding-lg);
    }
    
    .chart-title {
        font-size: 16px;
    }
    
    /* Adjust flex ratios for better volume chart visibility */
    .price-chart-container {
        flex: 4; /* Reduce from 5 to 4 */
    }
    
    .volume-chart-container {
        flex: 1.2; /* Increase from 1 to 1.2 */
        min-height: 80px; /* Reduce minimum height */
    }
}

/* Specific fixes for iPad mini landscape mode */
@media screen and (max-height: 780px) and (min-width: 1000px) and (max-width: 1200px) {
    /* Further optimize for iPad mini landscape */
    .chart-header {
        height: 44px;
        min-height: 44px;
        padding: 6px var(--padding-md);
    }
    
    .chart-title {
        font-size: 15px;
    }
    
    .chart-controls {
        gap: 8px;
    }
    
    .chart-actions {
        gap: 4px;
    }
    
    .chart-btn {
        padding: 4px 6px;
        min-width: 24px;
        height: 24px;
        font-size: 11px;
    }
    
    #chart-interval {
        padding: 3px 6px;
        font-size: 11px;
        min-width: 50px;
    }
    
    /* Optimize chart container ratios */
    .price-chart-container {
        flex: 3.5; /* Further reduce to give more space to volume */
    }
    
    .volume-chart-container {
        flex: 1.5; /* Increase volume chart space */
        min-height: 70px; /* Smaller minimum height */
    }
    
    /* Adjust zoom controls positioning */
    .zoom-controls {
        top: 8px;
        left: 8px;
    }
    
    .zoom-btn {
        width: 20px;
        height: 20px;
        font-size: 12px;
    }
}

/* Extra small height adjustments for very constrained landscapes */
@media screen and (max-height: 700px) and (min-width: 1000px) {
    .desktop-container {
        grid-template-columns: 1fr 380px; /* Reduce sidebar width slightly */
    }
    
    .chart-header {
        height: 40px;
        min-height: 40px;
        padding: 4px var(--padding-md);
    }
    
    .chart-title {
        font-size: 14px;
    }
    
    /* More aggressive chart ratio adjustments */
    .price-chart-container {
        flex: 3;
    }
    
    .volume-chart-container {
        flex: 1.8;
        min-height: 60px;
    }
}
