/* ========== CSS Custom Properties (Theme System) ========== */
:root {
  --custom-primary: #000000;
  --custom-secondary: #FFFFFF;
  --custom-accent: #777777;
  --bg: #FFB84E;
  --text: #1B1A1B;
  --accent: #FF9A3B;
}

/* Theme: Bee (default) */
body[data-theme="bee"] {
  --bg: #FFB84E;
  --text: #1B1A1B;
  --accent: #FF9A3B;
}

/* Theme: Anti-Bee */
body[data-theme="anti-bee"] {
  --bg: #1B1A1B;
  --text: #FFB84E;
  --accent: #474747;
}

/* Theme: Heather */
body[data-theme="heather"] {
  --bg: #FFC2E7;
  --text: #1B1A1B;
  --accent: #EE90CA;
}

/* Theme: Anti-Heather */
body[data-theme="anti-heather"] {
  --bg: #1B1A1B;
  --text: #FFC2E7;
  --accent: #474747;
}

/* Theme: Pearl */
body[data-theme="pearl"] {
  --bg: #A8D5D1;
  --text: #1B1A1B;
  --accent: #84C1BB;
}

/* Theme: Anti-Pearl */
body[data-theme="anti-pearl"] {
  --bg: #1B1A1B;
  --text: #A8D5D1;
  --accent: #474747;
}

/* Theme: Oatmeal */
body[data-theme="oatmeal"] {
  --bg: #FFDBB1;
  --text: #1B1A1B;
  --accent: #FFCC93;
}

/* Theme: Anti-Oatmeal */
body[data-theme="anti-oatmeal"] {
  --bg: #1B1A1B;
  --text: #FFDBB1;
  --accent: #474747;
}

/* Theme: Coral */
body[data-theme="coral"] {
  --bg: #FFBBA9;
  --text: #1B1A1B;
  --accent: #FFA38A;
}

/* Theme: Anti-Coral */
body[data-theme="anti-coral"] {
  --bg: #1B1A1B;
  --text: #FFBBA9;
  --accent: #474747;
}

/* Theme: Custom */
body[data-theme="custom"] {
  --bg: var(--custom-secondary);
  --text: var(--custom-primary);
  --accent: var(--custom-accent);
}

/* ========== Base Styles ========== */
* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Plus Jakarta Sans", sans-serif;
  transition: background 0.3s ease, color 0.3s ease;
}

/* ========== Typography ========== */
h1 {
  font-size: clamp(48px, 10vw, 120px);
  line-height: 0.98;
  margin: 0;
  letter-spacing: -0.025em;
}

h3 {
  margin: 0;
  font-family: "Plus Jakarta Sans", sans-serif;
  font-size: clamp(28px, 6vw, 44px);
  letter-spacing: -0.05em;
}

p {
  margin-top: 24px;
  font-family: "Space Mono", monospace;
  font-size: clamp(16px, 2vw, 24px);
}

/* ========== Buttons ========== */
button {
  background: var(--bg);
  color: inherit;
  border: 2px solid currentColor;
  padding: 12px 18px;
  font-family: "Plus Jakarta Sans", sans-serif;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.16s ease, background 0.16s ease, box-shadow 0.16s ease, filter 0.16s ease;
}

button:hover,
button:focus-visible,
button.active {
  background: var(--accent);
  transform: translateY(-2px);
  box-shadow: 4px 4px 0 currentColor;
  filter: brightness(1.02);
}

button:active {
  transform: translateY(1px) scale(0.98);
  box-shadow: 1px 1px 0 currentColor;
}

/* ========== Layout Sections ========== */
.hero,
.game-section,
.settings-modal {
  display: flex;
  align-items: center;
  justify-content: center;
}

.hero {
  min-height: 100vh;
  text-align: center;
  padding: 100px 24px 80px;
}

.hero p {
  max-width: 760px;
  margin: 24px auto 0;
  text-align: center;
  line-height: 1.45;
}

.game-section {
  position: relative;
  min-height: 100vh;
  padding: 110px 24px 80px;
  flex-direction: column;
  box-shadow: inset 0 2px 0 currentColor;
  overflow: hidden;
}

.game-section h2 {
  font-size: clamp(40px, 8vw, 96px);
  margin: 0 0 24px;
  letter-spacing: -0.05em;
}

/* ========== Aesthetic Menu (Theme Picker) ========== */
.aesthetic-menu-wrap {
  position: fixed;
  top: 24px;
  left: 24px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
  animation: aestheticSlideIn 0.55s cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
}

@keyframes aestheticSlideIn {
  from { opacity: 0; transform: translateX(-42px) rotate(-2deg); }
  to { opacity: 1; transform: translateX(0) rotate(0deg); }
}

.aesthetic-panel,
.overlay-box,
.settings-box {
  background: var(--accent);
  color: var(--text);
}

.aesthetic-panel {
  width: 270px;
  border: 2px solid currentColor;
  padding: 10px;
  box-shadow: 8px 8px 0 currentColor;
  animation: aestheticPanelPop 0.18s ease both;
  font-family: "Space Mono", monospace;
}

.aesthetic-panel.closing {
  animation: aestheticPanelAway 0.18s ease both;
}

@keyframes aestheticPanelPop {
  from { opacity: 0; transform: translateY(-8px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes aestheticPanelAway {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(-8px) scale(0.97); }
}

.aesthetic-panel-title,
.custom-theme-title {
  font-family: "Space Mono", monospace;
  font-size: 12px;
  margin-bottom: 10px;
}

.theme-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 8px;
  margin-top: 8px;
  align-items: stretch;
}

.theme-option {
  width: 100%;
  min-height: 42px;
  padding: 10px 8px;
  text-align: center;
  white-space: nowrap;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.custom-theme-picker {
  padding-top: 12px;
  font-size: 12px;
  font-family: "Space Mono", monospace;
}

.custom-theme-picker label,
.toggle-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}

.custom-theme-picker input[type="color"] {
  width: 44px;
  height: 32px;
  padding: 0;
  border: 2px solid currentColor;
  background: transparent;
}

#applyCustomTheme {
  width: 100%;
  margin-top: 10px;
  padding: 10px 12px;
  font-size: 12px;
}

/* ========== Game Board ========== */
.game-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
  flex-wrap: wrap;
}

.game-board {
  position: relative;
}

.game-board.impact-small {
  animation: impactSmall 0.16s ease;
}

.game-board.impact-big {
  animation: impactBig 0.24s ease;
}

.game-board.resume-pop {
  animation: resumePop 0.28s ease;
}

@keyframes impactSmall {
  35% { transform: translateY(5px); }
  65% { transform: translateY(-2px); }
}

@keyframes impactBig {
  20% { transform: translate(-4px, 7px) rotate(-1deg); }
  40% { transform: translate(5px, -3px) rotate(1deg); }
  65% { transform: translate(-2px, 4px) rotate(-0.5deg); }
}

@keyframes resumePop {
  0% { transform: scale(0.985); }
  55% { transform: scale(1.015); }
  100% { transform: scale(1); }
}

/* ========== Canvas Styles ========== */
canvas {
  display: block;
  border: 3px solid currentColor;
  background: color-mix(in srgb, var(--text) 8%, transparent);
  image-rendering: pixelated;
}

#tetris {
  width: 250px;
  height: 500px;
}

#nextPiece {
  width: 118px;
  height: 118px;
  margin: 0 auto;
}

/* ========== Overlays ========== */
.overlay {
  position: absolute;
  inset: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: color-mix(in srgb, var(--accent) 48%, transparent);
  backdrop-filter: blur(2px);
  padding: 20px;
  z-index: 5;
}

.overlay-box {
  border: 2px solid currentColor;
  padding: 22px;
  max-width: 210px;
  animation: overlayBoxPop 0.22s ease both;
}

@keyframes overlayBoxPop {
  from { opacity: 0; transform: scale(0.92) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.overlay-title {
  font-family: "Plus Jakarta Sans", sans-serif;
  font-size: 28px;
  line-height: 0.95;
  letter-spacing: -0.04em;
  margin-bottom: 12px;
}

.overlay-text {
  font-family: "Space Mono", monospace;
  font-size: 12px;
  line-height: 1.4;
  margin-bottom: 16px;
}

/* ========== Side Panel (Stats & Controls) ========== */
.panel {
  width: 150px;
  height: 500px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  font-family: "Space Mono", monospace;
  font-size: 16px;
  line-height: 1.5;
}

.score {
  width: 100%;
  min-height: 52px;
  font-family: "Plus Jakarta Sans", sans-serif;
  font-size: 30px;
  line-height: 1;
  margin: 0;
  text-align: center;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.high-score {
  margin-top: 6px;
  font-family: "Space Mono", monospace;
  font-size: 12px;
  line-height: 1.2;
}

.next-piece-panel {
  width: 100%;
  text-align: center;
}

.next-piece-title {
  font-family: "Plus Jakarta Sans", sans-serif;
  font-size: 28px;
  line-height: 1;
  margin-bottom: 8px;
  letter-spacing: -0.04em;
}

.stat-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 8px;
}

.stat-line {
  width: 100%;
  border: 2px solid currentColor;
  background: transparent;
  padding: 8px;
  font-size: 13px;
  text-align: center;
  font-family: "Space Mono", monospace;
}

.side-buttons {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  width: 100%;
  margin-top: auto;
}

.side-buttons button {
  width: 100%;
  text-align: center;
  padding: 9px 10px;
}

/* ========== Settings Modal ========== */
.settings-modal {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--accent) 42%, transparent);
  backdrop-filter: blur(5px);
  padding: 24px;
  z-index: 100;
  animation: settingsBackdropIn 0.2s ease both;
}

.settings-modal.closing {
  animation: settingsBackdropOut 0.18s ease both;
}

.settings-box {
  border: 2px solid currentColor;
  padding: 22px;
  max-width: 560px;
  width: min(100%, 560px);
  max-height: min(82vh, 640px);
  overflow: auto;
  box-shadow: 10px 10px 0 currentColor;
  animation: settingsBoxIn 0.24s cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
  font-family: "Space Mono", monospace;
}

.settings-modal.closing .settings-box {
  animation: settingsBoxOut 0.18s ease both;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.settings-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.settings-tab {
  padding: 10px;
  font-size: 12px;
}

.settings-panel {
  display: none;
  border: 2px solid currentColor;
  padding: 14px;
  background: var(--bg);
  font-family: "Space Mono", monospace;
}

.settings-panel.active {
  display: block;
}

.settings-panel p,
.small-note {
  font-size: 14px;
  line-height: 1.65;
  margin: 0;
  font-family: "Space Mono", monospace;
}

.small-note {
  margin-top: 16px;
}

@keyframes settingsBackdropIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes settingsBackdropOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes settingsBoxIn {
  from { opacity: 0; transform: translateY(24px) scale(0.96) rotate(-1deg); }
  to { opacity: 1; transform: translateY(0) scale(1) rotate(0deg); }
}

@keyframes settingsBoxOut {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(16px) scale(0.97); }
}

/* ========== Toggle Switch ========== */
.toggle-row input {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  width: 28px;
  height: 28px;
  border: 2px solid currentColor;
  background: var(--accent);
  cursor: pointer;
  transition: transform 0.16s ease, background 0.16s ease;
}

.toggle-row input::after {
  content: "";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: "Plus Jakarta Sans", sans-serif;
  font-size: 22px;
  line-height: 1;
  color: var(--text);
}

.toggle-row input:checked {
  animation: toggleBounce 0.24s ease;
}

.toggle-row input:checked::after {
  content: "X";
}

.toggle-row input:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 3px;
}

@keyframes toggleBounce {
  0% { transform: scale(0.88); }
  55% { transform: scale(1.16); }
  100% { transform: scale(1); }
}

/* ========== Fullscreen Mode ========== */
.game-section.enter-fullscreen .game-wrap {
  animation: fullscreenSettle 0.42s ease both;
}

@keyframes fullscreenSettle {
  0% { opacity: 0.9; transform: scale(0.975); }
  60% { opacity: 1; transform: scale(1.01); }
  100% { opacity: 1; transform: scale(1); }
}

.game-section:fullscreen {
  width: 100vw;
  height: 100vh;
  min-height: 100vh;
  padding: 0;
  background: var(--bg);
  color: var(--text);
}

.game-section:fullscreen h2 {
  display: none;
}

.game-section:fullscreen .game-wrap {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-columns: minmax(160px, 1fr) auto minmax(170px, 1fr);
  grid-template-rows: auto 1fr auto;
  align-items: stretch;
  justify-items: center;
  gap: 22px 26px;
  padding: 32px clamp(20px, 5vw, 72px);
}

.game-section:fullscreen .game-board {
  grid-column: 2;
  grid-row: 1 / 4;
  height: min(94vh, calc((100vw - 420px) * 2));
  aspect-ratio: 10 / 20;
  align-self: center;
}

.game-section:fullscreen #tetris {
  width: 100%;
  height: 100%;
}

.game-section:fullscreen .panel {
  display: contents;
}

.game-section:fullscreen .score {
  grid-column: 3;
  grid-row: 1;
  align-self: start;
  justify-self: start;
  width: 170px;
  font-size: 34px;
}

.game-section:fullscreen .next-piece-panel {
  grid-column: 1;
  grid-row: 1;
  align-self: start;
  justify-self: end;
  width: 150px;
}

.game-section:fullscreen #nextPiece {
  width: 150px;
  height: 150px;
}

.game-section:fullscreen .stat-stack {
  grid-column: 3;
  grid-row: 1 / 3;
  align-self: start;
  justify-self: start;
  width: 170px;
  margin-top: 74px;
  gap: 14px;
}

.game-section:fullscreen .stat-line {
  padding: 12px 10px;
  font-size: 16px;
}

.game-section:fullscreen .side-buttons {
  grid-column: 3;
  grid-row: 3;
  align-self: end;
  justify-self: start;
  width: 190px;
  gap: 14px;
}

.game-section:fullscreen .side-buttons button {
  padding: 14px 12px;
  font-size: 16px;
}

.game-section:fullscreen .settings-modal {
  position: fixed;
  width: 100vw;
  height: 100vh;
}

/* ========== Visibility Utilities ========== */
.hidden,
.aesthetic-panel.hidden,
.overlay.hidden,
.settings-modal.hidden,
.is-hidden {
  display: none !important;
}

/* ========== Responsive Design ========== */
@media (max-width: 820px) {
  #tetris {
    width: 210px;
    height: 420px;
  }

  .panel {
    width: 210px;
    height: auto;
  }

  .game-section:fullscreen .game-wrap {
    grid-template-columns: auto;
    grid-template-rows: auto auto;
    gap: 14px;
    align-content: center;
  }

  .game-section:fullscreen .game-board {
    grid-column: 1;
    grid-row: 1;
    height: min(68vh, 78vw * 2);
  }

  .game-section:fullscreen .panel {
    display: grid;
    grid-column: 1;
    grid-row: 2;
    width: min(92vw, 420px);
    height: auto;
    grid-template-columns: repeat(4, 1fr);
    align-items: center;
    gap: 8px;
  }

  .game-section:fullscreen .score,
  .game-section:fullscreen .stat-stack,
  .game-section:fullscreen .next-piece-panel,
  .game-section:fullscreen .side-buttons {
    grid-column: auto;
    grid-row: auto;
    width: auto;
    margin: 0;
    justify-self: stretch;
  }

  .game-section:fullscreen .next-piece-panel {
    display: none;
  }

  .game-section:fullscreen .side-buttons {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
  }
}
