:root {
  --amber: #ffb347;
  --ember: #e0762a;
  --cream: #f6e7cc;
  --ink: #0b0d14;
  --pixel: 'Press Start 2P', monospace;
  --mono: 'VT323', 'Courier New', monospace;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--ink); color: var(--cream); font-family: var(--mono); overflow: hidden; }
body { -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; touch-action: none; overscroll-behavior: none; }
button, input { font-family: inherit; }
input, textarea { -webkit-user-select: text; user-select: text; touch-action: manipulation; }
.hidden { display: none !important; }
.screen { position: fixed; inset: 0; }

/* ---------------- login ---------------- */
#login { display: flex; align-items: center; justify-content: center; padding: 16px;
  background: radial-gradient(ellipse 70% 60% at 50% 62%, #3d2311 0%, #1a120c 40%, #07080c 100%); }
.login-card { width: min(460px, 100%); background: rgba(18, 14, 12, 0.92); border: 2px solid #5a3a1c; border-radius: 12px;
  padding: 26px 26px 20px; box-shadow: 0 0 70px rgba(255, 130, 40, 0.16), inset 0 0 0 1px #2a1c10; }
.brand { font-family: var(--pixel); font-size: clamp(20px, 5.5vw, 28px); color: var(--amber); letter-spacing: 2px; text-align: center;
  text-shadow: 0 0 18px rgba(255, 140, 40, 0.55), 3px 3px 0 #3a1a08; }
.brand-flame { display: inline-block; margin-right: 10px; animation: flick 1.2s infinite alternate ease-in-out; }
@keyframes flick { from { transform: scale(1) rotate(-3deg); filter: brightness(1); } to { transform: scale(1.08) rotate(3deg); filter: brightness(1.25); } }
.tagline { text-align: center; font-size: 20px; color: #d9b98c; margin: 10px 0 18px; line-height: 1.1; }
label { display: block; font-family: var(--pixel); font-size: 9px; color: #b88b5a; letter-spacing: 1px; margin: 12px 0 6px; text-transform: uppercase; }
#nick, #wallet, #chat-input { width: 100%; background: #0e0c0b; border: 2px solid #3d2a18; border-radius: 8px; color: #ffe9c9; font-size: 24px; padding: 10px 12px; outline: none; }
#nick:focus, #wallet:focus { border-color: var(--amber); }
.avatars { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
@media (max-width: 420px) { .avatars { grid-template-columns: repeat(3, 1fr); } }
.avatar { border: 2px solid #2b1e12; border-radius: 8px; padding: 6px 2px 4px; text-align: center; cursor: pointer; background: rgba(0,0,0,0.25); transition: transform .08s; }
.avatar:hover { transform: translateY(-2px); border-color: #6b4520; }
.avatar.selected { border-color: var(--amber); background: rgba(255, 150, 60, 0.1); box-shadow: 0 0 14px rgba(255, 150, 60, 0.25); }
.avatar canvas { width: 45px; height: 72px; image-rendering: pixelated; display: block; margin: 0 auto; }
.avatar span { display: block; font-size: 17px; color: #e5c9a3; margin-top: 3px; }
.btn-primary { width: 100%; margin-top: 18px; background: var(--ember); color: #1a0e05; border: 0; border-radius: 8px; padding: 16px 12px; font-family: var(--pixel); font-size: 13px; cursor: pointer; box-shadow: 0 4px 0 #8a4412; }
.btn-primary:active { transform: translateY(3px); box-shadow: 0 1px 0 #8a4412; }
.btn-primary:disabled { opacity: .6; cursor: default; }
.btn-ghost { background: transparent; color: #c9a57a; border: 2px solid #3d2a18; border-radius: 8px; padding: 12px; font-size: 20px; cursor: pointer; }
.hint { text-align: center; font-size: 18px; color: #8f7355; margin: 12px 0 0; }
.status { text-align: center; font-size: 19px; color: #ff9f7a; min-height: 22px; margin: 6px 0 0; }
.links { text-align: center; font-size: 19px; color: #8f7355; margin: 10px 0 0; }
.links a, .links .ca { color: #e5c9a3; text-decoration: none; border-bottom: 1px dotted #6b4520; background: none; border-top: 0; border-left: 0; border-right: 0; padding: 0; font-size: inherit; cursor: pointer; }
.links a:hover, .links .ca:hover { color: var(--amber); border-bottom-color: var(--amber); }

/* ---------------- game ---------------- */
#game { background: #0a0d16; }
#canvas { display: block; width: 100vw; height: 100vh; image-rendering: pixelated; image-rendering: crisp-edges; }
#hud { position: fixed; top: calc(10px + var(--sat)); left: calc(12px + var(--sal)); right: calc(12px + var(--sar)); display: flex; justify-content: space-between; align-items: flex-start; pointer-events: none; z-index: 5; }
.hud-right { text-align: right; }
.title { font-family: var(--pixel); font-size: 18px; color: var(--amber); letter-spacing: 2px; text-shadow: 0 0 16px rgba(255, 140, 40, 0.6), 2px 2px 0 #2a1408; }
.pill { display: inline-block; margin-top: 7px; background: rgba(8, 6, 6, 0.62); border: 1px solid rgba(255, 170, 90, 0.35); border-radius: 6px; padding: 6px 9px; font-family: var(--pixel); font-size: 10px; color: #ffd9a8; }
.hud-right .pill { display: block; margin-left: auto; width: max-content; }
#claim-pill { display: block; pointer-events: auto; cursor: pointer; border-color: #ffe27a; color: #ffe27a; background: rgba(60, 40, 10, 0.8); animation: pulse 0.9s infinite alternate; }
#claim-pill:hover { background: rgba(90, 60, 15, 0.9); }
#battle-pill.hot { border-color: #ff5a3c; color: #ffb4a0; animation: pulse 0.8s infinite alternate; }
#battle-pill.fight { border-color: #ff3b1f; color: #fff; background: rgba(120, 20, 10, 0.75); }
@keyframes pulse { from { box-shadow: 0 0 0 rgba(255, 80, 40, 0); } to { box-shadow: 0 0 14px rgba(255, 80, 40, 0.7); } }

#banners { position: fixed; top: 17%; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; gap: 12px; pointer-events: none; z-index: 6; }
#banner { width: 94vw; text-align: center; font-family: var(--pixel); font-size: clamp(16px, 3.6vw, 34px); line-height: 1.4; color: #fff1d6; text-shadow: 0 0 26px #ff8c2a, 3px 3px 0 #3a1a08; animation: pop .28s ease-out; }
#banner.huge { font-size: clamp(64px, 16vw, 150px); color: var(--amber); }
#banner.win { color: #ffe27a; text-shadow: 0 0 30px #ffb347, 3px 3px 0 #3a1a08; }
#subbanner { width: 92vw; text-align: center; font-size: clamp(18px, 3vw, 26px); color: #ffcf9a; text-shadow: 2px 2px 0 #000; }
@keyframes pop { from { transform: scale(1.45); } to { transform: scale(1); } }

#chat { position: fixed; left: calc(12px + var(--sal)); bottom: calc(12px + var(--sab)); width: min(400px, 72vw); z-index: 5; pointer-events: none; }
#chat-log { display: flex; flex-direction: column; justify-content: flex-end; max-height: 26vh; overflow: hidden; margin-bottom: 6px; font-size: 21px; line-height: 1.05; text-shadow: 1px 1px 0 #000, 0 0 6px rgba(0,0,0,.8); }
#chat-log .line { color: #f4e6cf; opacity: 0; animation: fadein .2s forwards; word-break: break-word; }
#chat-log .line.system { color: #ffb347; }
#chat-log .line.priv { color: #ff8a70; }
#chat-log .line .who { color: #9fd8ff; }
#chat-log .line.old { opacity: .45; }
@keyframes fadein { to { opacity: 1; } }
#chat-form { pointer-events: auto; }
#chat-input { font-size: 22px; padding: 8px 10px; background: rgba(0, 0, 0, 0.55); border: 1px solid rgba(255, 170, 90, 0.35); opacity: .75; }
#chat-input:focus { opacity: 1; border-color: var(--amber); background: rgba(10, 6, 4, 0.85); box-shadow: 0 0 0 2px rgba(255, 179, 71, 0.25), 0 0 18px rgba(255, 150, 60, 0.25); }

.hint-bar { position: fixed; bottom: calc(12px + var(--sab)); left: 50%; transform: translateX(-50%); font-size: 18px; color: rgba(255, 220, 170, 0.5); pointer-events: none; z-index: 4; white-space: nowrap; }

/* ---------------- touch controls ---------------- */
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 7; }
#joystick { position: absolute; left: calc(22px + var(--sal)); bottom: calc(26px + var(--sab)); width: 130px; height: 130px; border-radius: 50%; background: rgba(255, 255, 255, 0.07); border: 2px solid rgba(255, 200, 140, 0.35); pointer-events: auto; touch-action: none; }
#stick { position: absolute; left: 39px; top: 39px; width: 48px; height: 48px; border-radius: 50%; background: rgba(255, 190, 110, 0.55); border: 2px solid rgba(255, 230, 190, 0.7); }
#attack-btn { position: absolute; right: calc(22px + var(--sar)); bottom: calc(34px + var(--sab)); width: 104px; height: 104px; border-radius: 50%; border: 3px solid #ffb98a; background: radial-gradient(circle at 40% 35%, #ff9a4a, #c9501c 70%); color: #fff3e2; font-family: var(--pixel); font-size: 11px; pointer-events: auto; touch-action: none; box-shadow: 0 6px 0 #6e2a0e, 0 0 20px rgba(255, 120, 40, 0.35); }
#attack-btn:active, #attack-btn.pressed { transform: translateY(4px); box-shadow: 0 2px 0 #6e2a0e; }
#attack-btn.cooldown { filter: saturate(.4) brightness(.8); }
#chat-btn { position: absolute; right: calc(36px + var(--sar)); bottom: calc(158px + var(--sab)); width: 56px; height: 56px; border-radius: 50%; border: 2px solid rgba(255, 200, 140, 0.5); background: rgba(20, 14, 10, 0.7); font-size: 24px; pointer-events: auto; }
body.touch .hint-bar { display: none; }
body.touch #chat { bottom: calc(176px + var(--sab)); width: min(340px, 58vw); }
body.touch #chat-form { display: none; }
body.touch.chat-open #chat-form { display: block; }
body.touch.chat-open #joystick, body.touch.chat-open #attack-btn, body.touch.chat-open #chat-btn { display: none; }
body.touch.chat-open #chat { bottom: calc(12px + var(--sab) + var(--kb, 0px)); width: min(460px, 92vw); }
body.touch .title { font-size: 13px; }
body.touch .pill { font-size: 8px; padding: 5px 7px; }

/* ---------------- modal ---------------- */
.modal { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.62); z-index: 20; padding: 16px; }
.modal .box { width: min(440px, 100%); background: rgba(22, 16, 12, 0.97); border: 2px solid var(--amber); border-radius: 12px; padding: 22px; box-shadow: 0 0 60px rgba(255, 170, 70, 0.3); }
.modal h2 { font-family: var(--pixel); font-size: 20px; color: #ffe27a; margin: 0 0 12px; text-align: center; text-shadow: 0 0 18px rgba(255, 200, 80, 0.5); }
.modal p { font-size: 20px; color: #dcc2a0; margin: 0 0 12px; line-height: 1.1; }
.modal p.reward { color: #ffe27a; font-size: 23px; }
.modal p.reward:empty { display: none; }
.modal .row { display: flex; gap: 10px; align-items: stretch; margin-top: 4px; }
.modal .row .btn-primary { margin-top: 12px; flex: 1; }
.modal .row .btn-ghost { margin-top: 12px; }

/* ---------------- dev panel ---------------- */
#dev-toggle { position: fixed; right: calc(12px + var(--sar)); bottom: calc(40px + var(--sab)); z-index: 9; background: rgba(28, 10, 8, 0.85); color: #ff7a5c; border: 1px solid #b03a2a; border-radius: 6px; padding: 5px 8px; font-family: var(--pixel); font-size: 8px; cursor: pointer; }
#dev { position: fixed; right: calc(12px + var(--sar)); bottom: calc(72px + var(--sab)); width: 262px; background: rgba(28, 10, 8, 0.88); border: 1px solid #b03a2a; border-radius: 8px; padding: 10px; z-index: 8; font-size: 16px; }
.dev-title { font-family: var(--pixel); font-size: 9px; color: #ff7a5c; margin-bottom: 6px; }
#dev-readout { color: #ffd9a8; margin-bottom: 8px; font-size: 16px; }
.dev-row { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 6px; }
.dev-row button { background: #2a1a12; color: #ffd9a8; border: 1px solid #7a4a2a; border-radius: 4px; padding: 5px 7px; font-size: 15px; cursor: pointer; flex: 1 1 auto; }
.dev-row button.sell { border-color: #6a5a9a; color: #cbb8ff; }
.dev-row button:hover { background: #3d2418; }
body.touch #dev { width: 200px; top: calc(118px + var(--sat)); bottom: auto; right: calc(8px + var(--sar)); padding: 6px; }
body.touch #dev-toggle { top: calc(92px + var(--sat)); bottom: auto; right: calc(12px + var(--sar)); }
body.touch #dev .dev-row button { font-size: 13px; padding: 4px 5px; }
