:root {
  --bg: #0f1720;
  --panel: #18222d;
  --panel2: #212d3a;
  --line: #2c3a48;
  --text: #eef2f5;
  --muted: #9fb0bf;
  --accent: #2fb67c;
  --accent-ink: #04140c;
  --ring: #22d3ee;
  --jack: #ff8a1f;
  --first: #34d399;
  --second: #fbbf24;
  --other: #e5e7eb;
  --danger: #f87171;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-t) + 6px) 12px 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; letter-spacing: 0.2px; }
.top-actions { display: flex; gap: 4px; }
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 10px; background: transparent;
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--panel2); }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: var(--text); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* welcome */
.welcome { padding: 20px 20px calc(var(--safe-b) + 20px); overflow-y: auto; max-width: 560px; margin: 0 auto; }
.hero { width: 100%; max-width: 280px; display: block; margin: 8px auto 4px; }
.lead { font-size: 19px; text-align: center; margin: 8px 0 20px; }
.big-actions { display: grid; gap: 10px; }
.primary, .secondary {
  min-height: 50px; border-radius: 12px; border: 0; font-weight: 600; font-size: 17px; padding: 0 18px;
}
.primary { background: var(--accent); color: var(--accent-ink); }
.secondary { background: var(--panel2); border: 1px solid var(--line); }
.tips { color: var(--muted); padding-left: 20px; margin: 22px 0 8px; }
.tips li { margin: 6px 0; }
.setup-hint { color: var(--muted); font-size: 14px; }
.setup-hint button { background: none; border: 0; color: var(--ring); padding: 0; text-decoration: underline; }

/* workspace */
.workspace { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.stage { position: relative; flex: 1 1 58%; min-height: 220px; background: #000; overflow: hidden; }
#view { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
.loupe {
  position: absolute; top: 10px; left: 10px; width: 132px; height: 132px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 4px 16px rgba(0,0,0,.5); pointer-events: none; background: #000;
}
.loupe.right { left: auto; right: 10px; }
.chip {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 10px; max-width: calc(100% - 20px);
  background: rgba(15, 23, 32, .86); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 14px; font-size: 14px; text-align: center; pointer-events: none;
}
.chip.warn { border-color: var(--second); }
.busy {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  background: rgba(0,0,0,.45); font-weight: 600;
}
.spinner { width: 38px; height: 38px; border-radius: 50%; border: 4px solid rgba(255,255,255,.25); border-top-color: #fff; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toolbar {
  display: flex; gap: 6px; padding: 8px 10px; background: var(--panel); border-top: 1px solid var(--line);
  overflow-x: auto; align-items: center;
}
.toolbar button {
  flex: 1 1 auto; min-width: 0; min-height: 42px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel2);
  padding: 0 10px; font-size: 15px; font-weight: 600; white-space: nowrap;
}
.toolbar button.active { background: var(--ring); color: #031417; border-color: var(--ring); }
.toolbar button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.toolbar button.danger { color: var(--danger); }
.toolbar.sel span { font-weight: 700; padding: 0 2px; white-space: nowrap; }
.toolbar.sel #btnDeselect { flex: 0 0 42px; padding: 0; }

.results {
  flex: 0 1 42%; overflow-y: auto; background: var(--bg); padding: 10px 12px calc(var(--safe-b) + 12px);
  -webkit-user-select: text; user-select: text;
}
.results h3 { margin: 4px 0 8px; font-size: 15px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.verdict { font-size: 18px; font-weight: 700; margin: 2px 0 10px; }
.verdict.close { color: var(--second); }
.rank-list { list-style: none; margin: 0; padding: 0; }
.rank-list li {
  display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 6px; border-bottom: 1px solid var(--line); cursor: pointer;
}
.rank-list li.selected { background: var(--panel2); border-radius: 8px; }
.badge { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; color: #0b1118; }
.dist { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.dist small { font-size: 13px; color: var(--muted); font-weight: 500; margin-left: 4px; }
.gap { font-size: 13px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.gap.close { color: var(--second); font-weight: 600; }
.note { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.warnings { margin: 8px 0 0; padding: 0; list-style: none; }
.warnings li { color: var(--second); font-size: 14px; margin: 4px 0; }
.warnings li::before { content: "⚠︎ "; }
.map { display: block; width: 100%; max-width: 360px; margin: 12px auto 0; background: var(--panel); border-radius: 12px; }
.empty { color: var(--muted); padding: 6px 0; }

/* settings */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; background: var(--panel);
  border-radius: 18px 18px 0 0; padding: 16px 16px calc(var(--safe-b) + 16px);
  -webkit-user-select: text; user-select: text;
}
@media (min-width: 700px) { .modal { align-items: center; } .sheet { border-radius: 18px; } }
.sheet h2 { margin: 0 0 8px; }
fieldset { border: 1px solid var(--line); border-radius: 12px; margin: 10px 0; padding: 6px 12px 10px; }
legend { color: var(--muted); font-size: 14px; padding: 0 6px; }
.sheet label { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0; }
.sheet input, .sheet select {
  font: inherit; font-size: 16px; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px;
}
.sheet input { width: 96px; text-align: right; }
.help { color: var(--muted); font-size: 13px; margin: 4px 0; }
.link { background: none; border: 0; color: var(--ring); padding: 4px 0; text-decoration: underline; font-size: 14px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.version { color: var(--muted); font-size: 12px; text-align: center; margin: 14px 0 0; }

@media (orientation: landscape) and (max-height: 500px) {
  .workspace { display: grid; grid-template-columns: 1fr minmax(280px, 38%); grid-template-rows: auto 1fr; }
  .stage { grid-column: 1; grid-row: 1 / 3; }
  .toolbar { grid-column: 2; grid-row: 1; border-top: 0; }
  .results { grid-column: 2; grid-row: 2; }
}

.secondary.small { min-height: 40px; font-size: 15px; width: 100%; margin-top: 6px; }

/* camera: in-app photo and live view */
.cam { position: fixed; inset: 0; z-index: 20; background: #000; overflow: hidden; touch-action: none; }
.cam video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
#camOverlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.cam-top {
  position: absolute; left: 0; right: 0; top: 0; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(var(--safe-t) + 8px) 12px 8px; background: linear-gradient(rgba(0,0,0,.55), transparent);
}
.cam-btn { width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgba(15,23,32,.7); font-size: 18px; }
.cam-modes { display: flex; background: rgba(15,23,32,.7); border-radius: 999px; padding: 3px; }
.cam-modes button { border: 0; background: none; border-radius: 999px; padding: 8px 16px; font-weight: 600; font-size: 15px; color: var(--muted); }
.cam-modes button.active { background: var(--text); color: #0b1118; }
.cam-tilt { display: flex; align-items: center; gap: 6px; min-width: 44px; justify-content: flex-end; font-weight: 700; font-variant-numeric: tabular-nums; }
.level { position: relative; width: 56px; height: 56px; border-radius: 50%; border: 2px solid rgba(255,255,255,.6); background: rgba(15,23,32,.5); display: grid; place-items: center; }
.level::after { content: ""; position: absolute; width: 14px; height: 14px; border-radius: 50%; border: 1px solid rgba(255,255,255,.5); }
.bubble { width: 12px; height: 12px; border-radius: 50%; background: var(--second); transition: transform .08s linear; }
.bubble.ok { background: var(--first); }
.cam-msg {
  position: absolute; left: 50%; top: 40%; transform: translate(-50%, -50%); max-width: calc(100% - 40px);
  background: rgba(15,23,32,.82); border-radius: 12px; padding: 10px 16px; font-weight: 600; text-align: center; pointer-events: none;
}
.cam-fps { position: absolute; left: 8px; top: calc(var(--safe-t) + 70px); font: 12px ui-monospace, monospace; background: rgba(0,0,0,.6); padding: 2px 6px; border-radius: 4px; }
.cam-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 24px 12px calc(var(--safe-b) + 18px); background: linear-gradient(transparent, rgba(0,0,0,.6));
}
.cam-result { background: rgba(15,23,32,.85); border: 1px solid var(--line); border-radius: 999px; padding: 8px 16px; font-weight: 700; font-size: 17px; text-align: center; }
.cam-result.stable { border-color: var(--first); color: var(--first); }
.cam-progress { width: min(280px, 70vw); height: 8px; border-radius: 999px; background: rgba(15,23,32,.7); border: 1px solid var(--line); overflow: hidden; }
.cam-progress span { display: block; height: 100%; width: 0; background: var(--first); transition: width .2s; }
.shutter.recording { background: #ef4444; border-color: #fff; }
.shutter { width: 74px; height: 74px; border-radius: 50%; border: 4px solid #fff; background: rgba(255,255,255,.25); box-shadow: inset 0 0 0 4px rgba(0,0,0,.35); }
.shutter:active { background: #fff; }
.shutter:disabled { opacity: .5; }
.cam-error {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; gap: 12px; padding: 32px;
  background: var(--bg); text-align: center; max-width: 520px; margin: 0 auto;
}

.angles { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 8px; }
.angles h3 { margin: 4px 0; font-size: 15px; color: var(--muted); }
.rank-list.compact li { padding: 6px 0; }
.angle-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 10px 0 4px; }
.verdict small { font-weight: 400; color: var(--muted); font-size: 14px; }
.angle-actions .secondary.small { width: auto; flex: 1 1 auto; margin-top: 0; }
