:root {
  --black: #000;
  --deep: #030303;
  --edge: #151515;
  --metal: #777;
  --light: #fff;
  --light-2: #ccc;
  --muted: #666;
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --font: "Helvetica Neue", Helvetica, Arial, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--black);
  color: var(--light-2);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  font-family: var(--font);
}

body {
  position: fixed;
  inset: 0;
}

#game {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--black);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: default; /* RULE 01: an ordinary pointer that never changes over hidden objects */
}
#stage .bloom { mix-blend-mode: screen; pointer-events: none; }

/* --------------------------------------------------------------- screens */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--sat)) calc(24px + var(--sar)) calc(24px + var(--sab)) calc(24px + var(--sal));
  text-align: center;
  opacity: 0;
  transition: opacity 1.6s ease;
  pointer-events: none;
}
.screen.is-on { opacity: 1; pointer-events: auto; }

#title { cursor: pointer; outline: none; touch-action: manipulation; }
.title-center { margin-top: -6vh; }
.logo {
  margin: 0;
  font-weight: 300;
  font-size: clamp(28px, 5.4vw, 58px);
  letter-spacing: 0.42em;
  margin-right: -0.42em;
  color: var(--light);
}
.logo.small { font-size: 15px; letter-spacing: 0.4em; margin: 0 -0.4em 6px 0; }
.slogan {
  margin: 1.4em 0 0;
  font-size: clamp(12px, 1.25vw, 15px);
  letter-spacing: 0.32em;
  color: var(--muted);
}
.title-bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(15vh + var(--sab));
}
.listen { margin: 0 0 14px; font-size: 13px; letter-spacing: 0.3em; color: var(--light-2); }
.phones { margin: 18px 0 0; font-size: 11px; color: #3c3c3c; letter-spacing: 0.06em; }
.enter {
  margin: 0;
  font-size: 13px;
  letter-spacing: 0.3em;
  color: var(--light-2);
  animation: breathe 3.6s ease-in-out infinite;
}
.enter-ko { margin: 12px 0 0; font-size: 12px; color: #4a4a4a; letter-spacing: 0.08em; }
.credit {
  position: absolute;
  bottom: calc(22px + var(--sab));
  left: 0;
  right: 0;
  margin: 0;
  font-size: 10.5px;
  letter-spacing: 0.18em;
  color: #3b3b3b;
}
.credit a, .credit-end, .end-actions a { color: #5a5a5a; text-decoration: none; }
.credit a:hover, .credit a:focus-visible { color: var(--light-2); }

@keyframes breathe { 0%, 100% { opacity: 0.35; } 50% { opacity: 0.85; } }

#whisper {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(13vh + var(--sab));
  margin: 0;
  padding: 0 24px;
  text-align: center;
  font-size: 12.5px;
  letter-spacing: 0.22em;
  line-height: 1.9;
  color: #8a8a8a;
  opacity: 0;
  transition: opacity 1.4s ease;
  pointer-events: none;
}
#whisper.is-on { opacity: 1; }
#whisper .ko { display: block; letter-spacing: 0.04em; font-size: 12px; color: #555; }

/* ------------------------------------------------------------------- HUD */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.8;
  transition: opacity 1.2s ease;
}
#hud.idle { opacity: 0.22; }
#hud[hidden] { display: none; }
.hud-top, .hud-bottom {
  position: absolute;
  left: calc(16px + var(--sal));
  right: calc(16px + var(--sar));
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.hud-top { top: calc(10px + var(--sat)); }
.hud-bottom { bottom: calc(8px + var(--sab)); }
.hud-logo { font-size: 10.5px; letter-spacing: 0.34em; color: #777; }
#counter {
  font-size: 11px;
  letter-spacing: 0.24em;
  color: #777;
  font-variant-numeric: tabular-nums;
}
.hud-text-btn {
  pointer-events: auto;
  min-width: 56px;
  min-height: 44px;
  background: none;
  border: 0;
  color: #777;
  font: inherit;
  font-size: 10.5px;
  letter-spacing: 0.3em;
  cursor: pointer;
  padding: 0 0 0 0.3em;
}
.hud-text-btn:hover, .hud-text-btn:focus-visible { color: var(--light-2); }
.hud-text-btn:focus-visible { outline: 1px solid #444; outline-offset: -4px; }
.hud-right { display: flex; gap: 4px; }
.hud-btn {
  pointer-events: auto;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  background: none;
  border: 0;
  color: var(--muted);
  border-radius: 50%;
  cursor: pointer;
  padding: 0;
}
.hud-btn svg { width: 18px; height: 18px; }
.hud-btn:hover, .hud-btn:focus-visible { color: var(--light-2); }
.hud-btn:focus-visible { outline: 1px solid #444; outline-offset: -6px; }
#btn-sound .mute { display: none; }
#btn-sound[aria-pressed="false"] .mute { display: inline; }
#btn-sound[aria-pressed="false"] .wave { display: none; }

/* --------------------------------------------------------------- settings */
.sheet {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  padding: calc(56px + var(--sat)) calc(20px + var(--sar)) calc(40px + var(--sab)) calc(20px + var(--sal));
  touch-action: pan-y;
  user-select: none;
}
.sheet[hidden] { display: none; }
.sheet-inner { width: 100%; max-width: 420px; }
.sheet-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 18px; }
.sheet-head h2 { margin: 0; font-size: 13px; font-weight: 400; letter-spacing: 0.3em; color: var(--light-2); }
.group { border-top: 1px solid var(--edge); padding: 10px 0; }
.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 48px;
  font-size: 14px;
  color: #b5b5b5;
  cursor: pointer;
}
.row small { display: block; font-size: 11.5px; color: #555; margin-top: 3px; }
.row input[type="range"] { width: 46%; accent-color: #999; }
.row input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 38px;
  height: 22px;
  border-radius: 11px;
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  position: relative;
  flex: none;
  cursor: pointer;
  transition: background 0.2s;
}
.row input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #555;
  transition: transform 0.2s, background 0.2s;
}
.row input[type="checkbox"]:checked { background: #3a3a3a; }
.row input[type="checkbox"]:checked::after { transform: translateX(16px); background: #e5e5e5; }
.row input:focus-visible { outline: 1px solid #777; outline-offset: 3px; }
.row input:disabled { opacity: 0.35; }

.text-btn {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0;
  background: none;
  border: 0;
  color: #b5b5b5;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  letter-spacing: 0.02em;
}
.text-btn:hover, .text-btn:focus-visible { color: #fff; }
.text-btn:focus-visible { outline: 1px solid #555; outline-offset: 4px; }
.actions { display: flex; flex-direction: column; align-items: flex-start; }
.hint-text { margin: 4px 0 6px; font-size: 13px; line-height: 1.7; color: #8d8d8d; min-height: 0; }
.hint-text .en { display: block; color: #cfcfcf; letter-spacing: 0.04em; }
.confirm { padding: 6px 0 10px 14px; border-left: 1px solid #2a2a2a; margin: 2px 0 8px; }
.confirm p { margin: 4px 0; font-size: 13px; color: #888; }
.confirm .text-btn { margin-right: 18px; }
.about { margin-top: 18px; border-top: 1px solid var(--edge); padding-top: 16px; font-size: 11.5px; line-height: 1.8; color: #4d4d4d; }

/* -------------------------------------------------------------------- end */
#end { justify-content: space-between; padding-top: 30vh; }
#end[hidden] { display: none; }
.end-lines .line { margin: 0; letter-spacing: 0.24em; font-size: clamp(13px, 1.6vw, 17px); color: var(--light-2); opacity: 0; transition: opacity 2.4s ease; }
.end-lines .l2 { margin-top: 2.6em; font-size: 12px; color: #5c5c5c; line-height: 2; }
.end-lines .ko { letter-spacing: 0.06em; color: #444; }
#end.is-on .l1 { opacity: 1; transition-delay: 0.4s; }
#end.is-on .l2 { opacity: 1; transition-delay: 2.6s; }
.end-foot { opacity: 0; transition: opacity 2s ease 4.2s; }
#end.is-on .end-foot { opacity: 1; }
.credit-end { margin: 0 0 18px; font-size: 10.5px; letter-spacing: 0.18em; }
.end-actions { display: flex; gap: 28px; justify-content: center; }
.end-actions .text-btn { font-size: 12px; letter-spacing: 0.24em; color: #8a8a8a; }

.noscript { position: absolute; inset: auto 0 45% 0; text-align: center; color: #777; }

@media (prefers-reduced-motion: reduce) {
  .enter { animation: none; opacity: 0.7; }
  .screen, #whisper, #hud, .end-lines .line, .end-foot { transition-duration: 0.4s !important; transition-delay: 0s !important; }
}
.reduced .enter { animation: none; opacity: 0.7; }
.reduced .screen, .reduced #whisper, .reduced .end-lines .line, .reduced .end-foot { transition-duration: 0.4s !important; transition-delay: 0s !important; }

.note { margin: 0 0 8px; font-size: 12px; line-height: 1.7; color: #8a8a8a; }

/* ----------------------------------------------------------- MECHANISMS */
/* wordless thumbnails: door line drawing + gesture marks (see js/ui/codex.js) */
.codex { align-items: flex-start; }
.codex-inner { width: 100%; max-width: 820px; }
.codex .sheet-head h2 { letter-spacing: 0.34em; }
.codex-intro { margin: 0 0 18px; font-size: 12.5px; line-height: 1.8; color: #6a6a6a; }
.codex-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
  border-top: 1px solid var(--edge); padding-top: 18px;
}
.cx-card { margin: 0; border: 1px solid #161616; background: #000; }
.cx-card svg { display: block; width: 100%; height: auto; user-select: none; -webkit-user-select: none; }
.cx-card .floor { stroke: #2c2c2c; stroke-width: 1; fill: none; }
.cx-card .frame { stroke: #3c3c3c; stroke-width: 1; fill: none; }
.cx-card .leaf { stroke: #8d8d8d; stroke-width: 1.1; fill: #070707; }
.cx-card .leaf.inner { fill: none; stroke: #444; }
.cx-card .leaf.glass { fill: #0a0d0f; stroke: #9aa3a8; }
.cx-card .leaf.metal { fill: #0b0b0b; stroke: #a0a0a0; stroke-width: 1.6; }
.cx-card .leaf.film { fill: #101010; stroke: #6a6a6a; stroke-dasharray: 2 2; }
.cx-card .glint { stroke: #3a4246; stroke-width: 1; fill: none; }
.cx-card .rivet { stroke: #777; stroke-width: 2.4; stroke-linecap: round; fill: none; }
.cx-card .seam { stroke: #5a5a5a; stroke-width: 1; }
.cx-card .hinge { stroke: #777; stroke-width: 2.4; }
.cx-card .part { stroke: #c8c8c8; stroke-width: 1.1; fill: none; }
.cx-card .part.thick { stroke-width: 2.6; stroke-linecap: round; }
.cx-card .part.chain { stroke-dasharray: 1.6 1.4; }
.cx-card .key { fill: #9a9a9a; }
.cx-card .tool { stroke: #e8e8e8; stroke-width: 1.2; fill: none; stroke-linecap: round; }
/* gesture marks */
.cx-card .touch { fill: #fff; }
.cx-card .trail { stroke: #fff; stroke-width: 1.3; fill: none; stroke-linecap: round; }
.cx-card .tip { stroke: #fff; stroke-width: 1.3; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.cx-card .ring { stroke: #fff; stroke-width: 1; fill: none; }
.cx-card .count { fill: #bdbdbd; }
.cx-card .clock-bg { stroke: #333; stroke-width: 1.4; fill: none; }
.cx-card .clock { stroke: #fff; stroke-width: 1.6; fill: none; stroke-dasharray: 1; stroke-dashoffset: 0; }
.cx-card .no { fill: #8a8a8a; font-family: var(--font); font-size: 7px; text-anchor: middle; dominant-baseline: middle; }
.cx-card .auto .trail { stroke: #9a9a9a; stroke-dasharray: 2.5 2.5; }
.cx-card .auto .tip { stroke: #9a9a9a; }
.cx-card .auto .crack { stroke: #e6eef2; stroke-dasharray: none; stroke-width: 0.9; }
/* sequencing: one step lit at a time, then all of them */
.cx-card .s { opacity: 0.16; transition: opacity 0.25s ease; }
.cx-card .s.on, .cx-card.rest .s { opacity: 1; }
.cx-card .s.on:not(.auto) .trail { animation: cx-draw 0.75s ease-out both; }
.cx-card .s.on:not(.auto) .trail { stroke-dasharray: 1; }
.cx-card .s.on .tip { animation: cx-tip 0.75s ease-out both; }
.cx-card .s.on .ring { animation: cx-ring 0.75s ease-out both; transform-box: fill-box; transform-origin: center; }
.cx-card .s.on .clock { animation: cx-clock 0.9s linear both; }
@keyframes cx-draw { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }
@keyframes cx-tip { 0%, 60% { opacity: 0; } 100% { opacity: 1; } }
@keyframes cx-ring { from { transform: scale(0.4); opacity: 1; } to { transform: scale(1.25); opacity: 0.5; } }
@keyframes cx-clock { from { stroke-dashoffset: 1; } to { stroke-dashoffset: 0; } }
.reduced .cx-card .s { opacity: 1; transition: none; }
@media (prefers-reduced-motion: reduce) { .cx-card .s { opacity: 1; transition: none; } }
#stage .ripple { animation: ripple 0.7s ease-out forwards; }
@keyframes ripple { from { r: 3; opacity: 0.9; } to { r: 26; opacity: 0; } }
@media (max-width: 640px) {
  .codex-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
}

/* ------------------------------------------------------------ stage card */
#stage-card {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: baseline;
  justify-content: center;
  padding-top: 44vh;
  gap: 14px;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--sc-fade, 1s) ease-in-out;
  color: var(--light);
}
#stage-card.is-on { opacity: 1; }
#stage-card .sc-num { font-size: clamp(34px, 5vw, 56px); font-weight: 200; letter-spacing: 0.18em; font-variant-numeric: tabular-nums; }
#stage-card .sc-total { font-size: 12px; letter-spacing: 0.3em; color: var(--muted); }

/* ------------------------------------------------------------- code card */
/* The code for 12 / 13, shown for three seconds before the dark begins. */
#code-card {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--sc-fade, 0.8s) ease-in-out;
  color: var(--light);
}
#code-card[hidden] { display: none; }
#code-card.is-on { opacity: 1; }
#code-card .cc-label { font-size: 11px; letter-spacing: 0.4em; color: var(--muted); }
#code-card .cc-digits { font-size: clamp(30px, 4.4vw, 48px); font-weight: 200; letter-spacing: 0.32em; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- ranking */
.title-board {
  position: absolute;
  top: calc(14px + var(--sat));
  right: calc(18px + var(--sar));
  font-size: 11px;
  letter-spacing: 0.24em;
  color: #4a4a4a;
}
#name-card[hidden] { display: none; }
.name-form { display: flex; flex-direction: column; align-items: center; width: min(320px, 100%); }
.name-title { margin: 0; font-weight: 300; font-size: 13px; letter-spacing: 0.4em; margin-right: -0.4em; color: var(--light-2); }
.name-ko { margin: 14px 0 26px; font-size: 12px; color: #6a6a6a; letter-spacing: 0.06em; }
#name-input, .name-field {
  width: 100%;
  background: transparent;
  border: 0;
  border-bottom: 1px solid #2a2a2a;
  border-radius: 0;
  color: var(--light);
  font: inherit;
  font-size: 18px;
  letter-spacing: 0.12em;
  text-align: center;
  padding: 8px 0;
  outline: none;
  user-select: text;
}
#name-input:focus, .name-field:focus { border-bottom-color: #777; }
#name-input::placeholder { color: #333; }
.name-note { margin: 18px 0 20px; font-size: 11px; line-height: 1.7; color: #4a4a4a; }
.name-form .text-btn { font-size: 12px; letter-spacing: 0.26em; color: #8a8a8a; }
.name-field { width: 46%; font-size: 14px; text-align: right; letter-spacing: 0.04em; padding: 6px 0; }

.board-tabs { display: flex; flex-wrap: wrap; gap: 2px 4px; margin: 0 0 6px; }
.board-tabs button {
  min-width: 40px;
  min-height: 36px;
  padding: 0 6px;
  background: none;
  border: 0;
  border-bottom: 1px solid transparent;
  color: #555;
  font: inherit;
  font-size: 12px;
  letter-spacing: 0.08em;
  cursor: pointer;
}
.board-tabs button[aria-selected="true"] { color: #e5e5e5; border-bottom-color: #777; }
.board-tabs button:focus-visible { outline: 1px solid #555; }
.board-tabs .wide { min-width: 0; padding: 0 10px 0 0; }
.board-sub { margin: 10px 0 6px; font-size: 11.5px; color: #555; letter-spacing: 0.04em; min-height: 1.5em; }
.board-list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--edge); }
.board-list li {
  display: grid;
  grid-template-columns: 3em 1fr auto;
  align-items: baseline;
  gap: 10px;
  padding: 11px 0;
  border-bottom: 1px solid #0d0d0d;
  font-size: 13.5px;
  color: #9a9a9a;
}
.board-list .rk { color: #555; font-variant-numeric: tabular-nums; }
.board-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board-list .tm { font-variant-numeric: tabular-nums; color: #b5b5b5; }
.board-list li.me { color: #fff; }
.board-list li.me .rk, .board-list li.me .tm { color: #fff; }
.board-list li.empty { display: block; color: #444; font-size: 12.5px; }
.board-me { margin: 12px 0 0; font-size: 12.5px; color: #8a8a8a; min-height: 1.5em; }

.end-lines .l3 { margin-top: 1.6em; font-size: 12px; color: #8a8a8a; line-height: 2; letter-spacing: 0.16em; }
.end-lines .l3 .ko { display: block; letter-spacing: 0.06em; color: #555; }
#end.is-on .l3 { opacity: 1; transition-delay: 3.2s; }
/* times are announced while the light is on: keep them legible over the floor glow */
#whisper { text-shadow: 0 0 8px #000, 0 0 2px #000; }

/* ------------------------------------------------------- line breaking */
/* Korean breaks between words, not inside them, and no line ends up with a
   lone syllable; CJK/latin follow their own rules */
body { word-break: keep-all; overflow-wrap: break-word; }
p, small, .row span, li, h2, h3 { text-wrap: pretty; }
.name-note, .name-ko, #whisper, .codex-intro, .end-lines .ko, .res-rank, .board-sub { text-wrap: balance; }
:root[data-lang="ja"] body, :root[data-lang="zh"] body { word-break: normal; line-break: strict; }

/* "RANKING · 순위" — the local word after a dot, hidden in English */
.dot-sub::before { content: "·"; margin: 0 0.55em; }
.dot-sub[hidden] { display: none; }

.lang-select {
  background: transparent;
  color: #b5b5b5;
  border: 0;
  border-bottom: 1px solid #2a2a2a;
  font: inherit;
  font-size: 14px;
  padding: 6px 0;
  text-align: right;
  cursor: pointer;
}
.lang-select option { background: #0b0b0b; color: #ddd; }
.lang-select:focus-visible { outline: 1px solid #777; outline-offset: 3px; }

/* ---------------------------------------------------------------- result */
#result { background: rgba(0, 0, 0, 0.9); justify-content: flex-start; overflow-y: auto; padding-top: calc(10vh + var(--sat)); }
#result[hidden] { display: none; }
.res-inner { width: min(420px, 100%); display: flex; flex-direction: column; align-items: stretch; text-align: left; }
.res-num { margin: 0; font-size: 11px; letter-spacing: 0.34em; color: #555; text-align: center; }
.res-time { margin: 18px 0 0; font-size: clamp(30px, 6vw, 44px); font-weight: 300; letter-spacing: 0.08em; color: var(--light); text-align: center; font-variant-numeric: tabular-nums; }
.res-rank { margin: 12px 0 26px; font-size: 13px; letter-spacing: 0.08em; color: #8a8a8a; text-align: center; min-height: 1.5em; }
.res-me { margin: 12px 0 0; font-size: 12.5px; color: #8a8a8a; min-height: 1.5em; }
.res-next { align-self: center; margin-top: 26px; font-size: 12px; letter-spacing: 0.3em; color: #d5d5d5; }
/* Japanese: break at phrase boundaries where the browser supports it */
:root[data-lang="ja"] body { word-break: auto-phrase; }
