:root {
  --bg-1: #040611;
  --bg-2: #0e1635;
  --text: #f3f7ff;
  --muted: #c4d3ff;
  --accent: #7dd3fc;
}

#starfield {
  position: fixed;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  color: var(--text);
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
  background:
    radial-gradient(circle at 12% 10%, #263d87 0%, transparent 28%),
    radial-gradient(circle at 84% 18%, #4e1e70 0%, transparent 24%),
    linear-gradient(160deg, var(--bg-1), var(--bg-2));
  /* ドラッグ操作がテキスト選択に取られないように（モーダル内は下で解除） */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  overflow: hidden;
  touch-action: none;
}

/* モーダル表示中はテキスト入力・ソフトキーボード用（body の touch-action:none を一時解除） */
body.billiards-modal-open {
  touch-action: manipulation;
}

/* 背面 UI：クリックは透過、ボックス内のみ反応 */
.game-ui {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}

/* キャンバス（z-index 10）より手前。game-ui の外に置く */
.nav-back {
  pointer-events: auto;
  position: fixed;
  top: 10px;
  right: 10px;
  z-index: 35;
  margin: 0;
  padding: 10px 14px;
  min-height: 44px;
  min-width: 44px;
  border-radius: 999px;
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  border: 1px solid rgba(180, 205, 255, 0.35);
  background: rgba(10, 18, 40, 0.75);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.nav-back:hover,
.nav-back:focus-visible {
  background: rgba(18, 28, 52, 0.88);
  outline: none;
}

.back-confirm {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.back-confirm.hidden {
  display: none;
}

.back-confirm__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 8, 20, 0.72);
  backdrop-filter: blur(4px);
}

.back-confirm__box {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 340px;
  padding: 20px 18px;
  border-radius: 16px;
  border: 1px solid rgba(180, 205, 255, 0.28);
  background: rgba(10, 18, 40, 0.94);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
  -webkit-user-select: text;
  user-select: text;
}

.back-confirm__title {
  margin: 0 0 8px;
  font-size: clamp(1.05rem, 3vw, 1.2rem);
  font-weight: 700;
}

.back-confirm__lead {
  margin: 0 0 18px;
  font-size: 0.88rem;
  color: var(--muted);
  line-height: 1.5;
}

.back-confirm__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.back-confirm__btn {
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 0.92rem;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid transparent;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.back-confirm__btn--primary {
  border-color: rgba(120, 210, 255, 0.55);
  background: linear-gradient(180deg, #2a4fc0, #223688);
  color: #eef7ff;
}

.back-confirm__btn--ghost {
  border-color: rgba(180, 205, 255, 0.35);
  background: rgba(15, 25, 55, 0.85);
  color: var(--text);
}

.float-box {
  pointer-events: auto;
  -webkit-user-select: none;
  user-select: none;
  position: absolute;
  left: 10px;
  right: 10px;
  max-width: min(560px, calc(100% - 20px));
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid rgba(180, 205, 255, 0.28);
  background: rgba(10, 18, 40, 0.62);
  backdrop-filter: blur(8px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
}

.float-box--top {
  top: 10px;
}

.float-box__head h1 {
  margin: 0 0 6px;
  font-size: clamp(1.05rem, 3.2vw, 1.4rem);
}

.float-box__lead {
  margin: 0;
  color: var(--muted);
  font-size: clamp(0.76rem, 2.1vw, 0.9rem);
  line-height: 1.5;
}

.float-box__stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  align-items: baseline;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(180, 205, 255, 0.18);
}

.stat {
  font-size: clamp(0.8rem, 2.1vw, 0.92rem);
  color: var(--muted);
}

.stat strong {
  color: var(--accent);
  font-size: 1.08rem;
}

.stat--best strong {
  color: #86efac;
}

.float-box--bottom {
  bottom: 10px;
  margin-left: auto;
  margin-right: auto;
  left: 10px;
  right: 10px;
  font-size: clamp(0.74rem, 2vw, 0.86rem);
  color: var(--muted);
  line-height: 1.55;
}

.float-box--bottom p {
  margin: 0 0 8px;
}

.float-box--bottom p:last-child {
  margin-bottom: 0;
}

.float-box__nav {
  margin-top: 6px;
}

.float-box__nav a {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.float-box__nav a:hover {
  text-decoration: underline;
}

/* 全面プレイレイヤー（球は手前＝上の z-index） */
.game-stage {
  -webkit-user-select: none;
  user-select: none;
  position: fixed;
  inset: 0;
  z-index: 10;
  margin: 0;
  border: none;
  border-radius: 0;
  overflow: hidden;
  background: transparent;
  box-shadow: none;
}

#table {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  vertical-align: top;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.overlay-msg {
  -webkit-user-select: text;
  user-select: text;
  touch-action: manipulation;
  pointer-events: auto;
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(4, 8, 20, 0.78);
  backdrop-filter: blur(4px);
  text-align: center;
  padding: 20px;
  font-size: clamp(0.95rem, 2.8vw, 1.1rem);
  line-height: 1.65;
}

/* 親の .game-stage { user-select:none } より強く、入力可能にする */
.overlay-msg input,
.overlay-msg textarea {
  -webkit-user-select: text;
  user-select: text;
  touch-action: manipulation;
}

.overlay-msg.hidden {
  display: none;
}

.overlay-msg--gameover {
  background: rgba(28, 8, 12, 0.88);
  border: 1px solid rgba(248, 113, 113, 0.35);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.overlay-msg--gameover strong {
  color: #fecaca;
  font-size: 1.15em;
}

.overlay-msg--win {
  border: 1px solid rgba(110, 231, 183, 0.3);
}

.overlay-msg--pick {
  border: 1px solid rgba(180, 205, 255, 0.22);
}

.diff-picker {
  max-width: 360px;
  margin: 0 auto;
}

.diff-picker__title {
  margin: 0 0 16px;
  font-size: clamp(1rem, 3vw, 1.2rem);
  font-weight: 700;
}

.diff-picker__grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.diff-picker__btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: 100%;
  min-height: 52px;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid rgba(120, 210, 255, 0.45);
  background: linear-gradient(180deg, rgba(35, 55, 120, 0.85), rgba(20, 32, 72, 0.92));
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(255, 255, 255, 0.12);
}

.diff-picker__btn span {
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--muted);
}

.diff-picker__btn:hover,
.diff-picker__btn:focus-visible {
  border-color: rgba(180, 230, 255, 0.75);
  background: linear-gradient(180deg, rgba(45, 70, 150, 0.9), rgba(28, 42, 95, 0.95));
  outline: none;
}

.diff-picker__btn:active {
  transform: scale(0.98);
}

.clear-modal {
  max-width: 340px;
  margin: 0 auto;
}

.clear-modal__title {
  margin: 0 0 10px;
  font-size: clamp(1.35rem, 4vw, 1.75rem);
  font-weight: 800;
  color: #a7f3d0;
  text-shadow: 0 2px 16px rgba(52, 211, 153, 0.35);
}

.clear-modal__badge {
  margin: 0 0 8px;
  font-size: 0.85rem;
  font-weight: 700;
  color: #fde68a;
}

.clear-modal__meta {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.92rem;
}

.clear-modal__time {
  margin: 0 0 16px;
  font-size: clamp(1.1rem, 3.2vw, 1.35rem);
  color: var(--text);
}

.clear-modal__time strong {
  color: var(--accent);
  font-size: 1.15em;
}

.clear-modal__hint {
  margin: 0;
  font-size: 0.88rem;
  color: var(--muted);
}

.clear-modal__rank {
  margin: 0 0 14px;
  text-align: left;
}

.clear-modal__label {
  display: block;
  margin: 0 0 6px;
  font-size: 0.82rem;
  color: var(--muted);
}

.clear-modal__input {
  width: 100%;
  box-sizing: border-box;
  margin-bottom: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(180, 205, 255, 0.35);
  background: rgba(5, 10, 24, 0.65);
  color: var(--text);
  font-size: 1rem;
  -webkit-user-select: text;
  user-select: text;
  touch-action: manipulation;
}

.clear-modal__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  border: 1px solid transparent;
  touch-action: manipulation;
}

.clear-modal__btn--primary {
  border-color: rgba(120, 210, 255, 0.55);
  background: linear-gradient(180deg, #2a4fc0, #223688);
  color: #eef7ff;
}

.clear-modal__btn--secondary {
  width: 100%;
  margin-bottom: 8px;
  border-color: rgba(180, 205, 255, 0.35);
  background: rgba(15, 25, 55, 0.85);
  color: var(--text);
}

.clear-modal__btn--ghost {
  border-color: rgba(180, 205, 255, 0.28);
  background: rgba(8, 14, 32, 0.5);
  color: var(--accent);
}

.clear-modal__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 4px;
}

.clear-modal__rank-note {
  margin: 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
}

.clear-modal__rank-note--ok {
  color: #86efac;
}

.diff-picker__rank-btn {
  display: block;
  width: 100%;
  margin-top: 14px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid rgba(250, 204, 21, 0.35);
  background: rgba(55, 45, 10, 0.55);
  color: #fde68a;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.diff-picker__rank-btn:hover {
  background: rgba(70, 58, 18, 0.75);
}

.go-modal {
  max-width: 300px;
  margin: 0 auto;
  text-align: center;
}

.go-modal__sub {
  margin: 8px 0 16px;
  color: var(--muted);
}

.overlay-msg--ranking {
  align-items: flex-start;
  overflow-y: auto;
  padding: 16px 0 24px;
}

.rank-overlay {
  max-width: min(420px, 100vw - 32px);
  margin: 0 auto;
  padding: 0 16px 8px;
  text-align: center;
}

.rank-loading {
  margin: 0;
  padding: 28px 16px;
  color: var(--muted);
}

.rank-overlay__title {
  margin: 0 0 6px;
  font-size: clamp(1.1rem, 3.2vw, 1.35rem);
}

.rank-overlay__note {
  margin: 0 0 14px;
  font-size: 0.75rem;
  color: var(--muted);
}

.rank-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 12px;
}

.rank-tab {
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid rgba(180, 205, 255, 0.3);
  background: rgba(8, 14, 32, 0.55);
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
  touch-action: manipulation;
}

.rank-tab--active {
  border-color: rgba(120, 210, 255, 0.65);
  color: var(--text);
  background: rgba(35, 55, 120, 0.75);
}

.rank-table-wrap {
  overflow-x: auto;
  margin-bottom: 14px;
  border-radius: 12px;
  border: 1px solid rgba(180, 205, 255, 0.2);
  background: rgba(5, 10, 24, 0.45);
}

.rank-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.rank-table th,
.rank-table td {
  padding: 8px 6px;
  text-align: left;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.rank-table th {
  color: var(--muted);
  font-weight: 600;
}

.rank-table .rank-col {
  width: 2rem;
  color: var(--muted);
}

.rank-table .time-col {
  white-space: nowrap;
  color: var(--accent);
  font-weight: 700;
}

.rank-table .date-col {
  font-size: 0.72rem;
  color: var(--muted);
}

.rank-row--empty td {
  opacity: 0.45;
}

.rank-overlay__close {
  padding: 10px 28px;
  border-radius: 12px;
  border: 1px solid rgba(180, 205, 255, 0.35);
  background: rgba(15, 25, 55, 0.85);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.overlay-msg small {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.88em;
}

@media (max-width: 520px) {
  .float-box {
    left: 8px;
    right: 8px;
    padding: 9px 12px;
  }

  .float-box__stats {
    gap: 8px 14px;
  }
}
