:root {
    --bg: #0f1117;
    --bg-elevated: #181b24;
    --bg-hover: #222633;
    --border: #2a2f3d;
    --text: #e8eaef;
    --text-muted: #8b92a5;
    --primary: #5b8def;
    --primary-hover: #4a7de0;
    --danger: #e55353;
    --danger-hover: #d04444;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    --topbar-h: 60px;
    --sidebar-w: 260px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

.screen {
    min-height: 100dvh;
}

/* Login */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background:
        radial-gradient(ellipse at 20% 0%, rgba(91, 141, 239, 0.12), transparent 50%),
        radial-gradient(ellipse at 80% 100%, rgba(91, 141, 239, 0.08), transparent 50%),
        var(--bg);
}

.login-card {
    width: 100%;
    max-width: 380px;
    padding: 40px 32px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-align: center;
}

.logo {
    display: block;
    width: 96px;
    height: 96px;
    margin: 0 auto 12px;
    object-fit: contain;
}

.topbar-logo {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    object-fit: contain;
    opacity: 0.92;
}

.login-card h1 {
    margin: 0 0 8px;
    font-size: 1.5rem;
    font-weight: 600;
}

.subtitle {
    margin: 0 0 28px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

#login-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#login-form input {
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.15s;
}

#login-form input:focus {
    border-color: var(--primary);
}

.error {
    margin-top: 12px;
    color: var(--danger);
    font-size: 0.875rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 18px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
    white-space: nowrap;
    touch-action: manipulation;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
    background: var(--primary);
    color: #fff;
}

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

.btn-ghost {
    background: transparent;
    color: var(--text);
    border: 1px solid var(--border);
}

.btn-ghost:hover { background: var(--bg-hover); }

.btn-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid rgba(229, 83, 83, 0.4);
}

.btn-danger:hover {
    background: rgba(229, 83, 83, 0.12);
}

.btn-sm {
    padding: 7px 14px;
    font-size: 0.8125rem;
}

.btn-icon {
    width: 40px;
    height: 40px;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text);
    font-size: 1.1rem;
    cursor: pointer;
}

.upload-label {
    cursor: pointer;
}

.mobile-only { display: none; }

/* Topbar */
.topbar {
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    height: var(--topbar-h);
    padding: 0 16px;
    background: rgba(15, 17, 23, 0.85);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.topbar h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* Layout */
.layout {
    display: flex;
    min-height: calc(100dvh - var(--topbar-h));
}

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    padding: 16px;
    border-right: 1px solid var(--border);
    background: var(--bg-elevated);
    display: flex;
    flex-direction: column;
}

.breadcrumbs {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
}

.crumb {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 6px 10px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-size: 0.8125rem;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.crumb:hover, .crumb.active {
    background: var(--bg-hover);
    color: var(--text);
}

.crumb-sep {
    color: var(--text-muted);
    font-size: 0.75rem;
    user-select: none;
}

.folder-tree {
    flex: 1;
    min-height: 0;
    margin-top: 12px;
    overflow: auto;
    padding-right: 4px;
}

.tree-node {
    margin-bottom: 1px;
}

.tree-row {
    display: flex;
    align-items: center;
    gap: 2px;
    min-height: 30px;
}

.tree-toggle,
.tree-toggle-spacer {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
}

.tree-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.7rem;
    line-height: 1;
    cursor: pointer;
}

.tree-toggle:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.tree-label {
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
    padding: 5px 8px 5px 2px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font-size: 0.8125rem;
    text-align: left;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.tree-label:hover,
.tree-node.active > .tree-row .tree-label,
.tree-node.ancestor > .tree-row .tree-label {
    background: var(--bg-hover);
    color: var(--text);
}

.tree-node.active > .tree-row .tree-label {
    color: var(--primary);
    font-weight: 500;
}

.tree-icon {
    flex-shrink: 0;
    font-size: 0.85rem;
    line-height: 1;
}

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

.sidebar-footer {
    margin-top: auto;
    padding-top: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.app-version {
    font-size: 0.6875rem;
    opacity: 0.6;
    letter-spacing: 0.02em;
}

.login-version {
    margin: 20px 0 0;
}

.main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-elevated);
}

.view-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
}

.view-bar-label {
    font-size: 0.8125rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.view-bar-right {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1;
    justify-content: flex-end;
}

.search-box {
    position: relative;
    flex: 1;
    max-width: 280px;
    min-width: 120px;
}

.search-icon {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 0.9rem;
    pointer-events: none;
}

.search-input {
    width: 100%;
    padding: 7px 12px 7px 30px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    color: var(--text);
    font-size: 0.8125rem;
    outline: none;
    transition: border-color 0.15s;
}

.search-input::placeholder {
    color: var(--text-muted);
}

.search-input:focus {
    border-color: var(--primary);
}

.search-input::-webkit-search-cancel-button {
    -webkit-appearance: none;
}

.view-toggle {
    display: flex;
    gap: 4px;
    padding: 3px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
}

.view-btn {
    width: 32px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 5px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.9rem;
    line-height: 1;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.view-btn:hover {
    color: var(--text);
    background: var(--bg-hover);
}

.view-btn.active {
    color: #fff;
    background: var(--primary);
}

/* Dropzone & file list */
.dropzone {
    position: relative;
    flex: 1;
    overflow: auto;
    padding: 16px;
    padding-bottom: calc(16px + var(--safe-bottom));
}

.dropzone.marquee-active {
    user-select: none;
    cursor: crosshair;
}

.drop-overlay {
    position: absolute;
    inset: 8px;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(91, 141, 239, 0.12);
    border: 2px dashed var(--primary);
    border-radius: var(--radius);
    pointer-events: none;
}

.drop-message {
    text-align: center;
}

.drop-icon {
    font-size: 2.5rem;
    display: block;
    margin-bottom: 8px;
}

.drop-message p {
    margin: 0;
    font-size: 1.1rem;
    color: var(--primary);
    font-weight: 500;
}

.file-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
    gap: 10px;
}

.file-list.view-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.list-header,
.file-list.view-list .file-item-list {
    display: grid;
    grid-template-columns: 28px 36px 1fr 88px 148px 32px;
    align-items: center;
    gap: 10px;
}

.list-header {
    padding: 8px 14px;
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    margin-bottom: 2px;
}

.file-check {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.file-check-input {
    width: 15px;
    height: 15px;
    margin: 0;
    accent-color: var(--primary);
    cursor: pointer;
}

.file-check-spacer,
.file-actions-spacer {
    width: 15px;
}

.file-actions-btn {
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s, background 0.15s, color 0.15s;
}

.file-item-list:hover .file-actions-btn,
.file-item-list.selected .file-actions-btn {
    opacity: 1;
}

.file-actions-btn:hover {
    background: var(--bg-hover);
    color: var(--text);
}

.folder-up-row {
    border-color: transparent;
    background: transparent;
}

.folder-up-row:hover {
    background: var(--bg-hover);
}

.folder-up-icon {
    color: var(--primary);
    font-size: 1rem;
}

.folder-up-name {
    color: var(--primary);
    font-weight: 500;
    text-align: left;
}

.list-empty {
    padding: 40px 20px;
}

.file-list.view-list .file-item-list {
    flex-direction: row;
    padding: 7px 14px;
    gap: 10px;
    border: none;
    border-radius: 0;
    border-bottom: 1px solid var(--border);
    background: transparent;
}

.file-list.view-list .file-item-list:hover {
    background: var(--bg-hover);
}

.file-list.view-list .file-item-list.selected {
    background: rgba(91, 141, 239, 0.1);
    border-bottom-color: var(--border);
}

.file-list.view-list .file-item-list.has-thumb {
    padding: 5px 14px;
}

.file-list.view-list .file-item-list:active {
    transform: none;
}

.file-list.view-list .file-thumb {
    width: 36px;
    height: 36px;
    aspect-ratio: 1;
    flex-shrink: 0;
}

.file-list.view-list .file-icon {
    width: 36px;
    font-size: 1.25rem;
    text-align: center;
    flex-shrink: 0;
}

.file-list.view-list .file-name {
    text-align: left;
    -webkit-line-clamp: 1;
}

.file-list.view-list .file-meta-size,
.file-list.view-list .file-meta-date {
    text-align: right;
    font-size: 0.75rem;
    white-space: nowrap;
}

.file-item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 16px 12px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    background: var(--bg-elevated);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, transform 0.1s;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}

.file-item.has-thumb {
    padding: 8px;
    gap: 6px;
}

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

.file-item.selected {
    border-color: var(--primary);
    background: rgba(91, 141, 239, 0.1);
}

.file-item:active {
    transform: scale(0.98);
}

.file-thumb {
    width: 100%;
    aspect-ratio: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border-radius: var(--radius-sm);
    background: var(--bg);
}

.thumb-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    pointer-events: none;
}

.file-icon {
    font-size: 2rem;
    line-height: 1;
}

.file-name {
    width: 100%;
    text-align: center;
    font-size: 0.8125rem;
    line-height: 1.3;
    word-break: break-word;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.file-meta {
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* Marquee selection */
.marquee {
    position: absolute;
    z-index: 40;
    border: 1px solid var(--primary);
    background: rgba(91, 141, 239, 0.15);
    border-radius: 4px;
    pointer-events: none;
}

.file-item.marquee-candidate {
    border-color: rgba(91, 141, 239, 0.5);
    background: rgba(91, 141, 239, 0.06);
}

/* Context menu */
.context-menu {
    position: fixed;
    z-index: 200;
    min-width: 180px;
    padding: 6px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
}

.context-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 12px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text);
    font-size: 0.8125rem;
    text-align: left;
    cursor: pointer;
    text-decoration: none;
}

.context-menu-item:hover,
.context-menu-item:focus {
    background: var(--bg-hover);
    outline: none;
}

.context-menu-item.danger {
    color: var(--danger);
}

.context-menu-item.danger:hover {
    background: rgba(229, 83, 83, 0.12);
}

.context-menu-sep {
    height: 1px;
    margin: 4px 8px;
    background: var(--border);
}

.context-menu-item.disabled {
    opacity: 0.4;
    pointer-events: none;
}

/* Upload overlay */
.upload-overlay {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
}

.upload-card {
    width: min(360px, calc(100vw - 48px));
    padding: 28px 24px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-align: center;
}

.upload-spinner {
    width: 40px;
    height: 40px;
    margin: 0 auto 16px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.upload-status {
    margin: 0 0 6px;
    font-size: 1rem;
    font-weight: 600;
}

.upload-filename {
    margin: 0 0 16px;
    font-size: 0.8125rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.upload-progress-track {
    height: 6px;
    margin-bottom: 10px;
    background: var(--bg);
    border-radius: 3px;
    overflow: hidden;
}

.upload-progress-bar {
    height: 100%;
    width: 0%;
    background: var(--primary);
    border-radius: 3px;
    transition: width 0.2s ease;
}

.upload-count {
    margin: 0;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.upload-label.uploading {
    opacity: 0.6;
    pointer-events: none;
}

.empty-state {
    grid-column: 1 / -1;
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
}

.empty-state .empty-icon {
    font-size: 3rem;
    margin-bottom: 12px;
    opacity: 0.5;
}

/* Modal */
.modal {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0;
    background: var(--bg-elevated);
    color: var(--text);
    box-shadow: var(--shadow);
    max-width: calc(100vw - 32px);
    width: 400px;
}

.modal::backdrop {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
}

.modal form {
    padding: 24px;
}

.modal h2 {
    margin: 0 0 16px;
    font-size: 1.1rem;
}

.modal input {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    color: var(--text);
    font-size: 1rem;
    outline: none;
}

.modal input:focus {
    border-color: var(--primary);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 20px;
}

/* Image preview lightbox */
.modal-preview {
    position: relative;
    width: min(92vw, 900px);
    padding: 0;
    overflow: hidden;
    border: none;
}

.modal-preview .preview-close {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 2;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
}

.modal-preview .preview-body {
    display: flex;
    align-items: center;
    justify-content: center;
    max-height: 75vh;
    background: #000;
}

.modal-preview #preview-image {
    display: block;
    max-width: 100%;
    max-height: 75vh;
    object-fit: contain;
}

.modal-preview .preview-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px;
    border-top: 1px solid var(--border);
    background: var(--bg-elevated);
}

.preview-name {
    font-size: 0.875rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
}

.preview-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

/* Hover preview (desktop) */
.hover-preview {
    position: fixed;
    z-index: 150;
    pointer-events: none;
    width: 240px;
    max-height: 240px;
    padding: 4px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    transform: translate(-50%, -100%);
}

.hover-preview img {
    display: block;
    width: 100%;
    max-height: 232px;
    object-fit: contain;
    border-radius: 4px;
}

/* Toast */
.toast {
    position: fixed;
    bottom: calc(24px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    padding: 12px 20px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
    font-size: 0.875rem;
    max-width: calc(100vw - 32px);
    animation: toast-in 0.25s ease;
}

.toast.error {
    border-color: rgba(229, 83, 83, 0.5);
    color: var(--danger);
}

@keyframes toast-in {
    from { opacity: 0; transform: translateX(-50%) translateY(12px); }
    to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* Loading */
.loading {
    grid-column: 1 / -1;
    text-align: center;
    padding: 40px;
    color: var(--text-muted);
}

/* Mobile */
@media (max-width: 768px) {
    .mobile-only { display: inline-flex; }

    .topbar-actions .btn-ghost:last-child {
        display: none;
    }

    .topbar-actions {
        gap: 6px;
    }

    .btn {
        padding: 9px 14px;
        font-size: 0.8125rem;
    }

    .sidebar {
        position: fixed;
        top: var(--topbar-h);
        left: 0;
        bottom: 0;
        z-index: 90;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: var(--shadow);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .file-list {
        grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
        gap: 8px;
    }

    .file-item {
        padding: 14px 10px;
    }

    .hover-preview {
        display: none !important;
    }

    .view-bar {
        flex-wrap: wrap;
    }

    .view-bar-right {
        width: 100%;
    }

    .search-box {
        max-width: none;
    }

    .list-header,
    .file-list.view-list .file-item-list {
        grid-template-columns: 24px 32px 1fr 64px 32px;
    }

    .list-header-size,
    .list-header-date,
    .file-list.view-list .file-meta-size,
    .file-list.view-list .file-meta-date {
        display: none;
    }
}

@media (max-width: 480px) {
    .topbar h1 {
        font-size: 0.95rem;
    }

    #btn-new-folder {
        display: none;
    }
}
