:root {
  color-scheme: light;
  --page: #f7f7f4;
  --surface: #ffffff;
  --ink: #181a1c;
  --muted: #92979c;
  --line: #dedfdf;
  --tile-gray: #e6e8e9;
  --tile-gray-edge: #cfd2d4;
  --tile-green: #68a45e;
  --tile-green-edge: #4f8648;
  --tile-yellow: #e5b83f;
  --tile-yellow-edge: #c79524;
  --focus: #202428;
  --shadow: 0 20px 60px rgb(20 24 28 / 8%);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--page);
  font-family: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  min-width: 320px;
  min-height: 100dvh;
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 35%, rgb(255 255 255 / 90%) 0 23rem, transparent 42rem),
    var(--page);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  color: inherit;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 0.75rem;
  left: 0.75rem;
  padding: 0.7rem 1rem;
  border-radius: 0.7rem;
  color: white;
  background: var(--ink);
  transform: translateY(-180%);
  transition: transform 160ms ease;
}

.skip-link:focus {
  transform: translateY(0);
}

.topbar {
  width: min(100% - 1.25rem, 46rem);
  min-height: 6.5rem;
  margin: 0 auto;
  padding: max(1rem, env(safe-area-inset-top)) 0 0.75rem;
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.75rem;
}

.topbar-actions {
  display: flex;
  gap: 0.45rem;
}

.icon-button,
.close-button {
  border: 0;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 140ms ease, background-color 140ms ease;
}

.icon-button {
  width: 3.1rem;
  height: 3.1rem;
  border-radius: 0.9rem;
  background: #eceeed;
}

.icon-button:hover {
  background: #e3e5e4;
}

.icon-button:active,
.close-button:active {
  transform: scale(0.94);
}

.icon-button svg {
  width: 1.65rem;
  height: 1.65rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand {
  min-width: 0;
  text-align: center;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.34rem, 5.6vw, 2rem);
  line-height: 1;
  letter-spacing: 0.14em;
  font-weight: 900;
}

.brand p {
  margin: 0.48rem 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: clamp(0.64rem, 2.4vw, 0.78rem);
  line-height: 1;
  letter-spacing: 0.15em;
  font-weight: 800;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.game-shell {
  width: 100%;
  flex: 1;
  display: grid;
  place-items: center;
  padding: 1.5rem 0.85rem 2.25rem;
}

.game-card {
  width: min(100%, 35rem);
  text-align: center;
}

.game-heading {
  margin-bottom: clamp(1.25rem, 4vh, 2.25rem);
}

.eyebrow,
.dialog-kicker {
  margin: 0;
  color: var(--muted);
  font-size: 0.74rem;
  line-height: 1.2;
  letter-spacing: 0.18em;
  font-weight: 900;
}

.instruction {
  margin: 0.45rem 0 0;
  color: #656a6e;
  font-size: 0.94rem;
  line-height: 1.45;
}

.waffle-wrap {
  width: min(100%, 30rem);
  margin: 0 auto;
  padding: 0.55rem;
  border-radius: 2rem;
  background: rgb(255 255 255 / 58%);
}

.waffle-grid {
  --gap: clamp(0.42rem, 2vw, 0.7rem);
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
  gap: var(--gap);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.tile {
  position: relative;
  width: 100%;
  min-width: 0;
  padding: 0 0 0.24rem;
  border: 0;
  border-radius: clamp(0.68rem, 2.6vw, 1rem);
  color: var(--ink);
  background: var(--tile-gray);
  box-shadow: inset 0 -0.36rem 0 var(--tile-gray-edge), 0 0.18rem 0.22rem rgb(25 28 30 / 5%);
  cursor: grab;
  display: grid;
  place-items: center;
  font-size: clamp(1.85rem, 10vw, 3.35rem);
  line-height: 1;
  font-weight: 800;
  text-transform: uppercase;
  transition: transform 150ms ease, box-shadow 150ms ease, filter 150ms ease, background-color 220ms ease;
  -webkit-tap-highlight-color: transparent;
}

.tile:not(:disabled) {
  touch-action: none;
}

.tile:not(:disabled):hover {
  filter: brightness(0.98);
  transform: translateY(-0.08rem);
}

.tile:not(:disabled):active {
  cursor: grabbing;
  transform: translateY(0.12rem) scale(0.97);
}

.tile--green {
  color: #fff;
  background: var(--tile-green);
  box-shadow: inset 0 -0.36rem 0 var(--tile-green-edge), 0 0.18rem 0.22rem rgb(25 28 30 / 7%);
  cursor: default;
}

.tile--yellow {
  color: #fff;
  background: var(--tile-yellow);
  box-shadow: inset 0 -0.36rem 0 var(--tile-yellow-edge), 0 0.18rem 0.22rem rgb(25 28 30 / 7%);
}

.tile--selected {
  z-index: 2;
  outline: 0.25rem solid var(--focus);
  outline-offset: 0.14rem;
  transform: translateY(-0.15rem) scale(1.035);
}

.tile--dragging {
  z-index: 3;
  opacity: 0.12;
  filter: saturate(0.5);
  transform: scale(0.94);
}

.tile--target {
  outline: 0.25rem dashed var(--focus);
  outline-offset: 0.1rem;
  transform: scale(1.05);
}

.tile--pop {
  animation: tile-pop 260ms ease-out;
}

.drag-ghost {
  position: fixed !important;
  z-index: 1000;
  margin: 0;
  opacity: 0.98;
  pointer-events: none;
  cursor: grabbing;
  transform: scale(1.06) rotate(-1deg);
  transition: none !important;
  will-change: left, top, transform;
  box-shadow: inset 0 -0.36rem 0 var(--tile-gray-edge), 0 1rem 2.2rem rgb(20 24 28 / 24%);
}

.drag-ghost.tile--yellow {
  box-shadow: inset 0 -0.36rem 0 var(--tile-yellow-edge), 0 1rem 2.2rem rgb(20 24 28 / 24%);
}

body.drag-active,
body.drag-active * {
  cursor: grabbing !important;
}

.tile:focus-visible,
.icon-button:focus-visible,
.close-button:focus-visible,
.primary-button:focus-visible,
.secondary-button:focus-visible,
.menu-row:focus-visible,
.footer button:focus-visible,
.setting-row input:focus-visible {
  outline: 0.2rem solid var(--focus);
  outline-offset: 0.18rem;
}

.tile:disabled {
  opacity: 1;
}

.tile-hole {
  pointer-events: none;
}

@keyframes tile-pop {
  0% { transform: scale(0.88); }
  60% { transform: scale(1.07); }
  100% { transform: scale(1); }
}

.moves {
  min-height: 3.4rem;
  margin-top: clamp(1.45rem, 4vh, 2.35rem);
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.7rem;
}

.moves strong {
  font-size: clamp(2.1rem, 9vw, 3rem);
  line-height: 1;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.moves span {
  color: var(--muted);
  font-size: clamp(1rem, 4.2vw, 1.35rem);
  letter-spacing: 0.08em;
  font-weight: 700;
  text-transform: uppercase;
}

.game-note {
  min-height: 1.4rem;
  margin: 0.1rem 0 0;
  color: #6f7478;
  font-size: 0.9rem;
}

.footer {
  width: min(100% - 2rem, 42rem);
  margin: 0 auto;
  padding: 0 0 max(1.15rem, env(safe-area-inset-bottom));
  color: #858a8e;
  font-size: 0.78rem;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.45rem;
}

.footer button {
  padding: 0;
  border: 0;
  color: #606569;
  background: transparent;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 0.16rem;
}

.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: max(1.5rem, env(safe-area-inset-bottom));
  max-width: calc(100% - 2rem);
  padding: 0.75rem 1rem;
  border-radius: 999px;
  color: #fff;
  background: #202326;
  box-shadow: 0 0.8rem 2.2rem rgb(0 0 0 / 18%);
  font-size: 0.88rem;
  font-weight: 750;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 1rem);
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}

.toast--visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.dialog {
  width: min(calc(100% - 1.5rem), 31rem);
  max-height: min(90dvh, 48rem);
  padding: 0;
  border: 0;
  border-radius: 1.45rem;
  color: var(--ink);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: auto;
}

.dialog::backdrop {
  background: rgb(17 20 23 / 48%);
  backdrop-filter: blur(0.2rem);
}

.dialog-panel {
  padding: clamp(1.25rem, 5vw, 2rem);
}

.dialog-head {
  margin-bottom: 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
}

.dialog h2 {
  margin: 0.3rem 0 0;
  font-size: clamp(1.65rem, 7vw, 2.2rem);
  line-height: 1.08;
}

.close-button {
  width: 2.55rem;
  height: 2.55rem;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #eff0f0;
  font-family: system-ui, sans-serif;
  font-size: 1.65rem;
  font-weight: 400;
  line-height: 1;
}

.lead {
  margin: 0;
  color: #555b60;
  font-size: 1rem;
  line-height: 1.6;
}

.steps {
  margin: 1.15rem 0 1.35rem;
  padding-left: 1.35rem;
  color: #353a3e;
}

.steps li {
  padding-left: 0.25rem;
  line-height: 1.52;
}

.steps li + li {
  margin-top: 0.7rem;
}

.legend {
  padding: 0.45rem 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.legend-row {
  min-height: 4.6rem;
  display: grid;
  grid-template-columns: 3rem 1fr;
  align-items: center;
  gap: 0.9rem;
}

.mini-tile {
  width: 2.8rem;
  height: 2.8rem;
  padding-bottom: 0.15rem;
  border-radius: 0.58rem;
  display: grid;
  place-items: center;
  color: #fff;
  font-size: 1.35rem;
  font-weight: 850;
}

.mini-tile--green { background: var(--tile-green); box-shadow: inset 0 -0.24rem 0 var(--tile-green-edge); }
.mini-tile--yellow { background: var(--tile-yellow); box-shadow: inset 0 -0.24rem 0 var(--tile-yellow-edge); }
.mini-tile--gray { color: var(--ink); background: var(--tile-gray); box-shadow: inset 0 -0.24rem 0 var(--tile-gray-edge); }

.legend-row strong,
.legend-row small,
.menu-row strong,
.menu-row small,
.setting-row strong,
.setting-row small {
  display: block;
}

.legend-row small,
.menu-row small,
.setting-row small {
  margin-top: 0.18rem;
  color: #767b80;
  font-size: 0.82rem;
  line-height: 1.35;
}

.primary-button,
.secondary-button {
  width: 100%;
  min-height: 3.2rem;
  border-radius: 0.85rem;
  cursor: pointer;
  font-weight: 850;
  transition: transform 140ms ease, filter 140ms ease;
}

.primary-button {
  margin-top: 1.25rem;
  border: 0;
  color: white;
  background: var(--ink);
}

.secondary-button {
  margin-top: 0.7rem;
  border: 1px solid var(--line);
  color: var(--ink);
  background: #f1f2f2;
}

.primary-button:hover,
.secondary-button:hover {
  filter: brightness(0.96);
}

.primary-button:active,
.secondary-button:active {
  transform: scale(0.98);
}

.menu-list {
  border-top: 1px solid var(--line);
}

.menu-row {
  width: 100%;
  min-height: 4.8rem;
  padding: 0.7rem 0;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  cursor: pointer;
  display: grid;
  grid-template-columns: 2.75rem 1fr auto;
  align-items: center;
  gap: 0.8rem;
  text-align: left;
}

.menu-row:hover {
  background: #fafafa;
}

.menu-row-icon {
  width: 2.55rem;
  height: 2.55rem;
  border-radius: 0.7rem;
  background: #eceeed;
  display: grid;
  place-items: center;
  font-size: 1.35rem;
  font-weight: 900;
}

.chart-icon {
  font-size: 1.05rem;
}

.setting-row {
  min-height: 5rem;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  cursor: pointer;
}

.setting-row input {
  appearance: none;
  width: 3.25rem;
  height: 1.85rem;
  flex: 0 0 auto;
  border: 0.14rem solid #babec1;
  border-radius: 999px;
  background: #d7d9da;
  cursor: pointer;
  position: relative;
  transition: background 160ms ease, border-color 160ms ease;
}

.setting-row input::after {
  content: "";
  position: absolute;
  top: 0.15rem;
  left: 0.15rem;
  width: 1.28rem;
  height: 1.28rem;
  border-radius: 50%;
  background: white;
  box-shadow: 0 0.12rem 0.35rem rgb(0 0 0 / 20%);
  transition: transform 160ms ease;
}

.setting-row input:checked {
  border-color: var(--tile-green-edge);
  background: var(--tile-green);
}

.setting-row input:checked::after {
  transform: translateX(1.4rem);
}

.privacy-note,
.empty-stats {
  margin: 1rem 0 0;
  color: #7a7f83;
  font-size: 0.78rem;
  line-height: 1.5;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.45rem;
}

.stats-grid div {
  min-width: 0;
  padding: 0.8rem 0.2rem;
  border-radius: 0.8rem;
  background: #f1f2f2;
  text-align: center;
}

.stats-grid strong,
.stats-grid span {
  display: block;
}

.stats-grid strong {
  font-size: clamp(1.35rem, 6vw, 1.85rem);
  font-variant-numeric: tabular-nums;
}

.stats-grid span {
  margin-top: 0.2rem;
  color: #73787c;
  font-size: 0.67rem;
  line-height: 1.15;
}

.average-row {
  margin-top: 0.8rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 0.85rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  color: #5f6569;
  font-size: 0.86rem;
}

.average-row strong {
  color: var(--ink);
  font-size: 1.35rem;
}

.result-panel {
  position: relative;
  text-align: center;
}

.result-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
}

.result-mark {
  width: 5rem;
  height: 5rem;
  margin: 0.4rem auto 1.1rem;
  padding-bottom: 0.25rem;
  border-radius: 1rem;
  color: #fff;
  background: var(--tile-green);
  box-shadow: inset 0 -0.38rem 0 var(--tile-green-edge);
  display: grid;
  place-items: center;
  font-size: 2.6rem;
  font-weight: 900;
}

.result-dialog h2 {
  margin-top: 0.45rem;
}

.stars {
  min-height: 2.1rem;
  margin: 0.9rem 0 0.75rem;
  color: var(--tile-yellow);
  font-family: system-ui, sans-serif;
  font-size: 1.8rem;
  letter-spacing: 0.12rem;
}

.result-copy {
  max-width: 24rem;
  margin: 0 auto;
  color: #60666a;
  line-height: 1.55;
}

.solution-words {
  margin-top: 1rem;
  padding: 0.9rem;
  border-radius: 0.85rem;
  background: #f1f2f2;
  font-size: 0.9rem;
  line-height: 1.7;
  letter-spacing: 0.06em;
  font-weight: 850;
}

.result-actions {
  margin-top: 0.25rem;
}

body.high-contrast {
  --tile-green: #3280c5;
  --tile-green-edge: #1c609a;
  --tile-yellow: #e58a32;
  --tile-yellow-edge: #bd681c;
}

@media (max-width: 430px) {
  .topbar {
    width: calc(100% - 1rem);
    min-height: 5.55rem;
    gap: 0.42rem;
  }

  .icon-button {
    width: 2.7rem;
    height: 2.7rem;
    border-radius: 0.78rem;
  }

  .icon-button svg {
    width: 1.45rem;
    height: 1.45rem;
  }

  .topbar-actions {
    gap: 0.3rem;
  }

  .game-shell {
    padding-top: 1.15rem;
  }

  .instruction {
    font-size: 0.87rem;
  }

  .waffle-wrap {
    padding: 0.38rem;
    border-radius: 1.35rem;
  }

  .footer > span:first-child,
  .footer > span:nth-child(2) {
    display: none;
  }
}

@media (max-height: 720px) and (orientation: landscape) {
  .topbar {
    min-height: 4.6rem;
    padding-top: 0.45rem;
  }

  .game-shell {
    padding-top: 0.6rem;
    padding-bottom: 0.9rem;
  }

  .game-heading {
    margin-bottom: 0.65rem;
  }

  .waffle-wrap {
    width: min(64vh, 27rem);
  }

  .moves {
    margin-top: 0.8rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
