:root {
  --gold: #c8a45c;
  --gold-bright: #e7c878;
  --red: #a02424;
  --red-bright: #d64545;
  --blood: #6e1414;
  --panel-bg: rgba(12, 10, 14, 0.96);
  --panel-border: #3a2f1e;
  --text: #d8cfc0;
  --dim: #8a8073;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: #050407;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--text);
  cursor: crosshair;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

#game { display: block; width: 100%; height: 100%; touch-action: none; }

.hidden { display: none !important; }

#ui { position: absolute; inset: 0; pointer-events: none; }
#ui > * { pointer-events: auto; }

/* ---------- tooltip ---------- */
#tooltip {
  position: absolute;
  z-index: 40;
  max-width: 300px;
  padding: 8px 10px;
  background: rgba(8, 6, 10, 0.95);
  border: 1px solid var(--panel-border);
  box-shadow: 0 4px 18px rgba(0,0,0,0.7);
  font-size: 12px;
  line-height: 1.5;
  pointer-events: none;
}
#tooltip .tt-name { font-weight: 700; font-size: 13px; margin-bottom: 3px; }
#tooltip .tt-type { font-size: 10px; letter-spacing: 1px; margin-bottom: 4px; }
#tooltip .tt-stat { color: #8fb4ff; }
#tooltip .tt-req { color: var(--dim); font-size: 11px; }
#tooltip .tt-flavor { color: #c9a86a; font-style: italic; margin-top: 3px; }
#tooltip .tt-sep { border: 0; border-top: 1px solid var(--panel-border); margin: 4px 0; }
#tooltip .tt-hint { color: #6f7f6f; font-size: 10px; margin-top: 3px; }

.rarity-common   { color: #cfcfcf !important; }
.rarity-magic    { color: #5f8cff !important; }
.rarity-rare     { color: #ffd23f !important; }
.rarity-legendary{ color: #ff7a2f !important; }

/* ---------- panels ---------- */
.panel {
  position: absolute;
  z-index: 30;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  box-shadow: 0 10px 40px rgba(0,0,0,0.8), inset 0 0 0 1px rgba(0,0,0,0.6);
  border-radius: 6px;
}
.panel-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--panel-border);
  background: linear-gradient(180deg, rgba(60,48,28,0.35), rgba(20,16,12,0.35));
}
.panel-title { font-size: 14px; letter-spacing: 3px; color: var(--gold-bright); }
.panel-close { cursor: pointer; color: var(--dim); font-size: 14px; padding: 0 4px; }
.panel-close:hover { color: #fff; }

#panel-inventory {
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: 720px; max-width: 96vw;
}
.panel-body { padding: 12px; display: flex; gap: 12px; }

.equip-col { width: 150px; flex: none; display: flex; flex-direction: column; gap: 6px; }
.equip-slot {
  height: 40px; display: flex; align-items: center; gap: 8px;
  border: 1px solid var(--panel-border);
  background: rgba(30,26,34,0.6);
  padding: 3px 6px; border-radius: 3px; font-size: 12px; color: var(--dim);
  cursor: pointer; position: relative;
}
.equip-slot .slot-label { width: 44px; color: var(--dim); }
.equip-slot .slot-item { flex: 1; text-align: right; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.equip-slot.filled { color: #fff; }
.equip-slot:hover { border-color: var(--gold); }

.inv-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(6, 44px);
  grid-auto-rows: 44px;
  gap: 4px;
  align-content: start;
}
.inv-cell {
  width: 44px; height: 44px;
  border: 1px solid #2a2319;
  background: rgba(34,28,40,0.55);
  border-radius: 3px;
  cursor: pointer;
  position: relative;
  display: flex; align-items: center; justify-content: center;
}
.inv-cell:hover { border-color: var(--gold-bright); }
.inv-cell .inv-icon { font-size: 24px; line-height: 1; text-shadow: 0 2px 4px rgba(0,0,0,0.8); }
.inv-cell.equipped { box-shadow: inset 0 0 0 2px var(--gold); }

.side-col { width: 210px; flex: none; display: flex; flex-direction: column; gap: 10px; }
#char-stats, #loot-stats {
  border: 1px solid var(--panel-border); border-radius: 3px;
  background: rgba(30,26,34,0.5); padding: 8px 10px; font-size: 12px;
}
#char-stats h4, #loot-stats h4 {
  font-size: 11px; letter-spacing: 2px; color: var(--gold); margin-bottom: 6px;
  font-weight: 600;
}
.stat-row { display: flex; justify-content: space-between; padding: 1px 0; }
.stat-row .sv { color: #9fd0ff; font-weight: 600; }

/* ---------- help ---------- */
#panel-help { left: 50%; top: 50%; transform: translate(-50%, -50%); width: 560px; max-width: 94vw; }
.help-body { flex-direction: column; font-size: 13px; line-height: 1.9; }
.help-body .key { display: inline-block; min-width: 22px; text-align: center; padding: 0 5px; border: 1px solid var(--panel-border); border-radius: 3px; background: rgba(60,50,40,0.4); color: var(--gold-bright); font-weight: 700; }
.help-body .hl { color: var(--gold-bright); }

/* ---------- overlay ---------- */
.overlay {
  position: absolute; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, rgba(20,10,20,0.82), rgba(0,0,0,0.96));
  pointer-events: auto;
}
.overlay-inner { text-align: center; max-width: 560px; padding: 40px; }
.overlay-inner h1 {
  font-size: 44px; letter-spacing: 8px; color: var(--gold-bright);
  text-shadow: 0 0 24px rgba(200,150,70,0.5), 0 4px 0 #2a1508;
  margin-bottom: 18px;
}
.overlay-inner p { color: var(--dim); font-size: 14px; line-height: 1.8; margin-bottom: 26px; white-space: pre-line; }
#overlay-btn {
  pointer-events: auto;
  font-size: 15px; letter-spacing: 3px; color: #1c1206;
  background: linear-gradient(180deg, var(--gold-bright), var(--gold));
  border: 1px solid #6e5324; border-radius: 3px;
  padding: 12px 34px; cursor: pointer; font-weight: 700;
}
#overlay-btn:hover { filter: brightness(1.1); }

/* ---------- level up banner ---------- */
#lvl-up-banner {
  position: absolute; left: 50%; top: 18%;
  transform: translateX(-50%);
  z-index: 25;
  font-size: 34px; font-weight: 800; letter-spacing: 6px;
  color: var(--gold-bright);
  text-shadow: 0 0 30px rgba(255,210,80,0.8), 0 3px 0 #2a1508;
  animation: lvlpop 1.6s ease-out forwards;
  pointer-events: none;
}
@keyframes lvlpop {
  0% { opacity: 0; transform: translateX(-50%) scale(0.6); }
  25% { opacity: 1; transform: translateX(-50%) scale(1.15); }
  60% { opacity: 1; transform: translateX(-50%) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) scale(1) translateY(-20px); }
}

/* ============================================================
 * Mobile touch controls
 * ============================================================ */
#mobile-controls {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 20;
}
#mobile-controls > * { pointer-events: auto; }

#joy {
  position: fixed;
  left: 22px;
  left: calc(22px + env(safe-area-inset-left, 0px));
  bottom: 28px;
  bottom: calc(28px + env(safe-area-inset-bottom, 0px));
  width: 112px; height: 112px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgba(255,255,255,0.08), rgba(0,0,0,0.35));
  border: 2px solid rgba(200,164,92,0.4);
  touch-action: none;
}
#joy-knob {
  position: absolute; left: 50%; top: 50%;
  width: 54px; height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #e8d8a8, #8a6a3a);
  border: 2px solid #2a1c0c;
  box-shadow: 0 2px 8px rgba(0,0,0,0.6);
  will-change: transform;
}

#mb-buttons {
  position: fixed;
  right: 12px;
  right: calc(12px + env(safe-area-inset-right, 0px));
  bottom: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: grid;
  grid-template-columns: repeat(3, 54px);
  grid-template-rows: repeat(3, 54px);
  gap: 8px;
  touch-action: none;
}

.mb-btn {
  position: relative;
  width: 54px; height: 54px;
  border-radius: 50%;
  border: 2px solid #3a2f1e;
  background: radial-gradient(circle at 35% 30%, rgba(60,50,40,0.75), rgba(12,10,16,0.9));
  color: #d8cfc0;
  font-size: 24px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none; user-select: none;
}
.mb-btn .mb-icon { position: relative; z-index: 1; pointer-events: none; }
.mb-btn .mb-cd {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 0%;
  background: rgba(0,0,0,0.72);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700; color: #fff;
  pointer-events: none;
}
.mb-btn.pressed { filter: brightness(1.5); transform: scale(0.94); }
.mb-btn.low-rage { filter: grayscale(0.7) brightness(0.6); }
.mb-btn.active { border-color: #ffd23f; box-shadow: 0 0 14px rgba(255,210,63,0.7); }

.mb-skill[data-skill="cleave"]     { grid-area: 1 / 1; }
.mb-skill[data-skill="whirlwind"]   { grid-area: 2 / 1; }
.mb-skill[data-skill="leap"]        { grid-area: 3 / 1; }
.mb-skill[data-skill="nova"]        { grid-area: 1 / 2; }
#mb-potion                          { grid-area: 1 / 3; }
#mb-attack {
  grid-area: 2 / 2 / 4 / 4;
  width: 100%; height: 100%;
  border-radius: 50%;
  border: 3px solid #c8a45c;
  background: radial-gradient(circle at 35% 30%, #7a2f22, #3a120c);
  font-size: 40px;
  color: #ffd9c0;
  box-shadow: 0 4px 16px rgba(0,0,0,0.7), 0 0 0 6px rgba(0,0,0,0.25);
}
#mb-attack .mb-icon { font-size: 42px; }
#mb-attack.pressed { filter: brightness(1.4); transform: scale(0.95); }

#mb-menu {
  position: fixed;
  left: 12px;
  left: calc(12px + env(safe-area-inset-left, 0px));
  top: 12px;
  top: calc(12px + env(safe-area-inset-top, 0px));
  display: flex; flex-direction: column; gap: 8px;
  align-items: flex-start;
  touch-action: none;
}
#mb-menu button {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(200,164,92,0.5);
  background: rgba(10,8,14,0.8);
  color: #d8cfc0;
  font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  touch-action: none;
}
#mb-popup { display: flex; flex-direction: column; gap: 8px; }
#mb-popup button { font-size: 18px; opacity: 0.92; }
#mb-menu button:active { filter: brightness(1.5); }

/* ============================================================
 * Desktop toolbar (backpack / help / mute)
 * ============================================================ */
#desktop-bar {
  position: fixed;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 20;
}
body.is-touch #desktop-bar { display: none; }
#desktop-bar button {
  position: relative;
  width: 46px; height: 46px;
  border-radius: 50%;
  border: 1px solid rgba(200,164,92,0.5);
  background: radial-gradient(circle at 35% 30%, rgba(50,42,32,0.9), rgba(10,8,14,0.95));
  color: #d8cfc0;
  font-size: 20px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#desktop-bar button:hover { border-color: var(--gold-bright); filter: brightness(1.25); }
#desktop-bar .db-ico { line-height: 1; }
#desktop-bar .db-key {
  position: absolute; right: -2px; bottom: -2px;
  min-width: 13px; line-height: 13px;
  font-size: 9px; font-weight: 800; font-style: normal;
  color: #1c1206; background: var(--gold-bright);
  border: 1px solid #6e5324;
  border-radius: 3px; padding: 0 2px;
  text-align: center;
}

/* ============================================================
 * Responsive panels
 * ============================================================ */
@media (max-width: 700px) {
  #panel-inventory {
    width: 100vw; height: 100vh; max-width: 100vw;
    top: 0; left: 0; transform: none;
    border-radius: 0; border: none;
  }
  #panel-inventory .panel-body {
    flex-direction: column;
    overflow-y: auto;
    padding: 10px;
    height: calc(100vh - 40px);
  }
  #panel-inventory .equip-col {
    flex-direction: row; flex-wrap: wrap; width: 100%;
  }
  #panel-inventory .equip-slot { flex: 1 1 30%; min-width: 96px; }
  #panel-inventory .inv-grid {
    grid-template-columns: repeat(6, 1fr);
    width: 100%;
  }
  #panel-inventory .inv-cell { width: auto; height: auto; aspect-ratio: 1; font-size: 22px; }
  #panel-inventory .side-col { width: 100%; flex-direction: row; flex-wrap: wrap; }
  #panel-inventory .side-col > div { flex: 1 1 45%; }

  #panel-help { width: 94vw; }
  .overlay-inner h1 { font-size: 30px; letter-spacing: 5px; }
  #tooltip { max-width: 220px; }
}
