/*
 * Colours are tokens, defined once here on :root and named everywhere
 * else (tests/js/styles-tokens.test.mjs fails on a colour literal in any
 * declaration outside :root). --text-muted is the dimmest text colour, and
 * every colour a color declaration names is >= 4.5:1 on a card (--surface
 * over --bg-mid).
 * visualization.js and treemap.js read the chart and tile tokens with
 * getComputedStyle; the item colours are its PALETTE.
 */
:root {
    --bg-deep: #1a1a2e;
    --bg-mid: #16213e;

    --text: #e8ecf4;
    --text-strong: #ffffff;
    --text-soft: #c9d0dd;
    --text-muted: #9ba5ba;

    --accent: #00d9ff;
    --accent-2: #00ff88;
    --accent-light: #66e8ff;
    --accent-2-light: #66ffb8;
    --on-accent: #1a1a2e;
    --danger: #ff9bb0;

    --gradient-brand: linear-gradient(135deg, var(--accent), var(--accent-2));
    --gradient-brand-v: linear-gradient(180deg, var(--accent), var(--accent-2));
    --gradient-brand-v-light: linear-gradient(180deg, var(--accent-light), var(--accent-2-light));
    --gradient-page: linear-gradient(135deg, var(--bg-deep) 0%, var(--bg-mid) 100%);

    --surface: rgba(255, 255, 255, 0.03);
    --surface-hover: rgba(255, 255, 255, 0.05);
    --surface-sunk: rgba(0, 0, 0, 0.22);
    --border: rgba(255, 255, 255, 0.07);
    --border-strong: rgba(255, 255, 255, 0.12);
    --scroll-track: rgba(255, 255, 255, 0.02);

    --accent-wash: rgba(0, 217, 255, 0.02);
    --accent-tint: rgba(0, 217, 255, 0.05);
    --accent-hover: rgba(0, 217, 255, 0.1);
    --accent-select: rgba(0, 217, 255, 0.3);
    --accent-border: rgba(0, 217, 255, 0.4);
    --accent-border-strong: rgba(0, 217, 255, 0.7);
    --accent-glow: rgba(0, 217, 255, 0.15);
    --accent-2-tint: rgba(0, 255, 136, 0.1);
    --accent-2-glow: rgba(0, 255, 136, 0.25);
    --danger-bg: rgba(255, 99, 132, 0.15);
    --danger-border: rgba(255, 99, 132, 0.5);
    --disabled-bg: #3a4052;
    --shadow: rgba(0, 0, 0, 0.3);
    --noise-opacity: 0.03;

    --chart-grid: rgba(255, 255, 255, 0.07);
    --tooltip-bg: rgba(20, 26, 50, 0.96);
    --tooltip-border: rgba(0, 217, 255, 0.35);
    --tile-text: #ffffff;
    --tile-shadow: rgba(0, 0, 0, 0.7);
    --tile-edge: rgba(255, 255, 255, 0.2);
    --tile-hatch: rgba(255, 255, 255, 0.14);
    /* over a frame's colour, below its header strip: its children's ground */
    --tile-frame: rgba(18, 18, 36, 0.72);
}

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

body {
    font-family: 'JetBrains Mono', 'SF Mono', Monaco, 'Cascadia Code', monospace;
    background: var(--gradient-page);
    background-attachment: fixed;
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
    position: relative;
}

/* Subtle noise texture overlay */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
    opacity: var(--noise-opacity);
    pointer-events: none;
    z-index: 9999;
}

.container {
    max-width: 1920px;
    margin: 0 auto;
    padding: 2rem clamp(1rem, 3vw, 3rem);
}

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

@keyframes pulse {
    0%, 100% { opacity: 0.4; }
    50% { opacity: 0.8; }
}

@keyframes borderGlow {
    0%, 100% {
        border-color: var(--accent-border);
        box-shadow: 0 0 20px var(--accent-hover);
    }
    50% {
        border-color: var(--accent-2-glow);
        box-shadow: 0 0 30px var(--accent-2-tint);
    }
}

/* Header */
header {
    text-align: center;
    margin-bottom: 2rem;
    animation: fadeSlideUp 0.6s ease-out;
}

header h1 {
    font-size: 3rem;
    font-weight: 700;
    background: var(--gradient-brand);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 0.25rem;
    letter-spacing: -0.02em;
    line-height: 1.2;
}

.subtitle {
    color: var(--text-muted);
    font-size: 0.95rem;
    font-weight: 400;
    letter-spacing: 0.02em;
}

/* Controls */
.controls {
    text-align: center;
    margin-bottom: 2rem;
    animation: fadeSlideUp 0.6s ease-out 0.1s backwards;
}

.hint {
    margin-top: 1rem;
    color: var(--text-muted);
    font-size: 0.8rem;
    letter-spacing: 0.02em;
}

.build-info {
    margin-top: 1.5rem;
    text-align: center;
    font-size: 0.8rem;
    color: var(--text-muted);
    letter-spacing: 0.05em;
    animation: fadeSlideUp 0.6s ease-out 0.4s backwards;
}

/* While results show: a compact header bar, and no landing-page text */
body:has(#results:not(.hidden)) .container {
    padding-top: 1rem;
}

body:has(#results:not(.hidden)) header {
    display: flex;
    align-items: baseline;
    gap: 1rem;
    text-align: left;
    margin-bottom: 0.75rem;
    animation: none;
}

body:has(#results:not(.hidden)) header h1 {
    font-size: 1.35rem;
    margin-bottom: 0;
}

body:has(#results:not(.hidden)) .subtitle,
body:has(#results:not(.hidden)) .controls {
    display: none;
}

/* Drop Zone */
.drop-zone {
    border: 2px dashed var(--accent-border);
    border-radius: 16px;
    padding: 3rem 2rem;
    margin: 0 auto 1rem;
    max-width: 500px;
    transition: all 0.3s ease;
    background: var(--accent-wash);
    animation: borderGlow 3s ease-in-out infinite;
    position: relative;
}

.drop-zone:hover {
    border-color: var(--accent-border-strong);
    background: var(--accent-tint);
    animation: none;
    box-shadow: 0 0 40px var(--accent-glow);
}

.drop-zone.drag-over {
    border-color: var(--accent-2);
    border-style: solid;
    background: var(--accent-2-tint);
    transform: scale(1.02);
    box-shadow: 0 0 50px var(--accent-2-glow);
    animation: none;
}

.drop-zone.disabled {
    opacity: 0.5;
    pointer-events: none;
    animation: none;
}

.drop-zone-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
}

.drop-icon {
    opacity: 0.9;
    transition: transform 0.3s ease;
}

.drop-zone:hover .drop-icon {
    transform: scale(1.1);
}

.drop-zone.drag-over .drop-icon {
    transform: scale(1.15);
}

.drop-icon svg {
    display: block;
}

.drop-text {
    font-size: 1.1rem;
    color: var(--text-soft);
    font-weight: 600;
}

.drop-subtext {
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* Sample button: below the drop zone; the one way in without a folder */
.try-sample-btn {
    margin: 0.25rem auto 0;
    padding: 0.65rem 1.4rem;
    /* a brand-gradient ring around the page colour */
    border: 1.5px solid transparent;
    border-radius: 999px;
    background: linear-gradient(var(--bg-mid), var(--bg-mid)) padding-box,
                var(--gradient-brand) border-box;
    color: var(--accent);
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    letter-spacing: 0.01em;
    cursor: pointer;
    box-shadow: 0 0 18px var(--accent-hover);
    transition: transform 0.2s ease, box-shadow 0.2s ease, color 0.2s ease, opacity 0.2s ease;
}

.try-sample-btn:hover {
    color: var(--accent-2);
    transform: translateY(-1px);
    box-shadow: 0 0 28px var(--accent-2-glow);
}

.try-sample-btn:disabled {
    opacity: 0.55;
    cursor: progress;
    transform: none;
    box-shadow: none;
}

/* Error */
.error {
    background: var(--danger-bg);
    border: 1px solid var(--danger-border);
    color: var(--danger);
    padding: 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
    text-align: center;
    font-size: 0.9rem;
    animation: fadeSlideUp 0.3s ease-out;
}

/* What the scan could not read, below the summary */
.scan-notice {
    background: var(--surface);
    border: 1px solid var(--danger-border);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    margin-bottom: 1.25rem;
    font-size: 0.85rem;
    color: var(--text-soft);
}

.scan-notice-line {
    margin: 0 0 0.25rem;
    color: var(--danger);
}

.scan-notice summary {
    cursor: pointer;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

.scan-notice-paths {
    margin: 0.4rem 0 0;
    padding-left: 1.25rem;
    overflow-wrap: anywhere;
}

.scan-notice-more {
    margin: 0.25rem 0 0;
    color: var(--text-muted);
}

/* Loading */
.loading {
    text-align: center;
    padding: 3rem;
    animation: fadeSlideUp 0.4s ease-out;
}

.spinner {
    width: 60px;
    height: 60px;
    margin: 0 auto 1.5rem;
    position: relative;
}

.spinner::before,
.spinner::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 3px solid transparent;
}

.spinner::before {
    border-top-color: var(--accent);
    border-right-color: var(--accent);
    animation: spin 1s linear infinite;
}

.spinner::after {
    border-bottom-color: var(--accent-2);
    border-left-color: var(--accent-2);
    animation: spin 1s linear infinite reverse;
    inset: 6px;
}

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

#scan-progress {
    color: var(--text-muted);
    font-size: 0.9rem;
    animation: pulse 1.5s ease-in-out infinite;
}

.hidden {
    display: none !important;
}

/* Results Section */
.results {
    animation: fadeSlideUp 0.5s ease-out;
}

.results-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
}

.scan-another-btn {
    background: transparent;
    border: 1px solid var(--accent-border);
    color: var(--accent);
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.scan-another-btn:hover {
    background: var(--accent-hover);
    border-color: var(--accent);
    color: var(--accent-2);
}

/* "Sample data": shown beside the breadcrumb while the sample is */
.sample-label {
    flex-shrink: 0;
    padding: 0.3rem 0.75rem;
    border: 1px solid var(--accent-2-glow);
    border-radius: 999px;
    background: var(--accent-2-tint);
    color: var(--accent-2);
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    white-space: nowrap;
}

/* Breadcrumb */
.breadcrumb {
    background: var(--surface);
    padding: 0.6rem 0.75rem;
    border-radius: 8px;
    font-size: 0.85rem;
    border: 1px solid var(--border);
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
}

.breadcrumb-link {
    color: var(--accent);
    text-decoration: none;
    transition: all 0.2s;
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
}

.breadcrumb-link:hover {
    color: var(--accent-2);
    background: var(--accent-2-tint);
}

.breadcrumb-sep {
    color: var(--text-muted);
    margin: 0 0.15rem;
    font-weight: 300;
}

.breadcrumb-current {
    color: var(--text-strong);
    font-weight: 600;
    padding: 0.2rem 0.4rem;
}

/* Summary: the total is the headline, the counts sit beside it */
.summary {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
    gap: 1rem 2rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
}

.summary-item {
    animation: fadeSlideUp 0.5s ease-out backwards;
}

.summary-item .label {
    display: block;
    color: var(--text-muted);
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    font-weight: 500;
}

.summary-item .value {
    display: block;
    font-weight: 700;
    color: var(--text);
    font-size: 1.15rem;
    line-height: 1.3;
}

.summary-total .value {
    font-size: clamp(2.5rem, 6vw, 4rem);
    line-height: 1.05;
    letter-spacing: -0.03em;
    background: var(--gradient-brand);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.summary-stats {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.summary-stats .summary-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.55rem 1rem;
    min-width: 7.5rem;
}

.summary-stats .summary-item:nth-child(1) { animation-delay: 0.05s; }
.summary-stats .summary-item:nth-child(2) { animation-delay: 0.1s; }
.summary-stats .summary-item:nth-child(3) { animation-delay: 0.15s; }

/* Cards */
.treemap-wrapper,
.chart-wrapper,
.file-list-container {
    background: var(--surface);
    padding: 1.25rem;
    border-radius: 14px;
    border: 1px solid var(--border);
    min-width: 0;
}

.treemap-wrapper h3,
.chart-wrapper h3,
.file-list-container h3 {
    margin-bottom: 0.9rem;
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

/* Treemap: full width, as tall as the viewport allows */
.treemap-wrapper {
    margin-bottom: 1.25rem;
    animation: fadeSlideUp 0.5s ease-out 0.1s backwards;
}

/* The heading and the file-type legend on one line, the legend wrapping
   under the heading when there is no room */
.treemap-head {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.4rem 1.25rem;
    margin-bottom: 0.9rem;
}

.treemap-wrapper .treemap-head h3 {
    margin-bottom: 0;
}

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem 1rem;
    list-style: none;
    font-size: 0.75rem;
    color: var(--text-soft);
}

.legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
}

.legend-swatch {
    width: 0.7rem;
    height: 0.7rem;
    border-radius: 3px;
    background-color: var(--swatch, transparent);
}

.treemap-container {
    height: clamp(300px, 62vh, 860px);
    position: relative;
    border-radius: 10px;
    overflow: hidden;
    background: var(--surface-sunk);
}

.treemap-container canvas {
    display: block;
    width: 100%;
    height: 100%;
}

/* Treemap Tooltip */
.treemap-tooltip {
    position: fixed;
    z-index: 10000;
    background: var(--tooltip-bg);
    border: 1px solid var(--tooltip-border);
    border-radius: 8px;
    padding: 12px 16px;
    pointer-events: none;
    opacity: 0;
    transform: translateY(5px);
    transition: opacity 0.15s ease, transform 0.15s ease;
    font-family: 'JetBrains Mono', monospace;
    box-shadow: 0 8px 32px var(--shadow);
    max-width: 300px;
}

.treemap-tooltip.visible {
    opacity: 1;
    transform: translateY(0);
}

.treemap-tooltip-title {
    font-weight: 600;
    color: var(--accent);
    font-size: 0.9rem;
    margin-bottom: 6px;
    word-break: break-word;
}

.treemap-tooltip-path {
    color: var(--text-muted);
    font-size: 0.75rem;
    margin-bottom: 6px;
    max-width: 28rem;
    word-break: break-all;
}

.treemap-tooltip-size {
    color: var(--text);
    font-size: 0.85rem;
    margin-bottom: 2px;
}

.treemap-tooltip-percent {
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-bottom: 6px;
}

.treemap-tooltip-hint {
    color: var(--accent-2);
    font-size: 0.75rem;
    font-style: italic;
    padding-top: 6px;
    border-top: 1px solid var(--border-strong);
}

/* Bar chart and file list: side by side on a wide screen */
.details {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 1.25rem;
    align-items: start;
}

@media (max-width: 1099px) {
    .details {
        grid-template-columns: minmax(0, 1fr);
    }
}

.chart-wrapper {
    animation: fadeSlideUp 0.5s ease-out 0.2s backwards;
}

.chart-container {
    position: relative;
    min-height: 140px;
}

/* File List */
.file-list-container {
    animation: fadeSlideUp 0.5s ease-out 0.25s backwards;
}

.file-list {
    max-height: 460px;
    overflow-y: auto;
}

.file-item {
    display: flex;
    align-items: center;
    padding: 0.6rem 0.9rem 0.6rem 1.1rem;
    border-bottom: 1px solid var(--surface);
    transition: background 0.2s;
    border-radius: 6px;
    margin-bottom: 2px;
    position: relative;
    overflow: hidden;
}

/* Share bar: a thin line along the row's bottom in the item's colour,
   the full row width being 100% */
.file-item::before {
    content: '';
    position: absolute;
    left: 0;
    bottom: 0;
    height: 3px;
    width: var(--percentage, 0%);
    border-radius: 2px;
    background-color: var(--swatch, var(--accent));
    opacity: 0.85;
    pointer-events: none;
    transition: width 0.3s ease;
}

/* Swatch: the item's colour, as in the treemap and the bar chart */
.file-item::after {
    content: '';
    position: absolute;
    left: 0.35rem;
    top: 0.55rem;
    bottom: 0.55rem;
    width: 4px;
    border-radius: 2px;
    background-color: var(--swatch, transparent);
    pointer-events: none;
}

.file-item:last-child {
    border-bottom: none;
}

.file-item.directory {
    cursor: pointer;
}

.file-item.directory:hover {
    background: var(--accent-hover);
}

.file-item.directory:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.file-item .icon {
    margin-right: 0.6rem;
    font-size: 1rem;
    position: relative;
    z-index: 1;
}

.file-item .name {
    flex: 1;
    min-width: 0;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    position: relative;
    z-index: 1;
    font-size: 0.88rem;
    color: var(--text);
}

.file-item .size {
    width: 84px;
    flex-shrink: 0;
    text-align: right;
    color: var(--text-soft);
    font-size: 0.8rem;
    position: relative;
    z-index: 1;
}

.file-item .percentage {
    width: 56px;
    flex-shrink: 0;
    text-align: right;
    color: var(--accent);
    font-weight: 600;
    font-size: 0.8rem;
    position: relative;
    z-index: 1;
}

/* A directory the scan could not read: its size reads "not read" */
.file-item.unread .size {
    color: var(--danger);
    font-style: italic;
}

.empty-message {
    color: var(--text-muted);
    text-align: center;
    padding: 2rem;
    font-size: 0.9rem;
}

/* Scrollbar */
.file-list::-webkit-scrollbar {
    width: 6px;
}

.file-list::-webkit-scrollbar-track {
    background: var(--scroll-track);
    border-radius: 3px;
}

.file-list::-webkit-scrollbar-thumb {
    background: var(--gradient-brand-v);
    border-radius: 3px;
}

.file-list::-webkit-scrollbar-thumb:hover {
    background: var(--gradient-brand-v-light);
}

/* Focus states for accessibility */
a:focus-visible,
button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Selection color */
::selection {
    background: var(--accent-select);
    color: var(--text-strong);
}

/* Narrow screens */
@media (max-width: 600px) {
    header h1 {
        font-size: 2.2rem;
    }

    .results-header {
        flex-wrap: wrap;
    }

    .summary-stats .summary-item {
        min-width: 0;
        flex: 1;
        padding: 0.5rem 0.7rem;
    }

    .summary-stats {
        width: 100%;
    }

    .treemap-wrapper,
    .chart-wrapper,
    .file-list-container {
        padding: 0.9rem;
    }

    .drop-zone {
        padding: 2rem 1rem;
    }
}

/* Reduced motion: no animations or transitions */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation: none !important;
        transition: none !important;
    }
}
