/* Design system: ui-ux-pro-max — Minimalism & Swiss, "folder blue + file amber",
   Baloo 2 headings + Atkinson Hyperlegible body. */

:root {
    --color-primary: #2563eb;
    --color-on-primary: #ffffff;
    --color-primary-soft: #dbeafe;
    --color-accent: #d97706;
    --color-background: #f8fafc;
    --color-foreground: #0f172a;
    --color-card: #ffffff;
    --color-muted: #f1f5fd;
    --color-muted-foreground: #475569;
    --color-border: #e2e8f0;
    --color-hover: #f1f5fd;
    --color-selected: #dbeafe;
    --color-ring: #2563eb;
    --color-viewer: #e2e8f0;

    --font-heading: "Baloo 2", "Atkinson Hyperlegible", system-ui, sans-serif;
    --font-body: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-6: 24px;
    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;
    --shadow: 0 1px 2px rgb(15 23 42 / .06), 0 1px 3px rgb(15 23 42 / .08);
    --ease: cubic-bezier(.2, 0, 0, 1);
    --dur-fast: 150ms;
    --dur: 220ms;
    --row-h: 34px;

    --tree-width: clamp(260px, 30%, 440px); /* left ≈ 30%, right ≈ 70%; draggable */
}

/* Dark theme: follows the system unless the toggle forced light (data-theme on <html>). */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --color-primary: #60a5fa;
        --color-on-primary: #0b1220;
        --color-primary-soft: #172554;
        --color-accent: #f59e0b;
        --color-background: #0b1220;
        --color-foreground: #e2e8f0;
        --color-card: #111a2e;
        --color-muted: #18233a;
        --color-muted-foreground: #a3b1c6;
        --color-border: #23314d;
        --color-hover: #18233a;
        --color-selected: #1e3a6e;
        --color-ring: #93c5fd;
        --color-viewer: #0b1220;
        --shadow: 0 1px 2px rgb(0 0 0 / .4);
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --color-primary: #60a5fa;
    --color-on-primary: #0b1220;
    --color-primary-soft: #172554;
    --color-accent: #f59e0b;
    --color-background: #0b1220;
    --color-foreground: #e2e8f0;
    --color-card: #111a2e;
    --color-muted: #18233a;
    --color-muted-foreground: #a3b1c6;
    --color-border: #23314d;
    --color-hover: #18233a;
    --color-selected: #1e3a6e;
    --color-ring: #93c5fd;
    --color-viewer: #0b1220;
    --shadow: 0 1px 2px rgb(0 0 0 / .4);
}

:root[data-theme="light"] { color-scheme: light; }

/* Theme toggle: show the moon in light mode, the sun in dark mode. */
.ic-sun { display: none; }
:root[data-theme="dark"] .ic-sun { display: block; }
:root[data-theme="dark"] .ic-moon { display: none; }

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .ic-sun { display: block; }
    :root:not([data-theme="light"]) .ic-moon { display: none; }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    background: var(--color-background);
    color: var(--color-foreground);
    font: 16px/1.5 var(--font-body);
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}

.sprite { display: none; }

.icon {
    width: 20px;
    height: 20px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

.skip-link {
    position: absolute;
    top: -100px;
    left: var(--space-4);
    z-index: 100;
    padding: var(--space-2) var(--space-4);
    color: var(--color-on-primary);
    background: var(--color-primary);
    border-radius: var(--radius-sm);
}

.skip-link:focus { top: var(--space-2); }

:focus-visible {
    outline: 2px solid var(--color-ring);
    outline-offset: 2px;
}

[hidden] { display: none !important; }

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

/* ---------- Buttons ---------- */

.btn, .icon-btn, .chip {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    font: inherit;
    font-size: .9375rem;
    color: var(--color-foreground);
    text-decoration: none;
    white-space: nowrap;
    background: var(--color-card);
    border: 1px solid var(--color-border);
    cursor: pointer;
    transition: background-color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

.btn {
    min-height: 40px;
    padding: 0 var(--space-4);
    border-radius: var(--radius);
}

.btn:hover, .icon-btn:hover, .chip:hover { background: var(--color-hover); border-color: var(--color-primary); }
.btn:active, .icon-btn:active { background: var(--color-selected); }

.btn-primary {
    color: var(--color-on-primary);
    background: var(--color-primary);
    border-color: var(--color-primary);
}

.btn-primary:hover { color: var(--color-on-primary); background: var(--color-primary); filter: brightness(1.08); }

.icon-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    border-color: transparent;
    border-radius: var(--radius);
    background: transparent;
}

/* ---------- Top bar ---------- */

.topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3) var(--space-6);
    padding: var(--space-3) var(--space-4);
}

.brand {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    color: inherit;
    text-decoration: none;
    border-radius: var(--radius);
}

.brand-mark {
    display: grid;
    place-items: center;
    width: 42px;
    height: 42px;
    color: var(--color-on-primary);
    background: var(--color-primary);
    border-radius: var(--radius);
}

.brand-mark .icon { width: 24px; height: 24px; }

.brand-text { display: flex; flex-direction: column; line-height: 1.15; }

.brand-title {
    font-family: var(--font-heading);
    font-size: 1.375rem;
    font-weight: 700;
}

.brand-sub { font-size: .8125rem; color: var(--color-muted-foreground); }

.searchbox {
    position: relative;
    display: flex;
    align-items: center;
    flex: 1 1 320px;
    max-width: 560px;
    margin-left: auto;
}

.search-icon {
    position: absolute;
    left: var(--space-3);
    color: var(--color-muted-foreground);
    pointer-events: none;
}

#search {
    width: 100%;
    height: 44px;
    padding: 0 44px 0 42px;
    font: inherit;
    color: inherit;
    background: var(--color-card);
    border: 1px solid var(--color-border);
    border-radius: 999px;
    transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

#search::placeholder { color: var(--color-muted-foreground); }
#search::-webkit-search-cancel-button { display: none; }

#search:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-soft);
}

#search-clear { position: absolute; right: 2px; width: 40px; height: 40px; border-radius: 999px; }

.tree-actions { display: flex; gap: var(--space-2); }
.btn-icon { width: 40px; padding: 0; }

/* ---------- Address bar ---------- */

.addressbar {
    display: flex;
    align-items: center;
    gap: var(--space-1);
    min-height: 44px;
    margin: 0 var(--space-4) var(--space-3);
    padding: var(--space-1) var(--space-2);
    background: var(--color-card);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
}

#breadcrumb {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    min-width: 0;
    margin: 0;
    padding: 0;
    list-style: none;
}

#breadcrumb li { display: flex; align-items: center; }

#breadcrumb li + li::before {
    content: "";
    width: 7px;
    height: 7px;
    margin: 0 var(--space-2);
    border-top: 1.5px solid var(--color-muted-foreground);
    border-right: 1.5px solid var(--color-muted-foreground);
    transform: rotate(45deg);
}

#breadcrumb a {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    min-height: 32px;
    padding: 0 var(--space-2);
    color: var(--color-muted-foreground);
    text-decoration: none;
    border-radius: var(--radius-sm);
    overflow-wrap: anywhere;
    transition: background-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}

#breadcrumb a:hover { color: var(--color-foreground); background: var(--color-hover); }
#breadcrumb li:last-child a { font-weight: 700; color: var(--color-foreground); }
#breadcrumb .icon { width: 18px; height: 18px; }

/* ---------- Panes ---------- */

.panes {
    display: flex;
    flex: 1;
    min-height: 0;
    margin: 0 var(--space-4);
    background: var(--color-card);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
}

#tree-pane {
    flex: 0 0 var(--tree-width);
    min-width: 0;
    padding: var(--space-2);
    overflow: auto;
    overscroll-behavior: contain;
}

#splitter {
    position: relative;
    z-index: 2; /* keep the widened hit area above the iframe */
    flex: 0 0 1px;
    background: var(--color-border);
    cursor: col-resize;
    touch-action: none;
}

#splitter::after {
    content: "";
    position: absolute;
    inset: 0 -4px;
}

#splitter:hover, #splitter:focus-visible, body.resizing #splitter {
    background: var(--color-primary);
    box-shadow: 0 0 0 1px var(--color-primary);
    outline: none;
}

body.resizing { cursor: col-resize; user-select: none; }
body.resizing iframe { pointer-events: none; }

#view-pane {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    outline: none;
}

/* ---------- Tree ---------- */

.tree { list-style: none; margin: 0; padding: 0; }
.tree .tree { margin-left: 17px; padding-left: var(--space-1); border-left: 1px solid var(--color-border); }

.row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: var(--row-h);
    padding: 0 var(--space-2);
    color: var(--color-foreground);
    text-decoration: none;
    white-space: nowrap;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color var(--dur-fast) var(--ease);
}

.row:hover { background: var(--color-hover); }
.row:focus-visible { outline-offset: -2px; }

summary { list-style: none; }
summary::-webkit-details-marker { display: none; }

.chev {
    width: 16px;
    height: 16px;
    color: var(--color-muted-foreground);
    transition: transform var(--dur) var(--ease);
}

details[open] > summary .chev { transform: rotate(90deg); }

.ic-folder, .ic-folder-open { color: var(--color-primary); }
.ic-folder-open, details[open] > summary .ic-folder { display: none; }
details[open] > summary .ic-folder-open { display: block; }

.ic-file { color: var(--color-accent); }
.file .row { padding-left: 32px; }

.tree .name { overflow: hidden; text-overflow: ellipsis; }

.selected > details > summary,
.selected > .row {
    background: var(--color-selected);
    box-shadow: inset 3px 0 0 var(--color-primary);
}

.selected > details > summary .name,
.selected > .row .name { font-weight: 700; }

.count {
    margin-left: auto;
    min-width: 28px;
    padding: 0 var(--space-2);
    font-size: .75rem;
    font-variant-numeric: tabular-nums;
    line-height: 20px;
    text-align: center;
    color: var(--color-muted-foreground);
    background: var(--color-muted);
    border-radius: 999px;
}

mark {
    color: inherit;
    background: rgb(245 158 11 / .35);
    border-radius: 2px;
}

.hidden { display: none !important; }

/* ---------- Empty search ---------- */

#no-results { padding: var(--space-4); }
.empty-title { margin: 0 0 var(--space-1); font-weight: 700; overflow-wrap: anywhere; }
.empty-hint { margin: 0 0 var(--space-2); color: var(--color-muted-foreground); }
.chips { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.chip { min-height: 36px; padding: 0 var(--space-3); border-radius: 999px; }

/* ---------- Home view ---------- */

#home-view, #folder-view { flex: 1; overflow: auto; }

#home-view { padding: var(--space-6); }

.home-title, .view-title {
    margin: 0;
    font-family: var(--font-heading);
    font-weight: 700;
    line-height: 1.2;
}

.home-title { font-size: 1.75rem; }
.home-sub { margin: var(--space-1) 0 var(--space-6); color: var(--color-muted-foreground); }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: var(--space-4);
}

.tile {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
    padding: var(--space-4);
    font: inherit;
    text-align: left;
    color: inherit;
    background: var(--color-card);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: border-color var(--dur-fast) var(--ease), background-color var(--dur-fast) var(--ease), transform var(--dur) var(--ease);
}

.tile:hover { border-color: var(--color-primary); background: var(--color-hover); transform: translateY(-2px); }

.tile-icon {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    color: var(--color-primary);
    background: var(--color-primary-soft);
    border-radius: var(--radius);
}

.tile-icon .icon { width: 24px; height: 24px; }

.tile-name { font-family: var(--font-heading); font-size: 1.25rem; font-weight: 700; line-height: 1.2; }
.tile-meta { font-size: .875rem; color: var(--color-muted-foreground); }

/* ---------- Folder view (details table) ---------- */

.view-title {
    padding: var(--space-4) var(--space-4) var(--space-2);
    font-size: 1.375rem;
    overflow-wrap: anywhere;
}

table.details { width: 100%; border-collapse: collapse; }

.details th {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: var(--space-2) var(--space-4);
    font-size: .8125rem;
    font-weight: 700;
    text-align: left;
    color: var(--color-muted-foreground);
    background: var(--color-card);
    border-bottom: 1px solid var(--color-border);
}

.details td {
    padding: var(--space-2) var(--space-4);
    white-space: nowrap;
    border-bottom: 1px solid var(--color-border);
}

.details td:first-child { width: 100%; white-space: normal; }
.details .num { text-align: right; font-variant-numeric: tabular-nums; }
.details td.num, .details td.type { font-size: .875rem; color: var(--color-muted-foreground); }

.details tbody tr { cursor: pointer; transition: background-color var(--dur-fast) var(--ease); }
.details tbody tr:hover { background: var(--color-hover); }

.cell-link {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 32px;
    padding: 0;
    font: inherit;
    text-align: left;
    color: inherit;
    background: none;
    border: 0;
    cursor: pointer;
}

.cell-link .icon { width: 22px; height: 22px; }

#folder-empty { margin: var(--space-4); color: var(--color-muted-foreground); }

/* ---------- File view ---------- */

#file-view { display: flex; flex: 1; flex-direction: column; min-height: 0; }

.file-toolbar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--color-border);
}

#file-name {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: 1rem;
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.viewer-wrap { position: relative; flex: 1; min-height: 0; background: var(--color-viewer); }

#viewer { display: block; width: 100%; height: 100%; border: 0; }

.loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-3);
    color: var(--color-muted-foreground);
    background: var(--color-viewer);
    transition: opacity var(--dur) var(--ease);
}

.loading.done { opacity: 0; pointer-events: none; }

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

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

#no-preview { margin: var(--space-4); color: var(--color-muted-foreground); }

/* ---------- Status bar ---------- */

.statusbar {
    display: flex;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-4);
    font-size: .8125rem;
    color: var(--color-muted-foreground);
}

#status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Tablet & phone ---------- */

@media (max-width: 1024px) {
    .tree-actions .btn span { display: none; }
    .tree-actions .btn { width: 44px; padding: 0; }
}

@media (max-width: 768px) {
    :root { --row-h: 44px; }

    .topbar { gap: var(--space-2); padding: var(--space-2) var(--space-3); }
    .brand-sub { display: none; }
    .brand-mark { width: 36px; height: 36px; }
    .brand-title { font-size: 1.2rem; }
    .searchbox { order: 3; flex-basis: 100%; max-width: none; }
    .tree-actions { margin-left: auto; }

    .addressbar, .panes { margin-left: var(--space-3); margin-right: var(--space-3); }
    .addressbar { margin-bottom: var(--space-2); }
    #breadcrumb { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
    #breadcrumb a { white-space: nowrap; overflow-wrap: normal; }

    #splitter { display: none; }
    #tree-pane { flex: 1 1 auto; }
    #view-pane { display: none; }
    body.viewing #tree-pane { display: none; }
    body.viewing #view-pane { display: flex; }
    body:not(.viewing) #back { display: none; }

    #home-view { padding: var(--space-4); }
    .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: var(--space-3); }
    .details th.type, .details td.type { display: none; }
    .file-toolbar { flex-wrap: wrap; }
    .file-toolbar .btn { flex: 1; min-height: 44px; }
    #file-name { flex-basis: calc(100% - 40px); white-space: normal; }
    .statusbar span:first-child { display: none; }
}

@media (min-width: 769px) {
    #back { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        transition-duration: 0ms !important;
        animation-duration: 0ms !important;
    }
    .tile:hover { transform: none; }
    .spinner { animation: none; }
}
