/* Shared styling for every page. Light by default, dark from the system or the toggle. */

:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --sunken: #f1f3f6;
  --line: #e6eaef;
  --line-strong: #d5dbe3;
  --text: #171a1f;
  --muted: #6b7480;
  --accent: #856427;
  --pos: #1f6b45;
  --neg: #9a3a2e;
  --shadow: 0 1px 2px rgba(16, 20, 26, .04);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c0e11;
    --panel: #14171b;
    --sunken: #101317;
    --line: #21262d;
    --line-strong: #2f363f;
    --text: #e5e9ee;
    --muted: #8a929e;
    --accent: #cbab6d;
    --pos: #6ec08c;
    --neg: #d78a7e;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  --bg: #0c0e11;
  --panel: #14171b;
  --sunken: #101317;
  --line: #21262d;
  --line-strong: #2f363f;
  --text: #e5e9ee;
  --muted: #8a929e;
  --accent: #cbab6d;
  --pos: #6ec08c;
  --neg: #d78a7e;
  --shadow: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 18px;
  height: 52px;
  padding: 0 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: .86rem;
  font-weight: 600;
  letter-spacing: -.01em;
  white-space: nowrap;
}

.brand .mark {
  position: relative;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  background: var(--text);
}

.brand .mark::after {
  content: "";
  position: absolute;
  inset: 5px;
  border-radius: 2px;
  background: var(--panel);
}

.nav {
  display: flex;
  flex: 1;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}

.nav::-webkit-scrollbar { display: none; }

.nav a {
  padding: 5px 10px;
  border-radius: 7px;
  font-size: .82rem;
  color: var(--muted);
  white-space: nowrap;
  transition: color .12s, background .12s;
}

.nav a:hover { color: var(--text); background: var(--sunken); }
.nav a.active { color: var(--text); background: var(--sunken); font-weight: 550; }

.icon-btn {
  display: grid;
  place-items: center;
  flex: none;
  width: 30px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
}

.icon-btn:hover { color: var(--text); border-color: var(--line-strong); }
.icon-btn svg { width: 15px; height: 15px; }

/* ---------- page frame ---------- */

/* Wide on purpose: a calculator page should fit every stat card and the top of each
   table on one screen, so the frame takes whatever width the display gives it. */
main { max-width: 1900px; margin: 0 auto; padding: 20px 24px 60px; }

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px 16px;
  margin-bottom: 18px;
}

h1 { margin: 0; font-size: 1.22rem; font-weight: 600; letter-spacing: -.02em; }

/* ---------- tabs ---------- */

.tabs { display: flex; gap: 2px; margin-right: auto; }

.tabs .tab {
  font: inherit;
  font-size: .82rem;
  padding: 5px 12px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: none;
  color: var(--muted);
  cursor: pointer;
  transition: color .12s, background .12s;
}

.tabs .tab:hover { color: var(--text); background: var(--sunken); }
.tabs .tab.active { color: var(--text); background: var(--sunken); border-color: var(--line); font-weight: 550; }

.blurb { margin: 2px 0 0; max-width: 62ch; font-size: .84rem; color: var(--muted); }

.head-actions { display: flex; align-items: center; gap: 8px; }

/* ---------- cards ---------- */

/* Columns rather than a grid: cards have very different heights, and a grid would
   leave a ragged gap under every short one. */
.cards { columns: 268px; column-gap: 14px; }
.cards > * { break-inside: avoid; margin-bottom: 14px; }

.card {
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card > header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}

.card > header h2 {
  margin: 0;
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}

.card .body { padding: 6px 14px 10px; }

/* ---------- stat fields ---------- */

/* minmax(0, …) lets a long label wrap inside its card instead of pushing the box out of it. */
.field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 84px;
  align-items: center;
  gap: 12px;
  padding: 4px 0;
}

.field + .field { border-top: 1px solid var(--line); }
.field > span { font-size: .81rem; color: var(--muted); overflow-wrap: anywhere; }
.field.filled > span { color: var(--text); }

/* The "?" after a stat's label, and the explanation it shows under the field. */
.help-button {
  display: inline-grid;
  place-items: center;
  width: 15px;
  height: 15px;
  margin-left: 5px;
  padding: 0;
  vertical-align: 1px;
  font: inherit;
  font-size: .64rem;
  font-weight: 600;
  line-height: 1;
  color: var(--muted);
  background: none;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  cursor: pointer;
}

.help-button:hover,
.help-button[aria-expanded="true"] { color: var(--accent); border-color: var(--accent); }

.help-text {
  grid-column: 1 / -1;
  margin: 0;
  font-size: .75rem;
  line-height: 1.4;
  color: var(--muted);
}

/* A text box, not a number box: it holds grouped digits and the short form, which
   type="number" would refuse to display. */
input.numbox {
  width: 100%;
  padding: 5px 8px;
  font: inherit;
  font-size: .81rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  background: var(--sunken);
  border: 1px solid transparent;
  border-radius: 7px;
  outline: none;
  transition: border-color .12s, background .12s;
}

input.numbox:hover { border-color: var(--line-strong); }
input.numbox:focus { border-color: var(--accent); background: var(--panel); }

.field.select { grid-template-columns: minmax(0, 1fr) 84px; }
/* A drop-down of names ("Flawless Aquamarine") sits under its label at the card's full width. */
.field.select.wide { grid-template-columns: minmax(0, 1fr); gap: 4px; }
/* .field's display: grid would otherwise override the hidden attribute. */
.field[hidden] { display: none; }

select {
  width: 100%;
  padding: 5px 8px;
  font: inherit;
  font-size: .81rem;
  color: var(--text);
  background: var(--sunken);
  border: 1px solid transparent;
  border-radius: 7px;
  outline: none;
  cursor: pointer;
  transition: border-color .12s, background .12s;
}

select:hover { border-color: var(--line-strong); }
select:focus { border-color: var(--accent); background: var(--panel); }

/* A page option that steers one table sits in that table's header. */
.head-controls { display: flex; align-items: center; gap: 10px; }
.head-controls .field { padding: 0; gap: 8px; grid-template-columns: auto minmax(112px, auto); }
.head-controls .field + .field { border-top: none; }

.head-controls .field > span {
  font-size: .69rem;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  overflow-wrap: normal;
}

.head-controls select { padding: 3px 7px; font-size: .78rem; }

.field.toggle { grid-template-columns: 1fr auto; }
input[type="checkbox"] { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); cursor: pointer; }

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  font-size: .82rem;
  padding: 6px 13px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: border-color .12s, opacity .12s;
}

.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; border-color: var(--line-strong); }

.btn.primary { background: var(--text); border-color: var(--text); color: var(--panel); }
.btn.primary:hover { opacity: .85; }
.btn.quiet { border-color: transparent; color: var(--muted); }
.btn.quiet:hover { color: var(--text); border-color: var(--line); }

/* ---------- calculator layout ---------- */

.layout {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

/*
 * Three narrow columns rather than one tall one, so the whole stat sheet is visible at a
 * glance. The last column is the tab's own stats: rebuilding it on a tab switch leaves the
 * shared cards in the first two exactly where they were.
 */
.sidebar {
  display: grid;
  grid-template-columns: repeat(3, 178px);
  gap: 12px;
  align-items: start;
}

.stat-cols { grid-column: span 2; columns: 2; column-gap: 12px; }
.stat-cols > * { break-inside: avoid; margin-bottom: 12px; }
.tab-col { display: grid; gap: 12px; align-content: start; }

/* Alchemy and Pets ask for one column instead: every input in a single stack. */
.layout[data-page="alchemy"] .sidebar,
.layout[data-page="pets"] .sidebar,
.layout[data-page="forge"] .sidebar { grid-template-columns: 220px; }
.layout[data-page="alchemy"] .stat-cols,
.layout[data-page="pets"] .stat-cols,
.layout[data-page="forge"] .stat-cols { grid-column: auto; columns: 1; }

/* Farming keeps only Fortune in the shared block, so two columns: Fortune, then the tab's. */
.layout[data-page="farming"] .sidebar,
.layout[data-page="mining"] .sidebar { grid-template-columns: repeat(2, 200px); }
/* Minions: Wisdom, then a wider Assumptions column so fuel and upgrade names fit their drop-downs. */
.layout[data-page="minions"] .sidebar { grid-template-columns: 200px 280px; }
.layout[data-page="minions"] .field.select { grid-template-columns: minmax(0, 1fr) 170px; }
.layout[data-page="farming"] .stat-cols,
.layout[data-page="minions"] .stat-cols { grid-column: auto; columns: 1; }

/* Mining shares nothing across its tabs, so the tab's own cards fill both columns. */
.layout[data-page="mining"] .stat-cols { display: none; }
.layout[data-page="mining"] .tab-col { grid-column: 1 / -1; display: block; columns: 2; column-gap: 12px; }
.layout[data-page="mining"] .tab-col > * { break-inside: avoid; margin-bottom: 12px; }

.results-pane { display: grid; gap: 12px; }
.table-stack,
.side-tables { display: grid; gap: 14px; align-content: start; min-width: 0; }
.side-tables:empty { display: none; }
/* The spray table carries more columns than the pest one, so it gets the wider half. */
.tables {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 14px;
  align-items: start;
}

.tables:has(> :nth-child(2)):not(:has(> :nth-child(3))) {
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
}

/*
 * Alchemy's two tables are close enough in width that an even split lets both show every
 * column, rather than one of them hiding its last column behind a scrollbar you can only
 * reach from the bottom of the rows.
 */
.layout[data-page="alchemy"] .tables:has(> :nth-child(2)):not(:has(> :nth-child(3))),
.layout[data-page="pets"] .tables:has(> :nth-child(2)):not(:has(> :nth-child(3))) {
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}

/*
 * Capped well short of the frame, so the page does not spend its whole width on a table:
 * the columns sit at the width they need and the space that frees up is left empty on the
 * right. Long names wrap onto a second line rather than scroll, since a horizontal
 * scrollbar sits at the foot of 186 rows where nothing can reach it.
 */
.layout[data-page="alchemy"] .results-pane,
.layout[data-page="pets"] .results-pane { max-width: 1240px; }
.layout[data-page="alchemy"] thead th,
.layout[data-page="alchemy"] td.name,
.layout[data-page="pets"] thead th,
.layout[data-page="pets"] td.name { white-space: normal; }
/* Forge: its two recipe tables, then the narrower three-column gauntlet table, all in one row. */
.layout[data-page="forge"] .tables { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 0.8fr); }
.layout[data-page="forge"] .results-pane { max-width: 1500px; }
.layout[data-page="forge"] thead th,
.layout[data-page="forge"] td.name { white-space: normal; }

/* Gemstone Powder wants its two chest tables one above the other, not side by side. */
.layout[data-page="mining"] .tables,
.layout[data-page="mining"] .tables:has(> :nth-child(2)):not(:has(> :nth-child(3))) {
  grid-template-columns: minmax(0, 1fr);
}
.layout[data-page="mining"] .results-pane { max-width: 560px; }
.layout[data-page="mining"] thead th,
.layout[data-page="mining"] td.name { white-space: normal; }

/*
 * Composter: one column of inputs, Upgrades over Assumptions over what the upgrades cost,
 * then organic matter stacked over fuel beside next upgrade stacked over the next two.
 */
.layout[data-page="composter"] .sidebar { grid-template-columns: 250px; }
.layout[data-page="composter"] .stat-cols { grid-column: auto; columns: 1; }
.layout[data-page="composter"] .tables { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.layout[data-page="composter"] .results-pane { max-width: 1400px; }
.layout[data-page="composter"] thead th,
.layout[data-page="composter"] td.name,
.layout[data-page="composter"] td.dim { white-space: normal; }

.layout[data-page="minions"] .results-pane { max-width: 900px; }
.layout[data-page="minions"] thead th,
.layout[data-page="minions"] td.name { white-space: normal; }

@media (max-width: 1480px) {
  .sidebar { grid-template-columns: repeat(2, 190px); }
  .stat-cols { grid-column: span 1; columns: 1; }
}

@media (max-width: 1080px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  .stat-cols { grid-column: 1 / -1; columns: 240px; }
  .layout[data-page="alchemy"] .sidebar,
  .layout[data-page="pets"] .sidebar,
  .layout[data-page="forge"] .sidebar { grid-template-columns: 1fr; }
  .layout[data-page="farming"] .sidebar,
  .layout[data-page="mining"] .sidebar,
  .layout[data-page="minions"] .sidebar,
  .layout[data-page="composter"] .sidebar { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  .layout[data-page="composter"] .tables,
  .tables,
  .tables:has(> :nth-child(2)):not(:has(> :nth-child(3))) { grid-template-columns: 1fr; }
}

.results-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 30px;
  font-size: .78rem;
  color: var(--muted);
}

/* ---------- tables ---------- */

.table-wrap { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; font-size: .82rem; }

thead th {
  padding: 9px 11px;
  text-align: left;
  font-size: .69rem;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}

thead th:hover { color: var(--text); }
thead th.sorted { color: var(--text); }
thead th .caret { margin-left: 3px; font-size: .8em; opacity: .55; }

tbody td { padding: 7px 11px; border-bottom: 1px solid var(--line); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--sunken); }

td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.name { font-weight: 500; white-space: nowrap; }
td.dim { color: var(--muted); }
td.pos { color: var(--pos); }
td.neg { color: var(--neg); }

td.rank {
  width: 1%;
  padding-right: 0;
  font-size: .72rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.note {
  padding: 9px 14px;
  border-top: 1px solid var(--line);
  font-size: .75rem;
  color: var(--muted);
}

/* ---------- states ---------- */

.empty {
  padding: 34px 20px;
  text-align: center;
  font-size: .84rem;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
}

.error {
  padding: 12px 14px;
  border: 1px solid var(--neg);
  border-radius: var(--radius);
  font-size: .82rem;
  color: var(--neg);
  background: var(--panel);
}

.skeleton {
  height: 96px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--panel), var(--sunken), var(--panel));
  background-size: 200% 100%;
  animation: shimmer 1.1s linear infinite;
}

@keyframes shimmer { to { background-position: -200% 0; } }
