*, 
*::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: 6%;
  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: 16%;
  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: 24%;
  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;
}

.chart-segment:nth-child(6n+1) {
  background: linear-gradient(135deg, #4a9eff, #2563eb);
}

.chart-segment:nth-child(6n+2) {
  background: linear-gradient(135deg, #f59e0b, #d97706);
}

.chart-segment:nth-child(6n+3) {
  background: linear-gradient(135deg, #10b981, #059669);
}

.chart-segment:nth-child(6n+4) {
  background: linear-gradient(135deg, #ec4899, #db2777);
}

.chart-segment:nth-child(6n+5) {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
}

.chart-segment:nth-child(6n+6) {
  background: linear-gradient(135deg, #06b6d4, #0891b2);
}

@media (max-width: 768px) {
  .chart-overlay {
    top: 22%;
    width: min(88vw, 500px);
  }
  
  .chart-container {
    height: 36px;
  }
  
  .chart-segment {
    font-size: 0.7rem;
  }
}

/* Start button */
.start-button {
  position: absolute;
  bottom: 8%;
  left: 50%;
  transform: translateX(-50%);
  padding: 0.9rem 2.8rem;
  font-size: 1.3rem;
  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;
  z-index: 15;
}

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

/* 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 {
  box-shadow:
    0 26px 46px rgba(0, 0, 0, 0.9),
    0 0 0 2px rgba(255, 238, 170, 0.9);
}

/* 3x3, 4x4, 5x5 and 6x6 faces share the same styling base */
.cube-face,
.cube-face-4x4,
.cube-face-5x5,
.cube-face-6x6 {
  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));
}

/* Slight highlight rim */
.cube-face::before,
.cube-face-4x4::before,
.cube-face-5x5::before,
.cube-face-6x6::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 */
.cell {
  position: relative;
  background: linear-gradient(145deg, #252e2c, #101615);
  border-radius: 6px;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.08),
    0 4px 8px rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  transform-style: preserve-3d;
  transition:
    transform 200ms ease-out,
    box-shadow 200ms ease-out,
    opacity 200ms ease-out,
    filter 200ms ease-out;
}

/* Subtle extruded effect */
.cell::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0.12), transparent 45%);
  mix-blend-mode: screen;
  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 */
.cell.active {
  box-shadow:
    inset 0 0 0 1px rgba(255, 248, 205, 0.9),
    0 0 24px rgba(255, 238, 170, 0.75),
    0 8px 16px rgba(0, 0, 0, 0.9);
  transform: translateZ(10px) scale(1.03);
  opacity: 1;
  filter: saturate(1.4);
}

/* Extra highlight for the current term in the equation */
.cell.sub-active {
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 200, 0.95),
    0 0 32px rgba(255, 255, 200, 0.95),
    0 10px 18px rgba(0, 0, 0, 1);
  transform: translateZ(14px) scale(1.06);
  background: radial-gradient(circle at 30% 0%, rgba(255, 255, 230, 0.22), rgba(255, 255, 230, 0.05) 55%, transparent 100%);
}

/* Dimmed non-active cells */
.cell.dim {
  opacity: 0.22;
  filter: grayscale(0.5);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.03),
    0 2px 4px rgba(0, 0, 0, 0.6);
}

/* 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;
}

/* Mobile: shrink numbers inside cells by ~35% */
@media (max-width: 768px) {
  .cell span {
    font-size: clamp(1.43rem, 4.55vw, 2.08rem);
  }
}

/* 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 {
  position: absolute;
  right: -10px;
  bottom: -10px;
  width: clamp(40px, 11vw, 56px);
  height: clamp(40px, 11vw, 56px);
  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: translateY(0);
  animation: magicBallFloat 2600ms ease-in-out infinite alternate;
  animation-delay: var(--ball-delay, 0ms);
  pointer-events: none;
}

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

/* 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;
}

/* 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;
  }
}