:root {
  --bg: #11161c; --panel: #1a222b; --ink: #e6edf3; --muted: #8b98a5; --line: #2b3744; --accent: #3fb68b; --warn: #e5a23c; --bad: #e5604c;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #eef2f5; --panel: #ffffff; --ink: #18222c; --muted: #5d6b79; --line: #d4dde5; --accent: #1f9a6f; --warn: #b9770e; --bad: #c0392b; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
main { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; padding: 16px; }
.stage { position: relative; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
canvas { box-shadow: 0 14px 34px rgba(0,0,0,.4); width: 100%; height: auto; aspect-ratio: 2.3 / 1; background: transparent; border-radius: 10px; touch-action: none; display: block; }
.hint { color: var(--muted); font-size: 13px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 14px; align-self: start; }
h1 { font-size: 19px; margin: 0; }
.sub { margin: 0; color: var(--muted); font-size: 13px; }
.group { display: flex; flex-direction: column; gap: 10px; padding-top: 12px; border-top: 1px solid var(--line); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
label.check { flex-direction: row; align-items: flex-start; gap: 8px; }
.muted { color: var(--muted); font-size: 12px; }
select, button { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; cursor: pointer; }
button:hover, select:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 600; }
button.big { padding: 11px 14px; font-size: 16px; flex: 1; }
.btnrow { display: flex; flex-wrap: wrap; gap: 8px; }
.seg { display: flex; gap: 6px; }
.seg button { width: 40px; padding: 6px 0; }
.seg button.on { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; }
input[type=range] { width: 100%; accent-color: var(--accent); }
details { font-size: 14px; } summary { cursor: pointer; color: var(--muted); }
.info { font-size: 14px; display: flex; flex-direction: column; gap: 6px; min-height: 40px; }
.info .kv { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed var(--line); padding: 3px 0; }
.info .kv b { font-variant-numeric: tabular-nums; }
.info .ok { color: var(--accent); } .info .warn { color: var(--warn); } .info .bad { color: var(--bad); }
.info h3 { margin: 6px 0 0; font-size: 14px; color: var(--muted); font-weight: 600; }
@media (max-width: 900px) { main { grid-template-columns: minmax(0, 1fr); padding: 6px 8px 10px; gap: 10px; } .panel { min-width: 0; padding: 12px; } .btnrow button { flex: 1 1 40%; } select { max-width: 100%; } }
#tdfInfo { display: none; }  /* chi tiet TDF khong hien thi (chu du an 30/10) */

.face-row { display: flex; gap: 12px; align-items: center; }
#face { width: 104px; height: 104px; flex: 0 0 104px; border-radius: 50%; touch-action: none; cursor: crosshair; background: transparent; }
.face-side { display: flex; flex-direction: column; gap: 6px; min-width: 0; font-size: 14px; }
.face-side .btnrow button { padding: 5px 8px; font-size: 13px; }
.credit { margin: 4px 0 0; font-size: 11px; } .credit a { color: var(--accent); }
button[hidden] { display: none; }

[hidden] { display: none !important; }

/* ---- v6: action bar under the table, compact phone layout ---- */
.actions { display: flex; gap: 8px; }
.actions button { padding: 12px 14px; font-size: 17px; border-radius: 10px; }
.actions .big { flex: 1; }
.actions .ghost { flex: 0 0 auto; background: transparent; }
.layout-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.layout-row button { padding: 7px 2px; font-size: 13px; white-space: nowrap; }
.row2 { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.row2 label { flex-direction: row; align-items: center; gap: 6px; }
.row2 select { padding: 5px 8px; }
.spin-btns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.spin-btns button { padding: 6px 4px; font-size: 13px; }
.spin-btns button.on { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; }
.group.first { border-top: 0; padding-top: 0; }
@media (max-width: 900px) {
  .sub { display: none; }
  h1 { font-size: 17px; }
  .panel { gap: 10px; }
  .group { gap: 8px; padding-top: 10px; }
  .face-row { gap: 10px; }
  #face { width: 92px; height: 92px; flex-basis: 92px; }
  .spin-btns { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .spin-btns button { font-size: 12px; padding: 7px 2px; }
  .actions { position: sticky; bottom: 0; z-index: 3; padding: 6px 0; background: linear-gradient(to top, var(--bg) 70%, transparent); }
  .hint { font-size: 12px; padding: 0 8px; }
  .info { font-size: 13px; }
  .stage { gap: 6px; }
}

.note { background: rgba(63,182,139,.14); border: 1px solid var(--accent); border-radius: 8px; padding: 6px 10px; font-size: 13px; }

.pk { display: flex; flex-direction: column; gap: 6px; }
.pocket-btns { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.pocket-btns button { padding: 6px 2px; font-size: 13px; position: relative; }
.pocket-btns button.bad { opacity: .55; }
.pocket-btns button.ok { border-color: var(--accent); }
.pocket-btns button.star::after { content: "★"; position: absolute; top: -6px; right: -3px; font-size: 12px; color: #ffe94d; }
.pocket-btns button.on { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; opacity: 1; }

.legend { display: flex; gap: 12px; justify-content: flex-end; font-size: 11px; color: var(--muted); margin-top: -2px; }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i { display: inline-block; width: 14px; height: 3px; border-radius: 2px; }

/* ---- v16: control dock under the table: cue-ball face + force + shoot / redo ---- */
.dock { display: flex; gap: 12px; align-items: center; }
.face-wrap { display: flex; flex-direction: column; align-items: center; gap: 3px; flex: 0 0 auto; }
#face { width: 104px; height: 104px; flex: 0 0 104px; }
.spin-name { font-size: 12px; color: var(--ink); text-align: center; max-width: 108px; line-height: 1.2; font-weight: 600; }
.dock-main { flex: 1; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.force { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.actions button.icon { flex: 0 0 52px; font-size: 24px; padding: 8px 0; background: transparent; line-height: 1; }
@media (max-width: 900px) {
  #face { width: 92px; height: 92px; flex-basis: 92px; }
  .dock { position: sticky; bottom: 0; z-index: 3; padding: 6px 0; background: linear-gradient(to top, var(--bg) 78%, transparent); }
  .actions { position: static; padding: 0; background: none; }
}

.face-cap { font-size: 11px; color: var(--muted); letter-spacing: .02em; }

/* v18: nothing under / on the cue-ball face changes size while dragging it (no layout jitter) */
.face-wrap { gap: 0; }
#face { touch-action: none; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
body { overflow-anchor: none; }
@media (max-width: 900px) { .info { min-height: 420px; } }

/* ---- v20: magnifier, shot bar on the table, drills ---- */
#loupe { position: fixed; z-index: 50; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 8px 26px rgba(0,0,0,.55); pointer-events: none; background: #222; }
#loupe[hidden] { display: none; }
.shotbar { position: absolute; left: 8px; right: 8px; top: 8px; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  background: rgba(10,16,28,.9); color: #fff; border: 1px solid rgba(255,255,255,.25); border-radius: 10px; padding: 6px 8px; font-size: 13px; backdrop-filter: blur(3px); }
.shotbar.good { border-color: #3fb68b; } .shotbar.badr { border-color: #e5604c; }
.shotbar .sb-btns { display: flex; gap: 6px; }
.shotbar button { padding: 4px 9px; font-size: 12px; border-radius: 8px; background: rgba(255,255,255,.1); color: #fff; border-color: rgba(255,255,255,.25); }
.drill-card { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.dc-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
#dcScore { color: var(--accent); font-weight: 700; font-size: 13px; white-space: nowrap; }
.dc-text { display: flex; flex-direction: column; gap: 4px; color: var(--ink); }
#dcNew { align-self: flex-start; padding: 5px 12px; background: transparent; }
#drill { width: 100%; }

@media (max-width: 900px) {
  .shotbar { flex-wrap: nowrap; padding: 4px 6px; left: 6px; right: 6px; top: 6px; font-size: 12px; }
  #shotMsg { flex: 1; min-width: 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.25; }
  .shotbar .tx { display: none; }
  .shotbar button { padding: 5px 10px; font-size: 15px; }
}

.cheat-btns { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 5px; }
.cheat-btns button { padding: 6px 1px; font-size: 12px; white-space: nowrap; }
.cheat-btns button.ok { border-color: var(--accent); }
.cheat-btns button.bad { opacity: .5; }
.cheat-btns button.on { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; opacity: 1; }
canvas#table { will-change: contents; }

.aim-ctl { display: grid; grid-template-columns: auto auto 1fr auto 52px; gap: 6px; align-items: center; }
.aim-ctl button { padding: 6px 10px; font-size: 14px; }
.aim-ctl button.on { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; }
.aim-ctl b { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }
.group.drill { border-top: 1px dashed var(--line); }

.seg { flex-wrap: wrap; justify-content: flex-end; }
.seg button { width: 36px; }
.shotbar #sbAgain { border-color: var(--accent); }

.force { display: flex; flex-direction: column; gap: 5px; font-size: 13px; }
.force-head { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; flex-wrap: wrap; }
.v0-chips { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 4px; }
.v0-chips button { padding: 4px 1px; font-size: 11px; white-space: nowrap; }
.v0-chips button.on { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; }
@media (max-width: 900px) { .v0-chips { grid-template-columns: repeat(3, minmax(0, 1fr)); } .v0-chips button { padding: 5px 2px; font-size: 12px; } }

.sel-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: 14px; }
.sel-row .ghost { padding: 5px 10px; font-size: 12px; background: transparent; }
.sel-row[hidden], .sel-row .ghost[hidden] { display: none; }

/* v37: spin-compensation / cheat-the-pocket rows sit right under the table; no force buttons on phones */
.tweaks { display: flex; flex-direction: column; gap: 8px; }
.tweaks .pk { gap: 4px; font-size: 13px; }
@media (max-width: 900px) { .v0-chips { display: none; } .tweaks { gap: 6px; } .tweaks .cheat-btns button { padding: 5px 0; font-size: 11px; } }

/* v39: all notes under the table, never on it */
.legend i.dash { background: repeating-linear-gradient(90deg, #fff 0 4px, transparent 4px 7px); }
.live-note { font-size: 13px; color: var(--ink); min-height: 1.3em; }
.stage .shotbar { position: static; left: auto; right: auto; top: auto; }
.stage .shotbar[hidden] { display: flex !important; visibility: hidden; }   /* keeps its place, so nothing jumps when a result appears */
/* v40: the spin ball can be small - the magnifier shows it enlarged while it is pressed */
#face { width: 72px; height: 72px; flex: 0 0 72px; }
@media (max-width: 900px) { #face { width: 64px; height: 64px; flex-basis: 64px; } }

/* v42: tighter stack under the table; the result bar sits on the note row */
.notes { position: relative; min-height: 34px; display: flex; align-items: center; }
.stage .shotbar { position: absolute; left: 0; right: 0; top: 0; bottom: 0; padding: 3px 6px; z-index: 2; }
.stage .shotbar[hidden] { display: none !important; visibility: visible; }
.live-note { font-size: 12.5px; line-height: 1.25; }
.stage { gap: 5px; }
.tweaks { gap: 5px; }
.dock { gap: 10px; }
.actions { gap: 0; }
@media (max-width: 900px) {
  main { padding-top: 4px; }
  .stage { gap: 4px; }
  .legend { margin-top: 0; }
  .hint { font-size: 11.5px; line-height: 1.3; }
  .dock { padding: 4px 0; }
}

.aim-ctl button.zero { padding: 6px 4px; min-width: 52px; font-size: 13px; background: transparent; font-variant-numeric: tabular-nums; }

/* v45: result bar without a frame */
.stage .shotbar { background: transparent; border: 0; backdrop-filter: none; padding: 0 2px; color: var(--ink); }
.stage .shotbar.good #shotMsg { color: var(--accent); font-weight: 600; }
.stage .shotbar.badr #shotMsg { color: var(--bad); font-weight: 600; }
.stage .shotbar button { background: transparent; color: var(--ink); border-color: var(--line); }

.notes.has-bar .live-note { visibility: hidden; }

.donebar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 8px 10px; border: 1px solid var(--accent); border-radius: 10px; background: var(--panel); }
.donebar select { flex: 1; min-width: 150px; }
.donebar button { padding: 8px 16px; }
.donebar[hidden] { display: none; }

/* v48: finished screen */
.stage.is-done .dock, .stage.is-done .tweaks, .stage.is-done .hint { display: none; }
.donebar { display: flex; flex-direction: column; gap: 10px; padding: 14px 14px 12px; border: 1px solid color-mix(in srgb, var(--accent) 55%, transparent); border-radius: 16px;
  background: linear-gradient(160deg, color-mix(in srgb, var(--accent) 16%, var(--panel)), var(--panel) 60%); box-shadow: 0 10px 28px rgba(0,0,0,.28); }
.donebar[hidden] { display: none; }
.done-head { display: flex; align-items: center; gap: 12px; }
.done-ic { font-size: 34px; line-height: 1; filter: drop-shadow(0 3px 6px rgba(0,0,0,.35)); }
.done-title { font-size: 20px; font-weight: 800; letter-spacing: .01em; }
.done-sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.done-label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-top: 2px; }
.done-tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.done-tile { display: flex; align-items: center; gap: 9px; text-align: left; padding: 9px 10px; border-radius: 12px; background: var(--bg); border: 1px solid var(--line); min-width: 0; }
.done-tile .dt-badge { flex: 0 0 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; font-weight: 800; font-size: 14px; background: color-mix(in srgb, var(--accent) 22%, var(--panel)); color: var(--ink); }
.done-tile .dt-txt { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.done-tile b { font-size: 13px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.done-tile i { font-style: normal; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.done-tile.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.done-tile:active { transform: scale(.98); }
.done-go { padding: 12px; font-size: 15px; border-radius: 12px; }

/* v50: calm "an gian lo" chips (same look in every state, a small dot tells whether the ball still drops) */
.cheat-btns button, .cheat-btns button.ok, .cheat-btns button.bad { opacity: 1; border-color: var(--line); background: var(--bg); position: relative; }
.cheat-btns button.ok::after, .cheat-btns button.bad::after { content: ""; position: absolute; top: 4px; right: 5px; width: 6px; height: 6px; border-radius: 50%; }
.cheat-btns button.ok::after { background: #3fb68b; }
.cheat-btns button.bad::after { background: #e5604c; }
.cheat-btns button.on, .cheat-btns button.on.ok, .cheat-btns button.on.bad { background: var(--accent); color: #08150f; border-color: var(--accent); font-weight: 700; }
.cheat-btns button.on::after { background: #08150f; opacity: .55; }
/* force ticks */
.force-ticks { position: relative; height: 13px; margin-top: -2px; font-size: 10px; color: var(--muted); }
.force-ticks span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
/* mode button / menu */
.mode-btn { width: 100%; padding: 8px 10px; font-size: 13px; background: transparent; border-style: dashed; }
.done-label { display: flex; justify-content: space-between; align-items: center; }
#menuClose { padding: 2px 9px; font-size: 13px; background: transparent; }
#menuClose[hidden], .done-go[hidden] { display: none; }

/* v51: compact finished card (drop-down instead of tiles) */
.donebar { gap: 8px; padding: 10px 12px; border-radius: 14px; }
.done-ic { font-size: 26px; }
.done-title { font-size: 17px; }
.done-sub { font-size: 12px; }
.done-txt { flex: 1; min-width: 0; }
.done-row { display: flex; gap: 8px; }
.done-row select { flex: 1; min-width: 0; padding: 8px 10px; }
.done-row .primary { padding: 8px 18px; }
#menuClose { padding: 3px 10px; font-size: 14px; background: transparent; align-self: flex-start; }
#menuClose[hidden] { display: none; }

/* aim mode switch: the app aims ("Tự động") or the player swings the ruler ("Tự ngắm") */
.aim-seg { display: grid; grid-template-columns: 1fr 1fr; gap: 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.aim-seg button { border: 0; border-radius: 0; padding: 7px 4px; font-size: 12.5px; background: transparent; }
.aim-seg button.on { background: var(--accent); color: #08150f; font-weight: 700; }
body.hand #cheatRow, body.hand #railRow { display: none !important; }

/* ================= landscape game HUD: left rail (menu, spin, mode) | balls row + table | cue (power) bar ================= */
.opts-btn, .sheet-x, #scrim, .hud-left, .hud-top, .cuebar, .face-modal { display: none; }
.face-modal[hidden] { display: none !important; }
@media (orientation: landscape) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); -webkit-user-select: none; user-select: none; }
  main { display: block; padding: 0; height: 100%; }
  .stage { display: grid; grid-template-columns: 84px minmax(0, 1fr) 84px; grid-template-rows: 34px minmax(0, 1fr) 22px; gap: 0; height: 100%; padding: 2px 4px; position: relative; }
  .hud-left { display: flex; flex-direction: column; align-items: center; gap: 10px; grid-column: 1; grid-row: 1 / span 3; padding-top: 4px; }
  .hud-gap { height: 40px; flex: 0 0 40px; }
  #face { width: 52px; height: 52px; flex-basis: 52px; cursor: pointer; }
  .hud-left .aim-seg { grid-template-columns: 1fr; width: 78px; }
  .hud-left .aim-seg button { padding: 8px 2px; font-size: 12px; }
  .hud-top { display: flex; grid-column: 2; grid-row: 1; justify-content: center; align-items: center; gap: 4px; min-width: 0; overflow: hidden; }
  .bchip { width: 24px; height: 24px; flex: 0 0 24px; }
  .bchip.gone { opacity: .22; filter: grayscale(1); }
  .bchip.now { filter: drop-shadow(0 0 4px #3fb68b) drop-shadow(0 0 2px #3fb68b); }
  .notes { grid-column: 2; grid-row: 3; min-height: 0; align-self: center; overflow: hidden; max-width: calc(100% - 200px); }
  .live-note { font-size: 11.5px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .legend { grid-column: 2; grid-row: 3; justify-self: end; align-self: center; margin: 0; font-size: 10px; gap: 8px; }
  .legend i { width: 10px; }
  .stage .shotbar { font-size: 11.5px; }
  #shotMsg { -webkit-line-clamp: 1; }
  .donebar { position: absolute; z-index: 8; left: 50%; top: 50%; transform: translate(-50%, -50%); width: min(430px, 60%); }
  .dock { display: none !important; }
  .tweaks, .mode-btn, .hint { display: none; }
  .sheet .tweaks, .sheet .mode-btn, .sheet .hint { display: flex; }
  .opts-btn { display: grid; place-items: center; position: fixed; z-index: 30; top: calc(6px + env(safe-area-inset-top)); left: calc(8px + env(safe-area-inset-left)); width: 38px; height: 38px; padding: 0; border-radius: 10px; font-size: 18px; background: var(--panel); }
  .cuebar { display: flex; flex-direction: column; align-items: center; gap: 4px; grid-column: 3; grid-row: 1 / span 3; padding: 2px 0; touch-action: none; user-select: none; }
  .cue-val { font-size: 15px; font-weight: 800; font-variant-numeric: tabular-nums; min-height: 20px; }
  .cue-cap { font-size: 10px; color: var(--muted); }
  .cue-track { position: relative; flex: 1; width: 30px; border-radius: 15px; border: 1px solid var(--line); overflow: visible;
    background: linear-gradient(to bottom, rgba(255,255,255,.07), rgba(255,255,255,.02)), repeating-linear-gradient(to bottom, transparent 0 calc(10% - 1px), rgba(255,255,255,.22) calc(10% - 1px) 10%); }
  .cue-fill { position: absolute; left: 3px; right: 3px; top: 0; height: 40%; border-radius: 12px; background: linear-gradient(to bottom, #3fb68b, #e5c13c 60%, #e5604c); opacity: .85; }
  .cue-stick { position: absolute; left: 50%; top: 24%; transform: translateX(-50%); width: 16px; height: 150%; border-radius: 8px 8px 3px 3px; background: linear-gradient(90deg, #6a4527, #d9b27a 40%, #f3dcb0 55%, #8a5a30); box-shadow: 0 4px 10px rgba(0,0,0,.5); }
  .cue-stick::before { content: ""; position: absolute; top: 0; left: 0; right: 0; height: 7px; border-radius: 8px 8px 0 0; background: #4aa3d8; }
  .cuebar.pull .cue-stick { box-shadow: 0 6px 16px rgba(63,182,139,.55); }
  .panel.sheet { position: fixed; z-index: 40; top: 0; right: 0; bottom: 0; width: min(460px, 80vw); border-radius: 14px 0 0 14px; overflow-y: auto; padding: calc(12px + env(safe-area-inset-top)) calc(14px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) 14px;
    transform: translateX(105%); transition: transform .22s ease; align-self: auto; box-shadow: -10px 0 30px rgba(0,0,0,.4); -webkit-user-select: text; user-select: text; }
  body.opts .panel.sheet { transform: none; }
  .sheet-x { display: block; position: absolute; top: 8px; right: 10px; padding: 3px 11px; background: transparent; }
  #scrim { position: fixed; inset: 0; z-index: 35; background: rgba(0,0,0,.45); }
  body:not(.opts) #scrim { display: none; }
  body.opts #scrim { display: block; }
  .info { min-height: 0 !important; }
  .panel .sub { display: none; }
  /* the sheet is a scrolling flex column: its blocks must keep their natural height (default shrinking squeezed them into each other), and the close button gets its own row */
  .panel.sheet > * { flex: 0 0 auto; }
  .panel.sheet { padding-top: calc(44px + env(safe-area-inset-top)); }
  .sheet-x { top: calc(8px + env(safe-area-inset-top)); }
  .face-modal { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); place-items: center; display: grid; }
  .fm-card { display: flex; flex-direction: column; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 12px 18px; }
  #faceBig { width: 220px; height: 220px; touch-action: none; border-radius: 50%; }
  .fm-name { font-size: 13px; font-weight: 600; min-height: 1.3em; }
  .fm-card .primary { padding: 7px 28px; }
}

/* the static table (wood, cushions, cloth) lives on its own canvas so a frame only repaints balls and lines */
.table-wrap { position: relative; width: 100%; }
.table-wrap canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; border-radius: 10px; }
#tableBg { pointer-events: none; }   /* no blurred drop shadow around the table: a 34 px blur around a canvas this size costs the compositor on every frame of a 120 Hz iPad */
#table { box-shadow: none !important; }
@media (orientation: landscape) {
  .table-wrap { grid-column: 2; grid-row: 2; align-self: center; justify-self: center; width: min(100%, calc((100dvh - 62px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--ar, 1.83))); }
  .table-wrap canvas { border-radius: 8px; }
}

/* ================= landscape HUD v2: slim top bar, black background, pull-down cue ================= */
.topbar { display: contents; }
@media (orientation: landscape) {
  :root { color-scheme: dark; --bg: #000; --panel: #14181d; --ink: #e6edf3; --muted: #8b98a5; --line: #262d36; --accent: #3fb68b; }
  html, body { background: #000; color: var(--ink); }
  .stage { grid-template-columns: minmax(0, 1fr) 72px; grid-template-rows: auto minmax(0, 1fr); padding: 2px 6px 3px 6px; }
  .topbar { display: flex; align-items: center; gap: 8px; grid-column: 1; grid-row: 1; min-width: 0; height: 42px; padding: 0 2px; }
  .topbar .opts-btn { position: static; flex: 0 0 auto; width: 34px; height: 34px; border-radius: 9px; font-size: 16px; background: #14181d; }
  .topbar .face-wrap { flex: 0 0 auto; }
  .topbar #face { width: 38px; height: 38px; flex-basis: 38px; }
  .topbar .aim-seg { display: inline-grid; grid-template-columns: auto auto; width: auto; flex: 0 0 auto; border-radius: 8px; }
  .topbar .aim-seg button { padding: 4px 9px; font-size: 10.5px; line-height: 1.2; }
  .topbar .hud-top { grid-column: auto; grid-row: auto; flex: 0 0 auto; gap: 3px; justify-content: flex-start; overflow: visible; }
  .bchip { width: 22px; height: 22px; flex: 0 0 22px; }
  .topbar .notes { grid-column: auto; grid-row: auto; flex: 1 1 0; min-width: 0; max-width: none; height: 34px; min-height: 0; position: relative; display: flex; align-items: center; overflow: hidden; }
  .topbar .live-note { font-size: 10.5px; color: #aab6c2; }
  .topbar .stage .shotbar, .topbar .shotbar { font-size: 11px; }
  .legend { display: none; }
  .table-wrap { grid-column: 1; grid-row: 2; width: min(100%, calc((100dvh - 52px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--ar, 1.83))); }
  .cuebar { grid-column: 2; grid-row: 1 / span 2; }
  #tableBg { box-shadow: none; }
  .cue-track { background: linear-gradient(to bottom, rgba(255,255,255,.06), rgba(255,255,255,.02)), repeating-linear-gradient(to bottom, transparent 0 calc(10% - 1px), rgba(255,255,255,.16) calc(10% - 1px) 10%); }
  .cue-stick { top: 0; transition: top .28s cubic-bezier(.2, .8, .2, 1); }
  .cuebar.pull .cue-stick { transition: none; }
  .cue-fill { transition: height .2s ease; }
  .cuebar.pull .cue-fill { transition: none; }
  .cue-mark { position: absolute; left: -5px; right: -5px; height: 2px; margin-top: -1px; background: #e6edf3; opacity: .55; border-radius: 1px; pointer-events: none; }
}
@media (orientation: landscape) {
  .topbar .opts-btn { color: #e6edf3; border: 1px solid #262d36; }
  .topbar .aim-seg button { color: #cfd8e0; background: transparent; }
  .topbar .aim-seg button.on { color: #06140e; background: var(--accent); }
  .cuebar { overflow: hidden; }
  .cue-cap { position: relative; z-index: 2; background: #000; width: 100%; text-align: center; padding-top: 2px; }
  .cue-val { color: #e6edf3; }
}

/* ---- right rail: spin ball + slim carbon cue ---- */
.rail { display: contents; }
@media (orientation: landscape) {
  .rail { display: flex; flex-direction: column; align-items: center; gap: 6px; grid-column: 2; grid-row: 1 / span 2; min-height: 0; padding-top: 2px; }
  .rail .face-wrap { flex: 0 0 auto; }
  .rail #face { width: 50px; height: 50px; flex-basis: 50px; }
  .rail .cuebar { grid-column: auto; grid-row: auto; flex: 1 1 0; min-height: 0; width: 100%; }
  .rail { --pc: hsl(105 90% 56%); }
  .cue-val { color: var(--pc); font-size: 14px; min-height: 18px; }
  .cue-track { width: 16px; border-radius: 8px; border: 1px solid #2a323c; background: rgba(255,255,255,.04); }
  .cue-fill { left: 2px; right: 2px; border-radius: 6px; background: linear-gradient(to bottom, hsl(140 90% 56%), var(--pc)); opacity: .95; }
  /* a slim, tapered carbon cue: thin at the tip (top), thicker towards the butt; weave + a gloss line + a coloured inlay and tip */
  .cue-stick { width: 11px; height: 150%; border-radius: 0; clip-path: polygon(34% 0, 66% 0, 100% 100%, 0 100%); box-shadow: none; overflow: hidden;
    background:
      linear-gradient(to bottom, transparent 0 15%, var(--pc) 15% 16.5%, transparent 16.5% 17.5%, #cfd6df 17.5% 18.6%, transparent 18.6% 100%),
      linear-gradient(90deg, rgba(255,255,255,0) 0, rgba(255,255,255,.34) 36%, rgba(255,255,255,0) 54%, rgba(0,0,0,.45) 100%),
      repeating-linear-gradient(45deg, rgba(255,255,255,.07) 0 1.5px, transparent 1.5px 3px),
      repeating-linear-gradient(-45deg, rgba(0,0,0,.35) 0 1.5px, transparent 1.5px 3px),
      #15171a; }
  .cue-stick::before { height: 6px; border-radius: 0; left: 0; right: 0; background: linear-gradient(to bottom, var(--pc) 0 55%, #f1f3f6 55% 100%); }
  .cuebar.pull .cue-stick { box-shadow: none; }
  .cue-mark { left: -4px; right: -4px; opacity: .6; }
  .topbar .live-note { display: none; }
  /* cue (pull to shoot) beside a thin power gauge on the outer edge (sets the planned power) */
  .stage { grid-template-columns: minmax(0, 1fr) 82px; }
  .rail .cuewrap { display: flex; gap: 6px; flex: 1 1 0; min-height: 0; width: 100%; }
  .rail .cuewrap .cuebar { flex: 1 1 0; min-width: 0; width: auto; }
  .fine-tip { position: absolute; z-index: 5; left: 50%; top: 6px; transform: translateX(-50%); padding: 3px 9px; border-radius: 9px; background: rgba(0,0,0,.65); color: #e6edf3; font: 600 11px system-ui, sans-serif; pointer-events: none; white-space: nowrap; }
  /* default power: a ruler on the outer rim of the cue track with a knob; pull the cue itself to shoot */
  .cue-rim { position: absolute; left: calc(100% + 8px); top: 0; bottom: 0; width: 16px; border-radius: 4px; touch-action: none; background: repeating-linear-gradient(to bottom, transparent 0 calc(10% - 1px), rgba(255,255,255,.28) calc(10% - 1px) 10%); border-left: 1px solid #2a323c; }
  .cue-rim .cue-mark { left: -1px; right: auto; width: 16px; height: 8px; margin-top: -4px; opacity: 1; border-radius: 4px; background: var(--pc); box-shadow: 0 0 0 1px #000; }
  .fine-tip[hidden] { display: none; }
}
@media (orientation: portrait) { .fine-tip, .cheat-quick, .rail-quick { display: none; } }
@media (orientation: landscape) {
  /* Auto mode: the five "cheat the pocket" choices live in the top bar's free space (never on the table); each icon is a pocket mouth with the aim point on it */
  .cheat-quick { flex: 0 0 auto; display: flex; gap: 3px; margin-left: 6px; }
  .cheat-quick[hidden] { display: none; }
  .cheat-quick button { width: 34px; height: 30px; padding: 0; display: grid; place-items: center; border-radius: 8px; border: 1px solid #2a323c; background: #14181d; color: #cfd8e0; }
  .cheat-quick button svg { width: 24px; height: 24px; display: block; }
  .cheat-quick button.on { background: var(--accent); color: #06140e; border-color: var(--accent); }
  .cheat-quick button.bad:not(.on) { color: #e5604c; }
  .cheat-quick button.ok:not(.on) { color: #7ee2b8; }
}

/* portrait on a phone: the game is landscape only. Show ONLY the rotate note (the table and HUD are removed, so the two can never appear together);
   a wide window that merely happens to be taller than it is wide (desktop, tablet) keeps the normal layout */
.rotate-note { display: none; }
@media (orientation: portrait) and (max-width: 760px) and (pointer: coarse) {   /* phones / tablets only: a narrow desktop window keeps the normal layout */
  html, body { background: #000; overflow: hidden; }
  main, #scrim, #faceModal, #loupe { display: none !important; }
  .rotate-note { display: grid; position: fixed; inset: 0; z-index: 100; background: #000; place-items: center; text-align: center; color: #e6edf3; font: 600 16px/1.5 system-ui, sans-serif; padding: 24px; }
  .rotate-note { overflow-y: auto; place-items: start center; }
  .ln-in { max-width: 420px; margin: auto; }
  .ln-ic { font-size: 56px; }
  .rotate-note h1 { font-size: 24px; margin: 6px 0; }
  .rotate-note p { font-weight: 400; margin: 8px 0; color: #b6c2d0; }
  .ln-start { margin: 14px 0 6px; padding: 14px 34px; font: 700 18px system-ui, sans-serif; color: #fff; background: #1f6feb; border: 0; border-radius: 999px; }
  .ln-hint { font-weight: 600 !important; color: #e6edf3 !important; }
  .ln-rot { font-size: 40px; margin: 4px 0; animation: lnrot 2.4s ease-in-out infinite; }
  @keyframes lnrot { 0%, 100% { transform: rotate(0); } 50% { transform: rotate(-90deg); } }
  .ln-links { font-size: 14px; margin-top: 18px !important; }
  .ln-links a { color: #8ab4ff; }
}

@media (orientation: landscape) and (max-width: 760px) {   /* small phones: keep the top bar on one line */
  .topbar .hud-top { gap: 2px; flex: 0 1 auto; min-width: 0; }
  .bchip { width: 18px; height: 18px; flex: 0 0 18px; }
  .cheat-quick { gap: 2px; margin-left: 3px; }
  .cheat-quick button { width: 28px; height: 26px; border-radius: 7px; }
  .cheat-quick button svg { width: 20px; height: 20px; }
}

.count-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 14px; }
.count-row .stepper { display: inline-flex; align-items: center; gap: 6px; }
.count-row .stepper button { width: 36px; height: 32px; padding: 0; font-size: 18px; line-height: 1; border-radius: 8px; }
.count-row .stepper b { min-width: 24px; text-align: center; font-size: 16px; font-variant-numeric: tabular-nums; }

@media (orientation: landscape) {
  /* ball on the cushion: offset of the cue ball in 1/32 balls (+ into the rail, - away), with an Auto find; sits next to the pocket-cheat buttons */
  .topbar .hud-top { overflow: hidden; flex: 0 1 auto; min-width: 0; }
  .rail-quick { flex: 0 0 auto; display: flex; align-items: center; gap: 2px; margin-left: 6px; padding-left: 6px; border-left: 1px solid #2a323c; }
  .rail-quick[hidden] { display: none; }
  .rail-quick button { height: 30px; min-width: 30px; padding: 0 4px; display: grid; place-items: center; border-radius: 8px; border: 1px solid #2a323c; background: #14181d; color: #cfd8e0; font: 700 14px system-ui, sans-serif; }
  .rail-quick button svg { width: 20px; height: 20px; display: block; }
  .rail-quick #rqVal { min-width: 36px; font-size: 12px; font-variant-numeric: tabular-nums; }
  .rail-quick.hit #rqVal { color: #7ee2b8; } .rail-quick.miss #rqVal { color: #e5604c; }
}
@media (orientation: landscape) and (max-width: 760px) {
  .rail-quick { margin-left: 3px; padding-left: 3px; }
  .rail-quick button { height: 26px; min-width: 26px; border-radius: 7px; }
  .rail-quick #rqVal { min-width: 30px; font-size: 11px; }
}

@media (orientation: landscape) {
  /* thick / thin hit: aim offset in degrees, next to the other quick controls */
  .aim-quick { flex: 0 0 auto; display: flex; align-items: center; gap: 2px; margin-left: 6px; padding-left: 6px; border-left: 1px solid #2a323c; }
  .aim-quick[hidden] { display: none; }
  .aim-quick button { height: 30px; min-width: 30px; padding: 0 4px; display: grid; place-items: center; border-radius: 8px; border: 1px solid #2a323c; background: #14181d; color: #cfd8e0; font: 700 14px system-ui, sans-serif; }
  .aim-quick #aqVal { min-width: 46px; font-size: 12px; font-variant-numeric: tabular-nums; }
  .aim-quick.man #aqVal { border-color: var(--accent); }
  .aim-quick.hit #aqVal { color: #7ee2b8; } .aim-quick.miss #aqVal { color: #e5604c; }
}
@media (orientation: landscape) and (max-width: 760px) {
  .aim-quick { margin-left: 3px; padding-left: 3px; }
  .aim-quick button { height: 26px; min-width: 26px; border-radius: 7px; }
  .aim-quick #aqVal { min-width: 38px; font-size: 11px; }
}
/* the menu keeps only the explanation and the mode / ball-count choice: every aim adjustment (spin sliders, pocket, offsets) lives on the screen */
.sheet details, .sheet #selAuto { display: none !important; }

@media (orientation: landscape) {
  /* the balls still on the table: a vertical column on the left; the top bar keeps only the controls */
  .stage { grid-template-columns: 30px minmax(0, 1fr) 82px; }
  .topbar { grid-column: 1 / span 2; grid-row: 1; }
  .stage > .hud-top { display: flex; flex-direction: column; grid-column: 1; grid-row: 2; align-self: stretch; justify-content: center; align-items: center; gap: 3px; overflow: hidden; padding: 0; }
  .table-wrap { grid-column: 2; grid-row: 2; }
  .rail { grid-column: 3; grid-row: 1 / span 2; }
  .stage > .hud-top .bchip { width: 22px; height: 22px; flex: 0 0 22px; }
  .rail-quick button svg { width: 22px; height: 22px; }
  .rail-quick #rqVal { min-width: 48px; }
  /* default power: the ruler sits well left of the cue (the cue stays on the right), with a dead strip between so a pull never moves it */
  .cue-track { align-self: flex-end; margin-right: 4px; }
  .cue-rim { left: auto; right: calc(100% + 16px); border-left: 0; border-right: 1px solid #2a323c; }
  .cue-rim .cue-mark { left: auto; right: -1px; }
}
@media (orientation: landscape) and (max-width: 760px) {
  .stage { grid-template-columns: 24px minmax(0, 1fr) 82px; }
  .stage > .hud-top .bchip { width: 18px; height: 18px; flex: 0 0 18px; }
  .rail-quick #rqVal { min-width: 40px; }
}

/* ball-row chips take the size of their (2x) drawing */
.stage > .hud-top .bchip { flex: 0 0 auto; }
/* (no touch-action on the HUD containers: on a real iPhone it stopped taps on the top bar and ball drags; html/body overflow:hidden + overscroll-behavior:none already stop page scroll / pull-to-refresh) */

/* phones: keep every control clear of the screen edges (Safari reveals its toolbar on an edge tap), a bigger spin ball over the cue, and the
   default-power ruler starting well BELOW the spin ball so a press meant for one never lands on the other */
@media (orientation: landscape) and (pointer: coarse) {
  .stage { padding-top: 8px; padding-bottom: 6px; }
  .rail .face-wrap { align-self: flex-end; margin: 4px 2px 0 0; }
  .rail #face { width: 56px; height: 56px; flex-basis: 56px; }
}
@media (orientation: landscape) {
}

/* only one mode for now: random balls. The preset layouts and the drill list are hidden until new drills are supplied */
[data-layout]:not([data-layout="random"]) { display: none !important; }

.cheat-quick.dim, .rail-quick.dim { opacity: .5; }   /* always shown; dimmed while a shot plays, in Manual mode or with no plan */

/* one line with the throw of the object ball (before and after the compensation), just above the top edge of the table (3 px clear of it, never on it) */
.shot-line { display: none; }
@media (orientation: landscape) {
  .table-wrap { align-self: center; margin-top: 19px; margin-bottom: 19px; width: min(100%, calc((100dvh - 94px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--ar, 1.83))); }   /* 42 px shorter than before: a 19 px margin above (the throw line) and below (the last shot line), both move with the table */
  .shot-line { display: block; position: absolute; left: 0; right: 0; bottom: calc(100% + 3px); height: 16px; font: 500 11px/16px system-ui, sans-serif; color: #aab6c2; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; pointer-events: none; }
  .shot-line .k { opacity: .62; } .shot-line b { color: #e6edf3; font-weight: 600; } .shot-line i { font-style: normal; opacity: .4; margin: 0 6px; }
  .shot-line.stale { opacity: .5; }
}
@media (orientation: landscape) and (max-width: 760px) { .shot-line { font-size: 10px; } .shot-line i { margin: 0 4px; } }.shot-line.compact { font-size: 10px; } .shot-line.compact i { margin: 0 4px; }

.shot-line.bottom { bottom: auto; top: calc(100% + 3px); }
/* Ad space: reserved inside the options panel only (never over the table); stays hidden until an ad network code is configured */
.ad-slot { min-height: 100px; margin: 10px 0 2px; text-align: center; overflow: hidden; }
.ad-slot[hidden] { display: none; }

/* top-bar helpers: full screen + help; the options-panel help box */
.tb-ico { text-decoration: none; }
.topbar .tb-ico { font-size: 15px; line-height: 1; }
.opts-btn.tb-ico { display: grid; }
.helpbox { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin: 0 0 10px; padding: 8px 10px; border: 1px solid #2b3744; border-radius: 10px; background: rgba(31, 111, 235, .12); }
.helpbox a { color: #8ab4ff; text-decoration: none; }
.helpbox .ghost { padding: 5px 10px; font-size: 12px; background: transparent; }
.helpbox .muted { flex: 1 1 100%; font-size: 12px; }
.ln-help a { color: #8ab4ff; }

/* ---- small phones in landscape (visible height <= 360 px, e.g. 640x300 with the browser bars): give the table every pixel we can ---- */
@media (orientation: landscape) and (max-height: 360px) {
  .stage { padding-top: 1px; padding-bottom: 1px; }
  .topbar { height: 36px; }
  .topbar .opts-btn { width: 32px; height: 32px; }
  .topbar .notes { height: 32px; }
  .table-wrap { margin-top: 14px; margin-bottom: 14px; width: min(100%, calc((100dvh - 78px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--ar, 1.83))); }
  .shot-line { height: 13px; font-size: 9.5px; line-height: 13px; bottom: calc(100% + 1px); }
  .shot-line.bottom { top: calc(100% + 1px); bottom: auto; }
}
/* narrow phones: full screen and help live in the options panel only (no room in the top bar) */
@media (orientation: landscape) and (max-width: 620px) { #fsBtn, #helpBtn { display: none !important; } }

/* ---- short landscape phones, 620 px or wider (e.g. 640x300 … 844x340): the table gets the FULL height; every control moves into the two side columns ---- */
@media (orientation: landscape) and (max-height: 380px) and (min-width: 620px) {
  .stage { grid-template-columns: 40px 36px minmax(0, 1fr) 82px; grid-template-rows: minmax(0, 1fr); column-gap: 4px; row-gap: 0; padding: 2px 6px; }
  .topbar { display: contents; }
  #optsBtn { grid-column: 1; grid-row: 1; align-self: start; justify-self: center; width: 36px; height: 34px; }
  .topbar .aim-seg { grid-column: 1; grid-row: 1; align-self: start; justify-self: center; margin-top: 40px; display: grid; grid-template-columns: 1fr; width: 38px; }
  .topbar .aim-seg button { padding: 7px 0; font-size: 10px; text-align: center; }
  #fsBtn, #helpBtn { display: none !important; }
  .topbar .notes { grid-column: 1; grid-row: 1; align-self: end; justify-self: center; width: 38px; height: auto; min-height: 0; max-width: none; overflow: visible; position: relative; flex: none; flex-direction: column; justify-content: flex-end; align-items: stretch; }
  .notes .live-note { display: none; }
  .stage .shotbar { position: static; flex-direction: column; align-items: stretch; padding: 0; gap: 4px; }
  .stage .shotbar #shotMsg { display: none; }
  .stage .shotbar .sb-btns { flex-direction: column; gap: 4px; }
  .stage .shotbar button { padding: 5px 0; font-size: 14px; width: 38px; }
  .stage .shotbar .tx { display: none; }
  .cheat-quick { grid-column: 2; grid-row: 1; align-self: start; flex-direction: column; margin: 0; gap: 3px; }
  .cheat-quick button { width: 34px; height: 26px; }
  .cheat-quick button svg { width: 22px; height: 22px; }
  .rail-quick { grid-column: 2; grid-row: 1; align-self: end; flex-direction: column; margin: 0; padding: 4px 0 0; border-left: 0; border-top: 1px solid #2a323c; gap: 3px; }
  .rail-quick button { width: 34px; min-width: 34px; height: 26px; padding: 0; }
  .rail-quick #rqVal { min-width: 34px; font-size: 10px; }
  .rail-quick button svg { width: 20px; height: 20px; }
  .stage > .hud-top { display: none; }
  .table-wrap { grid-column: 3; grid-row: 1; margin-top: 17px; margin-bottom: 14px; align-self: start; width: min(100%, calc((100dvh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--ar, 1.83))); }
  .rail { grid-column: 4; grid-row: 1; }
}
