:root {
  --bg: #070a14;
  --bg-panel: #0f1426;
  --bg-card: #141a30;
  --bg-card-2: #1a2140;
  --line: rgba(255, 255, 255, 0.07);
  --text: #f2f4ff;
  --text-dim: #9aa3c7;
  --text-faint: #5f6a94;
  --amber: #ffb84d;
  --amber-2: #ff9a3c;
  --cyan: #5ee7df;
  --violet: #b48cff;
  --red: #ff6a3d;
  --green: #6ee7a2;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --tap-min: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); overscroll-behavior: none; }
body { position: fixed; inset: 0; overflow: hidden; -webkit-user-select: none; user-select: none; touch-action: manipulation; }
button { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }

#app {
  height: 100dvh;
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(180px, 1fr) minmax(200px, 38%) auto;
  grid-template-areas: "hud" "stage" "panel" "tabs";
  padding-top: var(--safe-top);
}
#app > * { min-width: 0; }

/* ---------- HUD ---------- */
#hud { grid-area: hud; padding: 8px 14px 6px; background: linear-gradient(180deg, rgba(7, 10, 20, 0.95), rgba(7, 10, 20, 0.6)); position: relative; z-index: 3; }
.hud-top { display: flex; align-items: center; gap: 10px; min-height: 30px; }
.hud-form { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.hud-form-name { font-weight: 800; letter-spacing: 0.02em; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-form-flavor { font-size: 11px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-dm { display: inline-flex; align-items: center; gap: 5px; padding: 4px 10px; border-radius: 999px; background: rgba(180, 140, 255, 0.14); border: 1px solid rgba(180, 140, 255, 0.35); color: var(--violet); font-weight: 700; font-size: 13px; cursor: pointer; }
.dm-icon { width: 10px; height: 10px; background: var(--violet); transform: rotate(45deg); border-radius: 2px; box-shadow: 0 0 8px var(--violet); }
.icon-btn { width: 36px; height: 36px; border-radius: 12px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.05); display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }
.icon-btn svg { width: 20px; height: 20px; }
.hud-main { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-top: 4px; flex-wrap: wrap; }
.hud-dust { display: flex; align-items: baseline; gap: 8px; }
#hud-dust { font-size: 30px; font-weight: 800; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; color: #fff; text-shadow: 0 0 18px rgba(255, 184, 77, 0.35); }
.hud-label { font-size: 12px; color: var(--amber); font-weight: 700; text-transform: uppercase; letter-spacing: 0.12em; }
.hud-sub { display: flex; flex-direction: column; align-items: flex-end; font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
#hud-rate { font-weight: 700; color: var(--text); }
#hud-compare { color: var(--cyan); }
.hud-buffs { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; min-height: 0; }
.buff { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: rgba(94, 231, 223, 0.15); color: var(--cyan); border: 1px solid rgba(94, 231, 223, 0.35); }
.buff.frenzy { background: rgba(255, 154, 60, 0.15); color: var(--amber); border-color: rgba(255, 154, 60, 0.4); }
.buff.storm { background: rgba(180, 140, 255, 0.15); color: var(--violet); border-color: rgba(180, 140, 255, 0.4); }

/* ---------- Stage ---------- */
#stage { grid-area: stage; position: relative; overflow: hidden; background: #000; touch-action: none; }
#scene { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: pointer; }
.stage-hint { position: absolute; left: 50%; bottom: 62px; transform: translateX(-50%); font-size: 13px; color: rgba(255, 255, 255, 0.75); background: rgba(0, 0, 0, 0.35); padding: 6px 12px; border-radius: 999px; pointer-events: none; animation: pulse 1.6s ease-in-out infinite; backdrop-filter: blur(4px); }
.stage-hint.hidden { display: none; }
@keyframes pulse { 0%, 100% { opacity: 0.55; } 50% { opacity: 1; } }
.toasts { position: absolute; top: 8px; left: 10px; right: 10px; display: flex; flex-direction: column; gap: 4px; pointer-events: none; z-index: 4; }
.toast { align-self: center; max-width: 100%; background: rgba(15, 20, 38, 0.9); border: 1px solid var(--line); border-left: 3px solid var(--amber); border-radius: 10px; padding: 5px 10px; font-size: 12px; line-height: 1.25; box-shadow: var(--shadow); animation: toast-in 0.25s ease-out; }
/* Milestone + achievement can arrive in the same frame. Keep the main form visible. */
.toasts:has(.toast:nth-child(2)) .toast { padding-block: 4px; }
.toasts:has(.toast:nth-child(2)) .toast-sub { display: none; }
.toast.achievement { border-left-color: var(--green); }
.toast.stray { border-left-color: var(--cyan); }
.toast.planet { border-left-color: var(--violet); }
.toast .toast-title { font-weight: 800; }
.toast .toast-sub { color: var(--text-dim); font-size: 11px; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

.evolve { position: absolute; left: 12px; right: 12px; bottom: 10px; height: 46px; border-radius: 14px; border: 1px solid rgba(255, 184, 77, 0.4); background: rgba(20, 26, 48, 0.85); color: var(--text); overflow: hidden; cursor: pointer; display: flex; align-items: center; justify-content: center; backdrop-filter: blur(6px); box-shadow: var(--shadow); z-index: 2; }
.evolve .evolve-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0%; background: linear-gradient(90deg, rgba(255, 184, 77, 0.35), rgba(255, 154, 60, 0.55)); transition: width 0.2s linear; }
.evolve .evolve-text { position: relative; display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.evolve-title { font-weight: 800; font-size: 14px; letter-spacing: 0.02em; }
.evolve-sub { font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.evolve.ready { border-color: var(--amber); animation: glow 1.4s ease-in-out infinite; }
.evolve.ready .evolve-fill { width: 100% !important; }
.evolve.ready .evolve-sub { color: #3a2400; font-weight: 700; }
.evolve.ready .evolve-title { color: #2a1800; }
.evolve.locked { opacity: 0.85; }
@keyframes glow { 0%, 100% { box-shadow: 0 0 0 rgba(255, 184, 77, 0); } 50% { box-shadow: 0 0 24px rgba(255, 184, 77, 0.55); } }

/* ---------- Panel ---------- */
#panel { grid-area: panel; overflow-y: auto; overflow-x: hidden; background: var(--bg-panel); border-top: 1px solid var(--line); -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.panel-content { padding: 10px 12px 16px; }
.section-title { font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.12em; color: var(--text-faint); margin: 10px 2px 8px; display: flex; justify-content: space-between; align-items: center; }
.section-title:first-child { margin-top: 2px; }
.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); }
.small { font-size: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }

.card { background: var(--bg-card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; display: flex; align-items: center; gap: 10px; position: relative; }
.card.locked { opacity: 0.55; }
.card-icon { width: 40px; height: 40px; border-radius: 12px; flex: none; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, rgba(255, 184, 77, 0.18), rgba(255, 154, 60, 0.05)); color: var(--amber); border: 1px solid rgba(255, 184, 77, 0.2); }
.card-icon svg { width: 22px; height: 22px; }
.card-icon.cyan { background: linear-gradient(135deg, rgba(94, 231, 223, 0.18), rgba(94, 231, 223, 0.04)); color: var(--cyan); border-color: rgba(94, 231, 223, 0.25); }
.card-icon.violet { background: linear-gradient(135deg, rgba(180, 140, 255, 0.2), rgba(180, 140, 255, 0.04)); color: var(--violet); border-color: rgba(180, 140, 255, 0.3); }
.card-icon.green { background: linear-gradient(135deg, rgba(110, 231, 162, 0.2), rgba(110, 231, 162, 0.04)); color: var(--green); border-color: rgba(110, 231, 162, 0.3); }
.card-body { flex: 1; min-width: 0; }
.card-title { font-weight: 800; font-size: 14px; display: flex; align-items: baseline; gap: 8px; }
.card-title .count { color: var(--amber); font-variant-numeric: tabular-nums; font-size: 13px; }
.card-desc { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.card-meta { font-size: 11px; color: var(--text-faint); margin-top: 3px; font-variant-numeric: tabular-nums; }
.buy { flex: none; min-width: 84px; min-height: var(--tap-min); padding: 6px 10px; border-radius: 12px; border: 1px solid rgba(255, 184, 77, 0.45); background: linear-gradient(180deg, rgba(255, 184, 77, 0.22), rgba(255, 154, 60, 0.12)); color: #ffd9a0; font-weight: 800; font-size: 12px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; cursor: pointer; font-variant-numeric: tabular-nums; transition: transform 0.08s; }
.buy .buy-cost { font-size: 12px; font-weight: 700; }
.buy .buy-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; opacity: 0.8; }
.buy:active { transform: scale(0.96); }
.buy:disabled, .buy.disabled { border-color: var(--line); background: rgba(255, 255, 255, 0.04); color: var(--text-faint); cursor: default; }
.buy.violet { border-color: rgba(180, 140, 255, 0.5); background: linear-gradient(180deg, rgba(180, 140, 255, 0.25), rgba(180, 140, 255, 0.1)); color: #e6d8ff; }
.buy.violet:disabled { border-color: var(--line); background: rgba(255, 255, 255, 0.04); color: var(--text-faint); }
.buy.cyan { border-color: rgba(94, 231, 223, 0.5); background: linear-gradient(180deg, rgba(94, 231, 223, 0.22), rgba(94, 231, 223, 0.08)); color: #d8fffb; }
.buy.cyan:disabled { border-color: var(--line); background: rgba(255, 255, 255, 0.04); color: var(--text-faint); }

.segmented { display: inline-flex; background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented button { border: 0; background: transparent; padding: 5px 10px; font-size: 12px; font-weight: 700; color: var(--text-dim); cursor: pointer; min-height: 30px; }
.segmented button.active { background: rgba(255, 184, 77, 0.2); color: var(--amber); }

.upgrade-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.upgrade { background: var(--bg-card); border: 1px solid var(--line); border-radius: 14px; padding: 10px; display: flex; flex-direction: column; gap: 6px; cursor: pointer; text-align: left; min-height: 96px; transition: transform 0.08s, border-color 0.2s; }
.upgrade:active { transform: scale(0.98); }
.upgrade.affordable { border-color: rgba(255, 184, 77, 0.55); background: linear-gradient(180deg, rgba(255, 184, 77, 0.12), var(--bg-card)); }
.upgrade .u-head { display: flex; align-items: center; gap: 8px; }
.upgrade .u-icon { width: 28px; height: 28px; border-radius: 8px; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, 0.06); color: var(--amber); flex: none; }
.upgrade .u-icon svg { width: 16px; height: 16px; }
.upgrade .u-name { font-weight: 800; font-size: 13px; line-height: 1.15; }
.upgrade .u-desc { font-size: 11px; color: var(--text-dim); flex: 1; }
.upgrade .u-cost { font-size: 12px; font-weight: 700; color: var(--amber); font-variant-numeric: tabular-nums; }
.upgrade.unaffordable .u-cost { color: var(--text-faint); }

.progress { height: 6px; border-radius: 999px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.progress > span { display: block; height: 100%; background: linear-gradient(90deg, var(--amber), var(--amber-2)); border-radius: 999px; transition: width 0.25s; }
.progress.violet > span { background: linear-gradient(90deg, var(--violet), #8b6cff); }

.big-btn { width: 100%; min-height: 54px; border-radius: 14px; border: 1px solid rgba(180, 140, 255, 0.5); background: linear-gradient(180deg, rgba(180, 140, 255, 0.3), rgba(140, 100, 255, 0.15)); color: #fff; font-weight: 800; font-size: 15px; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; box-shadow: var(--shadow); }
.big-btn .sub { font-size: 12px; color: var(--text-dim); font-weight: 600; }
.big-btn:disabled { opacity: 0.55; cursor: default; box-shadow: none; }
.big-btn.amber { border-color: rgba(255, 184, 77, 0.5); background: linear-gradient(180deg, rgba(255, 184, 77, 0.3), rgba(255, 154, 60, 0.12)); }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stat { background: var(--bg-card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.stat .k { font-size: 11px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.08em; }
.stat .v { font-size: 14px; font-weight: 800; font-variant-numeric: tabular-nums; margin-top: 2px; word-break: break-word; }

.ach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 6px; }
.ach { background: var(--bg-card); border: 1px solid var(--line); border-radius: 12px; padding: 8px; font-size: 12px; }
.ach.done { border-color: rgba(110, 231, 162, 0.5); background: linear-gradient(180deg, rgba(110, 231, 162, 0.12), var(--bg-card)); }
.ach .a-name { font-weight: 800; }
.ach .a-desc { color: var(--text-dim); font-size: 11px; margin-top: 2px; }
.ach.locked .a-name { color: var(--text-dim); }

.body-card { display: flex; align-items: center; gap: 10px; background: var(--bg-card); border: 1px solid var(--line); border-radius: 14px; padding: 8px 10px; margin-bottom: 8px; }
.body-card.absorbed { opacity: 0.7; border-color: rgba(180, 140, 255, 0.35); }
.body-card.available { border-color: rgba(180, 140, 255, 0.7); box-shadow: 0 0 18px rgba(180, 140, 255, 0.25); }
.body-orb { width: 44px; height: 44px; border-radius: 50%; flex: none; position: relative; box-shadow: inset -8px -8px 14px rgba(0, 0, 0, 0.55), 0 0 12px rgba(255, 255, 255, 0.08); }
.body-orb.ringed::after { content: ""; position: absolute; left: -12px; right: -12px; top: 50%; height: 8px; margin-top: -4px; border-radius: 50%; border: 2px solid rgba(230, 207, 154, 0.8); transform: rotate(-18deg); }
.body-orb.star { box-shadow: 0 0 22px currentColor, inset -4px -4px 10px rgba(0, 0, 0, 0.2); }
.body-orb.hole { background: radial-gradient(circle, #000 42%, #c9a0ff 48%, #6b3fd6 60%, transparent 62%) !important; box-shadow: 0 0 18px rgba(201, 160, 255, 0.6); }
.body-orb.galaxy, .body-orb.cluster, .body-orb.universe { background: radial-gradient(circle, #fff 0 12%, rgba(240, 230, 255, 0.7) 30%, rgba(180, 140, 255, 0.25) 60%, transparent 72%) !important; box-shadow: 0 0 20px rgba(200, 180, 255, 0.5); }
.body-orb.absorbed-orb { filter: grayscale(0.7) brightness(0.6); }
.body-name { font-weight: 800; font-size: 14px; }
.body-mass { font-size: 11px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.body-bonus { font-size: 11px; color: var(--violet); margin-top: 2px; }

.tag { display: inline-block; font-size: 10px; font-weight: 800; padding: 2px 6px; border-radius: 6px; background: rgba(255, 255, 255, 0.08); color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.06em; }
.tag.violet { background: rgba(180, 140, 255, 0.2); color: var(--violet); }
.tag.amber { background: rgba(255, 184, 77, 0.2); color: var(--amber); }
.tag.green { background: rgba(110, 231, 162, 0.2); color: var(--green); }

.dm-row { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--bg-card); border: 1px solid var(--line); border-radius: 14px; margin-bottom: 8px; }
.dm-row .lvl { font-size: 11px; color: var(--violet); font-weight: 800; }

.settings-list { display: flex; flex-direction: column; gap: 8px; }
.settings-list button, .settings-list label { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-card); text-align: left; cursor: pointer; font-size: 14px; }
.settings-list .danger { border-color: rgba(255, 106, 61, 0.4); color: #ffb3a0; }
textarea.save-box { width: 100%; min-height: 90px; background: #0a0e1c; color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 8px; font-family: ui-monospace, monospace; font-size: 11px; resize: vertical; }

/* ---------- Tabs ---------- */
.tabs { grid-area: tabs; display: flex; background: #0a0e1c; border-top: 1px solid var(--line); padding-bottom: var(--safe-bottom); }
.tab { flex: 1; border: 0; background: transparent; color: var(--text-faint); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 8px 2px 6px; font-size: 11px; font-weight: 700; cursor: pointer; position: relative; min-height: 54px; }
.tab .tab-icon { width: 22px; height: 22px; display: flex; align-items: center; justify-content: center; }
.tab .tab-icon svg { width: 22px; height: 22px; }
.tab.active { color: var(--amber); }
.tab.active::after { content: ""; position: absolute; top: 0; left: 25%; right: 25%; height: 2px; background: var(--amber); border-radius: 0 0 4px 4px; box-shadow: 0 0 10px var(--amber); }
.tab-badge { position: absolute; top: 6px; right: calc(50% - 22px); min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px; background: var(--red); color: #fff; font-size: 10px; font-weight: 800; display: flex; align-items: center; justify-content: center; }
.tab-badge[hidden] { display: none; }
.tab.attention .tab-icon { animation: bob 1s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-3px); } }

/* ---------- Modals ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(3, 5, 12, 0.72); display: flex; align-items: center; justify-content: center; padding: 18px; z-index: 50; backdrop-filter: blur(6px); animation: fade-in 0.2s ease-out; }
.modal { width: min(440px, 100%); max-height: 88dvh; overflow-y: auto; background: var(--bg-panel); border: 1px solid var(--line); border-radius: 20px; padding: 18px; box-shadow: var(--shadow); animation: modal-in 0.25s ease-out; }
.modal h2 { font-size: 18px; font-weight: 900; margin-bottom: 6px; }
.modal p { color: var(--text-dim); font-size: 14px; line-height: 1.45; margin-bottom: 10px; }
.modal .actions { display: flex; gap: 8px; margin-top: 12px; }
.modal .actions button { flex: 1; min-height: 46px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg-card); font-weight: 800; cursor: pointer; }
.modal .actions button.primary { border-color: rgba(255, 184, 77, 0.6); background: linear-gradient(180deg, rgba(255, 184, 77, 0.3), rgba(255, 154, 60, 0.15)); color: #ffe4bd; }
.modal .actions button.violet { border-color: rgba(180, 140, 255, 0.6); background: linear-gradient(180deg, rgba(180, 140, 255, 0.3), rgba(140, 100, 255, 0.15)); color: #efe6ff; }
.modal .actions button.danger { border-color: rgba(255, 106, 61, 0.5); color: #ffb3a0; }
.modal .big-number { font-size: 28px; font-weight: 900; color: var(--amber); font-variant-numeric: tabular-nums; margin: 6px 0; text-shadow: 0 0 20px rgba(255, 184, 77, 0.35); }
.modal.ending { text-align: center; }
.modal.ending h2 { font-size: 24px; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-in { from { opacity: 0; transform: translateY(12px) scale(0.98); } to { opacity: 1; transform: none; } }

.flash { position: fixed; inset: 0; background: #fff; pointer-events: none; z-index: 40; animation: flash 0.7s ease-out forwards; }
@keyframes flash { from { opacity: 0.9; } to { opacity: 0; } }

/* ---------- Wide screens ---------- */
@media (min-width: 900px) {
  #app { grid-template-columns: minmax(0, 1fr) minmax(380px, 460px); grid-template-rows: auto 1fr auto; grid-template-areas: "hud panel" "stage panel" "stage tabs"; }
  #panel { border-top: 0; border-left: 1px solid var(--line); }
  .tabs { border-left: 1px solid var(--line); }
  .evolve { left: 50%; right: auto; width: min(460px, 90%); transform: translateX(-50%); }
}
@media (prefers-reduced-motion: reduce) { .stage-hint, .evolve.ready, .tab.attention .tab-icon { animation: none; } }
