:root {
    --ink: #111827;
    --muted: #667085;
    --line: #e5e7eb;
    --surface: #ffffff;
    --canvas: #f4f5f8;
    --violet: #6d4aff;
    --violet-dark: #5334d5;
    --violet-soft: #f0edff;
    --navy: #1f2937;
    --cyan: #30c7d8;
    --danger: #c2414b;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-width: 320px;
    color: var(--ink);
    background: var(--canvas);
    font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, textarea, select, input { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: 64px;
    padding: 16px 8px;
    background: var(--navy);
    overflow: hidden;
    transition: width .3s ease;
}

.sidebar:hover { width: 256px; }
.brand-mark { display: grid; width: 48px; height: 48px; place-items: center; margin-bottom: 32px; }
.brand-mark img { width: 38px; height: 38px; object-fit: contain; }
.sidebar nav { display: grid; gap: 9px; max-height: calc(100vh - 98px); overflow-y: auto; scrollbar-width: none; }
.sidebar nav::-webkit-scrollbar { display: none; }
.sidebar nav a {
    display: flex;
    align-items: center;
    width: 240px;
    min-height: 48px;
    padding: 0 12px;
    color: #aeb6c5;
    text-decoration: none;
    border-radius: 12px;
    transition: background .2s, color .2s;
}
.sidebar nav a:hover, .sidebar nav a.active { color: white; background: rgba(255,255,255,.1); }
.sidebar nav a.active { box-shadow: inset 3px 0 0 #9b87ff; }
.sidebar svg { width: 24px; height: 24px; flex: 0 0 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.sidebar nav span { margin-left: 17px; white-space: nowrap; font-size: 14px; font-weight: 600; opacity: 0; transition: opacity .15s; }
.sidebar:hover nav span { opacity: 1; transition-delay: .12s; }

main { margin-left: 64px; padding: 24px clamp(16px, 4vw, 48px) 64px; }
.topbar { display: flex; justify-content: space-between; align-items: center; max-width: 1440px; margin: 0 auto 24px; }
.eyebrow { margin: 0 0 5px; color: var(--violet); font-size: 11px; font-weight: 700; letter-spacing: .15em; }
.topbar h1 { margin: 0; font-size: 25px; letter-spacing: -.03em; }
.connection-pill { display: flex; align-items: center; gap: 8px; padding: 9px 13px; color: #475467; background: white; border: 1px solid var(--line); border-radius: 999px; font-size: 12px; font-weight: 600; }
.connection-pill i { width: 8px; height: 8px; background: #f59e0b; border-radius: 50%; box-shadow: 0 0 0 4px #fef3c7; }
.connection-pill.connected i { background: #12b76a; box-shadow: 0 0 0 4px #d1fadf; }
.connection-pill.error i { background: #f04438; box-shadow: 0 0 0 4px #fee4e2; }

.intro {
    display: flex;
    justify-content: space-between;
    gap: 40px;
    max-width: 1440px;
    margin: 0 auto 24px;
    padding: clamp(22px, 3vw, 32px);
    color: var(--ink);
    background: white;
    border: 1px solid var(--line);
    border-radius: 16px;
    box-shadow: 0 8px 30px rgba(16,24,40,.045);
    overflow: hidden;
    position: relative;
}
.intro::after { content: none; }
.intro > div:first-child { position: relative; z-index: 1; max-width: 780px; }
.scope-label { display: inline-flex; margin-bottom: 18px; padding: 7px 11px; color: #5b3bdb; background: var(--violet-soft); border: 1px solid #ddd6fe; border-radius: 7px; font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.intro h2 { margin: 0 0 10px; font-size: clamp(25px, 2.5vw, 35px); line-height: 1.1; letter-spacing: -.045em; }
.intro p { max-width: 720px; margin: 0; color: var(--muted); font-size: 14px; line-height: 1.7; }
.intro-metric { position: relative; z-index: 1; align-self: flex-end; min-width: 145px; padding-left: 24px; border-left: 1px solid var(--line); }
.intro-metric strong { display: block; font-size: 38px; font-weight: 600; }
.intro-metric span { color: var(--muted); font-size: 12px; }

.alert { max-width: 1440px; margin: 0 auto 20px; padding: 13px 16px; color: #8a2832; background: #fff1f2; border: 1px solid #fecdd3; border-radius: 10px; font-size: 13px; }
.workspace { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(330px, .75fr); gap: 24px; align-items: start; max-width: 1440px; margin: 0 auto; }
.builder-card, .result-card { background: var(--surface); border: 1px solid #e9eaf0; border-radius: 18px; box-shadow: 0 8px 30px rgba(16,24,40,.045); }
.builder-card { overflow: hidden; }
.form-section { padding: 27px; border-bottom: 1px solid var(--line); }
.section-heading { display: flex; gap: 13px; margin-bottom: 20px; }
.section-heading > span { display: grid; width: 30px; height: 30px; flex: 0 0 30px; place-items: center; color: var(--violet); background: var(--violet-soft); border-radius: 8px; font-size: 11px; font-weight: 700; }
.section-heading h3 { margin: 0 0 4px; font-size: 17px; letter-spacing: -.02em; }
.section-heading p { margin: 0; color: var(--muted); font-size: 12px; }

.category-grid { display: flex; gap: 10px; overflow-x: auto; padding: 3px 3px 12px; margin: 0 -3px; scroll-snap-type: x mandatory; scrollbar-width: thin; scrollbar-color: #c8cbd4 transparent; }
.category-card { min-height: 104px; flex: 0 0 205px; padding: 14px; text-align: left; color: var(--ink); background: #fafafa; border: 1px solid var(--line); border-radius: 12px; scroll-snap-align: start; transition: transform .18s, border-color .18s, box-shadow .18s; }
.category-card:hover { transform: translateY(-2px); border-color: #c4b5fd; }
.category-card.active { background: #f7f5ff; border-color: var(--violet); box-shadow: 0 0 0 3px rgba(109,74,255,.1); }
.category-card i { display: block; width: 25px; height: 3px; margin-bottom: 17px; background: #b7bcc7; border-radius: 4px; }
.category-card.active i { background: var(--violet); }
.category-card strong { display: block; margin-bottom: 5px; font-size: 13px; line-height: 1.25; }
.category-card small { display: block; color: var(--muted); font-size: 10px; line-height: 1.35; }
.skeleton { min-height: 112px; background: linear-gradient(90deg,#f2f3f5,#fafafa,#f2f3f5); background-size: 200% 100%; border-radius: 12px; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

textarea { width: 100%; resize: vertical; min-height: 128px; padding: 15px; color: var(--ink); background: #fbfbfc; border: 1px solid #d9dce3; border-radius: 11px; outline: none; line-height: 1.55; transition: border-color .18s, box-shadow .18s; }
textarea:focus { border-color: var(--violet); box-shadow: 0 0 0 3px rgba(109,74,255,.1); }
.field-meta { display: flex; justify-content: space-between; margin-top: 8px; color: #98a2b3; font-size: 11px; }
.field-meta button { padding: 0; color: var(--violet); background: none; border: 0; font-weight: 600; }
.field-label { display: block; margin: 20px 0 9px; color: #475467; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; }
.preset-list { display: flex; gap: 8px; flex-wrap: wrap; }
.preset-chip { padding: 9px 12px; color: #475467; background: white; border: 1px solid #d9dce3; border-radius: 9px; font-size: 11px; font-weight: 600; }
.preset-chip:hover { border-color: #a78bfa; }
.preset-chip.active { color: #5b3bdb; background: var(--violet-soft); border-color: #a78bfa; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; padding: 4px; background: #f1f2f5; border-radius: 10px; }
.segmented button { padding: 10px; color: #667085; background: transparent; border: 0; border-radius: 7px; font-size: 12px; font-weight: 600; }
.segmented button.active { color: #3c2a8d; background: white; box-shadow: 0 2px 7px rgba(16,24,40,.08); }

.upload-panel { margin-top: 12px; }
#uploadButton { display: flex; align-items: center; justify-content: center; gap: 13px; width: 100%; padding: 18px; color: #596174; background: #fafbfc; border: 1px dashed #b9c0cc; border-radius: 11px; }
#uploadButton:hover { color: var(--violet); border-color: var(--violet); background: #faf9ff; }
#uploadButton svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
#uploadButton strong, #uploadButton small { display: block; text-align: left; }
#uploadButton strong { margin-bottom: 3px; font-size: 12px; }
#uploadButton small { color: #98a2b3; font-size: 10px; }
.upload-preview { display: flex; align-items: center; gap: 12px; padding: 10px; background: #f8f7ff; border: 1px solid #ddd6fe; border-radius: 11px; }
.upload-preview img { width: 58px; height: 58px; object-fit: cover; border-radius: 8px; }
.upload-preview div { min-width: 0; }
.upload-preview strong { display: block; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.upload-preview button { margin-top: 5px; padding: 0; color: var(--danger); background: none; border: 0; font-size: 10px; font-weight: 600; }
.settings-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 18px; }
.settings-row label > span { display: flex; justify-content: space-between; min-height: 19px; margin-bottom: 7px; color: #667085; font-size: 11px; font-weight: 600; }
.settings-row select { width: 100%; padding: 10px 12px; color: #344054; background: white; border: 1px solid #d9dce3; border-radius: 9px; outline: none; }
.settings-row input { width: 100%; accent-color: var(--violet); }
.edit-notice { display: flex; justify-content: space-between; align-items: center; margin: 20px 27px 0; padding: 10px 12px; color: #4c368e; background: #f3f0ff; border-radius: 9px; font-size: 11px; }
.edit-notice button { color: #6d4aff; background: none; border: 0; font-weight: 700; }
.generate-button { display: flex; align-items: center; justify-content: center; gap: 9px; width: calc(100% - 54px); margin: 22px 27px 27px; padding: 14px; color: white; background: var(--violet); border: 0; border-radius: 11px; font-weight: 700; box-shadow: 0 7px 18px rgba(109,74,255,.24); transition: background .18s, transform .18s; }
.generate-button:hover { background: var(--violet-dark); transform: translateY(-1px); }
.generate-button:disabled { cursor: wait; opacity: .65; transform: none; }
.generate-button svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.result-card { position: sticky; top: 24px; overflow: hidden; }
.result-head { display: flex; justify-content: space-between; align-items: center; padding: 22px 23px; border-bottom: 1px solid var(--line); }
.result-head span:first-child { color: var(--violet); font-size: 9px; font-weight: 700; letter-spacing: .13em; }
.result-head h3 { margin: 3px 0 0; font-size: 16px; }
.status-badge { padding: 6px 9px; color: #475467; background: #f2f4f7; border-radius: 999px; font-size: 10px; font-weight: 700; }
.status-badge.processing { color: #7a2e0e; background: #fff4e5; }
.status-badge.success { color: #067647; background: #ecfdf3; }
.status-badge.failed { color: #b42318; background: #fef3f2; }
.empty-result, .loading-result, .failed-result { min-height: 430px; display: grid; place-content: center; justify-items: center; padding: 35px; text-align: center; }
.empty-result h4, .loading-result h4 { margin: 25px 0 7px; font-size: 15px; }
.empty-result p, .loading-result p, .failed-result p { max-width: 280px; margin: 0; color: var(--muted); font-size: 11px; line-height: 1.6; }
.radar { position: relative; width: 132px; height: 132px; border: 1px solid #e0dcf6; border-radius: 50%; }
.radar::after, .radar::before, .radar i { content: ""; position: absolute; inset: 18px; border: 1px solid #e5e1f7; border-radius: 50%; }
.radar::before { inset: 42px; }
.radar i:nth-child(1) { inset: 65px 0 auto; border: 0; border-top: 1px solid #e0dcf6; border-radius: 0; }
.radar i:nth-child(2) { inset: 0 auto 0 65px; border: 0; border-left: 1px solid #e0dcf6; border-radius: 0; }
.radar span { position: absolute; left: 61px; top: 61px; width: 10px; height: 10px; background: var(--violet); border-radius: 50%; box-shadow: 0 0 0 7px rgba(109,74,255,.13); }
.scan-frame { position: relative; width: 190px; height: 150px; background: #f3f1ff; border: 1px solid #ddd6fe; border-radius: 14px; overflow: hidden; }
.scan-frame::before { content: ""; position: absolute; inset: 30px 35px; background: #d8d2fb; clip-path: polygon(0 55%,20% 28%,38% 48%,58% 15%,77% 40%,100% 25%,100% 76%,0 76%); }
.scan-frame span { position: absolute; left: 0; right: 0; height: 2px; background: var(--cyan); box-shadow: 0 0 14px var(--cyan); animation: scan 1.6s ease-in-out infinite; }
@keyframes scan { 0%,100% { top: 16px; } 50% { top: 132px; } }
.result-image-wrap { position: relative; background: #111827; aspect-ratio: 4/3; }
.result-image-wrap img { display: block; width: 100%; height: 100%; object-fit: cover; }
.result-image-wrap > span { position: absolute; left: 13px; bottom: 13px; padding: 6px 9px; color: white; background: rgba(17,24,39,.78); border-radius: 6px; font-size: 9px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; }
.result-copy { padding: 16px 20px; border-bottom: 1px solid var(--line); }
.result-copy p { display: -webkit-box; margin: 0; overflow: hidden; color: #475467; font-size: 11px; line-height: 1.55; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.result-actions { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; padding: 14px; }
.result-actions button { display: flex; align-items: center; justify-content: center; gap: 6px; padding: 9px; color: #475467; background: white; border: 1px solid #dfe2e8; border-radius: 8px; font-size: 10px; font-weight: 600; }
.result-actions button:hover { color: var(--violet); border-color: #b9aafa; }
.result-actions svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.failed-result strong { margin-bottom: 8px; color: var(--danger); }
.failed-result button { margin-top: 18px; padding: 9px 13px; color: white; background: var(--danger); border: 0; border-radius: 8px; font-size: 11px; font-weight: 700; }

.history-section { max-width: 1440px; margin: 32px auto 0; }
.history-heading { display: flex; justify-content: space-between; align-items: end; margin-bottom: 15px; }
.history-heading h2 { margin: 0; font-size: 21px; }
.history-heading button, #loadMoreHistory { padding: 9px 13px; color: #475467; background: white; border: 1px solid #d9dce3; border-radius: 8px; font-size: 11px; font-weight: 600; }
.history-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.history-card { overflow: hidden; text-align: left; background: white; border: 1px solid #e4e7ec; border-radius: 13px; transition: transform .18s, box-shadow .18s; }
.history-card:hover { transform: translateY(-3px); box-shadow: 0 9px 22px rgba(16,24,40,.08); }
.history-card-image { position: relative; aspect-ratio: 4/3; background: #e9eaf0; }
.history-card-image img { display: block; width: 100%; height: 100%; object-fit: cover; }
.history-card-image span { position: absolute; top: 9px; right: 9px; padding: 5px 7px; color: white; background: rgba(17,24,39,.74); border-radius: 5px; font-size: 8px; font-weight: 700; text-transform: uppercase; }
.history-card-copy { padding: 12px; }
.history-card-copy strong { display: block; margin-bottom: 5px; overflow: hidden; font-size: 11px; white-space: nowrap; text-overflow: ellipsis; }
.history-card-copy small { color: #98a2b3; font-size: 9px; }
.history-card.status-pending .history-card-image, .history-card.status-processing .history-card-image { display: grid; place-items: center; color: #667085; font-size: 10px; }
.history-card.status-pending .history-card-image::after, .history-card.status-processing .history-card-image::after { content: ""; width: 23px; height: 23px; border: 2px solid #d8d2fb; border-top-color: var(--violet); border-radius: 50%; animation: rotate .8s linear infinite; }
.history-card.status-failed .history-card-image { display: grid; place-items: center; color: #b42318; background: #fef3f2; font-size: 10px; font-weight: 700; }
.history-card.status-failed .history-card-image::after { content: "Generation failed"; }
@keyframes rotate { to { transform: rotate(360deg); } }
.history-empty { grid-column: 1/-1; padding: 50px 20px; color: #98a2b3; text-align: center; background: white; border: 1px dashed #d0d5dd; border-radius: 13px; font-size: 12px; }
.load-more-wrap { margin-top: 16px; text-align: center; }
.toast { position: fixed; z-index: 50; left: 50%; bottom: 25px; max-width: calc(100vw - 32px); padding: 11px 16px; color: white; background: #1f2937; border-radius: 9px; box-shadow: 0 12px 32px rgba(16,24,40,.24); transform: translateX(-50%); font-size: 12px; }
.toast.error { background: #b42318; }

@media (max-width: 1050px) {
    .workspace { grid-template-columns: 1fr; }
    .result-card { position: static; }
    .history-grid { grid-template-columns: repeat(3, minmax(0,1fr)); }
}

@media (max-width: 760px) {
    .sidebar, .sidebar:hover { width: 58px; padding: 14px 7px; }
    .brand-mark { width: 44px; }
    .sidebar nav a { width: 44px; padding: 0 10px; }
    .sidebar nav span { display: none; }
    main { margin-left: 58px; padding: 18px 12px 45px; }
    .topbar h1 { font-size: 21px; }
    .connection-pill span { display: none; }
    .intro { display: block; padding: 24px 20px; border-radius: 16px; }
    .intro-metric { display: none; }
    .category-card { flex: 0 0 178px; scroll-snap-align: start; }
    .form-section { padding: 20px; }
    .generate-button { width: calc(100% - 40px); margin: 20px; }
    .edit-notice { margin: 18px 20px 0; }
    .history-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (max-width: 430px) {
    .history-grid { grid-template-columns: 1fr; }
    .settings-row { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---------- 3D reconstruction ---------- */

.result-actions button.primary-action { grid-column: 1 / -1; color: white; background: var(--violet); border-color: var(--violet); }
.result-actions button.primary-action:hover { color: white; background: var(--violet-dark); border-color: var(--violet-dark); }
.result-actions button:disabled { opacity: .55; cursor: not-allowed; }
.result-actions svg.spinning { animation: rotate 1s linear infinite; transform-origin: 50% 50%; }

.model-panel { padding: 0 14px 16px; border-top: 1px solid var(--line); }
.model-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 0 12px; }
.model-head span:first-child { display: block; color: var(--violet); font-size: 9px; font-weight: 700; letter-spacing: .14em; }
.model-head h4 { margin: 3px 0 0; font-size: 14px; letter-spacing: -.02em; }

.model-viewport {
    position: relative;
    height: 230px;
    background: linear-gradient(160deg, #1c2340, #2b2456);
    border: 1px solid #d9dbe6;
    border-radius: 12px;
    overflow: hidden;
    cursor: grab;
    touch-action: none;
}

.model-viewport:active { cursor: grabbing; }
.model-viewport canvas { display: block; width: 100%; height: 100%; }
.model-hint {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    color: rgba(255, 255, 255, .6);
    font-size: 11px;
    font-weight: 600;
    pointer-events: none;
}
.model-viewport.is-live .model-hint { top: auto; bottom: 10px; transform: translateX(-50%); font-size: 10px; }

.model-facts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0 0; }
.model-facts div { padding: 9px 11px; background: var(--canvas); border-radius: 10px; }
.model-facts dt { color: #667085; font-size: 9px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.model-facts dd { margin: 3px 0 0; font-size: 13px; font-weight: 700; letter-spacing: -.01em; }

.model-actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.model-actions button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 6px;
    color: #475467;
    background: white;
    border: 1px solid #dfe2e8;
    border-radius: 8px;
    font-size: 10px;
    font-weight: 600;
}
.model-actions button:hover:not(:disabled) { color: var(--violet); border-color: #b9aafa; }
.model-actions button:disabled { opacity: .5; cursor: not-allowed; }
.model-actions svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.model-note { margin: 12px 0 0; color: #667085; font-size: 10.5px; line-height: 1.5; }

@media (max-width: 520px) {
    .model-actions { grid-template-columns: 1fr; }
    .model-facts { grid-template-columns: 1fr; }
}
