:root {
  --bg: #111418;
  --panel: #1a1f26;
  --raised: #252b34;
  --line: #2b323c;
  --text: #e8ebef;
  --muted: #8b95a3;
  --accent: #f5c542;
  --skip: #4aa3ff;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding-top: env(safe-area-inset-top);
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}
button:disabled { opacity: .3; cursor: default; }

svg use { pointer-events: none; }
.icon-btn svg, .tool svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.row { display: flex; gap: 6px; }
.spacer { flex: 1; }

.btn {
  min-height: 44px;
  padding: 0 16px;
  border-radius: 12px;
  background: var(--raised);
}
.btn.primary, .icon-btn.primary { background: var(--accent); color: #1a1500; font-weight: 600; }
.btn.wide { width: 100%; margin-top: 20px; }
.btn.wide + .btn.wide { margin-top: 8px; }

/* ---------- photo area ---------- */

#viewport {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  background:
    repeating-conic-gradient(#161a1f 0 25%, #12151a 0 50%) 0 0 / 24px 24px;
}

#stage {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
}
#view { display: block; }
#overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}
/* Unselected detections: highlighted, not blurred. Selected: blurred, solid outline. */
#overlay .det { fill: rgba(245, 197, 66, .18); stroke: var(--accent); stroke-dasharray: 6 4; vector-effect: non-scaling-stroke; stroke-width: 2.5; }
#overlay .det.text { fill: rgba(196, 120, 255, .16); stroke: #c478ff; }
#overlay .det.on { fill: none; stroke: #3ddc84; stroke-dasharray: none; }
#overlay .det.emergency { fill: rgba(74, 163, 255, .2); stroke: var(--skip); }
#overlay .det.emergency.on { fill: none; stroke: #3ddc84; }
#overlay .draft { fill: rgba(245, 197, 66, .15); stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; }
#overlay text {
  fill: var(--skip);
  font-weight: 600;
  paint-order: stroke;
  stroke: #000;
  stroke-width: 3px;
  vector-effect: non-scaling-stroke;
}
body.peek #overlay { display: none; }

#empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}
#empty h1 { margin: 0; font-size: 24px; color: var(--text); }
#empty .lead { margin: -8px 0 8px; max-width: 320px; }
#empty .big {
  padding: 18px 36px;
  border-radius: 16px;
  background: var(--accent);
  color: #1a1500;
  font-size: 18px;
  font-weight: 700;
}
body.has-image #empty { display: none; }

.install-card {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 36px 12px 12px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  text-align: left;
}
.install-card img { width: 44px; height: 44px; border-radius: 10px; }
.install-card div { flex: 1; display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
.install-card span { color: var(--muted); }
.install-card .btn { min-height: 40px; }
.install-card .close {
  position: absolute;
  top: 4px;
  right: 6px;
  width: 28px;
  height: 28px;
  font-size: 20px;
  color: var(--muted);
}
body.dragging #viewport { outline: 3px dashed var(--accent); outline-offset: -12px; }

#toast {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(0, 0, 0, .85);
  border: 1px solid var(--line);
  text-align: center;
  pointer-events: none;
}

/* ---------- bottom dock ---------- */

.dock {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 12px calc(8px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.editing { display: flex; flex-direction: column; gap: 10px; }

.actions {
  align-items: center;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}
body:not(.has-image) .actions { border-top: 0; padding-top: 0; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 48px;
  height: 44px;
  border-radius: 12px;
}
.icon-btn:not(:disabled):active { background: var(--raised); }

.tools .tool {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 7px 0 6px;
  border-radius: 12px;
  font-size: 12px;
  color: var(--muted);
}
.tool.active { background: var(--raised); color: var(--text); box-shadow: inset 0 -2px var(--accent); }
#detectBtn { color: var(--accent); }
#detectBtn.busy { animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .4; } }

/* 5-step pickers */
.steps { justify-content: space-between; }
.steps button {
  flex: 1;
  display: grid;
  place-items: center;
  height: 52px;
  border-radius: 10px;
  border: 2px solid transparent;
}
.steps button.active { border-color: var(--accent); }
.steps canvas { width: 44px; height: 44px; border-radius: 6px; display: block; }
.sizes button { height: 40px; }
.sizes i { display: block; border-radius: 50%; background: var(--text); }
.sizes button.active i { background: var(--accent); }

.picks { align-items: center; font-size: 13px; }
.picks span { flex: 1.1; color: var(--muted); }
.chip {
  flex: 1;
  padding: 8px 4px;
  border-radius: 999px;
  background: #20262e;
  font-size: 13px;
}

/* ---------- dialogs ---------- */

dialog {
  width: min(360px, calc(100% - 32px));
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h2 { margin: 0 0 4px; font-size: 18px; }
dialog h3 { margin: 18px 0 8px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.seg .chip { padding: 10px 0; font-size: 14px; color: var(--muted); }
.seg .chip.active { background: var(--text); color: var(--bg); font-weight: 600; }

.switch { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
.beta {
  padding: 1px 7px;
  border-radius: 999px;
  background: #c478ff;
  color: #1a0f24;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
}

#savedDialog { text-align: center; }
#savedDialog p { margin: 0 0 16px; font-size: 17px; white-space: pre-line; word-break: break-all; }
#savedDialog .btn { flex: 1; min-height: 48px; }

/* Wide screens: dock becomes a side panel. */
@media (min-width: 900px) {
  body { display: grid; grid-template-columns: 1fr 340px; }
  .dock { flex-direction: column-reverse; justify-content: flex-end; border-top: 0; border-left: 1px solid var(--line); }
  .actions { border-top: 0; padding-top: 0; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
}
