/* === Reset / base === */
:root {
    color-scheme: light dark;
    --bg: #f8fafc;
    --surface: #ffffff;
    --surface-2: #f1f5f9;
    --border: #e2e8f0;
    --text: #0f172a;
    --text-soft: #475569;
    --accent: #4f46e5;
    --accent-hover: #4338ca;
    --danger: #dc2626;
    --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
}
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f172a;
        --surface: #1e293b;
        --surface-2: #334155;
        --border: #334155;
        --text: #f1f5f9;
        --text-soft: #94a3b8;
        --accent: #818cf8;
        --accent-hover: #6366f1;
    }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
}
#app { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* === Forms (login/register) === */
.center-card {
    flex: 1;
    display: grid;
    place-items: center;
    padding: 1.5rem;
}
.card {
    background: var(--surface);
    border-radius: 16px;
    padding: 1.5rem;
    box-shadow: var(--shadow);
    width: 100%;
    max-width: 360px;
}
.card h1 { margin: 0 0 1rem; font-size: 1.5rem; letter-spacing: -.02em; }
.card p { color: var(--text-soft); margin: .5rem 0 1rem; font-size: .9rem; }
label { display: block; font-size: .85rem; color: var(--text-soft); margin-top: 1rem; }
input[type="text"], input[type="email"], input[type="password"], input[type="number"] {
    width: 100%;
    padding: .65rem .8rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    margin-top: .25rem;
    -webkit-appearance: none;
    appearance: none;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .5em;
    min-height: 44px;
    padding: 0 1.2rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    transition: background .15s, border-color .15s;
    user-select: none;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-block { width: 100%; margin-top: 1rem; }
.btn-icon {
    width: 44px;
    padding: 0;
    aspect-ratio: 1;
}
.error {
    color: var(--danger);
    font-size: .85rem;
    margin-top: .5rem;
    min-height: 1.2em;
}
.muted-link {
    margin-top: 1rem;
    text-align: center;
    font-size: .9rem;
    color: var(--text-soft);
}
.muted-link a { color: var(--accent); text-decoration: none; }

/* === Project list === */
.topbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: .8rem 1.2rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: sticky; top: 0; z-index: 10;
}
.topbar h1 {
    flex: 1;
    margin: 0;
    font-size: 1.1rem;
    letter-spacing: -.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar .icon-row { display: flex; gap: .5rem; }

.project-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1rem;
    padding: 1.2rem;
}
.project-card {
    background: var(--surface);
    border-radius: 14px;
    padding: 1rem;
    box-shadow: var(--shadow);
    cursor: pointer;
    transition: transform .15s;
    display: flex;
    flex-direction: column;
    gap: .25rem;
}
.project-card:hover { transform: translateY(-2px); }
.project-card h2 {
    margin: 0;
    font-size: 1rem;
    letter-spacing: -.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.project-card .meta { color: var(--text-soft); font-size: .8rem; }
.project-card .preview {
    margin-bottom: .5rem;
    aspect-ratio: 1;
    background: var(--surface-2);
    border-radius: 8px;
    border: 1px solid var(--border);
}
.empty-state {
    padding: 3rem 1rem;
    text-align: center;
    color: var(--text-soft);
}

/* === Editor === */
.editor {
    flex: 1;
    min-height: 0;       /* allow grid tracks to shrink — prevents overflow */
    display: grid;
    /* minmax(0, 1fr) lets the canvas row shrink below the toolbar/palette
     * content height; the strips then scroll internally. Plain `1fr` would
     * grow the row to fit the longest sidebar and push the page beyond the
     * viewport. */
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    background: var(--surface-2);
    overflow: hidden;
}
.editor canvas {
    display: block;
    background: var(--surface);
    width: 100%;
    height: 100%;
    touch-action: none;
}
.canvas-wrap {
    position: relative;
    overflow: hidden;
    min-height: 0;       /* grid cells default to min-height:auto otherwise */
    min-width: 0;
}
.toolbar {
    display: flex;
    align-items: center;
    gap: .35rem;
    padding: .5rem .6rem;
    background: var(--surface);
    border-top: 1px solid var(--border);
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}
.tool {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
}
.tool.active { background: var(--accent); border-color: var(--accent); color: white; }
.tool:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tool-divider {
    flex: 0 0 auto;
    width: 1px;
    height: 28px;
    background: var(--border);
    margin: 0 .25rem;
}
.tool-spacer { flex: 1; }
.color-strip {
    display: flex;
    gap: .35rem;
    padding: .35rem .6rem;
    background: var(--surface-2);
    border-top: 1px solid var(--border);
    overflow-x: auto;
    overflow-y: hidden;
}
.swatch {
    flex: 0 0 auto;
    width: 36px; height: 36px;
    border-radius: 50%;
    border: 2px solid transparent;
    background: var(--c, #ccc);
    cursor: pointer;
    box-shadow: 0 0 0 1px var(--border) inset;
}
.swatch.active { border-color: var(--text); }

/* === Wide layout (iPad landscape, desktop) ===
 * Topbar stays at the top; toolbar moves to the left edge as a vertical strip;
 * color palette moves to the right edge (also vertical). Canvas takes the
 * middle, growing in both axes. Triggered at 900px (covers iPad landscape
 * and bigger; iPad portrait keeps the phone-style bottom layout). */
@media (min-width: 900px) {
    .editor {
        grid-template-areas:
            "topbar topbar topbar"
            "tools  canvas swatches";
        grid-template-columns: auto 1fr auto;
        /* minmax(0, 1fr) again — the toolbar/palette tracks would otherwise
         * inflate the row to their content height. */
        grid-template-rows: auto minmax(0, 1fr);
    }
    .editor > .topbar      { grid-area: topbar; }
    .editor > .canvas-wrap { grid-area: canvas; }
    .editor > .toolbar     { grid-area: tools; }
    .editor > .color-strip { grid-area: swatches; }

    .toolbar {
        flex-direction: column;
        align-items: center;
        padding: .6rem .5rem;
        gap: .35rem;
        border-top: none;
        border-right: 1px solid var(--border);
        overflow-x: hidden;
        overflow-y: auto;
        min-width: 64px;
        /* allow this grid item to shrink so its overflow-y can take over */
        min-height: 0;
        max-height: 100%;
    }
    .tool-divider {
        width: 28px;
        height: 1px;
        margin: .25rem 0;
    }
    .tool-spacer {
        flex: 1;
        width: 1px;
        height: auto;
        min-height: .5rem;
    }

    .color-strip {
        flex-direction: column;
        align-items: center;
        padding: .5rem .35rem;
        gap: .35rem;
        border-top: none;
        border-left: 1px solid var(--border);
        overflow-x: hidden;
        overflow-y: auto;
        min-width: 56px;
        min-height: 0;
        max-height: 100%;
    }
}

/* Save indicator */
.save-status {
    color: var(--text-soft);
    font-size: .8rem;
    padding: 0 .5rem;
}
.save-status.saving { color: var(--accent); }
.save-status.error { color: var(--danger); }

/* Tiny SVG icons styling */
.icon { width: 22px; height: 22px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Divider with text in login card */
.divider {
    display: flex;
    align-items: center;
    gap: .6rem;
    margin: 1.2rem 0 .8rem;
    color: var(--text-soft);
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .1em;
}
.divider::before, .divider::after {
    content: "";
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* Guest banner — shown above content for unupgraded guests */
.guest-banner {
    margin: 1rem 1.2rem 0;
    padding: .8rem 1rem;
    background: rgba(99, 102, 241, .1);
    border: 1px solid rgba(99, 102, 241, .3);
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: .75rem;
    flex-wrap: wrap;
    color: var(--text);
    font-size: .9rem;
}
.guest-banner > span { flex: 1; min-width: 12em; }
.guest-banner .btn { margin: 0; }

/* Modal — generic full-screen overlay */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(15, 23, 42, .5);
    display: grid;
    place-items: center;
    z-index: 100;
    padding: 1rem;
}
.modal-overlay .card { max-width: 420px; }
.modal-overlay .card h1 { font-size: 1.25rem; }

/* Popovers (Snap, Symmetry) */
.popover-backdrop {
    position: fixed;
    inset: 0;
    z-index: 90;
    background: rgba(15, 23, 42, .25);
}
.popover {
    background: var(--surface);
    border-radius: 16px;
    box-shadow: 0 12px 36px rgba(15,23,42,.3);
    width: min(360px, calc(100vw - 16px));
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.popover-bottom-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    border-radius: 18px 18px 0 0;
    max-height: 70vh;
    animation: popover-slide-up .2s ease;
}
@keyframes popover-slide-up { from { transform: translateY(100%); } to { transform: none; } }

.popover-header {
    display: flex;
    align-items: center;
    padding: .8rem 1rem .4rem;
    border-bottom: 1px solid var(--border);
}
.popover-header h2 {
    flex: 1;
    margin: 0;
    font-size: 1.1rem;
    letter-spacing: -.01em;
}
.popover-close { width: 36px; height: 36px; }

.popover-body {
    overflow-y: auto;
    padding: .5rem 0;
}
.popover-section {
    padding: .8rem 1rem;
}
.popover-section + .popover-section {
    border-top: 1px solid var(--border);
}
.popover-title {
    font-size: .8rem;
    color: var(--text-soft);
    text-transform: uppercase;
    letter-spacing: .08em;
    margin-bottom: .5rem;
}
.popover-hint {
    color: var(--text-soft);
    font-size: .8rem;
    margin: .5rem 0 0;
}

.option-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
    gap: .5rem;
}
.option-btn {
    min-height: 44px;
    padding: 0 .6rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font-size: .9rem;
    cursor: pointer;
    transition: background .15s, border-color .15s;
}
.option-btn:hover { background: var(--surface); }
.option-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: .6rem;
    cursor: pointer;
    padding: .25rem 0;
    font-size: 1rem;
    margin-top: 0;
}
.checkbox-row input[type="checkbox"] {
    width: 22px; height: 22px;
    accent-color: var(--accent);
    cursor: pointer;
}

/* "Neuer Quilt" preset row + side-by-side size inputs */
.preset-row {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: .5rem;
    margin-top: .25rem;
}
.preset-btn {
    min-height: 40px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-size: .9rem;
    cursor: pointer;
    transition: background .15s, border-color .15s, color .15s;
}
.preset-btn:hover { background: var(--surface); }
.preset-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
}
.size-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: .75rem;
    margin-top: 1rem;
}
.size-row label { margin-top: 0; }

/* History list */
.history-list {
    display: flex;
    flex-direction: column;
    gap: .5rem;
    margin: .5rem 0 1rem;
    max-height: 50vh;
    overflow-y: auto;
}
.history-row {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .6rem .75rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 10px;
}
.history-meta {
    flex: 1;
    min-width: 0;
}
.history-when {
    font-size: .9rem;
    color: var(--text);
    overflow: hidden;
    text-overflow: ellipsis;
}
.history-abs {
    color: var(--text-soft);
    font-size: .8rem;
}
.history-stats {
    color: var(--text-soft);
    font-size: .75rem;
    margin-top: .15rem;
}
.history-row .btn {
    margin: 0;
    flex: 0 0 auto;
    font-size: .85rem;
    padding: .4rem .8rem;
    min-height: 36px;
}
.history-row .btn[disabled] {
    opacity: .55;
    cursor: default;
}

/* AI spinner */
.ai-spinner {
    width: 28px;
    height: 28px;
    margin-top: .8rem;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: ai-spin .9s linear infinite;
}
@keyframes ai-spin { to { transform: rotate(360deg); } }

/* Admin gallery */
.admin-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 1rem;
    padding: 1.2rem;
}
.admin-card {
    background: var(--surface);
    border-radius: 14px;
    overflow: hidden;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
}
.admin-card .thumb {
    aspect-ratio: 1;
    background: var(--surface-2) center/cover no-repeat;
    border-bottom: 1px solid var(--border);
}
.admin-card .thumb.empty {
    display: grid;
    place-items: center;
    color: var(--text-soft);
    font-size: .85rem;
}
.admin-card .body {
    padding: .65rem .8rem;
    font-size: .85rem;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: .25rem;
}
.admin-card .prompt {
    color: var(--text);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.admin-card .meta {
    color: var(--text-soft);
    font-size: .75rem;
    display: flex;
    flex-wrap: wrap;
    gap: .25rem .5rem;
}
.admin-card .status-pill {
    display: inline-block;
    padding: .1em .5em;
    border-radius: 999px;
    font-size: .7rem;
    text-transform: uppercase;
    letter-spacing: .05em;
}
.status-succeeded { background: rgba(34,197,94,.15); color: #15803d; }
.status-failed    { background: rgba(220,38,38,.15); color: #b91c1c; }
.status-pending,
.status-running   { background: rgba(99,102,241,.15); color: #4338ca; }

