*, 
*::before, 
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, 
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #101515;
  color: #f8f8f6;
}

:root {
  --cell-size: min(8.25vw, 8.25vh);
}

.app {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

/* Large animated counter */
.counter-overlay {
  position: absolute;
  top: 18%;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(2.6rem, 9vw, 3.8rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  color: #f5f2d8;
  text-shadow:
    0 0 12px rgba(0, 0, 0, 0.75),
    0 0 32px rgba(255, 230, 150, 0.45);
  pointer-events: none;
  z-index: 20;
}

/* Equation display */
.equation-overlay {
  position: absolute;
  top: 4%;
  left: 50%;
  transform: translateX(-50%);
  font-size: clamp(1.2rem, 4.5vw, 2rem);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #f5f2d8;
  text-shadow:
    0 0 10px rgba(0, 0, 0, 0.8),
    0 0 22px rgba(255, 230, 150, 0.35);
  pointer-events: none;
  z-index: 20;
  min-height: 1.4em;
}

/* Chart display */
.chart-overlay {
  position: absolute;
  top: 10%;
  left: 50%;
  transform: translateX(-50%);
  width: min(80vw, 500px);
  pointer-events: none;
  z-index: 20;
}

.chart-container {
  width: 100%;
  height: 42px;
  background: rgba(20, 25, 24, 0.85);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow:
    inset 0 2px 8px rgba(0, 0, 0, 0.6),
    0 4px 16px rgba(0, 0, 0, 0.7);
  overflow: hidden;
  display: flex;
  position: relative;
}

.chart-segment {
  height: 100%;
  position: relative;
  transition: width 400ms cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.95);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}

.chart-segment::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0.15), transparent 50%);
  pointer-events: none;
}



@media (max-width: 768px) {
  .equation-overlay {
    top: 3%;
    font-size: clamp(0.84rem, 3.15vw, 1.4rem); /* ~30% smaller than default */
  }

  .chart-overlay {
    top: 8%;
    width: min(88vw, 500px);
  }
  
  .chart-container {
    height: 28px; /* reduce height for ~30% smaller footprint */
  }
  
  .chart-segment {
    font-size: 0.6rem;
  }

  .counter-overlay {
    top: 14%;
  }
}

/* Controls container */
.controls {
  position: absolute;
  bottom: 8%;
  left: 50%;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  z-index: 15;
}

/* Start button */
.start-button {
  padding: 0.9rem 2.4rem;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  border-radius: 999px;
  border: none;
  outline: none;
  cursor: pointer;
  background: #f5f2d8;
  color: #151815;
  box-shadow:
    0 10px 20px rgba(0, 0, 0, 0.65),
    0 0 0 1px rgba(0, 0, 0, 0.4);
  transition:
    transform 160ms ease-out,
    box-shadow 160ms ease-out,
    opacity 220ms ease-out;
}

.start-button:active {
  transform: translateY(2px);
  box-shadow:
    0 6px 12px rgba(0, 0, 0, 0.7),
    0 0 0 1px rgba(0, 0, 0, 0.5);
}

/* Next and Toggle buttons */
.next-button,
.control-toggle-button {
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  border-radius: 999px;
  border: none;
  outline: none;
  cursor: pointer;
  background: rgba(245, 242, 216, 0.08);
  color: #f5f2d8;
  box-shadow:
    0 8px 16px rgba(0, 0, 0, 0.7),
    0 0 0 1px rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition:
    background 140ms ease-out,
    transform 140ms ease-out,
    box-shadow 140ms ease-out;
}

.next-button:active,
.control-toggle-button:active {
  transform: translateY(2px);
  box-shadow:
    0 5px 10px rgba(0, 0, 0, 0.8),
    0 0 0 1px rgba(255, 255, 255, 0.12);
  background: rgba(245, 242, 216, 0.16);
}

.control-toggle-button {
  padding: 0.7rem 0.9rem;
  min-width: 3.2rem;
}

@media (max-width: 768px) {
  .controls {
    bottom: 7%;
    gap: 0.5rem;
    flex-wrap: wrap;
    max-width: 90vw;
    justify-content: center;
  }

  .start-button {
    padding: 0.8rem 1.9rem;
    font-size: 1rem;
  }

  .next-button {
    padding: 0.6rem 0.9rem;
    font-size: 0.8rem;
  }
}

/* Visual feedback for a selected (draggable) wrapper */
.cube-wrapper.selected .cube-face,
.cube-wrapper.selected .cube-face-4x4,
.cube-wrapper.selected .cube-face-5x5,
.cube-wrapper.selected .cube-face-6x6,
.cube-wrapper.selected .cube-face-7x7,
.cube-wrapper.selected .cube-face-8x8,
.cube-wrapper.selected .cube-face-9x9,
.cube-wrapper.selected .cube-face-10x10,
.cube-wrapper.selected .cube-face-12x12 {
  box-shadow:
    0 26px 46px rgba(0, 0, 0, 0.9),
    0 0 0 2px rgba(255, 238, 170, 0.9);
}

/* 3x3, 4x4, 5x5, 6x6, 7x7, 8x8, 9x9, 10x10 faces share the same styling base */
.cube-face,
.cube-face-4x4,
.cube-face-5x5,
.cube-face-6x6,
.cube-face-7x7,
.cube-face-8x8,
.cube-face-9x9,
.cube-face-10x10 {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  gap: 4px;
  padding: 8px;
  border-radius: 18px;
  background: radial-gradient(circle at 10% 0%, #343c3a, #181e1d 55%, #050606 100%);
  box-shadow:
    0 24px 40px rgba(0, 0, 0, 0.65),
    0 0 0 1px rgba(255, 255, 255, 0.06);
  transform-style: preserve-3d;
  transform: rotateX(24deg) rotateY(-28deg);
  transition: transform 180ms ease-out, box-shadow 180ms ease-out;
}

/* Specific grid layouts */
.cube-face {
  grid-template-columns: repeat(3, var(--cell-size));
  grid-template-rows: repeat(3, var(--cell-size));
}

.cube-face-4x4 {
  grid-template-columns: repeat(4, var(--cell-size));
  grid-template-rows: repeat(4, var(--cell-size));
}

.cube-face-5x5 {
  grid-template-columns: repeat(5, var(--cell-size));
  grid-template-rows: repeat(5, var(--cell-size));
}

.cube-face-6x6 {
  grid-template-columns: repeat(6, var(--cell-size));
  grid-template-rows: repeat(6, var(--cell-size));
}

.cube-face-7x7 {
  grid-template-columns: repeat(7, var(--cell-size));
  grid-template-rows: repeat(7, var(--cell-size));
}

.cube-face-8x8 {
  grid-template-columns: repeat(8, var(--cell-size));
  grid-template-rows: repeat(8, var(--cell-size));
}

.cube-face-9x9 {
  grid-template-columns: repeat(9, var(--cell-size));
  grid-template-rows: repeat(9, var(--cell-size));
}

.cube-face-10x10 {
  grid-template-columns: repeat(10, var(--cell-size));
  grid-template-rows: repeat(10, var(--cell-size));
}

.cube-face-11x11 {
  grid-template-columns: repeat(11, var(--cell-size));
  grid-template-rows: repeat(11, var(--cell-size));
}

.cube-face-12x12 {
  grid-template-columns: repeat(12, var(--cell-size));
  grid-template-rows: repeat(12, var(--cell-size));
}

.cube-face-15x15 {
  grid-template-columns: repeat(15, var(--cell-size));
  grid-template-rows: repeat(15, var(--cell-size));
}

/* Slight highlight rim */
.cube-face::before,
.cube-face-4x4::before,
.cube-face-5x5::before,
.cube-face-6x6::before,
.cube-face-7x7::before,
.cube-face-8x8::before,
.cube-face-9x9::before,
.cube-face-10x10::before,
.cube-face-12x12::before,
.cube-face-15x15::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  border: 1px solid rgba(255, 255, 255, 0.12);
  mix-blend-mode: screen;
  pointer-events: none;
}

/* Individual cubelets (Tiles/Coins) */
.cell {
  position: relative;
  background: linear-gradient(145deg, #3a4442, #181e1d);
  border-radius: 8px;
  /* Multi-layered shadow to simulate coin thickness/depth */
  box-shadow:
    0 1px 0 #2c3533,
    0 2px 0 #242b29,
    0 3px 0 #1c2120,
    0 4px 0 #141817,
    0 5px 0 #0c0e0d,
    0 8px 15px rgba(0, 0, 0, 0.8),
    inset 0 1px 1px rgba(255, 255, 255, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  transform-style: preserve-3d;
  /* Slightly lift cells by default for the 3D look */
  transform: translateZ(5px);
  transition:
    transform 200ms ease-out,
    box-shadow 200ms ease-out,
    opacity 200ms ease-out,
    filter 200ms ease-out;
}

/* Subtle rim highlight for the 'coin' top */
.cell::before {
  content: "";
  position: absolute;
  inset: 2px;
  border-radius: 6px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.05), transparent 60%);
  border: 1px solid rgba(255, 255, 255, 0.08);
  pointer-events: none;
}

/* Numbers */
.cell span {
  position: relative;
  display: block;
  font-weight: 700;
  font-size: clamp(2.2rem, 7vw, 3.2rem);
  color: #f8f8f6;
  text-shadow:
    0 0 4px rgba(0, 0, 0, 0.7),
    0 2px 4px rgba(0, 0, 0, 0.9);
  transform: translateZ(8px);
}

/* Highlighted group - Preserves thickness while glowing */
.cell.active {
  box-shadow:
    0 1px 0 #4a5553,
    0 2px 0 #3c4644,
    0 3px 0 #2e3735,
    0 4px 0 #202826,
    0 5px 0 #121917,
    0 0 30px rgba(255, 238, 170, 0.6),
    0 12px 20px rgba(0, 0, 0, 0.9),
    inset 0 0 0 1px rgba(255, 248, 205, 0.4);
  transform: translateZ(15px) scale(1.04);
  opacity: 1;
  filter: saturate(1.4);
}

/* Extra highlight for current term - Higher lift and stronger glow */
.cell.sub-active {
  box-shadow:
    0 1px 0 #5a6663,
    0 2px 0 #4c5654,
    0 3px 0 #3e4745,
    0 4px 0 #303836,
    0 5px 0 #222927,
    0 0 40px rgba(255, 255, 200, 0.8),
    0 15px 25px rgba(0, 0, 0, 1),
    inset 0 0 0 2px rgba(255, 255, 200, 0.6);
  transform: translateZ(25px) scale(1.08);
  background: radial-gradient(circle at 30% 0%, rgba(255, 255, 230, 0.25), rgba(255, 255, 230, 0.08) 60%, transparent 100%);
}

/* Dimmed non-active cells - Only dims the background/rim, preserving number legibility */
.cell.dim {
  filter: grayscale(0.8) contrast(0.8);
  transform: translateZ(0px);
  box-shadow:
    0 1px 0 #111,
    0 2px 4px rgba(0, 0, 0, 0.5);
}

.cell.dim span {
  opacity: 0.45;
}

.cell.dim::before {
  opacity: 0.1;
}

/* Background color glow layer */
.cell-glow {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0;
  pointer-events: none;
  z-index: 0;
  /* Reset transition for quick showing */
  transition: none;
}

.cell-glow.active {
  opacity: 0.6;
  /* Quick fade animation with minimal delay */
  animation: cellGlowFade 4s ease-out 0.2s forwards;
}

@keyframes cellGlowFade {
  0% { opacity: 0.6; }
  100% { opacity: 0; }
}

/* Color variations matching the chart bar */
.color-0 { background: linear-gradient(135deg, #4a9eff, #2563eb); }
.color-1 { background: linear-gradient(135deg, #f59e0b, #d97706); }
.color-2 { background: linear-gradient(135deg, #10b981, #059669); }
.color-3 { background: linear-gradient(135deg, #ec4899, #db2777); }
.color-4 { background: linear-gradient(135deg, #8b5cf6, #7c3aed); }
.color-5 { background: linear-gradient(135deg, #06b6d4, #0891b2); }

/* Slight hover / active elevation (applies to large cube via wrapper) */
@media (hover: hover) {
  .cube-wrapper-6x6:hover .cube-face-6x6 {
    box-shadow:
      0 28px 50px rgba(0, 0, 0, 0.8),
      0 0 0 1px rgba(255, 255, 255, 0.08);
  }
}

/* Make sure everything fits on small phones */
@media (max-height: 600px) {
  .size-large {
    width: min(78vw, 78vh);
    height: min(78vw, 78vh);
  }
}

/* Ensure wrapper sizes are driven by the grid so all cells stay the same size */
.cube-wrapper.size-large,
.cube-wrapper.size-small {
  width: auto !important;
  height: auto !important;
}

/* Specific font sizes for higher order squares */
.cube-face-8x8 .cell span { font-size: clamp(2.05rem, 6.5vw, 3.0rem); }
.cube-face-9x9 .cell span { font-size: clamp(1.9rem, 6vw, 2.8rem); }
.cube-face-10x10 .cell span { font-size: clamp(1.75rem, 5.5vw, 2.6rem); }
.cube-face-11x11 .cell span { font-size: clamp(1.6rem, 5vw, 2.4rem); }
.cube-face-12x12 .cell span { font-size: clamp(1.45rem, 4.5vw, 2.2rem); }
.cube-face-15x15 .cell span { font-size: clamp(0.75rem, 2.2vw, 1.1rem); }

/* Mobile: shrink numbers inside cells by ~35% */
@media (max-width: 768px) {
  .cell span {
    font-size: clamp(1.43rem, 4.55vw, 2.08rem);
  }
  .cube-face-8x8 .cell span { font-size: clamp(1.25rem, 4.1vw, 1.85rem); }
  .cube-face-9x9 .cell span { font-size: clamp(1.15rem, 3.7vw, 1.7rem); }
}

/* Floating magic constant balls */
.cube-wrapper {
  position: absolute;
}

/* Staggered delays for each magic square's ball */
.cube-wrapper-small { --ball-delay: 0ms; }
.cube-wrapper-medium { --ball-delay: 200ms; }
.cube-wrapper-large { --ball-delay: 400ms; }
.cube-wrapper-6x6 { --ball-delay: 600ms; }

.magic-ball-container {
  position: absolute;
  right: -10px;
  bottom: -10px;
  width: clamp(40px, 11vw, 56px);
  height: clamp(40px, 11vw, 56px);
  animation: magicBallFloat 2600ms ease-in-out infinite alternate;
  animation-delay: var(--ball-delay, 0ms);
  z-index: 10;
  pointer-events: auto; /* Allow dragging via the ball area */
}

.magic-ball {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 20%, #ffffff, #f7e6a8 45%, #d4a83a 75%, #8b5a16 100%);
  box-shadow:
    0 4px 10px rgba(0, 0, 0, 0.8),
    0 0 18px rgba(255, 230, 150, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.2rem, 4vw, 1.6rem);
  color: #3a2603;
  text-shadow:
    0 1px 0 rgba(255, 255, 255, 0.7),
    0 0 6px rgba(255, 255, 255, 0.4);
  transform-origin: center center;
  transition: transform 200ms ease-out;
}

.magic-ball.pulse {
  animation: magicBallPulse 500ms cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes magicBallFloat {
  0% { transform: translateY(0); }
  100% { transform: translateY(-8px); }
}

@keyframes magicBallPulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.6); filter: brightness(1.25); }
  100% { transform: scale(1); }
}

/* App logo in lower-right */
.app-logo {
  position: absolute;
  right: 6%;
  bottom: 7%;
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.45rem 0.9rem 0.45rem 0.6rem;
  border-radius: 999px;
  background: radial-gradient(circle at 0% 0%, rgba(255, 255, 255, 0.06), transparent 55%),
              linear-gradient(135deg, rgba(255, 255, 255, 0.06), rgba(0, 0, 0, 0.8));
  box-shadow:
    0 10px 20px rgba(0, 0, 0, 0.75),
    0 0 0 1px rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: #f9f7e9;
  z-index: 16;
  pointer-events: none;
}

.app-logo-mark {
  flex: 0 0 auto;
  width: 26px;
  height: 26px;
  border-radius: 999px;
  background:
    radial-gradient(circle at 30% 20%, #ffffff, #f7e6a8 40%, #ddb452 72%, #8b5a16 100%);
  box-shadow:
    0 0 12px rgba(255, 230, 150, 0.6),
    0 2px 6px rgba(0, 0, 0, 0.85);
  position: relative;
}

.app-logo-mark::after {
  content: "Σ";
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 800;
  color: #3a2603;
  text-shadow:
    0 1px 0 rgba(255, 255, 255, 0.7),
    0 0 6px rgba(255, 255, 255, 0.4);
}

.app-logo-text {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}

.app-logo-main {
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.app-logo-sub {
  font-size: 0.62rem;
  font-weight: 500;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  opacity: 0.72;
}

/* Navigation links container */
.nav-links {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  align-items: flex-end;
}

.nav-link {
  text-decoration: none;
  color: #f5f2d8;
  font-weight: 700;
  font-size: 0.85rem;
  background: rgba(255, 255, 255, 0.1);
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: background 0.2s;
  white-space: nowrap;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Slightly smaller logo on small screens */
@media (max-width: 768px) {
  .app-logo {
    right: 5%;
    bottom: 6%;
    padding: 0.35rem 0.8rem 0.35rem 0.5rem;
    gap: 0.45rem;
  }

  .app-logo-mark {
    width: 22px;
    height: 22px;
  }

  .app-logo-main {
    font-size: 0.8rem;
  }

  .app-logo-sub {
    font-size: 0.55rem;
  }
}