:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--app-text);
  background: var(--app-background);
  --app-text: #292e28;
  --app-background: #eee9dc;
  --accent: #72521c;
  --line: #b3ac99;
  --surface: #faf6ec;
  --muted: #5d6254;
  --hover: #e8dfca;
  --focus: #806020;
  --on-accent: #fffaf0;
  --accent-hover: #5b4016;
  --error-bg: #f5dfd4;
  --error-text: #852e20;
}
:root[data-theme=dark], .desktop-game {
  color-scheme: dark;
  --app-background: #1b211e;
  --app-text: #efe9db;
  --accent: #dfbe7e;
  --line: #6d7260;
  --surface: #262e28;
  --muted: #bfc5b1;
  --hover: #374033;
  --focus: #efd197;
  --on-accent: #25291f;
  --accent-hover: #eed3a0;
  --error-bg: #482a23;
  --error-text: #ffd3bd;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
main { display: flex; flex-direction: column; gap: 8px; max-width: 1200px;
  height: var(--app-height, 100dvh); margin: auto; min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
header { display: flex; align-items: center; gap: 8px; flex: none; min-width: 0; }
h1 { font-size: 1.1rem; margin: 0; letter-spacing: -.025em; white-space: nowrap; }
h2 { font-size: 1.2rem; line-height: 1.3; margin: 0; text-wrap: balance; }
h3 { font-size: 1rem; margin: 8px 0; }
p { line-height: 1.4; margin: 0; overflow-wrap: anywhere; }
#status { color: var(--muted); font-size: .75rem; margin-left: auto; text-align: right; }
#error { flex: none; background: var(--error-bg); color: var(--error-text); padding: 8px; border-radius: 6px; }
#menu, #lobby { width: 100%; max-width: 560px; margin: auto; }
#menu { display: grid; gap: 12px; }
label { display: block; font-weight: 600; margin: 8px 0 4px; }
button, input, select { font: inherit; min-height: 44px; border-radius: 6px; min-width: 0; }
button, select, input { border: 1px solid var(--line); padding: 8px 10px; }
input, select { width: 100%; background: var(--surface); color: inherit; }
button { cursor: pointer; background: var(--surface); color: var(--accent); font-weight: 600;
  margin: 0; transition: background-color 140ms ease-out; }
button:hover { background: var(--hover); }
button.primary { color: var(--on-accent); border-color: var(--accent); background: var(--accent); }
button.primary:hover { background: var(--accent-hover); }
button:disabled { opacity: .5; cursor: not-allowed; }
button:focus-visible, input:focus-visible, select:focus-visible, canvas:focus-visible {
  outline: 3px solid var(--focus); outline-offset: 3px;
}
#solo, #lobby > button { width: 100%; margin-top: 8px; }
#joinForm { margin-top: 12px; padding-top: 4px; border-top: 1px solid var(--line); }
.actions { display: flex; gap: 8px; align-items: center; }
.actions > * { flex: 1; min-width: 0; }
.quiet { background: transparent; border: 0; text-decoration: underline; color: var(--muted); }
ol { padding-left: 20px; margin: 8px 0; }
li { line-height: 1.35; padding: 4px 0; overflow-wrap: anywhere; }
#lobby { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
#lobbyPlayers li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#game { flex: 1; display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.game-body { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; }
.game-info { flex: none; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.players { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
  list-style: none; padding: 0; margin: 0; }
.players .player-card { min-width: 0; padding: 8px; font-size: .8rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  display: flex; flex-direction: column; gap: 4px; }
.player-heading { display: flex; gap: 8px; align-items: baseline; min-width: 0; }
.player-name { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-details { display: block; color: var(--muted); font-size: .7rem; }
.player-tag { margin-left: auto; font-size: .65rem; color: var(--muted); white-space: nowrap; }
.players .active { border-color: var(--accent); }
.players .active .player-name, .players .active .player-tag { font-weight: 750; color: var(--accent); }
.ball-rack { display: flex; flex-wrap: wrap; gap: 4px; padding: 3px 0; }
.ball-badge { width: 22px; height: 22px; flex: none; display: grid; place-items: center;
  border-radius: 50%; background: var(--ball-color); border: 1px solid #00000040;
  box-shadow: inset 0 -2px 2px #00000030; }
.ball-badge.striped { background: linear-gradient(#fffdf5 22%, var(--ball-color) 22% 78%, #fffdf5 78%); }
.ball-badge > span { display: grid; place-items: center; width: 14px; height: 14px;
  border-radius: 50%; background: #fffdf5; color: #14202b; font-size: 10px;
  font-weight: 750; line-height: 1; }
.target-ball { outline: 2px solid var(--accent); outline-offset: 2px; }
#objective { background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 12px; }
.objective-title { font-size: .8rem; font-weight: 700; margin-bottom: 4px; }
.shared-rack { gap: 6px; }
.turn-info { display: flex; flex-direction: column; gap: 4px; }
.match-heading { display: flex; align-items: center; gap: 8px; flex: none; }
#openMatchMenu { display: grid; place-items: center; width: 44px; height: 44px;
  padding: 0; flex: none; }
#timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: .8rem;
  margin-left: auto; white-space: nowrap; }
#turnStatus { font-size: .85rem; font-weight: 600; }
#aimHint, #lastShot { font-size: .8rem; }
#lastShot:empty { display: none; }
.table-stage { flex: 1; min-height: 0; min-width: 0; display: flex; align-items: center;
  justify-content: center; position: relative; }
.table-wrap { position: relative; flex: none; background: #263f38; padding: 6px; border-radius: 10px; }
#gesturePower { position: absolute; top: 18px; left: 50%; transform: translateX(-50%);
  padding: 8px 12px; background: var(--surface); color: var(--app-text); border-radius: 6px;
  border: 1px solid var(--line); width: min(220px, calc(100% - 24px));
  font-size: .75rem; font-weight: 700; pointer-events: none;
  box-shadow: 0 4px 12px #00000030; }
#powerMeter { height: 10px; margin-top: 6px; background: var(--app-background);
  border: 1px solid var(--muted); border-radius: 3px; overflow: hidden; }
#powerFill { display: block; height: 100%; width: 0; background: var(--accent); }
canvas { display: block; touch-action: none; user-select: none;
  background: #17664f; border-radius: 6px; }
.table-wrap.is-3d { padding: 0; background: transparent; border-radius: 0; }
#table3d { background: transparent; border-radius: 0; cursor: crosshair; }
#table3d.rotating { cursor: grabbing; }
#table3d.cue-view { cursor: default; }
#table3d.cue-view.aiming { cursor: none; }
#table3d.placing { cursor: move; }
#placementArrows { position: absolute; width: 88px; height: 88px;
  transform: translate(-50%, -50%); pointer-events: none; color: #f4d79d;
  filter: drop-shadow(0 2px 2px #101813); }
#placementArrows svg { display: block; width: 100%; height: 100%; }
.flat-game #placementArrows { width: 64px; height: 64px; }
#cameraHint { position: absolute; bottom: 6px; left: 0; right: 0;
  text-align: center; color: var(--muted); font-size: .75rem; pointer-events: none; }
#matchMenu { width: min(360px, calc(100% - 32px)); max-height: calc(100dvh - 32px);
  overflow-y: auto; padding: 24px; color: var(--app-text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 12px 40px #00000060; }
#matchMenu::backdrop { background: #101813b3; }
#matchMenuHint { margin-top: 8px; color: var(--muted); font-size: .85rem; }
.match-menu-actions { display: grid; gap: 10px; margin-top: 24px; }
.sensitivity-setting label { display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; margin: 6px 0 0; font-size: .85rem; }
.sensitivity-setting output { font-variant-numeric: tabular-nums; white-space: nowrap; }
.sensitivity-setting input { display: block; padding: 0; accent-color: var(--accent); }
.sensitivity-setting p, #settingsWarning { color: var(--muted); font-size: .75rem; }
.timer-label::after { content: ": "; white-space: pre; }
@media (max-width: 480px) {
  .timer-label { display: none; }
}
#result { position: absolute; inset: 0; display: flex; flex-direction: column;
  justify-content: center; align-items: center; gap: 10px; padding: 12px;
  text-align: center; background: var(--app-background, #f2f5f8); }
#result p { max-width: 38ch; }
.finished .game-info { display: none; }
.desktop-game { color: var(--app-text); background: var(--app-background); }
.desktop-game main { max-width: none; padding: 0; gap: 0; }
.desktop-game .table-stage {
  position: fixed; inset: 0; height: var(--app-height, 100dvh); z-index: 0;
}
.desktop-game header, .desktop-game .match-heading {
  position: fixed; z-index: 2; padding: 12px 16px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 6px 24px #00000040;
}
.desktop-game header {
  top: max(16px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left));
  width: min(360px, 40vw); flex-wrap: wrap;
}
.desktop-game #status { text-align: left; margin-left: 0; }
.desktop-game .match-heading {
  top: max(16px, env(safe-area-inset-top)); right: max(16px, env(safe-area-inset-right));
  gap: 12px;
}
.desktop-game .game-info {
  position: fixed; z-index: 2;
  bottom: max(56px, env(safe-area-inset-bottom)); left: max(16px, env(safe-area-inset-left));
  width: 300px; gap: 12px; max-height: calc(var(--app-height, 100dvh) - 180px);
  overflow-y: auto;
}
.desktop-game .players { grid-template-columns: 1fr; }
.desktop-game .player-card { padding: 12px 16px; }
.desktop-game .player-card.active { background: #343c2e; }
.desktop-game .turn-info { background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 12px 16px; }
.desktop-game .player-name { font-weight: 650; }
.desktop-game .ball-badge { width: 26px; height: 26px; }
.desktop-game .ball-badge > span { width: 16px; height: 16px; font-size: 11px; }
.desktop-game .shared-rack .ball-badge { width: 24px; height: 24px; }
.desktop-game #cameraHint { bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%; right: auto; transform: translateX(-50%); width: max-content;
  max-width: calc(100% - 32px); padding: 6px 12px; background: var(--surface); border-radius: 6px; }
.desktop-game #gesturePower { top: 112px; }
.desktop-game #error {
  position: fixed; z-index: 4; top: 112px; right: 16px; max-width: min(420px, 45vw);
}
.desktop-game #retry { position: fixed; z-index: 4; top: 180px; right: 16px; }
.desktop-game #result {
  inset: auto; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(420px, calc(100% - 32px)); padding: 24px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 32px #00000026; z-index: 3;
}
@media (min-width: 700px), (orientation: landscape) {
  .game-body { flex-direction: row-reverse; }
  .game-info { flex: 0 0 clamp(180px, 24vw, 260px); }
  .players { grid-template-columns: 1fr; }
}
@media (max-height: 600px) {
  main { gap: 4px; }
  #menu { gap: 6px; }
  #menu h2 { font-size: 1.1rem; }
}
@media (max-width: 480px) {
  .player-heading { gap: 4px; flex-wrap: wrap; }
  .player-tag { margin-left: 0; }
  .players .player-card { padding: 6px; gap: 2px; }
  .ball-rack { gap: 3px; }
  .ball-badge { width: 18px; height: 18px; }
  .ball-badge > span { width: 12px; height: 12px; font-size: 9px; }
}
@media (orientation: landscape) and (max-height: 600px) {
  #menu { max-width: 780px; grid-template-columns: 1fr 1fr; gap: 8px 24px; }
  #menu h2 { grid-column: 1 / -1; }
  #menu h3 { margin: 0; }
  #joinForm { margin-top: 8px; }
  #lobby { max-width: 780px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #lobbyPlayers { grid-column: 2; grid-row: 1 / span 4; margin: 0; }
  #exitLobby { grid-column: 1; }
  .game-info { gap: 4px; }
  .players { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; }
  .player-details { font-size: .65rem; }
}
@media (prefers-reduced-motion: reduce) {
  button { transition: none; }
}
.flat-game main { padding: max(6px, env(safe-area-inset-top))
  max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom))
  max(8px, env(safe-area-inset-left)); gap: 6px; }
.flat-game .match-heading { gap: 10px; }
.flat-game #modeTitle { font-size: 1rem; }
.flat-game #timer { font-size: .85rem; }
.flat-game .game-body { flex-direction: column; gap: 8px; }
.flat-game .game-info { display: contents; }
.flat-game .players { order: 0; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.flat-game .player-card { padding: 6px 10px; gap: 3px; }
.flat-game .player-heading { flex-wrap: nowrap; align-items: center; gap: 6px; }
.flat-game .player-name { font-size: .8rem; }
.flat-game .player-tag { margin-left: auto; font-size: .65rem; }
.flat-game .player-details { font-size: .7rem; }
.flat-game .ball-rack { gap: 4px; padding: 2px 0; }
.flat-game .ball-badge { width: 20px; height: 20px; }
.flat-game .ball-badge > span { width: 13px; height: 13px; font-size: 10px; }
.flat-game #objective { order: 1; display: flex; align-items: center; flex-wrap: wrap;
  gap: 6px 12px; padding: 6px 10px; }
.flat-game .objective-title { margin: 0; font-size: .75rem; }
.flat-game .table-stage { order: 2; min-height: 0; }
.flat-game .table-wrap { padding: 6px; background: #172a21; box-shadow: 0 5px 14px #00000030; }
.flat-game #table { background: #172a21; }
.flat-game .turn-info { order: 3; padding: 8px 10px; background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; gap: 3px; flex: none; }
.flat-game #turnStatus { color: var(--accent); font-size: .85rem; }
.flat-game #aimHint, .flat-game #lastShot { font-size: .75rem; }
.flat-game #gesturePower { top: auto; bottom: 16px; width: 150px; padding: 6px 10px; }
.flat-game #powerMeter { margin-top: 4px; height: 8px; }
.flat-game.finished .game-info { display: none; }
@media (orientation: landscape) {
  .flat-game .game-body { display: grid; grid-template-columns: minmax(0, 1fr) 190px;
    grid-template-rows: auto auto minmax(0, 1fr); column-gap: 10px; }
  .flat-game .table-stage { grid-column: 1; grid-row: 1 / -1; }
  .flat-game .players { grid-column: 2; grid-row: 1; grid-template-columns: 1fr; }
  .flat-game #objective { grid-column: 2; grid-row: 2; }
  .flat-game .turn-info { grid-column: 2; grid-row: 3; align-self: start;
    max-height: 100%; overflow-y: auto; }
}
@media (max-height: 430px) and (orientation: landscape) {
  .flat-game main { gap: 2px; }
  .flat-game .game-body { gap: 4px 8px; grid-template-columns: minmax(0, 1fr) 180px; }
  .flat-game .player-card { padding: 4px 8px; }
  .flat-game .players { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px; }
  .flat-game .player-tag { display: none; }
  .flat-game #objective { padding: 4px 8px; }
}
