:root {
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #2563eb;
  --track: #eef2f7;
  --bg: #fff;
  --surface: #fff;
  --panel: #fafafa;
  --danger: #b91c1c;
}

* { box-sizing: border-box; }

/* The hidden attribute must win over the display rules below. */
[hidden] { display: none !important; }

body {
  margin: 0 auto;
  max-width: 1600px;
  padding: 1.5rem 1rem 3rem;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.5;
}

header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; }
.header-text { min-width: 0; }
.header-actions { display: flex; gap: 0.5rem; flex: none; padding-top: 0.25rem; }

/* Small icon-only buttons (gear, theme toggle) in the header. */
.icon-btn {
  padding: 0.35rem 0.55rem;
  font-size: 1.1rem;
  line-height: 1;
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
}
.icon-btn:hover { background: var(--track); filter: none; }

header h1 { margin: 0 0 0.25rem; font-size: 1.75rem; }
header p { margin: 0; color: var(--muted); }

h2 { font-size: 1.15rem; margin: 0 0 0.5rem; }
h3 { font-size: 1rem; margin: 0 0 0.5rem; }

#status { margin: 0 0 0.75rem; }
#status.error { color: var(--danger); }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

button, .file-button {
  font: inherit;
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: 0.375rem;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:hover, .file-button:hover { filter: brightness(1.1); }
button.secondary {
  background: var(--surface);
  color: var(--accent);
}
button:disabled { opacity: 0.5; cursor: default; }

.file-button input { display: none; }

.hint { color: var(--muted); font-size: 0.9rem; margin: 0.25rem 0 0; }
.hint code { font-size: 0.85em; }

#warnings details { margin-top: 0.75rem; font-size: 0.9rem; color: var(--muted); }
#warnings summary { cursor: pointer; }
#warnings ul { max-height: 12rem; overflow: auto; }

/* Two-column layout */

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 1fr) 8px minmax(280px, 360px);
  gap: 0;
  height: calc(100vh - 13rem);
  min-height: 24rem;
  margin-top: 1.5rem;
}

#chart-col { overflow-y: auto; padding-right: 1rem; }
#settings-col { overflow-y: auto; padding-left: 1rem; }

#splitter {
  cursor: col-resize;
  background: var(--line);
  border-radius: 0.25rem;
}
#splitter:hover, #splitter.dragging { background: var(--accent); }

#settings-col section {
  margin-bottom: 1rem;
  padding: 1rem;
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  background: var(--panel);
}

#settings-col label { display: block; margin-bottom: 0.75rem; }
#settings-col select,
#settings-col input[type="number"],
#settings-col input[type="text"] {
  display: block;
  width: 100%;
  margin-top: 0.25rem;
  font: inherit;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 0.375rem;
  background: var(--surface);
  color: var(--ink);
}
#settings-col select:disabled,
#settings-col input:disabled {
  background: var(--track);
  color: var(--muted);
}

.check-list { margin-bottom: 0.5rem; }

#series-list { margin: 0.25rem 0 0; padding-left: 1.1rem; font-size: 0.95rem; }
#series-list li { margin-bottom: 0.2rem; }
.check-row {
  display: flex !important;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.25rem !important;
}
.check-row input { flex: none; }
.check-row .count { color: var(--muted); font-size: 0.9rem; }

/* The Group by row: the select grows, the Explode checkbox sits on
 * the same line, aligned with the select. */
.group-by-row { display: flex; align-items: flex-end; gap: 0.75rem; margin-bottom: 0.75rem; }
#settings-col .group-by-row > label { margin-bottom: 0; }
#settings-col .group-by-row > label:first-child { flex: 1; min-width: 0; }
#settings-col .group-by-row .explode-toggle { margin-bottom: 0 !important; padding-bottom: 0.4rem; white-space: nowrap; }
#settings-col .group-by-row .info { cursor: help; color: var(--muted); }

.settings-footer {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  padding: 0.5rem 0;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.settings-footer .primary { font-weight: 600; }
#settings-status { margin: 0; font-size: 0.9rem; }
#settings-status.error { color: var(--danger); }

/* Series blocks */

.series-block {
  border: 1px solid var(--line);
  border-radius: 0.375rem;
  padding: 0.75rem;
  margin-bottom: 0.75rem;
  background: var(--surface);
}
.series-block .series-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.series-block label { margin-bottom: 0.5rem; }
.series-block .remove-series {
  padding: 0.4rem 0.6rem;
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}
.series-block .series-filters { margin-top: 0.5rem; }

/* Filter blocks */

.filter-block {
  border: 1px solid var(--line);
  border-radius: 0.375rem;
  padding: 0.5rem;
  margin-bottom: 0.5rem;
  background: var(--surface);
}
.filter-block .filter-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 0.5rem;
  align-items: end;
}
.filter-block label { margin-bottom: 0 !important; }
.filter-block .filter-value { margin-top: 0.5rem; }
.filter-block .remove-filter {
  padding: 0.4rem 0.6rem;
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}
.filter-block .field-error {
  color: var(--danger);
  font-size: 0.85rem;
  margin: 0.25rem 0 0;
}

/* Bestiary manager modal */

dialog {
  border: 1px solid var(--line);
  border-radius: 0.5rem;
  padding: 1.5rem;
  max-width: 32rem;
  width: calc(100% - 2rem);
  color: var(--ink);
  background: var(--surface);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
dialog section { margin-bottom: 1rem; }
dialog ul { list-style: none; padding: 0; margin: 0 0 0.5rem; }
dialog li {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0;
}
dialog li .count { color: var(--muted); font-size: 0.9rem; }
dialog li button {
  margin-left: auto;
  padding: 0.2rem 0.6rem;
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}
dialog input[type="file"] { font: inherit; }
dialog .file-button { display: inline-block; margin: 0 0.5rem 0.5rem 0; }
#upload-status, #session-status { font-size: 0.9rem; }
#upload-status.error, #session-status.error { color: var(--danger); }
#storage-meter { color: var(--muted); font-size: 0.9rem; }

/* Charts */

.chart {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--line);
}
.chart:first-child { margin-top: 0; padding-top: 0; border-top: none; }

.chart h2 { margin-bottom: 0.75rem; }

.chart .subtitle { color: var(--muted); font-size: 0.9rem; margin: 0 0 0.75rem; }

.empty { color: var(--muted); font-style: italic; }

.bars { display: flex; flex-direction: column; gap: 0.75rem; }

.bin { display: grid; grid-template-columns: 11rem 1fr; gap: 0.75rem; align-items: start; }
.bin-label {
  text-align: right;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
  padding-top: 0.1rem;
}
.bin-rows { display: flex; flex-direction: column; gap: 0.25rem; }

.bar-row { display: grid; grid-template-columns: 1fr 4.5rem; gap: 0.5rem; align-items: center; }
.bar-track { background: var(--track); border-radius: 0.25rem; height: 1.1rem; }
.bar { height: 100%; border-radius: 0.25rem; min-width: 2px; }
.bar-value { font-variant-numeric: tabular-nums; text-align: right; }

.legend { margin-top: 0.75rem; display: flex; flex-wrap: wrap; gap: 1rem; }
.legend-item { display: inline-flex; align-items: center; gap: 0.35rem; }
.swatch { display: inline-block; width: 0.9rem; height: 0.9rem; border-radius: 0.2rem; }

footer { margin-top: 2.5rem; color: var(--muted); font-size: 0.85rem; }

@media (max-width: 900px) {
  .layout { display: block; height: auto; min-height: 0; }
  #chart-col, #settings-col { overflow: visible; padding: 0; }
  #settings-col { margin-top: 1.5rem; }
  #splitter { display: none; }
}

@media (max-width: 640px) {
  .bin { grid-template-columns: 6rem 1fr; }
}

/* Dark mode. html[data-theme="dark"] is set by the inline script in
 * index.html (saved choice, else the device setting) and toggled by
 * #theme-toggle. color-scheme: dark lets the browser render form
 * controls, scrollbars and the dialog natively dark. The series
 * bars keep their fixed palette; they are brightened a little so
 * the darker colours stay readable, and the legend swatches match. */
html[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e5e7eb;
  --muted: #9ca3af;
  --line: #374151;
  --accent: #60a5fa;
  --track: #1f2937;
  --bg: #111827;
  --surface: #1f2937;
  --panel: #1f2937;
  --danger: #f87171;
}
html[data-theme="dark"] button,
html[data-theme="dark"] .file-button {
  color: #111827;
}
html[data-theme="dark"] button.secondary {
  color: var(--accent);
}
html[data-theme="dark"] .icon-btn {
  color: var(--ink);
}
html[data-theme="dark"] a {
  color: var(--accent);
}
html[data-theme="dark"] .bar,
html[data-theme="dark"] .swatch {
  filter: brightness(1.3);
}
