/* Import Google Font */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600&display=swap');

:root {
    --bg-color: #f5f5f7;
    --card-bg: rgba(255, 255, 255, 0.9);
    --text-primary: #1d1d1f;
    --text-secondary: #6e6e73;
    --accent-color: #0071e3;
    /* Apple-style blue */
    --accent-hover: #0058b0;
    --border-color: rgba(0, 0, 0, 0.1);
    --up-color: #34c759;
    --down-color: #ff3b30;
    --glass-border: 1px solid rgba(0, 0, 0, 0.08);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    background-image: radial-gradient(circle at 50% 0%, #ffffff 0%, var(--bg-color) 70%);
    color: var(--text-primary);
    height: 100vh;
    /* Fallback for older browsers */
    height: 100dvh;
    /* Dynamic viewport height - accounts for mobile browser UI */
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    border-bottom: var(--glass-border);
    z-index: 10;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.logo {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.logo span {
    color: var(--accent-color);
}

.controls {
    display: flex;
    gap: 1.5rem;
    align-items: center;
}

/* Exchange Toggle (Binance/Hyperliquid) */
.exchange-toggle {
    display: flex;
    background: rgba(0, 0, 0, 0.04);
    padding: 0.25rem;
    border-radius: 8px;
    gap: 0.25rem;
    border: 1px solid rgba(0, 0, 0, 0.08);
}

.exchange-option {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.exchange-option input[type="radio"] {
    display: none;
}

.exchange-option span {
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    transition: all 0.2s ease;
}

.exchange-option input[type="radio"]:checked+span {
    background: linear-gradient(135deg, rgba(52, 199, 89, 0.15), rgba(0, 188, 212, 0.15));
    color: #34c759;
    box-shadow: 0 2px 8px rgba(52, 199, 89, 0.2);
}

.exchange-option:hover span {
    color: var(--text-primary);
}

/* Market Toggle (Spot/Futures) */
.market-toggle {
    display: flex;
    background: rgba(0, 0, 0, 0.04);
    padding: 0.25rem;
    border-radius: 8px;
    gap: 0.25rem;
}

.market-option {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.market-option input[type="radio"] {
    display: none;
}

.market-option span {
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    transition: all 0.2s ease;
}

.market-option input[type="radio"]:checked+span {
    background: rgba(0, 0, 0, 0.08);
    color: var(--accent-color);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.market-option:hover span {
    color: var(--text-primary);
}

/* Disabled state for Spot option (when Hyperliquid is selected) */
.market-option.disabled {
    pointer-events: none;
    opacity: 0.4;
}

.market-option.disabled span {
    text-decoration: line-through;
}

/* Pair Controls */
.pair-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.edit-btn {
    background: rgba(0, 0, 0, 0.04);
    border: var(--glass-border);
    border-radius: 8px;
    padding: 0.5rem 0.6rem;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.2s ease;
    color: var(--text-primary);
}

.edit-btn:hover {
    background: rgba(0, 0, 0, 0.08);
    border-color: var(--accent-color);
}

/* Dropdown */
.select-wrapper {
    position: relative;
}

select {
    appearance: none;
    background: rgba(0, 0, 0, 0.04);
    border: var(--glass-border);
    color: var(--text-primary);
    padding: 0.5rem 2.5rem 0.5rem 1rem;
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

select:hover {
    background: rgba(0, 0, 0, 0.06);
    border-color: rgba(0, 0, 0, 0.15);
}

select:focus {
    outline: none;
    border-color: var(--accent-color);
}

.select-arrow {
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    color: var(--text-secondary);
    font-size: 0.8rem;
}

/* Timeframe Tabs */
.timeframes {
    display: flex;
    background: rgba(0, 0, 0, 0.04);
    padding: 0.25rem;
    border-radius: 8px;
    gap: 0.25rem;
}

.tf-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    transition: all 0.2s ease;
}

.tf-btn:hover {
    color: var(--text-primary);
    background: rgba(0, 0, 0, 0.04);
}

.tf-btn.active {
    background: rgba(0, 0, 0, 0.08);
    color: var(--accent-color);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

/* Main Content */
main {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 1rem;
    gap: 1rem;
    position: relative;
}

.chart-container {
    flex: 1;
    background: var(--card-bg);
    border-radius: 12px;
    border: var(--glass-border);
    overflow: hidden;
    position: relative;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

#tv-chart {
    width: 100%;
    height: 60%;
    position: relative;
}

#rsi-chart {
    width: 100%;
    height: 20%;
    border-top: var(--glass-border);
    position: relative;
}

#delta-chart {
    width: 100%;
    height: 20%;
    border-top: var(--glass-border);
    position: relative;
}

/* Footprint Canvas Overlay */
#footprint-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 3;
}

/* Chart Legend */
.chart-legend {
    position: absolute;
    top: 8px;
    left: 12px;
    z-index: 5;
    display: flex;
    gap: 16px;
    font-size: 11px;
    font-family: 'Inter', sans-serif;
    font-weight: 500;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 8px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 4px;
    backdrop-filter: blur(4px);
    color: var(--text-primary);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.legend-item::before {
    content: '';
    display: inline-block;
    width: 12px;
    height: 3px;
    border-radius: 2px;
}

/* Legend colors matching indicator lines */
.legend-item.ema-fast::before {
    background-color: #fcd535;
    /* Yellow - EMA 24 */
}

.legend-item.ema-slow::before {
    background-color: #8750db;
    /* Purple - EMA 120 */
}

.legend-item.vwap::before {
    background-color: #3b82f6;
    /* Blue - VWAP */
}

.legend-item.rsi::before {
    background-color: #e91e63;
    /* Pink - RSI */
}

.legend-item.delta::before {
    background: linear-gradient(90deg, #0ecb81, #f6465d);
    /* Green to Red - Delta */
}

.legend-item.cvd::before {
    background-color: #00bcd4;
    /* Cyan - CVD */
}

.legend-item.footprint::before {
    background: linear-gradient(90deg, #26a69a, #ef5350);
    /* Teal to Red - Footprint */
}

/* Loading State */
.loader {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    border: 3px solid rgba(0, 0, 0, 0.08);
    border-radius: 50%;
    border-top-color: var(--accent-color);
    animation: spin 1s ease-in-out infinite;
    z-index: 20;
    display: none;
}

.loader.active {
    display: block;
}

@keyframes spin {
    to {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

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

::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.25);
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal-overlay.active {
    display: flex;
}

.modal {
    background: var(--card-bg);
    border: var(--glass-border);
    border-radius: 12px;
    width: 400px;
    max-width: 90vw;
    max-height: 80vh;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.25rem;
    border-bottom: var(--glass-border);
}

.modal-header h3 {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 1.25rem;
}

.add-token-form {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.add-token-form input {
    flex: 1;
    background: rgba(255, 255, 255, 0.05);
    border: var(--glass-border);
    border-radius: 8px;
    padding: 0.6rem 1rem;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
    text-transform: uppercase;
}

.add-token-form input:focus {
    outline: none;
    border-color: var(--accent-color);
}

.add-token-form button {
    background: var(--accent-color);
    color: #000;
    border: none;
    border-radius: 8px;
    padding: 0.6rem 1.2rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.add-token-form button:hover {
    background: var(--accent-hover);
}

.add-token-form button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.validation-msg {
    font-size: 0.85rem;
    padding: 0.5rem 0;
    min-height: 1.5rem;
}

.validation-msg.success {
    color: var(--up-color);
}

.validation-msg.error {
    color: var(--down-color);
}

.validation-msg.loading {
    color: var(--text-secondary);
}

.token-list {
    max-height: 300px;
    overflow-y: auto;
}

.token-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.6rem 0.8rem;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 6px;
    margin-bottom: 0.5rem;
}

.token-item:hover {
    background: rgba(255, 255, 255, 0.06);
}

.token-name {
    font-weight: 500;
    color: var(--text-primary);
}

.token-remove {
    background: none;
    border: none;
    color: var(--down-color);
    cursor: pointer;
    font-size: 1.2rem;
    padding: 0;
    opacity: 0.6;
    transition: opacity 0.2s;
}

.token-remove:hover {
    opacity: 1;
}

.token-item.default .token-remove {
    display: none;
}

.token-item.default::after {
    content: 'default';
    font-size: 0.7rem;
    color: var(--text-secondary);
    padding: 2px 6px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 4px;
}

/* Settings Button (next to timeframes) */
.settings-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 0.4rem 0.6rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s ease;
    margin-left: 0.25rem;
}

.settings-btn:hover {
    color: var(--accent-color);
    background: rgba(255, 255, 255, 0.05);
}

/* EMA Settings Modal */
.ema-settings-modal {
    width: 480px;
    max-width: 95vw;
}

.settings-description {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-bottom: 1rem;
}

.ema-settings-list {
    max-height: 400px;
    overflow-y: auto;
}

.ema-setting-row {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.75rem;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    margin-bottom: 0.5rem;
}

.ema-setting-row:hover {
    background: rgba(255, 255, 255, 0.06);
}

.ema-tf-label {
    font-weight: 600;
    color: var(--text-primary);
    min-width: 50px;
    font-size: 0.95rem;
}

.ema-inputs {
    display: flex;
    gap: 1rem;
    flex: 1;
}

.ema-input-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
}

.ema-input-group label {
    font-size: 0.7rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.ema-input-group input {
    background: rgba(255, 255, 255, 0.05);
    border: var(--glass-border);
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
    width: 100%;
    text-align: center;
}

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

.ema-input-group.fast input {
    border-left: 3px solid #fcd535;
}

.ema-input-group.slow input {
    border-left: 3px solid #8750db;
}

.ema-settings-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    margin-top: 1.25rem;
    padding-top: 1rem;
    border-top: var(--glass-border);
}

.btn-primary {
    background: var(--accent-color);
    color: #000;
    border: none;
    border-radius: 8px;
    padding: 0.6rem 1.5rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

.btn-secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    border: var(--glass-border);
    border-radius: 8px;
    padding: 0.6rem 1.5rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* ==================== Mobile Responsive Styles ==================== */

@media screen and (max-width: 768px) {

    /* Header adjustments */
    header {
        flex-direction: column;
        gap: 0.75rem;
        padding: 0.75rem 1rem;
    }

    .logo {
        font-size: 1.1rem;
    }

    /* Controls stack vertically below logo */
    .controls {
        flex-direction: column;
        width: 100%;
        gap: 0.75rem;
    }

    /* Market toggle full width on mobile */
    .market-toggle {
        width: 100%;
        justify-content: center;
    }

    .market-option span {
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }

    /* Pair controls take full width */
    .pair-controls {
        width: 100%;
        justify-content: space-between;
    }

    .select-wrapper {
        flex: 1;
    }

    select {
        width: 100%;
        padding: 0.6rem 2.5rem 0.6rem 1rem;
        font-size: 1rem;
    }

    .edit-btn {
        padding: 0.6rem 0.8rem;
        font-size: 1rem;
    }

    /* Timeframe buttons take full width and wrap */
    .timeframes {
        width: 100%;
        justify-content: center;
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    .tf-btn {
        flex: 1;
        min-width: 40px;
        padding: 0.5rem 0.4rem;
        font-size: 0.8rem;
        text-align: center;
    }

    /* Main content adjustments */
    main {
        padding: 0.5rem;
        gap: 0.5rem;
        flex: 1;
        min-height: 0;
        /* Important for flex children to shrink properly */
        overflow: hidden;
    }

    .chart-container {
        border-radius: 8px;
        flex: 1;
        min-height: 0;
        /* Allow shrinking */
        display: flex;
        flex-direction: column;
    }

    #tv-chart {
        flex: 3;
        /* 75% of available space */
        min-height: 0;
    }

    #rsi-chart {
        flex: 1;
        /* 20% of available space */
        min-height: 60px;
        /* Ensure RSI is always visible */
    }

    #delta-chart {
        flex: 1;
        /* 20% of available space */
        min-height: 60px;
        /* Ensure Delta is always visible */
    }

    /* Chart legend adjustments for mobile */
    .chart-legend {
        gap: 8px;
        font-size: 10px;
        flex-wrap: wrap;
    }

    .legend-item {
        padding: 3px 6px;
        gap: 4px;
    }

    .legend-item::before {
        width: 10px;
        height: 2px;
    }

    /* Modal adjustments for mobile */
    .modal {
        width: 95vw;
        max-height: 90vh;
    }

    .modal-header {
        padding: 0.75rem 1rem;
    }

    .modal-body {
        padding: 1rem;
    }

    .add-token-form {
        flex-direction: column;
    }

    .add-token-form input {
        padding: 0.7rem 1rem;
        font-size: 1rem;
    }

    .add-token-form button {
        padding: 0.7rem 1.2rem;
        font-size: 1rem;
    }

    .token-item {
        padding: 0.7rem 0.8rem;
    }

    .token-name {
        font-size: 0.95rem;
    }

    /* EMA Settings modal mobile */
    .ema-settings-modal {
        width: 95vw;
    }

    .ema-setting-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .ema-tf-label {
        font-size: 1rem;
    }

    .ema-inputs {
        width: 100%;
    }

    .ema-settings-actions {
        flex-direction: column;
    }

    .ema-settings-actions button {
        width: 100%;
    }

    .settings-btn {
        padding: 0.5rem;
    }
}

/* Extra small screens (phones in portrait) */
@media screen and (max-width: 480px) {
    header {
        padding: 0.5rem 0.75rem;
    }

    .logo {
        font-size: 1rem;
    }

    .tf-btn {
        font-size: 0.75rem;
        padding: 0.45rem 0.3rem;
    }

    .chart-legend {
        font-size: 9px;
        gap: 6px;
        top: 4px;
        left: 8px;
    }

    .legend-item {
        padding: 2px 5px;
    }

    .legend-item::before {
        width: 8px;
    }
}