:root {
  color-scheme: dark;
  font-family: Inter, ui-rounded, "Arial Rounded MT Bold", system-ui, sans-serif;
  background: #05040f;
  color: #f8fbff;
  --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);
  --chrome: 15.5rem;
  --accent: #76f7d2;
  --panel: #121833f2;
  --line: #ffffff22;
}

* { box-sizing: border-box; }

html, body {
  min-height: 100%;
  min-height: 100dvh;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  display: grid;
  place-items: center;
  padding: max(16px, var(--safe-top)) max(16px, var(--safe-right)) max(16px, var(--safe-bottom)) max(16px, var(--safe-left));
  overflow-x: hidden;
  overscroll-behavior: none;
  background:
    radial-gradient(ellipse 50% 40% at 20% 18%, #5b2d8fcc 0%, transparent 55%),
    radial-gradient(ellipse 45% 35% at 78% 30%, #1f6b9bcc 0%, transparent 50%),
    radial-gradient(ellipse 40% 30% at 60% 75%, #7a2a6e99 0%, transparent 55%),
    radial-gradient(circle at 50% 100%, #12081f 0%, #05040f 55%, #02010a 100%);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

body.modal-open { overflow: hidden; }

.game-shell {
  width: min(100%, 1060px);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

header, footer, .scoreboard {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

header { margin-bottom: 14px; min-width: 0; }
header > div { min-width: 0; }

.header-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--accent);
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .16em;
  text-transform: uppercase;
}

h1 { margin: 0; font-size: clamp(1.4rem, 5vw, 3.2rem); line-height: .95; letter-spacing: .04em; }

button {
  border: 0;
  border-radius: 999px;
  padding: 11px 18px;
  min-height: 44px;
  font: inherit;
  font-weight: 800;
  color: #11152a;
  background: var(--accent);
  cursor: pointer;
  box-shadow: 0 5px 0 #2d9f88;
  touch-action: manipulation;
}

button:active { transform: translateY(3px); box-shadow: 0 2px 0 #2d9f88; }
.icon-button { color: #fff; background: #ffffff16; box-shadow: none; border: 1px solid #ffffff2b; flex-shrink: 0; }
.icon-button:active { transform: none; box-shadow: none; }
.ghost-button {
  width: 100%;
  margin-top: 12px;
  color: #d7defa;
  background: #ffffff10;
  box-shadow: none;
  border: 1px solid var(--line);
}
.ghost-button:active { transform: none; box-shadow: none; }

.scoreboard {
  justify-content: flex-start;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.scoreboard div {
  min-width: 0;
  flex: 1 1 96px;
  padding: 8px 14px;
  border-radius: 14px;
  background: #ffffff0d;
  border: 1px solid #ffffff14;
}

.scoreboard-hidden { display: none !important; }
.scoreboard-best strong { color: var(--accent); }

.scoreboard span { display: block; color: #aab4dc; font-size: .72rem; text-transform: uppercase; letter-spacing: .1em; }
.scoreboard strong {
  display: block;
  font-size: clamp(1.1rem, 3.5vw, 1.4rem);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.canvas-wrap {
  position: relative;
  overflow: hidden;
  width: min(100%, calc((100dvh - var(--chrome)) * 16 / 9));
  max-width: 100%;
  aspect-ratio: 16 / 9;
  margin-inline: auto;
  border: 3px solid #ffffff22;
  border-radius: 24px;
  background: #07051a;
  box-shadow: 0 25px 70px #0008, inset 0 0 60px #5a7aff12;
}

.level-progress {
  position: absolute;
  z-index: 2;
  top: 12px;
  left: 4%;
  width: 92%;
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: #ffffff20;
}

.level-progress span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #76f7d2, #ffe66d);
  transition: width .08s linear;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  touch-action: none;
}

.game-message {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  text-align: center;
  padding: clamp(12px, 4vw, 24px);
  background: #080b1d99;
  backdrop-filter: blur(3px);
}

.game-message.hidden { display: none; }
.game-message p { margin: 7px 0; color: #c3cae9; }
.game-message .message-kicker { color: var(--accent); font-weight: 900; text-transform: uppercase; letter-spacing: .15em; }
.game-message h2 { margin: 0; font-size: clamp(1.6rem, 7vw, 4.5rem); }
kbd { padding: 3px 8px; border-radius: 6px; background: #fff; color: #11152a; font: inherit; font-size: .85em; }

footer { margin-top: 16px; color: #b9c2e5; }
footer p { margin: 0; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: max(16px, var(--safe-top)) max(16px, var(--safe-right)) max(16px, var(--safe-bottom)) max(16px, var(--safe-left));
  background: #080b1dcc;
  backdrop-filter: blur(6px);
  overflow: auto;
}

.modal.hidden { display: none; }

.modal-card {
  width: min(100%, 560px);
  max-height: min(92dvh, 900px);
  overflow: auto;
  padding: 18px;
  border-radius: 22px;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 24px 80px #000a;
  user-select: text;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.modal-header h2 {
  margin: 0;
  font-size: clamp(1.2rem, 4vw, 1.7rem);
}

.name-field {
  display: grid;
  gap: 8px;
  margin-bottom: 14px;
}

.name-field span,
.option-group legend {
  color: #aab4dc;
  font-size: .72rem;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.name-field input {
  width: 100%;
  min-height: 48px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #ffffff10;
  color: #fff;
  font: inherit;
  font-size: 1.05rem;
  font-weight: 700;
}

.name-field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.preview-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 16px;
}

#characterPreview {
  width: 96px;
  height: 96px;
  border-radius: 18px;
  border: 1px solid var(--line);
  background: #10162f;
  flex-shrink: 0;
}

#characterSummary {
  margin: 0;
  color: #c3cae9;
  font-weight: 700;
}

.option-group {
  margin: 0 0 14px;
  padding: 0;
  border: 0;
  min-inline-size: 0;
}

.option-group legend { margin-bottom: 8px; }

.option-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.option-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid var(--line);
  color: #f4f7ff;
  background: #ffffff10;
  box-shadow: none;
  font-size: .92rem;
}

.option-button:active { transform: none; box-shadow: none; }
.option-button.selected {
  border-color: var(--accent);
  background: #76f7d222;
  box-shadow: inset 0 0 0 1px #76f7d255;
}

.color-swatch {
  width: 14px;
  height: 14px;
  border-radius: 999px;
  border: 1px solid #ffffff55;
  flex-shrink: 0;
}

#saveCharacter { width: 100%; margin-top: 4px; }

.leaderboard-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.leaderboard-item {
  display: grid;
  grid-template-columns: 28px 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: #ffffff0c;
}

.leaderboard-rank {
  color: #aab4dc;
  font-weight: 800;
  text-align: center;
}

.leaderboard-avatar {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: #10162f;
}

.leaderboard-meta {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.leaderboard-meta strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-meta span {
  color: #aab4dc;
  font-size: .78rem;
}

.leaderboard-score {
  color: var(--accent);
  font-size: 1.05rem;
}

.leaderboard-empty {
  margin: 8px 0 0;
  color: #c3cae9;
  text-align: center;
}

.leaderboard-empty.hidden { display: none; }

@media (max-width: 700px) {
  :root { --chrome: 7rem; }

  html, body {
    height: 100%;
    height: 100dvh;
  }

  body {
    display: flex;
    align-items: stretch;
    justify-content: stretch;
    padding: max(6px, var(--safe-top)) 8px max(6px, var(--safe-bottom)) 8px;
    overflow: hidden;
  }

  .game-shell {
    width: 100%;
    height: 100%;
    min-height: 0;
  }

  header {
    margin-bottom: 6px;
    gap: 8px;
  }

  .eyebrow { display: none; }
  h1 { font-size: 1.15rem; }

  .header-actions { gap: 5px; }
  .icon-button {
    min-height: 34px;
    padding: 6px 10px;
    font-size: .78rem;
  }

  .scoreboard {
    gap: 4px;
    margin-bottom: 6px;
    flex-wrap: nowrap;
  }

  .scoreboard div {
    flex: 1 1 0;
    padding: 4px 6px;
    border-radius: 10px;
  }

  .scoreboard span { font-size: .55rem; letter-spacing: .06em; }
  .scoreboard strong { font-size: .95rem; }

  .canvas-wrap {
    flex: 0 0 auto;
    width: min(100%, calc((100dvh - var(--chrome)) * 16 / 9));
    max-width: 100%;
    aspect-ratio: 16 / 9;
    height: auto;
    max-height: calc(100dvh - var(--chrome));
    margin-top: auto;
    margin-bottom: auto;
    align-self: center;
    border-width: 2px;
    border-radius: 14px;
  }

  .level-progress { top: 8px; height: 6px; }

  footer {
    align-items: stretch;
    flex-direction: row;
    gap: 8px;
    margin-top: 6px;
  }

  footer p { display: none; }
  footer button {
    width: 100%;
    min-height: 40px;
    padding: 8px 14px;
  }

  .game-message p { font-size: .85rem; }
  .game-message h2 { font-size: clamp(1.4rem, 8vw, 2.4rem); }
  .modal-card { padding: 14px; border-radius: 18px; }
  .option-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .option-grid.presets,
  .option-grid.colors { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 380px) {
  :root { --chrome: 6.5rem; }
  h1 { font-size: 1.05rem; }
  .icon-button { padding: 6px 8px; font-size: .72rem; }
  .scoreboard strong { font-size: .88rem; }
}

@media (orientation: landscape) and (max-height: 540px) {
  :root { --chrome: 5.5rem; }

  body {
    place-items: stretch center;
    padding: max(4px, var(--safe-top)) max(8px, var(--safe-right)) max(4px, var(--safe-bottom)) max(8px, var(--safe-left));
    overflow: hidden;
    height: 100dvh;
  }

  .game-shell {
    width: 100%;
    height: 100%;
    max-height: none;
  }

  header { margin-bottom: 4px; gap: 8px; }
  .eyebrow { display: none; }
  h1 { font-size: clamp(1rem, 4.5vh, 1.5rem); }
  button { min-height: 32px; padding: 6px 12px; }
  .header-actions { gap: 5px; }
  .icon-button { padding: 6px 9px; font-size: .78rem; }

  .scoreboard { margin-bottom: 4px; gap: 4px; flex-wrap: nowrap; }
  .scoreboard div { padding: 3px 8px; }
  .scoreboard span { font-size: .55rem; }
  .scoreboard strong { font-size: .9rem; }

  .canvas-wrap {
    flex: 0 0 auto;
    width: min(100%, calc((100dvh - var(--chrome)) * 16 / 9));
    max-width: 100%;
    aspect-ratio: 16 / 9;
    height: auto;
    max-height: calc(100dvh - var(--chrome));
    margin-top: auto;
    margin-bottom: auto;
    align-self: center;
    border-radius: 12px;
  }

  footer { margin-top: 4px; gap: 8px; }
  footer p { display: none; }
  footer button { width: auto; margin-left: auto; min-height: 32px; }

  .modal { padding: 8px 12px; }
  .modal-card { max-height: 94dvh; padding: 12px; }
}

/* Native fullscreen + mobile immersive fill */
html:fullscreen,
html:-webkit-full-screen {
  background: #080b1d;
  height: 100%;
}

body.is-immersive,
body.is-fullscreen,
html:fullscreen body,
html:-webkit-full-screen body {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: 100dvh;
  margin: 0;
  padding: max(4px, var(--safe-top)) 6px max(4px, var(--safe-bottom)) 6px;
  overflow: hidden;
  background: #080b1d;
}

body.is-immersive .game-shell,
body.is-fullscreen .game-shell,
html:fullscreen .game-shell,
html:-webkit-full-screen .game-shell {
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  justify-content: stretch;
}

body.is-immersive .canvas-wrap,
body.is-fullscreen .canvas-wrap,
html:fullscreen .canvas-wrap,
html:-webkit-full-screen .canvas-wrap {
  flex: 0 0 auto;
  width: min(100%, calc((100dvh - var(--chrome)) * 16 / 9));
  max-width: 100%;
  aspect-ratio: 16 / 9;
  height: auto;
  max-height: calc(100dvh - var(--chrome));
  margin-top: auto;
  margin-bottom: auto;
  align-self: center;
  border-radius: 12px;
}

body.is-immersive .eyebrow,
body.is-fullscreen .eyebrow,
html:fullscreen .eyebrow,
html:-webkit-full-screen .eyebrow,
body.is-immersive footer p,
body.is-fullscreen footer p,
html:fullscreen footer p,
html:-webkit-full-screen footer p {
  display: none;
}

body.is-immersive h1,
body.is-fullscreen h1,
html:fullscreen h1,
html:-webkit-full-screen h1 {
  font-size: clamp(1rem, 3.5vw, 1.35rem);
}

body.is-immersive header,
body.is-fullscreen header,
html:fullscreen header,
html:-webkit-full-screen header {
  margin-bottom: 4px;
}

body.is-immersive .scoreboard,
body.is-fullscreen .scoreboard,
html:fullscreen .scoreboard,
html:-webkit-full-screen .scoreboard {
  margin-bottom: 4px;
  gap: 4px;
  flex-wrap: nowrap;
}

body.is-immersive .scoreboard div,
body.is-fullscreen .scoreboard div,
html:fullscreen .scoreboard div,
html:-webkit-full-screen .scoreboard div {
  padding: 3px 8px;
}

body.is-immersive footer,
body.is-fullscreen footer,
html:fullscreen footer,
html:-webkit-full-screen footer {
  margin-top: 4px;
}

body.is-immersive .icon-button,
body.is-fullscreen .icon-button,
html:fullscreen .icon-button,
html:-webkit-full-screen .icon-button {
  min-height: 34px;
  padding: 6px 10px;
  font-size: .78rem;
}

@media (orientation: landscape) {
  body.is-immersive,
  body.is-fullscreen,
  html:fullscreen,
  html:-webkit-full-screen {
    --chrome: 5rem;
  }

  body.is-immersive,
  body.is-fullscreen,
  html:fullscreen body,
  html:-webkit-full-screen body {
    padding: max(2px, var(--safe-top)) 6px max(2px, var(--safe-bottom)) 6px;
  }
}
