:root {
  color-scheme: dark;
  --stage: #020203;
  --ink: #b8b2a4;
  --ink-strong: #efe9d9;
  --focus: #f3e6b4;
  --ivory: #f7eddc;
  --key-height: max(44px, 2.75rem, min(6.2vh, 3.5rem));
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--stage);
}

body {
  color: var(--ink);
  font: 0.75rem/1.35 var(--mono);
  -webkit-text-size-adjust: 100%;
}

#stars {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 0;
  display: block;
  width: 100%;
  height: 100%;
}

#app {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

#stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

#art {
  display: block;
  flex: none;
  transform-origin: 50% 100%;
  transition: opacity 0.45s linear, transform 0.9s cubic-bezier(0.2, 0.7, 0.2, 1);
}

/* The renderer closes the flowers; this only carries what remains down and out of sight. */
body:is([data-lock-state="opening"], [data-lock-state="pin"], [data-lock-state="message"]) #art {
  opacity: 0;
  transform: translateY(2.5%) scale(0.93);
  transition: opacity 0.42s linear 0.5s, transform 0.95s cubic-bezier(0.55, 0, 0.75, 0.35);
}

#fallback {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  color: var(--ink-strong);
  font-size: 0.875rem;
  line-height: 1.5;
  text-align: center;
  opacity: 0;
  animation: fallback-in 0.2s linear 1.2s forwards;
}

#fallback p {
  max-width: 34em;
  margin: 0;
}

#fallback[hidden] {
  display: none;
}

#fallback.now {
  opacity: 1;
  animation: none;
}

@keyframes fallback-in {
  to {
    opacity: 1;
  }
}

#controls {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 8px 6px;
  visibility: hidden;
  transition: opacity 0.35s linear;
}

#picker {
  display: flex;
  justify-content: center;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--ink);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:active {
  background: rgba(255, 255, 255, 0.1);
}

button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: -3px;
}

@media (hover: hover) {
  button:hover {
    background: rgba(255, 255, 255, 0.06);
    color: var(--ink-strong);
  }
}

.swatch {
  display: block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.22);
  opacity: 0.72;
}

.dot[aria-current="true"] .swatch {
  opacity: 1;
  outline: 1.5px solid var(--ink-strong);
  outline-offset: 3px;
}

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

.ready #controls,
.ready #lock {
  visibility: visible;
}

.failed #art,
.failed #stars,
.failed #controls,
.failed #lock {
  display: none;
}

/* ---------- lock button ---------- */

#lock {
  position: fixed;
  top: calc(6px + env(safe-area-inset-top, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  z-index: 2;
  visibility: hidden;
  transition: opacity 0.3s linear;
}

#lock[hidden] {
  display: none;
}

#lock-art {
  display: block;
  flex: none;
}

body:is([data-lock-state="opening"], [data-lock-state="pin"], [data-lock-state="message"]) #lock,
body:is([data-lock-state="opening"], [data-lock-state="pin"], [data-lock-state="message"]) #controls {
  opacity: 0;
}

/* ---------- PIN scene: flowers and window above, number pad below ---------- */

#lock-dialog {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
  overflow: auto;
  overscroll-behavior: contain;
  border: 0;
  background: transparent;
  color: var(--ivory);
}

#lock-dialog:not([open]) {
  display: none;
}

#lock-dialog[open] {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(min-content, 1fr) auto;
}

#lock-dialog::backdrop {
  background: transparent;
}

#lock-dialog[data-phase="out"] > * {
  pointer-events: none;
}

/* The padding keeps a band for flowers above and below the window, so tall text scrolls instead of crowding them out. */
#lock-scene {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  padding: 3.25rem 0.5rem;
  overflow: hidden;
}

#frame-art {
  position: absolute;
  top: 0;
  display: block;
  opacity: 0;
  transition: opacity 0.2s linear 0.3s;
}

#lock-dialog[data-phase="in"] #frame-art {
  opacity: 1;
  transition: opacity 0.3s linear 0.5s;
}

/* ---------- the small black window ---------- */

#lock-panel {
  position: relative;
  z-index: 1;
  flex: none;
  width: min(13.5rem, 100%);
  padding: 0.5rem 0.75rem;
  background: #000;
  text-align: center;
  opacity: 0;
  transition: opacity 0.2s linear;
}

#lock-dialog[data-phase="in"] #lock-panel {
  opacity: 1;
  transition: opacity 0.25s linear 0.62s;
}

#lock-panel:focus {
  outline: none;
}

#lock-dialog[data-view="message"] #lock-panel {
  width: min(22rem, 100%);
  padding: 1rem;
}

/* Message arrival: the window unfolds with a clip rather than a fade, so its words are always at full contrast. */
#lock-dialog[data-view="message"] #lock-panel {
  animation: message-window-unfold 0.25s cubic-bezier(0.2, 0.8, 0.2, 1) 0.05s both;
}

@keyframes message-window-unfold {
  from {
    clip-path: inset(50% 0);
  }
  to {
    clip-path: inset(0);
  }
}

/* The PIN flowers leave first; the canvas returns holding only the window edge, then the circle. */
#lock-dialog[data-phase="in"][data-view="message"]:not([data-message-phase]) #frame-art {
  opacity: 0;
  transition: opacity 0.2s linear;
}

#lock-dialog[data-phase="in"][data-message-phase] #frame-art {
  transition: opacity 0.15s linear;
}

/* The text has to fit inside the circle, which the viewport's shorter side limits. */
@media (max-width: 479px), (max-height: 559px) {
  #lock-dialog[data-view="message"] #lock-panel {
    padding: 0.75rem;
  }

  #lock-dialog[data-view="message"] #message {
    font-size: 0.875rem;
    line-height: 1.5;
  }
}

@media (max-width: 360px) {
  #lock-dialog[data-view="message"] #message {
    font-size: 0.8125rem;
    line-height: 1.45;
  }
}

#lock-dialog[data-view="message"] #pin-view,
#lock-dialog[data-view="pin"] #message-view {
  display: none;
}

#pin-intro {
  display: grid;
  justify-items: center;
  gap: 0.25rem;
}

#pin-title {
  margin: 0;
  color: var(--ivory);
  font: 600 0.8125rem/1.3 var(--mono);
  letter-spacing: 0.08em;
}

#pin-status {
  min-height: 1.35em;
  margin: 0;
  color: #dcd3c2;
  font-size: 0.75rem;
  line-height: 1.35;
}

#pin-status[data-tone="error"] {
  color: #f6b7cd;
}

#pin-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.seed {
  position: relative;
  display: block;
  width: 1.6rem;
  height: 1.9rem;
}

.seed canvas {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
  transition: opacity 0.12s linear;
}

.seed-lit,
.seed.filled .seed-rest {
  opacity: 0;
}

.seed.filled .seed-lit {
  opacity: 1;
}

#pin-display.wilt .seed-lit {
  animation: seed-wilt 0.6s ease-out;
}

@keyframes seed-wilt {
  0% {
    opacity: 1;
    transform: none;
  }
  35% {
    opacity: 0.85;
    transform: translateY(3px) rotate(-14deg);
  }
  100% {
    opacity: 0;
    transform: translateY(6px) rotate(9deg);
  }
}

#message {
  margin: 0;
  color: #fdf6e8;
  font-size: 1rem;
  line-height: 1.6;
}

#message:focus {
  outline: none;
}

/* ---------- the separate number pad ---------- */

#keypad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.25rem 0.375rem;
  justify-self: center;
  width: min(100%, 20rem);
  padding: 0 0.5rem 0.5rem;
  transition: opacity 0.25s linear;
}

/* Out of flow at once, so the window can recentre, then fades away where it stands. */
#lock-dialog[data-view="message"] #keypad {
  position: fixed;
  right: 0;
  bottom: env(safe-area-inset-bottom, 0px);
  left: 0;
  margin: 0 auto;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s linear, visibility 0s linear 0.25s;
}

.key {
  position: relative;
  width: 100%;
  height: var(--key-height);
  opacity: 0;
  transform: translateY(0.5rem);
  transition: transform 0.18s ease-in calc(var(--i) * 10ms), opacity 0.14s linear calc(var(--i) * 10ms);
  -webkit-user-select: none;
  user-select: none;
}

.key:nth-child(1), .key:nth-child(3), .key:nth-child(5) { --i: 1; }
.key:nth-child(2) { --i: 0; }
.key:nth-child(4), .key:nth-child(6), .key:nth-child(8) { --i: 2; }
.key:nth-child(7), .key:nth-child(9), .key:nth-child(11) { --i: 3; }
.key:nth-child(10), .key:nth-child(12) { --i: 4; }

#lock-dialog[data-phase="in"] .key {
  opacity: 1;
  transform: none;
  transition: transform 0.3s cubic-bezier(0.2, 0.9, 0.3, 1) calc(0.6s + var(--i) * 45ms), opacity 0.16s linear calc(0.6s + var(--i) * 45ms);
}

.key-face {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: block;
  transition: transform 0.1s ease-out;
}

.key-art {
  position: absolute;
  top: 0;
  left: 0;
  display: block;
}

/* Plain text until the glyph art has drawn, then hidden visually but kept for assistive technology. */
.key-label {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fdf6e8;
  font: 600 1.125rem/1 var(--mono);
}

.glyph-ready .key-label {
  top: auto;
  right: auto;
  bottom: auto;
  left: auto;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* The glyphs themselves brighten, so pressed and hovered keys stay in the flowers' technique. */
.key:hover,
.key:active,
#dialog-close:hover,
#dialog-close:active {
  background: none;
}

@media (hover: hover) {
  .key:hover .key-art,
  #dialog-close:hover .key-art {
    filter: brightness(1.25);
  }
}

.key:active .key-art,
#dialog-close:active .key-art {
  filter: brightness(1.7);
}

.key:active .key-face,
#dialog-close:active .key-face {
  transform: scale(0.96);
}

.key.press .key-art {
  animation: key-flash 0.24s ease-out;
}

@keyframes key-flash {
  0% {
    filter: brightness(1.8);
  }
  100% {
    filter: brightness(1);
  }
}

.key:focus-visible,
#dialog-close:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

/* ---------- close ---------- */

#dialog-close {
  position: fixed;
  top: calc(6px + env(safe-area-inset-top, 0px));
  right: calc(8px + env(safe-area-inset-right, 0px));
  z-index: 3;
  width: 6.5rem;
  height: max(44px, 2.75rem);
  opacity: 0;
  transition: opacity 0.2s linear;
}

#lock-dialog[data-phase="in"] #dialog-close {
  opacity: 1;
  transition: opacity 0.25s linear 0.6s;
}

/* ---------- short landscape phones ---------- */

@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --key-height: max(44px, 2.75rem);
  }

  #app {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: minmax(0, 1fr);
  }

  #stage {
    grid-column: 1;
    grid-row: 1;
  }

  #controls {
    grid-column: 2;
    grid-row: 1;
    padding: 0 6px;
  }

  #picker {
    display: grid;
    grid-template-columns: repeat(2, 44px);
  }

  #lock-scene {
    padding-top: 2.75rem;
    padding-bottom: 2.75rem;
  }

  #keypad {
    gap: 0.125rem 0.375rem;
    padding-bottom: 0.25rem;
  }
}

/* ---------- reduced motion: every state change is immediate ---------- */

@media (prefers-reduced-motion: reduce) {
  #art,
  #controls,
  #lock,
  #lock-dialog * {
    animation: none !important;
    transition: none !important;
  }
}
