:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --ink: #10151b;
  --muted: #5b6773;
  --line: #dde3e9;
  --brand: #0f2740;
  --brand-alt: #0aa5b5;
  --primary: #0f2740;
  --primary-ink: #ffffff;
  --ok: #1f9d63;
  --danger: #c0392b;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16,21,27,.06), 0 6px 18px rgba(16,21,27,.06);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
code { background: #eef2f6; padding: 2px 6px; border-radius: 6px; }

/* Brand */
.brand { display: inline-flex; align-items: center; gap: 1px; font-weight: 700; letter-spacing: .2px; }
.brand-mark { color: var(--brand); font-size: 20px; }
.brand-mark.alt { color: var(--brand-alt); }

/* Buttons */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 10px 16px; border-radius: 10px; font: inherit; font-weight: 600; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform .04s ease, background .15s ease, border-color .15s ease;
}
.btn:hover { border-color: #c4ccd4; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { background: #16344f; }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; padding: 14px; font-size: 16px; }
.btn.small { padding: 7px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Screens */
.screen.center { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px; width: 100%; max-width: 380px; text-align: center;
}
.login-card h1 { margin: 14px 0 4px; font-size: 20px; }
#loginForm { display: grid; gap: 10px; margin-top: 18px; }
input, select {
  font: inherit; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: #fff; color: var(--ink); width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--brand-alt); outline-offset: 1px; border-color: var(--brand-alt); }
.error { color: var(--danger); font-weight: 600; margin-top: 10px; }

/* Topbar */
.topbar {
  display: flex; align-items: center; gap: 18px; padding: 12px 18px;
  background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
.topbar-title { color: var(--muted); font-weight: 600; margin-left: 10px; font-size: 14px; }
.steps { display: flex; gap: 6px; margin-left: auto; }
.step {
  font-size: 13px; font-weight: 600; color: var(--muted); padding: 6px 12px; border-radius: 999px;
  border: 1px solid transparent;
}
.step.active { color: var(--brand); background: #eaf6f8; }
.step.done { color: var(--ok); }
.topbar-actions { display: flex; gap: 8px; }

/* Stage */
.stage { max-width: 1120px; margin: 22px auto; padding: 0 18px; }
.stage-head h2 { margin: 0 0 4px; }
.capture-wrap { display: grid; grid-template-columns: 1fr 320px; gap: 20px; margin-top: 16px; align-items: start; }

.viewport {
  position: relative; background: #0b0f14; border-radius: var(--radius); overflow: hidden;
  aspect-ratio: 3 / 4; height: 72vh; width: auto; max-width: 100%; margin: 0 auto;
  user-select: none; touch-action: none;
}
#video { width: 100%; height: 100%; object-fit: contain; display: block; background: #0b0f14; }

/* Crop overlay */
.shade { position: absolute; inset: 0; pointer-events: none;
  box-shadow: 0 0 0 9999px rgba(8,12,18,.5); border-radius: 2px; display: none; }
.crop {
  position: absolute; left: 12%; top: 12%; width: 76%; height: 76%;
  border: 2px solid var(--brand-alt); border-radius: 2px; cursor: move;
  box-shadow: 0 0 0 9999px rgba(8,12,18,.45), 0 0 0 1px rgba(255,255,255,.35) inset;
}
.crop-handle {
  position: absolute; width: 18px; height: 18px; background: #fff; border: 2px solid var(--brand-alt);
  border-radius: 50%;
}
.crop-handle[data-h="nw"] { left: -10px; top: -10px; cursor: nwse-resize; }
.crop-handle[data-h="ne"] { right: -10px; top: -10px; cursor: nesw-resize; }
.crop-handle[data-h="sw"] { left: -10px; bottom: -10px; cursor: nesw-resize; }
.crop-handle[data-h="se"] { right: -10px; bottom: -10px; cursor: nwse-resize; }
/* edge handles: resize ONE dimension independently */
.crop-handle.edge[data-h="n"] { left: 50%; top: -10px; margin-left: -9px; cursor: ns-resize; }
.crop-handle.edge[data-h="s"] { left: 50%; bottom: -10px; margin-left: -9px; cursor: ns-resize; }
.crop-handle.edge[data-h="e"] { right: -10px; top: 50%; margin-top: -9px; cursor: ew-resize; }
.crop-handle.edge[data-h="w"] { left: -10px; top: 50%; margin-top: -9px; cursor: ew-resize; }
/* rotation handle above the box */
.rotate-handle { position: absolute; left: 50%; top: -36px; width: 18px; height: 18px; margin-left: -9px;
  background: #fff; border: 2px solid var(--brand-alt); border-radius: 50%; cursor: grab; }
.rotate-handle::after { content: ""; position: absolute; left: 50%; top: 16px; width: 2px; height: 20px;
  margin-left: -1px; background: var(--brand-alt); }

/* Controls */
.controls { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; display: grid; gap: 12px; }
.field { display: grid; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.field.wide { max-width: 520px; margin: 16px 0; }
.crop-tools { display: flex; gap: 8px; }
.crop-tools .btn { flex: 1; }
.thumb-row { display: flex; gap: 8px; flex-wrap: wrap; }
.thumb-row img { width: 64px; height: 48px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }

/* Review */
.review { max-width: 900px; margin: 22px auto; padding: 0 18px; }
.review-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin: 16px 0; }
.review-grid figure { margin: 0; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px; }
.review-grid figcaption { font-weight: 600; color: var(--muted); margin-bottom: 8px; font-size: 13px; }
.review-grid img { width: 100%; border-radius: 8px; background: #0b0f14; display: block; }
.review-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* Done */
.ok-mark { width: 56px; height: 56px; border-radius: 50%; margin: 0 auto 12px; background: #e7f6ee;
  position: relative; }
.ok-mark::after { content: ""; position: absolute; left: 20px; top: 12px; width: 12px; height: 24px;
  border: solid var(--ok); border-width: 0 3px 3px 0; transform: rotate(45deg); }

/* Gallery */
.gallery { max-width: 1000px; margin: 22px auto; padding: 0 18px; }
.gallery-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.gallery-head > div { display: flex; gap: 8px; }
.gallery-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-top: 16px; }
.gcard { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; }
.gcard .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; background: #0b0f14; }
.gcard .pair img { width: 100%; height: 90px; object-fit: cover; display: block; }
.gcard .gmeta { padding: 10px 12px; font-size: 13px; }
.gcard .gmeta .gid { font-weight: 600; }

/* Setup */
.card.wide { max-width: 640px; text-align: left; margin: 26px auto; }
.setup { padding: 0 18px; }
.setup h2 { margin: 0 0 4px; }
.setup-grid { display: grid; grid-template-columns: 1fr 1fr 1.4fr; gap: 14px; margin: 16px 0; }
.setup .btn.big { margin-top: 8px; }

/* Sequence header */
.seq-head { max-width: 1120px; margin: 18px auto 0; padding: 0 18px; }
.seq-head h2 { margin: 0; font-size: 26px; }

/* Counter chip */
.counter { font-size: 13px; font-weight: 600; color: var(--muted); padding: 6px 10px;
  border: 1px solid var(--line); border-radius: 999px; }

/* Saved */
.saved-actions { display: grid; gap: 10px; margin-top: 16px; }
.error.center { text-align: center; margin: 16px auto; max-width: 520px; }

@media (max-width: 840px) {
  .capture-wrap { grid-template-columns: 1fr; }
  .setup-grid { grid-template-columns: 1fr; }
  .viewport { height: 58vh; }
}
