:root {
  --bg: #14121f;
  --panel: #1e1b2e;
  --panel-alt: #262240;
  --text: #f1eefc;
  --text-dim: #a79fc9;
  --hp: #e0507a;
  --mp: #4aa3e0;
  --enemy-hp: #e0507a;
  --accent: #f2b84b;
  --danger: #e0507a;
  --ok: #4ade80;
  --border: #3a3560;

  --rank-common: #a79fc9;
  --rank-uncommon: #4ade80;
  --rank-rare: #4aa3e0;
  --rank-epic: #a855f7;
}

[data-rank='common'] {
  --rank-color: var(--rank-common);
}
[data-rank='uncommon'] {
  --rank-color: var(--rank-uncommon);
}
[data-rank='rare'] {
  --rank-color: var(--rank-rare);
}
[data-rank='epic'] {
  --rank-color: var(--rank-epic);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  touch-action: manipulation;
}

body {
  background: #0a0912;
  color: var(--text);
  font-family: 'Segoe UI', 'Hiragino Sans', 'Yu Gothic', sans-serif;
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  max-width: 480px;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  position: relative;
}

/* ---------- status bar ---------- */

#status-bar {
  padding: 10px 14px 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.status-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 6px;
}

#floor-label {
  color: var(--accent);
}

.level-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-button {
  border: 1px solid var(--border);
  background: var(--panel-alt);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.85rem;
  line-height: 1;
  padding: 4px 6px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.icon-button:active {
  transform: scale(0.94);
}

.exp-bar .bar-fill {
  background: linear-gradient(90deg, var(--accent), #f7d68a);
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 3px 0;
}

.bar-label {
  width: 24px;
  font-size: 0.75rem;
  color: var(--text-dim);
  flex-shrink: 0;
}

.bar {
  flex: 1;
  height: 12px;
  background: #0d0b16;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.bar-fill {
  height: 100%;
  width: 0%;
  transition: width 0.25s ease;
}

.hp-bar .bar-fill,
.enemy-hp-bar .bar-fill {
  background: linear-gradient(90deg, var(--hp), #f0839f);
}

.mp-bar .bar-fill {
  background: linear-gradient(90deg, var(--mp), #7cc4f0);
}

.bar-text {
  width: 64px;
  text-align: right;
  font-size: 0.75rem;
  color: var(--text-dim);
  flex-shrink: 0;
}

/* ---------- battle area ---------- */

#battle-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  gap: 8px;
  min-height: 180px;
}

.enemy-name-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

#enemy-name {
  font-size: 1rem;
  font-weight: 700;
}

.boss-tag {
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  color: #1a1420;
  background: linear-gradient(135deg, var(--accent), #e0507a);
  border-radius: 999px;
  padding: 2px 8px;
  box-shadow: 0 0 8px rgba(242, 184, 75, 0.6);
}

.boss-tag.hidden {
  display: none;
}

#enemy-visual {
  font-size: 3.2rem;
  line-height: 1;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.4));
}

#enemy-visual.hit {
  animation: shake 0.25s ease;
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-6px);
  }
  75% {
    transform: translateX(6px);
  }
}

#battle-area .bar-row {
  width: 100%;
  max-width: 260px;
}

.status-icons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  min-height: 20px;
  font-size: 0.72rem;
}

.status-chip {
  padding: 2px 7px;
  border-radius: 10px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

.status-chip.player {
  border-color: var(--mp);
}

.status-chip.enemy {
  border-color: var(--danger);
}

/* ---------- battle log ---------- */

#battle-log {
  background: #0d0b16;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 8px 14px;
  font-size: 0.8rem;
  color: var(--text-dim);
  height: 84px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

#battle-log p {
  margin: 2px 0;
}

#battle-log p:first-child {
  color: var(--text);
}

/* ---------- skill panel ---------- */

#skill-panel {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 8px;
  padding: 10px;
  background: var(--panel);
  min-height: 168px;
}

.skill-button {
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-left-color: var(--rank-color, var(--border));
  background: var(--panel-alt);
  color: var(--text);
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 700;
  padding: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 74px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.skill-button .skill-name {
  color: var(--rank-color, var(--text));
}

.skill-button:active:not(:disabled) {
  transform: scale(0.96);
  background: var(--panel);
}

.skill-button .skill-cost {
  font-size: 0.7rem;
  font-weight: 400;
  color: var(--text-dim);
}

.skill-button:disabled {
  opacity: 0.45;
}

.skill-button.empty {
  border-style: dashed;
  color: var(--text-dim);
  font-weight: 400;
}

/* ---------- overlay / dialogs ---------- */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(6, 5, 12, 0.86);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

.overlay.hidden {
  display: none;
}

.overlay-content {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  max-height: 100%;
  overflow-y: auto;
  text-align: center;
}

.overlay-content h2 {
  margin-top: 0;
}

.candidate-list,
.dialog-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 14px;
}

.candidate-button,
.dialog-button {
  display: block;
  width: 100%;
  box-sizing: border-box;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-left-color: var(--rank-color, var(--border));
  color: var(--text);
  border-radius: 10px;
  padding: 12px;
  font-size: 0.95rem;
  font-weight: 700;
  text-align: left;
  text-decoration: none;
}

.candidate-button .candidate-name {
  color: var(--rank-color, var(--text));
}

.candidate-button .candidate-desc {
  display: block;
  font-weight: 400;
  font-size: 0.78rem;
  color: var(--text-dim);
  margin-top: 2px;
}

.rank-tag {
  color: var(--rank-color, var(--text-dim));
  font-weight: 700;
}

.dialog-button.primary {
  background: var(--accent);
  color: #241a06;
  text-align: center;
}

.dialog-button.danger {
  border-color: var(--danger);
  color: var(--danger);
}

.dialog-button:disabled {
  opacity: 0.4;
}

.overlay-hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-top: 8px;
}

.alloc-summary {
  min-height: 1em;
}

/* ---------- stats panel ---------- */

.stat-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 14px;
  text-align: left;
}

.stat-row {
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

.stat-row-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
}

.stat-row-desc {
  font-size: 0.76rem;
  color: var(--text-dim);
  margin-top: 3px;
}

.stat-plus {
  background: var(--accent);
  color: #241a06;
  border: none;
  border-radius: 8px;
  width: 32px;
  height: 32px;
  font-size: 1.1rem;
  font-weight: 900;
  line-height: 1;
  flex-shrink: 0;
}

.stat-plus:active {
  transform: scale(0.92);
}

/* ---------- ranking ---------- */

.rank-section-title {
  margin: 14px 0 8px;
  font-size: 0.85rem;
  color: var(--text-dim);
  text-align: left;
}

.rank-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rank-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  text-align: left;
}

.rank-index {
  color: var(--accent);
  font-weight: 900;
  width: 1.4em;
  flex-shrink: 0;
}

.rank-floor {
  font-weight: 700;
}

.rank-meta {
  color: var(--text-dim);
  font-size: 0.76rem;
  margin-left: auto;
}

.rank-section-subtitle {
  margin: 10px 0 6px;
  font-size: 0.76rem;
  color: var(--text-dim);
  text-align: left;
  font-weight: 400;
}

/* ---------- global ranking opt-in ---------- */

.global-rank-optin {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.nickname-input {
  display: block;
  width: 100%;
  box-sizing: border-box;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.9rem;
  margin: 8px 0;
}

.nickname-input::placeholder {
  color: var(--text-dim);
}

/* ---------- level-up banner ---------- */

.levelup-banner {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  overflow: hidden;
}

.levelup-banner.hidden {
  display: none;
}

.levelup-flash {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, rgba(242, 184, 75, 0.35), transparent 60%);
  animation: levelup-flash 1.6s ease forwards;
}

.levelup-burst {
  position: absolute;
  width: 0;
  height: 0;
}

.levelup-burst .spark {
  position: absolute;
  font-size: 1.3rem;
  opacity: 0;
  animation: spark-burst 1.1s ease-out forwards;
}

.levelup-burst .spark::before {
  content: '✨';
}

.levelup-burst .spark:nth-child(1) {
  --angle: 0deg;
}
.levelup-burst .spark:nth-child(2) {
  --angle: 45deg;
}
.levelup-burst .spark:nth-child(3) {
  --angle: 90deg;
}
.levelup-burst .spark:nth-child(4) {
  --angle: 135deg;
}
.levelup-burst .spark:nth-child(5) {
  --angle: 180deg;
}
.levelup-burst .spark:nth-child(6) {
  --angle: 225deg;
}
.levelup-burst .spark:nth-child(7) {
  --angle: 270deg;
}
.levelup-burst .spark:nth-child(8) {
  --angle: 315deg;
}

@keyframes spark-burst {
  0% {
    opacity: 0;
    transform: rotate(var(--angle)) translateY(0) scale(0.5);
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: rotate(var(--angle)) translateY(-90px) scale(1.1);
  }
}

@keyframes levelup-flash {
  0% {
    opacity: 0;
  }
  15% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.levelup-content {
  position: relative;
  text-align: center;
  animation: levelup-content-life 1.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.levelup-title {
  font-size: 2.2rem;
  font-weight: 900;
  color: var(--accent);
  letter-spacing: 0.05em;
  text-shadow: 0 0 12px var(--accent), 0 0 28px rgba(242, 184, 75, 0.8);
}

.levelup-level {
  margin-top: 6px;
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text);
  text-shadow: 0 0 10px rgba(0, 0, 0, 0.7);
}

@keyframes levelup-content-life {
  0% {
    transform: scale(0.3);
    opacity: 0;
  }
  25% {
    transform: scale(1.2);
    opacity: 1;
  }
  35% {
    transform: scale(0.95);
  }
  45% {
    transform: scale(1.05);
  }
  50% {
    transform: scale(1);
    opacity: 1;
  }
  85% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

/* ---------- floor milestone toast ---------- */

.floor-milestone-toast {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 21;
  pointer-events: none;
}

.floor-milestone-toast.hidden {
  display: none;
}

.floor-milestone-toast span {
  display: inline-block;
  transform: translateX(-50%);
  background: var(--panel-alt);
  border: 1px solid var(--accent);
  color: var(--accent);
  font-weight: 800;
  font-size: 0.9rem;
  letter-spacing: 0.03em;
  padding: 6px 16px;
  border-radius: 999px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5);
  white-space: nowrap;
  animation: floor-milestone-life 1.8s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes floor-milestone-life {
  0% {
    transform: translateX(-50%) translateY(-16px) scale(0.8);
    opacity: 0;
  }
  15% {
    transform: translateX(-50%) translateY(0) scale(1);
    opacity: 1;
  }
  80% {
    transform: translateX(-50%) translateY(0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translateX(-50%) translateY(-8px) scale(0.95);
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .levelup-content,
  .levelup-flash,
  .levelup-burst .spark,
  .floor-milestone-toast span {
    animation: none;
    opacity: 1;
  }
}
