.main-layout {
  display: flex;
  gap: var(--space-lg);
  align-items: flex-start;
  justify-content: center;
  flex-wrap: wrap;
  width: 100%;
  max-width: 900px;
}

.board {
  background: var(--bg-card);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  padding: var(--space-lg);
  box-shadow: var(--shadow-board);
}

.board-title {
  font-size: var(--text-xs);
  letter-spacing: 0.15em;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: var(--space-md);
  text-transform: uppercase;
  font-weight: 600;
}

.rows-container {
  display: flex;
  flex-direction: column-reverse;
  gap: var(--space-sm);
  align-items: center;
}

.guess-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: all var(--transition-base);
  position: relative;
  width: fit-content;
}

.guess-row.active {
  border-color: var(--border-default);
  background: var(--bg-subtle);
}

.row-num {
  font-size: var(--text-xs);
  color: var(--text-muted);
  width: 16px;
  text-align: center;
  flex-shrink: 0;
  font-weight: 600;
}

.pegs {
  display: flex;
  gap: var(--space-sm);
}

.feedback {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-xs);
  width: 24px;
  flex-shrink: 0;
}

.fb-dot {
  width: 10px;
  height: 10px;
  border-radius: var(--radius-full);
  background: transparent;
  border: 1px solid var(--border-subtle);
  transition: all var(--transition-base);
}

.fb-dot.exact {
  background: var(--color-correct);
  border-color: var(--color-correct);
  animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
}

.fb-dot.misplaced {
  background: var(--color-misplaced);
  border-color: var(--color-misplaced);
  animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: scale(0.5);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  min-width: 240px;
}

@media (min-width: 960px) {
  .main-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 360px;
    gap: 24px;
    align-items: start;
    max-width: 1180px;
  }

  .board {
    width: 100%;
    min-width: 0;
  }

  .side-panel {
    width: 360px;
    min-width: 360px;
  }
}

.palette {
  --palette-ball-size: var(--ball-md);
  --palette-row-width: 196px;
  display: flex;
  flex-wrap: wrap;
  width: var(--palette-row-width);
  max-width: 100%;
  gap: 12px;
  justify-content: center;
  margin-inline: auto;
  margin-top: var(--space-sm);
}

.palette .ball {
  --ball-size: var(--palette-ball-size);
}

.palette[data-color-count="6"] {
  --palette-row-width: 144px;
}

@media (max-width: 640px) {
  .palette {
    --palette-ball-size: 36px;
    --palette-row-width: 180px;
  }

  .palette[data-color-count="6"] {
    --palette-row-width: 132px;
  }
}

.secret-row {
  display: flex;
  gap: var(--space-md);
  justify-content: center;
  margin: var(--space-md) 0;
}

.secret-row.reveal-win .ball {
  animation: waveBounce 0.6s ease-in-out;
}

@keyframes waveBounce {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-10px);
  }
}

.selected-count {
  font-size: var(--text-xs);
  color: var(--text-muted);
  text-align: center;
  margin-top: var(--space-md);
}

/* ---- Ball ---- */
.ball {
  border-radius: var(--radius-full);
  flex-shrink: 0;
  position: relative;
  transition: transform var(--transition-fast), opacity var(--transition-fast), box-shadow var(--transition-fast);
  border: none;
  touch-action: manipulation;
  width: var(--ball-size, var(--ball-md));
  height: var(--ball-size, var(--ball-md));
  background: var(--ball-bg);
}

.ball:not(.ball--empty) {
  background: radial-gradient(
    circle at 32% 32%,
    rgba(255, 255, 255, 0.55) 0%,
    rgba(255, 255, 255, 0.1) 20%,
    transparent 70%,
    rgba(0, 0, 0, 0.45) 100%
  ), var(--ball-bg);
  box-shadow: 
    inset 3px 3px 5px rgba(255, 255, 255, 0.55),
    inset -3px -4px 7px rgba(0, 0, 0, 0.45),
    0 4px 10px rgba(0, 0, 0, 0.35);
}

.ball--empty {
  background: rgba(0, 0, 0, 0.25);
  border: 2px dashed rgba(255, 255, 255, 0.08);
  box-shadow: 
    inset 1px 1px 3px rgba(0, 0, 0, 0.5),
    1px 1px 1px rgba(255, 255, 255, 0.05);
  cursor: default;
}

.ball--used {
  opacity: 0.2;
  cursor: not-allowed;
  pointer-events: none;
}

/* Cursor/Focus ring with breathing glow pulse (drawn exactly on the border edge) */
.ball--focused {
  outline: none;
  border: 2px solid var(--accent-primary) !important;
}

.ball:not(.ball--empty).ball--focused {
  box-shadow: 
    inset 3px 3px 5px rgba(255, 255, 255, 0.35),
    inset -3px -4px 7px rgba(0, 0, 0, 0.4),
    0 4px 10px rgba(0, 0, 0, 0.35),
    0 0 12px rgba(255, 255, 255, 0.45);
  animation: glowPulseColored 1.6s infinite ease-in-out;
}

.ball--empty.ball--focused {
  box-shadow: 
    inset 1px 1px 3px rgba(0, 0, 0, 0.5),
    1px 1px 1px rgba(255, 255, 255, 0.05),
    0 0 12px rgba(255, 255, 255, 0.45);
  animation: glowPulseEmpty 1.6s infinite ease-in-out;
}

@keyframes glowPulseColored {
  0%, 100% {
    box-shadow: 
      inset 3px 3px 5px rgba(255, 255, 255, 0.35),
      inset -3px -4px 7px rgba(0, 0, 0, 0.4),
      0 4px 10px rgba(0, 0, 0, 0.35),
      0 0 6px rgba(255, 255, 255, 0.25);
  }
  50% {
    box-shadow: 
      inset 3px 3px 5px rgba(255, 255, 255, 0.35),
      inset -3px -4px 7px rgba(0, 0, 0, 0.4),
      0 4px 10px rgba(0, 0, 0, 0.35),
      0 0 18px rgba(255, 255, 255, 0.7);
  }
}

@keyframes glowPulseEmpty {
  0%, 100% {
    box-shadow: 
      inset 1px 1px 3px rgba(0, 0, 0, 0.5),
      1px 1px 1px rgba(255, 255, 255, 0.05),
      0 0 6px rgba(255, 255, 255, 0.25);
  }
  50% {
    box-shadow: 
      inset 1px 1px 3px rgba(0, 0, 0, 0.5),
      1px 1px 1px rgba(255, 255, 255, 0.05),
      0 0 18px rgba(255, 255, 255, 0.75);
  }
}

@media (hover: hover) {
  .ball:not(.ball--empty):not(.ball--used):hover {
    transform: scale(1.1);
  }
}

.ball:not(.ball--empty):not(.ball--used):active {
  transform: scale(0.95);
}

.active-arrow {
  position: absolute;
  left: -8px;
  font-size: var(--text-xs);
  color: var(--text-primary);
  pointer-events: none;
  opacity: 0.8;
}

/* ---- Responsive ---- */
@media (max-width: 640px) {
  .main-layout {
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    min-height: calc(100vh - 200px);
  }

  /* 非猜测阶段隐藏 board */
  .board {
    display: none;
    width: 100%;
    max-width: 480px;
    order: 2;
    padding: 14px 10px 12px;
  }

  .side-panel {
    width: 100%;
    max-width: 480px;
    min-width: unset;
    order: 1;
    gap: 14px;
    margin: auto 0;
  }

  .board-title {
    margin-bottom: 10px;
  }

  .rows-container {
    padding-top: 0;
  }

  .ball {
    --ball-size: var(--ball-md);
  }

  .pegs .ball {
    --ball-size: 32px;
  }

  .guess-row {
    width: 100%;
    justify-content: space-between;
    padding: 8px 16px;
  }
}

/* ---- 猜测阶段：固定视口分屏布局 ---- */
@media (max-width: 640px) {
  body.game-phase--guessing {
    min-height: 100svh;
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    overflow: hidden;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  body.game-phase--guessing .main-layout {
    width: 100%;
    max-width: 480px;
    flex: 1;
    min-height: 0;
    gap: 8px;
  }

  /* 历史记录区：占满剩余高度，内部可滚动 */
  body.game-phase--guessing .board {
    order: 1;
    flex: 1;
    width: 100%;
    min-height: 0;
    height: auto;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-default);
    box-shadow: var(--shadow-card);
    padding: 8px 8px 6px;
    display: flex;
    flex-direction: column;
  }

  body.game-phase--guessing .board-title {
    margin-bottom: 6px;
  }

  body.game-phase--guessing .rows-container {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    gap: 6px;
  }

  body.game-phase--guessing .guess-row {
    padding: 6px 12px;
  }

  body.game-phase--guessing .row-num {
    width: 14px;
  }

  body.game-phase--guessing .pegs {
    gap: 6px;
  }

  body.game-phase--guessing .pegs .ball {
    --ball-size: 28px;
  }

  body.game-phase--guessing .feedback {
    width: 20px;
    gap: 3px;
  }

  body.game-phase--guessing .fb-dot {
    width: 8px;
    height: 8px;
  }

  /* 操作面板：固定在底部，不压缩 */
  body.game-phase--guessing .side-panel {
    order: 2;
    flex-shrink: 0;
    width: 100%;
    gap: 8px;
    padding: 0;
    background: transparent;
  }

  /* 操作面板内的 card 内边距压缩 */
  body.game-phase--guessing .side-panel .card {
    padding: 10px 12px;
  }

  body.game-phase--guessing #screenGuess {
    gap: 8px;
  }

  body.game-phase--guessing .card-title {
    margin-bottom: 10px;
  }

  body.game-phase--guessing .secret-row {
    gap: 10px;
    margin: 10px 0;
  }

  body.game-phase--guessing .palette {
    --palette-ball-size: 32px;
    --palette-row-width: 164px;
    gap: 10px;
    margin-top: 6px;
  }

  body.game-phase--guessing .palette[data-color-count="6"] {
    --palette-row-width: 116px;
  }

  /* 状态提示压缩 */
  body.game-phase--guessing .status-box {
    min-height: unset;
    padding: 8px 10px;
    font-size: var(--text-xs);
    line-height: 1.4;
  }

  body.game-phase--guessing .btn-row {
    gap: 6px;
  }

  body.game-phase--guessing .btn {
    min-height: 40px;
    padding: 10px 12px;
  }

  body.game-phase--guessing .status-row {
    gap: 8px;
  }

  body.game-phase--guessing .help-link {
    padding-top: 8px;
    font-size: 11px;
  }

  body.game-phase--guessing .legend-sheet {
    padding: 14px;
  }
}

@media (max-width: 640px) and (max-height: 820px) {
  body.game-phase--guessing .main-layout {
    gap: 6px;
  }

  body.game-phase--guessing .board {
    padding-top: 6px;
  }

  body.game-phase--guessing .side-panel {
    gap: 6px;
    padding: 0;
  }

  body.game-phase--guessing .side-panel .card {
    padding: 8px 10px;
  }

  body.game-phase--guessing .palette {
    --palette-ball-size: 30px;
    --palette-row-width: 156px;
    gap: 8px;
  }

  body.game-phase--guessing .palette[data-color-count="6"] {
    --palette-row-width: 106px;
  }

  body.game-phase--guessing .pegs .ball {
    --ball-size: 26px;
  }

  body.game-phase--guessing .btn {
    min-height: 38px;
    padding: 9px 10px;
  }
}

@media (max-width: 360px) {
  .palette {
    --palette-ball-size: 32px;
    --palette-row-width: 164px;
  }

  .palette[data-color-count="6"] {
    --palette-row-width: 116px;
  }

  .pegs .ball {
    --ball-size: 28px;
  }

  .guess-row {
    padding: 6px 12px;
  }
}