:root {
  color-scheme: dark;
  --page: #101513;
  --surface: #18201d;
  --surface-2: #202a26;
  --line: #33403a;
  --text: #f4f7f5;
  --muted: #9aa9a2;
  --green: #36c985;
  --green-dark: #117c54;
  --amber: #f4c95d;
  --red: #ef6b68;
  --blue: #4ea1f3;
  --focus: #8adbb6;
  --radius: 8px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
  letter-spacing: 0;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  background: var(--page);
  color: var(--text);
}

html {
  scrollbar-gutter: stable;
}

body {
  min-height: 100dvh;
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.hidden {
  display: none !important;
}

.app-shell {
  min-height: 100dvh;
}

.topbar {
  position: sticky;
  z-index: 30;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 58px;
  padding: 0 clamp(16px, 3vw, 38px);
  border-bottom: 1px solid var(--line);
  background: rgb(16 21 19 / 94%);
  backdrop-filter: blur(12px);
}

.brand {
  display: inline-flex;
  gap: 10px;
  align-items: center;
  color: var(--text);
  font-size: 16px;
  font-weight: 760;
  text-decoration: none;
}

.brand-mark {
  position: relative;
  display: inline-flex;
  align-items: flex-end;
  justify-content: center;
  width: 25px;
  height: 25px;
  padding-bottom: 3px;
}

.brand-mark span {
  width: 9px;
  height: 9px;
  margin: 0 -1px;
  border: 2px solid #101513;
  border-radius: 50%;
  background: var(--amber);
}

.brand-mark span:nth-child(2) {
  background: var(--green);
  transform: translateY(-7px);
}

.brand-mark span:nth-child(3) {
  background: var(--red);
}

.topbar-actions,
.toolbar-commands {
  display: flex;
  gap: 8px;
  align-items: center;
}

.connection-badge {
  display: inline-flex;
  gap: 7px;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: var(--surface);
  font-size: 12px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--red);
}

.connection-badge[data-state="online"] .status-dot {
  background: var(--green);
  box-shadow: 0 0 0 4px rgb(54 201 133 / 12%);
}

.connection-badge[data-state="connecting"] .status-dot {
  background: var(--amber);
}

.icon-button,
.command-button,
.primary-button,
.secondary-button,
.shoot-button,
.room-code-button,
.segment {
  border: 0;
  cursor: pointer;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}

.icon-button:hover,
.command-button:hover,
.room-code-button:hover {
  background: var(--surface-2);
}

.icon-button.danger:hover {
  border-color: rgb(239 107 104 / 65%);
  color: var(--red);
}

.icon-button svg,
.command-button svg,
.primary-button svg,
.secondary-button svg,
.shoot-button svg,
.room-code-button svg {
  width: 17px;
  height: 17px;
  stroke-width: 2;
}

.lobby-view {
  position: relative;
  display: grid;
  place-items: center;
  min-height: calc(100dvh - 58px);
  padding: 40px 20px 80px;
  overflow: hidden;
}

.lobby-panel {
  position: relative;
  z-index: 2;
  width: min(100%, 430px);
  padding: 34px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 28px 60px rgb(0 0 0 / 30%);
}

.lobby-title {
  margin-bottom: 26px;
}

.eyebrow,
.toolbar-label {
  display: block;
  margin-bottom: 7px;
  color: var(--green);
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}

.lobby-title h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1.12;
}

.lobby-title p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--page);
}

.segment {
  min-height: 38px;
  border-radius: 4px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  font-weight: 650;
}

.segment.active {
  color: var(--text);
  background: var(--surface-2);
}

#roomForm {
  display: grid;
  gap: 16px;
  margin-top: 22px;
}

.field {
  display: grid;
  gap: 7px;
}

.field > span,
.power-control > span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.field input,
.chat-form input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: var(--page);
}

.field input {
  height: 46px;
  padding: 0 13px;
}

.field input::placeholder,
.chat-form input::placeholder {
  color: #68766f;
}

.primary-button,
.secondary-button,
.command-button,
.shoot-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 42px;
  border-radius: 6px;
  font-weight: 760;
}

.primary-button {
  color: #07130e;
  background: var(--green);
}

.primary-button:hover {
  background: #55d99a;
}

.primary-button.compact {
  min-height: 38px;
  padding: 0 16px;
}

.secondary-button,
.command-button {
  border: 1px solid var(--line);
  color: var(--text);
  background: var(--surface);
}

.secondary-button {
  padding: 0 15px;
}

.command-button {
  height: 36px;
  padding: 0 12px;
  font-size: 13px;
}

.lobby-orbit {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.lobby-ball {
  position: absolute;
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  border: 5px solid rgb(255 255 255 / 88%);
  border-radius: 50%;
  color: white;
  background: #151a18;
  box-shadow: 0 18px 28px rgb(0 0 0 / 32%);
  font-size: 15px;
  font-weight: 850;
}

.ball-eight {
  top: 18%;
  left: 12%;
}

.ball-three {
  right: 13%;
  bottom: 18%;
  background: #d84743;
}

.ball-eleven {
  top: 26%;
  right: 10%;
  color: #d84743;
  background: white;
}

.game-view {
  padding-bottom: 30px;
}

.room-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 70px;
  padding: 10px clamp(14px, 2.6vw, 34px);
  border-bottom: 1px solid var(--line);
}

.room-code-button {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  padding: 0;
  color: var(--text);
  background: transparent;
  font-size: 19px;
  font-weight: 850;
}

.room-code-button svg {
  color: var(--muted);
}

.match-strip {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) minmax(150px, 220px) minmax(160px, 1fr);
  align-items: center;
  gap: 20px;
  padding: 14px clamp(14px, 2.6vw, 34px);
}

.player-item {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.player-item.align-right {
  justify-content: flex-end;
  text-align: right;
}

.seat-number {
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--muted);
  background: var(--surface);
  font-size: 12px;
  font-weight: 800;
}

.player-copy {
  display: grid;
  min-width: 0;
}

.player-copy strong {
  overflow: hidden;
  font-size: 14px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-copy span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.turn-light {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #4a554f;
}

.player-item.active .turn-light {
  background: var(--green);
  box-shadow: 0 0 0 5px rgb(54 201 133 / 13%);
}

.match-status {
  display: grid;
  gap: 3px;
  text-align: center;
}

.match-status span {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
}

.match-status strong {
  font-size: 13px;
}

.play-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.table-area {
  min-width: 0;
  padding: clamp(12px, 2.4vw, 30px);
  background: #0d1210;
}

.table-stage {
  position: relative;
  width: 100%;
  max-width: 1180px;
  margin: 0 auto;
  aspect-ratio: 1000 / 520;
  overflow: hidden;
  border-radius: 5px;
  box-shadow: 0 24px 45px rgb(0 0 0 / 34%);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.stage-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
  align-items: center;
  justify-content: center;
  background: rgb(8 14 11 / 76%);
  backdrop-filter: blur(3px);
}

.stage-overlay strong {
  font-size: 18px;
}

.waiting-pulse {
  width: 14px;
  height: 14px;
  border: 3px solid rgb(54 201 133 / 30%);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

.winner-overlay {
  background: rgb(8 14 11 / 86%);
}

.trophy {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  color: #11140f;
  background: var(--amber);
}

.trophy svg {
  width: 27px;
  height: 27px;
}

.shot-controls {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) minmax(220px, 1.5fr) 122px;
  gap: 24px;
  align-items: center;
  max-width: 1180px;
  min-height: 74px;
  margin: 12px auto 0;
  padding: 10px 14px;
  border-top: 1px solid var(--line);
}

.aim-readout {
  display: flex;
  gap: 10px;
  align-items: center;
  min-width: 0;
}

.cue-dot {
  width: 22px;
  height: 22px;
  border: 5px solid white;
  border-radius: 50%;
  background: #dfe5df;
  box-shadow: 0 4px 10px rgb(0 0 0 / 25%);
}

.aim-readout > div {
  display: grid;
  min-width: 0;
}

.aim-readout span,
.power-control span {
  color: var(--muted);
  font-size: 11px;
}

.aim-readout strong {
  overflow: hidden;
  margin-top: 2px;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.power-control {
  display: grid;
  grid-template-columns: auto minmax(90px, 1fr) 35px;
  gap: 10px;
  align-items: center;
}

.power-control input {
  width: 100%;
  accent-color: var(--green);
}

.power-control output {
  color: var(--amber);
  font-size: 13px;
  font-weight: 800;
  text-align: right;
}

.shoot-button {
  min-height: 44px;
  color: #07130e;
  background: var(--green);
}

.shoot-button:disabled {
  cursor: not-allowed;
  color: #718078;
  background: #26322d;
}

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(220px, 1fr) auto;
  min-height: 540px;
  border-left: 1px solid var(--line);
  background: var(--surface);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 66px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}

.chat-header strong {
  font-size: 14px;
}

.spectator-count {
  color: var(--muted);
  font-size: 11px;
}

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  max-height: 590px;
  padding: 14px 16px;
  overflow-y: auto;
}

.system-message {
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}

.chat-message {
  display: grid;
  gap: 3px;
}

.chat-message .chat-meta {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 10px;
}

.chat-message p {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 13px;
  line-height: 1.45;
}

.chat-message[data-seat="0"] .chat-meta strong {
  color: var(--blue);
}

.chat-message[data-seat="1"] .chat-meta strong {
  color: var(--red);
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr 38px;
  gap: 8px;
  padding: 12px;
  border-top: 1px solid var(--line);
}

.chat-form input {
  min-width: 0;
  height: 38px;
  padding: 0 10px;
}

.send-button {
  width: 38px;
  height: 38px;
  color: #07130e;
  background: var(--green);
}

.toast-region {
  position: fixed;
  z-index: 100;
  right: 18px;
  bottom: 18px;
  display: grid;
  gap: 8px;
  width: min(340px, calc(100vw - 36px));
}

.toast {
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--green);
  border-radius: 6px;
  background: #1b2420;
  box-shadow: 0 16px 28px rgb(0 0 0 / 30%);
  font-size: 13px;
}

.toast.error {
  border-left-color: var(--red);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 980px) {
  .play-layout {
    grid-template-columns: 1fr;
  }

  .chat-panel {
    min-height: 330px;
    border-top: 1px solid var(--line);
    border-left: 0;
  }

  .chat-messages {
    max-height: 260px;
  }
}

@media (max-width: 700px) {
  .topbar {
    height: 54px;
    padding: 0 12px;
  }

  .connection-badge {
    padding: 0 8px;
  }

  #connectionText {
    display: none;
  }

  .lobby-view {
    min-height: calc(100dvh - 54px);
    padding: 24px 14px 64px;
  }

  .lobby-panel {
    padding: 24px 20px;
  }

  .lobby-ball {
    width: 44px;
    height: 44px;
    border-width: 4px;
    font-size: 12px;
  }

  .ball-eight {
    top: 8%;
    left: 6%;
  }

  .ball-three {
    right: 6%;
    bottom: 6%;
  }

  .ball-eleven {
    top: 9%;
    right: 7%;
  }

  .room-toolbar {
    min-height: 62px;
    padding: 8px 12px;
  }

  .command-button span {
    display: none;
  }

  .command-button {
    width: 36px;
    padding: 0;
  }

  .match-strip {
    grid-template-columns: 1fr auto 1fr;
    gap: 8px;
    padding: 10px 12px;
  }

  .seat-number,
  .turn-light {
    display: none;
  }

  .player-copy strong {
    font-size: 12px;
  }

  .player-copy span,
  .match-status span {
    font-size: 9px;
  }

  .match-status strong {
    max-width: 100px;
    font-size: 11px;
  }

  .table-area {
    padding: 8px 0 10px;
  }

  .table-stage {
    border-radius: 0;
  }

  .shot-controls {
    grid-template-columns: 1fr 100px;
    gap: 10px;
    min-height: 104px;
    margin-top: 8px;
    padding: 8px 12px;
  }

  .power-control {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .aim-readout {
    grid-column: 1;
    grid-row: 2;
  }

  .shoot-button {
    grid-column: 2;
    grid-row: 2;
  }

  .stage-overlay strong {
    font-size: 14px;
  }

  .chat-panel {
    min-height: 300px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
