:root {
    --bg: #d6cfc4;
    --bg-deep: #c4bbb0;
    --panel: #f6f1ea;
    --panel-2: #efe8dd;
    --ink: #1c1610;
    --muted: #6d6459;
    --line: #d8cfc3;
    --accent: #0f5c4c;
    --accent-2: #147a66;
    --sienna: #c45c26;
    --danger: #9b2c2c;
    --shadow: 0 18px 40px rgba(40, 28, 16, 0.12);
    --radius: 16px;
    --font: "IBM Plex Sans", "Segoe UI", sans-serif;
    --serif: "Libre Baskerville", Georgia, serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: var(--font);
    color: var(--ink);
    background:
        radial-gradient(1200px 500px at 10% -10%, rgba(20, 122, 102, 0.08), transparent 50%),
        radial-gradient(900px 400px at 100% 0%, rgba(196, 92, 38, 0.08), transparent 45%),
        var(--bg);
    letter-spacing: -0.01em;
}

button, input, textarea { font-family: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.brand-mark {
    font-family: var(--serif);
    font-weight: 700;
    letter-spacing: 0.02em;
    margin: 0;
    font-size: 1.15rem;
    white-space: nowrap;
}

.btn {
    appearance: none;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    border-radius: 999px;
    padding: 0.45rem 0.9rem;
    font-weight: 600;
    font-size: 0.85rem;
    transition: 0.15s ease;
    white-space: nowrap;
}
.btn:hover { border-color: var(--ink); }
.btn.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #f6f1ea;
}
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
.btn.ghost { background: transparent; }
.btn.small { padding: 0.28rem 0.7rem; font-size: 0.78rem; }
.btn.danger { color: var(--danger); border-color: #e8c4c4; background: #fff6f6; }
.btn.icon {
    width: 32px;
    height: 32px;
    padding: 0;
    display: inline-grid;
    place-items: center;
    border-radius: 10px;
}

.gate {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}
.gate-card {
    width: min(420px, 100%);
    background: var(--panel);
    border-radius: 28px;
    padding: 40px 36px 32px;
    box-shadow: var(--shadow);
    border: 1px solid rgba(255,255,255,0.6);
}
.gate-card h1 { margin: 10px 0 6px; font-size: 1.6rem; }
.muted { color: var(--muted); margin: 0; font-size: 0.92rem; }
.field { display: grid; gap: 8px; margin: 22px 0 16px; font-weight: 600; font-size: 0.85rem; }
.field input, #promptInput {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px 14px;
    font-size: 1rem;
    background: #fff;
}
.error { color: var(--danger); font-size: 0.88rem; margin: 10px 0 0; }
.gate-card .btn { width: 100%; padding: 12px; }

.app {
    display: grid;
    grid-template-columns: 420px minmax(0, 1fr) 280px;
    grid-template-rows: auto 1fr;
    min-height: 100vh;
    gap: 16px;
    padding: 16px;
}
.topbar {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    gap: 16px;
    background: var(--panel);
    border-radius: 22px;
    padding: 10px 14px 10px 20px;
    box-shadow: var(--shadow);
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 180px; }
.brand-sub { color: var(--muted); font-size: 0.78rem; text-transform: lowercase; }
.search {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--panel-2);
    border-radius: 999px;
    padding: 8px 14px;
    color: var(--muted);
}
.search input {
    border: 0;
    background: transparent;
    outline: none;
    width: 100%;
    font-size: 0.95rem;
    color: var(--ink);
}
.edit-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 0.85rem;
    user-select: none;
    padding: 4px 10px;
    border-radius: 999px;
    border: 1px solid var(--line);
    background: #fff;
}
.edit-toggle input { accent-color: var(--accent); width: 16px; height: 16px; }
.app.is-editing .edit-toggle { border-color: var(--accent); background: #eef7f4; }
.edit-hint {
    margin: 0 12px 8px;
    font-size: 0.78rem;
    color: var(--muted);
}
.app.is-editing .folder-item { cursor: grab; }
.app.is-editing .folder-item.is-dragging { opacity: 0.45; }
.drop-over {
    outline: 2px dashed var(--accent);
    background: #eef7f4 !important;
}

.sidebar, .pool, .stage {
    background: var(--panel);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}
.sidebar, .pool {
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 110px);
    overflow: hidden;
}
.side-head, .pool-head, .crumbbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 16px 16px 10px;
}
.side-head h2, .pool h2 { margin: 0; font-size: 0.95rem; }
.folder-list {
    overflow: auto;
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.folder-item, .folder-row {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    text-align: left;
    border: 1px solid transparent;
    background: transparent;
    border-radius: 14px;
    padding: 8px;
    color: inherit;
}
.folder-item:hover, .folder-item.active,
.folder-row:hover, .folder-row.active {
    background: #fff;
    border-color: var(--line);
}
.folder-row.all { margin: 0 8px; width: calc(100% - 16px); }
.folder-open {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 10px;
    border: 0;
    background: transparent;
    color: inherit;
    padding: 0;
    text-align: left;
}
.folder-icon {
    width: 34px;
    height: 26px;
    border-radius: 0 8px 8px 8px;
    background: linear-gradient(180deg, #f0d7a4, #e2c27a);
    position: relative;
    flex-shrink: 0;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.5);
}
.folder-icon::before {
    content: "";
    position: absolute;
    left: 0;
    top: -7px;
    width: 18px;
    height: 10px;
    background: #e8cc88;
    border-radius: 4px 6px 0 0;
}
.folder-icon.all-icon {
    background: linear-gradient(180deg, #9fd4c6, #5eaa97);
}
.folder-icon.all-icon::before { background: #7ec2b2; }
.folder-meta { display: grid; min-width: 0; flex: 1; }
.folder-meta strong { font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-meta em { font-style: normal; color: var(--muted); font-size: 0.75rem; }
.folder-tools {
    display: flex;
    flex-shrink: 0;
}
.folder-menu-btn {
    width: 32px;
    height: 32px;
    border: 1px solid transparent;
    background: transparent;
    border-radius: 10px;
    font-size: 1.15rem;
    line-height: 1;
    color: var(--ink);
    letter-spacing: 0;
}
.folder-item:hover .folder-menu-btn,
.folder-item.active .folder-menu-btn,
.folder-menu-btn[aria-expanded="true"] {
    background: #fff;
    border-color: var(--line);
}
.folder-pop {
    position: fixed;
    z-index: 80;
    min-width: 220px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: var(--shadow);
    padding: 6px;
    display: none;
}
.folder-pop.is-open {
    display: grid;
}
.folder-pop button {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    border-radius: 10px;
    padding: 10px 12px;
    font-weight: 600;
    font-size: 0.86rem;
    color: var(--ink);
}
.folder-pop button:hover { background: var(--panel-2); }
.folder-pop button.danger { color: var(--danger); }
.twisty, .twisty-spacer {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
    border: 0;
    background: transparent;
    padding: 0;
    border-radius: 6px;
    position: relative;
}
.twisty:hover { background: var(--panel-2); }
.twisty::before {
    content: "";
    position: absolute;
    inset: 6px;
    border-left: 2px solid var(--ink);
    border-bottom: 2px solid var(--ink);
    transform: rotate(-45deg);
    transform-origin: 50% 50%;
    width: 8px;
    height: 8px;
    box-sizing: border-box;
    left: 7px;
    top: 5px;
}
.twisty[aria-expanded="true"]::before {
    transform: rotate(45deg);
    top: 4px;
    left: 6px;
}
.hint {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 18px 20px;
    color: var(--muted);
    margin-bottom: 16px;
}

.stage { padding: 8px 16px 20px; min-height: calc(100vh - 110px); overflow: auto; }
.crumbbar { padding: 12px 0 8px; }
.crumb-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    align-items: center;
    margin: 4px 0 2px;
    font-size: 0.82rem;
}
.crumb-nav button {
    background: none;
    border: 0;
    padding: 0;
    color: var(--accent);
    font-weight: 600;
}
.crumb-nav button:hover { text-decoration: underline; }
.crumb-nav span { color: var(--muted); }
.folder-item { --d: 0; }
.folder-item .folder-open { padding-left: 0; }
.folder-item { padding-left: calc(6px + var(--d) * 16px); }
.folder-node { min-width: 0; }
.folder-kids {
    display: none;
    flex-direction: column;
    gap: 4px;
}
.folder-kids.is-open {
    display: flex;
}
.subgrid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(168px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}
.subcard {
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 12px;
    text-align: left;
    color: inherit;
}
.subcard:hover { border-color: var(--accent); }
.subcard .folder-icon { width: 48px; height: 36px; }
.subcard strong { font-size: 0.9rem; }
.subcard em { font-style: normal; color: var(--muted); font-size: 0.75rem; }
.subcard-tools { display: flex; gap: 4px; }
.eyebrow { margin: 0; text-transform: uppercase; letter-spacing: 0.12em; font-size: 0.68rem; color: var(--muted); font-weight: 700; }
.crumb-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.dropzone {
    border: 1.5px dashed #cbbba6;
    border-radius: 18px;
    background:
        repeating-linear-gradient(-45deg, transparent, transparent 8px, rgba(15,92,76,0.03) 8px, rgba(15,92,76,0.03) 16px),
        #fff;
    padding: 22px;
    margin-bottom: 16px;
}
.dropzone.over { border-color: var(--accent); background-color: #eef7f4; }
.dropzone-inner { display: grid; justify-items: center; gap: 6px; text-align: center; }
.dropzone-inner span { color: var(--muted); font-size: 0.85rem; }
.dropzone .btn { margin-top: 8px; }
.drop-actions { display: flex; gap: 8px; justify-content: center; }
.url-import {
    margin-top: 18px;
    padding-top: 16px;
    border-top: 1px dashed #d5cbbd;
    display: grid;
    gap: 8px;
    text-align: left;
}
.url-import label { font-weight: 700; font-size: 0.88rem; }
.url-import label em { font-style: normal; font-weight: 500; color: var(--muted); margin-left: 8px; font-size: 0.78rem; }
.url-import textarea {
    width: 100%;
    min-height: 88px;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px 12px;
    font-size: 0.82rem;
    resize: vertical;
    background: #fff;
}
.url-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.url-actions .btn { margin-top: 0; }
.fetch-list { display: grid; gap: 8px; }
.fetch-row {
    display: grid;
    grid-template-columns: 56px minmax(0, 1fr) auto;
    gap: 10px;
    align-items: center;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 8px;
}
.fetch-row img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; background: #ece6dc; }
.fetch-row.err { border-color: #e8c4c4; }
.fetch-meta { min-width: 0; }
.fetch-meta strong, .fetch-meta span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fetch-meta span { color: var(--muted); font-size: 0.75rem; }
.fetch-tools { display: flex; gap: 6px; flex-wrap: wrap; }
.progress { margin-top: 14px; }
.progress-bar {
    height: 8px;
    border-radius: 99px;
    background: var(--panel-2);
    overflow: hidden;
    position: relative;
}
.progress-bar::after {
    content: "";
    position: absolute;
    inset: 0 auto 0 0;
    width: var(--w, 0%);
    background: var(--accent);
    border-radius: inherit;
}
#progressText { font-size: 0.8rem; color: var(--muted); }

.bulkbar {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    background: #1c1610;
    color: #f6f1ea;
    border-radius: 14px;
    padding: 8px 12px;
    margin-bottom: 14px;
}
.bulkbar .btn { background: #2a231c; color: #f6f1ea; border-color: #3a322a; }
.bulkbar .btn.danger { background: #9b2c2c; border-color: #9b2c2c; color: #fff; }

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 14px;
}
.grid.compact {
    grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
    gap: 10px;
}
.grid.compact .card-body { padding: 8px; gap: 4px; }
.grid.compact .card-body strong { font-size: 0.72rem; }
.grid.compact .card-actions .btn { padding: 0.2rem 0.5rem; font-size: 0.7rem; }
.card {
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    border: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    position: relative;
}
.card.selected { outline: 2px solid var(--accent); }
.thumb {
    aspect-ratio: 1;
    background: #ece6dc;
    overflow: hidden;
    cursor: zoom-in;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-body { padding: 10px 10px 12px; display: grid; gap: 6px; }
.card-body strong {
    font-size: 0.8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.card-body small { color: var(--muted); font-size: 0.72rem; }
.card-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.select {
    position: absolute;
    top: 8px;
    left: 8px;
    width: 22px;
    height: 22px;
    accent-color: var(--accent);
}
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

.pool-list {
    flex: 1;
    margin: 0 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 6px;
    background: #fff;
    min-height: 220px;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.pool-empty {
    margin: auto;
    padding: 20px 12px;
    color: var(--muted);
    font-size: 0.82rem;
    text-align: center;
}
.pool-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    padding: 6px 8px;
    border-radius: 10px;
}
.pool-row:hover { background: var(--panel-2); }
.pool-url {
    font-size: 0.72rem;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--ink);
}
.pool-foot {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
    padding: 10px 12px 14px;
}
.pool-foot span { margin-right: auto; font-size: 0.78rem; color: var(--muted); }

.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: #1c1610;
    color: #f6f1ea;
    padding: 10px 16px;
    border-radius: 999px;
    z-index: 50;
    font-size: 0.88rem;
    box-shadow: var(--shadow);
}

dialog {
    border: 0;
    border-radius: 20px;
    padding: 18px;
    background: var(--panel);
    box-shadow: var(--shadow);
    max-width: min(92vw, 920px);
}
dialog::backdrop { background: rgba(28, 22, 16, 0.55); }
#lightbox {
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
    max-width: none;
    width: 100vw;
    height: 100vh;
    max-height: none;
}
#lightbox[open] {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    justify-items: center;
    gap: 12px;
    padding: 16px;
}
#lightbox::backdrop { background: rgba(28, 22, 16, 0.62); }
.lightbox-box {
    position: relative;
    background: var(--panel);
    border-radius: 18px;
    padding: 44px 18px 16px;
    box-shadow: var(--shadow);
    text-align: center;
    grid-column: 2;
}
.lightbox-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.lb-nav {
    width: 48px;
    height: 48px;
    border: 0;
    border-radius: 50%;
    background: #1c1610;
    color: #f6f1ea;
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    display: grid;
    place-items: center;
    padding-bottom: 4px;
}
.lb-nav:hover { background: var(--accent); }
.lb-nav[hidden] { display: none !important; }
#lightbox::backdrop { background: rgba(28, 22, 16, 0.62); }
.lightbox-box {
    position: relative;
    background: var(--panel);
    border-radius: 18px;
    padding: 44px 18px 16px;
    box-shadow: var(--shadow);
    text-align: center;
}
#lightbox img { max-width: min(86vw, 920px); max-height: 74vh; border-radius: 12px; display: block; margin: 0 auto 12px; }
.lightbox-x {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 10px;
    background: #1c1610;
    color: #f6f1ea;
    font-size: 1.4rem;
    line-height: 1;
    display: grid;
    place-items: center;
    cursor: pointer;
}
.lightbox-x:hover { background: var(--danger); }
#promptForm { display: grid; gap: 12px; min-width: min(92vw, 360px); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }

@media (max-width: 1100px) {
    .app {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto auto;
    }
    .sidebar, .pool { min-height: 0; }
    .folder-list { max-height: 240px; }
    .pool { order: 3; }
    .pool-list { min-height: 140px; }
}
