:root {
  --bg: #0f0f10;
  --panel: #17181b;
  --panel-2: #1f2125;
  --ink: #f3f3f3;
  --ink-dim: #b3b6bc;
  --accent: #c8a463;
  --accent-ink: #1a1a1a;
  --line: #2a2c31;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0,0,0,.35);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Inter, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* respect HTML hidden over class display */
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink); min-height: 100%; }
body { overflow-x: hidden; }

button { font-family: inherit; }

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(15,15,16,.85);
  position: sticky; top: 0; z-index: 10;
  backdrop-filter: blur(8px);
}
.brand { font-weight: 700; letter-spacing: .04em; text-transform: uppercase; font-size: 14px; }

.step { display: none; padding: 24px 20px 60px; }
.step.active { display: block; }

/* ---------- Step 1: Upload ---------- */
.hero { max-width: 720px; margin: 40px auto 0; text-align: center; }
.hero h1 { font-size: clamp(28px, 5vw, 44px); margin: 0 0 12px; line-height: 1.15; }
.lede { color: var(--ink-dim); font-size: 18px; margin: 0 0 28px; }

.upload-card {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 40px 24px; border: 2px dashed var(--line); border-radius: var(--radius);
  background: var(--panel); color: var(--ink);
  cursor: pointer; transition: border-color .2s, background .2s, transform .1s;
}
.upload-card:hover { border-color: var(--accent); background: var(--panel-2); }
.upload-card.dragging { border-color: var(--accent); background: var(--panel-2); transform: scale(.99); }
.upload-card strong { font-size: 20px; margin-top: 8px; }
.upload-card span { color: var(--ink-dim); font-size: 14px; }
.upload-card svg { color: var(--accent); }

.tips { color: var(--ink-dim); font-size: 13px; margin-top: 18px; }

/* "or" divider between upload-card and camera button */
.upload-or {
  display: flex; align-items: center; gap: 12px;
  margin: 14px auto 14px;
  max-width: 360px;
  color: var(--ink-dim);
  font-size: 13px; text-transform: uppercase; letter-spacing: .12em;
}
.upload-or::before,
.upload-or::after {
  content: ""; flex: 1; height: 1px; background: var(--line);
}

.camera-btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--panel);
  color: var(--ink);
  border: 2px solid var(--accent);
  border-radius: 50%;
  width: 64px; height: 64px;
  cursor: pointer;
  transition: background .15s, transform .05s;
}
.camera-btn:hover { background: rgba(200,164,99,.08); }
.camera-btn:active { transform: translateY(1px); }
.camera-btn svg { color: var(--accent); }

/* ---------- Step 2: Design ---------- */
.design-grid {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 20px;
  max-width: 1280px;
  margin: 0 auto;
}
@media (max-width: 880px) {
  .design-grid { grid-template-columns: 1fr; }
}

.stage-wrap { background: var(--panel); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }

/* The stage container holds the Konva canvas, an HTML <img> for the door
   (perspective-warped via CSS matrix3d), and the four corner-drag handles.
   They're all absolutely-stacked inside this relative container. */
.stage-container {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
}
.stage {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
}
.door-canvas {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  display: none;
  z-index: 2;
}
.door-canvas.visible { display: block; }
/* Konva canvas (background photo) — pin to z-index 1 so doorCanvas stacks above */
.stage { z-index: 1; }
.corner-outline { z-index: 3; }
.corner-handles { z-index: 4; }

.corner-outline {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  overflow: visible;
  display: none;
}
.corner-outline.active { display: block; }

/* AI target markers: cyan crosshairs the customer drags the handles toward. */
.ai-targets {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 3;
  display: none;
}
.ai-targets.active { display: block; }
.ai-targets circle {
  fill: rgba(0,212,255,.12);
  stroke: #00d4ff;
  stroke-width: 1.5;
  filter: drop-shadow(0 0 3px rgba(0,212,255,.6));
}
.ai-targets line {
  stroke: #00d4ff;
  stroke-width: 1.5;
  filter: drop-shadow(0 0 2px rgba(0,212,255,.6));
}
.corner-outline polygon {
  fill: rgba(200,164,99,.05);
  stroke: var(--accent);
  stroke-width: 1.5;
  stroke-dasharray: 6 4;
  vector-effect: non-scaling-stroke;
}

.corner-handles { position: absolute; inset: 0; pointer-events: none; }
.corner-handle {
  position: absolute;
  width: 26px; height: 26px;
  margin: -13px 0 0 -13px;
  border-radius: 50%;
  background: rgba(200,164,99,.25);
  border: 2.5px solid var(--accent);
  box-shadow: 0 0 0 2px rgba(0,0,0,.5), 0 2px 10px rgba(0,0,0,.6);
  pointer-events: auto;
  cursor: grab;
  touch-action: none;
  display: none;
  transition: transform .08s, background .15s;
  animation: handlePulse 2s ease-in-out 0s 3;
}
@keyframes handlePulse {
  0%, 100% { box-shadow: 0 0 0 2px rgba(0,0,0,.5), 0 2px 10px rgba(0,0,0,.6); }
  50%      { box-shadow: 0 0 0 4px rgba(200,164,99,.5), 0 2px 16px rgba(200,164,99,.4); }
}
.corner-handles.active .corner-handle { display: block; }
.corner-handle:hover { background: rgba(200,164,99,.4); transform: scale(1.12); }
.corner-handle.dragging,
.corner-handle:active { cursor: grabbing; background: var(--accent); transform: scale(1.15); }
.corner-handle::after {
  content: ""; position: absolute; inset: 6px;
  border-radius: 50%; background: var(--accent);
  opacity: .55;
}

/* On touch devices, bump handle size to match fingertip target size
   (44px is Apple HIG recommendation for tappable controls). */
@media (pointer: coarse) {
  .corner-handle {
    width: 44px; height: 44px;
    margin: -22px 0 0 -22px;
    border-width: 3px;
  }
  .corner-handle::after { inset: 12px; }
}

/* Magnifier loupe shown while dragging a handle on touch devices.
   Renders a zoomed snapshot of the area under the user's finger so they
   can see the alignment they're making despite finger occlusion. */
.magnifier {
  position: absolute;
  width: 140px; height: 140px;
  border-radius: 50%;
  border: 3px solid var(--accent);
  box-shadow: 0 4px 16px rgba(0,0,0,.55);
  pointer-events: none;
  display: none;
  background: #000;
  overflow: hidden;
  z-index: 50;
  transform: translate(-50%, -100%);    /* center horizontally, sit above finger */
}
.magnifier.active { display: block; }
.magnifier canvas { width: 100%; height: 100%; display: block; }
.stage-controls {
  display: flex; flex-wrap: wrap; gap: 12px; align-items: stretch;
  padding: 12px 4px 4px;
}
.control-group {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  padding: 8px 12px;
  background: var(--panel-2);
  border-radius: 10px;
}
.control-group-label {
  font-size: 11px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent); font-weight: 600;
  padding-right: 6px; border-right: 1px solid var(--line); margin-right: 2px;
}
.control-inline { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-dim); }
.control-inline input[type="range"] { width: 120px; accent-color: var(--accent); }
.hint { color: var(--ink-dim); font-size: 12px; margin: 8px 4px 0; }

.catalog {
  background: var(--panel); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 12px;
  max-height: calc(100vh - 120px);
}
.catalog h2 { margin: 0; font-size: 16px; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-dim); }

.catalog-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  overflow-y: auto;
  padding-right: 4px;
}
.door-card {
  background: var(--panel-2);
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 8px;
  cursor: pointer;
  text-align: center;
  transition: border-color .15s, transform .1s;
}
.door-card:hover { border-color: var(--line); }
.door-card.selected { border-color: var(--accent); }
.door-card img { width: 100%; height: 110px; object-fit: contain; display: block; }
.door-card .name { font-size: 12px; color: var(--ink-dim); margin-top: 6px; line-height: 1.2; }

/* ---------- Buttons ---------- */
.primary-btn {
  background: var(--accent); color: var(--accent-ink);
  border: none; padding: 12px 18px; border-radius: 10px;
  font-size: 15px; font-weight: 600; cursor: pointer;
  transition: filter .15s, transform .05s;
  text-decoration: none; display: inline-block; text-align: center;
}
.primary-btn:hover:not(:disabled) { filter: brightness(1.08); }
.primary-btn:active { transform: translateY(1px); }
.primary-btn:disabled { opacity: .45; cursor: not-allowed; }

.ghost-btn {
  background: transparent; color: var(--ink);
  border: 1px solid var(--line); padding: 10px 14px; border-radius: 10px;
  font-size: 14px; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.ghost-btn:hover { background: var(--panel-2); border-color: var(--accent); }

.secondary-btn {
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid var(--accent);
  padding: 11px 16px; border-radius: 10px;
  font-size: 14px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: background .15s, transform .05s, opacity .15s;
}
.secondary-btn:hover:not(:disabled) { background: rgba(200,164,99,.12); }
.secondary-btn:active { transform: translateY(1px); }
.secondary-btn:disabled { opacity: .45; cursor: not-allowed; }
.secondary-btn .btn-spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--accent); border-top-color: transparent;
  border-radius: 50%; display: none;
  animation: spin .9s linear infinite;
}
.secondary-btn.loading .btn-spinner { display: inline-block; }
.secondary-btn.loading .btn-label { opacity: .7; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Lead dialog ---------- */
.lead-dialog {
  border: none; border-radius: var(--radius); padding: 0;
  background: var(--panel); color: var(--ink);
  max-width: 420px; width: 92vw;
  box-shadow: var(--shadow);
}
.lead-dialog::backdrop { background: rgba(0,0,0,.6); backdrop-filter: blur(4px); }
.lead-dialog form { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
.lead-dialog h2 { margin: 0; font-size: 22px; }
.lead-dialog p { margin: 0 0 8px; color: var(--ink-dim); font-size: 14px; }
.lead-dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-dim); }
.lead-dialog input {
  background: var(--bg); border: 1px solid var(--line); color: var(--ink);
  padding: 10px 12px; border-radius: 8px; font-size: 15px; outline: none;
}
.lead-dialog input:focus { border-color: var(--accent); }
.dialog-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 6px; }
.fineprint { color: var(--ink-dim); font-size: 11px; margin: 8px 0 0; text-align: center; }

/* ---------- Step 4: Result ---------- */
.result-wrap { max-width: 900px; margin: 0 auto; text-align: center; }
.result-wrap h2 { font-size: clamp(22px, 4vw, 32px); margin: 8px 0 20px; }
.result-wrap img { max-width: 100%; border-radius: var(--radius); box-shadow: var(--shadow); }
.result-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin: 18px 0 8px; }
