:root {
  color-scheme: light;
  --rain-glass: #d8e7e5;
  --rain-light: #f2f7f5;
  --deep-grout: #173b3f;
  --deep-grout-soft: #245158;
  --pool-tile: #2c6d78;
  --jade: #3d8b62;
  --celadon: #a8cf83;
  --pomegranate: #d65743;
  --ink: #152624;
  --muted: #486663;
  --line: #a9c3bd;
  --focus: #f4d36b;
  --z-topbar: 10;
  --z-dropdown: 20;
  --z-overlay: 30;
  font-family:
    "Avenir Next",
    "Noto Sans TC",
    "Noto Sans SC",
    "Noto Sans JP",
    "Noto Sans KR",
    "Noto Sans Thai",
    "Segoe UI",
    sans-serif;
}

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

html {
  min-width: 0;
  min-height: 100%;
  background: var(--rain-glass);
}

body {
  min-width: 0;
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  overflow-x: hidden;
  background: var(--rain-glass);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
select {
  font: inherit;
}

button,
a,
summary,
select,
canvas {
  -webkit-tap-highlight-color: transparent;
}

button {
  touch-action: manipulation;
}

button:focus-visible,
a:focus-visible,
summary:focus-visible,
select:focus-visible,
canvas:focus-visible {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

.skip-link {
  position: fixed;
  top: 8px;
  left: 50%;
  z-index: calc(var(--z-dropdown) + 1);
  min-height: 44px;
  padding: 11px 18px;
  border-radius: 10px;
  background: var(--deep-grout);
  color: #fff;
  text-decoration: none;
  transform: translate(-50%, -160%);
  transition: transform 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

.skip-link:focus {
  transform: translate(-50%, 0);
}

.app-shell {
  width: 100%;
  min-height: 100vh;
  min-height: 100svh;
  padding: 0 24px 24px;
}

.topbar {
  position: relative;
  z-index: var(--z-topbar);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  width: min(100%, 1120px);
  min-height: 68px;
  margin: 0 auto;
}

.back-link,
.settings > summary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 8px 13px;
  border: 2px solid var(--deep-grout);
  border-radius: 10px;
  background: transparent;
  color: var(--deep-grout);
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
}

.back-link {
  justify-self: start;
}

.back-link:hover,
.settings > summary:hover {
  background: rgba(255, 255, 255, 0.48);
}

.back-arrow,
.settings-icon {
  font-size: 1.1rem;
  line-height: 1;
}

.garden-mark {
  position: relative;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--deep-grout);
}

.garden-mark::before {
  position: absolute;
  inset: 9px;
  border: 7px solid var(--celadon);
  border-right-color: transparent;
  border-radius: 50%;
  content: "";
  transform: rotate(-28deg);
}

.garden-mark::after {
  position: absolute;
  top: 8px;
  right: 7px;
  width: 10px;
  height: 10px;
  border: 2px solid var(--rain-light);
  border-radius: 50%;
  background: var(--pomegranate);
  content: "";
}

.settings {
  position: relative;
  justify-self: end;
}

.settings > summary {
  list-style: none;
}

.settings > summary::-webkit-details-marker {
  display: none;
}

.settings-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: var(--z-dropdown);
  width: min(290px, calc(100vw - 28px));
  padding: 16px;
  border: 2px solid var(--deep-grout);
  border-radius: 12px;
  background: var(--rain-light);
}

.settings-panel label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.settings-panel select {
  width: 100%;
  min-height: 44px;
  padding: 9px 36px 9px 11px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font-size: 16px;
}

.settings-version {
  display: flex;
  justify-content: space-between;
  margin: 13px 0 0;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.75rem;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(420px, 570px);
  gap: 28px;
  align-items: center;
  justify-content: center;
  width: min(100%, 1120px);
  min-height: calc(100svh - 92px);
  margin: 0 auto;
}

.control-deck {
  min-width: 0;
  padding: 24px;
  border: 2px solid var(--deep-grout);
  border-radius: 14px;
  background: var(--rain-light);
}

.garden-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 10px;
  color: var(--deep-grout-soft);
  font-size: 0.78rem;
  font-weight: 800;
}

.garden-label::before {
  width: 10px;
  height: 10px;
  border-radius: 3px 8px 3px 8px;
  background: var(--jade);
  content: "";
  transform: rotate(-12deg);
}

.title-block h1 {
  margin: 0;
  color: var(--deep-grout);
  font-family:
    ui-rounded,
    "Arial Rounded MT Bold",
    "Noto Sans TC",
    "Noto Sans SC",
    "Noto Sans JP",
    "Noto Sans KR",
    "Noto Sans Thai",
    "Segoe UI",
    sans-serif;
  font-size: 2.55rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  line-height: 1.04;
  text-wrap: balance;
}

.app-subtitle {
  max-width: 35ch;
  margin: 11px 0 0;
  color: var(--muted);
  font-size: 0.94rem;
  line-height: 1.55;
  text-wrap: pretty;
}

.score-strip {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 22px;
}

.score-cell {
  min-width: 0;
  padding: 12px 13px;
  border-radius: 10px;
  background: var(--deep-grout);
  color: #fff;
}

.score-cell span {
  display: block;
  color: #bfd4cf;
  font-size: 0.72rem;
  font-weight: 700;
}

.score-cell strong {
  display: block;
  margin-top: 3px;
  color: var(--celadon);
  font-family: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 1.7rem;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.status-band {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 11px;
  align-items: center;
  min-height: 55px;
  margin-top: 14px;
  padding: 9px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.status-seed {
  width: 15px;
  height: 15px;
  border: 3px solid var(--deep-grout);
  border-radius: 50% 50% 50% 10%;
  background: var(--celadon);
  transform: rotate(45deg);
}

.status-copy span {
  display: block;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
}

.status-copy strong {
  display: block;
  margin-top: 2px;
  color: var(--deep-grout);
  font-size: 1rem;
}

.start-button,
.play-again-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: 10px;
  background: var(--pomegranate);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  transition:
    background-color 180ms ease-out,
    transform 180ms cubic-bezier(0.22, 1, 0.36, 1);
}

.start-button {
  margin-top: 14px;
}

.start-button:hover,
.play-again-button:hover {
  background: #bf4939;
}

.start-button:active,
.play-again-button:active {
  transform: translateY(2px);
}

.start-glyph {
  font-size: 0.9rem;
}

.keyboard-hint {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 0.72rem;
  line-height: 1.45;
  text-align: center;
}

.direction-pad {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(3, 52px);
  gap: 5px;
  justify-content: center;
  margin-top: 17px;
}

.direction-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 52px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: #e5f0eb;
  color: var(--deep-grout);
  font-size: 1.3rem;
  font-weight: 900;
  cursor: pointer;
  transition:
    background-color 160ms ease-out,
    border-color 160ms ease-out,
    color 160ms ease-out,
    transform 160ms cubic-bezier(0.22, 1, 0.36, 1);
}

.direction-button:hover:not(:disabled) {
  border-color: var(--jade);
  background: #d7e8df;
}

.direction-button:active:not(:disabled),
.direction-button.is-active {
  border-color: var(--deep-grout);
  background: var(--celadon);
  transform: scale(0.94);
}

.direction-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.direction-up {
  grid-column: 2;
  grid-row: 1;
}

.direction-left {
  grid-column: 1;
  grid-row: 2;
}

.direction-center {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  width: 52px;
  height: 52px;
}

.direction-center::before,
.direction-center::after {
  position: absolute;
  content: "";
}

.direction-center::before {
  inset: 9px;
  border: 6px solid var(--jade);
  border-right-color: transparent;
  border-radius: 50%;
  transform: rotate(30deg);
}

.direction-center::after {
  top: 8px;
  right: 7px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--pomegranate);
}

.direction-right {
  grid-column: 3;
  grid-row: 2;
}

.direction-down {
  grid-column: 2;
  grid-row: 3;
}

.playfield-panel {
  min-width: 0;
}

.board-heading {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  margin: 0 4px 10px;
}

.board-heading h2 {
  margin: 0;
  color: var(--deep-grout);
  font-size: 1rem;
  font-weight: 800;
}

.board-heading p {
  max-width: 42ch;
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.76rem;
  line-height: 1.4;
}

.board-shell {
  position: relative;
  width: 100%;
  max-width: 570px;
  padding: 8px;
  border-radius: 16px;
  background: var(--deep-grout);
  box-shadow: 0 7px 0 #0f3033;
  isolation: isolate;
}

#game-canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  border-radius: 9px;
  background: var(--pool-tile);
  touch-action: none;
}

.board-shell::after {
  position: absolute;
  inset: 35%;
  z-index: 2;
  border: 3px solid var(--celadon);
  border-radius: 50%;
  content: "";
  opacity: 0;
  pointer-events: none;
}

.board-shell.is-feeding::after {
  animation: grout-ripple 240ms cubic-bezier(0.22, 1, 0.36, 1);
}

.board-shell.is-crashed {
  animation: tile-jolt 260ms cubic-bezier(0.22, 1, 0.36, 1);
}

.score-cell.is-scoring strong {
  animation: score-settle 220ms cubic-bezier(0.22, 1, 0.36, 1);
}

.board-overlay {
  position: absolute;
  inset: 8px;
  z-index: var(--z-overlay);
  display: grid;
  place-items: center;
  padding: 18px;
  border-radius: 9px;
  background: rgba(23, 59, 63, 0.94);
}

.board-overlay[hidden] {
  display: none;
}

.result-panel {
  width: min(100%, 310px);
  padding: 22px;
  border-radius: 12px;
  background: var(--rain-light);
  color: var(--ink);
  text-align: center;
}

.result-panel h2 {
  margin: 0;
  color: var(--pomegranate);
  font-family:
    ui-rounded,
    "Arial Rounded MT Bold",
    "Noto Sans TC",
    "Noto Sans SC",
    "Noto Sans JP",
    "Noto Sans KR",
    "Noto Sans Thai",
    "Segoe UI",
    sans-serif;
  font-size: 1.65rem;
}

.result-panel p {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.45;
}

.result-score {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
  margin: 15px 0;
  color: var(--deep-grout);
}

.result-score span {
  font-size: 0.75rem;
  font-weight: 700;
}

.result-score strong {
  color: var(--jade);
  font-family: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 2.2rem;
  font-variant-numeric: tabular-nums;
}

.new-record {
  display: inline-block;
  margin: 0 0 14px;
  padding: 5px 9px;
  border-radius: 7px;
  background: var(--celadon);
  color: var(--deep-grout);
  font-size: 0.75rem;
  font-weight: 800;
}

.new-record[hidden] {
  display: none;
}

.version-note {
  position: fixed;
  right: 8px;
  bottom: 6px;
  color: rgba(21, 38, 36, 0.55);
  font-family: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 0.62rem;
  pointer-events: none;
}

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

@keyframes grout-ripple {
  from {
    opacity: 0.8;
    transform: scale(0.45);
  }
  to {
    opacity: 0;
    transform: scale(2.5);
  }
}

@keyframes tile-jolt {
  0%,
  100% {
    transform: translateX(0);
  }
  35% {
    transform: translateX(-5px);
  }
  70% {
    transform: translateX(4px);
  }
}

@keyframes score-settle {
  0% {
    color: var(--focus);
    transform: translateY(-4px);
  }
  100% {
    color: var(--celadon);
    transform: translateY(0);
  }
}

@media (max-width: 860px) {
  .app-shell {
    padding: 0 16px 24px;
  }

  .game-layout {
    grid-template-columns: minmax(0, 560px);
    gap: 18px;
    min-height: auto;
  }

  .control-deck {
    padding: 20px;
  }

  .title-block {
    text-align: center;
  }

  .garden-label {
    justify-content: center;
  }

  .title-block h1 {
    font-size: 2.15rem;
  }

  .app-subtitle {
    margin-right: auto;
    margin-left: auto;
  }

  .board-shell {
    margin: 0 auto;
  }
}

@media (max-width: 480px) {
  .app-shell {
    padding: 0 10px 20px;
  }

  .topbar {
    min-height: 58px;
  }

  .back-link,
  .settings > summary {
    width: 44px;
    padding: 0;
  }

  .back-label,
  .settings-label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }

  .garden-mark {
    width: 38px;
    height: 38px;
  }

  .control-deck {
    padding: 16px;
  }

  .title-block h1 {
    font-size: 1.95rem;
  }

  .app-subtitle {
    font-size: 0.86rem;
  }

  .score-strip {
    margin-top: 16px;
  }

  .direction-pad {
    margin-top: 14px;
  }

  .board-heading {
    margin-bottom: 8px;
  }

  .board-heading p {
    max-width: 29ch;
  }
}

@media (max-height: 600px) and (orientation: landscape) {
  body {
    overflow-y: hidden;
  }

  .app-shell {
    height: 100svh;
    min-height: 0;
    padding: 0 10px 8px;
  }

  .topbar {
    min-height: 48px;
  }

  .back-link,
  .settings > summary {
    min-height: 44px;
    padding: 6px 10px;
    font-size: 0.75rem;
  }

  .garden-mark {
    width: 34px;
    height: 34px;
  }

  .garden-mark::before {
    inset: 7px;
    border-width: 6px;
  }

  .garden-mark::after {
    top: 6px;
    right: 5px;
    width: 9px;
    height: 9px;
  }

  .game-layout {
    grid-template-columns: minmax(230px, 360px) minmax(0, 1fr);
    gap: 12px;
    width: 100%;
    height: calc(100svh - 56px);
    min-height: 0;
    align-items: stretch;
  }

  .control-deck {
    display: grid;
    grid-template-areas:
      "title title"
      "scores dpad"
      "status dpad"
      "start dpad";
    grid-template-columns: minmax(0, 1fr) 138px;
    grid-template-rows: auto auto auto 1fr;
    gap: 6px 10px;
    min-height: 0;
    padding: 10px 12px;
    overflow: hidden;
  }

  .title-block {
    grid-area: title;
    min-width: 0;
    text-align: left;
  }

  .garden-label {
    justify-content: flex-start;
    margin-bottom: 2px;
    font-size: 0.64rem;
  }

  .title-block h1 {
    font-size: 1.45rem;
  }

  .app-subtitle,
  .keyboard-hint {
    display: none;
  }

  .score-strip {
    grid-area: scores;
    gap: 5px;
    align-self: start;
    margin: 0;
  }

  .score-cell {
    padding: 7px 8px;
    border-radius: 7px;
  }

  .score-cell span {
    font-size: 0.58rem;
  }

  .score-cell strong {
    font-size: 1.18rem;
  }

  .status-band {
    grid-area: status;
    gap: 7px;
    min-height: 39px;
    margin: 0;
    padding: 4px 0;
  }

  .status-seed {
    width: 12px;
    height: 12px;
    border-width: 2px;
  }

  .status-copy span {
    font-size: 0.58rem;
  }

  .status-copy strong {
    font-size: 0.78rem;
  }

  .start-button {
    grid-area: start;
    align-self: end;
    min-height: 44px;
    margin: 0;
    border-radius: 8px;
    font-size: 0.78rem;
  }

  .direction-pad {
    grid-area: dpad;
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(3, 44px);
    gap: 4px;
    align-self: center;
    justify-self: center;
    margin: 0;
  }

  .direction-button,
  .direction-center {
    width: 44px;
    height: 44px;
  }

  .direction-button {
    min-width: 44px;
    min-height: 44px;
    border-radius: 8px;
    font-size: 1rem;
  }

  .direction-center::before {
    inset: 8px;
    border-width: 5px;
  }

  .playfield-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    min-height: 0;
  }

  .board-heading {
    min-height: 27px;
    margin: 0 2px 4px;
    align-items: center;
  }

  .board-heading h2 {
    font-size: 0.76rem;
  }

  .board-heading p {
    max-width: 50ch;
    margin: 0;
    font-size: 0.59rem;
  }

  .board-shell {
    align-self: center;
    width: min(100%, calc(100svh - 88px));
    max-width: none;
    margin: 0 auto;
    padding: 6px;
    border-radius: 12px;
    box-shadow: 0 4px 0 #0f3033;
  }

  #game-canvas,
  .board-overlay {
    border-radius: 7px;
  }

  .board-overlay {
    inset: 6px;
  }

  .result-panel {
    width: min(100%, 250px);
    padding: 13px;
  }

  .result-panel h2 {
    font-size: 1.15rem;
  }

  .result-panel p {
    margin-top: 4px;
    font-size: 0.68rem;
  }

  .result-score {
    margin: 7px 0;
  }

  .result-score strong {
    font-size: 1.45rem;
  }

  .new-record {
    margin-bottom: 7px;
  }

  .play-again-button {
    min-height: 44px;
    font-size: 0.75rem;
  }
}

@media (max-height: 600px) and (orientation: landscape) and (max-width: 680px) {
  .game-layout {
    grid-template-columns: minmax(218px, 44%) minmax(0, 1fr);
    gap: 8px;
  }

  .control-deck {
    grid-template-columns: minmax(0, 1fr) 140px;
    gap: 4px 6px;
    padding: 7px 8px;
  }

  .direction-pad {
    grid-template-columns: repeat(3, 44px);
    grid-template-rows: repeat(3, 44px);
  }

  .direction-button,
  .direction-center {
    width: 44px;
    height: 44px;
  }

  .direction-button {
    min-width: 44px;
    min-height: 44px;
  }

  .garden-label {
    display: none;
  }

  .title-block h1 {
    font-size: 1.18rem;
  }
}

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