:root {
  --bg: #0b0a09;
  --bg-2: #13110f;
  --panel: rgba(16, 14, 12, 0.92);
  --ash: #2c2824;
  --ash-line: rgba(215, 196, 160, 0.28);
  --champagne: #d7c4a0;
  --gold: #c9a56a;
  --ember: #e06030;
  --ember-dim: #a33c1c;
  --text: #ebe6dc;
  --text-dim: #9a9286;
  --danger: #c4453c;
  --ok: #8faf7a;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  --serif: "Source Han Serif SC", "Noto Serif SC", "Songti SC", "STSong", "SimSun", "Palatino Linotype", "Times New Roman", serif;
  --sans: "Source Han Sans SC", "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Segoe UI", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  overflow: hidden;
  user-select: none;
  touch-action: manipulation;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
}
#app::after {
  content: "";
  pointer-events: none;
  position: absolute;
  inset: 0;
  z-index: 40;
  opacity: 0.055;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)' opacity='.9'/%3E%3C/svg%3E");
  mix-blend-mode: overlay;
}

canvas#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #0b0a09;
}

.hidden { display: none !important; }

.screen {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) 22px max(24px, env(safe-area-inset-bottom));
  background:
    radial-gradient(ellipse at 50% 30%, rgba(224, 96, 48, 0.16), transparent 50%),
    radial-gradient(ellipse at 50% 100%, rgba(20, 16, 12, 0.9), #0b0a09 70%);
}

.brand {
  text-align: center;
  margin-bottom: 8px;
}
.brand h1 {
  font-family: var(--serif);
  font-weight: 500;
  font-size: clamp(42px, 12vw, 72px);
  letter-spacing: 0.28em;
  margin: 0 0 0 0.14em;
  color: var(--champagne);
  text-shadow: 0 0 28px rgba(224, 96, 48, 0.28);
}
.brand .sub {
  margin: 14px auto 0;
  max-width: 22em;
  font-size: 13px;
  line-height: 1.75;
  color: var(--text-dim);
  letter-spacing: 0.04em;
}
.brand .rule {
  width: 48px;
  height: 1px;
  margin: 22px auto 0;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
}

.btn {
  appearance: none;
  border: 1px solid var(--ash-line);
  background: linear-gradient(180deg, rgba(40, 34, 28, 0.9), rgba(18, 16, 14, 0.95));
  color: var(--champagne);
  font-family: var(--sans);
  font-size: 15px;
  letter-spacing: 0.18em;
  padding: 12px 28px;
  border-radius: 2px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.btn:active { transform: translateY(1px); }
.btn.primary {
  border-color: rgba(201, 165, 106, 0.55);
  color: #f3e6c8;
  background: linear-gradient(180deg, rgba(80, 52, 28, 0.85), rgba(32, 22, 14, 0.95));
}
.btn.ghost {
  background: transparent;
  box-shadow: none;
  color: var(--text-dim);
  letter-spacing: 0.1em;
}
.btn:disabled {
  opacity: 0.4;
  cursor: default;
}
.btn-row {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 36px;
  min-width: 220px;
  align-items: stretch;
}
.btn-row .btn { width: 100%; }

.class-grid {
  width: min(100%, 420px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 20px;
}
.class-card {
  text-align: left;
  border: 1px solid var(--ash-line);
  background: rgba(18, 16, 14, 0.78);
  color: inherit;
  padding: 14px 14px 14px 16px;
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 12px;
  cursor: pointer;
  border-radius: 2px;
  font-family: var(--sans);
}
.class-card.selected,
.class-card:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 1px rgba(201, 165, 106, 0.25), var(--shadow);
}
.class-card canvas {
  width: 56px;
  height: 72px;
  background: transparent;
}
.class-card h3 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 500;
  font-size: 20px;
  letter-spacing: 0.16em;
  color: var(--champagne);
}
.class-card .role {
  font-size: 11px;
  color: var(--gold);
  letter-spacing: 0.2em;
  margin: 2px 0 6px;
}
.class-card p {
  margin: 0;
  font-size: 12px;
  line-height: 1.65;
  color: var(--text-dim);
}

.screen-title-bar {
  position: absolute;
  top: max(16px, env(safe-area-inset-top));
  left: 0;
  right: 0;
  text-align: center;
  font-family: var(--serif);
  letter-spacing: 0.28em;
  color: var(--champagne);
  font-size: 18px;
  pointer-events: none;
}

/* HUD */
#hud {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
}
#hud > * { pointer-events: auto; }
#hud-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: max(10px, env(safe-area-inset-top)) 12px 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: start;
}
.bars {
  min-width: 0;
  max-width: 360px;
}
.name-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 5px;
  font-size: 12px;
  letter-spacing: 0.08em;
  color: var(--champagne);
}
.name-row .lv { color: var(--gold); }
.name-row .embers {
  margin-left: auto;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.bar {
  height: 8px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(215, 196, 160, 0.2);
  border-radius: 1px;
  overflow: hidden;
  position: relative;
  margin-bottom: 4px;
}
.bar > i {
  display: block;
  height: 100%;
  width: 0%;
  transform-origin: left;
  background: linear-gradient(90deg, #6a2a1a, #e06030);
}
.bar.xp { height: 5px; }
.bar.xp > i { background: linear-gradient(90deg, #7a6240, #d7b56d); }
.bar-label {
  position: absolute;
  right: 4px;
  top: -1px;
  font-size: 9px;
  color: #f0e6d4;
  text-shadow: 0 1px 2px #000;
}
.hud-actions {
  display: flex;
  gap: 6px;
}
.icon-btn {
  pointer-events: auto;
  width: 40px;
  height: 40px;
  border: 1px solid var(--ash-line);
  background: rgba(14, 12, 10, 0.72);
  color: var(--champagne);
  border-radius: 2px;
  font-size: 11px;
  letter-spacing: 0.08em;
  cursor: pointer;
  font-family: var(--sans);
  padding: 0;
}

#toast {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 72px);
  left: 50%;
  transform: translateX(-50%);
  max-width: min(86vw, 360px);
  text-align: center;
  font-size: 13px;
  line-height: 1.6;
  color: var(--champagne);
  text-shadow: 0 1px 4px #000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
  z-index: 12;
}
#toast.show { opacity: 1; }

#interact-prompt {
  position: absolute;
  left: 50%;
  bottom: 168px;
  transform: translateX(-50%);
  pointer-events: none;
  text-align: center;
  font-size: 12px;
  letter-spacing: 0.14em;
  color: var(--champagne);
  text-shadow: 0 1px 6px #000;
  padding: 6px 12px;
  border: 1px solid rgba(201, 165, 106, 0.35);
  background: rgba(12, 10, 8, 0.7);
  border-radius: 2px;
}
@media (pointer: fine) {
  #interact-prompt { bottom: 48px; }
}

#damage-layer, #pickup-layer { display: none; }

/* Touch controls — reserved at the bottom so combat stays visible */
#touch {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 168px;
  padding-bottom: env(safe-area-inset-bottom);
  pointer-events: none;
  display: none;
}
body.touch #touch { display: block; }

#stick-zone {
  position: absolute;
  left: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  width: 128px;
  height: 128px;
  pointer-events: auto;
  touch-action: none;
}
#stick-base, #stick-knob {
  position: absolute;
  border-radius: 50%;
}
#stick-base {
  inset: 14px;
  border: 1px solid rgba(215, 196, 160, 0.28);
  background: rgba(8, 7, 6, 0.35);
}
#stick-knob {
  width: 52px;
  height: 52px;
  left: 38px;
  top: 38px;
  background: radial-gradient(circle at 35% 30%, rgba(215, 196, 160, 0.55), rgba(40, 32, 24, 0.85));
  border: 1px solid rgba(215, 196, 160, 0.4);
  box-shadow: 0 4px 12px rgba(0,0,0,.4);
  will-change: transform;
}

#skill-pad {
  position: absolute;
  right: 10px;
  bottom: calc(10px + env(safe-area-inset-bottom));
  width: 168px;
  height: 148px;
  pointer-events: none;
}
.skill-btn {
  position: absolute;
  pointer-events: auto;
  border: 1px solid rgba(215, 196, 160, 0.35);
  background: rgba(12, 10, 8, 0.72);
  color: var(--champagne);
  border-radius: 50%;
  font-family: var(--sans);
  font-size: 11px;
  letter-spacing: 0.04em;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.2;
  overflow: hidden;
}
.skill-btn .cd {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  transform-origin: bottom;
  transform: scaleY(0);
  pointer-events: none;
}
.skill-btn .label { position: relative; z-index: 1; padding: 0 4px; }
.skill-btn.locked { opacity: 0.35; }
.skill-btn.at { width: 68px; height: 68px; right: 8px; bottom: 8px; font-size: 13px; border-color: rgba(224, 96, 48, 0.5); }
.skill-btn.s1 { width: 50px; height: 50px; right: 86px; bottom: 10px; }
.skill-btn.s2 { width: 50px; height: 50px; right: 78px; bottom: 68px; }
.skill-btn.s3 { width: 50px; height: 50px; right: 20px; bottom: 86px; }
.skill-btn.pot {
  width: 42px;
  height: 42px;
  left: -52px;
  bottom: 8px;
  border-radius: 2px;
  font-size: 10px;
}
#pot-desktop { display: none; }
@media (pointer: fine) {
  body:not(.touch) #pot-desktop {
    display: flex;
    position: absolute;
    right: 16px;
    bottom: 20px;
    width: 64px;
    height: 40px;
    pointer-events: auto;
    border-radius: 2px;
    border: 1px solid rgba(215, 196, 160, 0.35);
    background: rgba(12, 10, 8, 0.72);
    color: var(--champagne);
    font-family: var(--sans);
    font-size: 11px;
    letter-spacing: 0.06em;
    align-items: center;
    justify-content: center;
    cursor: pointer;
  }
}

/* Panels */
.panel {
  position: absolute;
  inset: 0;
  z-index: 25;
  background: rgba(6, 5, 4, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 14px max(16px, env(safe-area-inset-bottom));
}
.panel-card {
  width: min(100%, 440px);
  max-height: 100%;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--ash-line);
  box-shadow: var(--shadow);
  padding: 18px 16px 16px;
  border-radius: 2px;
}
.panel-card h2 {
  font-family: var(--serif);
  font-weight: 500;
  letter-spacing: 0.2em;
  font-size: 18px;
  margin: 0 0 14px;
  color: var(--champagne);
  text-align: center;
}
.panel-card .body {
  font-size: 13px;
  line-height: 1.8;
  color: var(--text-dim);
}
.inv-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}
.equip-row, .inv-grid {
  display: grid;
  gap: 6px;
}
.equip-row { grid-template-columns: repeat(5, 1fr); }
.inv-grid { grid-template-columns: repeat(4, 1fr); }
.slot, .inv-cell {
  border: 1px solid rgba(215, 196, 160, 0.22);
  background: rgba(8, 7, 6, 0.55);
  min-height: 58px;
  border-radius: 2px;
  padding: 4px;
  font-size: 10px;
  line-height: 1.3;
  color: var(--text);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  gap: 2px;
}
.slot .slot-name, .inv-cell .empty {
  color: var(--text-dim);
  font-size: 9px;
  letter-spacing: 0.08em;
}
.slot.filled, .inv-cell.filled { border-color: rgba(201, 165, 106, 0.45); }
.item-name { letter-spacing: 0.02em; }
.stats-box {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px 12px;
  font-size: 12px;
  color: var(--text-dim);
  padding: 8px 4px;
  border-top: 1px solid rgba(215, 196, 160, 0.15);
  border-bottom: 1px solid rgba(215, 196, 160, 0.15);
}
.stats-box b { color: var(--champagne); font-weight: 500; }
.compare {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid rgba(215, 196, 160, 0.2);
  font-size: 12px;
  line-height: 1.7;
  color: var(--text-dim);
  min-height: 4em;
}
.compare .up { color: var(--ok); }
.compare .down { color: #d08078; }
.panel-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
}
.panel-actions .btn { flex: 1; letter-spacing: 0.1em; padding: 10px 12px; font-size: 13px; }

#screen-death .brand h1 { font-size: 32px; letter-spacing: 0.2em; }

.help-keys {
  margin-top: 28px;
  font-size: 11px;
  color: #6e685f;
  letter-spacing: 0.06em;
  line-height: 1.8;
  text-align: center;
}

@media (min-width: 720px) {
  .class-grid { width: 520px; }
  .inv-layout { grid-template-columns: 200px 1fr; }
}

/* Desktop: hide stick, keep skill cluster so cooldown is visible */
body:not(.touch) #touch {
  display: block;
  height: 120px;
  pointer-events: none;
}
body:not(.touch) #stick-zone { display: none; }
body:not(.touch) #skill-pad {
  pointer-events: none;
  right: 18px;
  bottom: calc(16px + env(safe-area-inset-bottom));
}
body:not(.touch) .skill-btn.pot { display: none; }

#interact-prompt.clickable { pointer-events: auto; cursor: pointer; }

.panel, .panel-card { touch-action: pan-y; }
