:root {
  --bg: #0f1216;
  --panel: #181d24;
  --line: #2a323d;
  --text: #e8edf2;
  --muted: #93a1b0;
  --accent: #4fd1c5;
  --warn: #f6ad55;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  overflow-x: hidden;
  padding-left: var(--sl);
  padding-right: var(--sr);
  padding-bottom: calc(var(--sb) + 12px);
}
h1, h2, h3, p, ol, ul, figure, dl { margin: 0; }

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 14px;
  padding-top: max(4px, env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 17px; font-weight: 650; letter-spacing: 0.01em; }
.topnav { display: flex; gap: 10px; }
.toplink {
  color: var(--accent);
  text-decoration: none;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 0 4px;
  font-weight: 600;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin: 8px 10px;
  padding: 14px;
}
.panel h2 { font-size: 16px; font-weight: 650; margin-bottom: 8px; }
.ph { font-size: 13px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 8px; }
.help { color: var(--muted); margin-bottom: 8px; }
.help.small { font-size: 13px; }
.status, .note { color: var(--muted); margin: 8px 0; min-height: 1.4em; }
.note.warn, .warn { color: var(--warn); }
.fine { color: var(--muted); font-size: 12.5px; margin: 6px 12px 10px; }

.btn {
  min-height: 44px;
  padding: 0 16px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: #212832;
  color: var(--text);
  font: 600 15px var(--font);
  cursor: pointer;
  touch-action: manipulation;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #062b28; }
.btn.ghost { background: transparent; }
.btn.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.grow { flex: 1; }
.btn.big { min-height: 56px; font-size: 17px; }
.btn.small { min-height: 44px; padding: 0 12px; font-size: 14px; white-space: nowrap; }
.btn.danger { color: #ff9b9b; border-color: #5a2c2c; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* ---------------------------------------------------------- start */
.start { padding: 18px 0 8px; }
.startwrap { max-width: 520px; margin: 0 auto; }
.start h2 { font-size: 24px; line-height: 1.2; font-weight: 700; margin: 6px 14px 8px; }
.guide { color: var(--text); margin: 0 14px 6px; font-size: 16px; }
.start .bigbtns { display: grid; gap: 10px; margin: 12px 10px; }
.gamelab { margin: 14px 14px 8px; }
.gamepick { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 0 10px; }
.gbtn {
  min-height: 64px;
  padding: 6px 10px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #212832;
  color: var(--text);
  font: 15px var(--font);
  cursor: pointer;
  touch-action: manipulation;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 1px;
}
.gbtn b { font-size: 20px; font-weight: 750; line-height: 1.15; }
.gbtn span { font-size: 12px; color: var(--muted); }
.gbtn.on { background: var(--accent); border-color: var(--accent); color: #062b28; }
.gbtn.on span { color: #0b4a45; }
.gamepick.compact { margin: 0; }
.gamepick.compact .gbtn { min-height: 48px; }
.gamepick.compact .gbtn b { font-size: 16px; }
.minisettings { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 10px 10px 0; }
.panel .minisettings { margin: 10px 0 0; }
.minisettings label { display: grid; gap: 3px; color: var(--muted); font-size: 12px; min-width: 0; }
.startlinks { display: flex; justify-content: space-between; align-items: center; margin: 4px 10px 0; gap: 10px; }
.optin { display: flex; align-items: center; gap: 10px; margin: 14px 10px 0; color: var(--muted); font-size: 12.5px; line-height: 1.35; cursor: pointer; }
.optin input { width: 20px; height: 20px; margin: 0; accent-color: var(--accent); flex: none; }
.textlink {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 0 4px;
  background: none;
  border: 0;
  color: var(--accent);
  font: 600 15px var(--font);
  text-decoration: none;
  cursor: pointer;
}
.start .status { margin: 0 14px; min-height: 1.4em; }

.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.settings label:first-child { grid-column: 1 / -1; }
.settings label { display: grid; gap: 4px; color: var(--muted); font-size: 12.5px; min-width: 0; }
select {
  min-height: 44px;
  width: 100%;
  background: #212832;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 0 8px;
  font: 15px var(--font);
}

/* ---------------------------------------------------------- busy */
.busy { display: grid; place-items: center; align-content: center; gap: 16px; min-height: 60vh; color: var(--muted); font-size: 17px; }
.spinner {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 4px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

/* ---------------------------------------------------------- photo stage */
.work { max-width: 1180px; margin: 0 auto; }
.left, .right { min-width: 0; }

.strip { display: grid; gap: 6px; padding: 8px 10px 0; }
.stripitem {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: space-between;
  border: 1px solid #6b4a1f;
  background: rgba(246, 173, 85, 0.1);
  color: var(--warn);
  border-radius: 9px;
  padding: 6px 6px 6px 12px;
  font-size: 14px;
  line-height: 1.3;
}
.stripitem:not(:has(button)) { padding: 10px 12px; }
.stripitem .btn { border-color: #6b4a1f; color: var(--text); flex: none; }

.stagewrap { margin: 8px 0 0; }
.stage { position: relative; background: #000; text-align: center; line-height: 0; }
.stage canvas#view { display: block; margin: 0 auto; max-width: 100%; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: pan-y; }
body[data-mode="corners"] canvas#view,
body[data-zoom="1"] canvas#view,
body[data-edit="1"] canvas#view { touch-action: none; cursor: crosshair; }
body[data-mode="result"] canvas#view { cursor: pointer; }

/* top view: a second canvas in the same stage; only one of the two is ever shown */
.stage canvas#topview { display: none; margin: 0 auto; max-width: 100%; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: pan-y; cursor: pointer; background: #000; }
body[data-view="top"] canvas#topview { display: block; }
body[data-view="top"] canvas#view { display: none; }
body[data-view="top"] .tools [data-zoomout] { display: none; }
.tools .viewseg { margin: 0; flex: none; }
.tools .viewseg button { flex: none; padding: 0 12px; font-size: 14px; white-space: nowrap; }
.tools .viewseg button:disabled { opacity: 0.4; cursor: default; }

/* under the photo: fix balls / corners / zoom, always in reach */
.tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 8px 10px 0; }
.tools .hint { flex: 1; min-width: 120px; color: var(--muted); font-size: 12.5px; text-align: right; }
body[data-edit="1"] .tools { display: none; }
.stagebusy {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  background: rgba(15, 18, 22, 0.72);
  line-height: 1.4;
  color: var(--text);
}

.editbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 10px 0;
  padding: 8px 8px 8px 12px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: rgba(79, 209, 197, 0.08);
  font-size: 13.5px;
  color: var(--text);
}
.editbar p { flex: 1; }

.loupe {
  position: fixed;
  z-index: 50;
  width: 110px;
  height: 110px;
  border-radius: 50%;
  pointer-events: none;
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px rgba(0, 0, 0, 0.6), 0 6px 18px rgba(0, 0, 0, 0.5);
  background: #000;
}

/* ball popover: below the photo on phones, floating on wide screens */
.pop {
  line-height: 1.4;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--muted);
  padding: 10px;
  margin: 8px 10px;
  border-radius: 10px;
}
.pop h3 { margin: 0 0 8px; font-size: 14px; font-weight: 650; }
.poplab { color: var(--muted); font-size: 12px; margin: 6px 0 4px; text-transform: uppercase; letter-spacing: 0.05em; }
.chips { display: grid; grid-template-columns: repeat(6, 1fr); gap: 5px; }
.chips.likely { grid-template-columns: repeat(auto-fit, minmax(64px, 1fr)); gap: 8px; }
.chips.few { grid-template-columns: repeat(5, 1fr); }
.chips.few .chip { min-height: 52px; font-size: 17px; }
.chip {
  min-height: 44px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid var(--line);
  border-left-width: 5px;
  background: #212832;
  color: var(--text);
  font: 650 14px var(--font);
  cursor: pointer;
  position: relative;
}
.chip.lg { min-height: 60px; font-size: 22px; border-left-width: 8px; }
.chip.cur { background: var(--accent); color: #062b28; border-color: var(--accent); }
.chip.used { color: var(--muted); }
.chip.used::after { content: ""; position: absolute; top: 5px; right: 5px; width: 5px; height: 5px; border-radius: 50%; background: var(--muted); }
.pop .row { margin-top: 8px; }
.chips.groups { grid-template-columns: repeat(4, 1fr); gap: 6px; }
.chip.grp { border-left-width: 1px; font-size: 17px; min-height: 56px; }
.chips.seven { grid-template-columns: repeat(7, 1fr); gap: 5px; }
.chips.seven .chip.lg { min-height: 56px; font-size: 20px; border-left-width: 6px; padding: 0; }

/* ---------------------------------------------------------- the shot card */
.shotcard {
  margin: 10px 10px 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px 14px;
  touch-action: pan-y;
  -webkit-user-select: none;
  user-select: none;
}
.noshot p { color: var(--muted); font-size: 16px; padding: 10px 0; }
.noshot.warnmsg p { color: var(--warn); }

.shotnav { display: grid; grid-template-columns: 52px 1fr 52px; align-items: center; gap: 8px; }
.navbtn {
  min-height: 52px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #212832;
  color: var(--text);
  font: 400 34px/1 var(--font);
  padding: 0 0 4px;
  cursor: pointer;
  touch-action: manipulation;
}
.navbtn:disabled { opacity: 0.3; cursor: default; }
.shottitle { text-align: center; color: var(--muted); font-weight: 650; font-size: 15px; }

.route { display: flex; align-items: center; gap: 12px; margin: 12px 0 10px; min-width: 0; }
.route .dot { flex: none; }
.route .arrow { color: var(--accent); font-size: 34px; line-height: 1; flex: none; }
.route .pocketname { font-size: 26px; font-weight: 750; line-height: 1.1; letter-spacing: -0.01em; min-width: 0; }

.facerow { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; align-items: start; }
.face { text-align: center; min-width: 0; }
.face svg { display: block; width: 100%; height: auto; }
.face.cue svg { max-width: 176px; margin: 0 auto; }
.face.obj { margin-top: 12px; }
.face.obj svg { max-width: 420px; margin: 0 auto; }
.face figcaption { font-size: 14px; color: var(--muted); margin-top: 6px; line-height: 1.35; }
.face.obj figcaption b { color: var(--text); font-size: 16px; }
.face.cue figcaption { color: var(--text); font-weight: 600; font-size: 15px; }

.stats { display: grid; gap: 8px; }
.stat { border: 1px solid var(--line); background: #12161c; border-radius: 10px; padding: 8px 10px; }
.stat .k { display: block; color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
.stat b { display: block; font-size: 26px; line-height: 1.15; font-weight: 750; }
.stat .s { display: block; color: var(--muted); font-size: 12.5px; line-height: 1.3; }

.lines { margin: 12px 0 0; padding: 0; list-style: none; display: grid; gap: 5px; font-size: 14.5px; }
.lines li { color: var(--text); }

/* ---------------------------------------------------------- all shots, settings, actions */
.alllist { margin: 10px 10px 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; }
.alllist summary { min-height: 48px; display: flex; align-items: center; padding: 0 14px; font-weight: 650; cursor: pointer; list-style-position: inside; }
.runout { list-style: none; padding: 0 10px 10px; display: grid; gap: 6px; margin: 0; }
.runout button {
  width: 100%;
  min-height: 44px;
  display: grid;
  grid-template-columns: 26px 1fr auto;
  align-items: center;
  gap: 8px;
  text-align: left;
  padding: 0 12px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: #212832;
  color: var(--text);
  font: 15px var(--font);
  cursor: pointer;
}
.runout button .n { color: var(--muted); font-weight: 650; }
.runout button .r { color: var(--muted); font-size: 13px; }
.runout button.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.runout button.on .n { color: var(--accent); }
.runout .end { color: var(--muted); font-size: 13px; padding: 4px 2px; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 10px 0; }
.actions .span2 { grid-column: 1 / -1; }

.summary {
  margin: 10px 10px 0;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #12161c;
  font-size: 16px;
  font-weight: 650;
  line-height: 1.3;
}

/* "Next shot": fixed to the bottom on phones, above the home bar */
.nextbar { display: none; }
body[data-mode="result"] .nextbar { display: block; }
body[data-mode="result"][data-edit="1"] .nextbar { display: none; }
@media (max-width: 899px) {
  body[data-mode="result"] { padding-bottom: calc(var(--sb) + 92px); }
  body[data-mode="result"][data-edit="1"] { padding-bottom: calc(var(--sb) + 12px); }
  .nextbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    padding: 8px calc(10px + var(--sr)) calc(var(--sb) + 8px) calc(10px + var(--sl));
    background: rgba(15, 18, 22, 0.96);
    border-top: 1px solid var(--line);
  }
  .nextbar .btn { width: 100%; }
}
@media (min-width: 900px) {
  .nextbar { margin: 8px 0 0; }
  .nextbar .btn { width: 100%; }
}

/* corner step */
.seg { display: flex; gap: 0; margin: 8px 0; }
.seg button {
  flex: 1;
  min-height: 44px;
  border: 1px solid var(--line);
  background: #212832;
  color: var(--muted);
  font: 600 15px var(--font);
  cursor: pointer;
}
.seg button:first-child { border-radius: 9px 0 0 9px; }
.seg button:last-child { border-radius: 0 9px 9px 0; }
.seg button + button { border-left: 0; }
.seg button.on { background: var(--accent); border-color: var(--accent); color: #062b28; }

@media (max-width: 380px) {
  .chips { grid-template-columns: repeat(5, 1fr); }
  .route .pocketname { font-size: 23px; }
}

@media (min-width: 900px) {
  .work { display: grid; grid-template-columns: minmax(0, 1fr) 440px; align-items: start; gap: 10px; padding: 0 10px 24px; }
  .strip, .editbar, .tools, .stagewrap { margin-left: 0; margin-right: 0; padding-left: 0; padding-right: 0; }
  .stage { border-radius: 10px; overflow: visible; }
  .panel, .shotcard, .alllist, .actions { margin-left: 0; margin-right: 0; }
  .fine { margin-left: 2px; margin-right: 2px; }
  .right { position: sticky; top: 0; max-height: 100vh; overflow-y: auto; padding-bottom: 16px; }
  .start .bigbtns { grid-template-columns: 1fr 1fr; }
  .gamepick:not(.compact) { grid-template-columns: repeat(4, 1fr); }
  .startwrap { max-width: 720px; }
  .pop {
    position: absolute;
    z-index: 20;
    width: 300px;
    margin: 0;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
  }
}

/* Full advice stays visible; compact diagrams make both aiming references easy to compare. */
.aim-diagrams { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.65fr); gap: 8px; margin: 12px 0 10px; }
.aim-panel { background: #12161c; border: 1px solid var(--line); border-radius: 10px; padding: 10px 6px; }
.aim-panel h3 { font-size: 14px; line-height: 1.2; margin-bottom: 6px; color: var(--text); }
.aim-diagrams .face.obj { margin: 0; }
.aim-diagrams .face.cue svg { width: 96px; max-width: 100%; height: 96px; margin: 0 auto; }
.aim-diagrams .face.obj svg { width: 100%; height: 96px; margin: 0 auto; }
.aim-diagrams .face figcaption { font-size: 13px; line-height: 1.35; font-weight: 400; }
.aim-diagrams .face figcaption b { display: block; font-size: 13px; color: var(--text); }
.aim-diagrams .face figcaption span { display: block; font-size: 12px; margin-top: 5px; color: var(--muted); }
.shot-stats { display: grid; grid-template-columns: 1.3fr 1fr; gap: 8px; }
.shot-stats .stat b { font-size: 21px; }
.shot-stats .stat .k { font-size: 11px; }
.shotcard .lines { font-size: 14px; margin-top: 10px; }
.shotcard .shotnav { margin-top: 12px; grid-template-columns: 44px 1fr 44px; }
.shotcard .navbtn { min-height: 44px; }
.shotcard .fine { margin: 8px 0 0; }
.settings-fold .panel { border: 0; margin: 0; padding-top: 0; }
.ball-crop { display: block; float: right; width: 64px; height: 64px; border-radius: 8px; margin: 0 0 8px 8px; }
.pop .groups { clear: both; }
body.picking-ball .nextbar { display: none; }
@media (max-width: 899px) {
  .shotcard { padding: 10px 12px; margin-top: 8px; }
  .shotcard .route { margin: 0; gap: 8px; }
  .shotcard .route .pocketname { font-size: 23px; }
  .shotcard .route .arrow { font-size: 26px; }
  .summary { font-size: 13px; padding: 6px 10px; margin-top: 6px; }
  .tools { gap: 6px; }
  .tools .hint { display: none; }
  .tools .btn, .tools .viewseg button { padding: 0 9px; font-size: 13px; }
  #btnCorners { margin-left: auto; }
  .pop { position: fixed; left: 8px !important; right: 8px; bottom: calc(var(--sb) + 8px); top: auto !important; z-index: 60; margin: 0; max-height: 75dvh; overflow-y: auto; box-shadow: 0 0 0 100vmax #0007; }
  .pop .chip.lg { min-height: 48px; font-size: 19px; }
  .pop .chips.seven { grid-template-columns: repeat(7, minmax(0, 1fr)); }
  .strip { padding-top: 6px; }
  .stripitem { font-size: 12px; padding: 4px 4px 4px 8px; }
}
@media (max-width: 360px) {
  .tools { gap: 4px; }
  .tools .btn, .tools .viewseg button { font-size: 12px; padding: 0 6px; }
  .aim-diagrams .face.cue svg, .aim-diagrams .face.obj svg { height: 80px; }
  .aim-diagrams .face.cue svg { width: 80px; }
  .shot-stats .stat b { font-size: 18px; }
}

/* The preview contains the full frame: it never crops off a rail to fill the screen. */
body.camera-open { overflow: hidden; }
.camera-screen { position: fixed; inset: 0; z-index: 100; background: var(--bg); padding: max(10px, env(safe-area-inset-top)) max(12px, var(--sr)) max(12px, var(--sb)) max(12px, var(--sl)); display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.camera-header { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.camera-header h2 { font-size: 19px; }
.camera-preview { position: relative; flex: 1; min-height: 160px; background: #000; overflow: hidden; }
.camera-preview video, .camera-preview canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.camera-preview canvas { pointer-events: none; }
.camera-hint { padding: 10px; border: 1px solid var(--warn); border-radius: 9px; color: var(--warn); min-height: 44px; font-weight: 650; }
.camera-hint[data-ready="true"] { border-color: var(--accent); color: var(--accent); }
.camera-help { color: var(--muted); font-size: 13px; }
.camera-auto { display: flex; align-items: center; gap: 10px; min-height: 44px; }
.camera-auto input { width: 22px; height: 22px; accent-color: var(--accent); }
.camera-fallback { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (min-width: 900px) { .camera-screen { max-width: 850px; margin: auto; } }
@media (max-height: 500px) and (orientation: landscape) {
  .camera-screen { display: grid; grid-template-columns: 1fr 270px; grid-template-rows: auto 1fr auto auto; }
  .camera-header { grid-column: 1 / -1; }
  .camera-preview { grid-column: 1; grid-row: 2 / 5; }
  .camera-help { display: none; }
  .camera-fallback { grid-column: 2; }
}

@media (max-width: 899px) and (max-height: 650px) {
  #summary { display: none; }
  .shotcard .route .pocketname { font-size: 21px; }
}

.shot-alternatives { margin-top: 10px; }
.shot-alternatives > .btn { width: 100%; border-color: var(--accent); font-size: 14px; padding: 7px 10px; }
.alternative-note { color: var(--muted); font-size: 13px; margin: 6px 0; }
.shot-alternatives .textlink { min-height: 36px; font-size: 13px; }
