:root {
  --bun: #e8b86d;
  --bun-dark: #c4893a;
  --bun-light: #f5d9a8;
  --mustard: #f4c430;
  --mustard-dark: #d4a017;
  --ketchup: #c41e3a;
  --ketchup-dark: #8b0000;
  --grill: #3d2914;
  --cream: #fff8ee;
  --ink: #2a1a0a;
  --given: #5c3d1e;
  --conflict: #ff6b6b;
  --select: rgba(244, 196, 48, 0.45);
  --peer: rgba(232, 184, 109, 0.35);
  --shadow: rgba(61, 41, 20, 0.25);
  --radius: 14px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(244, 196, 48, 0.25), transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(196, 30, 58, 0.18), transparent 45%),
    linear-gradient(165deg, var(--bun-light) 0%, var(--cream) 40%, #f0d5a8 100%);
  color: var(--ink);
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1.25rem 1rem 2rem;
  position: relative;
  overflow-x: hidden;
}

.ketchup-drizzle,
.mustard-drizzle {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 0;
  opacity: 0.12;
}

.ketchup-drizzle {
  background: repeating-linear-gradient(
    -12deg,
    transparent,
    transparent 40px,
    var(--ketchup) 40px,
    var(--ketchup) 46px
  );
}

.mustard-drizzle {
  background: repeating-linear-gradient(
    18deg,
    transparent,
    transparent 55px,
    var(--mustard) 55px,
    var(--mustard) 60px
  );
}

.header,
.layout,
.footer {
  position: relative;
  z-index: 1;
}

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

.title {
  margin: 0;
  font-size: clamp(2rem, 6vw, 3rem);
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--ketchup-dark);
  text-shadow: 2px 2px 0 var(--mustard), 0 4px 12px var(--shadow);
}

.title .emoji {
  display: inline-block;
  animation: wiggle 2.5s ease-in-out infinite;
}

.title .emoji:last-child {
  animation-delay: 0.4s;
}

@keyframes wiggle {
  0%,
  100% {
    transform: rotate(-8deg);
  }
  50% {
    transform: rotate(8deg);
  }
}

.tagline {
  margin: 0.4rem 0 0;
  color: var(--grill);
  opacity: 0.85;
  font-size: 0.95rem;
}

.layout {
  display: flex;
  flex-wrap: wrap;
  gap: 1.5rem;
  justify-content: center;
  align-items: flex-start;
  max-width: 920px;
  width: 100%;
}

.board-wrap {
  background: linear-gradient(145deg, var(--bun) 0%, var(--bun-dark) 100%);
  padding: 12px;
  border-radius: calc(var(--radius) + 4px);
  box-shadow:
    0 8px 0 var(--grill),
    0 16px 32px var(--shadow),
    inset 0 2px 0 rgba(255, 255, 255, 0.35);
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  gap: 0;
  width: min(92vw, 420px);
  aspect-ratio: 1;
  background: var(--grill);
  border: 3px solid var(--grill);
  border-radius: 6px;
  overflow: hidden;
  user-select: none;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--cream);
  font-size: clamp(1.1rem, 4.2vw, 1.55rem);
  font-weight: 700;
  cursor: pointer;
  position: relative;
  color: var(--ink);
  border-right: 1px solid rgba(61, 41, 20, 0.2);
  border-bottom: 1px solid rgba(61, 41, 20, 0.2);
  transition: background 0.12s ease;
}

.cell:nth-child(9n) {
  border-right: none;
}

.cell:nth-child(n + 73) {
  border-bottom: none;
}

/* thick box borders */
.cell:nth-child(3n):not(:nth-child(9n)) {
  border-right: 3px solid var(--grill);
}

.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom: 3px solid var(--grill);
}

.cell.given {
  color: var(--given);
  font-weight: 800;
}

.cell.selected {
  background: var(--select);
  outline: 2px solid var(--mustard-dark);
  outline-offset: -2px;
  z-index: 2;
}

.cell.peer {
  background: var(--peer);
}

.cell.same-num {
  background: rgba(196, 30, 58, 0.12);
}

.cell.conflict {
  color: var(--ketchup);
  background: rgba(196, 30, 58, 0.2) !important;
  animation: pulse-conflict 0.45s ease;
}

@keyframes pulse-conflict {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.04);
  }
}

.cell .notes {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  padding: 2px;
  font-size: clamp(0.45rem, 1.8vw, 0.65rem);
  font-weight: 600;
  color: var(--bun-dark);
  line-height: 1;
}

.cell .notes span {
  display: flex;
  align-items: center;
  justify-content: center;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: min(100%, 260px);
  max-width: 280px;
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.pad button {
  aspect-ratio: 1;
  border: none;
  border-radius: 12px;
  font-size: 1.35rem;
  font-weight: 800;
  cursor: pointer;
  background: linear-gradient(180deg, var(--mustard) 0%, var(--mustard-dark) 100%);
  color: var(--grill);
  box-shadow: 0 4px 0 var(--bun-dark);
  transition: transform 0.08s, box-shadow 0.08s;
}

.pad button:hover {
  filter: brightness(1.05);
}

.pad button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--bun-dark);
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s;
}

.btn-primary {
  background: linear-gradient(180deg, var(--ketchup) 0%, var(--ketchup-dark) 100%);
  color: #fff;
  box-shadow: 0 4px 0 #5c0000;
}

.btn-secondary {
  background: linear-gradient(180deg, var(--bun-light) 0%, var(--bun) 100%);
  color: var(--grill);
  box-shadow: 0 3px 0 var(--bun-dark);
}

.btn-secondary[aria-pressed="true"] {
  background: linear-gradient(180deg, var(--mustard) 0%, var(--mustard-dark) 100%);
  box-shadow: 0 3px 0 var(--bun-dark), inset 0 0 0 2px var(--grill);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 transparent;
}

.hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--grill);
  opacity: 0.9;
}

.legend {
  margin: 0;
  padding-left: 1.1rem;
  font-size: 0.8rem;
  color: var(--grill);
  opacity: 0.85;
  line-height: 1.5;
}

.footer {
  margin-top: 2rem;
  font-size: 0.8rem;
  opacity: 0.7;
  color: var(--grill);
}

.fx-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 100;
}

.board.won .cell:not(.conflict) {
  animation: win-glow 0.8s ease infinite alternate;
}

@keyframes win-glow {
  from {
    box-shadow: inset 0 0 8px rgba(244, 196, 48, 0.3);
  }
  to {
    box-shadow: inset 0 0 16px rgba(196, 30, 58, 0.25);
  }
}

@media (max-width: 640px) {
  .layout {
    flex-direction: column;
    align-items: center;
  }

  .controls {
    width: 100%;
    max-width: min(92vw, 420px);
  }

  .pad {
    grid-template-columns: repeat(9, 1fr);
  }

  .pad button {
    aspect-ratio: auto;
    padding: 0.65rem 0;
    font-size: 1.1rem;
  }

  .actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .actions .btn {
    flex: 1 1 auto;
  }
}
