:root {
  --bg: #0d1220;
  --panel: #151c2e;
  --panel-2: #1c2540;
  --line: #2a3557;
  --ink: #ebe7dc;
  --muted: #8d97b3;
  --accent: #d4a72c;
  --debt: #ff5d5d;
  --ok: #4fc581;
  --warn: #f0b429;
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body.has-bg { background-size: cover; background-position: center; background-attachment: fixed; }
h1, h2, h3 { margin: 0 0 .5em; line-height: 1.2; }
h2 { font-size: 1.05rem; letter-spacing: .04em; text-transform: uppercase; color: var(--accent); }
h3 { font-size: .95rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin-top: 1.4em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; }
.ok { color: var(--ok); }
.warn { color: var(--warn); }
.bad { color: var(--debt); }
code { font-family: var(--mono); background: var(--panel-2); padding: 1px 5px; border-radius: 4px; font-size: .92em; }
pre.code { font-family: var(--mono); background: #0a0f1b; border: 1px solid var(--line); padding: 10px 12px; border-radius: 8px; overflow-x: auto; font-size: 12.5px; }

button, .btn {
  font: inherit;
  color: var(--ink);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
button:hover, .btn:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: #1a1406; border-color: var(--accent); font-weight: 600; }
button.danger { border-color: #7a2c2c; color: #ffb3b3; }
button.danger:hover { background: #4a1b1b; }
input[type=text], input[type=search], textarea, select {
  font: inherit;
  color: var(--ink);
  background: #0e1424;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  width: 100%;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
input.invalid { border-color: var(--debt); }
textarea.code { font-family: var(--mono); font-size: 12.5px; }

.icon { display: inline-flex; align-items: center; justify-content: center; line-height: 1; }
img.icon { object-fit: contain; }

/* ================= GAME ================= */
body.game { overflow: hidden; }
#app {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) minmax(300px, 1.5fr) minmax(300px, 1.1fr);
  height: 100vh;
  gap: 1px;
  background: var(--line);
}
.panel { background: var(--panel); overflow-y: auto; padding: 16px; }
body.has-bg .panel { background: rgba(21, 28, 46, .9); }

#left { display: flex; flex-direction: column; align-items: stretch; gap: 12px; }
.brand h1 { font-size: 1.5rem; color: var(--accent); margin: 0; }
.brand { text-align: center; }
.debt-box { text-align: center; padding: 14px 8px; background: #0b101c; border: 1px solid var(--line); border-radius: var(--radius); }
.debt-box .label { font-size: 12px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); }
.debt { font-family: var(--mono); font-size: clamp(1.3rem, 2.4vw, 2rem); font-weight: 700; color: var(--debt); text-shadow: 0 0 18px rgba(255, 93, 93, .35); word-break: break-word; }
.dps { color: var(--ink); }
.interest { color: #ff9a9a; font-size: 12px; min-height: 1em; }
.cash-line { margin-top: 6px; }
.cash-line strong { color: var(--ok); font-family: var(--mono); }

#bigButton {
  align-self: center;
  width: min(240px, 70%);
  aspect-ratio: 1;
  border-radius: 50%;
  border: 4px solid var(--accent);
  background: radial-gradient(circle at 35% 30%, #2e3b63, #121a2e 70%);
  box-shadow: 0 0 0 6px rgba(212, 167, 44, .12), 0 10px 40px rgba(0, 0, 0, .5);
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  transition: transform .06s;
  user-select: none;
}
#bigButton:hover { transform: scale(1.03); }
#bigButton:active { transform: scale(.96); }
.big-icon { font-size: 5rem; }
.big-icon img.icon { width: 60%; height: auto; max-height: 120px; }
.big-icon .icon { font-size: inherit; }
.big-label { font-weight: 800; font-size: 1.2rem; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); }

#buffs { display: flex; flex-direction: column; gap: 6px; }
.buff { position: relative; overflow: hidden; display: flex; justify-content: space-between; gap: 8px; font-size: 12px; padding: 5px 9px; border-radius: 6px; background: #1f3a2a; border: 1px solid #2f6a46; }
.buff.bad { background: #3a1f1f; border-color: #6a2f2f; }
.buff i { position: absolute; left: 0; bottom: 0; height: 2px; background: var(--ok); }
.buff.bad i { background: var(--debt); }

.left-actions { margin-top: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.left-actions > * { justify-content: center; }
#btnPrestige.glow { border-color: var(--accent); box-shadow: 0 0 12px rgba(212, 167, 44, .5); color: var(--accent); }

/* Middle */
#middle { padding: 0; display: flex; flex-direction: column; }
#ticker { background: #0b101c; border-bottom: 1px solid var(--line); padding: 9px 14px; overflow: hidden; white-space: nowrap; font-style: italic; color: #cfd6ea; min-height: 38px; }
#tickerText { display: inline-block; }
#tickerText.roll { animation: roll 9s linear forwards; }
@keyframes roll { 0% { transform: translateX(40%); opacity: 0; } 8% { opacity: 1; } 92% { opacity: 1; } 100% { transform: translateX(-10%); opacity: 0; } }
#swamp { padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.swamp-row { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; }
.swamp-label { font-size: 13px; display: flex; gap: 8px; flex-wrap: wrap; align-items: baseline; }
.swamp-label .muted { margin-left: auto; font-family: var(--mono); font-size: 12px; }
.swamp-icons { display: flex; flex-wrap: wrap; gap: 2px; margin-top: 6px; }
.swamp-icon { font-size: 22px; width: 26px; height: 26px; animation: bob 2.4s ease-in-out infinite; }
.swamp-icon:nth-child(3n) { animation-delay: -.8s; }
.swamp-icon:nth-child(3n+1) { animation-delay: -1.6s; }
@keyframes bob { 50% { transform: translateY(-3px); } }
.swamp-icons .more { color: var(--muted); font-size: 12px; align-self: center; margin-left: 4px; }
.swamp-empty { text-align: center; margin-top: 30%; font-size: 1.1rem; }

/* Store */
.upgrade-grid { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; min-height: 52px; }
.upg { width: 52px; height: 52px; padding: 0; justify-content: center; font-size: 26px; opacity: .45; }
.upg img.icon { width: 40px; height: 40px; }
.upg.afford { opacity: 1; border-color: var(--accent); }
.store-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.store-head h2 { margin: 0; }
.seg { display: inline-flex; }
.seg button { border-radius: 0; padding: 3px 9px; font-size: 12px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.on { background: var(--accent); color: #1a1406; border-color: var(--accent); }
#generators { display: flex; flex-direction: column; gap: 6px; }
.gen-row { width: 100%; display: grid; grid-template-columns: 48px 1fr auto; align-items: center; gap: 10px; text-align: left; padding: 6px 10px; opacity: .55; }
.gen-row.afford { opacity: 1; }
.gen-row.afford .gen-cost { color: var(--ok); }
.gen-row.mystery { opacity: .3; cursor: default; filter: grayscale(1); }
.gen-icon { font-size: 34px; display: flex; justify-content: center; }
.gen-icon img.icon { width: 44px; height: 44px; }
.gen-name { font-weight: 600; }
.gen-cost { font-family: var(--mono); font-size: 12.5px; color: var(--debt); }
.gen-count { font-size: 1.6rem; font-weight: 800; color: var(--muted); font-family: var(--mono); }
.no-refunds { margin-top: 14px; font-style: italic; }

/* Overlays */
#tooltip { position: fixed; z-index: 50; width: 320px; background: #0b101c; border: 1px solid var(--accent); border-radius: var(--radius); padding: 12px; pointer-events: none; box-shadow: 0 10px 30px rgba(0, 0, 0, .6); }
#tooltip p { margin: 8px 0 0; }
.tt-head { display: flex; gap: 10px; align-items: center; }
.tt-head .icon { font-size: 30px; width: 36px; height: 36px; }
.tt-name { font-weight: 700; }
.tt-cost { margin-left: auto; font-family: var(--mono); color: var(--debt); }
.tt-cost.ok { color: var(--ok); }
.tt-stats { margin: 8px 0 0; padding-left: 18px; color: #cfd6ea; }
.flavor { font-style: italic; color: var(--muted); }

#floaters, #eventLayer, #toasts { position: fixed; pointer-events: none; inset: 0; z-index: 40; }
.floater { position: fixed; transform: translate(-50%, -50%); font-weight: 800; color: var(--debt); text-shadow: 0 2px 4px #000; animation: floatUp 1.1s ease-out forwards; font-family: var(--mono); }
@keyframes floatUp { to { transform: translate(-50%, -120px); opacity: 0; } }
.emergency { position: fixed; pointer-events: auto; width: 72px; height: 72px; border-radius: 50%; font-size: 40px; justify-content: center; background: radial-gradient(circle, #5a1414, #2a0808); border: 3px solid var(--debt); box-shadow: 0 0 30px rgba(255, 93, 93, .7); animation: pulse 1.2s ease-in-out infinite, fadeLife 13s linear forwards; }
.emergency img.icon { width: 48px; height: 48px; }
@keyframes pulse { 50% { transform: scale(1.12) rotate(-6deg); } }
@keyframes fadeLife { 0% { opacity: 0; } 5% { opacity: 1; } 85% { opacity: 1; } 100% { opacity: 0; } }
#toasts { inset: auto auto 16px 16px; display: flex; flex-direction: column-reverse; gap: 8px; width: min(360px, calc(100vw - 32px)); }
.toast { pointer-events: auto; cursor: pointer; display: flex; gap: 10px; align-items: flex-start; background: #0b101c; border: 1px solid var(--accent); border-radius: var(--radius); padding: 10px 12px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5); animation: toastIn .25s ease-out; }
.toast .icon { font-size: 26px; width: 30px; height: 30px; }
@keyframes toastIn { from { transform: translateY(20px); opacity: 0; } }
#modal { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, .6); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(520px, 100%); max-height: 90vh; overflow-y: auto; }
#modalActions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 16px; }
.stats-table { width: 100%; border-collapse: collapse; }
.stats-table td { padding: 5px 4px; border-bottom: 1px solid var(--line); }
.stats-table td:last-child { text-align: right; font-family: var(--mono); }

@media (max-width: 900px) {
  body.game { overflow: auto; }
  #app { grid-template-columns: 1fr; height: auto; }
  .panel { overflow: visible; }
  #middle { max-height: 50vh; overflow-y: auto; }
  #tooltip { display: none; }
}

/* ================= ADMIN ================= */
body.admin { display: flex; flex-direction: column; height: 100vh; }
.admin-top { display: flex; align-items: center; gap: 16px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.admin-brand { font-size: 1.05rem; }
.admin-status { flex: 1; font-size: 13px; }
.admin-top-actions { display: flex; gap: 8px; }
.admin-body { flex: 1; display: flex; min-height: 0; }
.admin-tabs { width: 190px; background: var(--panel); border-right: 1px solid var(--line); padding: 10px; display: flex; flex-direction: column; gap: 4px; }
.admin-tabs button { justify-content: space-between; background: transparent; border-color: transparent; text-align: left; width: 100%; }
.admin-tabs button.on { background: var(--panel-2); border-color: var(--accent); }
.admin-tabs .count { color: var(--muted); font-size: 12px; }
#main { flex: 1; overflow: hidden; display: flex; flex-direction: column; }
#main > .form { overflow-y: auto; padding: 20px 28px; max-width: 980px; }
.split { flex: 1; display: flex; min-height: 0; }
.list-pane { width: 300px; border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.list-tools { display: flex; flex-direction: column; gap: 6px; padding: 10px; border-bottom: 1px solid var(--line); }
.list-tools button { justify-content: center; }
.item-list { overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 2px; }
.item { width: 100%; text-align: left; background: transparent; border-color: transparent; padding: 5px 8px; gap: 10px; }
.item.on { background: var(--panel-2); border-color: var(--accent); }
.item .icon { font-size: 22px; width: 28px; height: 28px; flex: none; }
.item-text { display: flex; flex-direction: column; min-width: 0; }
.item-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: 11.5px; font-family: var(--mono); }
.edit-pane { flex: 1; overflow-y: auto; padding: 18px 26px 60px; }
.edit-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; border-bottom: 1px solid var(--line); padding-bottom: 10px; margin-bottom: 10px; }
.edit-head h2 { margin: 0; text-transform: none; letter-spacing: 0; font-size: 1.3rem; }
.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 20px; align-content: start; }
.form > h2, .form > h3, .form > p, .form > .wide, .form > ul, .form > .row, .form > textarea, .form > div.cols, .form > pre { grid-column: 1 / -1; }
.settings-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.wide { grid-column: 1 / -1; }
.field > label { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.num-wrap { display: flex; gap: 8px; align-items: center; }
.num-wrap input { flex: 1; font-family: var(--mono); }
.num-hint { font-size: 12px; white-space: nowrap; }
.icon-field { display: flex; gap: 10px; align-items: flex-start; }
.icon-preview { width: 64px; height: 64px; flex: none; display: flex; align-items: center; justify-content: center; background: #0e1424; border: 1px dashed var(--line); border-radius: 8px; font-size: 36px; overflow: hidden; }
.icon-preview img.icon { max-width: 100%; max-height: 100%; }
.icon-controls { flex: 1; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.typed-list { display: flex; flex-direction: column; gap: 6px; }
.typed-row { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.typed-head { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.typed-head > select { width: auto; max-width: 280px; }
.typed-fields { display: flex; gap: 8px; flex: 1; flex-wrap: wrap; }
label.mini { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--muted); flex: 1; min-width: 140px; }
.icon-btn { padding: 4px 9px; }
.add-btn { align-self: flex-start; border-style: dashed; }
.preview-box { background: #0e1424; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; display: flex; gap: 24px; flex-wrap: wrap; }
.preview-box ul { margin: 0; padding-left: 18px; }
.mini-table { border-collapse: collapse; font-family: var(--mono); font-size: 12px; }
.mini-table td, .mini-table th { padding: 1px 10px; text-align: right; }
.problems { color: #ffb3b3; }
.empty { padding: 40px; text-align: center; }
.help .cols { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.help { display: block !important; line-height: 1.6; }
@media (max-width: 800px) {
  .admin-body { flex-direction: column; }
  .admin-tabs { width: auto; flex-direction: row; flex-wrap: wrap; }
  .admin-tabs button { width: auto; }
  .split { flex-direction: column; }
  .list-pane { width: auto; max-height: 40vh; border-right: 0; border-bottom: 1px solid var(--line); }
  .form, .settings-form { grid-template-columns: 1fr; }
}
