:root {
  color-scheme: dark;
  --ink: #121418;
  --paper: #e6e2d8;
  --muted: #b1b4b2;
  --line: #3c4243;
  --warm: #e7bd80;
  --cool: #a7d5d1;
  font-family: "Segoe UI", system-ui, sans-serif;
  color: var(--paper);
  background: var(--ink);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: radial-gradient(ellipse at 48% 30%, #1c2226, var(--ink) 70%);
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible { outline: 2px solid var(--cool); outline-offset: 3px; }
p, h1 { margin: 0; }

.game-shell {
  --surface-height: 100vh;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --stick-size: clamp(66px, calc(var(--surface-height) * .27), 112px);
  --jump-size: clamp(60px, calc(var(--stick-size) * .82), 92px);
  --stage-width: max(0px, calc((var(--surface-height) - var(--safe-top) - var(--safe-bottom) - 106px) * 16 / 9));
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 6px;
  width: min(100%, 1220px);
  height: var(--surface-height);
  min-height: 228px;
  margin: 0 auto;
  padding: max(5px, var(--safe-top)) max(10px, var(--safe-right)) max(6px, var(--safe-bottom)) max(10px, var(--safe-left));
}

@supports (height: 100dvh) {
  .game-shell { --surface-height: 100dvh; }
}

.topbar {
  display: grid;
  grid-template-columns: minmax(100px, 1fr) minmax(0, 1fr) auto auto;
  align-items: center;
  gap: clamp(6px, 2vw, 24px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .12em;
  white-space: nowrap;
}

.brand svg { color: var(--warm); flex: none; }

.room {
  min-width: 0;
  padding-inline-start: 14px;
  border-inline-start: 1px solid var(--line);
}

#room-number, .meter-label, .screen-kicker {
  color: var(--muted);
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .1em;
  text-transform: uppercase;
}

#room-title {
  font: 500 clamp(17px, 2.2vw, 23px)/1.15 Georgia, serif;
  overflow-wrap: anywhere;
}

.meter, #beat-strip, .toolbar { display: flex; align-items: center; }
.meter { gap: 9px; }
#beat-strip { gap: 4px; }

#beat-strip span {
  display: grid;
  place-items: center;
  width: 25px;
  height: 26px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

#beat-strip span:is(.active, .is-active, .current, [aria-current="true"], [aria-current="step"], [data-active="true"]) {
  border-color: var(--warm);
  color: #17130e;
  background: var(--warm);
  box-shadow: 0 0 14px #e7bd802c;
}

.toolbar { gap: 6px; justify-content: flex-end; }

.tool {
  display: grid;
  place-items: center;
  min-height: 44px;
  min-width: 44px;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0 9px;
  background: #1c2126;
  font-size: 12px;
  white-space: nowrap;
}

.tool[aria-pressed="false"] { color: var(--muted); background: var(--ink); }
.tool.reset { font-size: 22px; line-height: 1; }
.tool.menu { align-content: center; padding: 2px 4px; color: var(--cool); }
.tool.menu span { font-size: 9px; line-height: 1; }
.tool.rotate { display: none; gap: 5px; color: var(--cool); }
.rotate svg { flex: none; }
.rotate-portrait, .rotate[aria-pressed="true"] .rotate-landscape { display: none; }
.rotate[aria-pressed="true"] .rotate-portrait { display: inline; }

.play-layout {
  display: grid;
  grid-template-columns: minmax(var(--stick-size), 1fr) minmax(0, var(--stage-width)) minmax(var(--stick-size), 1fr);
  align-items: center;
  gap: clamp(6px, 1.5vw, 16px);
}

.stage {
  grid-column: 2;
  grid-row: 1;
  position: relative;
  overflow: hidden;
  border: 1px solid #575d59;
  border-radius: 8px;
  background: #1c2228;
  box-shadow: 0 12px 34px #0005;
}

#game {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  touch-action: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.screen {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 8px;
  overflow: auto;
  background: radial-gradient(ellipse at 50% 45%, #13191ae8 15%, #111316bb 61%, #10131886);
  text-align: center;
}

.screen-content { display: grid; justify-items: center; gap: clamp(5px, 1vw, 13px); max-width: 540px; }
.screen-kicker { color: var(--cool); }

.screen-title {
  font: 500 clamp(25px, calc(var(--surface-height) * .13), 68px)/.98 Georgia, serif;
  letter-spacing: -.035em;
  text-wrap: balance;
  text-shadow: 0 4px 24px #090a09;
}

.screen-copy {
  max-width: 33ch;
  color: #d3d5ce;
  font-size: clamp(12px, 1.45vw, 16px);
  line-height: 1.36;
  text-wrap: pretty;
}

.screen-copy:empty { display: none; }
.finish-reward { color: var(--warm); font-weight: 650; }

.action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  min-height: 44px;
  padding: 0 19px;
  border: 1px solid #f0c890;
  border-radius: 4px;
  background: var(--warm);
  color: #18140e;
  font-weight: 650;
  box-shadow: 0 0 20px #e7bd8022;
}

.pad {
  display: grid;
  place-items: center;
  grid-row: 1;
  justify-self: center;
  padding: 0;
  border: 1px solid #545f62;
  border-radius: 50%;
  background: linear-gradient(160deg, #262e34, #191f24);
  color: var(--paper);
  touch-action: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.joystick {
  --stick-x: 0px;
  --stick-y: 0px;
  position: relative;
  grid-column: 1;
  width: var(--stick-size);
  height: var(--stick-size);
  background: radial-gradient(circle, #162026, #263238);
  color: var(--cool);
  cursor: grab;
}

.joystick svg { width: 100%; pointer-events: none; }

#joystick-knob {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 44%;
  aspect-ratio: 1;
  border: 1px solid #779694;
  border-radius: 50%;
  background: radial-gradient(circle at 38% 31%, #567574, #283a40 75%);
  box-shadow: 0 3px 8px #101719b0, inset 0 0 0 2px #a7d5d113;
  pointer-events: none;
  transform: translate(calc(-50% + var(--stick-x)), calc(-50% + var(--stick-y)));
  transition: transform 120ms ease-out;
}

.joystick.is-active #joystick-knob { transition: none; }

.jump {
  grid-column: 3;
  grid-auto-rows: max-content;
  align-content: center;
  gap: 1px;
  width: var(--jump-size);
  height: var(--jump-size);
  border-color: #667d7d;
  color: var(--cool);
}

.jump span { font-size: 10px; font-weight: 650; }

.pad:is(:active, .pressed, .is-pressed, .held, [aria-pressed="true"]) {
  border-color: var(--cool);
  background: #35494b;
  color: #fff;
}

@media (hover: hover) {
  .action:hover { background: #f3d19e; }
  .tool:hover, .pad:hover { border-color: var(--cool); }
}

.feedback {
  display: flex;
  flex-wrap: wrap;
  gap: 3px 15px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--muted);
}

#status { color: var(--warm); }

.keyboard-hint {
  display: none;
  width: 100%;
  color: var(--muted);
  font-size: 11px;
}

@media (hover: hover) and (pointer: fine) and (min-width: 600px) {
  .keyboard-hint { display: block; }
}

@media (orientation: portrait) {
  .tool.rotate { display: flex; }

  html:not(.landscape-locked) .game-shell {
    --stick-size: clamp(78px, 22vw, 105px);
    grid-template-rows: auto auto auto;
    align-content: start;
    gap: 12px;
    width: min(100%, 720px);
    height: auto;
    min-height: var(--surface-height);
    padding-top: max(12px, var(--safe-top));
  }

  html:not(.landscape-locked) .topbar {
    grid-template-columns: minmax(0, 1fr) auto auto;
    gap: 7px;
  }

  html:not(.landscape-locked) .brand { grid-area: 1 / 1; white-space: normal; }
  html:not(.landscape-locked) .rotate { grid-area: 1 / 2; }
  html:not(.landscape-locked) .toolbar { grid-area: 1 / 3; }
  html:not(.landscape-locked) .room { grid-area: 2 / 1 / 3 / 3; padding: 0; border: 0; }
  html:not(.landscape-locked) .meter { grid-area: 2 / 3; justify-self: end; }
  html:not(.landscape-locked) .meter-label { display: none; }
  html:not(.landscape-locked) .play-layout { grid-template-columns: 1fr auto; gap: 12px; }
  html:not(.landscape-locked) .stage { grid-column: 1 / -1; }
  html:not(.landscape-locked) .joystick { grid-row: 2; justify-self: start; }
  html:not(.landscape-locked) .jump { grid-area: 2 / 2; justify-self: end; }
  html:not(.landscape-locked) .screen-title { font-size: clamp(26px, 8vw, 43px); }

  html.landscape-locked, html.landscape-locked body { overflow: hidden; }

  html.landscape-locked .game-shell {
    --surface-height: 100vw;
    --safe-top: env(safe-area-inset-right, 0px);
    --safe-right: env(safe-area-inset-bottom, 0px);
    --safe-bottom: env(safe-area-inset-left, 0px);
    --safe-left: env(safe-area-inset-top, 0px);
    position: fixed;
    top: 0;
    left: 100vw;
    width: 100vh;
    width: 100dvh;
    min-height: 0;
    transform: rotate(90deg);
    transform-origin: top left;
    background: radial-gradient(ellipse at 48% 30%, #1c2226, var(--ink) 70%);
  }

  html.landscape-locked .topbar {
    grid-template-columns: minmax(86px, 1fr) minmax(0, 1fr) auto auto auto;
    gap: 5px;
  }

  html.landscape-locked .meter-label, html.landscape-locked .keyboard-hint { display: none; }
}

@media (max-width: 540px), (orientation: landscape) and (max-height: 550px) {
  .brand { gap: 5px; font-size: 11px; }
  .brand svg { width: 19px; }
  .room { padding-inline-start: 8px; }
  #room-title { font-size: 15px; }
  #room-number { font-size: 9px; }
  .meter-label { display: none; }
  #beat-strip span { width: 21px; height: 22px; }
  .feedback { font-size: 11px; }
}

@media (orientation: landscape) and (max-height: 285px), (orientation: portrait) and (max-width: 340px) {
  .screen-kicker { display: none; }
  .screen-content { gap: 3px; }
}

@media (prefers-reduced-motion: reduce) {
  #joystick-knob { transition: none; }
}

.tower-menu {
  position: absolute;
  z-index: 5;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(7px, var(--safe-top)) max(9px, var(--safe-right)) max(7px, var(--safe-bottom)) max(9px, var(--safe-left));
  background: #101416ed;
}

.tower-dialog {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 8px;
  width: min(100%, 820px);
  height: min(100%, 468px);
  padding: clamp(9px, 2vw, 18px);
  border: 1px solid #586461;
  border-radius: 8px;
  background: radial-gradient(ellipse at 5% 0, #273638, #151c20 80%);
  box-shadow: 0 20px 50px #0008;
}

.tower-header,
.tower-chapter,
.tower-daily-details {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 9px;
}

.tower-header h2,
.tower-panel h3 {
  margin: 0;
  color: var(--paper);
  font: 500 clamp(16px, 3.6vw, 29px)/1.1 Georgia, serif;
}

.tower-panel h3 { font-size: clamp(17px, 2.8vw, 23px); }
.tower-header h2 { flex: 1; }

.tower-balance {
  color: var(--warm);
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}

.tower-back { min-height: 38px; }

.tower-tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.tower-tab {
  min-height: 40px;
  padding: 3px 5px;
  border: 1px solid #4d5b5b;
  border-radius: 4px;
  background: #1c262a;
  color: var(--paper);
  font-size: 12px;
  font-weight: 600;
}

.tower-tab[aria-pressed="true"] {
  border-color: var(--cool);
  background: #344b4c;
  color: #f3fbf8;
}

.tower-panel {
  min-height: 0;
  padding: 2px 2px 4px;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.tower-chapter { margin-bottom: 6px; }
.tower-chapter-nav { display: flex; gap: 4px; }
.tower-chapter-nav .tower-tab { min-height: 38px; min-width: 44px; }

.tower-rooms,
.tower-items {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.tower-items { grid-template-columns: repeat(4, minmax(0, 1fr)); }

.tower-room,
.tower-item {
  display: grid;
  align-content: center;
  gap: 3px;
  min-width: 0;
  min-height: 60px;
  padding: 6px 8px;
  border: 1px solid #4e5b5a;
  border-radius: 5px;
  background: #20292d;
  text-align: start;
}

.tower-room-title { font-size: 13px; font-weight: 650; }
.tower-room-meta, .tower-description, .tower-footer {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.3;
}

.tower-room:disabled { color: var(--muted); cursor: default; }
.tower-room.is-next, .tower-item.is-equipped { border-color: var(--warm); }
.tower-room.is-next .tower-room-meta, .tower-eyebrow { color: var(--warm); }
.tower-room:hover:enabled, .tower-chapter-nav .tower-tab:hover, .tower-item .tool:hover:enabled { border-color: var(--cool); }

.tower-daily-details {
  min-height: 80px;
  padding: 8px;
  border: 1px solid #4e5b5a;
  border-radius: 5px;
}

.tower-eyebrow { margin-bottom: 3px; font-size: 11px; }
.tower-daily-details .tower-description { margin-top: 4px; }
.tower-daily-details .action { flex: none; min-height: 40px; padding: 0 9px; font-size: 12px; }
.tower-items { margin-top: 6px; }
.tower-item .tool { min-height: 34px; padding: 3px; white-space: normal; }
.tower-item .tool:disabled { cursor: default; color: var(--muted); }

.tower-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-inline-end: 4px;
  border: 1px solid #ffffff46;
  border-radius: 50%;
  background: var(--swatch);
}

.tower-account-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  gap: 12px 18px;
}

.tower-account-copy { overflow-wrap: anywhere; }
.tower-account-copy .tower-description { margin-top: 5px; }

.tower-account-controls, .tower-account-form {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.tower-account-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
}

.tower-account-form label {
  display: grid;
  gap: 3px;
  color: var(--paper);
  font-size: 12px;
  font-weight: 600;
}

.tower-account-form input {
  width: 100%;
  min-height: 42px;
  padding: 7px 9px;
  border: 1px solid #586461;
  border-radius: 4px;
  background: #151c20;
  color: var(--paper);
  font: inherit;
  font-size: 16px;
}

.tower-account-form input:focus-visible {
  outline: 2px solid var(--cool);
  outline-offset: 1px;
}

.tower-account-form .action {
  justify-self: start;
  min-height: 40px;
  padding: 0 14px;
}

.tower-account-profile {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
}

.tower-account-feedback, .tower-shop-feedback {
  margin-top: 7px;
  color: var(--warm);
  font-size: 12px;
  line-height: 1.35;
}

.tower-account-feedback:empty, .tower-shop-feedback:empty { display: none; }
.tower-account-feedback[data-error="true"], .tower-shop-feedback[data-error="true"] { color: #f7a996; }
#tower-account :disabled { cursor: wait; }

.tower-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 12px;
  padding-top: 5px;
  border-top: 1px solid #4b5756;
}

@media (orientation: portrait) {
  html:not(.landscape-locked) .tower-dialog { height: min(100%, 570px); }
  html:not(.landscape-locked) .tower-rooms { grid-template-columns: 1fr; }
  html:not(.landscape-locked) .tower-items { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  html:not(.landscape-locked) .tower-account-grid { grid-template-columns: minmax(0, 1fr); }
}

@media (orientation: portrait) and (max-width: 420px) {
  html:not(.landscape-locked) .brand span { display: none; }
}

@media (orientation: portrait) and (max-width: 350px) {
  html:not(.landscape-locked) .rotate svg { display: none; }
  .tower-tabs .tower-tab { font-size: 10px; }
}

@media (orientation: landscape) and (max-height: 350px), (orientation: portrait) and (max-width: 350px) {
  .tower-dialog { gap: 5px; }
  .tower-header h2 { font-size: 19px; }
  .tower-tabs .tower-tab, .tower-back { min-height: 35px; }
  .tower-item { padding: 5px 6px; }
  .tower-chapter { margin-bottom: 3px; }
}
