:root {
  color-scheme: light;
  --blue: #0a78bd;
  --blue-bright: #23a6e8;
  --blue-dark: #07517f;
  --blue-deep: #032c4d;
  --ink: #102b40;
  --paper: #ffffff;
  --surface: #f8fcff;
  --surface-alt: #eaf6fd;
  --grid: #b7d7e9;
  --guide: #8bb7cf;
  --correct: #12834d;
  --correct-soft: #e4f8ed;
  --error: #c52c3a;
  --error-soft: #fff0f2;
  --focus: #58c8ff;
  --panel-border: rgb(255 255 255 / 20%);
  --panel-shadow: 0 26px 64px rgb(1 25 46 / 32%);
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  min-height: 100%;
  background: var(--blue-deep);
}

body {
  min-height: 100svh;
  margin: 0;
  background:
    radial-gradient(circle at 18% 12%, rgb(46 177 235 / 38%), transparent 31rem),
    radial-gradient(circle at 84% 88%, rgb(5 68 117 / 58%), transparent 34rem),
    linear-gradient(145deg, #086faf 0%, #075d96 46%, #03385f 100%);
  background-attachment: fixed;
  color: var(--paper);
  font-family: Aptos, "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    linear-gradient(120deg, rgb(255 255 255 / 4%) 0 1px, transparent 1px 100%),
    linear-gradient(30deg, rgb(255 255 255 / 3%) 0 1px, transparent 1px 100%);
  background-size: 72px 72px;
  content: "";
  mask-image: linear-gradient(to bottom, black, transparent 80%);
  pointer-events: none;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.game-shell {
  display: grid;
  width: min(94vw, 720px);
  min-height: 100svh;
  margin: 0 auto;
  padding: clamp(18px, 5vh, 52px) 0 20px;
  align-content: center;
}

#game-form {
  padding: clamp(16px, 3.5vw, 28px);
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  background: linear-gradient(145deg, rgb(5 60 100 / 42%), rgb(2 38 68 / 24%));
  box-shadow: var(--panel-shadow);
  backdrop-filter: blur(12px);
}

.puzzle {
  display: grid;
  gap: clamp(20px, 4vh, 32px);
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--board-columns), minmax(0, 1fr));
  width: 100%;
  border: 1px solid var(--grid);
  border-radius: 16px;
  overflow: hidden;
  background: var(--paper);
  box-shadow:
    0 16px 34px rgb(0 25 45 / 18%),
    inset 0 1px 0 rgb(255 255 255 / 80%);
  color: var(--ink);
}

.target-card {
  min-width: 0;
  background: var(--surface);
}

.target-card:not(:nth-child(4n)) {
  border-right: 1px solid var(--grid);
}

.target-card:nth-child(-n + 12) {
  border-bottom: 1px solid var(--grid);
}

.target-value {
  display: grid;
  min-height: clamp(48px, 9vw, 76px);
  margin: 0;
  place-items: center;
  background: linear-gradient(180deg, var(--paper), var(--surface));
  color: #0b3150;
  font-size: clamp(1.55rem, 4.8vw, 2.4rem);
  font-weight: 650;
  letter-spacing: -0.035em;
  line-height: 1;
}

.equation {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
}

.answer-input {
  width: 100%;
  min-width: 0;
  height: clamp(35px, 6vw, 49px);
  border: 0;
  border-top: 1px dashed var(--guide);
  border-radius: 0;
  padding: 2px;
  background: var(--surface-alt);
  color: var(--ink);
  text-align: center;
  font-size: clamp(1rem, 3.3vw, 1.45rem);
  font-weight: 600;
  line-height: 1;
  appearance: none;
  transition:
    background-color 140ms ease,
    box-shadow 140ms ease;
}

.answer-input + .answer-input {
  border-left: 1px dashed var(--guide);
}

.answer-input:hover,
.strip-input:hover {
  background: #def1fc;
}

.answer-input:focus,
.strip-input:focus {
  position: relative;
  z-index: 1;
  outline: none;
  background: #f4fbff;
  box-shadow: inset 0 0 0 3px var(--focus);
}

.check-button:focus-visible,
.seed-button:focus-visible,
.mobile-keypad-key:focus-visible,
.mobile-keypad-hide:focus-visible,
.easy-mode input:focus-visible + .switch-track {
  position: relative;
  z-index: 1;
  outline: 3px solid #8cddff;
  outline-offset: 3px;
}

.answer-input.is-correct,
.strip-input.is-correct {
  position: relative;
  z-index: 2;
  background: var(--correct-soft);
  box-shadow: inset 0 0 0 2px var(--correct);
}

.answer-input.is-error,
.strip-input.is-error {
  position: relative;
  z-index: 2;
  background: var(--error-soft);
  box-shadow: inset 0 0 0 2px var(--error);
}

.answer-input.is-correct:focus,
.strip-input.is-correct:focus {
  outline: none;
  box-shadow: inset 0 0 0 4px var(--correct);
}

.answer-input.is-error:focus,
.strip-input.is-error:focus {
  outline: none;
  box-shadow: inset 0 0 0 4px var(--error);
}

.number-strip {
  display: grid;
  grid-template-columns: repeat(var(--strip-columns), minmax(0, 1fr));
  width: 100%;
  border: 1px solid var(--grid);
  border-radius: 12px;
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 10px 24px rgb(0 25 45 / 14%);
  color: var(--ink);
}

.number-token {
  display: grid;
  min-width: 0;
  min-height: clamp(31px, 5.5vw, 43px);
  place-items: center;
  background: linear-gradient(180deg, var(--paper), var(--surface));
  color: #174261;
  font-size: clamp(0.6rem, 2.3vw, 1rem);
  font-weight: 600;
}

.number-token:not(:last-child) {
  border-right: 1px solid var(--grid);
}

.strip-input {
  width: 100%;
  height: 100%;
  min-width: 0;
  border: 0;
  border-radius: 0;
  padding: 2px;
  background: var(--surface-alt);
  color: var(--ink);
  text-align: center;
  font-weight: 600;
  appearance: none;
  transition:
    background-color 140ms ease,
    box-shadow 140ms ease;
}

.game-status {
  min-height: 1.5em;
  margin: 18px 0 10px;
  text-align: center;
  color: #eefaff;
  font-size: 1rem;
  font-weight: 700;
  text-shadow: 0 1px 8px rgb(0 31 55 / 40%);
}

.controls {
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: center;
}

.easy-mode {
  display: inline-flex;
  min-height: 44px;
  gap: 10px;
  align-items: center;
  border: 1px solid rgb(255 255 255 / 18%);
  border-radius: 12px;
  padding: 8px 11px 8px 14px;
  background: rgb(255 255 255 / 9%);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 8%);
  font-size: 0.94rem;
  font-weight: 600;
  user-select: none;
}

.switch {
  position: relative;
  display: inline-block;
  width: 45px;
  height: 25px;
  flex: 0 0 auto;
}

.switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.switch-track {
  position: absolute;
  inset: 0;
  border: 1px solid rgb(255 255 255 / 38%);
  border-radius: 999px;
  background: rgb(1 32 56 / 38%);
  box-shadow: inset 0 2px 4px rgb(0 28 50 / 28%);
  transition:
    background-color 160ms ease,
    border-color 160ms ease;
}

.switch-track::after {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: var(--paper);
  box-shadow: 0 2px 5px rgb(0 24 43 / 38%);
  content: "";
  transition: transform 160ms ease;
}

.switch input:checked + .switch-track {
  border-color: #6bd4ff;
  background: var(--blue-bright);
}

.switch input:checked + .switch-track::after {
  transform: translateX(20px);
}

.check-button {
  min-height: 44px;
  border: 1px solid rgb(255 255 255 / 72%);
  border-radius: 12px;
  padding: 9px 17px;
  background: linear-gradient(180deg, #ffffff, #e9f7ff);
  box-shadow:
    0 8px 18px rgb(0 24 43 / 20%),
    inset 0 1px 0 rgb(255 255 255 / 90%);
  color: var(--blue-dark);
  font-weight: 750;
  transition:
    box-shadow 140ms ease,
    transform 140ms ease;
}

.check-button:hover {
  box-shadow:
    0 11px 22px rgb(0 24 43 / 28%),
    inset 0 1px 0 rgb(255 255 255 / 90%);
  transform: translateY(-1px);
}

.check-button:active {
  box-shadow: 0 4px 10px rgb(0 24 43 / 20%);
  transform: translateY(1px);
}

.seed-button {
  justify-self: center;
  min-width: 18ch;
  max-width: 100%;
  margin-top: 16px;
  border: 1px solid rgb(255 255 255 / 12%);
  border-radius: 999px;
  padding: 6px 12px;
  background: rgb(2 38 66 / 18%);
  color: #def5ff;
  overflow-wrap: anywhere;
  font-family: ui-monospace, "Cascadia Code", "SFMono-Regular", Consolas, monospace;
  font-size: 0.74rem;
  letter-spacing: 0.015em;
  opacity: 0.78;
  transition:
    background-color 140ms ease,
    opacity 140ms ease;
}

.seed-button:hover,
.seed-button:focus-visible {
  background: rgb(255 255 255 / 9%);
  opacity: 1;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  clip: rect(0, 0, 0, 0);
  overflow: hidden;
  white-space: nowrap;
}

noscript {
  display: block;
  padding: 20px;
  text-align: center;
}

.mobile-keypad {
  display: none;
}

@media (max-width: 540px) {
  .game-shell {
    width: calc(100vw - 8px);
    padding: 4px 0 10px;
    align-content: start;
  }

  #game-form {
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
  }

  .puzzle {
    gap: 14px;
  }

  .board {
    border-radius: 10px;
  }

  .target-value {
    min-height: 56px;
  }

  .answer-input {
    height: 46px;
    font-size: 1rem;
  }

  .number-token {
    min-height: 38px;
  }

  .game-status {
    margin: 12px 0 8px;
  }

  .controls {
    gap: 9px;
  }

  .easy-mode {
    min-height: 42px;
    padding-inline: 10px;
    font-size: 0.87rem;
  }

  .check-button {
    min-height: 42px;
    padding-inline: 12px;
    font-size: 0.87rem;
  }

  .seed-button {
    margin-top: 10px;
  }
}

@media (max-width: 767px) and (pointer: coarse) {
  body.mobile-keypad-open .game-shell {
    padding-bottom: calc(var(--mobile-keypad-height, 306px) + 20px);
  }

  .mobile-keypad {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 20;
    display: block;
    padding:
      8px max(8px, env(safe-area-inset-right))
      calc(9px + env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
    border-top: 1px solid rgb(127 216 255 / 48%);
    border-radius: 16px 16px 0 0;
    background:
      radial-gradient(circle at 18% 0%, rgb(37 166 232 / 22%), transparent 17rem),
      linear-gradient(155deg, #064d7c, #033454);
    box-shadow: 0 -14px 36px rgb(0 22 40 / 34%);
    color: var(--paper);
    pointer-events: none;
    transform: translateY(105%);
    transition:
      transform 220ms ease,
      visibility 220ms;
    visibility: hidden;
  }

  .mobile-keypad.is-open {
    pointer-events: auto;
    transform: translateY(0);
    visibility: visible;
  }

  .mobile-keypad-toolbar,
  .mobile-keypad-layout {
    width: min(100%, 540px);
    margin-inline: auto;
  }

  .mobile-keypad-toolbar {
    display: flex;
    min-height: 38px;
    align-items: center;
    justify-content: flex-end;
  }

  .mobile-keypad-hide {
    display: grid;
    width: 32px;
    height: 32px;
    border: 1px solid rgb(157 220 247 / 52%);
    border-radius: 7px;
    padding: 0;
    place-items: center;
    background: rgb(234 248 255 / 10%);
    box-shadow: inset 0 1px 0 rgb(255 255 255 / 10%);
    color: #e7f8ff;
    font-size: 1.2rem;
    line-height: 1;
  }

  .mobile-keypad-layout {
    gap: 7px;
  }

  .mobile-keypad-layout[hidden] {
    display: none;
  }

  .mobile-keypad-numbers {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .mobile-keypad-operators {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding-block: 5px 8px;
  }

  .mobile-keypad-key {
    min-height: 51px;
    border: 1px solid #b7ddef;
    border-radius: 8px;
    padding: 4px;
    background: linear-gradient(180deg, #ffffff, #e8f6fd);
    box-shadow:
      0 3px 0 #9bc9df,
      0 6px 12px rgb(0 22 39 / 18%);
    color: #0a3f62;
    font-size: 1.4rem;
    font-weight: 650;
    line-height: 1;
    touch-action: manipulation;
    user-select: none;
  }

  .mobile-keypad-key:active {
    background: #d7eef9;
    box-shadow:
      0 1px 0 #8abbd2,
      0 2px 5px rgb(0 22 39 / 16%);
    transform: translateY(2px);
  }

  .mobile-keypad-operators .mobile-keypad-key {
    border-color: #49b8e9;
    background: linear-gradient(180deg, #209fda, #0a78b8);
    box-shadow:
      0 3px 0 #045788,
      0 6px 12px rgb(0 22 39 / 22%);
    color: var(--paper);
    font-size: 1.75rem;
  }

  .mobile-keypad-delete {
    border-color: #86bdd8;
    background: linear-gradient(180deg, #d8eef9, #c0e0f0);
    box-shadow:
      0 3px 0 #82b3ca,
      0 6px 12px rgb(0 22 39 / 18%);
    font-size: 1.25rem;
  }

  .mobile-keypad-spacer {
    min-height: 51px;
  }
}

@media (max-width: 360px) {
  .controls {
    gap: 6px;
  }

  .easy-mode {
    padding-inline: 8px;
  }

  .check-button {
    padding-inline: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .answer-input,
  .strip-input,
  .check-button,
  .seed-button,
  .switch-track,
  .switch-track::after,
  .mobile-keypad {
    transition: none;
  }
}
