:root {
  color-scheme: light;
  --ink: #26231f;
  --paper: #f7f0de;
  --wall: #e8dfca;
  --yellow: #f4cc65;
  --orange: #ee805f;
  --red: #d64f43;
  --blue: #68a6aa;
  --shadow: rgba(38, 35, 31, 0.18);
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #cfc4aa;
  color: var(--ink);
  font-family: "Arial Rounded MT Bold", "Trebuchet MS", system-ui, sans-serif;
  touch-action: none;
  user-select: none;
}

button, input { font: inherit; }
button { color: inherit; }

.game-shell {
  width: 100%;
  min-width: 0;
  height: 100dvh;
  max-width: 1180px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--paper);
  border-inline: 3px solid var(--ink);
  box-shadow: 0 0 60px rgba(55, 48, 35, 0.24);
}

.topbar {
  min-height: 76px;
  padding: 11px clamp(14px, 3vw, 30px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--yellow);
  border-bottom: 3px solid var(--ink);
}

.name-button {
  display: grid;
  text-align: left;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}

.eyebrow, .card-kicker {
  font-size: 10px;
  letter-spacing: .18em;
  font-weight: 900;
}

.name-button strong {
  max-width: 58vw;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: clamp(24px, 4vw, 37px);
  line-height: .95;
  text-transform: uppercase;
}

.top-actions { display: flex; align-items: center; gap: 10px; }

.coin-counter {
  height: 46px;
  min-width: 104px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 3px 3px 0 var(--ink);
  font-size: 21px;
}

.coin-counter img { width: 24px; height: 24px; }

.icon-button, .utility-button {
  border: 3px solid var(--ink);
  background: var(--paper);
  border-radius: 10px;
  box-shadow: 3px 3px 0 var(--ink);
  font-weight: 900;
  cursor: pointer;
}

.icon-button { width: 46px; height: 46px; font-size: 24px; }
.icon-button:active, .utility-button:active, .weapon-button:active, .primary-button:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }

.room-wrap {
  position: relative;
  min-height: 240px;
  overflow: hidden;
  background-color: var(--wall);
  background-image:
    linear-gradient(rgba(255,255,255,.16) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.16) 1px, transparent 1px);
  background-size: 32px 32px;
  border-bottom: 3px solid var(--ink);
  transform: translate(var(--shake-x, 0), var(--shake-y, 0));
}

#gameCanvas { width: 100%; height: 100%; display: block; cursor: crosshair; }

.room-label {
  position: absolute;
  inset: 16px auto auto 50%;
  transform: translateX(-50%) rotate(-1deg);
  padding: 6px 10px;
  background: rgba(247, 240, 222, .82);
  border: 2px solid rgba(38, 35, 31, .72);
  font-size: 11px;
  letter-spacing: .1em;
  font-weight: 900;
  white-space: nowrap;
  pointer-events: none;
}

.state-pill {
  position: absolute;
  left: 12px;
  bottom: 12px;
  padding: 6px 9px;
  background: var(--ink);
  color: var(--paper);
  border-radius: 5px;
  font-size: 10px;
  letter-spacing: .09em;
  pointer-events: none;
}

.chaos-wrap {
  position: absolute;
  right: 13px;
  top: 14px;
  width: min(170px, 35vw);
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .08em;
  pointer-events: none;
}

.chaos-track { flex: 1; height: 12px; padding: 2px; border: 2px solid var(--ink); background: var(--paper); }
.chaos-track i { display: block; height: 100%; width: 0; background: var(--orange); transition: width .12s linear; }
.screen-flash { position: absolute; inset: 0; opacity: 0; pointer-events: none; background: white; }
.screen-flash.active { animation: flash .16s ease-out; }

.controls {
  min-width: 0;
  overflow: hidden;
  background: var(--paper);
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}

.weapon-scroll {
  width: 100%;
  min-width: 0;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  padding: 10px 14px 9px;
  scrollbar-width: thin;
}

.weapon-button {
  position: relative;
  flex: 0 0 78px;
  height: 78px;
  display: grid;
  place-items: center;
  padding: 5px 5px 4px;
  background: #fffaf0;
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 3px 3px 0 #a99f89;
  cursor: pointer;
}

.weapon-button img { width: 42px; height: 39px; object-fit: contain; }
.weapon-button .weapon-name { font-size: 9px; font-weight: 900; text-transform: uppercase; }
.weapon-button .weapon-price { position: absolute; inset: 5px 5px auto auto; padding: 2px 4px; background: var(--ink); color: white; border-radius: 4px; font-size: 8px; }
.weapon-button.locked img { filter: grayscale(1); opacity: .42; }
.weapon-button.locked::after { content: "LOCKED"; position: absolute; top: 34px; padding: 3px 5px; color: white; background: var(--red); transform: rotate(-7deg); font-size: 8px; font-weight: 900; }
.weapon-button.selected { background: var(--yellow); box-shadow: 3px 3px 0 var(--ink); transform: translateY(-3px); }

.utility-row {
  min-height: 46px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
}

.utility-button { min-height: 36px; padding: 5px 12px; font-size: 11px; letter-spacing: .04em; }
.utility-button.active { background: var(--blue); color: white; }
.utility-row p { margin: 0; text-align: center; font-size: 11px; color: #6e6658; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.overlay {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(38, 35, 31, .72);
  backdrop-filter: blur(5px);
  opacity: 0;
  visibility: hidden;
  transition: opacity .18s ease, visibility .18s;
}

.overlay.open { opacity: 1; visibility: visible; }

.paper-card, .dialog-card {
  position: relative;
  width: min(430px, 100%);
  padding: clamp(24px, 6vw, 46px);
  text-align: center;
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: 18px;
  box-shadow: 10px 10px 0 var(--yellow), 14px 14px 0 var(--ink);
  transform: rotate(-.5deg);
}

.paper-card h1 { margin: 8px 0 28px; font-size: clamp(48px, 15vw, 76px); line-height: .79; letter-spacing: -.07em; }
.paper-card label { display: block; margin-bottom: 8px; font-size: 12px; letter-spacing: .12em; font-weight: 900; }
.paper-card p { margin: 20px 0 0; color: #746b5b; font-size: 12px; }

input {
  width: 100%;
  height: 52px;
  padding: 0 13px;
  border: 3px solid var(--ink);
  border-radius: 9px;
  background: #fffdf5;
  text-align: center;
  font-size: 22px;
  font-weight: 900;
  outline: 0;
  user-select: text;
}
input:focus { box-shadow: 0 0 0 4px rgba(244, 204, 101, .7); }

.character-picker {
  min-width: 0;
  margin: 14px 0 0;
  padding: 0;
  border: 0;
}

.character-picker legend {
  width: 100%;
  margin-bottom: 7px;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .12em;
}

.character-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.paper-card .character-option,
.dialog-card .character-option {
  display: block;
  margin: 0;
  cursor: pointer;
}

.character-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.character-card {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  padding: 5px 9px;
  border: 3px solid #aaa08c;
  border-radius: 10px;
  background: #fffaf0;
  box-shadow: 2px 2px 0 #aaa08c;
  font-size: 11px;
  letter-spacing: .08em;
}

.character-head { position: relative; width: 50px; height: 50px; }
.character-head img { display: block; width: 50px; height: 50px; }
.character-head i {
  position: absolute;
  inset: 19px 0 auto;
  font-family: Arial, sans-serif;
  font-size: 11px;
  font-style: normal;
  letter-spacing: 2px;
}

.character-option input:checked + .character-card {
  border-color: var(--ink);
  background: var(--yellow);
  box-shadow: 3px 3px 0 var(--ink);
}

.character-option input:focus-visible + .character-card { outline: 3px solid var(--blue); outline-offset: 2px; }
.character-picker.compact { margin-top: 12px; }
.character-picker.compact .character-card { min-height: 62px; }
.character-picker.compact .character-head,
.character-picker.compact .character-head img { width: 42px; height: 42px; }
.character-picker.compact .character-head i { top: 15px; font-size: 10px; }

.primary-button {
  width: 100%;
  min-height: 52px;
  margin-top: 13px;
  border: 3px solid var(--ink);
  border-radius: 9px;
  background: var(--orange);
  box-shadow: 4px 4px 0 var(--ink);
  font-size: 15px;
  font-weight: 900;
  letter-spacing: .08em;
  cursor: pointer;
}

.dialog-card { width: min(360px, 100%); padding: 32px; }
.dialog-card > img { width: 88px; height: 70px; object-fit: contain; margin-bottom: 12px; }
.dialog-card h2 { margin: 7px 0; font-size: 30px; }
.dialog-card p { min-height: 39px; margin: 8px 0; }
.close-button { position: absolute; right: 10px; top: 8px; border: 0; background: none; font-size: 28px; cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; clip: rect(0,0,0,0); overflow: hidden; }

@keyframes flash { 0% { opacity: .72; } 100% { opacity: 0; } }

@media (max-width: 620px) {
  .game-shell { border: 0; }
  .paper-card, .dialog-card {
    width: min(342px, calc(100vw - 36px));
    box-shadow: 6px 6px 0 var(--yellow), 9px 9px 0 var(--ink);
  }
  .topbar { min-height: 64px; padding: 8px 10px; border-bottom-width: 2px; }
  .eyebrow { font-size: 7px; }
  .name-button strong { font-size: 24px; }
  .coin-counter { min-width: 84px; height: 40px; padding: 0 9px; border-width: 2px; box-shadow: 2px 2px 0 var(--ink); font-size: 17px; }
  .coin-counter img { width: 21px; }
  .icon-button { width: 40px; height: 40px; border-width: 2px; box-shadow: 2px 2px 0 var(--ink); }
  .room-label { top: 8px; font-size: 8px; }
  .chaos-wrap { top: 40px; right: 8px; }
  .weapon-scroll { padding: 8px 9px; gap: 7px; }
  .weapon-button { flex-basis: 68px; height: 68px; border-width: 2px; box-shadow: 2px 2px 0 #a99f89; }
  .weapon-button img { width: 36px; height: 34px; }
  .utility-row { gap: 7px; padding: 0 9px; }
  .utility-button { padding-inline: 9px; }
  .utility-row p { font-size: 9px; }
  .paper-card { padding: 22px; }
  .paper-card h1 { margin-bottom: 18px; font-size: 52px; }
  .paper-card p { margin-top: 13px; }
}

@media (max-height: 610px) and (orientation: landscape) {
  .topbar { min-height: 54px; padding-block: 5px; }
  .weapon-button { height: 61px; flex-basis: 66px; }
  .weapon-button img { height: 29px; }
  .weapon-scroll { padding-block: 5px; }
  .utility-row { min-height: 38px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
