:root {
  --bg-void: #0b0a16;
  --bg-panel: #141225;
  --bg-card: #1d1933;
  --bg-card-hi: #272244;
  --border-arcane: rgba(245, 198, 105, 0.22);
  --border-bright: rgba(245, 198, 105, 0.48);
  --text-main: #f4f1ea;
  --text-muted: #aba4c2;
  --gold: #f59e0b;
  --gold-soft: #fde68a;
  --emerald: #34d399;
  --cyan: #38bdf8;
  --violet: #c084fc;
  --danger: #fb7185;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-void);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Shared Portal Masthead */
.masthead {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 14px;
  background: rgba(13, 11, 26, 0.96);
  border-bottom: 1px solid var(--border-arcane);
  z-index: 20;
}

.masthead h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: var(--gold-soft);
  white-space: nowrap;
}

.masthead nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.masthead button {
  appearance: none;
  border: 1px solid var(--border-arcane);
  background: var(--bg-card);
  color: var(--text-main);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  cursor: pointer;
}

.masthead button[aria-pressed="true"] {
  background: rgba(245, 158, 11, 0.22);
  border-color: var(--gold);
  color: var(--gold-soft);
}

/* Shell: Portrait Mobile (Stacked 47% 3D / 53% Drawer) */
.shell {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  width: 100%;
  overflow: hidden;
}

.stage-wrap {
  flex: 0 0 46%;
  min-height: 220px;
  min-width: 0;
  width: 100%;
  max-width: 100%;
  position: relative;
  overflow: hidden;
  background: radial-gradient(circle at 50% 45%, #1c1633 0%, #0b0a16 78%);
  border-bottom: 1px solid var(--border-arcane);
  touch-action: none;
}

#sanctum-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100% !important;
  height: 100% !important;
  cursor: pointer;
  touch-action: none;
}

.webgl-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 20px;
  text-align: center;
  background: radial-gradient(circle at 50% 45%, #231942, #0b0a16);
}

.fallback-core-btn {
  width: 112px;
  height: 112px;
  border-radius: 50%;
  border: 2px solid var(--gold);
  background: radial-gradient(circle, #34d399, #065f46);
  font-size: 44px;
  cursor: pointer;
  box-shadow: 0 0 32px rgba(52, 211, 153, 0.45);
}

/* Floating Stage HUD */
.stage-hud-top {
  position: absolute;
  top: 8px;
  left: 10px;
  right: 10px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}

.essence-readout {
  background: rgba(12, 10, 24, 0.72);
  border: 1px solid var(--border-arcane);
  backdrop-filter: blur(6px);
  border-radius: 13px;
  padding: 6px 12px;
}

.essence-label {
  display: block;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--emerald);
}

.essence-val {
  display: block;
  font-size: 22px;
  line-height: 1.15;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--gold-soft);
  text-shadow: 0 2px 12px rgba(245, 158, 11, 0.32);
}

.rate-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}

.rate-pill {
  font-size: 11px;
  font-weight: 700;
  color: var(--emerald);
  font-variant-numeric: tabular-nums;
}

.rate-click {
  color: var(--cyan);
}

.meta-pills {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.meta-pill {
  background: rgba(12, 10, 24, 0.76);
  border: 1px solid var(--border-arcane);
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 11px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--cyan);
}

#pill-stones {
  color: var(--gold-soft);
  border-color: rgba(245, 158, 11, 0.4);
}

.meta-seals {
  color: #f0abfc;
  border-color: rgba(232, 121, 249, 0.45);
}

.buff-strip {
  position: absolute;
  top: 80px;
  left: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  pointer-events: none;
}

.buff-chip {
  background: linear-gradient(90deg, rgba(245, 158, 11, 0.9), rgba(217, 119, 6, 0.9));
  color: #1a1004;
  font-size: 11px;
  font-weight: 800;
  padding: 3px 9px;
  border-radius: 999px;
  box-shadow: 0 0 14px rgba(245, 158, 11, 0.55);
}

.buff-chip.is-frenzy {
  background: linear-gradient(90deg, #fde047, #f43f5e);
  color: #19050b;
}

.float-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.float-num {
  position: absolute;
  font-size: 15px;
  font-weight: 900;
  color: #6ee7b7;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.85);
  pointer-events: none;
  transform: translate(-50%, -50%);
  animation: floatUp 0.78s ease-out forwards;
}

.float-num.is-crit {
  font-size: 20px;
  color: #fde047;
  text-shadow: 0 0 14px rgba(250, 204, 21, 0.8);
}

@keyframes floatUp {
  0% { opacity: 1; transform: translate(-50%, -50%) scale(0.9); }
  20% { opacity: 1; transform: translate(-50%, -85%) scale(1.12); }
  100% { opacity: 0; transform: translate(-50%, -220%) scale(0.95); }
}

.stage-hud-bottom {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 7px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}

.core-badge {
  background: rgba(12, 10, 24, 0.74);
  border: 1px solid var(--border-arcane);
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 10px;
  font-weight: 700;
  color: var(--gold-soft);
}

.drag-tip {
  font-size: 10px;
  color: rgba(212, 206, 234, 0.78);
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9);
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 34px;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(24, 18, 44, 0.94);
  border: 1px solid var(--gold);
  color: var(--gold-soft);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.6);
  pointer-events: none;
}

/* Workshop / Control Panel */
.panel-wrap {
  flex: 1 1 54%;
  min-height: 0;
  min-width: 0;
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
  overflow: hidden;
}

.tab-bar {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 4px;
  padding: 6px 8px;
  background: #100e1f;
  border-bottom: 1px solid var(--border-arcane);
}

.tab-btn {
  appearance: none;
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 7px 3px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-btn.is-active {
  background: var(--bg-card-hi);
  border-color: var(--border-bright);
  color: var(--gold-soft);
}

.tab-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 15px;
  height: 15px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--gold);
  color: #1c1205;
  font-size: 9px;
  font-weight: 900;
}

.buy-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(20, 18, 37, 0.96);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.buy-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
}

.buy-pills {
  display: flex;
  gap: 4px;
}

.buy-pill {
  appearance: none;
  border: 1px solid var(--border-arcane);
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 800;
  padding: 4px 8px;
  border-radius: 8px;
  cursor: pointer;
}

.buy-pill.is-active {
  background: rgba(245, 158, 11, 0.22);
  border-color: var(--gold);
  color: var(--gold-soft);
}

.elixir-mini {
  font-size: 11px;
  font-weight: 700;
  color: var(--emerald);
  white-space: nowrap;
}

/* Native Scrollable Panel Body */
.panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  padding: 8px 10px 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.panel-body * {
  touch-action: pan-y;
}

/* Construct & Upgrade Cards */
.construct-card {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 10px;
  border-radius: 13px;
  border: 1px solid var(--border-arcane);
  background: linear-gradient(180deg, #1f1b38, #17142a);
  transition: border-color 0.14s ease, opacity 0.14s ease;
}

.construct-card.is-affordable {
  border-color: rgba(52, 211, 153, 0.55);
  background: linear-gradient(180deg, #212540, #171830);
}

.construct-card.is-dim {
  opacity: 0.68;
}

.c-icon {
  flex: 0 0 38px;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.c-info {
  flex: 1 1 auto;
  min-width: 0;
}

.c-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px;
}

.c-name {
  font-size: 13px;
  font-weight: 800;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.c-count {
  font-size: 13px;
  font-weight: 900;
  color: var(--gold-soft);
  font-variant-numeric: tabular-nums;
}

.c-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.c-eps {
  color: var(--emerald);
  font-weight: 700;
}

.c-actions {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 0 0 auto;
  align-items: flex-end;
}

.buy-btn,
.dew-btn,
.primary-btn,
.secondary-btn {
  appearance: none;
  border-radius: 9px;
  font-weight: 800;
  cursor: pointer;
  border: 1px solid transparent;
  transition: transform 0.08s ease;
}

.buy-btn:active,
.dew-btn:active,
.primary-btn:active,
.secondary-btn:active {
  transform: scale(0.96);
}

.buy-btn {
  min-width: 82px;
  padding: 6px 9px;
  font-size: 11px;
  background: linear-gradient(180deg, #10b981, #059669);
  color: #041d15;
  font-variant-numeric: tabular-nums;
}

.buy-btn:disabled {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-muted);
  border-color: rgba(255, 255, 255, 0.08);
  cursor: not-allowed;
}

.dew-btn {
  padding: 3px 7px;
  font-size: 10px;
  background: rgba(56, 189, 248, 0.16);
  border-color: rgba(56, 189, 248, 0.45);
  color: #bae6fd;
}

.dew-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* Section Boxes (Daily, Brew, Prestige, Archive) */
.section-box {
  background: var(--bg-card);
  border: 1px solid var(--border-arcane);
  border-radius: 14px;
  padding: 11px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-box h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 800;
  color: var(--gold-soft);
}

.section-sub {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.48;
  color: var(--text-muted);
}

.progress-track {
  width: 100%;
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--emerald), var(--cyan));
  border-radius: 999px;
}

.row-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.row-item-text {
  flex: 1 1 auto;
  min-width: 0;
}

.row-item-title {
  font-size: 12px;
  font-weight: 800;
  color: var(--text-main);
}

.row-item-desc {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
}

.primary-btn {
  padding: 8px 14px;
  font-size: 12px;
  background: linear-gradient(180deg, #f59e0b, #d97706);
  color: #1c1004;
}

.primary-btn:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.secondary-btn {
  padding: 7px 12px;
  font-size: 12px;
  background: var(--bg-card-hi);
  border-color: var(--border-arcane);
  color: var(--text-main);
}

.recipe-grid,
.rune-grid,
.ach-grid,
.arcana-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.mini-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 9px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.24);
  border: 1px solid rgba(255, 255, 255, 0.08);
  text-align: left;
  color: var(--text-main);
}

.mini-card.is-owned,
.mini-card.is-equipped {
  border-color: var(--emerald);
  background: rgba(16, 185, 129, 0.12);
}

.mini-card.is-epoch {
  border-color: rgba(232, 121, 249, 0.45);
}

.mini-title {
  font-size: 11.5px;
  font-weight: 800;
  color: var(--gold-soft);
}

.mini-desc {
  font-size: 10.5px;
  line-height: 1.38;
  color: var(--text-muted);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.stat-cell {
  background: rgba(0, 0, 0, 0.24);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  padding: 7px 9px;
}

.stat-cell span {
  display: block;
  font-size: 10px;
  color: var(--text-muted);
}

.stat-cell strong {
  font-size: 13px;
  color: var(--gold-soft);
}

.ach-chip {
  padding: 6px 8px;
  border-radius: 9px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(0, 0, 0, 0.22);
  color: var(--text-muted);
}

.ach-chip.is-unlocked {
  border-color: rgba(245, 158, 11, 0.5);
  background: rgba(245, 158, 11, 0.14);
  color: var(--gold-soft);
}

.save-textarea {
  width: 100%;
  min-height: 74px;
  border-radius: 10px;
  border: 1px solid var(--border-arcane);
  background: #0d0b1a;
  color: var(--text-main);
  padding: 8px;
  font-size: 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  user-select: text;
  -webkit-user-select: text;
}

/* Desktop Side-by-Side Layout */
@media (min-width: 900px) {
  .shell {
    flex-direction: row;
  }

  .stage-wrap {
    flex: 1 1 58%;
    height: 100%;
    border-bottom: none;
    border-right: 1px solid var(--border-arcane);
  }

  .panel-wrap {
    flex: 0 0 430px;
    max-width: 44%;
    height: 100%;
  }

  .essence-val {
    font-size: 28px;
  }
}

/* Modals */
.overlay-root {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 7, 18, 0.8);
  backdrop-filter: blur(6px);
}

.overlay-card {
  width: 100%;
  max-width: 520px;
  max-height: min(84vh, 660px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  background: linear-gradient(180deg, #1f1a38, #141126);
  border: 1px solid var(--border-bright);
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.7);
}

.overlay-card * {
  touch-action: pan-y;
}

.overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.overlay-card h2 {
  margin: 0;
  font-size: 19px;
  color: var(--gold-soft);
}

.close-x {
  appearance: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border-arcane);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-main);
  cursor: pointer;
}

.guide-list {
  margin: 10px 0;
  padding-left: 18px;
  font-size: 13px;
  line-height: 1.55;
}

.guide-list li {
  margin-bottom: 6px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

.modal-actions > button {
  flex: 1 1 0;
}
