*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --text: #eaeaea;
  --text-muted: #a0a0b8;
  --border: #2d2d44;
  --cell-size: 24px;
}

html,
body {
  height: 100%;
  font-family: "Segoe UI", "Hiragino Sans", "Meiryo", sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.screen {
  display: none;
  width: 100%;
  max-width: 1400px;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

.screen.active {
  display: flex;
}

.hidden {
  display: none !important;
}

/* タイトル画面 */
.title-logo {
  font-size: 2.5rem;
  letter-spacing: 0.1em;
  background: linear-gradient(135deg, var(--accent), #a855f7);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-muted);
}

.mode-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 480px;
}

.mode-section h2 {
  font-size: 1rem;
  color: var(--text-muted);
  font-weight: 500;
}

.hint {
  font-size: 0.875rem;
  color: var(--text-muted);
  text-align: center;
}

.btn-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  width: 100%;
}

.btn {
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover {
  background: #252540;
  border-color: var(--accent);
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-back {
  margin-top: 0.5rem;
  font-size: 0.875rem;
  padding: 0.5rem 1rem;
}

/* 操作方法 */
#controls-help-section {
  max-width: 520px;
}

.controls-help-scroll {
  width: 100%;
  max-height: min(55vh, 420px);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 10px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.controls-help-block,
.controls-help-player {
  margin-bottom: 1.25rem;
}

.controls-help-block h3,
.controls-help-player h3 {
  font-size: 0.95rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.controls-help-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.controls-help-table th,
.controls-help-table td {
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.controls-help-table th {
  color: var(--text-muted);
  font-weight: 500;
  width: 42%;
}

.controls-help-list {
  font-size: 0.875rem;
  color: var(--text-muted);
  padding-left: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.controls-help-note {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}

.controls-help-footer-hint {
  margin-top: 0.25rem;
}

body.is-mobile .controls-help-scroll {
  max-height: min(50vh, 360px);
}

/* ゲーム画面 */
.game-header {
  display: flex;
  gap: 2rem;
  align-items: center;
  font-size: 1.125rem;
}

.timer {
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  font-weight: 600;
}

.game-boards-area {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}

.time-up-overlay {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 10;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  font-size: clamp(2.5rem, 8vw, 4.5rem);
  font-weight: 800;
  letter-spacing: 0.15em;
  color: var(--accent);
  text-shadow:
    0 0 20px rgba(233, 69, 96, 0.8),
    0 4px 24px rgba(0, 0, 0, 0.9);
}

.time-up-overlay.active {
  display: flex;
}

.game-end-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 1.5rem;
  background: rgba(15, 15, 26, 0.88);
  cursor: pointer;
  touch-action: manipulation;
}

.game-end-overlay.active {
  display: flex;
}

.game-end-title {
  font-size: clamp(2rem, 8vw, 3.5rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-align: center;
  color: #fbbf24;
  text-shadow:
    0 0 20px rgba(251, 191, 36, 0.6),
    0 4px 24px rgba(0, 0, 0, 0.9);
}

.game-end-title.is-game-over {
  color: var(--accent);
  text-shadow:
    0 0 20px rgba(233, 69, 96, 0.8),
    0 4px 24px rgba(0, 0, 0, 0.9);
}

.game-end-prompt {
  font-size: clamp(0.875rem, 3vw, 1.125rem);
  letter-spacing: 0.2em;
  color: var(--text-muted);
  text-align: center;
  animation: end-prompt-blink 1.2s ease-in-out infinite;
}

@keyframes end-prompt-blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.35;
  }
}

.game-boards {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
  align-items: flex-start;
}

#screen-game.players-4 {
  max-width: none;
  width: 100%;
}

#screen-game.players-4 .game-boards {
  flex-wrap: nowrap;
  justify-content: center;
  width: 100%;
}

#screen-game.players-4 .player-panel {
  flex-shrink: 0;
  padding: 0.5rem;
}

#screen-game.players-4 .player-label,
#screen-game.players-4 .player-stats {
  font-size: 0.75rem;
}

#screen-game.players-4 .controls-hint {
  max-width: none;
}

.player-panel {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.player-panel.game-over {
  opacity: 0.5;
}

.player-panel.winner {
  border-color: #fbbf24;
  box-shadow: 0 0 12px rgba(251, 191, 36, 0.3);
}

.player-label {
  font-weight: 600;
  font-size: 0.875rem;
}

.player-stats {
  display: flex;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.player-canvas-wrap {
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}

.canvas-main {
  display: block;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #0a0a12;
}

.canvas-next {
  display: block;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #0a0a12;
}

.next-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-align: center;
}

.controls-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  max-width: 600px;
}

/* ソフトキーボード（スマホ） */
.touch-controls {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 0 0.75rem;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.touch-controls-row {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

.touch-btn {
  flex: 1;
  min-height: 3.25rem;
  min-width: 3.25rem;
  max-width: 8rem;
  font-size: 1.25rem;
  font-weight: 600;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.touch-btn-wide {
  flex: 1;
  max-width: none;
  font-size: 1rem;
}

.touch-btn:active {
  background: #252540;
  border-color: var(--accent);
}

body.is-mobile #app {
  padding: 0.5rem;
  justify-content: flex-start;
}

body.is-mobile #screen-game.active {
  min-height: 100dvh;
  max-height: 100dvh;
  height: 100dvh;
  justify-content: flex-start;
  gap: 0.35rem;
  touch-action: none;
  overscroll-behavior: none;
  overflow: hidden;
}

body.is-mobile .game-header {
  flex-shrink: 0;
  font-size: 0.95rem;
  gap: 1rem;
}

body.is-mobile .game-boards-area {
  flex: 1;
  min-height: 0;
  align-items: center;
  overflow: hidden;
}

body.is-mobile .touch-controls {
  flex-shrink: 0;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
}

body.is-mobile .player-panel {
  padding: 0.5rem;
}

body.is-mobile .title-logo {
  font-size: 1.75rem;
}

/* 結果画面 */
#screen-result h2 {
  font-size: 1.75rem;
}

.result-list {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.result-card {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.25rem 1rem;
  align-items: center;
}

.result-card.rank-1 {
  border-color: #fbbf24;
}

.result-rank {
  grid-row: span 3;
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
  min-width: 3rem;
  text-align: center;
}

.result-card.rank-1 .result-rank {
  color: #fbbf24;
}

.result-name {
  font-weight: 600;
  font-size: 1.1rem;
}

.result-detail {
  font-size: 0.875rem;
  color: var(--text-muted);
}
