:root {
  --bg: #0e1a12;
  --panel: rgba(14, 26, 18, 0.78);
  --panel2: rgba(20, 34, 24, 0.92);
  --accent: #7c4dff;
  --accent2: #3d1dff;
  --gold: #ffd23b;
  --text: #eaf5ec;
  --muted: #9ab0a0;
  --hp: #e04a4a;
  --xp: #7c4dff;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
#game-canvas { position: fixed; inset: 0; display: block; }
.hidden { display: none !important; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#hud-tl {
  position: absolute; top: 10px; left: 10px; pointer-events: auto;
  background: var(--panel); border: 1px solid rgba(255,255,255,0.08);
  border-radius: 12px; padding: 10px 14px; backdrop-filter: blur(6px); min-width: 190px;
}
#hud-tl .row1 { display: flex; justify-content: space-between; align-items: center; }
#hud-name { font-weight: 700; font-size: 15px; }
#hud-level { font-size: 12px; color: var(--gold); font-weight: 700; }
.bar {
  position: relative; height: 12px; border-radius: 7px; background: rgba(0,0,0,0.4);
  overflow: hidden; margin-top: 7px; border: 1px solid rgba(255,255,255,0.08);
}
.bar-fill { position: absolute; left: 0; top: 0; height: 100%; transition: width 0.2s; }
.bar-fill.hp { background: linear-gradient(90deg, #ff6a6a, #e04a4a); }
.bar.xp { height: 6px; margin-top: 5px; }
.bar-fill.xp { background: linear-gradient(90deg, #a78bff, var(--xp)); }
#hp-text { position: absolute; right: 5px; top: -1px; font-size: 9px; color: #fff; text-shadow: 0 1px 2px #000; }
#hud-gold { font-size: 13px; margin-top: 6px; color: var(--gold); font-weight: 700; }
#hud-inventory { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; max-width: 190px; }
#hud-inventory .inv {
  display: flex; align-items: center; gap: 3px; font-size: 11px;
  background: rgba(255,255,255,0.06); border-radius: 6px; padding: 2px 6px;
}
#hud-inventory img { width: 18px; height: 18px; }

#hud-tr {
  position: absolute; top: 10px; right: 10px; pointer-events: auto;
  display: flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid rgba(255,255,255,0.08);
  border-radius: 12px; padding: 8px 10px; backdrop-filter: blur(6px); flex-wrap: wrap; max-width: 60vw;
}
#hud-buffs { display: flex; gap: 4px; }
.buff {
  position: relative; width: 26px; height: 26px; border-radius: 7px;
  background: rgba(255,255,255,0.08); display: flex; align-items: center; justify-content: center;
  border: 1px solid rgba(255,255,255,0.14);
}
.buff img { width: 20px; height: 20px; }
.buff .t { position: absolute; bottom: -6px; left: 0; right: 0; text-align: center; font-size: 8px; color: #fff; text-shadow: 0 1px 2px #000; }
#hud-online { font-size: 13px; color: var(--muted); }
#hud-online::before { content: "● "; color: #5dd27a; }
.icon-btn {
  background: rgba(255,255,255,0.08); border: none; color: var(--text);
  border-radius: 8px; width: 32px; height: 32px; font-size: 16px; cursor: pointer;
}
.icon-btn.badge::after { content: "!"; position: absolute; margin-left: -4px; color: var(--gold); font-weight: 800; font-size: 12px; }

#quest-tracker {
  position: absolute; top: 64px; right: 10px; pointer-events: auto;
  display: flex; flex-direction: column; gap: 6px; max-width: 240px;
}
.qt {
  background: var(--panel); border: 1px solid rgba(255,255,255,0.08);
  border-radius: 10px; padding: 8px 10px; font-size: 12px; backdrop-filter: blur(6px);
}
.qt .qt-name { font-weight: 700; color: var(--gold); }
.qt .qt-desc { color: var(--muted); font-size: 11px; }
.qt .qt-prog { color: var(--text); margin-top: 3px; }
.qt.done { opacity: 0.7; }
.qt.done .qt-name { color: #5dd27a; }

#chat-wrap { position: absolute; left: 10px; bottom: 10px; width: min(320px, 58vw); pointer-events: auto; }
#chat-feed {
  max-height: 160px; overflow-y: auto; display: flex; flex-direction: column;
  gap: 3px; margin-bottom: 6px; font-size: 13px;
  mask-image: linear-gradient(transparent, #000 20%);
}
#chat-feed .msg { background: var(--panel); border-radius: 8px; padding: 4px 8px; word-wrap: break-word; }
#chat-feed .msg .who { font-weight: 700; color: var(--gold); }
#chat-feed .msg.announce { color: #ffd0e0; font-style: italic; }
#chat-feed .msg.sys { color: #9fd0ff; }
#chat-input {
  width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.14);
  background: var(--panel); color: var(--text); font-size: 14px; backdrop-filter: blur(6px);
}

#toast {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  background: var(--panel); border-radius: 12px; padding: 10px 18px; font-size: 15px;
  opacity: 0; transition: opacity 0.25s; pointer-events: none; text-align: center; z-index: 30;
}
#toast.show { opacity: 1; }
#toast .big { font-size: 20px; font-weight: 800; color: var(--gold); }

/* ---------- Joystick ---------- */
#joystick {
  position: fixed; left: 24px; bottom: 24px; width: 108px; height: 108px;
  border-radius: 50%; background: rgba(255,255,255,0.07); border: 2px solid rgba(255,255,255,0.14);
  display: none; align-items: center; justify-content: center; z-index: 20;
}
#joystick-knob { width: 46px; height: 46px; border-radius: 50%; background: rgba(255,255,255,0.3); transition: transform 0.02s; }

/* ---------- Panels ---------- */
.panel {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,0.5); padding: 20px;
}
.panel-card {
  background: var(--panel2); border: 1px solid rgba(255,255,255,0.12);
  border-radius: 18px; padding: 20px; width: min(440px, 94vw); max-height: 84vh;
  overflow-y: auto; backdrop-filter: blur(10px);
}
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.panel-head h2 { margin: 0; font-size: 20px; }
.panel-close { background: rgba(255,255,255,0.1); border: none; color: var(--text); border-radius: 8px; width: 30px; height: 30px; cursor: pointer; font-size: 15px; }
.panel-stats { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.panel-stats b { color: var(--text); }
#equip-slots { display: inline-flex; gap: 4px; }
#equip-slots img { width: 22px; height: 22px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; }
.grid .cell {
  position: relative; background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 10px; padding: 8px 4px; text-align: center; cursor: pointer;
}
.grid .cell:hover { border-color: var(--accent); }
.grid .cell img { width: 36px; height: 36px; }
.grid .cell .n { font-size: 10px; color: var(--muted); margin-top: 3px; line-height: 1.15; }
.grid .cell .c { position: absolute; top: 3px; right: 5px; font-size: 11px; color: var(--gold); font-weight: 700; }
.grid .cell.equipped { border-color: var(--gold); }
.grid .cell.equipped::before { content: "✔"; position: absolute; top: 2px; left: 5px; color: var(--gold); font-size: 11px; }

.skill-points { font-size: 14px; color: var(--gold); margin-bottom: 12px; font-weight: 700; }
.skill-list { display: flex; flex-direction: column; gap: 8px; }
.skill-row {
  display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1); border-radius: 10px; padding: 10px 12px;
}
.skill-row .sname { flex: 1; }
.skill-row .sname b { font-size: 14px; }
.skill-row .sname .sdesc { font-size: 11px; color: var(--muted); }
.skill-row .slvl { font-size: 13px; color: var(--muted); min-width: 40px; text-align: center; }
.skill-row button {
  background: linear-gradient(90deg, var(--accent), var(--accent2)); color: #fff; border: none;
  border-radius: 8px; padding: 6px 12px; font-size: 13px; cursor: pointer; font-weight: 700;
}
.skill-row button:disabled { opacity: 0.4; cursor: default; }

.quest-list { display: flex; flex-direction: column; gap: 8px; }
.quest-item {
  background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 10px; padding: 10px 12px;
}
.quest-item .qname { font-weight: 700; }
.quest-item.done .qname { color: #5dd27a; }
.quest-item .qdesc { font-size: 12px; color: var(--muted); margin: 3px 0; }
.quest-item .qprog { font-size: 12px; }
.quest-item .qreward { font-size: 11px; color: var(--gold); margin-top: 3px; }

/* Shop */
.shop-gold { font-size: 16px; font-weight: 700; color: var(--gold); margin-bottom: 8px; }
.shop-grid { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.shop-row {
  display: flex; align-items: center; gap: 10px; background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1); border-radius: 10px; padding: 8px 10px;
}
.shop-row img { width: 32px; height: 32px; }
.shop-row .sname { flex: 1; font-size: 13px; }
.shop-row .sname .smeta { color: var(--muted); font-size: 11px; }
.shop-row .sprice { color: var(--gold); font-weight: 700; font-size: 13px; min-width: 44px; text-align: right; }
.shop-row button { background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 6px 12px; font-size: 12px; cursor: pointer; font-weight: 700; }
.shop-row button.sell { background: #c07a2a; }
.shop-row button:disabled { opacity: 0.4; cursor: default; }

/* Map panel */
.map-card #map-canvas { width: 100%; height: auto; border-radius: 10px; border: 1px solid rgba(255,255,255,0.12); image-rendering: pixelated; }


/* ---------- Screens ---------- */
.screen {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at 50% 30%, #1b3523, #0e1a12 70%);
  text-align: center; padding: 20px;
}
.card {
  background: var(--panel); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 20px; padding: 28px; width: min(360px, 92vw); backdrop-filter: blur(10px);
}
.splash-icon { width: 84px; height: 84px; animation: float 2.4s ease-in-out infinite; filter: drop-shadow(0 6px 16px rgba(124,77,255,0.5)); }
.login-icon { width: 56px; height: 56px; filter: drop-shadow(0 4px 12px rgba(124,77,255,0.5)); }
@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
h1 { margin: 14px 0 0; font-size: 30px; letter-spacing: 2px;
  background: linear-gradient(90deg, #a78bff, #ffd23b); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
h2 { margin: 0 0 10px; }
.sub { color: var(--muted); margin: 4px 0 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spinner {
  width: 34px; height: 34px; margin: 24px auto 0; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.15); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#login-form { display: flex; flex-direction: column; gap: 10px; }
#login-form input {
  padding: 12px 14px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.16);
  background: rgba(0,0,0,0.25); color: var(--text); font-size: 15px; width: 100%;
}
button {
  padding: 12px 16px; border-radius: 12px; border: none; font-size: 15px; font-weight: 600; cursor: pointer;
  transition: transform 0.06s, filter 0.15s;
}
button:active { transform: scale(0.97); }
button.primary { background: linear-gradient(90deg, var(--accent), var(--accent2)); color: #fff; }
button.ghost { background: rgba(255,255,255,0.08); color: var(--text); }
button.danger { color: #ff8a8a; }
.error { color: #ff8a8a; margin: 10px 0 0; font-size: 13px; }
.row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; padding: 8px 0; border: none; }
.row input { width: auto; transform: scale(1.2); }

@media (max-width: 640px) {
  #hud-tl { padding: 8px 10px; min-width: 150px; }
  #chat-wrap { width: min(240px, 52vw); }
  #chat-feed { max-height: 110px; }
  #quest-tracker { max-width: 180px; }
}

/* Touch (mobile) layout: joystick is bottom-left, chat moves to bottom-right */
.touch #chat-wrap { left: auto; right: 10px; }
.touch #hud-tl { max-width: 48vw; }

/* Native app: hide the quest tracker (quests are available via the menu) */
.native-app #quest-tracker { display: none !important; }

/* Landscape (mobile app is landscape-only): compact, no overlaps */
@media (orientation: landscape) and (max-height: 600px) {
  #hud-tl { padding: 6px 9px; min-width: 150px; }
  #hud-tl .bar { height: 9px; margin-top: 5px; }
  #hud-inventory { max-width: 150px; margin-top: 4px; }
  #hud-inventory .inv img { width: 15px; height: 15px; }
  #quest-tracker { top: 52px; max-width: 200px; }
  #chat-wrap { width: min(240px, 40vw); }
  #chat-feed { max-height: 90px; }
  #joystick { width: 90px; height: 90px; left: 18px; bottom: 18px; }
}

/* PvP status chip */
#hud-pvp {
  position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
  font-size: 12px; color: var(--gold); background: var(--panel);
  border-radius: 10px; padding: 4px 10px; pointer-events: none;
  border: 1px solid rgba(255,255,255,0.1); white-space: nowrap; z-index: 6;
  max-width: 50vw; overflow: hidden; text-overflow: ellipsis;
}
#hud-pvp:empty { display: none; }

/* Hut + respawn panels */
.hut-info { font-size: 14px; margin: 6px 0; }
.hut-cost { font-size: 14px; color: var(--gold); margin: 10px 0; }
.hut-body button, #hut-body button { margin-top: 10px; }
.respawn-title { font-size: 18px; font-weight: 700; color: #ff8a8a; margin-bottom: 6px; }
#respawn-body button { display: block; width: 100%; margin-top: 10px; }

/* Navigation chip (distance + direction to nearest city / hut) */
#hud-nav {
  position: absolute; top: 34px; left: 50%; transform: translateX(-50%);
  font-size: 12px; color: #bfe8c8; background: var(--panel);
  border-radius: 10px; padding: 4px 10px; pointer-events: none;
  border: 1px solid rgba(255,255,255,0.1); white-space: nowrap; z-index: 6;
  max-width: 60vw; overflow: hidden; text-overflow: ellipsis;
}
#hud-nav:empty { display: none; }

/* Stylist panel */
#stylist-preview { display: block; margin: 0 auto 10px; background: rgba(0,0,0,0.25); border-radius: 12px; }
.stylist-row { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.stylist-row > span { width: 70px; font-size: 13px; color: var(--muted); }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch { width: 24px; height: 24px; border-radius: 6px; border: 2px solid rgba(255,255,255,0.2); cursor: pointer; padding: 0; }
.swatch.sel { border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold); }
#stylist-body button#stylist-save { margin-top: 12px; width: 100%; }
