/* ============================================================
   DemoCrafter API — builder chrome & design system
   ============================================================ */

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

:root {
    --bg: #0e1015;
    --bg-raised: #14171f;
    --bg-panel: #171b24;
    --bg-inset: #10131a;
    --border: #262c39;
    --border-strong: #333a4b;
    --text: #e7eaf1;
    --text-dim: #9aa3b5;
    --text-faint: #646e82;
    --accent: #6d7cff;
    --accent-strong: #8b96ff;
    --accent-soft: rgba(109, 124, 255, 0.14);
    --success: #34d399;
    --success-soft: rgba(52, 211, 153, 0.14);
    --warning: #fbbf24;
    --danger: #f87171;
    --danger-soft: rgba(248, 113, 113, 0.12);
    --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
    --radius: 10px;
    --radius-sm: 7px;
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    --transition: all 0.16s ease;
}

html, body { height: 100%; }

body {
    font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
}

#app { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }

button { font-family: inherit; }
input, select, textarea { font-family: inherit; color: var(--text); }
a { color: var(--accent-strong); text-decoration: none; }
a:hover { text-decoration: underline; }

::selection { background: rgba(109, 124, 255, 0.35); }

/* Scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #2c3342; border-radius: 6px; border: 2px solid var(--bg); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; gap: 0.45rem;
    padding: 0.5rem 0.9rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-strong);
    background: var(--bg-raised);
    color: var(--text);
    font-size: 13px; font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    white-space: nowrap;
    user-select: none;
}
.btn:hover { background: #1c212c; border-color: #3d465c; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn i { font-size: 12px; }

.btn-primary {
    background: var(--accent); border-color: var(--accent);
    color: #fff;
}
.btn-primary:hover { background: #7c88ff; border-color: #7c88ff; }

.btn-danger { color: var(--danger); border-color: rgba(248, 113, 113, 0.35); background: transparent; }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover { background: var(--bg-raised); color: var(--text); border-color: transparent; }

.btn-sm { padding: 0.3rem 0.6rem; font-size: 12px; }
.btn-icon { padding: 0.35rem 0.45rem; }

/* ---------- Form controls (builder) ---------- */
.field-label {
    display: block;
    font-size: 12px; font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--text-dim);
    margin-bottom: 0.35rem;
    text-transform: uppercase;
}

.input, .select, .textarea {
    width: 100%;
    background: var(--bg-inset);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.7rem;
    font-size: 13.5px;
    transition: var(--transition);
}
.input:focus, .select:focus, .textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.input::placeholder, .textarea::placeholder { color: var(--text-faint); }
.textarea { resize: vertical; min-height: 80px; font-family: var(--mono); font-size: 12.5px; line-height: 1.6; }
.select {
    appearance: none;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath fill='%239aa3b5' d='M0 0l5 6 5-6z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.65rem center;
    padding-right: 2rem;
    cursor: pointer;
}

.input-error { border-color: var(--danger) !important; }
.error-text { color: var(--danger); font-size: 12.5px; margin-top: 0.35rem; }
.hint { color: var(--text-faint); font-size: 12.5px; }

.checkbox-row { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; user-select: none; }
.checkbox-row input[type="checkbox"] {
    width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer;
}

.switch { position: relative; display: inline-block; width: 34px; height: 20px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
    position: absolute; inset: 0;
    background: var(--border-strong); border-radius: 20px;
    transition: var(--transition); cursor: pointer;
}
.switch .track::before {
    content: ""; position: absolute;
    width: 14px; height: 14px; left: 3px; top: 3px;
    background: #aab3c5; border-radius: 50%;
    transition: var(--transition);
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::before { transform: translateX(14px); background: #fff; }

/* ---------- Cards & layout ---------- */
.card {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.card-pad { padding: 1.1rem 1.25rem; }
.card-title { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 0.5rem; }
.card-title i { color: var(--accent-strong); font-size: 13px; }
.card-sub { color: var(--text-dim); font-size: 12.5px; margin-top: 2px; }

.section-label {
    font-size: 11px; font-weight: 700; letter-spacing: 0.09em;
    text-transform: uppercase; color: var(--text-faint);
    margin-bottom: 0.6rem;
}

/* ---------- App shell ---------- */
.topbar {
    display: flex; align-items: center; gap: 1rem;
    padding: 0 1.1rem;
    height: 54px;
    background: var(--bg-raised);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    z-index: 20;
}
.brand { display: flex; align-items: center; gap: 0.55rem; font-weight: 700; font-size: 14.5px; letter-spacing: -0.01em; }
.brand i { color: var(--accent-strong); font-size: 15px; }
.brand .badge-v {
    font-size: 10px; font-weight: 700; color: var(--accent-strong);
    background: var(--accent-soft); border-radius: 20px; padding: 1px 7px;
    letter-spacing: 0.04em;
}

.demo-name-input {
    background: transparent; border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-size: 14px; font-weight: 600; color: var(--text);
    padding: 0.35rem 0.6rem; min-width: 220px; max-width: 340px;
    transition: var(--transition);
}
.demo-name-input:hover { border-color: var(--border); }
.demo-name-input:focus { outline: none; border-color: var(--accent); background: var(--bg-inset); }

.save-state { display: inline-flex; align-items: center; gap: 0.35rem; color: var(--text-faint); font-size: 12px; }
.save-state.saved { color: var(--success); }

.topbar .spacer { flex: 1; }

.app-body { flex: 1; display: flex; min-height: 0; }

.rail {
    width: 168px; flex-shrink: 0;
    background: var(--bg-raised);
    border-right: 1px solid var(--border);
    padding: 0.9rem 0.65rem;
    display: flex; flex-direction: column; gap: 2px;
}
.rail-btn {
    display: flex; align-items: center; gap: 0.65rem;
    padding: 0.55rem 0.75rem;
    border-radius: var(--radius-sm);
    border: none; background: transparent;
    color: var(--text-dim);
    font-size: 13.5px; font-weight: 500;
    cursor: pointer; text-align: left;
    transition: var(--transition);
    width: 100%;
}
.rail-btn i { width: 16px; text-align: center; font-size: 13px; }
.rail-btn:hover { background: var(--bg-panel); color: var(--text); }
.rail-btn.active { background: var(--accent-soft); color: var(--accent-strong); }
.rail-sep { height: 1px; background: var(--border); margin: 0.6rem 0.4rem; }
.rail-foot { margin-top: auto; padding: 0.5rem 0.6rem; }

.workspace { flex: 1; min-width: 0; overflow: hidden; display: flex; }

.tab-panel { flex: 1; min-width: 0; display: none; overflow-y: auto; padding: 1.25rem; }
.tab-panel.active { display: block; }

/* ---------- Builder editor/preview split ---------- */
.editor-grid { display: grid; grid-template-columns: minmax(380px, 1fr) minmax(420px, 1fr); gap: 1.25rem; align-items: start; }
.editor-grid.single { grid-template-columns: 1fr; }
.preview-col { position: sticky; top: 0; }

.preview-header {
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0.55rem 0.9rem;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-bottom: none;
    border-radius: var(--radius) var(--radius) 0 0;
    font-size: 12px; color: var(--text-dim);
}
.preview-header .live-dot {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--success);
    box-shadow: 0 0 6px var(--success);
    animation: pulse 2.4s infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ---------- Field config rows (API tab) ---------- */
.field-row {
    display: flex; flex-direction: column;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-raised);
    margin-bottom: 0.55rem;
    transition: var(--transition);
}
.field-row.excluded { opacity: 0.5; }
.field-row-main { display: flex; align-items: center; gap: 0.6rem; padding: 0.55rem 0.7rem; }
.field-row .fpath {
    font-family: var(--mono); font-size: 12px; color: var(--accent-strong);
    min-width: 0; flex-shrink: 1;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.field-row .ftype-badge {
    font-family: var(--mono); font-size: 10.5px; color: var(--text-faint);
    background: var(--bg-inset); border: 1px solid var(--border);
    padding: 1px 6px; border-radius: 4px; flex-shrink: 0;
}
.field-row-main .input { padding: 0.3rem 0.55rem; font-size: 13px; }
.field-row-main .select { padding: 0.3rem 1.7rem 0.3rem 0.5rem; font-size: 12.5px; width: auto; }
.field-row-expand-btn {
    background: none; border: none; color: var(--text-faint); cursor: pointer;
    padding: 4px 6px; border-radius: 5px; font-size: 12px;
}
.field-row-expand-btn:hover { color: var(--text); background: var(--bg-inset); }
.field-row-advanced { display: none; padding: 0.2rem 0.7rem 0.75rem; border-top: 1px dashed var(--border); }
.field-row-advanced.open { display: block; }
.adv-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; margin-top: 0.6rem; }
.adv-grid .full { grid-column: 1 / -1; }
.adv-item label { display: block; font-size: 11px; font-weight: 600; color: var(--text-faint); margin-bottom: 3px; text-transform: uppercase; letter-spacing: 0.04em; }
.adv-item .input, .adv-item .select { padding: 0.3rem 0.5rem; font-size: 12.5px; }
.move-btns { display: flex; flex-direction: column; }
.move-btns button {
    background: none; border: none; color: var(--text-faint); cursor: pointer;
    font-size: 9px; padding: 0 3px; line-height: 1.4;
}
.move-btns button:hover { color: var(--accent-strong); }
.move-btns button:disabled { opacity: 0.25; cursor: default; }

/* ---------- Steps list (Flow tab) ---------- */
.step-row {
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0.5rem 0.7rem;
    background: var(--bg-raised);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    margin-bottom: 0.5rem;
}
.step-row .step-num {
    width: 20px; height: 20px; border-radius: 50%;
    background: var(--accent-soft); color: var(--accent-strong);
    font-size: 11px; font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.step-row .input { padding: 0.32rem 0.55rem; font-size: 13px; }

/* ---------- Design tab ---------- */
.choice-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.8rem; }
.choice-card {
    position: relative;
    border: 1.5px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-raised);
    padding: 0.9rem;
    cursor: pointer;
    transition: var(--transition);
    text-align: left;
    color: var(--text);
    width: 100%;
}
.choice-card:hover { border-color: var(--border-strong); }
.choice-card.selected { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.choice-card .cc-check {
    position: absolute; top: 0.6rem; right: 0.6rem;
    color: var(--accent); font-size: 14px; opacity: 0; transition: var(--transition);
}
.choice-card.selected .cc-check { opacity: 1; }
.choice-card .cc-title { font-weight: 600; font-size: 13.5px; display: flex; align-items: center; gap: 0.5rem; }
.choice-card .cc-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.choice-card .cc-art {
    height: 64px; border-radius: 7px; margin-bottom: 0.65rem;
    border: 1px solid var(--border);
    display: flex; align-items: center; justify-content: center; gap: 6px;
    overflow: hidden;
}
.color-presets { display: flex; gap: 0.5rem; align-items: center; }
.color-dot {
    width: 26px; height: 26px; border-radius: 50%;
    border: 2px solid transparent; cursor: pointer;
    transition: var(--transition);
}
.color-dot.selected { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }
input[type="color"].color-input {
    width: 34px; height: 34px; padding: 2px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--bg-inset); cursor: pointer;
}

/* ---------- Library ---------- */
.library-wrap { max-width: 1080px; margin: 0 auto; width: 100%; }
.library-head { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 1.4rem; }
.library-head h1 { font-size: 21px; font-weight: 700; letter-spacing: -0.02em; }
.library-head p { color: var(--text-dim); font-size: 13.5px; margin-top: 3px; }

.demo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }
.demo-card {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.1rem 1.2rem;
    display: flex; flex-direction: column; gap: 0.7rem;
    transition: var(--transition);
}
.demo-card:hover { border-color: var(--border-strong); transform: translateY(-1px); box-shadow: var(--shadow); }
.demo-card .dc-name { font-weight: 600; font-size: 15px; }
.demo-card .dc-desc { color: var(--text-dim); font-size: 12.5px; min-height: 34px; }
.demo-card .dc-meta { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.tag {
    font-size: 10.5px; font-weight: 600; letter-spacing: 0.03em;
    color: var(--text-dim); background: var(--bg-inset);
    border: 1px solid var(--border);
    padding: 2px 8px; border-radius: 20px;
    display: inline-flex; align-items: center; gap: 4px;
}
.demo-card .dc-actions { display: flex; gap: 0.5rem; margin-top: auto; padding-top: 0.4rem; border-top: 1px solid var(--border); }
.demo-card .dc-actions .btn { flex: 1; justify-content: center; }
.dc-updated { color: var(--text-faint); font-size: 11.5px; }

.demo-card.new-demo {
    border-style: dashed; border-color: var(--border-strong);
    align-items: center; justify-content: center;
    cursor: pointer; color: var(--text-dim);
    min-height: 168px;
    background: transparent;
    gap: 0.6rem;
}
.demo-card.new-demo:hover { border-color: var(--accent); color: var(--accent-strong); background: var(--accent-soft); }
.demo-card.new-demo i { font-size: 22px; }

/* ---------- Modal ---------- */
.modal-backdrop {
    position: fixed; inset: 0;
    background: rgba(5, 7, 12, 0.72);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
    animation: fadeIn 0.15s ease;
    backdrop-filter: blur(2px);
}
.modal {
    background: var(--bg-panel);
    border: 1px solid var(--border-strong);
    border-radius: 14px;
    padding: 1.4rem 1.5rem;
    width: min(420px, calc(100vw - 2rem));
    box-shadow: var(--shadow);
    animation: slideUp 0.18s ease;
}
.modal h3 { font-size: 15.5px; margin-bottom: 0.5rem; }
.modal p { color: var(--text-dim); font-size: 13.5px; }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.2rem; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(10px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

/* ---------- Toast ---------- */
.toast {
    position: fixed; bottom: 1.4rem; left: 50%;
    transform: translateX(-50%) translateY(80px);
    background: #1d2330;
    border: 1px solid var(--border-strong);
    color: var(--text);
    padding: 0.6rem 1rem;
    border-radius: 10px;
    font-size: 13px;
    display: flex; align-items: center; gap: 0.55rem;
    box-shadow: var(--shadow);
    opacity: 0;
    transition: all 0.25s ease;
    z-index: 200;
    max-width: calc(100vw - 2rem);
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.success i { color: var(--success); }
.toast.error i { color: var(--danger); }

/* ---------- Empty state ---------- */
.empty-state {
    text-align: center; padding: 3rem 1rem; color: var(--text-dim);
}
.empty-state i { font-size: 26px; color: var(--text-faint); margin-bottom: 0.8rem; display: block; }
.empty-state h3 { color: var(--text); font-size: 15px; margin-bottom: 0.3rem; }
.empty-state p { font-size: 13px; }

/* ---------- Share tab ---------- */
.share-link-box {
    display: flex; gap: 0.5rem;
    background: var(--bg-inset);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.45rem 0.45rem 0.45rem 0.75rem;
    align-items: center;
}
.share-link-box .link-text {
    flex: 1; min-width: 0; font-family: var(--mono); font-size: 12px;
    color: var(--text-dim);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- Responsive ---------- */
@media (max-width: 1080px) {
    .editor-grid { grid-template-columns: 1fr; }
    .preview-col { position: static; }
}
@media (max-width: 760px) {
    .rail { width: 56px; padding: 0.9rem 0.4rem; }
    .rail-btn span { display: none; }
    .rail-btn { justify-content: center; }
    .demo-name-input { min-width: 120px; }
}
@media (max-width: 560px) {
    .topbar { gap: 0.45rem; padding: 0 0.7rem; }
    .topbar .btn span { display: none; }
    .brand > span:not(.badge-v) { display: none; }
    .demo-name-input { min-width: 0; max-width: 110px; flex: 1; }
    .viewer-topbar { flex-wrap: wrap; }
}
