:root {
  --font: 'Pixelify Sans', ui-rounded, system-ui, -apple-system, sans-serif;
  --ink: #e8f4ff;
  --dim: #9fb6d0;
  --glow: #7fd6ff;
  --panel: rgba(10, 20, 44, 0.9);
  --gold: #ffd75e;
  --hp: #e5484d;
  --mp: #3e8ef7;
  --xp: #f5c542;
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #0b0d1a; color: var(--ink);
  font-family: var(--font); font-size: 15px;
  touch-action: none; -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
canvas#game { position: fixed; inset: 0; display: block; image-rendering: pixelated; }
#ui { position: fixed; inset: 0; pointer-events: none; }
#ui button, #ui input, #window, .screen, #chatbar { pointer-events: auto; }
[hidden] { display: none !important; }

button {
  font: inherit; color: var(--ink); background: rgba(16, 32, 64, 0.82); border: 2px solid rgba(127, 214, 255, 0.55);
  border-radius: 8px; padding: 6px 10px; cursor: pointer; touch-action: manipulation;
}
button:active { transform: translateY(1px); filter: brightness(1.3); }
button:disabled { opacity: 0.45; }
button.big {
  font-size: 22px; padding: 12px 34px; border-radius: 12px; background: linear-gradient(#2b5db8, #1b3a7a);
  border-color: var(--glow); box-shadow: 0 0 18px rgba(127, 214, 255, 0.45); letter-spacing: 1px;
}

/* ---------------------------------------------------------------- HUD */
#left {
  position: absolute; left: calc(8px + var(--sl)); top: calc(8px + var(--st));
  display: flex; flex-direction: column; gap: 6px; width: min(220px, 46vw);
}
#card {
  background: rgba(8, 14, 32, 0.66); border: 1px solid rgba(127, 214, 255, 0.35);
  border-radius: 10px; padding: 6px 8px 7px;
}
#who { display: flex; justify-content: space-between; gap: 6px; font-weight: 600; font-size: 14px; margin-bottom: 4px; white-space: nowrap; }
#hudName { overflow: hidden; text-overflow: ellipsis; }
#hudLv { color: var(--gold); }
.bar { position: relative; height: 13px; background: rgba(0, 0, 0, 0.55); border-radius: 4px; margin-top: 3px; overflow: hidden; }
.bar i { position: absolute; inset: 0; width: 0%; transition: width 0.15s; }
.bar.hp i { background: linear-gradient(#ff6b6f, var(--hp)); }
.bar.mp i { background: linear-gradient(#69a9ff, var(--mp)); }
.bar.xp { height: 7px; }
.bar.xp i { background: linear-gradient(#ffe08a, var(--xp)); }
.bar span { position: absolute; inset: 0; font-size: 11px; line-height: 13px; text-align: center; text-shadow: 0 1px 1px #000; }
.bar.xp span { display: none; }
#gold { font-size: 13px; color: var(--gold); margin-top: 4px; }
#menu { display: flex; gap: 4px; flex-wrap: wrap; }
#menu button { font-size: 12px; padding: 4px 7px; border-radius: 6px; border-width: 1px; }
#quest {
  font-size: 12px; background: rgba(8, 14, 32, 0.6); border-left: 3px solid var(--gold);
  padding: 4px 8px; border-radius: 4px; color: #fff3c4;
}
#quest.done { border-left-color: #5fd35f; color: #c8ffc8; }

#mapbox { position: absolute; right: calc(8px + var(--sr)); top: calc(8px + var(--st)); text-align: right; }
#minimap {
  width: min(118px, 27vw); height: min(118px, 27vw); image-rendering: pixelated; border-radius: 50%;
  border: 2px solid rgba(127, 214, 255, 0.6); box-shadow: 0 0 10px rgba(0, 0, 0, 0.6); background: #000;
  pointer-events: auto; cursor: pointer;
}
#where { font-size: 12px; text-shadow: 0 1px 2px #000; max-width: 160px; margin-left: auto; }
#online { font-size: 11px; color: var(--dim); text-shadow: 0 1px 2px #000; }

#log {
  position: absolute; left: calc(8px + var(--sl)); bottom: calc(160px + var(--sb)); width: min(330px, 60vw);
  display: flex; flex-direction: column; gap: 2px; font-size: 12.5px;
}
#log div {
  background: rgba(0, 0, 0, 0.42); padding: 2px 6px; border-radius: 4px; text-shadow: 0 1px 1px #000;
  animation: fade 12s forwards; overflow-wrap: anywhere; width: fit-content; max-width: 100%;
}
#log .sys { color: #ffe9a0; }
#log .loot { color: #b5f5b5; }
#log b { color: var(--glow); font-weight: 600; }
@keyframes fade { 0%, 80% { opacity: 1; } 100% { opacity: 0; } }

#toast {
  position: absolute; left: 50%; top: calc(64px + var(--st)); transform: translateX(-50%);
  max-width: 70vw; text-align: center; font-size: 14px;
}
#toast div {
  background: rgba(10, 20, 44, 0.88); border: 1px solid rgba(127, 214, 255, 0.5); border-radius: 8px;
  padding: 5px 12px; margin-bottom: 4px; animation: fade 4s forwards;
}
#banner {
  position: absolute; left: 50%; top: 60%; transform: translateX(-50%); text-align: center;
  opacity: 0; transition: opacity 0.6s; text-shadow: 0 2px 4px #000, 0 0 12px rgba(0, 0, 0, 0.6); white-space: nowrap;
}
#banner.on { opacity: 1; }
#banner b { display: block; font-size: 28px; letter-spacing: 1px; }
#banner small { font-size: 14px; color: var(--gold); }

#actions { position: absolute; right: calc(14px + var(--sr)); bottom: calc(14px + var(--sb)); width: 190px; height: 190px; }
.act { position: absolute; border-radius: 50%; display: grid; place-items: center; padding: 0; line-height: 1; }
.act.atk { right: 0; bottom: 0; width: 84px; height: 84px; font-size: 18px; font-weight: 700; background: rgba(160, 40, 50, 0.75); border-color: #ffb3b3; }
.act.skill { right: 92px; bottom: 6px; width: 64px; height: 64px; font-size: 11px; overflow: hidden; background: rgba(60, 40, 140, 0.75); border-color: #c9b3ff; }
.act.skill .lbl { position: relative; z-index: 1; padding: 0 4px; text-align: center; }
.act.skill .cd { position: absolute; left: 0; right: 0; bottom: 0; height: 0%; background: rgba(0, 0, 0, 0.6); }
.act.skill.nomp { border-color: #666; color: #999; }
.act.pot { width: 46px; height: 46px; font-size: 11px; }
.act.pot b { display: block; font-size: 12px; color: var(--gold); }
.act.pot span { display: block; }
.act.hpp { right: 6px; bottom: 96px; background: rgba(150, 30, 40, 0.6); }
.act.mpp { right: 60px; bottom: 82px; background: rgba(30, 60, 150, 0.6); }
.act.talk { right: 112px; bottom: 82px; width: 66px; height: 40px; border-radius: 10px; font-size: 14px; background: rgba(200, 150, 30, 0.8); border-color: #ffe08a; }

#buffs { display: flex; flex-wrap: wrap; gap: 4px; }
#buffs span {
  font-size: 11px; background: rgba(8, 14, 32, 0.7); border: 1px solid rgba(125, 255, 138, 0.5);
  border-radius: 10px; padding: 1px 7px; color: #c8ffc8;
}
.act.tame {
  right: 112px; bottom: 128px; width: 66px; height: 44px; border-radius: 10px; font-size: 14px;
  background: rgba(40, 140, 70, 0.85); border-color: #9dffb0; display: flex; flex-direction: column; justify-content: center;
}
.act.tame small { font-size: 10px; color: #e8ffe8; }

#dialog {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(14px + var(--sb));
  width: min(640px, calc(100% - 230px - var(--sl) - var(--sr))); min-width: 260px;
  background: #fbfbf4; color: #232332; border: 4px solid #4a6aa8; border-radius: 10px;
  box-shadow: inset 0 0 0 2px #fff, 0 4px 14px rgba(0, 0, 0, 0.5); padding: 8px 14px 10px; pointer-events: auto;
}
#dialog b { display: block; color: #3d5fa0; font-size: 14px; }
#dialog p { margin: 3px 0 0; font-size: 16px; line-height: 1.35; min-height: 2.6em; }
#dialog i { position: absolute; right: 12px; bottom: 6px; color: #c43a3a; font-style: normal; animation: blink 1s infinite; }
#prompt {
  position: absolute; left: 50%; top: calc(110px + var(--st)); transform: translateX(-50%); pointer-events: auto;
  background: var(--panel); border: 2px solid var(--glow); border-radius: 10px; padding: 8px 12px; text-align: center;
  box-shadow: 0 0 16px rgba(127, 214, 255, 0.4); max-width: min(420px, 90vw);
}
#prompt p { margin: 0 0 6px; }
#prompt div { display: flex; gap: 8px; justify-content: center; }
.icon { width: 30px; height: 30px; image-rendering: pixelated; flex-shrink: 0; }
.row .lead { display: flex; align-items: center; gap: 10px; }
.pet-sprite { width: 40px; height: 34px; image-rendering: pixelated; object-fit: contain; }

#joy {
  position: absolute; width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.28); background: rgba(255, 255, 255, 0.07);
}
#knob {
  position: absolute; left: 50%; top: 50%; width: 52px; height: 52px; margin: -26px 0 0 -26px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.35); border: 2px solid rgba(255, 255, 255, 0.55);
}

#chatbar {
  position: absolute; left: calc(8px + var(--sl)); right: calc(8px + var(--sr)); bottom: calc(8px + var(--sb));
  display: flex; gap: 6px; margin: 0;
}
#chatbar input {
  flex: 1; font: inherit; font-size: 16px; color: var(--ink); background: rgba(8, 14, 32, 0.92);
  border: 2px solid var(--glow); border-radius: 8px; padding: 8px 10px; outline: none;
}

/* ---------------------------------------------------------------- windows */
#window {
  position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); display: grid; place-items: center;
  padding: calc(10px + var(--st)) calc(10px + var(--sr)) calc(10px + var(--sb)) calc(10px + var(--sl));
}
.win {
  width: min(560px, 100%); max-height: 100%; display: flex; flex-direction: column;
  background: var(--panel); border: 2px solid var(--glow); border-radius: 12px;
  box-shadow: 0 0 22px rgba(127, 214, 255, 0.4), inset 0 0 30px rgba(127, 214, 255, 0.08);
}
.win header { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid rgba(127, 214, 255, 0.35); }
.win h2 { margin: 0; font-size: 19px; letter-spacing: 1px; color: var(--glow); }
#winClose { padding: 2px 10px; }
#winBody { overflow-y: auto; padding: 10px 14px 14px; -webkit-overflow-scrolling: touch; touch-action: pan-y; user-select: text; -webkit-user-select: text; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 4px 14px; }
.kv { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed rgba(127, 214, 255, 0.18); padding: 2px 0; }
.kv span:first-child { color: var(--dim); }
.sec { margin: 12px 0 4px; color: var(--gold); font-size: 14px; letter-spacing: 1px; text-transform: uppercase; }
.note { color: var(--dim); font-size: 13px; }
.say { background: rgba(255, 255, 255, 0.06); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; font-style: italic; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid rgba(127, 214, 255, 0.12); }
.row .btns { display: flex; gap: 6px; flex-shrink: 0; }
.row small { display: block; color: var(--dim); }
.rank { display: inline-block; min-width: 26px; text-align: center; border-radius: 6px; padding: 0 6px; font-weight: 700; background: #2a3a5a; color: var(--gold); }
ol.top { margin: 4px 0 0; padding-left: 22px; }
ol.top li { padding: 1px 0; }
#worldmap { width: min(100%, 440px, calc(100dvh - 150px)); max-width: 440px; aspect-ratio: 1; display: block; margin: 0 auto; image-rendering: pixelated; border-radius: 8px; border: 1px solid rgba(127, 214, 255, 0.4); }

/* ---------------------------------------------------------------- screens */
.screen {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: calc(16px + var(--st)) calc(16px + var(--sr)) calc(16px + var(--sb)) calc(16px + var(--sl));
}
#title { background: radial-gradient(ellipse at center, rgba(11, 13, 26, 0.25), rgba(11, 13, 26, 0.85)); }
#title h1 {
  margin: 0; font-size: clamp(44px, 11vw, 92px); line-height: 0.95; letter-spacing: 4px; color: #fff;
  text-shadow: 0 0 24px rgba(127, 214, 255, 0.8), 0 4px 0 #1b3a7a;
}
#title h1 span { display: block; font-size: 0.45em; letter-spacing: 0.5em; color: var(--glow); margin-top: 6px; padding-left: 0.5em; }
#title .sub { color: var(--gold); font-size: 18px; margin: 10px 0 26px; text-shadow: 0 1px 3px #000; }
#title .hint { color: var(--dim); font-size: 12px; margin-top: 22px; max-width: 320px; text-shadow: 0 1px 2px #000; }

#story { background: rgba(4, 4, 10, 0.94); cursor: pointer; }
#story.light { background: radial-gradient(ellipse at center, rgba(255, 236, 170, 0.35), rgba(8, 6, 20, 0.96) 70%); }
#storyText { font-size: clamp(17px, 4.4vw, 24px); max-width: 640px; line-height: 1.45; min-height: 3em; text-shadow: 0 2px 4px #000; }
#storyText .speaker { display: block; color: var(--gold); font-size: 0.8em; margin-bottom: 4px; letter-spacing: 1px; }
#storyCtl { margin-top: 22px; display: flex; flex-direction: column; align-items: center; gap: 12px; width: 100%; max-width: 640px; }
#storyCtl .tap { color: var(--dim); font-size: 13px; animation: blink 1.4s infinite; }
@keyframes blink { 50% { opacity: 0.3; } }
#storyCtl input {
  font: inherit; font-size: 20px; text-align: center; color: #fff; background: rgba(255, 255, 255, 0.08);
  border: 2px solid var(--glow); border-radius: 10px; padding: 10px; width: min(320px, 90%); outline: none;
  user-select: text; -webkit-user-select: text;
}
.classes { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; width: 100%; }
.cls {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 6px; border-radius: 12px; text-align: center;
}
.cls canvas { width: 54px; height: 72px; image-rendering: pixelated; }
.cls strong { font-size: 17px; color: var(--glow); }
.cls small { color: var(--dim); font-size: 12px; line-height: 1.25; }
.err { color: #ff8a8a; font-size: 14px; min-height: 1em; }
.gift { font-size: 26px; color: var(--gold); text-shadow: 0 0 16px rgba(255, 215, 94, 0.7); }

#dead { background: radial-gradient(ellipse at center, rgba(90, 0, 10, 0.55), rgba(10, 0, 4, 0.9)); }
#dead h2 { font-size: 46px; margin: 0 0 8px; color: #ff5a5f; letter-spacing: 6px; text-shadow: 0 0 20px rgba(255, 0, 0, 0.6); }
#dead p { color: #f2d0d0; max-width: 420px; margin: 0 0 20px; }

#conn {
  position: absolute; left: 50%; top: 40%; transform: translateX(-50%); background: rgba(0, 0, 0, 0.75);
  padding: 10px 18px; border-radius: 10px; border: 1px solid var(--glow);
}

@media (max-height: 430px) {
  #actions { transform: scale(0.9); transform-origin: bottom right; }
  #log { bottom: calc(120px + var(--sb)); }
  #banner { top: 62%; }
  #banner b { font-size: 22px; }
  .cls { padding: 6px; }
  .cls canvas { width: 36px; height: 48px; }
  .cls small { font-size: 11px; }
  #storyCtl { margin-top: 12px; gap: 8px; }
}
@media (max-width: 600px) {
  #dialog { width: calc(100% - 20px); bottom: calc(210px + var(--sb)); }
}
@media (max-width: 430px) {
  .classes { grid-template-columns: 1fr; }
  .cls { flex-direction: row; text-align: left; gap: 10px; }
  .cls canvas { width: 36px; height: 48px; }
}
