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

:root {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-card: #1c2333;
    --bg-card-hover: #242d3d;
    --border: #30363d;
    --text-primary: #e6edf3;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;
    --accent: #58a6ff;
    --green: #3fb950;
    --green-bg: rgba(63, 185, 80, 0.1);
    --red: #f85149;
    --red-bg: rgba(248, 81, 73, 0.1);
    --gold: #f0c000;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.5;
}

a {
    color: var(--accent);
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}

/* === Header === */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-left h1 {
    font-size: 1.5rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

.logo-icon {
    font-size: 1.3rem;
}

.subtitle {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 2px;
    display: block;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.refresh-info {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-card);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

#countdown {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-secondary);
    min-width: 30px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

#refresh-btn {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    transition: background 0.2s;
}
#refresh-btn:hover {
    background: rgba(88, 166, 255, 0.15);
}

/* === Controls === */
.controls {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 16px 24px;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--border);
    background: var(--bg-secondary);
}

.search-box {
    flex: 1;
    min-width: 200px;
}

#search-input {
    width: 100%;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s;
}
#search-input::placeholder {
    color: var(--text-muted);
}
#search-input:focus {
    border-color: var(--accent);
}

.sort-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sort-controls label {
    font-size: 0.85rem;
    color: var(--text-secondary);
    white-space: nowrap;
}

#sort-select {
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    outline: none;
}

.filter-controls {
    display: flex;
    gap: 8px;
}

.filter-btn {
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
}
.filter-btn:hover {
    border-color: var(--accent);
    color: var(--text-primary);
}
.filter-btn.active {
    background: rgba(88, 166, 255, 0.15);
    border-color: var(--accent);
    color: var(--accent);
}

/* === Coin Grid === */
.coin-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
    padding: 24px;
    max-width: 1440px;
    margin: 0 auto;
}

/* === Loading === */
.loading {
    grid-column: 1 / -1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    color: var(--text-secondary);
    gap: 16px;
}

.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* === Coin Card === */
.coin-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}
.coin-card:hover {
    background: var(--bg-card-hover);
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.coin-card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.coin-logo {
    width: 36px;
    height: 36px;
    border-radius: 50%;
}

.coin-name-group {
    flex: 1;
    min-width: 0;
}

.coin-name {
    font-size: 1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.coin-symbol {
    font-size: 0.8rem;
    color: var(--text-secondary);
    text-transform: uppercase;
}

.coin-rank {
    font-size: 0.75rem;
    color: var(--text-muted);
    background: var(--bg-secondary);
    padding: 2px 8px;
    border-radius: 10px;
    font-weight: 500;
}

.fav-btn {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--text-muted);
    padding: 4px;
    transition: color 0.2s;
    line-height: 1;
}
.fav-btn:hover,
.fav-btn.active {
    color: var(--gold);
}

.coin-price-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 8px;
}

.coin-price {
    font-size: 1.35rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.coin-change {
    font-size: 0.9rem;
    font-weight: 600;
    padding: 3px 8px;
    border-radius: 6px;
    font-variant-numeric: tabular-nums;
}
.coin-change.positive {
    color: var(--green);
    background: var(--green-bg);
}
.coin-change.negative {
    color: var(--red);
    background: var(--red-bg);
}

.coin-meta {
    display: flex;
    justify-content: space-between;
    font-size: 0.78rem;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.sparkline-container {
    height: 50px;
    width: 100%;
}

.sparkline-container canvas {
    width: 100% !important;
    height: 50px !important;
}

/* === Detail Overlay === */
.detail-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
}
.detail-overlay.hidden {
    display: none;
}

.detail-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 32px;
    max-width: 520px;
    width: 100%;
    position: relative;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    max-height: 90vh;
    overflow-y: auto;
}

.detail-close {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.6rem;
    cursor: pointer;
    line-height: 1;
    padding: 4px;
}
.detail-close:hover {
    color: var(--text-primary);
}

.detail-header {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 24px;
}

.detail-logo {
    width: 56px;
    height: 56px;
    border-radius: 50%;
}

.detail-title h2 {
    font-size: 1.4rem;
    font-weight: 700;
}

.detail-title .detail-symbol {
    color: var(--text-secondary);
    font-size: 0.9rem;
    text-transform: uppercase;
}

.detail-price-section {
    margin-bottom: 24px;
}

.detail-current-price {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: 4px;
}

.detail-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 24px;
}

.detail-stat {
    background: var(--bg-secondary);
    padding: 12px 16px;
    border-radius: var(--radius-sm);
}

.detail-stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.detail-stat-value {
    font-size: 1rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.detail-chart-container {
    height: 120px;
    margin-top: 16px;
}

.detail-chart-container canvas {
    width: 100% !important;
    height: 120px !important;
}

/* === Footer === */
footer {
    text-align: center;
    padding: 24px;
    color: var(--text-muted);
    font-size: 0.8rem;
    border-top: 1px solid var(--border);
}

/* === Error === */
.error-msg {
    grid-column: 1 / -1;
    text-align: center;
    padding: 60px 20px;
    color: var(--red);
}
.error-msg p {
    margin-bottom: 12px;
}
.error-msg button {
    padding: 8px 20px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--red);
    background: var(--red-bg);
    color: var(--red);
    font-family: inherit;
    cursor: pointer;
}

/* === Responsive === */
@media (max-width: 768px) {
    header {
        flex-direction: column;
        gap: 12px;
        align-items: flex-start;
    }
    .controls {
        flex-direction: column;
        align-items: stretch;
    }
    .sort-controls,
    .filter-controls {
        width: 100%;
    }
    #sort-select {
        flex: 1;
    }
    .filter-controls {
        justify-content: stretch;
    }
    .filter-btn {
        flex: 1;
        text-align: center;
    }
    .coin-grid {
        grid-template-columns: 1fr;
        padding: 16px;
        gap: 12px;
    }
    .detail-card {
        padding: 24px;
    }
    .detail-stats {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    header {
        padding: 16px;
    }
    .header-left h1 {
        font-size: 1.25rem;
    }
    .coin-price {
        font-size: 1.15rem;
    }
    .detail-current-price {
        font-size: 1.6rem;
    }
}
