:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0eee9;
  --row-alt: #faf9f7;
  --border: #dedad2;
  --text: #1f1d1a;
  --muted: #77716a;
  --accent: #8a5a12;
  --accent-text: #ffffff;
  --unique: #8a6d1e;
  --set: #2d7a1f;
  --rune: #b25a00;
  --magic: #3550b8;
  --yes: #a23a2a;
  --yes-bg: #f8e4df;
  --error: #b3261e;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141311;
    --surface: #1c1b18;
    --surface-2: #26241f;
    --row-alt: #201f1b;
    --border: #36332d;
    --text: #e9e5dc;
    --muted: #9b958a;
    --accent: #d2a24c;
    --accent-text: #1a1408;
    --unique: #c7b377;
    --set: #6fd25a;
    --rune: #f0a04b;
    --magic: #8c9cff;
    --yes: #f08f7c;
    --yes-bg: #3a221c;
    --error: #ff8a80;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

body.busy {
  cursor: progress;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

/* ---- header & inputs ---- */

.top {
  display: flex;
  align-items: flex-end;
  gap: 12px 24px;
  padding: 14px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

/* One row while everything fits; otherwise every block on its own line (app.js toggles
   .stacked), rather than flex-wrap's partial wrapping. */
.top > * {
  flex: none;
}

.top.stacked {
  flex-direction: column;
  align-items: stretch;
}

h1 {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 650;
  letter-spacing: 0.01em;
}

.inputs {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 14px;
}

.inputs label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
  color: var(--muted);
}

.inputs input {
  width: 120px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.inputs input:focus,
.finput:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

button {
  padding: 7px 14px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

button.secondary {
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
}

/* Live filters, kept visually apart from the inputs that need Calculate. */
.live-filter {
  display: block;
  padding: 8px 12px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--bg);
}

.stat-filter-panel {
  margin-left: auto;
}

.top.stacked .stat-filter-panel {
  margin-left: 0;
}

.live-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--muted);
}

.live-head .hint {
  font-size: 11px;
  font-style: italic;
}

.checks {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
}

.checks label {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.checks input,
.picker-item input {
  margin: 0;
  accent-color: var(--accent);
}

/* ---- toolbar ---- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 8px 16px;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
}

.tab[aria-selected='true'] {
  border-color: var(--border);
  background: var(--surface);
  color: var(--text);
}

.tab .count {
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.stat-filter {
  display: block;
  width: 240px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

.top.stacked .stat-filter {
  width: 100%;
}

.stat-filter:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.hc-props li.match {
  font-weight: 650;
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, currentColor 45%, transparent);
  text-underline-offset: 2px;
}

.status {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.status.error {
  color: var(--error);
}

/* ---- table ---- */

.scroller {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 0 16px 16px;
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.table {
  position: relative;
  min-width: 100%;
}

.thead {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.hrow,
.tr {
  display: grid;
  grid-template-columns: var(--cols);
}

.th {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 7px 8px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}

@media (hover: hover) {
  .th:hover {
    background: var(--border);
  }
}

.th.num {
  justify-content: flex-end;
}

.th-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.sort-ind {
  font-size: 10px;
  color: var(--accent);
}

.filters {
  padding-bottom: 6px;
}

.fcell {
  padding: 0 4px;
}

.finput {
  width: 100%;
  min-width: 0;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface);
  font-size: 12px;
}

.fcell.with-pick {
  display: flex;
  gap: 3px;
}

.pick {
  flex: none;
  min-width: 26px;
  padding: 0 6px;
  border-color: var(--border);
  border-radius: 5px;
  background: var(--surface);
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.pick.wide {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  /* Match the text filters: `.pick` drops `.finput`'s vertical padding, and the cell sets row height. */
  height: 100%;
  padding: 4px 6px;
  font-size: 12px;
  font-weight: 400;
  text-align: left;
}

.pick-text {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.pick-caret {
  flex: none;
}

.pick.active,
.pick[aria-expanded='true'] {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

/* ---- checkbox-list picker ---- */

.picker {
  position: fixed;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: min(300px, calc(100vw - 16px));
  max-height: min(420px, 70vh);
  max-height: min(420px, 70dvh);
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
}

.picker-actions {
  display: flex;
  gap: 6px;
}

.picker-actions button {
  padding: 3px 10px;
  font-size: 12px;
}

.picker-list {
  overflow: auto;
  min-height: 0;
}

.picker-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 4px;
  border-radius: 4px;
  font-size: 13px;
  cursor: pointer;
}

@media (hover: hover) {
  .picker-item:hover {
    background: var(--surface-2);
  }
}

.picker-item[hidden] {
  display: none;
}

.tbody {
  position: relative;
}

.spacer {
  position: relative;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.tr {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: var(--row-h, 28px);
  align-items: center;
  border-bottom: 1px solid var(--surface-2);
}

.tr.odd {
  background: var(--row-alt);
}

@media (hover: hover) {
  .tr:hover {
    background: var(--surface-2);
  }
}

.td {
  padding: 0 8px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.tr[data-quality='unique'] .col-item,
.tr[data-quality='unique'] .col-quality {
  color: var(--unique);
  font-weight: 600;
}

.tr[data-quality='set'] .col-item,
.tr[data-quality='set'] .col-quality {
  color: var(--set);
  font-weight: 600;
}

.tr[data-quality='rune'] .col-item {
  color: var(--rune);
  font-weight: 600;
}

.td.col-terrorized {
  color: var(--muted);
}

.td.yes {
  color: var(--yes);
  font-weight: 600;
}

.empty {
  position: absolute;
  top: 110px;
  left: 0;
  right: 0;
  margin: 0;
  text-align: center;
  color: var(--muted);
}

.empty[hidden] {
  display: none;
}

/* ---- hover cards ---- */

.hovercard {
  position: fixed;
  z-index: 20;
  width: max-content;
  max-width: 360px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
  font-size: 13px;
  pointer-events: none;
}

/* Touch: the card opens on tap as a bottom sheet that scrolls and has a close button. */
.hovercard.sheet {
  top: auto;
  right: 0;
  bottom: 0;
  left: 0;
  width: auto;
  max-width: 560px;
  max-height: 65vh;
  max-height: 65dvh;
  margin: 0 auto;
  padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  border-bottom: 0;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -8px 24px rgb(0 0 0 / 0.18);
  pointer-events: auto;
}

.hc-close {
  position: sticky;
  top: 0;
  float: right;
  margin: -4px -6px 0 8px;
  width: 36px;
  height: 36px;
  padding: 0;
  border-color: var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 20px;
  font-weight: 500;
  line-height: 1;
}

.td.card-open {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.hovercard[hidden] {
  display: none;
}

.hc-head {
  margin-bottom: 6px;
}

.hc-title {
  font-size: 14px;
  font-weight: 650;
}

.hc-title.q-unique {
  color: var(--unique);
}

.hc-title.q-set {
  color: var(--set);
}

.hc-sub,
.hc-note,
.hc-label {
  color: var(--muted);
  font-size: 12px;
}

.hc-note {
  margin: 4px 0 2px;
  font-style: italic;
}

.hc-label {
  margin-bottom: 3px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 11px;
}

.hc-section {
  margin-top: 8px;
}

.hc-tags {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.hc-tag {
  padding: 1px 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 11px;
}

.hc-facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 12px;
  margin: 0;
}

.hc-facts dt {
  color: var(--muted);
}

.hc-facts dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.hc-res {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
}

.hc-res-cell {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  font-size: 12px;
}

.hc-res-cell b {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.hc-res-cell.immune {
  background: var(--yes-bg);
  color: var(--yes);
}

.hc-list {
  line-height: 1.45;
}

.hc-props {
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--magic);
  line-height: 1.45;
}

.hc-props.set {
  color: var(--set);
}

/* Touch: details open on tap, so mark the cells that have them; enlarge small targets; keep
   form text at 16px so iOS doesn't zoom the page on focus. */
@media (pointer: coarse) {
  .td.col-monster,
  .td.col-location,
  .td.col-item {
    cursor: pointer;
    text-decoration: underline dotted color-mix(in srgb, currentColor 40%, transparent);
    text-underline-offset: 3px;
  }
  input,
  select,
  .finput {
    font-size: 16px;
  }
  .pick {
    min-width: 36px;
  }
  .checks {
    gap: 2px 6px;
  }
  .checks label {
    padding: 6px 8px 6px 0;
  }
  .checks input {
    width: 18px;
    height: 18px;
  }
  .tab,
  button.secondary {
    min-height: 36px;
  }
  .picker-item {
    padding: 8px 4px;
  }
}

/* Small or short screens: the stacked header can be taller than the screen, so let the page scroll
   and give the table its own fixed-height viewport instead of the leftover space. */
@media (max-width: 640px), (max-height: 560px) {
  html,
  body {
    height: auto;
  }
  .scroller {
    flex: none;
    height: 85vh;
    height: 85dvh;
    min-height: 280px;
  }
}

@media (max-width: 640px) {
  .top {
    padding: 12px;
  }
  .toolbar {
    padding: 8px 12px;
  }
  .scroller {
    margin: 0 8px 8px;
  }
  .inputs input {
    width: 96px;
  }
}
