:root {
  color-scheme: dark;
  --ink: #101b48;
  --ink-2: #1d2c67;
  --cream: #fff9e9;
  --yellow: #ffd51e;
  --orange: #ff6a1a;
  --red: #f23b38;
  --cyan: #36d8ff;
  --green: #44d06b;
  --white: #ffffff;
  --shadow: 0 10px 0 rgba(6, 12, 38, 0.28);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  margin: 0;
  background: #09102e;
  font-family: Inter, ui-rounded, "Arial Rounded MT Bold", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  color: var(--white);
  overflow-x: hidden;
  background:
    radial-gradient(circle at 18% -5%, rgba(54, 216, 255, 0.32), transparent 30rem),
    radial-gradient(circle at 88% 15%, rgba(255, 106, 26, 0.23), transparent 32rem),
    #09102e;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(1500px, 100%);
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.topbar {
  display: grid;
  grid-template-columns: minmax(230px, 1fr) auto minmax(230px, 1fr);
  align-items: center;
  gap: 18px;
  margin-bottom: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  flex: 0 0 auto;
  color: var(--ink);
  background: var(--yellow);
  border: 4px solid var(--white);
  border-radius: 18px 12px 19px 10px;
  box-shadow: 5px 5px 0 var(--orange);
  font-weight: 1000;
  font-size: 15px;
  transform: rotate(-4deg);
}

.brand h1,
.dialog-header h2 {
  margin: 0;
  font-weight: 1000;
  letter-spacing: -0.04em;
}

.brand h1 {
  font-size: clamp(21px, 2vw, 32px);
  line-height: 0.95;
  color: var(--yellow);
  text-shadow: 3px 3px 0 #e94824;
}

.brand p {
  margin: 5px 0 0;
  color: #a8b8ef;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.14em;
}

.hud {
  display: flex;
  gap: 8px;
  padding: 7px;
  background: rgba(16, 27, 72, 0.82);
  border: 2px solid rgba(255, 255, 255, 0.16);
  border-radius: 18px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(12px);
}

.hud-item {
  min-width: 76px;
  padding: 4px 12px 6px;
  text-align: center;
  border-right: 1px dashed rgba(255, 255, 255, 0.2);
}

.hud-item:last-child {
  border-right: 0;
}

.hud-item span {
  display: block;
  color: #9baee6;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.1em;
}

.hud-item strong {
  color: var(--white);
  font-size: 21px;
  line-height: 1;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.icon-button,
.character-button,
.secondary-button,
.upload-button,
.close-button {
  border: 0;
  color: var(--white);
  cursor: pointer;
}

.icon-button {
  width: 46px;
  height: 46px;
  border-radius: 15px;
  background: var(--ink-2);
  font-size: 20px;
}

.character-button {
  display: flex;
  align-items: center;
  gap: 9px;
  height: 48px;
  padding: 4px 15px 4px 5px;
  border: 3px solid var(--yellow);
  border-radius: 18px;
  background: #25377a;
  font-size: 14px;
  font-weight: 1000;
}

.mini-avatar {
  width: 35px;
  height: 35px;
  overflow: hidden;
  border: 2px solid var(--white);
  border-radius: 50%;
  background: var(--yellow);
}

.mini-avatar img {
  width: 330%;
  height: 330%;
  object-fit: cover;
  object-position: 50% 29%;
  transform: translate(-35%, -35%);
}

.rotate-tip {
  display: none;
  margin: 0 0 8px;
  padding: 9px 12px;
  border-radius: 12px;
  background: var(--yellow);
  color: var(--ink);
  text-align: center;
  font-size: 13px;
  font-weight: 1000;
}

.game-frame {
  position: relative;
  width: 100%;
  overflow: hidden;
  border: 5px solid var(--white);
  border-radius: 26px;
  background: #7ddcff;
  box-shadow: 0 12px 0 #263876, 0 28px 60px rgba(0, 0, 0, 0.38);
}

#gameCanvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1200 / 680;
  touch-action: none;
  user-select: none;
}

.game-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: none;
  place-items: center;
  padding: 18px;
  background: rgba(8, 15, 46, 0.6);
  backdrop-filter: blur(5px);
}

.game-overlay.is-visible {
  display: grid;
}

.overlay-card {
  width: min(470px, 92%);
  padding: clamp(20px, 3vw, 34px);
  border: 4px solid var(--ink);
  border-radius: 28px 18px 30px 20px;
  background: var(--cream);
  color: var(--ink);
  text-align: center;
  box-shadow: 9px 9px 0 var(--yellow), 15px 15px 0 rgba(8, 15, 46, 0.35);
  transform: rotate(-0.5deg);
}

.overlay-card h2 {
  margin: 5px 0 8px;
  font-size: clamp(27px, 4vw, 44px);
  line-height: 0.98;
  font-weight: 1000;
  letter-spacing: -0.04em;
}

.overlay-card > p:not(.eyebrow) {
  margin: 0 auto 20px;
  max-width: 34rem;
  font-size: 16px;
  line-height: 1.45;
  font-weight: 700;
}

.eyebrow {
  margin: 0;
  color: #e94d24;
  font-size: 12px;
  font-weight: 1000;
  letter-spacing: 0.12em;
}

.hero-avatar {
  position: relative;
  width: 92px;
  height: 92px;
  margin: -4px auto 13px;
  overflow: hidden;
  border: 5px solid var(--ink);
  border-radius: 50%;
  background: var(--yellow);
  box-shadow: 6px 6px 0 var(--orange);
}

.hero-avatar img {
  position: absolute;
  width: 100%;
  height: 100%;
  inset: 0;
  object-fit: contain;
  object-position: center;
}

.hero-avatar span {
  position: absolute;
  right: -3px;
  bottom: -4px;
  font-size: 30px;
}

.primary-button,
.secondary-button,
.upload-button {
  min-height: 48px;
  padding: 12px 20px;
  border-radius: 15px;
  font-weight: 1000;
  transition: transform 120ms ease, box-shadow 120ms ease, filter 120ms ease;
}

.primary-button {
  border: 3px solid var(--ink);
  color: var(--ink);
  background: var(--yellow);
  box-shadow: 0 6px 0 var(--orange);
  cursor: pointer;
}

.secondary-button {
  border: 2px solid rgba(255, 255, 255, 0.28);
  background: #263876;
  box-shadow: 0 5px 0 #111b49;
}

.primary-button:hover,
.secondary-button:hover,
.upload-button:hover,
.character-button:hover,
.icon-button:hover {
  filter: brightness(1.08);
}

.primary-button:active,
.secondary-button:active,
.upload-button:active,
.character-button:active,
.icon-button:active {
  transform: translateY(3px);
  box-shadow: none;
}

.result-emoji {
  font-size: 64px;
  line-height: 1;
}

.result-actions,
.editor-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.game-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 16px 4px 2px;
}

.instruction {
  display: flex;
  align-items: center;
  gap: 9px;
  color: #dfe7ff;
  font-size: 14px;
  letter-spacing: 0.04em;
}

.instruction span {
  font-size: 25px;
}

.control-actions {
  display: flex;
  gap: 9px;
}

.compact {
  min-height: 42px;
  padding: 8px 13px;
  font-size: 12px;
}

.toast {
  position: absolute;
  z-index: 3;
  top: 16%;
  left: 50%;
  padding: 10px 18px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  color: var(--ink);
  background: var(--yellow);
  box-shadow: 5px 5px 0 var(--orange);
  font-size: clamp(15px, 2vw, 24px);
  font-weight: 1000;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -15px) rotate(-2deg) scale(0.8);
  transition: 180ms ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0) rotate(-2deg) scale(1);
}

.character-dialog {
  width: min(850px, calc(100% - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  overflow: auto;
  border: 4px solid var(--yellow);
  border-radius: 26px;
  background: var(--ink);
  color: var(--white);
  box-shadow: 0 24px 90px rgba(0, 0, 0, 0.62);
}

.character-dialog::backdrop {
  background: rgba(4, 8, 28, 0.8);
  backdrop-filter: blur(6px);
}

.dialog-shell {
  padding: clamp(18px, 4vw, 34px);
}

.dialog-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 20px;
}

.dialog-header h2 {
  margin-top: 4px;
  font-size: clamp(24px, 4vw, 38px);
  color: var(--yellow);
}

.close-button {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: 14px;
  background: #263876;
  font-size: 30px;
  line-height: 1;
}

.character-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(125px, 1fr));
  gap: 12px;
}

.character-card {
  position: relative;
  padding: 12px 8px 10px;
  border: 3px solid rgba(255, 255, 255, 0.14);
  border-radius: 18px;
  background: #1a2a65;
  color: var(--white);
  text-align: center;
  cursor: pointer;
}

.character-card.is-selected {
  border-color: var(--yellow);
  box-shadow: inset 0 0 0 2px var(--orange);
}

.card-avatar {
  display: block;
  width: 78px;
  height: 78px;
  margin: 0 auto 8px;
}

.card-avatar canvas {
  display: block;
  width: 78px;
  height: 78px;
}

.character-card strong {
  display: block;
  overflow: hidden;
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.delete-character {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 27px;
  height: 27px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: var(--white);
  background: var(--red);
  cursor: pointer;
  font-weight: 1000;
}

.uploader {
  margin-top: 16px;
  padding: 18px;
  border: 2px dashed rgba(255, 255, 255, 0.24);
  border-radius: 18px;
  text-align: center;
}

.upload-button {
  background: var(--orange);
  box-shadow: 0 5px 0 #a52e19;
}

.uploader p {
  margin: 10px 0 0;
  color: #9fb0e6;
  font-size: 13px;
}

.face-editor {
  display: grid;
  grid-template-columns: minmax(220px, 0.8fr) minmax(260px, 1.2fr);
  gap: 28px;
  margin-top: 18px;
  padding: 20px;
  border-radius: 20px;
  background: #0c1644;
}

.face-editor[hidden] {
  display: none;
}

.editor-preview-wrap {
  display: grid;
  place-items: center;
  min-height: 280px;
  border-radius: 18px;
  background:
    linear-gradient(45deg, rgba(255,255,255,.05) 25%, transparent 25%, transparent 75%, rgba(255,255,255,.05) 75%),
    linear-gradient(45deg, rgba(255,255,255,.05) 25%, transparent 25%, transparent 75%, rgba(255,255,255,.05) 75%);
  background-position: 0 0, 12px 12px;
  background-size: 24px 24px;
}

#facePreview {
  width: min(260px, 100%);
  height: auto;
}

.editor-controls {
  display: grid;
  align-content: center;
  gap: 14px;
}

.editor-controls label {
  display: grid;
  gap: 7px;
  color: #c6d2f6;
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.05em;
}

.editor-controls input[type="text"],
.editor-controls input:not([type]) {
  width: 100%;
}

.editor-controls input {
  min-height: 42px;
  padding: 8px 11px;
  border: 2px solid #3a4f93;
  border-radius: 11px;
  color: var(--white);
  background: #182960;
}

.editor-controls input[type="range"] {
  min-height: 28px;
  padding: 0;
  accent-color: var(--yellow);
}

@media (max-width: 920px) {
  .topbar {
    grid-template-columns: 1fr auto;
  }

  .hud {
    grid-column: 1 / -1;
    grid-row: 2;
    justify-self: stretch;
    justify-content: center;
    order: 3;
  }

  .top-actions {
    grid-column: 2;
  }

  .hud-item {
    flex: 1;
  }
}

@media (orientation: portrait) and (max-width: 800px) {
  .rotate-tip {
    display: block;
  }
}

@media (max-width: 620px) {
  .app {
    padding-left: 8px;
    padding-right: 8px;
  }

  .brand-mark {
    width: 46px;
    height: 46px;
    border-width: 3px;
    border-radius: 14px;
    font-size: 12px;
  }

  .brand h1 {
    font-size: 18px;
  }

  .brand p {
    display: none;
  }

  .character-button {
    width: 47px;
    padding: 4px;
    font-size: 0;
  }

  .mini-avatar {
    width: 34px;
    height: 34px;
  }

  .game-frame {
    border-width: 3px;
    border-radius: 17px;
    box-shadow: 0 7px 0 #263876, 0 18px 38px rgba(0, 0, 0, 0.35);
  }

  .game-controls {
    align-items: stretch;
    flex-direction: column;
    padding-top: 12px;
  }

  .instruction {
    justify-content: center;
    font-size: 12px;
  }

  .control-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .face-editor {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .editor-preview-wrap {
    min-height: 220px;
  }

  #facePreview {
    width: 210px;
  }

  .overlay-card {
    padding: 16px;
  }

  .hero-avatar {
    width: 72px;
    height: 72px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
