:root {
  color-scheme: dark;
  --bg: #111113; --panel: #1c1c1e; --panel2: #2c2c2e; --fg: #f2f2f4; --muted: #9a9aa0;
  --accent: #0a84ff; --ok: #4ade80; --warn: #fbbf24; --danger: #ff453a;
  --safe-t: env(safe-area-inset-top); --safe-b: env(safe-area-inset-bottom);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg); }
body { font: 15px/1.4 -apple-system, system-ui, sans-serif; overscroll-behavior: none; }
[hidden] { display: none !important; }
button, .btn, .chip {
  appearance: none; border: 0; border-radius: 12px; padding: 10px 14px; font: 600 15px system-ui;
  background: var(--panel2); color: var(--fg); cursor: pointer; text-decoration: none; text-align: center;
}
button:disabled { opacity: .4; }
.primary { background: var(--accent); color: #fff; }
.danger { color: var(--danger); }
.link { background: none; padding: 8px 4px; color: var(--accent); }
.link.danger { color: var(--danger); }
.big { display: block; width: 100%; padding: 16px; font-size: 17px; margin-top: 12px; }
.muted { color: var(--muted); }

.screen { min-height: 100dvh; }

/* ---------- Camera ---------- */
.cam { display: flex; flex-direction: column; height: 100dvh; background: #000; }
.viewport { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.viewport video, .viewport canvas, #flash { position: absolute; inset: 0; width: 100%; height: 100%; }
.viewport video { object-fit: contain; }
#flash { background: #fff; opacity: 0; pointer-events: none; }
.pill {
  position: absolute; top: calc(var(--safe-t) + 10px); left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.6); padding: 6px 12px; border-radius: 999px; font: 600 13px system-ui; white-space: nowrap;
}
.cam-error {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 24px; text-align: center; background: var(--bg);
}
.retake-bar {
  display: flex; justify-content: space-between; align-items: center; padding: 6px 16px;
  background: var(--warn); color: #000; font-weight: 600;
}
.retake-bar .link { color: #000; text-decoration: underline; }
.cam-bar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  padding: 14px 16px calc(14px + var(--safe-b)); background: #000;
}
.cam-side { display: flex; gap: 8px; flex-wrap: wrap; }
.cam-side.right { justify-content: flex-end; }
.chip { padding: 8px 12px; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.chip.strong { background: var(--panel2); }
#shutter {
  width: 72px; height: 72px; border-radius: 50%; padding: 0;
  border: 5px solid #fff; background: rgba(255,255,255,.25);
}
#shutter:active { background: rgba(255,255,255,.7); }

/* ---------- Manual corner adjustment ---------- */
.crop {
  position: fixed; inset: 0; z-index: 5; background: #000; display: flex; flex-direction: column;
  padding-top: var(--safe-t);
}
.crop-top { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 10px 16px 0; }
.crop-hint { margin: 0; text-align: center; color: var(--muted); font-size: 14px; }
#cropCanvas { flex: 1; min-height: 0; width: 100%; touch-action: none; }
.crop-bar {
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 12px; align-items: center;
  padding: 12px 16px calc(12px + var(--safe-b));
}
.crop-bar > :first-child { justify-self: start; }
.crop-bar > :last-child { justify-self: end; min-width: 96px; }

/* ---------- Editor ---------- */
.topbar {
  position: sticky; top: 0; z-index: 2; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: calc(var(--safe-t) + 8px) 16px 8px; background: rgba(17,17,19,.92); backdrop-filter: blur(10px);
}
.topbar h1 { margin: 0; font-size: 17px; }
.topbar > :first-child { justify-self: start; }
.topbar > :last-child { justify-self: end; }
.segmented { display: flex; margin: 8px 16px; background: var(--panel); border-radius: 12px; padding: 3px; }
.segmented button { flex: 1; background: none; padding: 8px 4px; border-radius: 9px; font-size: 14px; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--panel2); color: var(--fg); }
.hint { color: var(--muted); font-size: 13px; margin: 4px 16px 8px; }
.empty { text-align: center; color: var(--muted); margin: 40px 16px; }
.grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; padding: 4px 16px 16px;
}
.thumb {
  position: relative; background: var(--panel); border-radius: 10px; padding: 6px; aspect-ratio: 3 / 4;
  display: flex; align-items: center; justify-content: center; user-select: none; -webkit-user-select: none;
}
.thumb img { max-width: 100%; max-height: 100%; border-radius: 4px; background: #fff; pointer-events: none; }
.thumb .num {
  position: absolute; left: 8px; bottom: 8px; background: rgba(0,0,0,.7); border-radius: 6px; padding: 1px 7px; font: 600 12px system-ui;
}
.thumb .handle {
  position: absolute; right: 4px; top: 4px; width: 40px; height: 40px; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.6); border-radius: 8px; font-size: 20px; touch-action: none; cursor: grab;
}
.thumb .badge {
  position: absolute; left: 8px; top: 8px; background: var(--warn); color: #000; border-radius: 6px; padding: 1px 6px; font: 600 11px system-ui;
}
.thumb .fmt {
  position: absolute; right: 8px; bottom: 8px; background: rgba(10,132,255,.9); color: #fff; border-radius: 6px; padding: 1px 6px; font: 600 11px system-ui;
}
.thumb.dragging { outline: 3px solid var(--accent); transform: scale(1.04); z-index: 1; box-shadow: 0 8px 24px rgba(0,0,0,.6); }
.editor-foot { padding: 8px 16px calc(24px + var(--safe-b)); text-align: center; }

/* ---------- Sheet / modal / toast ---------- */
.sheet, .modal { position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.55); display: flex; }
.sheet { align-items: flex-end; }
.sheet-card {
  width: 100%; background: var(--panel); border-radius: 18px 18px 0 0; padding: 16px 16px calc(16px + var(--safe-b));
  display: flex; flex-direction: column; gap: 12px; max-height: 92dvh;
}
.sheet-title { font-weight: 600; text-align: center; }
.sheet-card img { max-height: 45dvh; max-width: 100%; align-self: center; border-radius: 6px; background: #fff; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sheet-close { background: none; color: var(--accent); }
.modal { align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: var(--panel); border-radius: 16px; padding: 20px; width: 100%; max-width: 360px; }
.modal-card h2 { margin: 0 0 6px; font-size: 18px; }
.modal-card p { margin: 0 0 16px; color: var(--muted); }
.modal-buttons { display: flex; gap: 8px; }
.modal-buttons button { flex: 1; }
.toast {
  position: fixed; z-index: 20; left: 16px; right: 16px; bottom: calc(110px + var(--safe-b)); margin: 0 auto; max-width: 420px;
  background: rgba(44,44,46,.96); padding: 12px 14px; border-radius: 12px; text-align: center; font-weight: 600;
}

/* ---------- Format picker ---------- */
.sheet-format { width: 100%; }
.format-card { max-height: 90dvh; overflow-y: auto; max-width: 420px; }
.format-list { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 14px; }
.format-item { display: flex; justify-content: space-between; align-items: baseline; text-align: left; padding: 11px 14px; }
.format-item small { color: var(--muted); font-weight: 400; font-size: 13px; }
.format-item.current { outline: 2px solid var(--accent); }
.custom-format { border-top: 1px solid var(--panel2); padding-top: 12px; }
.custom-title { font-weight: 600; margin-bottom: 8px; }
.custom-row { display: flex; align-items: flex-end; gap: 8px; }
.custom-row label { flex: 1; display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.custom-row input {
  width: 100%; padding: 10px; border-radius: 10px; border: 1px solid var(--panel2); background: var(--bg); color: var(--fg);
  font: 16px system-ui; /* 16px+ stops iOS zooming into the field */
}
.custom-row span { padding-bottom: 10px; color: var(--muted); }
.error { color: var(--danger); font-size: 13px; margin: 8px 0 0; }
.check { display: flex; align-items: center; gap: 8px; margin: 14px 0 4px; }
.check input { width: 20px; height: 20px; }

/* ---------- Export ---------- */
.export-body { padding: 24px 16px; max-width: 480px; margin: 0 auto; }
.export-status { font-size: 17px; font-weight: 600; }
progress { width: 100%; height: 8px; accent-color: var(--accent); }
