@import url("https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=Space+Grotesk:wght@500;700&display=swap");

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

:root {
  color-scheme: dark;
  --bg-start: #07111f;
  --bg-end: #132a4a;
  --bg-overlay:
    radial-gradient(circle at top left, rgba(125, 211, 252, 0.18), transparent 26%),
    radial-gradient(circle at bottom right, rgba(52, 211, 153, 0.14), transparent 24%);
  --card: rgba(10, 18, 40, 0.72);
  --card-border: rgba(255, 255, 255, 0.12);
  --text: #f4f7fb;
  --muted: rgba(244, 247, 251, 0.72);
  --tile: rgba(255, 255, 255, 0.94);
  --tile-hover: #ffffff;
  --accent: #7dd3fc;
  --accent-strong: #38bdf8;
  --win: #34d399;
  --shadow: 0 28px 70px rgba(2, 8, 23, 0.38);
  --surface: rgba(15, 23, 42, 0.38);
  --surface-strong: rgba(15, 23, 42, 0.5);
}

html[data-theme="light"] {
  color-scheme: light;
  --bg-start: #eef4ff;
  --bg-end: #dceafe;
  --bg-overlay:
    radial-gradient(circle at top left, rgba(59, 130, 246, 0.14), transparent 24%),
    radial-gradient(circle at bottom right, rgba(16, 185, 129, 0.1), transparent 22%);
  --card: rgba(255, 255, 255, 0.78);
  --card-border: rgba(15, 23, 42, 0.08);
  --text: #0f172a;
  --muted: rgba(15, 23, 42, 0.68);
  --tile: rgba(255, 255, 255, 0.98);
  --tile-hover: #ffffff;
  --accent: #2563eb;
  --accent-strong: #0ea5e9;
  --win: #16a34a;
  --shadow: 0 26px 70px rgba(15, 23, 42, 0.14);
  --surface: rgba(255, 255, 255, 0.72);
  --surface-strong: rgba(255, 255, 255, 0.86);
}

body {
  min-height: 100vh;
  font-family: "Plus Jakarta Sans", "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg-overlay), linear-gradient(160deg, var(--bg-start), var(--bg-end));
  display: grid;
  place-items: center;
  padding: 24px;
  overflow-x: hidden;
  transition:
    background-color 200ms ease,
    color 200ms ease;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.04) 1px, transparent 1px);
  background-size: 64px 64px;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.14), transparent 80%);
  opacity: 0.34;
}

.app-shell {
  width: 100%;
  display: flex;
  justify-content: center;
}

.game-card {
  width: min(96vw, 1240px);
  padding: clamp(20px, 4vw, 34px);
  border: 1px solid var(--card-border);
  border-radius: 28px;
  background: var(--card);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  animation: liftIn 450ms ease both;
}

.mode-card {
  margin-bottom: 1rem;
}

.setup-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
  margin-top: 1.5rem;
}

.setup-panel {
  padding: 1rem;
  border-radius: 24px;
  background: var(--surface);
  border: 1px solid var(--card-border);
}

.panel-header {
  margin-bottom: 1rem;
}

.panel-header h2 {
  font-size: 1.08rem;
  margin-bottom: 0.35rem;
}

.panel-header p {
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.header-copy {
  text-align: center;
  margin-bottom: 1.6rem;
}

.header-copy h1,
.stats-header h2,
.history-header h3,
.mode-label,
.stats-value,
#winner-box {
  font-family: "Space Grotesk", "Plus Jakarta Sans", sans-serif;
}

.eyebrow {
  font-size: 0.82rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.7rem;
}

h1 {
  font-size: clamp(2.2rem, 5vw, 3.8rem);
  line-height: 1;
  margin-bottom: 0.8rem;
}

.subtitle {
  font-size: clamp(0.98rem, 2vw, 1.1rem);
  color: var(--muted);
}

.mode-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem;
}

.mode-btn,
#change-mode-btn {
  border: none;
  border-radius: 20px;
  cursor: pointer;
}

.mode-btn {
  padding: 1.2rem;
  text-align: left;
  color: var(--text);
  background: linear-gradient(180deg, var(--surface-strong), var(--surface));
  border: 1px solid var(--card-border);
  box-shadow: 0 12px 26px rgba(2, 8, 23, 0.2);
  transition:
    transform 150ms ease,
    background-color 150ms ease,
    border-color 150ms ease;
}

.mode-btn:hover {
  transform: translateY(-3px);
  border-color: rgba(125, 211, 252, 0.45);
}

.mode-btn.active,
.size-btn.active {
  border-color: rgba(125, 211, 252, 0.7);
  box-shadow:
    0 0 0 1px rgba(125, 211, 252, 0.28),
    0 14px 30px rgba(2, 8, 23, 0.16);
}

.mode-btn:focus-visible,
#change-mode-btn:focus-visible {
  outline: 3px solid rgba(125, 211, 252, 0.85);
  outline-offset: 3px;
}

.mode-label {
  display: block;
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.45rem;
}

.mode-note {
  display: block;
  font-size: 0.96rem;
  color: var(--muted);
  line-height: 1.45;
}

.size-options {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
}

.size-btn,
.start-game-btn {
  border: none;
  border-radius: 18px;
  cursor: pointer;
  font-weight: 700;
  transition:
    transform 150ms ease,
    background-color 150ms ease,
    border-color 150ms ease;
}

.size-btn {
  padding: 0.95rem 0.75rem;
  background: var(--surface-strong);
  color: var(--text);
  border: 1px solid var(--card-border);
  font-size: 0.98rem;
}

.size-btn:hover,
.start-game-btn:hover {
  transform: translateY(-2px);
}

.start-game-btn {
  width: 100%;
  margin-top: 1rem;
  padding: 1rem 1.1rem;
  background: linear-gradient(135deg, #1d4ed8, #38bdf8);
  color: white;
  box-shadow: 0 12px 24px rgba(29, 78, 216, 0.24);
  font-size: 1rem;
}

.start-game-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.mode-strip {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

#mode-badge,
#board-badge,
#change-mode-btn {
  min-height: 44px;
  border-radius: 999px;
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
}

#board-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 44px;
  border-radius: 999px;
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--card-border);
}

#mode-badge {
  background: var(--surface-strong);
  color: var(--text);
  border: 1px solid var(--card-border);
}

.mode-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex-wrap: wrap;
}

#restart-board-btn {
  background: var(--surface);
}

.drawer-toggle-btn,
.drawer-close-btn {
  border: none;
  border-radius: 999px;
  min-height: 44px;
  padding: 0.72rem 0.95rem;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--card-border);
  transition:
    transform 150ms ease,
    background-color 150ms ease,
    opacity 150ms ease;
}

.drawer-toggle-btn {
  display: none;
}

.drawer-toggle-btn:hover,
.drawer-close-btn:hover {
  transform: translateY(-2px);
  background: var(--surface-strong);
}

.drawer-close-btn {
  display: none;
}

.stats-panel,
.history-panel {
  padding: 1rem;
  border-radius: 24px;
  border: 1px solid var(--card-border);
  background: var(--surface);
}

.history-panel {
  min-height: 100%;
}

.stats-header {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: end;
  flex-wrap: wrap;
  margin-bottom: 0.9rem;
}

.stats-header h2,
.history-header h3 {
  font-size: 1.05rem;
}

.stats-header p {
  color: var(--muted);
  font-size: 0.95rem;
}

.clear-score-btn {
  border: none;
  border-radius: 999px;
  padding: 0.72rem 0.95rem;
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 700;
  transition:
    transform 150ms ease,
    background-color 150ms ease;
}

.clear-score-btn:hover {
  transform: translateY(-2px);
  background: var(--surface-strong);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.8rem;
}

.stats-card {
  padding: 0.95rem;
  border-radius: 18px;
  background: var(--surface-strong);
  border: 1px solid var(--card-border);
}

.stats-card-wide {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.stats-label {
  display: block;
  font-size: 0.92rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

.stats-value {
  display: block;
  font-size: 1.8rem;
  line-height: 1;
  margin-bottom: 0.3rem;
  color: var(--text);
}

.stats-note {
  display: block;
  font-size: 0.88rem;
  color: var(--muted);
}

.history-wrap {
  margin-top: 0;
  padding-top: 1rem;
  border-top: 1px solid var(--card-border);
}

.history-header {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 0.75rem;
}

.history-count {
  color: var(--muted);
  font-size: 0.88rem;
}

.history-list {
  list-style: none;
  display: grid;
  gap: 0.55rem;
}

.history-item {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: center;
  padding: 0.75rem 0.9rem;
  border-radius: 14px;
  background: var(--surface-strong);
  border: 1px solid var(--card-border);
}

.history-main {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.history-result {
  font-weight: 700;
  color: var(--text);
}

.history-meta {
  color: var(--muted);
  font-size: 0.86rem;
}

.history-time {
  color: var(--accent);
  font-size: 0.84rem;
  white-space: nowrap;
}

#change-mode-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--card-border);
  transition:
    transform 150ms ease,
    background-color 150ms ease;
}

#change-mode-btn:hover {
  transform: translateY(-2px);
  background: var(--surface-strong);
}

.game-workspace {
  display: grid;
  grid-template-columns: minmax(220px, 280px) minmax(0, 620px);
  gap: 1rem;
  align-items: start;
  justify-content: center;
  margin-top: 1rem;
}

.drawer-panels {
  display: contents;
}

.side-panel,
.board-column {
  min-width: 0;
}

.board-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.side-panel {
  position: sticky;
  top: 1rem;
}

.side-panel-left {
  grid-column: 1;
}

.board-column {
  grid-column: 2;
}

.side-panel-right {
  grid-column: 3;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 1.5rem 0;
  width: 100%;
}

.game {
  --board-size: 3;
  --cell-font-size: 2.8rem;
  width: min(100%, 620px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(var(--board-size), 1fr);
  grid-template-rows: repeat(var(--board-size), 1fr);
  gap: clamp(10px, 1.7vw, 16px);
}

@media (max-width: 1080px) {
  .game-workspace {
    grid-template-columns: 1fr;
  }

  .drawer-toggle-btn {
    display: inline-flex;
  }

  .drawer-panels {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    order: 2;
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition:
      max-height 240ms ease,
      opacity 200ms ease;
  }

  .game-card.drawer-open .drawer-panels {
    max-height: 900px;
    opacity: 1;
    pointer-events: auto;
  }

  .drawer-close-btn {
    display: inline-flex;
    align-self: flex-start;
  }

  .side-panel {
    position: static;
  }

  .side-panel-left,
  .side-panel-right,
  .board-column {
    grid-column: auto;
  }

  .board-column {
    order: 1;
  }
}

.box {
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 18px;
  background: var(--tile);
  box-shadow: 0 10px 22px rgba(15, 23, 42, 0.18);
  font-size: var(--cell-font-size);
  font-weight: 700;
  line-height: 1;
  color: #0f172a;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition:
    transform 150ms ease,
    box-shadow 150ms ease,
    background-color 150ms ease,
    opacity 150ms ease;
}

.box:hover:not(:disabled) {
  transform: translateY(-3px) scale(1.01);
  background: var(--tile-hover);
  box-shadow: 0 14px 24px rgba(15, 23, 42, 0.24);
}

.box:active:not(:disabled) {
  transform: translateY(0) scale(0.98);
}

.box:focus-visible,
#reset-btn:focus-visible {
  outline: 3px solid rgba(125, 211, 252, 0.85);
  outline-offset: 3px;
}

.box.winning {
  background: linear-gradient(135deg, rgba(52, 211, 153, 0.95), rgba(125, 211, 252, 0.95));
  color: #031220;
  box-shadow:
    0 0 0 2px rgba(255, 255, 255, 0.24),
    0 14px 30px rgba(52, 211, 153, 0.3);
  animation: pulseWin 720ms ease-in-out infinite alternate;
}

#infos {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

#reset-btn,
#winner-container {
  min-height: 56px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  padding: 0.95rem 1.15rem;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  gap: 0.45rem;
  font-size: 1rem;
}

#reset-btn {
  border: none;
  background: linear-gradient(135deg, #0f172a, #1d4ed8);
  color: white;
  cursor: pointer;
  font-weight: 700;
  letter-spacing: 0.01em;
  transition:
    transform 150ms ease,
    box-shadow 150ms ease;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.2);
}

#reset-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 14px 28px rgba(15, 23, 42, 0.26);
}

#winner-container {
  color: var(--text);
  background: var(--surface-strong);
}

#winner-box {
  color: var(--accent);
  font-weight: 700;
}

#winner-box.draw-state {
  color: #f59e0b;
}

@media (max-width: 600px) {
  body {
    padding: 16px;
  }

  .game-card {
    width: min(100%, 720px);
    padding: 18px;
    border-radius: 24px;
  }

  .game-workspace {
    grid-template-columns: 1fr;
  }

  .setup-grid {
    grid-template-columns: 1fr;
  }

  .mode-actions {
    width: 100%;
    justify-content: space-between;
  }

  .drawer-toggle-btn {
    display: inline-flex;
  }

  .drawer-toggle-btn,
  .drawer-close-btn,
  #change-mode-btn,
  #restart-board-btn {
    width: 100%;
  }

  .drawer-panels {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    order: 2;
    max-height: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transition:
      max-height 240ms ease,
      opacity 200ms ease;
  }

  .game-card.drawer-open .drawer-panels {
    max-height: 900px;
    opacity: 1;
    pointer-events: auto;
  }

  .drawer-close-btn {
    display: inline-flex;
    align-self: flex-start;
  }

  .drawer-open .drawer-toggle-btn {
    background: rgba(125, 211, 252, 0.22);
    border-color: rgba(125, 211, 252, 0.5);
  }

  .side-panel {
    position: static;
  }

  .side-panel-left,
  .side-panel-right,
  .board-column {
    grid-column: auto;
  }

  .board-column {
    order: 1;
  }

  .drawer-panels {
    order: 2;
  }

  .mode-options {
    grid-template-columns: 1fr;
  }

  .size-options {
    grid-template-columns: 1fr;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .history-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .game {
    width: 100%;
    --cell-font-size: 2.4rem;
    gap: 10px;
  }

  #infos {
    gap: 0.75rem;
  }

  #reset-btn,
  #winner-container {
    width: 100%;
    font-size: 0.98rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@keyframes liftIn {
  from {
    opacity: 0;
    transform: translateY(18px) scale(0.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes pulseWin {
  from {
    transform: translateY(0) scale(1);
  }
  to {
    transform: translateY(-2px) scale(1.01);
  }
}
