/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ---- Theme variables (mirrors ShiroNexus portal) ---- */
[data-theme="dark"] {
  --bg:          #0c0c15;
  --surface:     #13131f;
  --surface-hov: #18182a;
  --inset:       rgba(255, 255, 255, 0.04);
  --border:      rgba(255, 255, 255, 0.07);
  --border-hov:  rgba(255, 255, 255, 0.14);
  --text:        #eeeef5;
  --muted:       #7878a0;
  --accent:      #a78bfa;
  --shadow:      rgba(0, 0, 0, 0.5);
}

[data-theme="light"] {
  --bg:          #f0ece8;
  --surface:     #ffffff;
  --surface-hov: #f7f5ff;
  --inset:       rgba(0, 0, 0, 0.03);
  --border:      rgba(0, 0, 0, 0.08);
  --border-hov:  rgba(0, 0, 0, 0.18);
  --text:        #1b1020;
  --muted:       #706080;
  --accent:      #7c3aed;
  --shadow:      rgba(0, 0, 0, 0.12);
}

:root {
  --mana:   #38bdf8;
  --gold:   #f59e0b;
  --insight:#34d399;
  --essence:#c084fc;
  --green:  #22c55e;
  --header-h: 54px;
}

/* ---- Base ---- */
html { font-family: 'Cabin', -apple-system, sans-serif; font-size: 15px; }

body {
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background 0.25s, color 0.25s;
}

/* ---- Header ---- */
.header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
  gap: 1rem;
  flex-shrink: 0;
}

.header-left  { display: flex; align-items: center; gap: 0.875rem; }
.header-right { display: flex; align-items: center; gap: 0.45rem; flex-wrap: wrap; justify-content: flex-end; }

.back-btn {
  font-size: 0.78rem;
  color: var(--muted);
  text-decoration: none;
  padding: 0.28rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  transition: color 0.2s, border-color 0.2s;
  white-space: nowrap;
}
.back-btn:hover { color: var(--text); border-color: var(--border-hov); }

.game-title { font-size: 1.05rem; font-weight: 700; letter-spacing: -0.02em; }

.hbadge {
  font-size: 0.82rem;
  font-weight: 700;
  padding: 0.26rem 0.7rem;
  border-radius: 99px;
  white-space: nowrap;
  display: inline-flex;
  gap: 0.1rem;
}
.mana-badge    { color: var(--mana);    background: rgba(56,189,248,0.1);  border: 1px solid rgba(56,189,248,0.25); }
.gold-badge    { color: var(--gold);    background: rgba(245,158,11,0.1);  border: 1px solid rgba(245,158,11,0.25); }
.insight-badge { color: var(--insight); background: rgba(52,211,153,0.1);  border: 1px solid rgba(52,211,153,0.25); }
.essence-badge { color: var(--essence); background: rgba(192,132,252,0.1); border: 1px solid rgba(192,132,252,0.25); }

.theme-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  width: 34px; height: 34px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.95rem;
  display: flex; align-items: center; justify-content: center;
  transition: border-color 0.2s, transform 0.2s;
  flex-shrink: 0;
}
.theme-btn:hover { border-color: var(--border-hov); transform: rotate(18deg); }

/* ---- Layout ---- */
.game-layout {
  display: grid;
  grid-template-columns: 270px 1fr 320px;
  grid-template-rows: minmax(0, 1fr);
  gap: 0.875rem;
  padding: 0.875rem;
  height: calc(100vh - var(--header-h));
  min-height: 0;
  max-width: 1360px;
  margin: 0 auto;
  width: 100%;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  overflow-y: auto;
  min-height: 0;
}

.panel-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.divider { height: 1px; background: var(--border); }

/* ---- Bars ---- */
.bar-track { height: 7px; background: var(--inset); border-radius: 99px; overflow: hidden; }
.bar-track.big-bar { height: 16px; }
.bar-fill { height: 100%; border-radius: 99px; transition: width 0.1s linear; }
.mana-fill { background: linear-gradient(90deg, #0ea5e9, #38bdf8); }

/* ════════ GRIMOIRE PANEL ════════ */
.mana-wrap { display: flex; flex-direction: column; gap: 0.3rem; }
.mana-regen { font-size: 0.66rem; color: var(--muted); text-align: right; }

.spell-list { display: flex; flex-direction: column; gap: 0.45rem; max-height: 16rem; overflow-y: auto; padding-right: 0.15rem; flex-shrink: 0; }

.spell-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--inset);
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  width: 100%;
  transition: border-color 0.12s, background 0.12s, transform 0.08s;
}
.spell-row:hover { border-color: var(--accent); background: var(--surface-hov); }
.spell-row:active { transform: scale(0.985); }
.spell-row.cant { opacity: 0.5; }
.spell-icon { font-size: 1.5rem; line-height: 1; flex-shrink: 0; width: 1.7rem; text-align: center; }
.spell-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.1rem; }
.spell-name { font-size: 0.82rem; font-weight: 700; }
.spell-desc { font-size: 0.64rem; color: var(--muted); line-height: 1.3; }
.spell-cost { flex-shrink: 0; font-size: 0.72rem; font-weight: 700; color: var(--mana); white-space: nowrap; }
.spell-locked-hint { font-size: 0.68rem; color: var(--muted); font-style: italic; padding: 0.2rem 0.25rem; opacity: 0.7; }

/* ── Hover tooltip (body-level, matches the site's surface/accent) ── */
.tip {
  position: fixed;
  z-index: 400;
  max-width: 230px;
  padding: 0.5rem 0.65rem;
  background: var(--surface);
  border: 1px solid rgba(167, 139, 250, 0.35);
  border-radius: 10px;
  box-shadow: 0 12px 32px var(--shadow), 0 0 0 1px rgba(167, 139, 250, 0.06);
  font-family: inherit;
  font-size: 0.72rem;
  line-height: 1.45;
  color: var(--text);
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.13s ease, transform 0.13s ease;
}
.tip.show { opacity: 1; transform: translateY(0); }
.tip-title  { font-weight: 700; color: var(--accent); margin-bottom: 0.1rem; }
.tip-cost   { color: var(--mana); font-weight: 700; font-size: 0.66rem; }
.tip-yield  { margin-top: 0.15rem; }
.tip-g { color: var(--gold); }
.tip-i { color: var(--insight); }
.tip-e { color: var(--essence); }
.tip-flavor { color: var(--muted); font-style: italic; font-size: 0.66rem; margin-top: 0.3rem; }

/* ── Automation ── */
.automation-hdr { display: flex; align-items: baseline; justify-content: space-between; }
.automation-interval { font-size: 0.66rem; color: var(--muted); }
.automation { display: flex; flex-direction: column; gap: 0.4rem; }
.auto-slot { display: flex; gap: 0.4rem; }
.auto-select {
  flex: 1; min-width: 0;
  font-family: inherit; font-size: 0.72rem;
  padding: 0.35rem 0.4rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--inset); color: var(--text);
  cursor: pointer;
}
.auto-toggle {
  flex-shrink: 0; width: 3.1rem;
  font-family: inherit; font-size: 0.66rem; font-weight: 700;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--inset); color: var(--muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.auto-toggle.on { color: var(--green); border-color: rgba(34,197,94,0.45); background: rgba(34,197,94,0.12); }

.arcane-box {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  border: 1px solid rgba(192,132,252,0.3);
  background: rgba(192,132,252,0.08);
  border-radius: 10px;
  padding: 0.55rem 0.75rem;
}
.arcane-ico { font-size: 1.35rem; }
.arcane-val   { display: block; font-size: 0.9rem; font-weight: 700; color: var(--essence); }
.arcane-bonus { display: block; font-size: 0.66rem; color: var(--muted); }

.transcend-btn {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  padding: 0.55rem;
  border: 1px solid rgba(192,132,252,0.4);
  border-radius: 10px;
  background: linear-gradient(135deg, rgba(192,132,252,0.18), rgba(124,58,237,0.12));
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  text-align: center;
  transition: transform 0.12s, box-shadow 0.2s, opacity 0.2s;
}
.transcend-btn:not(:disabled):hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(124,58,237,0.3); }
.transcend-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.transcend-top { font-size: 0.85rem; font-weight: 700; }
.transcend-sub { font-size: 0.64rem; color: var(--muted); }

.info-rows { display: flex; flex-direction: column; gap: 0.35rem; }
.info-row { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--muted); }
.info-row span:last-child { color: var(--text); font-weight: 600; }

.reset-row { margin-top: auto; display: flex; gap: 0.5rem; flex-wrap: wrap; }
.reset-btn {
  flex: 1 1 0;
  padding: 0.38rem 0.6rem;
  font-size: 0.7rem;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s;
  font-family: inherit;
  text-align: center;
  white-space: nowrap;
}
.reset-run-btn:hover { color: var(--accent); border-color: rgba(167,139,250,0.5); }
.reset-all-btn:hover { color: #ef4444;        border-color: rgba(239,68,68,0.5); }

/* ════════ STUDY PANEL ════════ */
.study-panel {
  background:
    radial-gradient(circle at 50% 22%, rgba(167,139,250,0.1) 0%, transparent 55%),
    var(--surface);
}

.orb-stage {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 1.4rem 0 1rem;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  flex-shrink: 0;
}
.orb-float-layer { position: absolute; inset: 0; pointer-events: none; overflow: visible; z-index: 3; }

.orb-core {
  font-size: 5rem;
  line-height: 1;
  filter: drop-shadow(0 0 28px rgba(167,139,250,0.55));
  transition: transform 0.09s ease;
  animation: orb-bob 4s ease-in-out infinite;
}
.orb-stage.pulse .orb-core { transform: scale(0.9); }
@keyframes orb-bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-7px); } }

.orb-hint { font-size: 0.7rem; color: var(--muted); opacity: 0.7; animation: hint-pulse 2.4s ease-in-out infinite; }
@keyframes hint-pulse { 0%,100% { opacity: 0.35; } 50% { opacity: 0.8; } }

.study-section { display: flex; flex-direction: column; gap: 0.5rem; }
.study-hdr { display: flex; align-items: center; justify-content: space-between; }
.slot-count { font-size: 0.7rem; font-weight: 700; color: var(--accent); }

.research-slots { display: flex; flex-direction: column; gap: 0.5rem; }
.slot {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--inset);
  padding: 0.55rem 0.7rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.slot-top { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.slot-name { font-size: 0.78rem; font-weight: 700; }
.slot-eta { font-size: 0.7rem; font-weight: 700; color: var(--accent); flex-shrink: 0; }
.slot-track { height: 8px; background: var(--inset); border: 1px solid var(--border); border-radius: 99px; overflow: hidden; }
.slot-fill { height: 100%; width: 0%; background: linear-gradient(90deg, #7c3aed, #a78bfa); transition: width 0.15s linear; }
.slot-empty { font-size: 0.74rem; color: var(--muted); font-style: italic; padding: 0.5rem 0.2rem; }

.log-wrap { flex: 1; min-height: 0; }
.event-log {
  flex: 1;
  min-height: 120px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--inset);
  padding: 0.6rem 0.7rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.log-line { font-size: 0.74rem; line-height: 1.4; color: var(--muted); padding-bottom: 0.4rem; border-bottom: 1px solid var(--border); }
.log-line:last-child { border-bottom: none; padding-bottom: 0; }
.log-line b { color: var(--text); font-weight: 700; }
.log-discovery { color: var(--text); }
.log-discovery b { color: var(--accent); }
.log-start b { color: var(--mana); }
.log-transcend { color: var(--essence); }
.log-transcend b { color: var(--essence); }

/* ════════ RESEARCH PANEL ════════ */
.research-panel { overflow: hidden; }
.research-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow-y: auto;
  /* Explicit, self-contained height bound (Pokébrowse-style) so the list
     always scrolls inside its column, independent of grid/flex context. */
  max-height: calc(100vh - var(--header-h) - 6rem);
  padding-right: 0.15rem;
}

.chapter-hdr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-top: 0.5rem;
  padding: 0.25rem 0.1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: color 0.15s;
}
.chapter-hdr:hover { color: var(--text); }
.chapter-hdr:first-child { margin-top: 0; }
.chapter-title { display: flex; align-items: center; gap: 0.3rem; }
.chapter-chevron { display: inline-block; width: 0.7rem; font-size: 0.7rem; text-align: center; }
.chapter-prog { color: var(--muted); }

.res-node {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--inset);
}
.res-node.locked { opacity: 0.5; }
.res-node.done   { opacity: 0.62; }
.res-node.active { border-color: rgba(167,139,250,0.45); background: rgba(167,139,250,0.07); }

.res-ico { font-size: 1.3rem; line-height: 1; flex-shrink: 0; width: 1.6rem; text-align: center; }
.res-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.18rem; }
.res-name { font-size: 0.8rem; font-weight: 700; }
.res-check { color: var(--green); }
.res-desc { font-size: 0.66rem; color: var(--muted); }
.res-desc.req { color: #f59e0b; opacity: 0.85; }

.res-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.3rem; margin-top: 0.15rem; }
.chip {
  font-size: 0.62rem;
  font-weight: 700;
  padding: 0.1rem 0.4rem;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--surface);
  white-space: nowrap;
}
.chip.short { color: #ef4444; border-color: rgba(239,68,68,0.4); }
.res-time { font-size: 0.62rem; color: var(--muted); }

.begin-btn {
  flex-shrink: 0;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
}
.begin-btn:not(:disabled):hover { background: var(--accent); color: #fff; transform: translateY(-1px); }
.begin-btn:disabled { opacity: 0.4; cursor: not-allowed; border-color: var(--border); }

/* ════════ FLOATING NUMBERS ════════ */
.float-num {
  position: absolute;
  font-weight: 700;
  font-size: 0.92rem;
  color: #fde68a;
  text-shadow: 0 2px 6px rgba(0,0,0,0.6);
  pointer-events: none;
  white-space: nowrap;
  transform: translate(-50%, 0);
  animation: floatUp 0.85s ease-out forwards;
}
.float-num.tap { color: var(--mana); }
@keyframes floatUp {
  0%   { opacity: 0; transform: translate(-50%, 0) scale(0.7); }
  15%  { opacity: 1; transform: translate(-50%, -10px) scale(1.08); }
  100% { opacity: 0; transform: translate(-50%, -60px) scale(1); }
}

/* ════════ TOAST ════════ */
.toast-container {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 200;
  align-items: center;
  pointer-events: none;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border-hov);
  color: var(--text);
  padding: 0.6rem 1.1rem;
  border-radius: 99px;
  font-size: 0.82rem;
  font-weight: 600;
  box-shadow: 0 10px 30px var(--shadow);
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
}
@keyframes toastIn  { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(14px); } }

/* ════════ ATTUNEMENT MODAL ════════ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  padding: 1rem;
}

.modal-box {
  background: var(--surface);
  border: 1px solid rgba(192,132,252,0.4);
  border-radius: 20px;
  padding: 2rem 2.25rem;
  max-width: 380px;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  box-shadow: 0 28px 64px rgba(0,0,0,0.65), 0 0 0 1px rgba(192,132,252,0.1);
  animation: modalIn 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.86) translateY(12px); }
  to   { opacity: 1; transform: scale(1)    translateY(0);    }
}

.modal-glow  { font-size: 2.4rem; line-height: 1; }
.modal-title { font-size: 1.4rem; font-weight: 700; letter-spacing: -0.02em; margin-top: -0.3rem; }

.modal-souls-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.9rem 1rem;
  border-radius: 14px;
  border: 1px solid rgba(192,132,252,0.3);
  background: rgba(192,132,252,0.08);
}
.modal-big   { font-size: 2.8rem; font-weight: 700; color: var(--essence); line-height: 1; }
.modal-label { font-size: 0.72rem; color: var(--muted); letter-spacing: 0.06em; text-transform: uppercase; }

.modal-gain {
  font-size: 1rem;
  font-weight: 700;
  color: var(--green);
  background: rgba(34,197,94,0.1);
  border-radius: 8px;
  padding: 0.45rem 0.75rem;
}

.modal-lines { display: flex; flex-direction: column; gap: 0.35rem; text-align: left; }
.modal-line { font-size: 0.75rem; padding: 0.3rem 0.6rem; border-radius: 7px; }
.modal-warn { background: rgba(239,68,68,0.08); color: #f87171; }
.modal-keep { background: rgba(163,230,53,0.07); color: #a3e635; }

.modal-tip { font-size: 0.7rem; color: var(--muted); font-style: italic; margin-top: -0.2rem; }

.modal-actions { display: flex; gap: 0.65rem; margin-top: 0.25rem; }

.modal-cancel {
  flex: 1;
  padding: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--inset);
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  transition: border-color 0.15s, color 0.15s;
}
.modal-cancel:hover { border-color: var(--border-hov); color: var(--text); }

.modal-confirm {
  flex: 1.6;
  padding: 0.6rem;
  border: 1px solid rgba(192,132,252,0.55);
  border-radius: 10px;
  background: linear-gradient(135deg, rgba(192,132,252,0.28), rgba(124,58,237,0.2));
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  transition: transform 0.12s, box-shadow 0.2s;
}
.modal-confirm:hover { transform: translateY(-2px); box-shadow: 0 10px 28px rgba(124,58,237,0.4); }

.confirm-body { font-size: 0.85rem; color: var(--text); line-height: 1.5; margin: 0; }
.confirm-danger {
  border-color: rgba(239,68,68,0.55);
  background: linear-gradient(135deg, rgba(239,68,68,0.28), rgba(185,28,28,0.2));
}
.confirm-danger:hover { box-shadow: 0 10px 28px rgba(239,68,68,0.4); }

/* ════════ RESPONSIVE ════════ */
@media (max-width: 880px) {
  .game-layout { grid-template-columns: 1fr; grid-template-rows: none; height: auto; overflow: visible; }
  .panel { min-height: unset; }
  .study-panel { order: -1; }
  .event-log { min-height: 160px; max-height: 260px; }
  .research-panel { overflow: visible; }
  .research-list { flex: none; max-height: 60vh; }
}
@media (max-width: 520px) {
  .back-btn { display: none; }
  /* Show every resource badge on mobile; let the header grow to a 2nd row if needed. */
  .header { height: auto; min-height: var(--header-h); padding: 0.3rem 0.9rem; }
  .hbadge { font-size: 0.74rem; padding: 0.2rem 0.5rem; }
  .game-layout { padding: 0.6rem; gap: 0.6rem; }
  .orb-core { font-size: 4rem; }
}
