@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Instrument+Serif:ital@0;1&display=swap');

:root {
  color-scheme: dark;
  --night: #0b111e;
  --panel: #151d2c;
  --edge: #293448;
  --ink: #fff1dc;
  --soft: #b8c1d5;
  --muted: #8e9ab2;
  --amber: #ffd28e;
  --violet: #c9b6fd;
  --serif: 'Instrument Serif', Georgia, serif;
  --sans: 'DM Sans', system-ui, sans-serif;
  background: var(--night);
  font-family: var(--sans);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; min-width: 280px; color: var(--ink); min-height: 100dvh;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
body::before {
  content: '';
  position: fixed;
  z-index: -1;
  inset: 0;
  background: radial-gradient(ellipse at 68% 52%, #222d4c 0, #151d2e 41%, #0b111e 76%);
}
button { color: inherit; font: inherit; cursor: pointer; touch-action: manipulation; }
button:focus-visible { outline: 2px solid var(--violet); outline-offset: 4px; }
button:disabled { opacity: .46; cursor: default; }

.shell.playing {
  --footer-height: calc(53px + env(safe-area-inset-bottom, 0px));
  --safe-height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  --stage-space: calc(var(--safe-height) - 251px);
  padding-bottom: calc(var(--footer-height) + 12px);
}

.shell { width: min(1040px, calc(100% - 60px)); margin: 0 auto; padding: max(16px, env(safe-area-inset-top)) 0 max(16px, env(safe-area-inset-bottom)); }
.masthead { height: 56px; display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.brand, .mast-actions { display: flex; align-items: center; gap: 10px; }
.brand { padding: 0; border: 0; background: none; letter-spacing: .12em; font-size: 10px; font-weight: 700; min-width: 0; }
.brand span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mast-actions, .brand-mark { flex-shrink: 0; }
.brand-mark {
  display: grid; place-items: center; width: 33px; height: 33px; border-radius: 50%;
  color: var(--amber); font-size: 19px; background: #241e28; border: 1px solid #594338;
  box-shadow: inset 0 0 12px #ffb04b19;
}
.icon-button {
  position: relative; display: grid; place-items: center; width: 42px; height: 42px; border-radius: 50%;
  background: #151e30; border: 1px solid var(--edge); color: #d8def0; font-size: 17px;
}
.icon-button:hover { background: #253049; }
.ranking-mark { display: flex; align-items: flex-end; gap: 2px; color: var(--amber); }
.ranking-mark i { width: 4px; height: 8px; border-radius: 1px; background: currentColor; }
.ranking-mark i:nth-child(2) { height: 15px; }
.ranking-mark i:nth-child(3) { height: 11px; }
.sound-off { position: absolute; width: 13px; height: 1px; background: currentColor; transform: rotate(-42deg) translateY(1px); }
#sound[aria-pressed='true'] { color: var(--amber); }
#sound[aria-pressed='true'] .sound-off { display: none; }

.intro {
  position: relative; min-height: clamp(555px, calc(100dvh - 115px), 758px); overflow: hidden;
  isolation: isolate; border-radius: 30px; border: 1px solid #2d3850;
  background: #0d1729; box-shadow: 0 28px 85px #02061070;
}
.hero-art {
  position: absolute; inset: 0 0 0 37%; z-index: -2;
  background: #111c34 url('assets/treehouse.webp') center 77% / cover no-repeat;
}
.intro::after {
  position: absolute; z-index: -1; content: ''; inset: 0;
  background: linear-gradient(90deg,#0d1729 32%,#0d1729f3 39%,#0d17294d 56%,transparent 74%),
    linear-gradient(0deg,#070f1d55,transparent 32%);
}
.intro-copy { display: flex; flex-direction: column; align-items: flex-start; padding: clamp(42px, 7vw, 78px); min-height: inherit; justify-content: center; }
.hero-star { color: var(--amber); font-size: 30px; line-height: .8; margin-bottom: 20px; }
h1 { font: 400 clamp(76px, 9.1vw, 136px)/.81 var(--serif); margin: 0; letter-spacing: -.035em; }
h1 em { font-weight: 400; color: var(--amber); }
.intro-bottom { margin-top: 32px; max-width: 310px; }
.intro-bottom p { margin: 0 0 20px; color: #d1d8e7; font-size: 16px; line-height: 1.5; }
.primary {
  background: var(--amber); color: #241a16; border: 1px solid #ffdfae; border-radius: 13px;
  min-height: 48px; padding: 0 20px; font-weight: 700; display: inline-flex; align-items: center;
  justify-content: center; gap: 9px; text-decoration: none; box-shadow: 0 4px 14px #ffbd631c;
}
.primary:hover { background: #ffe2b6; }
.primary:active { transform: translateY(1px); }
.start { min-width: 175px; min-height: 54px; }
.start > span:first-child { font-size: 11px; }
.intro-footer { display: flex; flex-wrap: wrap; align-items: center; gap: 0 12px; margin-top: 10px; }
.intro-bottom .intro-meta { margin: 0; color: #a3afc7; font-size: 12px; font-weight: 600; }
.intro-meta i { padding: 0 3px; color: var(--amber); font-style: normal; }
.install-link.text-button { color: var(--amber); text-decoration: underline; text-underline-offset: .2em; }
.legal-link { display: inline-flex; align-items: center; text-decoration: underline; text-underline-offset: .2em; }

.shell.playing { width: min(460px, calc(100% - 32px)); }
.shell.playing .masthead { margin-bottom: 4px; }
.level-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; padding: 0 1px 9px; }
.level-picker {
  min-height: 33px; margin: 0 0 5px; padding: 0 9px; border-radius: 8px; border: 1px solid #34405a;
  background: #172035; color: var(--soft); font-size: 11px; font-weight: 700; letter-spacing: .075em; text-transform: uppercase;
}
.level-picker span:last-child { color: var(--amber); font-size: 16px; line-height: 1; margin-left: 5px; }
#levels { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.level-button { position: relative; min-height: 51px; padding: 0; border-radius: 12px; font-size: 17px; font-weight: 700; border: 1px solid #3e4a65; background: #202b42; color: #eef1fa; }
.level-button[aria-current='true'] { border-color: #ba9a68; color: var(--amber); background: #342c30; }
.level-button[data-cleared='true']::after { content: '✦'; position: absolute; color: var(--amber); right: 7px; top: 5px; font-size: 9px; }
.level-name h2 { margin: 0; font: 400 clamp(29px, 7vw, 41px)/.96 var(--serif); letter-spacing: -.01em; }
.clock { display: flex; align-items: baseline; gap: 3px; white-space: nowrap; padding-right: 2px; color: var(--amber); }
.clock-value { font-size: 27px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.1; }
.clock-unit { font-size: 12px; font-weight: 700; }
.clock.danger { color: #ffaaa7; }
.time-track { height: 3px; border-radius: 3px; background: #253049; overflow: hidden; margin: 0 0 10px; }
.time-track span { display: block; width: 100%; height: 100%; background: var(--amber); border-radius: 3px; transform-origin: left; }
.stage {
  width: 100%; aspect-ratio: 390 / 440; position: relative; isolation: isolate; overflow: hidden;
  border-radius: 19px; border: 1px solid #334058; background: #0e172c;
  box-shadow: 0 15px 32px #03071360;
}
.stage::before {
  content: ''; position: absolute; inset: 0; z-index: -1;
  background: linear-gradient(#08132768,#111c3496 72%,#081124ce), url('assets/treehouse.webp') center 49% / cover;
}
#world { width: 100%; height: 100%; display: block; }
.stage-stats { position: absolute; top: 11px; left: 11px; display: flex; gap: 5px; pointer-events: none; }
.stage-stats span, .replay-chip {
  font-size: 10px; font-weight: 700; line-height: 1; padding: 7px 8px; border: 1px solid #52608775;
  background: #10192ee8; color: #e2e8f6; border-radius: 20px;
}
#ghost-count { color: var(--violet); }
.replay-chip { position: absolute; top: 11px; right: 11px; color: var(--amber); letter-spacing: .05em; }
.replay-chip i { display: inline-block; background: currentColor; width: 5px; height: 5px; border-radius: 50%; margin: 0 3px 1px 0; }
.stage-status { position: absolute; inset: 0; display: grid; place-items: center; padding: 32px 17px 17px; background: #080d1bad; }
.status-card {
  width: min(285px, 100%); text-align: center; border: 1px solid #444e6a; border-radius: 21px;
  background: linear-gradient(155deg,#202943,#151d30); padding: 17px 18px 13px;
  box-shadow: 0 14px 40px #050a12b0;
}
#status-star { color: var(--amber); font-size: 20px; }
.status-card h3 { font: 400 35px/.95 var(--serif); margin: 6px 0 8px; }
.status-card p { color: var(--soft); font-size: 13px; line-height: 1.43; margin: 0 0 14px; }
.status-card .primary { width: 100%; }
.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 0 12px; }
.status-card .text-button { margin-top: 4px; }
.status-card .run-score { color: var(--amber); font-variant-numeric: tabular-nums; }

.clip-actions, .play-footer-actions { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.text-button { border: 0; background: none; min-height: 42px; padding: 4px 3px; color: var(--soft); font-weight: 600; font-size: 12px; }
.text-button:hover { color: var(--ink); }
.play-panel { margin-top: 11px; }
.controls { display: grid; grid-template-columns: 1.65fr minmax(48px, .62fr) 1.3fr; gap: 8px; touch-action: none; }
.movement { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.control {
  border: 1px solid #43516e; border-radius: 15px; min-height: 58px; width: 100%; padding: 0;
  background: linear-gradient(#23304a,#192238); color: #eef1fa;
  box-shadow: inset 0 1px #8294b025, 0 3px #080c17; touch-action: none;
}
.control > span { font-size: 36px; line-height: .75; vertical-align: .02em; font-weight: 500; }
.control.rewind { color: var(--violet); background: linear-gradient(#28243c,#201c33); border-color: #554771; }
.control.rewind span { font-size: 27px; line-height: 1; }
.control.jump { background: linear-gradient(#3c2e36,#30242e); color: #ffe1ad; border-color: #876647; font-weight: 700; font-size: 15px; }
.control.jump span { font-size: 21px; vertical-align: -.09em; margin-right: 3px; }
.control[data-held='true'] { background: #394b70; box-shadow: none; transform: translateY(2px); }
.control.jump[data-held='true'] { background: #674536; }
.keyboard-controls { margin: 10px 0 0; color: var(--muted); font-size: 11px; }
.keyboard-controls b { color: #8071a9; }
.play-footer {
  position: fixed; z-index: 2; inset: auto 0 0;
  padding: 4px max(10px, env(safe-area-inset-right, 0px)) calc(4px + env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
  background: #10192a; border-top: 1px solid #334058; box-shadow: 0 -6px 20px #080c174d;
}
.play-footer-actions { width: min(460px, 100%); min-height: 44px; margin: 0 auto; }
.play-footer .text-button { min-height: 44px; padding: 4px 12px; border-radius: 8px; }
#reset { margin-left: auto; }
.clip-panel { padding: 6px 0 0; }
.clip-panel p { color: var(--soft); margin: 6px 0 10px; font-size: 13px; min-height: 1.35em; }
.clip-actions .primary { margin-left: auto; }

.dialog { background: #192235; color: var(--ink); border: 1px solid #465474; border-radius: 22px; padding: 24px; width: min(390px, calc(100% - 32px)); }
.dialog::backdrop { background: #070b15bd; backdrop-filter: blur(5px); }
.dialog h2 { font: 400 40px/1 var(--serif); margin: 6px 0 14px; }
.dialog p { color: #c9d2e6; font-size: 14px; line-height: 1.5; }
.dialog strong { color: var(--ink); }
.dialog .primary { width: 100%; margin-top: 5px; }
.close-dialog { position: absolute; top: 11px; right: 11px; }
.dialog .level-progress { margin: -3px 0 14px; }
.dialog .chapter-level { margin-bottom: 12px; }
.challenge-option {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 9px;
  border: 1px solid #3d4963; border-radius: 12px; padding: 11px 13px; background: #202a40;
}
.challenge-option small { display: block; color: var(--soft); font-size: 12px; margin-top: 2px; }
.challenge-option input { width: 22px; height: 22px; accent-color: var(--violet); flex-shrink: 0; }
.challenge-option:has(:disabled) { opacity: .57; }
.ranking-dialog { width: min(450px, calc(100% - 24px)); max-height: calc(100dvh - 30px); }
.ranking-level, .nickname-form label { font-size: 13px; font-weight: 700; }
.ranking-level { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.ranking-dialog .ranking-rule { margin: 0 0 13px; color: var(--soft); font-size: 12px; }
.ranking-level select, #nickname {
  color: var(--ink); background: #10182b; border: 1px solid #465474; border-radius: 9px;
  min-height: 42px; padding: 6px 9px; font: inherit; min-width: 0;
}
.ranking-level select { flex: 1; max-width: 100%; }
.nickname-form { margin-bottom: 13px; }
.nickname-form > div { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; margin-top: 4px; }
#nickname { width: 100%; font-size: 16px; -webkit-user-select: text; user-select: text; -webkit-touch-callout: initial; }
.nickname-form .primary { min-height: 42px; margin: 0; padding: 0 12px; width: auto; font-size: 13px; border-radius: 9px; }
.nickname-form small { display: block; color: var(--soft); margin-top: 5px; font-size: 12px; line-height: 1.4; }
.nickname-form .ranking-consent { display: flex; gap: 6px; align-items: flex-start; margin-top: 8px; font-weight: 400; }
.ranking-consent input { accent-color: var(--violet); }
.ranking-dialog a, .legal-page a { color: var(--amber); text-underline-offset: .15em; }
.ranking-account { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 12px; margin-bottom: 9px; }
.ranking-player-actions { padding: 4px 9px; margin: 0 0 8px; background: #28243b; border: 1px solid #51446b; border-radius: 9px; }
.ranking-player-actions p { display: inline; margin: 0 8px 0 0; font-weight: 700; }
.ranking-player-actions button { margin-right: 7px; }
.ranking-dialog .ranking-personal { background: #292341; color: #e8dafe; border: 1px solid #53456d; border-radius: 9px; padding: 8px 10px; margin: 0 0 10px; font-size: 13px; }
.ranking-dialog .ranking-status { margin: 0 0 8px; font-size: 13px; }
.ranking-status:empty { display: none; }
#ranking-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 13px; font-variant-numeric: tabular-nums; }
#ranking-table th, #ranking-table td { padding: 8px 5px; border-bottom: 1px solid var(--edge); }
#ranking-table th { color: var(--soft); font-size: 11px; }
#ranking-table :is(th, td):first-child { padding-left: 0; }
#ranking-table :is(th, td):nth-child(n+3) { text-align: right; white-space: nowrap; }
#ranking-table td:nth-child(2) { overflow-wrap: anywhere; }
#ranking-table .own-score { color: var(--amber); }
.row-action { color: var(--soft); min-width: 34px; min-height: 34px; background: transparent; border: 0; font-weight: 700; }

.legal-page { -webkit-user-select: text; user-select: text; -webkit-touch-callout: initial; }
.legal-shell { width: min(640px, calc(100% - 36px)); margin: 0 auto; padding: 30px 0 52px; }
.legal-shell h1 { font-size: clamp(42px, 8vw, 72px); line-height: .96; margin: 24px 0 16px; }
.legal-shell h2 { font: 400 32px/1 var(--serif); margin: 26px 0 10px; }
.legal-shell p, .legal-shell li, .legal-shell label { color: #c9d2e6; font-size: 15px; line-height: 1.58; }
.legal-shell label { display: block; margin: 10px 0; }
.legal-shell input[type='text'] { display: block; width: 100%; margin: 4px 0 12px; padding: 10px; font: inherit; color: var(--ink); background: #10182b; border: 1px solid #465474; border-radius: 9px; }
.legal-shell .primary { margin: 8px 8px 8px 0; }

@media (max-width: 670px) {
  .shell { width: calc(100% - 24px); padding-top: max(5px, env(safe-area-inset-top)); }
  .masthead { height: 51px; margin-bottom: 8px; }
  .mast-actions { gap: 6px; }
  .intro { min-height: max(540px, calc(100dvh - 82px - env(safe-area-inset-top) - env(safe-area-inset-bottom))); border-radius: 24px; }
  .hero-art { inset: 0; background-position: center 65%; }
  .intro::after { background: linear-gradient(0deg,#080f1efc 3%,#080f1eea 12%,#080f1e23 41%,transparent 68%,#080f1e42 100%); }
  .intro-copy { justify-content: flex-start; align-items: center; text-align: center; padding: clamp(18px, 4.5dvh, 38px) 18px 18px; }
  .hero-star { font-size: 22px; margin: 0 0 12px; }
  h1 { font-size: clamp(58px, min(17.2vw, 9.9dvh), 97px); line-height: .84; text-shadow: 0 3px 16px #070b20ac; }
  .intro-bottom { margin-top: auto; max-width: 345px; width: 100%; padding-top: 150px; }
  .intro-bottom p { font-size: 14px; margin-bottom: 12px; }
  .start { width: 100%; }
  .intro-footer { justify-content: center; margin-top: 7px; }
  .shell.playing { width: min(430px, calc(100% - 24px), calc(var(--stage-space) * .8864)); min-width: min(300px, calc(100% - 24px)); }
}

@media (max-height: 720px) {
  .shell.playing {
    --stage-space: calc(var(--safe-height) - 246px);
    width: min(390px, calc(100% - 20px));
    min-width: 0;
  }
  .shell.playing .masthead { height: 42px; margin-bottom: 2px; }
  .shell.playing .level-head { padding-bottom: 7px; }
  .shell.playing .level-picker { min-height: 30px; margin-bottom: 4px; }
  .shell.playing .level-name h2 { font-size: clamp(24px, 6.4vw, 30px); }
  .shell.playing .time-track { margin-bottom: 8px; }
  .shell.playing .play-panel { margin-top: 8px; }
  .shell.playing .control { min-height: 49px; }
}

@media (max-width: 670px) and (max-height: 620px) {
  .intro { min-height: max(450px, calc(100dvh - 81px - env(safe-area-inset-top) - env(safe-area-inset-bottom))); }
  .intro-copy { padding-top: 16px; }
  .intro-bottom { padding-top: 96px; }
  .stage {
    width: min(100%, calc(max(165px, var(--stage-space)) * 390 / 440));
    margin-inline: auto;
  }
}

@media (orientation: landscape) and (max-height: 530px) {
  .shell.playing {
    --stage-space: calc(var(--safe-height) - 120px);
    width: min(100%, 680px);
    min-width: 0;
    padding-top: max(5px, env(safe-area-inset-top, 0px));
    padding-left: max(10px, env(safe-area-inset-left, 0px));
    padding-right: max(10px, env(safe-area-inset-right, 0px));
  }
  .shell.playing .masthead { height: 42px; margin-bottom: 8px; }
  .shell.playing .icon-button { width: 37px; height: 37px; }
  .game {
    display: grid; grid-template-columns: minmax(0, calc(max(158px, var(--stage-space)) * 390 / 440)) 173px;
    align-items: start; justify-content: center; gap: 0 12px;
  }
  .game .level-head { grid-column: 2; grid-row: 1; align-items: flex-start; gap: 6px; flex-direction: column; }
  .game .level-name h2 { font-size: clamp(23px, 5dvh, 29px); }
  .game .clock-value { font-size: 22px; }
  .game .time-track { grid-column: 2; grid-row: 2; }
  .game .stage {
    grid-column: 1; grid-row: 1 / span 3; width: 100%;
  }
  .game .play-panel, .game .clip-panel { grid-column: 2; grid-row: 3; }
  .game .play-panel { margin: 0; }
  .game .controls { grid-template-columns: 48px 1fr; gap: 8px; }
  .game .movement { grid-column: 1 / -1; gap: 8px; }
  .shell.playing .game .control { min-height: 44px; }
  .game .clip-actions { flex-wrap: wrap; gap: 4px; }
  .game .clip-actions .primary { min-height: 44px; padding: 0 12px; }
  .game .status-card { padding: 9px; }
  .game .status-card h3 { font-size: 25px; }
  .game .status-card p { margin-bottom: 7px; }
  .game .status-card .primary { min-height: 39px; padding: 0 8px; }
}

@media (orientation: landscape) and (max-height: 350px) {
  .shell.playing .masthead { height: 36px; margin-bottom: 6px; }
  .game .level-picker { min-height: 27px; }
  .game .level-head { gap: 4px; padding-bottom: 5px; }
  .game .time-track { margin-bottom: 6px; }
}

@media (pointer: coarse) {
  .shell, body:has(.shell):not(:has(dialog[open])) { touch-action: pan-y; }
  .stage { touch-action: none; }
  .keyboard-controls { display: none; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; } }
