/* ============================================================
   Fablebound — design system
   Direction: dark storybook / illuminated manuscript.
   Ink navy surfaces, gold-leaf hairlines, ember accents,
   Grenze Gotisch display + Alegreya Sans body (no serifs).
   ============================================================ */

:root {
  --bg-0: #0b0910;
  --bg-1: #120e1c;
  --panel: #161126;
  --panel-2: #1e1733;
  --panel-3: #251d3e;
  --line: rgba(212, 175, 55, 0.14);
  --line-strong: rgba(212, 175, 55, 0.4);
  --text: #efe6d5;
  --text-dim: #a89e8b;
  --text-faint: #6f6759;
  --gold: #d4af37;
  --gold-bright: #f0cf6d;
  --ember: #ff8c42;
  --ink-blue: #8ecae6;
  --crimson: #e5484d;
  --green: #7dc95e;
  --font-display: 'Grenze Gotisch', 'Segoe UI', sans-serif;
  --font-body: 'Alegreya Sans', 'Segoe UI', sans-serif;
  --r-lg: 16px;
  --r-md: 11px;
  --r-sm: 7px;
  --shadow: 0 14px 40px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }

body {
  min-height: 100%;
  font-family: var(--font-body);
  color: var(--text);
  background:
    radial-gradient(1100px 700px at 15% -10%, #1d1430 0%, transparent 60%),
    radial-gradient(900px 650px at 110% 115%, #2a1508 0%, transparent 55%),
    var(--bg-0);
  background-attachment: fixed;
  line-height: 1.45;
}

/* paper grain */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.05;
  z-index: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)' opacity='0.6'/%3E%3C/svg%3E");
}

.hidden { display: none !important; }

:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; border-radius: 4px; }

#app {
  position: relative;
  z-index: 1;
  max-width: 1480px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 1.4vw, 20px);
  padding: clamp(10px, 2vw, 28px);
}

/* ================= top bar ================= */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }

.brand h1 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.7rem, 1.2rem + 2.2vw, 2.7rem);
  letter-spacing: 0.03em;
  background: linear-gradient(180deg, var(--gold-bright), var(--gold) 55%, #9a7b1f);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 34px rgba(212, 175, 55, 0.18);
}
.tagline { color: var(--text-faint); font-style: italic; font-size: clamp(0.8rem, 0.75rem + 0.2vw, 0.95rem); letter-spacing: 0.06em; margin-top: -2px; }

.currencies { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chip {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  box-shadow: var(--shadow);
  font-weight: 700;
  font-size: clamp(0.95rem, 0.85rem + 0.3vw, 1.15rem);
  transition: opacity 0.3s;
}
.chip svg { width: 18px; height: 18px; }
.chip-ink svg { fill: var(--ink-blue); }
.chip-ink { color: var(--ink-blue); }
.chip-ember svg { fill: var(--ember); }
.chip-ember { color: var(--ember); }
.chip-ember.dim { opacity: 0.45; }

#saveBlip { font-size: 0.75rem; color: var(--green); opacity: 0; transition: opacity 0.3s; }
#saveBlip.show { opacity: 0.85; }

.icon-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}
.icon-btn:hover { border-color: var(--line-strong); transform: translateY(-1px); }

/* ================= layout ================= */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(330px, 30vw, 430px);
  gap: clamp(12px, 1.6vw, 24px);
  align-items: start;
  flex: 1;
}

.card {
  background: linear-gradient(180deg, rgba(30, 23, 51, 0.5), rgba(22, 17, 38, 0.75));
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
}

/* ================= stage ================= */
.stage { padding: clamp(12px, 1.6vw, 22px); display: flex; flex-direction: column; gap: 14px; }

.stage-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.loc { display: flex; flex-direction: column; }
.chapter { font-family: var(--font-display); font-size: clamp(1.1rem, 0.95rem + 0.7vw, 1.5rem); letter-spacing: 0.03em; color: var(--gold-bright); }
.zone { color: var(--text-faint); font-size: 0.8rem; letter-spacing: 0.14em; text-transform: uppercase; }

.pips { display: flex; gap: 6px; flex-wrap: wrap; }
.pip { width: 10px; height: 10px; border-radius: 50%; background: rgba(255, 255, 255, 0.09); border: 1px solid var(--line); transition: background 0.2s; }
.pip.lit { background: var(--gold); box-shadow: 0 0 8px rgba(212, 175, 55, 0.7); border-color: var(--gold); }

.boss-timer {
  position: relative;
  width: min(300px, 42vw);
  height: 18px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid var(--line-strong);
  overflow: hidden;
}
#bossTimeFill { height: 100%; background: linear-gradient(90deg, var(--ember), var(--crimson)); transition: width 0.1s linear; }
#bossTimeText { position: absolute; inset: 0; display: grid; place-items: center; font-size: 0.7rem; font-weight: 700; text-shadow: 0 1px 3px #000; }

.stage-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.mini-btn {
  min-height: 36px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  background: var(--panel-2);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.15s, border-color 0.15s;
}
.mini-btn.off { opacity: 0.5; border-color: var(--line); }
.mini-btn.boss {
  background: linear-gradient(180deg, #3d2b09, #241804);
  color: var(--gold-bright);
  border-color: var(--gold);
  animation: pulseGold 1.6s infinite;
}
@keyframes pulseGold {
  0%, 100% { box-shadow: 0 0 6px rgba(212, 175, 55, 0.35); }
  50% { box-shadow: 0 0 18px rgba(212, 175, 55, 0.75); }
}

.canvas-wrap {
  position: relative;
  height: clamp(300px, 46vh, 540px);
  border-radius: var(--r-md);
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 30%, var(--panel-2), var(--bg-1) 75%);
  border: 1px solid var(--line);
}
#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: manipulation;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

.monster-plate {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  width: min(440px, 86%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  pointer-events: none;
}
#monsterName { font-family: var(--font-display); font-size: clamp(0.95rem, 0.85rem + 0.5vw, 1.2rem); letter-spacing: 0.03em; text-shadow: 0 2px 8px #000; }
#monsterName.boss { color: var(--gold-bright); }

.hpbar {
  position: relative;
  width: 100%;
  height: 20px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid var(--line);
  overflow: hidden;
}
#hpFill { height: 100%; width: 100%; background: linear-gradient(90deg, #a4161a, #e5484d); transition: width 0.12s linear; border-radius: 999px; }
.hpbar.boss #hpFill { background: linear-gradient(90deg, #6d28d9, #a855f7); }
.hpbar.boss { border-color: var(--line-strong); box-shadow: 0 0 18px rgba(212, 175, 55, 0.25); }
#hpText { position: absolute; inset: 0; display: grid; place-items: center; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.04em; text-shadow: 0 1px 3px #000; }

.stage-stats { display: grid; grid-template-columns: 1fr 1fr 1.4fr; gap: 10px; }
.stat { display: flex; flex-direction: column; gap: 4px; padding: 10px 14px; border: 1px solid var(--line); border-radius: var(--r-md); background: var(--panel-2); }
.stat-l { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-faint); }
.stat-v { font-weight: 700; font-size: clamp(1rem, 0.9rem + 0.4vw, 1.3rem); }
.stat.combo .stat-l b { color: var(--ember); font-size: 0.85rem; letter-spacing: 0; }
.combo-track { height: 8px; border-radius: 999px; background: rgba(0, 0, 0, 0.5); overflow: hidden; margin-top: 6px; }
#comboFill { height: 100%; width: 0; background: linear-gradient(90deg, var(--ember), var(--gold-bright)); border-radius: 999px; transition: width 0.1s linear; }

/* ================= panel ================= */
.panel { display: flex; flex-direction: column; overflow: hidden; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); padding: 0 4px; }
.tab {
  flex: 1;
  min-height: 46px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.8rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: color 0.15s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--gold-bright); border-bottom-color: var(--gold); }
.badge { font-size: 0.68rem; background: var(--panel-3); border: 1px solid var(--line); padding: 2px 7px; border-radius: 999px; color: var(--text-dim); font-family: var(--font-body); }

.buy-row { display: flex; align-items: center; gap: 8px; padding: 10px 12px 0; color: var(--text-faint); font-size: 0.85rem; }
.amt {
  min-width: 52px;
  min-height: 34px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text-dim);
  cursor: pointer;
  font-weight: 700;
  font-family: var(--font-body);
  transition: background 0.15s, color 0.15s;
}
.amt.active { color: var(--bg-0); background: var(--gold); border-color: var(--gold); }

.tab-body { display: flex; flex-direction: column; gap: 10px; padding: 12px; overflow-y: auto; flex: 1; min-height: 0; }
.tab-body::-webkit-scrollbar { width: 8px; }
.tab-body::-webkit-scrollbar-thumb { background: var(--panel-3); border-radius: 4px; }

/* ---- hero cards ---- */
.hero-card {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  transition: border-color 0.2s;
}
.hero-card:hover { border-color: var(--line-strong); }
.hero-card.teaser { opacity: 0.5; filter: saturate(0.35); }

.hero-portrait {
  flex: 0 0 auto;
  width: 52px;
  height: 52px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.04em;
  color: var(--pc, var(--gold));
  background: radial-gradient(circle at 30% 25%, rgba(255, 255, 255, 0.08), transparent 60%), var(--bg-1);
  border: 1px solid var(--pc, var(--gold));
  box-shadow: inset 0 0 14px rgba(0, 0, 0, 0.6);
}
.hero-info { flex: 1; min-width: 0; }
.hero-name { font-weight: 700; font-size: 0.95rem; display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.hero-lvl { color: var(--gold); font-size: 0.75rem; letter-spacing: 0.05em; }
.hero-flavor { color: var(--text-faint); font-style: italic; font-size: 0.78rem; line-height: 1.3; }
.hero-dps { color: var(--text-dim); font-size: 0.8rem; margin-top: 2px; }

.buy-btn {
  flex: 0 0 auto;
  min-width: 86px;
  min-height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 12px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line);
  background: linear-gradient(180deg, #2a2140, #1d1731);
  color: var(--text);
  cursor: pointer;
  font-family: var(--font-body);
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
}
.buy-btn:not(:disabled):hover { transform: translateY(-1px); border-color: var(--gold); box-shadow: 0 4px 14px rgba(212, 175, 55, 0.15); }
.buy-btn:not(:disabled):active { transform: translateY(0) scale(0.97); }
.buy-btn:disabled { opacity: 0.42; cursor: not-allowed; }
.buy-count { font-size: 0.72rem; color: var(--text-dim); }
.buy-cost { font-weight: 700; color: var(--ink-blue); font-size: 0.9rem; }
.buy-cost::before { content: '◆ '; font-size: 0.7em; vertical-align: 1px; }

/* ---- rewrite tab ---- */
.rewrite { display: flex; flex-direction: column; gap: 14px; padding: 6px 2px; }
.rewrite h3 { font-family: var(--font-display); font-weight: 500; font-size: 1.4rem; letter-spacing: 0.02em; color: var(--gold-bright); }
.lore { color: var(--text-dim); font-size: 0.9rem; }
.lore b { color: var(--ember); }
.rw-stats { display: flex; flex-direction: column; gap: 8px; }
.rw-stats div { display: flex; justify-content: space-between; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--r-sm); background: var(--panel-2); font-size: 0.9rem; }
.rw-stats b { color: var(--ember); }
.req { color: var(--text-faint); font-style: italic; font-size: 0.85rem; }

/* ---- deeds tab ---- */
.deeds { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.deed {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  transition: opacity 0.3s, filter 0.3s;
}
.deed-star { color: var(--gold); font-size: 1.1rem; }
.deed-name { font-weight: 700; font-size: 0.85rem; }
.deed-desc { font-size: 0.72rem; color: var(--text-dim); }
.deed.locked { opacity: 0.35; filter: grayscale(0.8); border-color: var(--line); }

/* ================= buttons ================= */
.btn {
  min-height: 44px;
  padding: 10px 20px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line-strong);
  background: linear-gradient(180deg, #2a2140, #1d1731);
  color: var(--text);
  font-family: var(--font-body);
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.danger { background: linear-gradient(180deg, #7f1d1d, #5f1414); border-color: rgba(229, 72, 77, 0.4); }
.btn-ember { background: linear-gradient(180deg, #c05621, #8a3c0f); border-color: rgba(255, 179, 119, 0.27); text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); }
.btn-ember:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(255, 140, 66, 0.25); }

/* ================= toasts ================= */
#toasts {
  position: fixed;
  left: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 60;
  max-width: min(360px, calc(100vw - 32px));
}
.toast {
  padding: 12px 16px;
  border-radius: var(--r-md);
  background: linear-gradient(180deg, var(--panel-3), var(--panel));
  border: 1px solid var(--line-strong);
  border-left: 3px solid var(--gold);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.35s, transform 0.35s;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.85rem;
}
.toast.show { opacity: 1; transform: none; }
.toast b { font-family: var(--font-display); font-weight: 500; font-size: 1rem; letter-spacing: 0.02em; color: var(--gold-bright); }
.toast.warn { border-left-color: var(--crimson); }
.toast.good { border-left-color: var(--green); }
.toast.deed { border-left-color: var(--ember); }

/* ================= modal ================= */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5, 4, 10, 0.7);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  z-index: 70;
  padding: 20px;
}
.modal-card {
  width: min(440px, 100%);
  background: linear-gradient(180deg, var(--panel-3), var(--panel));
  border: 1px solid var(--line-strong);
  border-radius: var(--r-lg);
  padding: 26px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-card h3 { font-family: var(--font-display); font-weight: 500; font-size: 1.5rem; letter-spacing: 0.02em; color: var(--gold-bright); }
.modal-card p { color: var(--text-dim); font-size: 0.95rem; }
.modal-card b { color: var(--ember); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

/* ================= tutorial ================= */
.tut {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  flex-wrap: wrap;
  padding: 14px 16px;
  border: 1px solid var(--line-strong);
  border-left: 3px solid var(--gold);
  border-radius: var(--r-md);
  background: linear-gradient(180deg, var(--panel-3), var(--panel));
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.3s, transform 0.3s;
}
.tut.show { opacity: 1; transform: none; }
.tut-quill { font-size: 1.5rem; line-height: 1.2; color: var(--gold-bright); }
.tut-body { flex: 1; min-width: 200px; }
.tut-title { font-family: var(--font-display); font-weight: 500; font-size: 1.15rem; color: var(--gold-bright); }
.tut-text { color: var(--text-dim); font-size: 0.9rem; margin-top: 2px; }
.tut-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; margin-left: auto; }
.tut-skip {
  background: none;
  border: none;
  color: var(--text-faint);
  font-family: var(--font-body);
  font-size: 0.75rem;
  cursor: pointer;
  text-decoration: underline;
  padding: 4px;
}
.tut-skip:hover { color: var(--text-dim); }

.tut-glow {
  animation: tutPulse 1.6s infinite;
  border-radius: var(--r-md);
}
@keyframes tutPulse {
  0%, 100% { box-shadow: 0 0 0 2px var(--gold), 0 0 12px rgba(212, 175, 55, 0.35); }
  50% { box-shadow: 0 0 0 3px var(--gold-bright), 0 0 30px rgba(212, 175, 55, 0.7); }
}

/* ================= footer ================= */
.foot { color: var(--text-faint); font-size: 0.75rem; text-align: center; padding: 8px; }

/* ================= responsive ================= */
@media (min-width: 921px) {
  .panel { position: sticky; top: 16px; max-height: calc(100vh - 90px); }
}

@media (max-width: 920px) {
  .layout { grid-template-columns: 1fr; }
  .canvas-wrap { height: clamp(260px, 42vh, 420px); }
  .stage-stats { grid-template-columns: 1fr 1fr; }
  .stat.combo { grid-column: 1 / -1; }
  .panel { max-height: none; }
}

@media (max-width: 480px) {
  #app { padding: 10px; }
  .topbar { gap: 8px; }
  .chip { padding: 6px 10px; }
  .hero-flavor { display: none; }
  .buy-btn { min-width: 76px; }
  .stage-actions { width: 100%; justify-content: flex-start; }
}

@media (min-width: 1800px) {
  html { font-size: 18px; }
  #app { max-width: 1760px; }
  .canvas-wrap { height: clamp(420px, 52vh, 720px); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}