/* Label Scanner — mobile-first, phone-only. */

:root {
  --bg: #17070c;
  --surface: #24101a;
  --surface-2: #32172352;
  --line: #4a2333;
  --text: #f6ecef;
  --muted: #b596a2;
  --accent: #c8324f;
  --accent-press: #a5253e;
  --ok: #4caf7d;
  --warn: #d99a3a;
  --err: #ef6a6a;
  --radius: 14px;
  --tap: 52px;
  --pad: 16px;
}

* { box-sizing: border-box; }

/* Any explicit `display` outranks the user-agent rule for [hidden], which is
   easy to trip over given how much of this UI is toggled by that attribute. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ── Screens ─────────────────────────────────────────────────────── */

.screen { display: none; }

body[data-screen="home"]     .screen[data-name="home"],
body[data-screen="settings"] .screen[data-name="settings"],
body[data-screen="capture"]  .screen[data-name="capture"],
body[data-screen="crop"]     .screen[data-name="crop"],
body[data-screen="review"]   .screen[data-name="review"],
body[data-screen="saved"]    .screen[data-name="saved"] {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: max(8px, env(safe-area-inset-top)) var(--pad) 8px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.topbar h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: .01em;
}

.scroll {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: var(--pad);
  padding-bottom: 8px;
}

/* Review has no footer bar — Save floats instead — so its scroll area needs
   its own clearance at the bottom, or the last field ends up under the FAB. */
body[data-screen="review"] .scroll {
  padding-bottom: calc(80px + env(safe-area-inset-bottom));
}

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

.actions-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.spacer { flex: 1 0 84px; }

.actions .actions-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.actions .actions-row .btn { flex: 1; }

.hint {
  margin: 0 2px 2px;
  font-size: 13px;
  line-height: 1.35;
}

/* ── Language chips ──────────────────────────────────────────────── */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 14px;
}

.chips button {
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.chips button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.chips button[disabled] { opacity: .4; }

.card .hint { margin-top: 10px; }

/* ── Live flattened preview ──────────────────────────────────────── */

/* A band of its own above the photo, at a fixed height so the stage below does
   not resize every time the label's proportions change. */
.preview {
  position: relative;
  flex: 0 0 auto;
  height: 136px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  background: #0c0407;
  border-bottom: 1px solid var(--line);
}

.preview canvas {
  max-width: 100%;
  max-height: 120px;
  display: block;
}

/* Tag and loupe stacked in the band's top-left corner, the loupe directly
   under the label so it reads as "what Flattened is showing you", rather
   than pinned to a raw pixel offset that a differently sized band could clip. */
.preview-corner {
  position: absolute;
  top: 5px;
  left: 10px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 5px;
}

.preview-tag {
  font-size: 10px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ── Slider ──────────────────────────────────────────────────────── */

.slider {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: var(--tap);
}

.slider > span {
  flex: 0 0 auto;
  min-width: 108px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.slider input[type="range"] {
  flex: 1;
  height: 32px;
  accent-color: var(--accent);
  touch-action: manipulation;
}

/* ── Buttons ─────────────────────────────────────────────────────── */

.btn {
  min-height: var(--tap);
  padding: 0 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .45; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:active { background: var(--accent-press); }
.btn-big { min-height: 58px; font-size: 17px; }
.btn-quiet { background: transparent; }

/* Floats over the scrolled content instead of holding a footer bar down, so
   review keeps the space that bar used to take. */
.btn-fab {
  position: fixed;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 10;
  min-height: 52px;
  padding: 0 22px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 8px 22px #000000a8;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-fab:active { background: var(--accent-press); }
.btn-fab[disabled] { opacity: .5; }

.btn-icon {
  width: 44px;
  height: 44px;
  margin-left: -10px;
  border: 0;
  background: none;
  color: var(--text);
  font-size: 30px;
  line-height: 1;
  cursor: pointer;
}

.topbar-action {
  margin-left: auto;         /* pushes the gear against the trailing edge */
  margin-right: -10px;
  font-size: 22px;
  position: relative;
}

.gear-badge {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px var(--surface);
}

.shutter {
  width: 72px;
  height: 72px;
  border: 4px solid #fff;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  flex: 0 0 auto;
}

.shutter:active { background: var(--accent-press); }
.shutter[disabled] { opacity: .4; }

/* ── Cards (home) ────────────────────────────────────────────────── */

.card {
  margin-bottom: 14px;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.card-head {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 6px;
}

.card h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

.card .btn { width: 100%; margin-top: 12px; }

.dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}

.dot[data-state="ok"]   { background: var(--ok); }
.dot[data-state="warn"] { background: var(--warn); }
.dot[data-state="err"]  { background: var(--err); }

.muted { margin: 0; color: var(--muted); font-size: 14px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; word-break: break-all; }

/* ── Progress bar ────────────────────────────────────────────────── */

.bar {
  height: 6px;
  margin-top: 12px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}

.bar > i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .2s ease;
}

.progress { margin-bottom: 16px; }
.progress .muted { margin-top: 6px; }

/* ── Camera / crop stage ─────────────────────────────────────────── */

.stage {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
}

.stage-dark { background: #000; }

#video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#crop-canvas { touch-action: none; }

/* A magnified, circular look at the handle being dragged, so its exact
   position against the label's edge is visible past the width of a
   fingertip. Sits under the Flattened tag rather than floating over the
   photo, so a finger can never end up covering it. */
.loupe {
  width: 96px;
  height: 96px;
  flex: 0 0 auto;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid #fff;
  box-shadow: 0 6px 18px #000000a0;
  pointer-events: none;
  background: #000;
}

.loupe canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.overlay-note {
  position: absolute;
  left: var(--pad);
  right: var(--pad);
  bottom: 18px;
  margin: 0;
  padding: 12px;
  border-radius: var(--radius);
  background: #000000b8;
  color: var(--text);
  font-size: 14px;
  text-align: center;
}

/* ── Review thumbnails ───────────────────────────────────────────── */

.thumbs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 18px;
}

.thumb {
  position: relative;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}

.thumb img {
  display: block;
  width: 100%;
  height: 150px;
  object-fit: contain;
  background: #000;
  cursor: zoom-in;
}

/* Floats over the photo instead of a bar of its own, so the thumbnail keeps
   the space that bar used to take. The gradient keeps the label readable over
   whatever the photo underneath happens to be; the caption itself passes taps
   through to that photo, so only the pencil — which needs its own — opts back
   in. */
.thumb figcaption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 4px 6px 10px;
  background: linear-gradient(to top, #000000cc, transparent);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: #fff;
  pointer-events: none;
}

.icon-edit {
  width: 36px;
  height: 36px;
  border: 0;
  background: none;
  color: #fff;
  font-size: 15px;
  cursor: pointer;
  pointer-events: auto;
}

.thumb-btn {
  width: 100%;
  height: 180px;
  border: 0;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 26px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
}

.thumb-btn span { font-size: 13px; }

.thumb-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: #000000c4;
  color: #fff;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

/* ── Form ────────────────────────────────────────────────────────── */

.field {
  position: relative;
  margin-bottom: 14px;
}

.field label {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
}

.field input,
.field select,
.field textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.field textarea { min-height: 96px; resize: vertical; }

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.chip {
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  letter-spacing: .06em;
}

/* A field with a pick list gets a permanent arrow marking it as one, since
   nothing else about a plain text input says so. It takes no pointer events,
   so a tap still falls through to the input underneath and opens the list. */
.field-list input { padding-right: 42px; }

.field-list::after {
  content: "▾";
  position: absolute;
  right: 15px;
  bottom: 0;
  height: var(--tap);
  display: flex;
  align-items: center;
  color: var(--muted);
  font-size: 15px;
  pointer-events: none;
}

/* The pick list itself — themed like the rest of the app, unlike the native
   datalist popup it replaces. Opens on every tap regardless of what the field
   already holds, so choosing a different value never means clearing it first. */
.suggest {
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  z-index: 5;
  margin-top: 4px;
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 10px 24px #00000080;
}

.suggest-option {
  display: block;
  width: 100%;
  min-height: var(--tap);
  padding: 0 14px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  text-align: left;
  cursor: pointer;
}

.suggest-option:last-child { border-bottom: 0; }
.suggest-option:active { background: var(--surface-2); }

/* ── Dragging a value between fields ─────────────────────────────── */

.grip {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 44px;
  height: 30px;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
  cursor: grab;
  touch-action: none;
}

/* Fills in behind the grip while it's held, finishing right as the drag
   itself becomes possible — a visual match for the long-press haptic. */
@keyframes grip-charging {
  from { background: transparent; transform: scale(1); }
  to   { background: var(--accent); transform: scale(1.15); }
}

.grip-pressing {
  color: #fff;
  animation: grip-charging 500ms ease-out forwards;   /* kept in step with LONG_PRESS_MS in drag.js */
}

.drop-target input,
.drop-target textarea {
  outline: 2px dashed var(--accent);
  outline-offset: -1px;
}

/* Confirms a swap landed without focusing the field, which would pop the
   on-screen keyboard right after a drag gesture. A ring rather than a
   background change, so the field's own colour is never overwritten. */
@keyframes field-landed {
  from { box-shadow: 0 0 0 2px var(--accent) inset; }
  to { box-shadow: 0 0 0 2px transparent inset; }
}

.field-landed input,
.field-landed textarea {
  animation: field-landed .5s ease-out;
}

.drag-chip {
  position: fixed;
  z-index: 30;
  max-width: 72vw;
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transform: translate(-50%, -50%);
  pointer-events: none;
  box-shadow: 0 8px 22px #000000a8;
}

body.dragging-field { user-select: none; -webkit-user-select: none; }

/* ── Enlarged photo ──────────────────────────────────────────────── */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: #000000ee;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.group {
  margin-bottom: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

.group > summary {
  min-height: var(--tap);
  display: flex;
  align-items: center;
  padding: 0 16px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
}

.group > summary::-webkit-details-marker { display: none; }
.group > summary::after { content: "▾"; margin-left: auto; }
.group[open] > summary::after { content: "▴"; }
.group > div { padding: 4px 16px 4px; }

.raw {
  margin: 0 0 12px;
  max-height: 240px;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  color: var(--muted);
}

/* ── Saved screen ────────────────────────────────────────────────── */

.done {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: var(--pad);
  text-align: center;
}

.tick {
  width: 84px;
  height: 84px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--ok);
  color: #06110b;
  font-size: 44px;
}

.done h2 { margin: 6px 0 0; font-size: 22px; }

#drafts-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.draft-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.draft-resume {
  flex: 1;
  min-width: 0;
  min-height: var(--tap);
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: inherit;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
}

.draft-thumb {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--bg);
}
.draft-thumb-empty { visibility: hidden; }

.draft-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.draft-name {
  font-size: 15px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.draft-when { font-size: 12px; color: var(--muted); }

.draft-discard {
  width: var(--tap);
  min-height: var(--tap);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

#saved-retry {
  width: 100%;
  max-width: 360px;
  margin-top: 8px;
}

#saved-retry .muted { text-align: left; margin-bottom: 12px; }

#saved-retry .btn { width: 100%; }

/* ── Toast ───────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  left: var(--pad);
  right: var(--pad);
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 10;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: #000000e0;
  border: 1px solid var(--line);
  font-size: 14px;
  text-align: center;
}
