:root {
  --bg: #120f22;
  --panel: #1d1936;
  --panel-2: #262046;
  --border: #3a3366;
  --fg: #f4f1ff;
  --muted: #b2abd6;
  --accent: #ff5fa2;
  --accent-2: #7c5cff;
  --good: #49d17d;
  --bad: #ff5f5f;
  --gold: #ffd75f;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: "Fredoka", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
[hidden] { display: none !important; }

#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ── HUD ─────────────────────────────────────────────────────────────── */

.hud {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}

.back {
  color: var(--muted);
  text-decoration: none;
  font-size: 1.3rem;
  line-height: 1;
}
.back:hover { color: var(--fg); }

.hud-slime {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}
.hud-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-sub { color: var(--muted); font-size: 0.85rem; }

.hud-stats {
  display: flex;
  gap: 14px;
  font-size: 0.9rem;
  white-space: nowrap;
}
.hud-stat { display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.hud-stat small { color: var(--muted); font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.05em; }

/* ── Field ───────────────────────────────────────────────────────────── */

.field {
  position: relative;
  flex: 1;
  overflow: hidden;
  background: #8fb9cf url("assets/bg/battle.webp") center / cover no-repeat;
  user-select: none;
}

.field-hint {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(18, 15, 34, 0.72);
  color: var(--fg);
  font-size: 0.9rem;
  text-align: center;
  pointer-events: none;
}
.field-hint:empty { display: none; }

.entity {
  position: absolute;
  left: 0;
  top: 0;
  will-change: transform;
}

.entity.clickable { cursor: pointer; }
.entity.clickable:hover .slime-body,
.entity.clickable:focus-visible .slime-body { filter: drop-shadow(0 0 10px #fff); }

.entity-tag {
  position: absolute;
  left: 50%;
  bottom: 100%;
  transform: translateX(-50%);
  padding: 1px 8px;
  border-radius: 999px;
  background: rgba(18, 15, 34, 0.8);
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
}

.entity.wild .entity-tag { color: #ffb3b3; }

.slime-body {
  display: block;
  width: 100%;
  height: 100%;
  animation: wobble 1.6s ease-in-out infinite;
  transform-origin: 50% 100%;
}

@keyframes wobble {
  0%, 100% { transform: scale(1, 1); }
  50% { transform: scale(1.06, 0.94); }
}

.sprite {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  image-rendering: auto;
}

.type-badge {
  position: absolute;
  right: -4px;
  top: -4px;
  font-size: 1rem;
}

.item-drop {
  width: 44px;
  height: 44px;
  border: 0;
  padding: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff, var(--gold) 45%, #c98a00);
  box-shadow: 0 0 18px var(--gold);
  animation: bob 1.4s ease-in-out infinite;
  font-size: 1.2rem;
}

@keyframes bob {
  0%, 100% { translate: 0 0; }
  50% { translate: 0 -6px; }
}

/* ── Dock ────────────────────────────────────────────────────────────── */

.dock {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 5;
}

.dock button {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 4px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
}
.dock button span { font-size: 1.3rem; }
.dock button:hover { color: var(--fg); background: var(--panel-2); }

.badge {
  position: absolute;
  top: 6px;
  left: calc(50% + 8px);
  min-width: 20px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 600;
}

/* ── Sheets (menus) ──────────────────────────────────────────────────── */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(8, 6, 18, 0.6);
  z-index: 20;
}

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 20px 20px 0 0;
  animation: rise 0.2s ease-out;
}

@media (min-width: 640px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 20px; border-bottom: 1px solid var(--border); }
}

@keyframes rise {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
}
.sheet-head h2 { margin: 0; font-size: 1.3rem; }

.sheet-body {
  overflow-y: auto;
  padding: 8px 16px 20px;
  padding-bottom: max(20px, env(safe-area-inset-bottom));
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: var(--panel-2);
}

.btn {
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 600;
}
.btn.secondary { background: var(--panel-2); border: 1px solid var(--border); }
.btn.small { padding: 6px 12px; font-size: 0.85rem; }
.btn.block { width: 100%; }

.muted { color: var(--muted); }
.good { color: var(--good); }
.bad { color: var(--bad); }

.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.spread { justify-content: space-between; }

.card {
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel-2);
}

.stat-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.stat-row:last-child { border-bottom: 0; }
.stat-name { font-weight: 500; }
.stat-value { font-variant-numeric: tabular-nums; }
.stat-pending { color: var(--good); margin-left: 4px; }
.stepper { display: flex; gap: 6px; }
.stepper button {
  min-width: 38px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--border);
  font-size: 0.82rem;
}

.item-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.item-row:last-child { border-bottom: 0; }
.item-row p { margin: 0; grid-column: 1; color: var(--muted); font-size: 0.85rem; }
.item-row .btn { grid-row: 1 / span 2; grid-column: 2; }

.progress {
  height: 10px;
  border-radius: 999px;
  background: var(--panel);
  overflow: hidden;
}
.progress > div {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}

input[type="text"] {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--fg);
  font: inherit;
}

input[type="range"] { width: 100%; accent-color: var(--accent); }

.element-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}
.element-grid .card { padding: 10px; font-size: 0.85rem; }

/* ── Battle ──────────────────────────────────────────────────────────── */

.battle {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  grid-template-rows: auto 1fr auto auto auto;
  gap: 8px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--bg);
  overflow: hidden;
}

.battle-bg,
.battle-aura {
  position: absolute;
  inset: 0;
  background-position: center;
  background-size: cover;
  pointer-events: none;
}
.battle-bg { background-image: url("assets/bg/battle.webp"); }
.battle-aura { opacity: 0; transition: opacity 0.6s ease; }
.battle-aura.on { opacity: 0.55; }

.battle > :is(.battle-top, .arena, .combatant, .battle-log, .moves, .clash) { position: relative; }

.combatant {
  max-width: 420px;
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: rgba(18, 15, 34, 0.82);
  border: 1px solid var(--border);
}
.combatant.player { justify-self: end; }

.combatant .row { gap: 8px; }
.combatant-name { font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hp-bar {
  height: 12px;
  margin-top: 6px;
  border-radius: 999px;
  background: #3b2a3f;
  overflow: hidden;
}
.hp-bar > div {
  height: 100%;
  background: var(--good);
  transition: width 0.4s ease, background 0.4s ease;
}
.hp-bar > div.mid { background: var(--gold); }
.hp-bar > div.low { background: var(--bad); }
.hp-text { font-size: 0.8rem; color: var(--muted); margin-top: 4px; display: flex; justify-content: space-between; gap: 8px; }
.effects { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.effects:empty { display: none; }
.effects span { font-size: 0.7rem; padding: 1px 7px; border-radius: 999px; background: var(--panel-2); }

.arena {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 0;
}

.fighter {
  width: min(32vw, 170px);
  aspect-ratio: 1;
  justify-self: center;
  transition: transform 0.18s ease;
}
.fighter.enemy { align-self: start; }
.fighter.player { align-self: end; }
.fighter.lunge-right { transform: translateX(30px) scale(1.08); }
.fighter.lunge-left { transform: translateX(-30px) scale(1.08); }
.fighter.hit { animation: hit 0.35s ease; }
.fighter.dodge { animation: dodge 0.4s ease; }
.fighter.fainted { opacity: 0.25; transform: translateY(20px) scaleY(0.6); transition: all 0.6s ease; }

@keyframes hit {
  0%, 100% { translate: 0 0; filter: none; }
  25% { translate: -8px 0; filter: brightness(2); }
  75% { translate: 8px 0; }
}
@keyframes dodge {
  50% { translate: 0 -24px; }
}

.clash {
  font-size: 2rem;
  text-align: center;
  min-width: 90px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}
.clash small {
  display: block;
  margin: 4px 0;
  font-size: 0.85rem;
  font-weight: 600;
  color: #fff;
}

.float-dmg {
  position: absolute;
  font-weight: 700;
  font-size: 1.6rem;
  color: #fff;
  text-shadow: 0 2px 0 var(--bad), 0 0 12px rgba(0, 0, 0, 0.8);
  pointer-events: none;
  animation: floatUp 1s ease-out forwards;
}
@keyframes floatUp {
  from { transform: translateY(0); opacity: 1; }
  to { transform: translateY(-50px); opacity: 0; }
}

.battle-log {
  height: 104px;
  overflow-y: auto;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: rgba(18, 15, 34, 0.85);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  line-height: 1.4;
}
.battle-log p { margin: 0; }
.battle-log .round { color: var(--fg); font-weight: 500; }
.battle-log .fx { color: var(--muted); padding-left: 10px; }

.moves {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.moves button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(38, 32, 70, 0.92);
  font-size: 0.78rem;
}
.moves button span { font-size: 1.5rem; }
.moves button:hover:not(:disabled) { border-color: var(--accent); }
.moves .channel { border-color: #5b4bb0; }
.moves .flee { color: var(--muted); }

.battle-result {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(8, 6, 18, 0.7);
  z-index: 3;
}
.battle-result .card {
  width: 100%;
  max-width: 380px;
  text-align: center;
  background: var(--panel);
}
.battle-result h2 { margin: 0 0 8px; font-size: 1.8rem; }
.battle-result p { margin: 0; }

/* ── Toasts ──────────────────────────────────────────────────────────── */

.toasts {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: calc(100% - 32px);
  max-width: 420px;
  z-index: 30;
  pointer-events: none;
}
.toast {
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  font-size: 0.9rem;
  text-align: center;
  animation: rise 0.2s ease-out;
}

/* ── Egg / contract ──────────────────────────────────────────────────── */

.egg-progress {
  position: absolute;
  left: 50%;
  top: calc(100% + 6px);
  transform: translateX(-50%);
  width: 80px;
}

@media (max-width: 420px) {
  .hud-stats { gap: 10px; font-size: 0.82rem; }
  .moves button { font-size: 0.7rem; }
  .moves button span { font-size: 1.3rem; }
}

@media (prefers-reduced-motion: reduce) {
  .slime-body, .item-drop { animation: none; }
}

/* ── Nemesis ─────────────────────────────────────────────────────────── */

.entity.nemesis .slime-body { filter: drop-shadow(0 0 8px rgba(255, 40, 40, 0.9)); }
.entity.nemesis .entity-tag { color: #ff8a8a; font-weight: 600; }
.entity.nemesis.ascended .slime-body {
  animation: wobble 1.6s ease-in-out infinite, nemesisPulse 1.2s ease-in-out infinite;
}
@keyframes nemesisPulse {
  0%, 100% { filter: drop-shadow(0 0 10px rgba(255, 40, 40, 0.8)) sepia(0.3) saturate(1.4); }
  50% { filter: drop-shadow(0 0 26px rgba(255, 40, 40, 1)) sepia(0.5) hue-rotate(-20deg) saturate(1.8); }
}
.chip.bad { color: var(--bad); border-color: var(--bad); }

/* ── HUD help, tabs, shop, codex ─────────────────────────────────────── */

.help-btn { flex-shrink: 0; font-weight: 700; }

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 4px;
  border-radius: 12px;
  background: var(--panel-2);
}
.tab {
  padding: 8px 4px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 0.88rem;
}
.tab[aria-selected="true"] { background: var(--accent-2); color: #fff; }

.select {
  padding: 10px;
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--fg);
  border: 1px solid var(--border);
  font: inherit;
}

.codex-row { display: flex; flex-direction: column; gap: 2px; padding: 6px 0; border-top: 1px solid var(--border); }

.shop-controls { grid-row: 1 / span 2; grid-column: 2; align-items: center; }
.shop-controls .qty { min-width: 22px; text-align: center; font-variant-numeric: tabular-nums; }
.shop-controls button:last-child {
  padding: 0 10px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border: 0;
  color: #fff;
  font-weight: 600;
}

/* ── Field events ────────────────────────────────────────────────────── */

.portal {
  border: 0;
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.85), rgba(124, 92, 255, 0.55) 55%, rgba(124, 92, 255, 0) 72%);
  animation: portalPulse 2s ease-in-out infinite;
}
.portal.dungeon { background: radial-gradient(circle, rgba(20, 14, 40, 0.95), rgba(60, 40, 20, 0.7) 55%, rgba(60, 40, 20, 0) 72%); }
.portal-icon { font-size: 2rem; }
.portal .entity-tag { color: #d9ceff; font-weight: 600; }
@keyframes portalPulse {
  0%, 100% { box-shadow: 0 0 10px rgba(124, 92, 255, 0.6); }
  50% { box-shadow: 0 0 28px rgba(124, 92, 255, 1); }
}

.entity.boss .slime-body { filter: drop-shadow(0 0 16px rgba(255, 200, 60, 0.9)); }
.entity.boss .entity-tag { color: var(--gold); font-weight: 600; font-size: 0.8rem; }
.boss-hp { position: absolute; left: 10%; right: 10%; bottom: -14px; height: 8px; margin: 0; }

.entity.immortal .slime-body { filter: drop-shadow(0 0 10px rgba(255, 240, 180, 1)) saturate(0.6) brightness(1.2); }
.entity.immortal .entity-tag { color: #fff2c4; }

/* ── Gauntlet ────────────────────────────────────────────────────────── */

.gauntlet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: var(--bg);
  overflow-y: auto;
}
.gauntlet .battle-bg { position: fixed; inset: 0; opacity: 0.35; background-image: url("assets/bg/void.webp"); }
.gauntlet > *:not(.battle-bg) { position: relative; }

.g-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: rgba(18, 15, 34, 0.85);
  border: 1px solid var(--border);
}
.g-dots { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.g-dots span {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--accent-2);
  background: transparent;
}
.g-dots span.done { background: var(--accent-2); }

.g-arena {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  min-height: 280px;
}
.g-vs { font-weight: 700; font-size: 1.4rem; color: var(--accent); }
.g-side { display: flex; justify-content: center; text-align: center; }
.g-name { font-size: 0.95rem; }
.g-pips { display: flex; gap: 6px; }
.g-pips span { width: 16px; height: 16px; border-radius: 50%; background: var(--bad); }
.g-pips span.lost { background: #4a4560; }
.g-roll {
  min-width: 72px;
  padding: 6px 10px;
  border-radius: 12px;
  background: rgba(18, 15, 34, 0.85);
  border: 1px solid var(--border);
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.g-label { min-height: 1.4em; font-weight: 700; }

.g-msg {
  margin: 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: rgba(18, 15, 34, 0.85);
  border: 1px solid var(--border);
  text-align: center;
}
.g-actions { display: flex; justify-content: center; }
.g-actions:empty { display: none; }
.g-summary { width: 100%; max-width: 420px; background: var(--panel); text-align: center; }

@media (max-width: 420px) {
  .hud { gap: 8px; padding-left: 12px; padding-right: 12px; }
  .hud-stats { gap: 8px; }
  .dock button { font-size: 0.72rem; }
}

/* ── Fusion preview ──────────────────────────────────────────────────── */

.warn {
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--bad);
  background: rgba(255, 95, 95, 0.12);
  font-size: 0.92rem;
}
.fusion-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.fusion-table th, .fusion-table td { padding: 8px 6px; text-align: right; border-bottom: 1px solid var(--border); }
.fusion-table th[scope="row"], .fusion-table thead th:first-child { text-align: left; font-weight: 500; }
.fusion-table thead th { color: var(--muted); font-weight: 500; font-size: 0.82rem; }
.fusion-table td:last-child { white-space: nowrap; }
.fusion-table .total th, .fusion-table .total td { border-bottom: 0; font-weight: 600; }

/* ── Online ──────────────────────────────────────────────────────────── */

.online-btn { position: relative; flex-shrink: 0; }
.online-btn::after {
  content: "";
  position: absolute;
  right: 3px;
  top: 3px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: transparent;
}
.online-btn.connected::after { background: var(--good); box-shadow: 0 0 6px var(--good); }
.online-btn.connecting::after { background: var(--gold); }

.room-code-card { text-align: center; }
.room-code {
  margin: 4px 0 10px;
  font-size: 2.6rem;
  font-weight: 700;
  letter-spacing: 0.25em;
  padding-left: 0.25em;
  font-variant-numeric: tabular-nums;
}
.code-input {
  flex: 1;
  text-transform: uppercase;
  letter-spacing: 0.3em;
  font-weight: 600;
  text-align: center;
}
.player-row { padding: 6px 0; border-top: 1px solid var(--border); }
.player-row .chip { margin-left: 4px; font-size: 0.72rem; }

.chat-log {
  max-height: 160px;
  overflow-y: auto;
  font-size: 0.88rem;
}
.chat-log p { margin: 0 0 4px; }
#chat-input { flex: 1; }

.clash .timer { color: var(--gold); }

.party { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.party-chip {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: 0.75rem;
}
.party-chip.down { opacity: 0.45; text-decoration: line-through; }
.party-chip.active { border-color: var(--accent); color: #fff; background: rgba(255, 95, 162, 0.2); }
.diag-row { display: flex; flex-direction: column; gap: 2px; padding: 6px 0; border-top: 1px solid var(--border); }
.diag-row small { word-break: break-all; }
