:root {
  --bg: #0f172a;
  --bg-soft: #111c33;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --ok: #16a34a;
  --err: #dc2626;
  --shadow: 0 10px 30px -12px rgba(15, 23, 42, .35);
  --radius: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 100% -10%, #1e293b 0%, transparent 55%),
    radial-gradient(1000px 500px at -10% 0%, #312e81 0%, transparent 45%),
    var(--bg);
  min-height: 100vh;
}

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.5rem;
  color: #e2e8f0;
}
.brand { display: flex; align-items: center; gap: .6rem; font-size: 1.15rem; }
.brand-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 10px;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  color: #fff; font-size: 1.1rem;
}
.brand-name { letter-spacing: .2px; font-weight: 500; }
.brand-name strong { font-weight: 800; }
.topbar-right { display: flex; align-items: center; gap: .75rem; }
.topbar-actions { display: flex; flex-direction: column; gap: .4rem; }
.who { font-size: .9rem; color: #94a3b8; }

/* ---- Buttons ---- */
.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid transparent; border-radius: 10px;
  padding: .6rem 1.1rem; transition: all .15s ease;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); transform: translateY(-1px); }
.btn.ghost { background: transparent; border-color: rgba(148,163,184,.5); color: #e2e8f0; }
.btn.ghost:hover { background: rgba(148,163,184,.15); }
.btn.block { width: 100%; margin-top: .5rem; }
.btn.grow { flex: 1; }
.btn.danger { color: #fca5a5; border-color: rgba(248,113,113,.4); background: transparent; }
.btn.danger:hover:not(:disabled) { background: rgba(248,113,113,.15); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.loading { opacity: 1; cursor: progress; display: inline-flex; align-items: center; justify-content: center; gap: .5rem; }
.spinner {
  width: 15px; height: 15px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.4); border-top-color: #fff;
  display: inline-block; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- Auth ---- */
.auth-wrap { display: grid; place-items: center; padding: 4vh 1rem; }
.card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.75rem;
}
.auth-card { width: 100%; max-width: 400px; }
.auth-card h1 { margin: 0 0 .3rem; font-size: 1.5rem; }
.field { display: block; margin: 1rem 0; }
.field span { display: block; font-size: .82rem; font-weight: 600; color: var(--muted); margin-bottom: .35rem; }
input[type=email], input[type=password], textarea {
  width: 100%; padding: .7rem .85rem; font: inherit;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.hint { color: var(--muted); font-size: .9rem; margin: 0 0 1rem; }
#google-signin { display: flex; justify-content: center; margin: .5rem 0; }
.auth-divider { display: flex; align-items: center; text-align: center; color: var(--muted); font-size: .78rem; margin: 1rem 0; }
.auth-divider::before, .auth-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.auth-divider span { padding: 0 .7rem; }.row { display: flex; align-items: center; }
.row.gap { gap: .6rem; }

/* ---- Workspace layout ---- */
.workspace {
  display: grid; grid-template-columns: 380px 1fr; gap: 1.25rem;
  max-width: 1200px; margin: 0 auto; padding: 0 1.5rem 3rem;
  align-items: start;
}
@media (max-width: 860px) { .workspace { grid-template-columns: 1fr; } }

.controls { position: sticky; top: 1rem; }
.step { display: flex; align-items: center; gap: .55rem; font-size: 1rem; margin: 1.25rem 0 .7rem; }
.step:first-child { margin-top: 0; }
.num {
  display: grid; place-items: center; width: 22px; height: 22px;
  background: var(--accent-soft); color: var(--accent);
  border-radius: 50%; font-size: .78rem; font-weight: 700;
}
.opt { font-size: .72rem; font-weight: 500; color: var(--muted); background: var(--surface-2);
  padding: .1rem .45rem; border-radius: 6px; }

/* ---- Dropzone ---- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: .35rem;
  border: 2px dashed var(--border); border-radius: 12px;
  padding: 1.4rem; cursor: pointer; text-align: center;
  transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dz-icon { font-size: 1.5rem; color: var(--accent); }
.dz-text { color: var(--muted); font-size: .9rem; }

/* ---- Source thumbnails ---- */
.thumb-strip { display: flex; gap: .6rem; flex-wrap: wrap; margin-top: .75rem; }
.thumb {
  position: relative; width: 104px; height: 104px; border-radius: 12px;
  overflow: hidden; cursor: pointer; border: 2px solid transparent;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; cursor: zoom-in; }
.thumb.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.thumb-pick {
  position: absolute; bottom: 0; left: 0; right: 0; font: inherit; font-size: .64rem;
  text-align: center; background: rgba(15,23,42,.72); color: #fff; padding: 3px 0;
  border: 0; cursor: pointer;
}
.thumb-pick.on { background: var(--accent); font-weight: 600; }
.thumb-del {
  position: absolute; top: 3px; right: 3px; z-index: 1;
  width: 20px; height: 20px; border: 0; border-radius: 50%;
  background: rgba(15,23,42,.7); color: #fff; font-size: .68rem; cursor: pointer;
  display: grid; place-items: center; padding: 0;
}
.thumb-del:hover { background: var(--err); }

/* ---- Categories ("what to remodel") ---- */
.step-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.categories { display: flex; flex-direction: column; gap: .5rem; margin-top: .3rem; }
.category { display: flex; flex-direction: column; }
.cat-chip {
  display: flex; align-items: center; gap: .5rem;
  padding: .5rem .8rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2);
  transition: all .15s;
}
.cat-chip.active { background: var(--accent); border-color: var(--accent); }
.cat-chip.active .cat-title { color: #fff; font-weight: 600; }
.cat-whole { border-style: dashed; }
.cat-title { flex: 1; cursor: pointer; font-size: .92rem; color: var(--text); }
.cat-del {
  border: 0; background: transparent; color: inherit; cursor: pointer;
  font-size: .8rem; opacity: .7; padding: .1rem .3rem; border-radius: 6px;
}
.cat-chip.active .cat-del { color: #fff; }
.cat-del:hover { opacity: 1; background: rgba(0,0,0,.12); }
.cat-refs { padding: .6rem 0 .3rem .4rem; }
.add-ref { margin-top: .5rem; }

/* ---- Reference thumbnails (shared with category picker) ---- */
.cab-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; }
.cab-item { position: relative; border: 2px solid var(--border); border-radius: 10px; overflow: hidden; cursor: pointer; background: var(--surface-2); }
.cab-item img { width: 100%; aspect-ratio: 1/1; object-fit: cover; display: block; }
.cab-item .cab-name { display: block; font-size: .68rem; text-align: center; padding: .25rem 2px; color: var(--text); }
.cab-item.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.cab-item.active::after { content: "✓"; position: absolute; top: 4px; left: 6px; color: #fff; background: var(--accent); border-radius: 50%; width: 18px; height: 18px; font-size: .7rem; display: grid; place-items: center; }
.ref-del {
  position: absolute; top: 4px; right: 4px; z-index: 1;
  width: 20px; height: 20px; border: 0; border-radius: 50%;
  background: rgba(15,23,42,.7); color: #fff; font-size: .7rem; cursor: pointer;
  display: grid; place-items: center;
}
.ref-del:hover { background: var(--err); }

/* ---- Add category / reference dialog ---- */
.add-modal {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(2,6,23,.6); backdrop-filter: blur(3px);
  display: grid; place-items: center; padding: 1.5rem;
}
.add-card { width: 100%; max-width: 380px; }
.add-card h3 { margin: 0 0 1rem; font-size: 1.1rem; }

/* ---- Admin: manage users ---- */
.admin-card { width: 100%; max-width: 480px; }
.admin-card h3 { margin: 0 0 .3rem; font-size: 1.1rem; }
.admin-section-title { margin: 1rem 0 .2rem; font-size: .92rem; }
.admin-stats { max-height: 34vh; overflow: auto; margin: .3rem 0 1rem; }
.stats-table { width: 100%; border-collapse: collapse; font-size: .82rem; }
.stats-table th, .stats-table td {
  padding: .38rem .5rem; border-bottom: 1px solid var(--border); text-align: right;
  white-space: nowrap;
}
.stats-table th { color: var(--muted); font-weight: 600; }
.stats-table th:first-child, .stats-table td:first-child { text-align: left; }
.stats-email { max-width: 190px; overflow: hidden; text-overflow: ellipsis; }
.admin-add-row { margin: .6rem 0; }
.admin-add-row input {
  flex: 1; padding: .55rem .7rem; font: inherit;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
}
.admin-users {
  display: flex; flex-direction: column; gap: .4rem;
  max-height: 46vh; overflow-y: auto; margin: .6rem 0 1rem;
}
.admin-user {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
  padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2);
}
.admin-user.off { opacity: .6; }
.admin-user-info { display: flex; flex-direction: column; min-width: 0; }
.admin-user-email { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-user-meta { font-size: .75rem; color: var(--muted); }
.admin-user-actions { display: flex; align-items: center; gap: .4rem; flex-shrink: 0; }
.badge {
  font-size: .72rem; font-weight: 700; padding: .2rem .5rem; border-radius: 999px;
  background: rgba(99,102,241,.2); color: #a5b4fc;
}

/* ---- Prompt preview / edit ---- */
.prompt-row { display: flex; justify-content: flex-end; margin: .8rem 0 .3rem; }
.prompt-preview { margin-bottom: .6rem; }
.prompt-preview textarea {
  width: 100%; font: inherit; font-size: .82rem; line-height: 1.4;
  padding: .6rem .7rem; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); resize: vertical;
}
.prompt-preview-foot { display: flex; align-items: center; justify-content: space-between; gap: .6rem; margin-top: .4rem; }
.prompt-preview-foot .hint { margin: 0; font-size: .76rem; }

/* ---- Cabinet style picker (legacy heading, still used elsewhere) ---- */

textarea { margin-top: 0; resize: vertical; }

.select {
  width: 100%; padding: .6rem .7rem; font: inherit;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
  cursor: pointer;
}
.select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.sub-opts { display: flex; gap: .6rem; margin-top: .55rem; }
.sub-field { flex: 1; }
.sub-field span { display: block; font-size: .75rem; font-weight: 600; color: var(--muted); margin-bottom: .3rem; }

/* ---- Mask toggle (in controls) ---- */
.mask-row { margin-top: .8rem; }
.mask-toggle { display: flex; align-items: center; gap: .45rem; font-size: .9rem; cursor: pointer; }
.mask-row-actions { display: flex; align-items: center; gap: .6rem; margin-top: .5rem; }
.mask-status { font-size: .8rem; color: var(--muted); }
.mask-status.ok { color: var(--ok); }

/* ---- Full-screen mask painter ---- */
.mask-modal {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(2, 6, 23, .94); backdrop-filter: blur(4px);
  display: flex; flex-direction: column;
}
.mask-modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 1rem 1.25rem; color: #e2e8f0;
}
.mask-modal-title { font-size: .92rem; color: #cbd5e1; }
.mask-modal-body {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding: 0 1rem; min-height: 0; overflow: hidden;
}
.mask-stage { position: relative; display: inline-block; line-height: 0; }
.mask-stage img {
  display: block; max-width: 94vw; max-height: 74vh;
  width: auto; height: auto; border-radius: 10px;
}
.mask-stage canvas {
  position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  opacity: .45; cursor: crosshair; touch-action: none;
}
.mask-modal-controls {
  display: flex; align-items: center; justify-content: center; gap: 1rem;
  padding: 1rem 1.25rem calc(1rem + env(safe-area-inset-bottom));
  color: #e2e8f0; flex-wrap: wrap;
}
.mask-modal-controls .brush { display: flex; align-items: center; gap: .5rem; font-size: .85rem; }
.mask-modal-controls .brush input { width: min(45vw, 240px); }
body.modal-open { overflow: hidden; }

/* ---- Messages ---- */
.msg { font-size: .85rem; margin: .7rem 0 0; min-height: 1em; }
.msg.error { color: var(--err); }
.msg.ok { color: var(--ok); }
.msg.loading { color: var(--accent); }

/* ---- Results ---- */
.results-head { display: flex; align-items: center; gap: .6rem; color: #e2e8f0; margin-bottom: .9rem; }
.results-head h2 { margin: 0; font-size: 1.2rem; }
#fetch-history { margin-left: auto; }
.pill { font-size: .78rem; background: rgba(148,163,184,.2); color: #cbd5e1;
  padding: .15rem .55rem; border-radius: 999px; }
.gallery {
  display: flex; flex-direction: column; gap: 1rem;
}
.result-card {
  background: var(--surface); border-radius: 14px; overflow: hidden;
  box-shadow: var(--shadow); display: flex; flex-direction: column;
}
/* Before / after side-by-side row */
.ba-row { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; background: var(--border); }
.ba-row:has(.ba-pane:only-child) { grid-template-columns: 1fr; }
.ba-pane { margin: 0; position: relative; background: var(--surface); }
.ba-pane figcaption {
  position: absolute; top: 8px; left: 8px; z-index: 1;
  font-size: .7rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: #fff; background: rgba(15,23,42,.7); padding: .15rem .5rem; border-radius: 6px;
}
.result-card .frame { position: relative; cursor: zoom-in; background: #0b1220; }
.result-card img { width: 100%; display: block; aspect-ratio: 4/3; object-fit: cover; }
.result-card .frame::after {
  content: "⤢ full size"; position: absolute; inset: auto 0 0 0;
  background: linear-gradient(transparent, rgba(15,23,42,.75)); color: #fff;
  font-size: .74rem; padding: 1.2rem .6rem .5rem; opacity: 0; transition: opacity .15s;
}
.result-card .frame:hover::after { opacity: 1; }
.result-meta { padding: .7rem .8rem; font-size: .8rem; color: var(--muted); }
.result-meta .parts { color: var(--text); font-weight: 600; text-transform: capitalize; }
.result-time { display: inline-block; margin-left: .4rem; font-size: .72rem; color: var(--muted);
  background: var(--surface-2); border: 1px solid var(--border); padding: .05rem .4rem; border-radius: 999px; }
.result-date { display: inline-block; margin-left: .4rem; font-size: .72rem; color: var(--muted); }
.result-actions { display: flex; gap: .4rem; padding: 0 .8rem .8rem; }
.result-actions .btn { flex: 1; padding: .45rem; font-size: .82rem; text-align: center; text-decoration: none; }
.btn.mini { background: var(--surface-2); border: 1px solid var(--border); color: var(--text); }
.btn.mini:hover { background: var(--accent-soft); border-color: var(--accent); }

/* Empty state spans the grid */
.empty {
  grid-column: 1 / -1; text-align: center; color: #94a3b8;
  padding: 3rem 1rem; border: 1px dashed rgba(148,163,184,.35); border-radius: 14px;
}
.empty-icon { font-size: 2.4rem; display: block; margin-bottom: .6rem; }

/* ---- Lightbox ---- */
.lightbox {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(2,6,23,.88); backdrop-filter: blur(4px);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1rem;
  padding: 2rem;
}
.lightbox img { max-width: 92vw; max-height: 74vh; border-radius: 12px; box-shadow: var(--shadow); }
.lb-close {
  position: absolute; top: 1.2rem; right: 1.4rem; width: 42px; height: 42px;
  border-radius: 50%; border: 0; background: rgba(255,255,255,.12); color: #fff;
  font-size: 1.1rem; cursor: pointer;
}
.lb-close:hover { background: rgba(255,255,255,.25); }
.lb-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  width: 52px; height: 52px; border-radius: 50%; border: 0;
  background: rgba(255,255,255,.12); color: #fff; font-size: 1.8rem; line-height: 1;
  cursor: pointer; display: grid; place-items: center;
}
.lb-nav:hover { background: rgba(255,255,255,.28); }
.lb-prev { left: 1.2rem; }
.lb-next { right: 1.2rem; }
.lb-bar { display: flex; align-items: center; gap: 1rem; }
.lb-caption { color: #e2e8f0; font-size: .9rem; font-weight: 600; }
.lb-download { text-decoration: none; }

@media (max-width: 560px) {
  .lb-nav { width: 44px; height: 44px; font-size: 1.5rem; }
  .lb-prev { left: .4rem; }
  .lb-next { right: .4rem; }
}

.hidden { display: none !important; }

/* ---- Responsive: tablet & phone ---- */
@media (max-width: 860px) {
  .controls { position: static; }            /* no sticky on small screens */
  .workspace { padding: 0 1rem 2.5rem; gap: 1rem; }
}

@media (max-width: 560px) {
  .topbar { padding: .8rem 1rem; }
  .brand { font-size: 1rem; }
  .who { max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .card { padding: 1.25rem; }
  .workspace { padding: 0 .8rem 2.5rem; }
  .gallery { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: .7rem; }
  .sub-opts { flex-direction: column; }
  .chip span { padding: .55rem 1rem; }        /* larger tap targets */
  .result-card .frame::after { opacity: 1; }   /* no hover on touch — always show */
  .mask-modal-title { font-size: .82rem; }
  .mask-stage img { max-height: 64vh; }
  .btn { padding: .7rem 1.1rem; }               /* comfortable tap targets */
}

/* Devices without hover (touch): reveal the "view full size" hint by default */
@media (hover: none) {
  .result-card .frame::after { opacity: 1; }
}

