:root {
  --color-o: #e53e3e;
  --color-x: #3182ce;
  --color-bg: #f7fafc;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #2d3748;
  --color-muted: #718096;
  --color-subtle: #edf2f7;
  --color-win-bg: #f0fff4;
  --color-win-border: #68d391;
  --shadow-sm: 0 1px 4px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.12);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --cell-size: min(28vw, 100px);
}

[data-theme="dark"] {
  --color-bg: #1a202c;
  --color-surface: #2d3748;
  --color-border: #4a5568;
  --color-text: #f7fafc;
  --color-muted: #a0aec0;
  --color-subtle: #374151;
  --color-win-bg: #1c3a2a;
  --color-win-border: #48bb78;
  --shadow-sm: 0 1px 4px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: background-color 0.25s, color 0.25s;
}

/* ── Header ── */
header {
  width: 100%;
  max-width: 480px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px 12px;
}

header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.back-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--color-border);
  background: var(--color-surface);
  font-size: 1rem;
  cursor: pointer;
  color: var(--color-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.back-btn:hover {
  background: var(--color-subtle);
  border-color: var(--color-muted);
  color: var(--color-text);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.help-btn,
.theme-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid var(--color-border);
  background: var(--color-surface);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--color-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.help-btn:hover,
.theme-btn:hover {
  background: var(--color-subtle);
  border-color: var(--color-muted);
  color: var(--color-text);
}

.theme-btn {
  font-size: 1.1rem;
  line-height: 1;
}

/* ── Main ── */
main {
  width: 100%;
  max-width: 480px;
  padding: 8px 20px 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

/* ── Mode Tabs ── */
.mode-tabs {
  display: flex;
  background: var(--color-subtle);
  border-radius: 10px;
  padding: 4px;
  gap: 4px;
  width: 100%;
}

.tab {
  flex: 1;
  padding: 10px 0;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  background: transparent;
  color: var(--color-muted);
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
}

.tab.active {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-sm);
}

/* ── Board ── */
.board {
  display: grid;
  grid-template-columns: repeat(3, var(--cell-size));
  grid-template-rows: repeat(3, var(--cell-size));
  gap: 8px;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--color-surface);
  border: 2px solid var(--color-border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background 0.15s, transform 0.12s, opacity 0.3s, border-color 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.cell:not(:disabled):hover {
  background: var(--color-subtle);
  transform: scale(1.04);
}

.cell:not(:disabled):active {
  transform: scale(0.97);
}

.cell svg {
  width: 56%;
  height: 56%;
  display: block;
}

.cell[data-player="O"] {
  color: var(--color-o);
}

.cell[data-player="X"] {
  color: var(--color-x);
}

.cell:disabled {
  cursor: default;
}

.cell.winning {
  background: var(--color-win-bg);
  border-color: var(--color-win-border);
}

.cell.fading {
  opacity: 0.3;
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.25; }
  50%       { opacity: 0.55; }
}

/* ── Status ── */
.status {
  font-size: 1.125rem;
  font-weight: 600;
  text-align: center;
  min-height: 1.8em;
  letter-spacing: 0.01em;
}

.status.win-o    { color: var(--color-o); }
.status.win-x    { color: var(--color-x); }
.status.draw     { color: var(--color-muted); }

/* ── Reset Button ── */
.reset-btn {
  padding: 13px 44px;
  border: none;
  border-radius: var(--radius-md);
  background: var(--color-text);
  color: var(--color-bg);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  letter-spacing: 0.02em;
  transition: opacity 0.15s, transform 0.12s;
  -webkit-tap-highlight-color: transparent;
}

.reset-btn:hover  { opacity: 0.82; }
.reset-btn:active { transform: scale(0.97); }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: auto;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 28px 24px 24px;
  width: 100%;
  max-width: 400px;
  max-height: 85dvh;
  overflow-y: auto;
  position: relative;
  transform: translateY(14px);
  transition: transform 0.2s;
  box-shadow: var(--shadow-md);
}

.modal-overlay.open .modal {
  transform: translateY(0);
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--color-subtle);
  font-size: 0.8125rem;
  cursor: pointer;
  color: var(--color-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

.modal-close:hover {
  background: var(--color-border);
  color: var(--color-text);
}

.modal h2 {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 20px;
  padding-right: 36px;
}

.modal section {
  margin-bottom: 20px;
}

.modal section:last-child {
  margin-bottom: 0;
}

.modal h3 {
  font-size: 0.9375rem;
  font-weight: 700;
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--color-border);
}

.modal ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.modal ul li {
  padding-left: 1.1em;
  position: relative;
  font-size: 0.9375rem;
  line-height: 1.55;
  color: var(--color-text);
}

.modal ul li::before {
  content: '・';
  position: absolute;
  left: 0;
  color: var(--color-muted);
}

.tip {
  margin-top: 12px;
  font-size: 0.875rem;
  color: var(--color-muted);
  background: var(--color-subtle);
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  line-height: 1.5;
}

.modal-footer {
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.modal-close-btn {
  width: 100%;
  padding: 12px 0;
  border: none;
  border-radius: var(--radius-md);
  background: var(--color-text);
  color: var(--color-bg);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.modal-close-btn:hover {
  opacity: 0.82;
}

.legal-link {
  font-size: 0.8125rem;
  color: var(--color-muted);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.legal-link:hover {
  color: var(--color-text);
}

/* ── Tablet & above ── */
@media (min-width: 480px) {
  :root {
    --cell-size: 120px;
  }

  header h1 {
    font-size: 1.5rem;
  }

  .status {
    font-size: 1.25rem;
  }
}
