/* Game controls live in normal document flow so the table keeps its own space. */
#panel.controls-dock {
  --dock-bg: #14221c;
  --dock-line: #354a3d;
  --dock-text: #f5f1e5;
  --dock-muted: #a7b7aa;
  --dock-gold: #d9b777;

  display: grid;
  grid-template:
    "spin power action tools" auto / 108px minmax(0, 1fr)
    140px auto;
  align-items: center;
  gap: 20px;
  width: calc(100% - 32px);
  max-width: 1160px;
  height: auto;
  min-height: 88px;
  flex: 0 0 auto;
  margin: 0 auto max(12px, env(safe-area-inset-bottom));
  padding: 12px 16px;
  border: 1px solid var(--dock-line);
  border-radius: 18px;
  background: linear-gradient(145deg, #1c2d23, var(--dock-bg));
  color: var(--dock-text);
  box-shadow:
    0 12px 38px rgb(0 0 0 / 28%),
    inset 0 1px 0 #ffffff05;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  z-index: 30;
  overflow: visible;
}

#panel.controls-dock * {
  box-sizing: border-box;
}

#panel.controls-dock [hidden] {
  display: none !important;
}

#panel.controls-dock :where(button, summary) {
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#panel.controls-dock :is(button, summary, input):focus-visible {
  outline: 2px solid var(--dock-gold);
  outline-offset: 4px;
}

#panel.controls-dock button:disabled {
  cursor: not-allowed;
}

#panel .spin-dock {
  grid-area: spin;
  position: relative;
  min-width: 0;
}

#panel .spin-toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  min-height: 60px;
  padding: 4px 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--dock-text);
  text-align: left;
}

#panel .spin-toggle:disabled {
  opacity: 0.48;
}

#panel .spin-preview {
  position: relative;
  display: block;
  flex: 0 0 45px;
  width: 45px;
  height: 45px;
  border-radius: 8px;
  overflow: hidden;
  isolation: isolate;
}

#panel .spin-preview-ball {
  position: absolute;
  inset: 5%;
  z-index: 1;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 25%, #fffdf1, #e4decc 58%, #a3ad9c);
  box-shadow:
    0 5px 10px #0004,
    inset -3px -4px 7px #0002;
}

#panel .spin-preview-object {
  position: absolute;
  top: 6%;
  left: 5%;
  width: 88%;
  height: 88%;
  border: 0.5px solid #002c40;
  border-radius: 50%;
  visibility: hidden;
}

#panel .spin-preview-tip {
  position: absolute;
  left: var(--spin-x, 50%);
  top: var(--spin-y, 50%);
  z-index: 2;
  width: 8px;
  height: 8px;
  border: 1px solid #fbf0d0;
  border-radius: 50%;
  background: #a67e3b;
  box-shadow: 0 1px 3px #0005;
  transform: translate(-50%, -50%);
}

#panel .spin-label {
  display: flex;
  flex: 1 1 0;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
  font-size: 12px;
  line-height: 1.2;
  color: var(--dock-text);
  white-space: nowrap;
}

#panel .spin-label :is(small, .spin-value) {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--dock-muted);
  font-size: 10px;
  font-weight: 400;
}

#panel .spin-popover {
  touch-action: pan-y;

  --ball-size-base: 180px;
  --ball-size-small: 180px;

  position: absolute;
  bottom: calc(100% + 18px);
  left: 0;
  width: min(430px, calc(100vw - 48px));
  max-height: calc(100vh - 132px);
  max-height: calc(100dvh - 132px);
  padding: 16px;
  border: 1px solid #52634d;
  border-radius: 17px;
  background: linear-gradient(145deg, #213329, #13221a);
  color: var(--dock-text);
  box-shadow: 0 18px 60px #0007;
  overflow: auto;
  overscroll-behavior: contain;
  z-index: 3001;
}

#panel .spin-popover-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: #213329;
  touch-action: pan-y;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 36px;
  gap: 12px;
  margin-bottom: 8px;
}

#panel .spin-popover-header :is(h2, h3, strong) {
  margin: 0;
  color: var(--dock-text);
  font-size: 14px;
  font-weight: 550;
}

#panel .spin-close,
#panel .spin-reset {
  min-height: 44px;
  min-width: 44px;
  padding: 8px 12px;
  border: 1px solid var(--dock-line);
  border-radius: 8px;
  background: #203227;
  color: var(--dock-text);
  font-size: 12px;
}

#panel .spin-close {
  padding: 0;
  font-size: 21px;
}

#panel .spin-controls {
  touch-action: pan-y;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 12px;
}

#panel
  .spin-controls
  :is(.ballContainerWrapper, .ballContainerWrapper.is-disabled) {
  flex: 0 0 var(--ball-size-base);
  width: var(--ball-size-base);
  height: var(--ball-size-base);
  z-index: auto;
  transition: none;
}

#panel .spin-controls :is(.ballContainer, .ballContainer.is-disabled) {
  position: relative;
  inset: auto;
  width: var(--ball-size-base);
  height: var(--ball-size-base);
  border: 0;
  border-radius: 16px;
  background: radial-gradient(circle, #314d3c 0, #1d3024 68%);
  box-shadow: none;
  overflow: hidden;
  transition: none;
}

#panel .spin-controls .cueBall {
  box-shadow:
    0 5px 17px #0004,
    inset -8px -8px 20px #0003;
  cursor: crosshair;
  touch-action: none;
}

#panel .spin-controls .cueBall::after {
  content: "";
  position: absolute;
  inset: 13%;
  border: 1px solid #69786b55;
  border-radius: 50%;
  background:
    linear-gradient(
      transparent calc(50% - 0.5px),
      #69786b55 calc(50% - 0.5px),
      #69786b55 calc(50% + 0.5px),
      transparent calc(50% + 0.5px)
    ),
    linear-gradient(
      90deg,
      transparent calc(50% - 0.5px),
      #69786b55 calc(50% - 0.5px),
      #69786b55 calc(50% + 0.5px),
      transparent calc(50% + 0.5px)
    );
  pointer-events: none;
}

#panel .spin-controls .cueTip {
  width: 15px;
  height: 15px;
  border: 2px solid #fff4d4;
  background: #b18b46;
  box-shadow: 0 1px 6px #0006;
  transition: none;
}

#panel .spin-popover .openElevation {
  position: static;
  width: auto;
  min-width: 44px;
  min-height: 44px;
  height: 44px;
  padding: 0 9px;
  border: 1px solid #4d624c;
  border-radius: 8px;
  background: #223c2c;
  color: #ead2a2;
  font-size: 11px;
}

#panel .spin-controls .tiltSliderContainer {
  flex: 1 1 145px;
  width: 160px;
  max-width: 180px;
  height: 180px;
  margin: 0;
  border: 1px solid #3e5441;
  border-radius: 13px;
  background: #13251b;
  transform: none;
  transition: opacity 0.15s;
}

#panel .spin-hint {
  margin: 12px 0;
  color: var(--dock-muted);
  font-size: 11px;
  line-height: 1.7;
  text-align: center;
}

#panel .spin-settings {
  touch-action: pan-y;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}

#panel .spin-reset {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  min-width: 110px;
}

#panel #spinElevation {
  margin-left: 8px;
  color: var(--dock-gold);
  font-variant-numeric: tabular-nums;
}

#panel .power-control {
  grid-area: power;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: stretch;
  gap: 0;
  width: auto;
  max-width: none;
  min-width: 0;
  height: auto;
  min-height: 60px;
  margin: 0;
  padding: 0;
  align-self: center;
  border: 0;
  border-radius: 0;
  background: none;
  overflow: visible;
}

#panel .power-heading {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  color: var(--dock-muted);
  font-size: 11px;
  line-height: 1;
}

#panel .power-heading label {
  width: auto;
  margin: 0;
  color: var(--dock-muted);
  font-size: 11px;
}

#panel .powerPercent {
  position: static;
  color: #ead2a2;
  text-shadow: none;
  font:
    550 13px / 1 "Segoe UI",
    sans-serif;
  font-variant-numeric: tabular-nums;
}

#panel .power-track {
  display: none;
}

#panel input.powerSlider {
  flex: 0 0 44px;
  min-height: 44px;
  width: 100%;
  height: 44px;
  margin: 0;
  padding: 0;
  background: transparent;
  border: 0;
  overflow: visible;
  touch-action: pan-y;
}

#panel input.powerSlider::-webkit-slider-runnable-track {
  width: 100%;
  height: 6px;
  border-radius: 4px;
  background: linear-gradient(
    to right,
    #d9b777 0%,
    #d9b777 var(--p),
    #3a5040 var(--p),
    #3a5040 100%
  );
  box-shadow: inset 0 1px 2px #0004;
}

#panel input.powerSlider::-moz-range-track {
  width: 100%;
  height: 6px;
  border-radius: 4px;
  background: linear-gradient(
    to right,
    #d9b777 0%,
    #d9b777 var(--p),
    #3a5040 var(--p),
    #3a5040 100%
  );
  box-shadow: inset 0 1px 2px #0004;
}

#panel input.powerSlider::-webkit-slider-thumb {
  appearance: none;
  width: 22px;
  height: 22px;
  margin-top: -8px;
  border: 3px solid #f5e6c0;
  border-radius: 50%;
  background: #c6a365;
  box-shadow: 0 2px 7px #0005;
  cursor: grab;
}

#panel input.powerSlider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: 3px solid #f5e6c0;
  border-radius: 50%;
  background: #c6a365;
  box-shadow: 0 2px 7px #0005;
  cursor: grab;
}

#panel .power-control.is-disabled {
  opacity: 0.45;
}

#panel .aimingArea {
  grid-area: action;
  width: auto;
  min-width: 0;
  align-self: stretch;
  justify-content: center;
}

#panel #cueHit {
  width: 100%;
  height: 48px;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid #e5c891;
  border-radius: 11px;
  background: linear-gradient(120deg, #ebcf97, #d2ac69);
  color: #253125;
  box-shadow:
    0 3px 9px #0003,
    inset 0 1px 0 #fff5;
  font-size: 14px;
  font-weight: 650;
  letter-spacing: 0.5px;
  overflow: hidden;
  visibility: visible;
}

#panel #cueHit::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: var(--progress, 0%);
  height: 3px;
  background: var(--timer-color, #95cba3);
  pointer-events: none;
}

#panel #cueHit:disabled {
  border-color: #455648;
  background: #293b2e;
  color: #a0b29f;
  box-shadow: none;
  opacity: 1;
}

#panel .outerMenu {
  grid-area: tools;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  width: auto;
  height: auto;
  margin: 0;
  padding: 0;
  border-radius: 0;
  z-index: 2000;
}

#panel .menuDropdown {
  width: 44px;
  flex: 0 0 44px;
}

#panel .menuButton {
  position: relative;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  margin: 0;
  border: 1px solid #3e5545;
  border-radius: 10px;
  background: #21362a;
  color: #dce5d8;
  font-size: 17px;
  box-shadow: inset 0 1px 0 #ffffff05;
  transition:
    background 0.15s,
    border-color 0.15s;
}

#panel .menuButton svg {
  width: 19px;
  height: 19px;
  stroke: currentcolor;
  pointer-events: none;
}

#panel .menuDropdown[open] > summary,
#panel .spin-toggle[aria-expanded="true"] {
  background: #2e4634;
  border-color: #8c845d;
}

#panel .menuItems {
  touch-action: pan-y;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: stretch;
  gap: 6px;
  right: 0;
  bottom: calc(100% + 18px);
  width: min(310px, calc(100vw - 40px));
  max-height: calc(100vh - 132px);
  max-height: calc(100dvh - 132px);
  padding: 10px;
  border: 1px solid #4d604c;
  border-radius: 14px;
  background: #172a1f;
  box-shadow: 0 15px 45px #0007;
  overflow: auto;
  overscroll-behavior: contain;
}

#panel .menuDropdown:not([open]) .menuItems {
  display: none;
}

#panel .menuItems .menuButton {
  display: flex;
  justify-content: flex-start;
  gap: 9px;
  width: 100%;
  min-width: 0;
  padding: 10px;
  border-color: #314a37;
  border-radius: 8px;
  font-size: 14px;
}

#panel .menuItems .tool-label {
  color: #dce5d8;
  font-size: 11px;
  white-space: nowrap;
}

#panel .menuDropdown > summary .tool-label,
#panel .outerMenu > .menuButton .tool-label {
  display: none;
}

#panel .menuItems #concede {
  color: #e1ada0;
}

body.analysis-mode #panel.controls-dock {
  width: calc(100% - 20px);
  grid-template-columns: 90px minmax(0, 1fr) 110px;
  grid-template-areas: "spin power action";
  gap: 12px;
  margin: 0 10px 10px;
  padding: 10px;
  background: var(--dock-bg);
}

body.analysis-mode #panel.controls-dock .outerMenu {
  display: none;
}

body.chromeless #panel.controls-dock {
  height: 0;
  min-height: 0;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
  overflow: visible;
  visibility: hidden;
  pointer-events: none;
}

body.chromeless
  #panel.controls-dock
  :is(.spin-dock, .aimingArea, .power-control) {
  display: none;
}

body.chromeless #panel.controls-dock .outerMenu {
  position: fixed;
  right: max(10px, env(safe-area-inset-right));
  bottom: max(10px, env(safe-area-inset-bottom));
  display: flex;
  visibility: visible;
  pointer-events: auto;
}

@media (hover: hover) and (pointer: fine) {
  #panel .spin-toggle:not(:disabled):hover {
    background: #ffffff07;
  }

  #panel .menuButton:not(:disabled):hover,
  #panel .spin-close:hover,
  #panel .spin-reset:hover {
    background: #314c38;
    border-color: #7a8b65;
  }

  #panel #cueHit:not(:disabled):hover {
    background: linear-gradient(120deg, #f3ddae, #dec087);
  }
}

@media (width <= 760px) {
  #panel.controls-dock {
    grid-template-columns: 94px minmax(0, 1fr) 118px auto;
    gap: 12px;
    width: calc(100% - 24px);
    padding: 10px 12px;
  }
}

@media (width <= 600px) {
  #panel.controls-dock {
    grid-template:
      "spin power tools" auto "action action action" auto / 95px minmax(
        72px,
        1fr
      )
      auto;
    gap: 8px 12px;
    width: calc(100% - 20px);
    min-height: 132px;
    margin-bottom: max(8px, env(safe-area-inset-bottom));
    padding: 10px 12px;
    border-radius: 15px;
  }

  #panel .spin-toggle {
    min-height: 50px;
  }

  #panel .power-control {
    min-height: 50px;
  }

  #panel .power-heading {
    padding-top: 2px;
    gap: 4px;
    white-space: nowrap;
  }

  #panel .power-heading label {
    font-size: 10px;
  }

  #panel .outerMenu {
    justify-self: end;
  }

  #panel #cueHit {
    min-height: 46px;
    height: 46px;
  }

  #panel .spin-popover {
    width: min(410px, calc(100vw - 44px));
    bottom: calc(100% + 16px);
    max-height: calc(100vh - 188px);
    max-height: calc(100dvh - 188px);
    padding: 12px;
  }

  #panel .menuItems {
    bottom: calc(100% + 12px);
    max-height: calc(100vh - 172px);
    max-height: calc(100dvh - 172px);
  }

  body.analysis-mode #panel.controls-dock {
    grid-template: "spin power" auto "action action" auto / 90px minmax(0, 1fr);
    min-height: 132px;
  }
}

@media (width <= 350px) {
  #panel.controls-dock {
    grid-template-columns: 86px minmax(72px, 1fr) auto;
    gap: 8px;
    width: calc(100% - 16px);
    padding: 8px;
  }

  #panel .spin-toggle {
    gap: 7px;
  }

  #panel .spin-preview {
    flex-basis: 39px;
    width: 39px;
    height: 39px;
  }

  #panel .spin-label {
    font-size: 11px;
  }
}

@media (width <= 380px) and (orientation: portrait) {
  body.peer-chat-enabled #panel.controls-dock {
    grid-template: "spin tools" auto "power power" auto "action action" auto / minmax(
        0,
        1fr
      ) auto;
  }
}

/* Short landscape displays keep a single 64px control row. */
@media (orientation: landscape) and (height <= 500px) {
  #panel.controls-dock {
    grid-template:
      "spin power action tools" auto / 84px minmax(80px, 1fr)
      108px auto;
    gap: 10px;
    min-height: 64px;
    width: calc(100% - 20px);
    margin-bottom: max(5px, env(safe-area-inset-bottom));
    padding: 7px 10px;
    border-radius: 12px;
  }

  #panel .spin-toggle,
  #panel .power-control {
    min-height: 46px;
  }

  #panel .spin-toggle {
    gap: 7px;
  }

  #panel .spin-preview {
    flex-basis: 35px;
    width: 35px;
    height: 35px;
  }

  #panel .spin-label {
    font-size: 11px;
  }

  #panel .spin-label small {
    display: none;
  }

  #panel .power-heading {
    font-size: 10px;
  }

  #panel input.powerSlider {
    min-height: 44px;
    height: 44px;
    flex-basis: 44px;
  }

  #panel #cueHit {
    height: 44px;
  }

  #panel .spin-popover {
    --ball-size-base: 140px;
    --ball-size-small: 140px;

    width: min(395px, calc(100vw - 40px));
    max-height: calc(100vh - 92px);
    max-height: calc(100dvh - 92px);
    bottom: calc(100% + 8px);
    padding: 10px 12px;
  }

  #panel .spin-popover-header {
    margin-bottom: 4px;
  }

  #panel .spin-controls .tiltSliderContainer {
    height: 140px;
    max-width: 160px;
  }

  #panel .menuItems {
    bottom: calc(100% + 10px);
    max-height: calc(100vh - 92px);
    max-height: calc(100dvh - 92px);
  }

  body.analysis-mode #panel.controls-dock {
    grid-template: "spin power action" auto / 76px minmax(0, 1fr) 90px;
    min-height: 64px;
    padding: 7px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #panel.controls-dock * {
    transition: none !important;
  }
}
