:root {
  color-scheme: light;
  --paper: #e9e7df;
  --ink: #202b27;
  --muted: #46534b;
  --line: #9da69a;
  --accent: #263d34;
  --canvas: #223330;
  --intro-duration: 3s;
  --viewport-height: 100%;
  color: var(--ink);
  background-color: var(--canvas);
  animation: launch-canvas var(--intro-duration) step-end both;
  font: 16px/1.5 system-ui, sans-serif;
}

@supports (height: 100dvh) {
  :root { --viewport-height: 100dvh; }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overflow: clip;
  overscroll-behavior: none;
}

body { position: relative; height: var(--viewport-height); }
button { font: inherit; cursor: pointer; }
.panel :is(button, label), dialog button { touch-action: manipulation; }
button:focus-visible { outline: 2px solid #6c8065; outline-offset: 3px; }

.launch-intro {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 40;
  background: #fff;
  animation: launch-fade var(--intro-duration) ease both;
}

.launch-intro img {
  display: block;
  width: min(390px, 72vw);
  height: auto;
  animation: launch-zoom var(--intro-duration) ease-out both;
}

@keyframes launch-canvas {
  from { background-color: #fff; }
  90%, to { background-color: var(--canvas); }
}

@keyframes launch-fade {
  0%, 90% { opacity: 1; visibility: visible; }
  100% { opacity: 0; visibility: hidden; }
}

@keyframes launch-zoom {
  from { transform: scale(.955); }
  to { transform: scale(1.035); }
}

@media (prefers-reduced-motion: reduce) {
  .launch-intro img { animation: none; }
}

.layout {
  --game-bar: calc(46px + env(safe-area-inset-bottom));
  position: relative;
  height: 100%;
  overflow: hidden;
  overflow: clip;
  background: linear-gradient(180deg, #13282220 15%, #162a2510 45%, #101a18b0 100%),
    url('assets/inlet.webp') center bottom / cover no-repeat;
}

.panel {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  container-type: size;
  overflow: hidden;
  overflow: clip;
  z-index: 2;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  color: #f7f3e8;
  background: linear-gradient(160deg, #0d1d1ad9, #162821d4 60%, #18241fc2);
}

.menu {
  width: min(100%, 520px);
  display: grid;
  gap: clamp(12px, 3dvh, 30px);
  text-align: center;
  transform: var(--fit-transform, none);
}

h2 { font-family: Georgia, serif; font-weight: 400; line-height: .96; letter-spacing: -.05em; }
h1 {
  margin: 0 0 12px;
  font: 600 clamp(2.15rem, min(10vw, 9.5dvh), 5.25rem) / 1 "Avenir Next", Avenir, system-ui, sans-serif;
  letter-spacing: .14em;
  text-indent: .14em;
  text-transform: uppercase;
}

.intro p { margin: 0 auto; max-width: 46ch; color: #e5e9de; }
.design-cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
.design-card {
  container-type: inline-size;
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 6px 3px;
  border: 2px solid #e9eee43f;
  border-radius: 5px;
  background: #f7f3e80c;
  cursor: pointer;
  font-size: clamp(.74rem, 3.2vw, .92rem);
  line-height: 1.2;
}
.design-card:hover { background: #f7f3e822; }
.design-cards input:checked + .design-card { border-color: #e8dfb7; background: #eeecd21e; }
.design-cards input:focus-visible + .design-card { outline: 2px solid #f7f3e8; outline-offset: 2px; }
.design-stack {
  --sprite-unit: min(calc(79cqw / 142), calc(15cqh / 92), .68px);
  position: relative;
  height: calc(92 * var(--sprite-unit));
  pointer-events: none;
}
fieldset { margin: 0; padding: 0; border: 0; min-width: 0; }
legend { margin: 0 auto 8px; padding: 0; font-weight: 650; color: inherit; }
.level-pages {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  margin-bottom: 7px;
}

.level-pages button { min-height: 34px; padding: 2px 4px; font-size: .9rem; }
.level-pages button[aria-pressed="true"] { border-color: currentColor; font-weight: 650; }
.menu .level-pages button[aria-pressed="true"] { color: #242f28; background: #e8dfb7; border-color: #e8dfb7; }
#recordPages button[aria-pressed="true"] { color: #f7f5eb; background: var(--accent); }
.level-buttons { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 7px; }
.level-buttons button, .secondary, .primary {
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 4px;
  color: var(--ink);
  background: #ffffff21;
}
.secondary:hover { background: #d6ddd1; }
.primary { border-color: var(--accent); background: var(--accent); color: #f7f5eb; }
.primary:hover { background: #385448; }
button:disabled { opacity: .43; cursor: default; }
.menu-actions { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 8px; }
.menu button { color: inherit; border-color: #e9eee477; background: #f7f3e810; }
.menu button:hover:not(:disabled) { background: #f7f3e837; }
.menu-actions .primary { color: #192824; background: #edece0; border-color: #edece0; }
.menu-actions .primary:hover:not(:disabled) { background: #fffaf0; }

.scene {
  position: absolute;
  inset: 0;
  overflow: hidden;
  isolation: isolate;
  container-type: size;
}

.scene > :is(.stone-platform, .preview, #game) {
  --platform-height: min(19cqw, 23cqh);
  --platform-bottom: calc(var(--interface-bottom, 0px) + 2cqh);
  --seat: calc(var(--platform-bottom) + var(--platform-height) * .445);
}

.stone-platform {
  position: absolute;
  bottom: var(--platform-bottom);
  left: 2%;
  width: 96%;
  height: var(--platform-height);
  object-fit: fill;
  z-index: -1;
  pointer-events: none;
  filter: drop-shadow(0 7px 12px #0e171790);
}

.preview, #game, .board { position: absolute; inset: 0; }
#game:focus { outline: none; }
.board { touch-action: none; -webkit-touch-callout: none; user-select: none; }
.preview { display: grid; place-items: end center; padding-bottom: var(--seat); }
.preview-stack {
  --sprite-unit: min(calc(40cqw / 142), calc(56cqh / 142), 2.95px);
  position: relative;
  height: calc(145 * var(--sprite-unit));
}

.layout.playing .scene { --interface-bottom: var(--game-bar); }

.layout.playing .panel {
  inset: auto 0 0;
  height: var(--game-bar);
  padding: 0 max(12px, env(safe-area-inset-right)) env(safe-area-inset-bottom)
    max(12px, env(safe-area-inset-left));
  background: #192824b8;
}

.game-controls {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
}

.level-label { font-weight: 650; }
.game-controls time { justify-self: end; font-weight: 600; font-variant-numeric: tabular-nums; }
#returnMenu {
  justify-self: start;
  min-height: 34px;
  color: inherit;
  border-color: #e6ecdc60;
  background: #f0eee31a;
  padding: 3px 12px;
}

.towers {
  --stack-room: max(1px, calc(100cqh - var(--seat) - max(4px, env(safe-area-inset-top))));
  --sprite-unit: min(calc(100cqw / 480), calc(100cqh / 280), calc(var(--stack-room) / var(--stack-height, 160)));
  position: absolute;
  inset: 0 4cqw;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.tower { position: relative; border: 0; background: transparent; padding: 0; }
.tower:focus-visible { outline: 2px solid #f1ede2; outline-offset: -3px; }
.tower::before {
  content: '';
  position: absolute;
  bottom: calc(var(--seat) - .8cqh);
  left: 50%;
  transform: translateX(-50%);
  width: 87%;
  max-width: calc(149 * var(--sprite-unit));
  height: clamp(5px, 2.4cqh, 20px);
  border-radius: 50%;
  background: radial-gradient(ellipse, #eee8d78a 6%, #e9e3d25e 45%, transparent 76%);
  pointer-events: none;
}

.tower[aria-pressed="true"]::before {
  outline: 1px solid #fff9e3a8;
  background-color: #fffae226;
}

.tower-stack { position: absolute; bottom: var(--seat); left: 50%; z-index: 1; pointer-events: none; }
.tower-stack, .preview-stack { --pile-base-bottom: 0; }
:is(.tower-stack, .preview-stack):has(.disk:not(.lifted))::before {
  content: '';
  position: absolute;
  --footprint: calc(var(--contact-width) * var(--sprite-unit));
  bottom: calc(var(--pile-base-bottom, 0) * var(--sprite-unit) - var(--footprint) * .036);
  left: 0;
  transform: translateX(-50%);
  width: calc(var(--footprint) * .98);
  height: calc(var(--footprint) * .091);
  border-radius: 50%;
  background: radial-gradient(ellipse, #141713d4 8%, #242b206e 54%, transparent 76%);
  filter: blur(clamp(.55px, calc(var(--footprint) * .009), 2px));
  pointer-events: none;
}

.disk {
  position: absolute;
  left: 0;
  bottom: calc(var(--sprite-bottom) * var(--sprite-unit));
  width: calc(var(--sprite-width) * var(--sprite-unit));
  height: calc(var(--sprite-height) * var(--sprite-unit));
  max-width: none;
  transform: translateX(-50%);
  user-select: none;
  -webkit-user-drag: none;
  --stone-tone: grayscale(1) sepia(1) saturate(1.7) hue-rotate(11deg) brightness(.89);
  filter: var(--stone-tone);
}

.slate-stone { --stone-tone: grayscale(.85) brightness(1.17) contrast(1.05); }
.top-disk { pointer-events: auto; cursor: grab; }
.selected .top-disk { filter: var(--stone-tone) brightness(1.16); }
.lifted { visibility: hidden; }
.drag-disk { position: absolute; z-index: 20; pointer-events: none; user-select: none; }

dialog {
  inset: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  width: 400px;
  max-width: calc(100% - max(8px, env(safe-area-inset-left)) - max(8px, env(safe-area-inset-right)));
  max-height: calc(var(--viewport-height) - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)));
  overflow: hidden;
  overflow: clip;
  padding: clamp(16px, 5vw, 34px);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: var(--paper);
}
.dialog-content { display: flow-root; transform: var(--fit-transform, none); transform-origin: top center; }
dialog::backdrop { background: #0e1916a0; }
#installPanel:has(.install-steps:not([hidden])) { width: 650px; }
#installPanel p { margin: 0 0 16px; }
.install-steps {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  list-style: none;
  padding: 0;
  margin: 0 0 15px;
}
.install-steps li {
  border: 1px solid #adb8aa;
  border-radius: 6px;
  padding: 7px;
  min-width: 0;
}
.install-picture {
  height: 66px;
  display: grid;
  place-items: center;
  background: #dce5dc;
  border-radius: 4px;
  color: #295b56;
}
.install-tile, .install-row {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fbfcf8;
  border: 1px solid #bccac0;
  border-radius: 8px;
}
.install-tile { width: 46px; height: 43px; padding-top: 6px; }
.share-symbol {
  width: 19px;
  height: 18px;
  border: 2px solid currentColor;
  border-radius: 3px;
  position: relative;
}
.share-symbol::before {
  content: '';
  position: absolute;
  width: 2px;
  height: 15px;
  left: 50%;
  bottom: 5px;
  transform: translateX(-50%);
  background: currentColor;
  box-shadow: 0 0 0 3px #fbfcf8;
}
.share-symbol::after {
  content: '';
  position: absolute;
  width: 7px;
  height: 7px;
  border-top: 2px solid currentColor;
  border-left: 2px solid currentColor;
  left: 50%;
  bottom: 11px;
  transform: translateX(-50%) rotate(45deg);
}
.install-row { padding: 6px; gap: 6px; width: min(91%, 105px); }
.home-symbol {
  display: grid;
  place-items: center;
  flex: 0 0 19px;
  height: 19px;
  border: 1.5px solid currentColor;
  border-radius: 3px;
  font: normal 14px/1 system-ui, sans-serif;
}
.home-symbol::before { content: '+'; }
.install-lines {
  width: 42px;
  height: 12px;
  background: linear-gradient(#80968e, #80968e) top left / 100% 2px no-repeat,
    linear-gradient(#bccac0, #bccac0) bottom left / 72% 2px no-repeat;
}
.install-picture-finish {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  padding: 5px;
}
.install-picture-finish span {
  padding: 2px 6px;
  border: 1px solid #9aafa0;
  border-radius: 4px;
  background: #fbfcf8;
  font-size: .72rem;
  font-weight: 650;
}
.install-switch {
  position: relative;
  width: 31px;
  height: 18px;
  border-radius: 10px;
  background: #2b7550;
}
.install-switch::after {
  content: '';
  position: absolute;
  inset: 3px 3px 3px auto;
  width: 12px;
  border-radius: 50%;
  background: white;
}
.install-caption {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin-top: 8px;
  font-size: .88rem;
  line-height: 1.25;
}
.install-caption b {
  flex: 0 0 1.25rem;
  text-align: center;
  border-radius: 50%;
  background: #dce5dc;
  line-height: 1.25rem;
}

dialog h2 { margin: 0 0 18px; font-size: 2.45rem; }
.record-tables { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; margin-bottom: 20px; }
dialog table { width: 100%; border-collapse: collapse; }
dialog th, dialog td { text-align: left; padding: 5px 0; border-bottom: 1px solid #9da69a65; }
dialog th:last-child, dialog td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

@media (max-width: 490px) {
  .level-buttons { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .menu { max-width: 340px; }
  .menu-actions { grid-auto-flow: row; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #installApp { grid-column: 1 / -1; }
}

@media (max-height: 355px) {
  h1 { margin-bottom: 6px; }
  .menu { gap: 9px; }
  .intro p { font-size: .9rem; }
  .level-buttons button, .menu-actions button { min-height: 36px; padding: 4px; }
}

@media (max-height: 290px) {
  .panel { padding-block: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-bottom)); }
  .layout.playing .panel { padding-block: 0 env(safe-area-inset-bottom); }
  .menu { gap: 7px; }
  .design-choice legend { margin-bottom: 4px; }
  .design-stack { --sprite-unit: min(calc(79cqw / 142), calc(11dvh / 92)); }
  .design-card { gap: 2px; padding-block: 3px; }
  dialog { padding: 12px; }
  dialog h2 { font-size: 1.65rem; margin-bottom: 7px; }
  #installPanel p { font-size: .9rem; line-height: 1.3; margin-bottom: 8px; }
  .record-tables { gap: 12px; margin-bottom: 8px; }
  dialog table { font-size: .875rem; line-height: 1.2; }
  dialog th, dialog td { padding-block: 1px; }
  dialog :is(.primary, .secondary) { min-height: 34px; padding-block: 3px; }
}

@container (max-height: 370px) {
  .menu { gap: clamp(5px, 2cqh, 9px); }
  .intro h1 { font-size: clamp(1.7rem, 11cqh, 2.8rem); margin-bottom: 5px; }
  .intro p { font-size: clamp(.74rem, 4.7cqh, .9rem); line-height: 1.28; }
  .menu legend { margin-bottom: 4px; }
  .menu .level-pages { margin-bottom: 4px; }
  .menu .level-pages button { min-height: 30px; }
  .design-stack { --sprite-unit: min(calc(79cqw / 142), calc(12cqh / 92)); }
  .design-card { padding-block: 3px; }
  .level-buttons button, .menu-actions button { min-height: 32px; padding-block: 3px; }
}

@media (max-height: 240px) and (orientation: landscape) {
  .menu { width: min(100%, 720px); grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px 12px; }
  .intro, .menu-actions { grid-column: 1 / -1; }
  .menu legend { margin-bottom: 4px; }
  .level-buttons { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
  .level-buttons button { min-height: 26px; padding: 1px; }
  h1 { font-size: 2rem; }
}

@media (max-width: 540px) and (orientation: portrait) {
  .install-steps { grid-template-columns: 1fr; }
  .install-steps li { display: grid; grid-template-columns: 105px minmax(0, 1fr); align-items: center; gap: 7px; }
  .install-picture { height: 58px; }
  .install-caption { margin: 0; }
}

@media (max-height: 290px) {
  .install-steps { margin-bottom: 8px; }
  .install-picture { height: 48px; }
  .install-caption { margin-top: 5px; font-size: .8rem; }
}
