:root {
  --panel: rgba(18, 24, 20, 0.72);
  --panel-solid: #1b241e;
  --line: rgba(255, 255, 255, 0.12);
  --text: #f4f1e8;
  --muted: #b9b3a4;
  --accent: #f2b33d;
  --good: #6fd36f;
  --bad: #ff6b5e;
  --hp: #e0483e;
  --hunger: #e8902c;
  --energy: #f2d03d;
  --xp: #59a8ff;
  --epic: #c77dff;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #0c120e; color: var(--text);
  font: 14px/1.35 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  user-select: none;
}
#game { position: fixed; inset: 0; }
#game canvas { display: block; }
.hidden { display: none !important; }

.btn {
  background: linear-gradient(#f6c453, #e09a20); color: #2a1a05; border: 0; border-radius: 10px;
  padding: 8px 14px; font-weight: 700; cursor: pointer; font-size: 14px;
  box-shadow: 0 2px 0 #9c6510; transition: transform .08s, filter .15s;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn:disabled { filter: grayscale(1) brightness(.7); cursor: default; }
.btn.big { font-size: 20px; padding: 12px 28px; width: 100%; }
.btn.alt { background: linear-gradient(#5a6a60, #3c4a42); color: var(--text); box-shadow: 0 2px 0 #222; }
.btn.epic { background: linear-gradient(#d39bff, #9b4fe0); color: #fff; box-shadow: 0 2px 0 #5b2390; }

.screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  z-index: 20;
}

/* Login */
#login { background: radial-gradient(ellipse at center, rgba(30, 60, 30, .55), rgba(5, 10, 6, .9)); }
.login-card {
  width: min(440px, calc(100vw - 32px)); background: var(--panel); border: 1px solid var(--line);
  backdrop-filter: blur(8px); border-radius: 18px; padding: 28px;
}
.login-card h1 { margin: 0; font-size: 44px; letter-spacing: 1px; text-align: center; }
.login-card h1 span { color: var(--accent); }
.login-card .sub { color: var(--muted); text-align: center; margin: 6px 0 20px; }
.login-card label { display: block; color: var(--muted); margin-bottom: 14px; }
.login-card input, #chat-input, .menu-card input {
  width: 100%; margin-top: 6px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: rgba(0, 0, 0, .35); color: var(--text); font-size: 16px; outline: none;
}
.login-card input:focus, #chat-input:focus { border-color: var(--accent); }
.auth-tabs { display: flex; gap: 4px; background: rgba(0, 0, 0, .3); border-radius: 12px; padding: 4px; margin-bottom: 14px; }
.auth-tabs button, .pick button {
  flex: 1; background: transparent; color: var(--muted); border: 0; border-radius: 9px; padding: 8px; font-size: 15px; font-weight: 600; cursor: pointer;
}
.auth-tabs button.active { background: rgba(242, 179, 61, .2); color: var(--accent); }
.reg-only { display: none !important; }
#login-forms.register .reg-only { display: block !important; }
.start-animal { color: var(--muted); margin-bottom: 14px; }
.pick { display: flex; gap: 8px; margin-top: 6px; }
.pick button { border: 1px solid var(--line); background: rgba(0, 0, 0, .25); color: var(--text); font-size: 16px; }
.pick button.active { border-color: var(--accent); background: rgba(242, 179, 61, .2); }
#login-resume { display: grid; gap: 10px; }
#login-resume.hidden { display: none; }
#resume-name { color: #3a2005; }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 14px; padding: 4px; }
.close.logout { font-size: 13px; color: var(--muted); }
.error { color: var(--bad); min-height: 20px; margin-top: 10px; text-align: center; }
.controls-help { margin-top: 16px; color: var(--muted); font-size: 12.5px; display: grid; gap: 3px; }
.controls-help b { color: var(--text); }

/* HUD */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#hud button, #hud input, #hud .card { pointer-events: auto; }
#hud-left { position: absolute; left: 14px; top: 14px; width: 240px; display: grid; gap: 6px; }
.portrait { display: flex; align-items: center; gap: 10px; font-size: 18px; font-weight: 700; text-shadow: 0 1px 3px #000; }
.lvl {
  width: 34px; height: 34px; border-radius: 50%; background: var(--xp); display: grid; place-items: center;
  font-size: 15px; border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, .4); order: -1;
}
.bar {
  position: relative; height: 18px; background: rgba(0, 0, 0, .45); border-radius: 9px; overflow: hidden;
  border: 1px solid rgba(255, 255, 255, .15);
}
.bar .fill { position: absolute; inset: 0; width: 50%; transition: width .2s; }
.bar span { position: relative; font-size: 11px; font-weight: 600; padding-left: 8px; line-height: 16px; text-shadow: 0 1px 2px #000; }
.bar.small { height: 14px; }
.bar.small span { line-height: 12px; font-size: 10px; }
.hp .fill { background: linear-gradient(90deg, #b8322b, var(--hp)); }
.hunger .fill { background: linear-gradient(90deg, #c2701b, var(--hunger)); }
.energy .fill { background: linear-gradient(90deg, #c9a823, var(--energy)); }
.xp .fill, .quest-bar .fill { background: linear-gradient(90deg, #2f78d0, var(--xp)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; backdrop-filter: blur(6px); }
#quest { margin-top: 6px; display: grid; gap: 6px; }
.quest-title { font-weight: 700; color: var(--accent); }
#quest-reward { color: var(--muted); font-size: 12px; }
#quest.done { border-color: var(--good); box-shadow: 0 0 12px rgba(111, 211, 111, .35); }

#hud-right { position: absolute; right: 14px; top: 14px; display: grid; gap: 8px; justify-items: end; }
.money { display: flex; gap: 8px; }
.money span { background: var(--panel); border: 1px solid var(--line); padding: 5px 10px; border-radius: 20px; font-size: 14px; }
#minimap { border-radius: 50%; border: 3px solid rgba(255, 255, 255, .5); background: #223; box-shadow: 0 4px 14px rgba(0, 0, 0, .45); }
#loc-name { background: var(--panel); padding: 3px 10px; border-radius: 10px; font-size: 12px; color: var(--muted); }

#target, #boss { position: absolute; left: 50%; transform: translateX(-50%); width: min(340px, 60vw); text-align: center; text-shadow: 0 1px 3px #000; }
#target { top: 14px; }
#boss { top: 58px; width: min(520px, 80vw); }
#target-name, #boss-name { font-weight: 700; margin-bottom: 3px; }
#boss-name { color: #ffb84d; font-size: 18px; }
.target-bar .fill { background: linear-gradient(90deg, #b8322b, #ef5a4c); }
.boss-bar { height: 22px; }
.boss-bar .fill { background: linear-gradient(90deg, #8a1fd0, #e04ad0); }

#toasts { position: absolute; top: 100px; left: 50%; transform: translateX(-50%); display: grid; gap: 6px; justify-items: center; }
.toast {
  background: var(--panel-solid); border: 1px solid var(--line); padding: 8px 16px; border-radius: 20px;
  animation: toast 3.5s forwards; box-shadow: 0 4px 12px rgba(0, 0, 0, .4); max-width: 80vw; text-align: center;
}
.toast.good { border-color: var(--good); color: #d9ffd6; }
.toast.bad { border-color: var(--bad); color: #ffd9d4; }
.toast.level { border-color: var(--accent); color: #ffe7a8; font-size: 18px; font-weight: 700; }
@keyframes toast { 0% { opacity: 0; transform: translateY(-8px); } 8% { opacity: 1; transform: none; } 85% { opacity: 1; } 100% { opacity: 0; } }

#chat { position: absolute; left: 14px; bottom: 14px; width: min(380px, 50vw); }
#chat-log { max-height: 180px; overflow: hidden; display: flex; flex-direction: column; justify-content: flex-end; gap: 2px; margin-bottom: 6px; }
#chat-log div { background: rgba(0, 0, 0, .38); padding: 3px 8px; border-radius: 8px; width: fit-content; max-width: 100%; text-shadow: 0 1px 2px #000; animation: fadeout 30s forwards; }
#chat-log b { color: var(--accent); }
#chat-log .sys { color: #b7e3ff; font-style: italic; }
#chat-input { margin: 0; font-size: 14px; padding: 7px 10px; opacity: .55; }
#chat-input:focus { opacity: 1; }
@keyframes fadeout { 0%, 80% { opacity: 1; } 100% { opacity: .25; } }

#actions { position: absolute; right: 16px; bottom: 16px; display: flex; gap: 10px; align-items: flex-end; }
.act {
  width: 64px; height: 64px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, .45);
  background: var(--panel); color: var(--text); font-size: 24px; cursor: pointer; display: grid; place-items: center;
  line-height: 1; padding-top: 4px;
}
.act small { font-size: 10px; color: var(--muted); }
.act.big { width: 86px; height: 86px; font-size: 34px; background: rgba(140, 40, 30, .7); }
.act:active { transform: scale(.95); }
#den-hint { position: absolute; bottom: 120px; left: 50%; transform: translateX(-50%); background: var(--panel); padding: 6px 14px; border-radius: 12px; border: 1px solid var(--accent); }

#dead { background: rgba(60, 0, 0, .45); }
.dead-card { text-align: center; background: var(--panel); padding: 24px 40px; border-radius: 16px; border: 1px solid var(--bad); }
.dead-card h2 { margin: 0 0 6px; color: var(--bad); }

/* Labels in the 3D view */
.label { text-align: center; pointer-events: none; text-shadow: 0 1px 3px #000, 0 0 2px #000; white-space: nowrap; transform: translateY(-6px); }
.label .nm { font-weight: 700; font-size: 13px; }
.label .lv { font-size: 11px; color: var(--muted); }
.label.npc .nm { font-size: 11px; font-weight: 600; }
.label.npc.danger .nm { color: #ff8a7a; }
.label.npc.prey .nm { color: #c8f5b8; }
.label.npc.boss .nm { color: #ffb84d; font-size: 16px; }
.label.fam .nm { font-size: 11px; color: #ffe7a8; }
.label .mini { width: 54px; height: 5px; background: rgba(0, 0, 0, .6); border-radius: 3px; margin: 2px auto 0; overflow: hidden; }
.label .mini i { display: block; height: 100%; background: #ef5a4c; }
.label.me .nm { color: #9fe0ff; }
.float { font-weight: 800; font-size: 18px; color: #fff; text-shadow: 0 2px 3px #000; pointer-events: none; animation: floatup 1s forwards; }
.float.crit { color: #ffd23f; font-size: 24px; }
.float.fin { color: #ff9a3c; font-size: 30px; }
.float.mine { color: #ffffff; }
.float.hurt { color: #ff6b5e; font-size: 20px; }
.float.miss { color: #9fe0ff; font-size: 16px; }
.float.warn { color: #ff3b2f; font-size: 34px; font-weight: 900; animation: warnpop .6s forwards; }
.float.reward { color: #ffe27a; font-size: 15px; animation: floatup 1.6s forwards; }
@keyframes warnpop { 0% { transform: scale(.4); opacity: 0; } 30% { transform: scale(1.3); opacity: 1; } 100% { transform: scale(1) translateY(-10px); opacity: 0; } }
#combo {
  position: absolute; left: 50%; top: 32%; transform: translateX(-50%) scale(.6); opacity: 0;
  font-size: 26px; font-weight: 900; color: #fff; text-shadow: 0 2px 6px #000, 0 0 12px rgba(255, 180, 60, .6); pointer-events: none; white-space: nowrap;
}
#combo.show { animation: combopop 1.3s forwards; }
#combo.fin { color: #ffb84d; font-size: 32px; }
@keyframes combopop { 0% { opacity: 0; transform: translateX(-50%) scale(.6); } 12% { opacity: 1; transform: translateX(-50%) scale(1.15); } 25% { transform: translateX(-50%) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translateX(-50%) scale(1) translateY(-12px); } }
#hurt-vignette { position: fixed; inset: 0; pointer-events: none; opacity: 0; background: radial-gradient(ellipse at center, transparent 55%, rgba(200, 0, 0, .5)); }
#hurt-vignette.on { animation: hurtflash .45s forwards; }
@keyframes hurtflash { 0% { opacity: 1; } 100% { opacity: 0; } }
.money .sound { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; color: var(--text); font-size: 14px; padding: 4px 9px; cursor: pointer; pointer-events: auto; }
@keyframes floatup { 0% { transform: translateY(0); opacity: 1; } 100% { transform: translateY(-50px); opacity: 0; } }

/* Menu */
#menu { background: rgba(0, 0, 0, .45); }
.menu-card {
  width: min(900px, calc(100vw - 24px)); height: min(640px, calc(100vh - 24px)); background: rgba(22, 30, 25, .94);
  border: 1px solid var(--line); border-radius: 18px; display: flex; flex-direction: column; overflow: hidden;
}
.menu-head { display: flex; align-items: center; border-bottom: 1px solid var(--line); padding: 8px; gap: 8px; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.tabs button, .close {
  background: transparent; color: var(--muted); border: 0; padding: 8px 14px; border-radius: 10px; cursor: pointer; font-size: 15px; font-weight: 600; white-space: nowrap;
}
.tabs button.active { background: rgba(242, 179, 61, .18); color: var(--accent); }
.close { font-size: 18px; }
#menu-body { flex: 1; overflow-y: auto; padding: 16px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.tile { background: rgba(255, 255, 255, .05); border: 1px solid var(--line); border-radius: 14px; padding: 12px; display: grid; gap: 6px; align-content: start; }
.tile.current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.tile.locked { opacity: .75; }
.tile h3 { margin: 0; font-size: 17px; }
.tile .meta { color: var(--muted); font-size: 12.5px; }
.tile .stats { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 12.5px; }
.swatch { display: inline-flex; width: 46px; height: 28px; border-radius: 8px; overflow: hidden; border: 1px solid rgba(255, 255, 255, .3); vertical-align: middle; }
.swatch i { flex: 1; }
.section-title { font-size: 16px; font-weight: 700; margin: 18px 0 10px; color: var(--accent); }
.section-title:first-child { margin-top: 0; }
.epic-tag { color: var(--epic); font-weight: 700; font-size: 11px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.up-row { display: grid; grid-template-columns: 110px 1fr auto; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.pips { display: flex; gap: 3px; }
.pips i { width: 14px; height: 10px; border-radius: 3px; background: rgba(255, 255, 255, .12); }
.pips i.on { background: var(--accent); }
table.top { width: 100%; border-collapse: collapse; }
table.top td, table.top th { padding: 8px; border-bottom: 1px solid var(--line); text-align: left; }
table.top th { color: var(--muted); font-weight: 600; }
table.top tr.me td { color: var(--accent); }
.chest-tile { text-align: center; }
.chest-tile .icon { font-size: 64px; }

#chest-result { background: rgba(0, 0, 0, .55); z-index: 30; }
.chest-card { background: var(--panel-solid); border: 1px solid var(--accent); border-radius: 18px; padding: 24px 36px; text-align: center; min-width: 280px; }
.chest-card.epic { border-color: var(--epic); box-shadow: 0 0 30px rgba(199, 125, 255, .5); }
.chest-anim { font-size: 72px; animation: pop .6s; }
@keyframes pop { 0% { transform: scale(.2) rotate(-20deg); } 70% { transform: scale(1.2) rotate(8deg); } 100% { transform: scale(1); } }
#chest-item { margin: 10px 0 16px; font-size: 16px; }

#loading { background: rgba(0, 0, 0, .7); flex-direction: column; gap: 12px; z-index: 40; }
.spinner { width: 42px; height: 42px; border: 4px solid rgba(255, 255, 255, .2); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 700px) {
  #hud-left { width: 170px; }
  #minimap { width: 120px; height: 120px; }
  #chat { width: 60vw; }
  #chat-log { max-height: 100px; }
}

/* Touch controls. They show only on a phone or a tablet. */
.touch-only, #joy-zone, .touch-help { display: none !important; }
body.touch .touch-only { display: grid !important; }
body.touch .touch-help { display: grid !important; }
body.touch .desktop-help { display: none !important; }
body.touch { -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
body.touch #game canvas { touch-action: none; }
body.touch button { touch-action: manipulation; }

body.touch #joy-zone {
  display: block !important; position: absolute; left: 0; bottom: 0;
  width: 45vw; height: 55vh; pointer-events: auto; touch-action: none;
}
#joy {
  position: absolute; left: calc(env(safe-area-inset-left) + 90px); top: calc(100% - 95px);
  width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, .08), rgba(255, 255, 255, .18));
  border: 2px solid rgba(255, 255, 255, .35); opacity: .55; transition: opacity .15s;
}
#joy.active { opacity: 1; }
#joy-knob {
  position: absolute; left: 50%; top: 50%; width: 54px; height: 54px; margin: -27px 0 0 -27px; border-radius: 50%;
  background: rgba(255, 255, 255, .75); box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
}

/* Buttons in an arc around the attack button. */
body.touch #actions {
  display: block; width: 250px; height: 250px;
  right: calc(env(safe-area-inset-right) + 12px); bottom: calc(env(safe-area-inset-bottom) + 12px);
}
body.touch .act { position: absolute; width: 58px; height: 58px; font-size: 22px; }
body.touch .act small { font-size: 9px; }
body.touch .act.pressed { transform: scale(.92); background: rgba(255, 255, 255, .3); }
body.touch .a-attack { right: 0; bottom: 0; width: 88px; height: 88px; font-size: 34px; }
body.touch .a-jump { right: 104px; bottom: 6px; }
body.touch .a-dodge { right: 172px; bottom: 4px; width: 54px; height: 54px; }
body.touch .a-run { right: 78px; bottom: 78px; }
body.touch .a-run.on { background: rgba(242, 179, 61, .55); border-color: var(--accent); }
body.touch .a-eat { right: 8px; bottom: 104px; }
body.touch .a-sleep { right: 168px; bottom: 76px; width: 50px; height: 50px; font-size: 18px; }
body.touch .a-menu { right: 76px; bottom: 166px; width: 50px; height: 50px; font-size: 18px; }
body.touch .a-chat { right: 150px; bottom: 150px; width: 46px; height: 46px; font-size: 18px; }

/* Chat sits between the joystick and the buttons; the input shows on demand. */
body.touch #chat {
  left: 50%; transform: translateX(-60%); bottom: calc(env(safe-area-inset-bottom) + 8px);
  width: min(320px, 34vw); pointer-events: none;
}
body.touch #chat-log { max-height: 84px; font-size: 12px; }
body.touch #chat-input { display: none; }
body.touch #chat.open { left: 50%; bottom: 50%; width: min(420px, 90vw); transform: translate(-50%, 50%); pointer-events: auto; z-index: 15; }
body.touch #chat.open #chat-input { display: block; opacity: 1; font-size: 16px; }

body.touch #den-hint { bottom: auto; top: 100px; font-size: 12px; max-width: 60vw; text-align: center; }
body.touch #hud-left { left: calc(env(safe-area-inset-left) + 10px); top: calc(env(safe-area-inset-top) + 10px); width: 200px; gap: 4px; }
body.touch #hud-right { right: calc(env(safe-area-inset-right) + 10px); top: calc(env(safe-area-inset-top) + 10px); }
body.touch .bar { height: 15px; }
body.touch .bar span { font-size: 10px; line-height: 13px; }
body.touch .portrait { font-size: 15px; }
body.touch .lvl { width: 28px; height: 28px; font-size: 13px; }
body.touch #quest { padding: 7px; font-size: 12px; gap: 4px; }
body.touch #minimap { width: 120px; height: 120px; }
body.touch .money span { font-size: 12px; padding: 3px 8px; }
body.touch #toasts { top: 70px; font-size: 13px; }

/* A phone in landscape is low: keep the top panels short. */
@media (max-height: 500px) {
  body.touch #quest-reward { display: none; }
  body.touch .bar.xp { display: none; }
  body.touch #minimap { width: 96px; height: 96px; }
  body.touch #loc-name { display: none; }
  body.touch #actions { transform: scale(.85); transform-origin: right bottom; }
  body.touch #joy-zone { height: 70vh; }
  .menu-card { height: calc(100vh - 12px); }
}
/* A narrow phone in portrait. */
@media (max-width: 500px) {
  body.touch #hud-left { width: 170px; }
  body.touch #chat { left: 12px; transform: none; bottom: 270px; width: 60vw; }
  body.touch #den-hint { top: auto; bottom: 370px; max-width: 80vw; }
  body.touch #actions { transform: scale(.9); transform-origin: right bottom; }
  body.touch #joy-zone { width: 55vw; height: 40vh; }
  .menu-card { width: 100vw; height: 100vh; border-radius: 0; }
  .tabs button { padding: 8px 10px; font-size: 14px; }
}
/* The quest card stays above the joystick zone. */
body.touch #hud-left { z-index: 2; }
body.touch #joy-zone { z-index: 1; }
body.touch #actions, body.touch #chat { z-index: 3; }
