:root {
  --bg-deep: #0c0f1a;
  --bg-panel: #151a2b;
  --bg-panel-light: #1d2438;
  --text-main: #eef1fb;
  --text-dim: #9aa3c0;
  --accent: #6c8cff;
  --border-soft: rgba(255,255,255,0.08);
  --hp-color: #43d17a;
  --hp-color-low: #ff5d5d;

  --c-common: #8a9296;
  --c-rare: #3fa9f5;
  --c-epic: #b06bfa;
  --c-legendary: #f5b942;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  background:
    radial-gradient(ellipse 900px 500px at 15% -10%, rgba(123, 63, 228, 0.28), transparent 60%),
    radial-gradient(ellipse 800px 500px at 110% 10%, rgba(63, 169, 245, 0.2), transparent 55%),
    radial-gradient(ellipse 700px 600px at 50% 110%, rgba(245, 185, 66, 0.12), transparent 60%),
    var(--bg-deep);
  background-attachment: fixed;
  color: var(--text-main);
  font-family: "Segoe UI", "Trebuchet MS", "Helvetica Neue", Arial, sans-serif;
}

.hidden { display: none !important; }

/* ===== Topbar ===== */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  background: rgba(10, 13, 24, 0.85);
  border-bottom: 1px solid var(--border-soft);
  position: sticky;
  top: 0;
  z-index: 20;
  backdrop-filter: blur(6px);
}
.brand {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  background: linear-gradient(90deg, #ffd76b, #ff9f6b);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.stats-bar { display: flex; gap: 10px; }
.stat-chip {
  background: var(--bg-panel-light);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}
.stat-chip.essence { color: #6ee7f5; }
.stat-chip.stage { color: #ffcf7a; }
.account-chip {
  color: var(--text-main);
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s ease;
}
.account-chip:hover { border-color: var(--accent); }

.account-card { width: 300px; }
.account-tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.account-tab {
  flex: 1;
  padding: 8px;
  border: 1px solid var(--border-soft);
  background: var(--bg-panel-light);
  color: var(--text-dim);
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
  font-size: 0.85rem;
  font-family: inherit;
}
.account-tab.active { color: var(--text-main); border-color: var(--accent); background: rgba(108,140,255,0.15); }

.account-note { color: var(--text-dim); font-size: 0.78rem; margin: 0 0 12px; text-align: center; }
.account-input {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 10px;
  border-radius: 10px;
  border: 1px solid var(--border-soft);
  background: rgba(0,0,0,0.25);
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.9rem;
}
.account-input:focus { outline: none; border-color: var(--accent); }
.account-error { color: #ff8a8a; font-size: 0.78rem; text-align: center; margin: -2px 0 10px; }
.account-submit { width: 100%; margin-top: 4px; }
.account-close { width: 100%; margin-top: 10px; background: transparent; }
.account-username { text-align: center; font-weight: 800; font-size: 1.1rem; margin-bottom: 18px; color: #ffe27a; }

#app { max-width: 1100px; margin: 0 auto; padding: 24px; }

.section-title {
  margin: 28px 0 14px;
  font-size: 1.05rem;
  color: var(--text-dim);
  font-weight: 600;
}

.empty-msg { color: var(--text-dim); text-align: center; padding: 30px; }

/* ===== Home actions ===== */
.home-actions { display: flex; gap: 16px; flex-wrap: wrap; }

.btn {
  cursor: pointer;
  border: none;
  border-radius: 14px;
  color: var(--text-main);
  font-family: inherit;
  transition: transform 0.15s ease, box-shadow 0.15s ease, filter 0.15s ease;
}
.btn:hover { transform: translateY(-2px); filter: brightness(1.08); }
.btn:active { transform: translateY(0); }
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  filter: none;
}

.btn-summon, .btn-start {
  flex: 1;
  min-width: 220px;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.35);
}
.btn-summon {
  background: linear-gradient(135deg, #7b3fe4, #b06bfa 60%, #f5b942);
}
.btn-start {
  background: linear-gradient(135deg, #1e6fd9, #3fa9f5 60%, #43d17a);
}
.btn-title { font-size: 1.05rem; font-weight: 700; }
.btn-sub { font-size: 0.8rem; opacity: 0.85; }

.btn-action {
  padding: 10px 18px;
  background: var(--bg-panel-light);
  border: 1px solid var(--border-soft);
  font-weight: 600;
}
.btn-action:not(:disabled):hover { border-color: var(--accent); }

/* ===== Character Card Grid ===== */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}

.char-card {
  position: relative;
  border-radius: 18px;
  padding: 30px 12px 12px;
  text-align: center;
  cursor: pointer;
  border: 2px solid var(--border-soft);
  background: linear-gradient(180deg, var(--bg-panel-light) 0%, var(--bg-panel) 45%, #0a0d17 100%);
  overflow: hidden;
  transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease;
  box-shadow: 0 8px 18px rgba(0,0,0,0.35), inset 0 1px 0 rgba(255,255,255,0.04);
}
.char-card::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: 0.28;
  z-index: 0;
  background: radial-gradient(ellipse 140% 60% at 50% 0%, var(--rarity-tint, var(--c-common)), transparent 70%);
}
/* diagonal shine sweep on hover */
.char-card::after {
  content: "";
  position: absolute;
  top: -60%;
  left: -60%;
  width: 40%;
  height: 220%;
  background: linear-gradient(115deg, transparent 0%, rgba(255,255,255,0.22) 50%, transparent 100%);
  transform: translateX(-120%) rotate(8deg);
  transition: transform 0.55s ease;
  z-index: 2;
  pointer-events: none;
}
.char-card:hover::after { transform: translateX(340%) rotate(8deg); }
.char-card > * { position: relative; z-index: 1; }

.char-card:hover { transform: translateY(-4px) scale(1.02); }

.char-card.rarity-common { border-color: var(--c-common); --rarity-tint: var(--c-common); }
.char-card.rarity-rare { border-color: var(--c-rare); --rarity-tint: var(--c-rare); }
.char-card.rarity-rare:hover { box-shadow: 0 0 18px rgba(63,169,245,0.45); }
.char-card.rarity-epic { border-color: var(--c-epic); --rarity-tint: var(--c-epic); }
.char-card.rarity-epic:hover { box-shadow: 0 0 22px rgba(176,107,250,0.55); }

.char-card.rarity-legendary {
  border-color: var(--c-legendary);
  --rarity-tint: var(--c-legendary);
  animation: legendary-pulse 2.4s ease-in-out infinite;
}
.char-card.rarity-legendary:hover { box-shadow: 0 0 28px rgba(245,185,66,0.65); }

@keyframes legendary-pulse {
  0%, 100% { box-shadow: 0 0 8px rgba(245,185,66,0.25); }
  50% { box-shadow: 0 0 18px rgba(245,185,66,0.55); }
}

.char-card.selected {
  border-color: #ffffff;
  box-shadow: 0 0 0 3px rgba(255,255,255,0.25), 0 0 20px rgba(255,255,255,0.3);
  transform: translateY(-4px) scale(1.03);
}
.char-card.selected .selected-badge { display: flex; }

.selected-badge {
  display: none;
  position: absolute;
  top: 8px;
  left: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #43d17a;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 800;
  z-index: 3;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.rarity-ribbon {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 3;
  font-size: 0.6rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  padding: 4px 10px 4px 14px;
  border-bottom-left-radius: 10px;
  color: #1a1408;
  background: linear-gradient(135deg, var(--rarity-tint, var(--c-common)), #fff8);
  box-shadow: 0 2px 6px rgba(0,0,0,0.35);
}

.portrait-frame {
  width: 84px;
  height: 84px;
  margin: 0 auto 10px;
  border-radius: 50%;
  padding: 4px;
  background: conic-gradient(from 180deg, var(--rarity-tint, var(--c-common)), #ffffff33, var(--rarity-tint, var(--c-common)));
  box-shadow: 0 0 0 1px rgba(255,255,255,0.08), 0 4px 14px rgba(0,0,0,0.45);
}

.char-icon {
  width: 100%;
  height: 100%;
  margin: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.1rem;
  position: relative;
  box-shadow: inset 0 0 14px rgba(0,0,0,0.45), inset 0 18px 20px -10px rgba(255,255,255,0.25);
  border: 2px solid rgba(6, 8, 16, 0.6);
}

.char-name { font-weight: 700; font-size: 0.9rem; margin-bottom: 3px; }
.char-stars { font-size: 0.78rem; letter-spacing: 1px; margin-bottom: 10px; }
.char-stars .filled { color: #ffe27a; text-shadow: 0 0 6px rgba(255,226,122,0.7); }
.char-stars .empty { color: rgba(255,255,255,0.15); }

.char-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 10px;
  font-size: 0.68rem;
  color: var(--text-dim);
  background: rgba(0,0,0,0.22);
  border-radius: 8px;
  padding: 8px 10px;
}
.char-stats .stat-row { display: flex; justify-content: space-between; }
.char-stats .stat-row b { color: var(--text-main); }

/* ===== Explore Map Screen ===== */
.explore-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}
.explore-hint { color: var(--text-dim); font-size: 0.85rem; }

.explore-map {
  position: relative;
  height: min(58vh, 460px);
  min-height: 320px;
  border-radius: 18px;
  overflow: hidden;
  cursor: pointer;
  border: 1px solid var(--border-soft);
  background:
    radial-gradient(ellipse 70% 40% at 50% 100%, rgba(120, 90, 40, 0.35), transparent 70%),
    linear-gradient(180deg, #4a6fa5 0%, #7d9bc2 28%, #d9c48f 55%, #c9a86a 75%, #a9895a 100%);
  box-shadow: inset 0 0 60px rgba(0,0,0,0.35), 0 8px 20px rgba(0,0,0,0.4);
}
/* decorative dunes/terrain */
.explore-map::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 220px 60px at 15% 60%, rgba(255,255,255,0.12), transparent 70%),
    radial-gradient(ellipse 260px 70px at 85% 45%, rgba(255,255,255,0.1), transparent 70%),
    radial-gradient(ellipse 200px 50px at 60% 80%, rgba(0,0,0,0.12), transparent 70%);
  pointer-events: none;
}
.explore-map::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 30%;
  background: linear-gradient(180deg, rgba(255,255,255,0.15), transparent);
  pointer-events: none;
}

.explore-sprite {
  position: absolute;
  transform: translate(-50%, -100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: left 0.48s ease, top 0.48s ease;
  z-index: 2;
}
.explore-sprite .explore-shadow {
  position: absolute;
  bottom: -4px;
  left: 50%;
  transform: translateX(-50%);
  width: 34px;
  height: 10px;
  border-radius: 50%;
  background: rgba(0,0,0,0.35);
  filter: blur(1px);
  z-index: 0;
}
.explore-icon {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.6rem;
  position: relative;
  z-index: 1;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.4), 0 4px 10px rgba(0,0,0,0.45);
  border: 2px solid rgba(255,255,255,0.55);
}
.explore-player .explore-icon { border-color: #fff; box-shadow: inset 0 0 12px rgba(0,0,0,0.4), 0 0 14px rgba(255,255,255,0.6); }
.explore-player { z-index: 3; }

.explore-enemy { cursor: pointer; animation: explore-idle-bob 1.6s ease-in-out infinite; }
.explore-enemy .explore-icon { border-color: #ff8a8a; }
.explore-name {
  margin-top: 3px;
  font-size: 0.62rem;
  font-weight: 700;
  color: #fff;
  background: rgba(0,0,0,0.45);
  padding: 1px 6px;
  border-radius: 8px;
  white-space: nowrap;
}
@keyframes explore-idle-bob {
  0%, 100% { transform: translate(-50%, -100%) translateY(0); }
  50% { transform: translate(-50%, -100%) translateY(-6px); }
}

/* ===== Battle Screen ===== */
.battle-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.battle-stage-label { font-weight: 700; font-size: 1.1rem; color: #ffcf7a; }
.turn-indicator { color: var(--text-dim); font-size: 0.9rem; }

.battlefield {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  position: relative;
  overflow: hidden;
  background:
    radial-gradient(ellipse 60% 45% at 50% 100%, rgba(80, 200, 130, 0.16), transparent 70%),
    linear-gradient(180deg, #232c52 0%, #171d3a 45%, #11101c 100%);
  border-radius: 18px;
  padding: 20px;
  border: 1px solid var(--border-soft);
  box-shadow: inset 0 0 60px rgba(0,0,0,0.45);
}
/* battlefield ground/horizon */
.battlefield::before {
  content: "";
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 40%;
  background: linear-gradient(180deg, transparent, rgba(20, 40, 30, 0.55) 60%, rgba(10, 20, 15, 0.6));
  z-index: 0;
}
.battlefield::after {
  content: "";
  position: absolute;
  left: 50%; bottom: 8%;
  width: 1px; height: 84%;
  background: linear-gradient(180deg, transparent, rgba(255,255,255,0.06), transparent);
  z-index: 0;
}
.party-side { flex: 1; position: relative; z-index: 1; }
.party-side h3 { margin: 0 0 10px; font-size: 0.85rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 1px; text-shadow: 0 1px 3px rgba(0,0,0,0.6); }
.vs-mark {
  font-weight: 800;
  color: #ffe27a;
  padding-top: 40px;
  position: relative;
  z-index: 1;
  text-shadow: 0 0 12px rgba(255, 226, 122, 0.6);
}

.unit-row { display: flex; flex-direction: column; gap: 12px; }

.battle-unit {
  display: flex;
  align-items: center;
  gap: 10px;
  background: linear-gradient(180deg, rgba(29, 36, 56, 0.92), rgba(15, 18, 30, 0.92));
  border: 2px solid var(--border-soft);
  border-radius: 12px;
  padding: 8px 10px;
  position: relative;
  transition: box-shadow 0.15s ease, border-color 0.15s ease, opacity 0.2s ease, transform 0.2s cubic-bezier(.34,1.56,.64,1);
  box-shadow: 0 6px 14px rgba(0,0,0,0.35);
}
.battle-unit.rarity-common { border-color: var(--c-common); }
.battle-unit.rarity-rare { border-color: var(--c-rare); }
.battle-unit.rarity-epic { border-color: var(--c-epic); }
.battle-unit.rarity-legendary { border-color: var(--c-legendary); }

.battle-unit.active-turn { box-shadow: 0 0 0 2px #fff, 0 0 16px rgba(255,255,255,0.4); }
.battle-unit.targetable { cursor: pointer; box-shadow: 0 0 0 2px var(--hp-color-low); }
.battle-unit.dead { opacity: 0.35; filter: grayscale(1); }

.battle-unit .char-icon { width: 44px; height: 44px; font-size: 1.3rem; margin: 0; flex-shrink: 0; }

/* ---- 2D fight animations ---- */
.battle-unit.attacking-right { transform: translateX(26px) scale(1.06); z-index: 8; box-shadow: 0 0 18px rgba(255,255,255,0.4); }
.battle-unit.attacking-left { transform: translateX(-26px) scale(1.06); z-index: 8; box-shadow: 0 0 18px rgba(255,255,255,0.4); }

.battle-unit.hit-flash { animation: hit-shake 0.4s ease; }
.battle-unit.hit-flash::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 12px;
  background: rgba(255, 70, 70, 0.4);
  animation: hit-fade 0.4s ease forwards;
  pointer-events: none;
  z-index: 4;
}
@keyframes hit-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(3px); }
}
@keyframes hit-fade {
  from { opacity: 1; }
  to { opacity: 0; }
}

.dmg-popup {
  position: absolute;
  top: -6px;
  right: 10px;
  font-weight: 800;
  font-size: 0.9rem;
  color: #ff6b6b;
  text-shadow: 0 1px 4px rgba(0,0,0,0.7), 0 0 8px rgba(255,107,107,0.5);
  animation: dmg-float 0.75s ease forwards;
  pointer-events: none;
  z-index: 9;
}
@keyframes dmg-float {
  0% { transform: translateY(0); opacity: 0; }
  15% { opacity: 1; }
  100% { transform: translateY(-28px); opacity: 0; }
}

.unit-info { flex: 1; min-width: 0; }
.unit-name { font-size: 0.8rem; font-weight: 700; display: flex; justify-content: space-between; }
.unit-name .cd-tag { color: var(--text-dim); font-weight: 500; font-size: 0.7rem; }

.hp-bar-track {
  width: 100%;
  height: 8px;
  border-radius: 6px;
  background: rgba(255,255,255,0.08);
  overflow: hidden;
  margin-top: 4px;
}
.hp-bar-fill {
  height: 100%;
  background: var(--hp-color);
  transition: width 0.35s ease, background-color 0.35s ease;
}
.hp-bar-fill.low { background: var(--hp-color-low); }
.hp-text { font-size: 0.65rem; color: var(--text-dim); margin-top: 2px; }

.action-panel {
  margin-top: 18px;
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 14px;
  padding: 14px 16px;
}
.action-panel-title { font-size: 0.85rem; color: var(--text-dim); margin-bottom: 10px; }
.action-panel-title span { color: var(--text-main); font-weight: 700; }
.action-buttons { display: flex; gap: 10px; flex-wrap: wrap; }
.target-hint { margin-top: 10px; color: #ffcf7a; font-size: 0.85rem; }

.battle-log {
  margin-top: 16px;
  background: rgba(0,0,0,0.25);
  border-radius: 12px;
  padding: 10px 14px;
  max-height: 140px;
  overflow-y: auto;
  font-size: 0.8rem;
  color: var(--text-dim);
  display: flex;
  flex-direction: column-reverse;
  gap: 4px;
}

/* ===== Result Screen ===== */
.result-card {
  max-width: 420px;
  margin: 60px auto;
  text-align: center;
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 18px;
  padding: 32px 24px;
}
.result-card h2 { margin-top: 0; font-size: 1.6rem; }
.result-card p { color: var(--text-dim); margin-bottom: 24px; }

/* ===== Summon Reveal Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4,6,14,0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.reveal-card {
  width: 260px;
  padding: 28px 20px;
  border-radius: 20px;
  text-align: center;
  background: var(--bg-panel);
  border: 3px solid var(--c-common);
  animation: reveal-pop 0.35s ease;
}
.reveal-card.rarity-common { border-color: var(--c-common); }
.reveal-card.rarity-rare { border-color: var(--c-rare); box-shadow: 0 0 30px rgba(63,169,245,0.5); }
.reveal-card.rarity-epic { border-color: var(--c-epic); box-shadow: 0 0 40px rgba(176,107,250,0.6); }
.reveal-card.rarity-legendary { border-color: var(--c-legendary); box-shadow: 0 0 55px rgba(245,185,66,0.75); }

@keyframes reveal-pop {
  0% { transform: scale(0.6); opacity: 0; }
  70% { transform: scale(1.06); opacity: 1; }
  100% { transform: scale(1); }
}

.reveal-rarity { font-size: 0.8rem; letter-spacing: 2px; text-transform: uppercase; color: var(--text-dim); margin-bottom: 10px; }
.reveal-icon {
  width: 96px;
  height: 96px;
  margin: 0 auto 14px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  box-shadow: inset 0 0 20px rgba(0,0,0,0.4), 0 6px 18px rgba(0,0,0,0.4);
}
.reveal-name { font-weight: 800; font-size: 1.15rem; margin-bottom: 6px; }
.reveal-stars { margin-bottom: 10px; letter-spacing: 2px; }
.reveal-stars .filled { color: #ffe27a; text-shadow: 0 0 6px rgba(255,226,122,0.7); }
.reveal-stars .empty { color: rgba(255,255,255,0.2); }
.reveal-dupe { color: var(--text-dim); font-size: 0.78rem; margin-bottom: 16px; }

@media (max-width: 640px) {
  .battlefield { flex-direction: column; }
  .vs-mark { padding-top: 0; align-self: center; }
}
