:root {
  --bark: #1B1712;
  --bark-raised: #241F18;
  --pine: #2B3A2C;
  --pine-soft: #37473A;
  --parchment: #E8E1D0;
  --parchment-dim: #CFC6B0;
  --ember: #B5502D;
  --ember-bright: #D4703F;
  --gold: #C9A227;
  --blood: #7A1F1F;
  --ink: #E8E1D0;
  --ink-dim: #A79E8C;
  --border: rgba(232, 225, 208, 0.14);
  --shadow: rgba(0, 0, 0, 0.45);

  --bg: var(--bark);
  --bg-raised: var(--bark-raised);
  --surface: var(--pine);
  --surface-soft: var(--pine-soft);
  --text: var(--ink);
  --text-dim: var(--ink-dim);
  --accent: var(--ember-bright);
  --accent-strong: var(--ember);
}

:root[data-theme="parchment"] {
  --bg: var(--parchment);
  --bg-raised: #DED4BC;
  --surface: #F4EEDD;
  --surface-soft: #EAE1C9;
  --text: #24201A;
  --text-dim: #5C5344;
  --accent: var(--ember);
  --accent-strong: var(--blood);
  --border: rgba(36, 32, 26, 0.14);
  --shadow: rgba(60, 50, 30, 0.18);
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s ease, color 0.25s ease;
}

h1,
h2,
h3,
.creature-name,
.stat-title {
  font-family: Iowan Old Style, Palatino Linotype, Palatino, Georgia, "Noto Serif", serif;
  font-weight: 600;
  letter-spacing: 0.01em;
}

/* ---------- Header ---------- */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.1;
}

.brand-sub {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--text-dim);
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--surface-soft);
}

/* ---------- List view ---------- */

.view {
  max-width: 860px;
  margin: 0 auto;
  padding: 20px;
}

.search-bar {
  position: relative;
  margin-bottom: 14px;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim);
  pointer-events: none;
}

#search-input {
  width: 100%;
  padding: 13px 16px 13px 42px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 1rem;
}

#search-input::placeholder {
  color: var(--text-dim);
}

#search-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
}

.filter-chip {
  padding: 6px 13px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 0.82rem;
  cursor: pointer;
  text-transform: capitalize;
}

.filter-chip.active {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: var(--parchment);
}

.result-count {
  color: var(--text-dim);
  font-size: 0.82rem;
  margin: 0 0 12px 2px;
}

.creature-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.creature-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  text-align: left;
  cursor: pointer;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.creature-card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.creature-card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card-top-main {
  display: flex;
  align-items: center;
  gap: 10px;
}

.card-token {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  flex-shrink: 0;
}

.creature-name {
  font-size: 1.05rem;
  margin: 0;
}

.creature-type {
  color: var(--text-dim);
  font-size: 0.8rem;
  margin: 2px 0 0;
  text-transform: capitalize;
}

.cr-badge {
  flex-shrink: 0;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 3px 8px;
  font-size: 0.72rem;
  color: var(--gold);
  white-space: nowrap;
}

.creature-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.tag-pill {
  font-size: 0.68rem;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px;
}

.empty-state {
  text-align: center;
  color: var(--text-dim);
  padding: 60px 20px;
  font-style: italic;
}

/* ---------- Detail / codex view ---------- */

.detail-view {
  padding-top: 14px;
}

.detail-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 8px 4px;
}

.print-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 0.85rem;
  border-radius: 8px;
  cursor: pointer;
  padding: 8px 12px;
}

.print-btn:hover {
  background: var(--surface-soft);
}

.codex-page {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 26px 24px 32px;
  box-shadow: 0 10px 30px var(--shadow);
}

.codex-quote {
  font-style: italic;
  color: var(--text-dim);
  border-left: 3px solid var(--accent-strong);
  padding-left: 14px;
  margin: 0 0 16px;
  font-size: 0.95rem;
}

.codex-lore {
  color: var(--text-dim);
  font-size: 0.92rem;
  line-height: 1.55;
  margin: 0 0 22px;
}

.codex-lore p {
  margin: 0 0 12px;
}

.codex-lore p:last-child {
  margin-bottom: 0;
}

.codex-lore strong,
.codex-lore b {
  color: var(--text);
}

.codex-lore em,
.codex-lore i {
  color: var(--accent);
}

.codex-lore a {
  color: var(--accent);
  text-decoration: underline;
}

.codex-lore ul,
.codex-lore ol {
  margin: 0 0 12px;
  padding-left: 22px;
}

.codex-lore blockquote {
  margin: 12px 0;
  padding-left: 14px;
  border-left: 3px solid var(--accent-strong);
  font-style: italic;
}

.codex-lore img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  margin: 10px 0;
  display: block;
}

.stat-portrait {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  margin: 0 0 16px;
  display: block;
}

.stat-title {
  font-size: 1.6rem;
  margin: 0 0 2px;
}

.stat-subtitle {
  color: var(--text-dim);
  font-style: italic;
  margin: 0 0 14px;
  font-size: 0.92rem;
}

.stat-divider {
  height: 1px;
  background: linear-gradient(90deg, var(--accent-strong), transparent 70%);
  margin: 12px 0;
  opacity: 0.6;
}

.stat-line {
  margin: 4px 0;
  font-size: 0.92rem;
  line-height: 1.5;
}

.stat-line b {
  color: var(--text);
}

.ability-table {
  width: 100%;
  border-collapse: collapse;
  margin: 14px 0;
  text-align: center;
}

.ability-table th {
  font-size: 0.72rem;
  color: var(--accent);
  font-weight: 600;
  padding-bottom: 4px;
  letter-spacing: 0.04em;
}

.ability-table td {
  font-size: 0.88rem;
  padding: 2px 0;
}

.stat-section-title {
  font-size: 1.05rem;
  margin: 20px 0 8px;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
  padding-bottom: 4px;
}

.entry {
  margin: 0 0 10px;
  font-size: 0.92rem;
  line-height: 1.55;
}

.entry b {
  color: var(--text);
}

.entry-list {
  margin: 4px 0 0;
  padding-left: 20px;
}

.entry .meta-badge {
  font-size: 0.7rem;
  color: var(--gold);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  margin-left: 6px;
}

.legendary-intro,
.lair-intro {
  font-size: 0.88rem;
  color: var(--text-dim);
  font-style: italic;
  margin: 0 0 10px;
}

.damage-line {
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-top: 2px;
}

.offline-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--accent-strong);
  color: var(--parchment);
  text-align: center;
  font-size: 0.82rem;
  padding: 8px;
  z-index: 20;
}

@media (max-width: 480px) {
  .app-header {
    padding: 14px 16px;
  }

  .view {
    padding: 16px 14px;
  }

  .codex-page {
    padding: 20px 16px 26px;
  }
}

/* ---------- Print (A4 stat block) ---------- */

@page {
  size: A4;
  margin: 14mm;
}

@media print {

  .app-header,
  .offline-banner,
  #view-list,
  .detail-toolbar {
    display: none !important;
  }

  html,
  body {
    background: #fff;
    color: #111;
  }

  .view,
  .detail-view {
    max-width: none;
    padding: 0;
    margin: 0;
  }

  /* Some browsers' print dialogs (e.g. Chrome "Margins: None") ignore the
     @page margin above, so pad the content itself to keep breathing room
     regardless of the user's print-dialog margin setting. */
  .codex-page {
    background: #fff;
    color: #111;
    border: none;
    box-shadow: none;
    padding: 10mm 8mm;
  }

  .codex-quote,
  .codex-lore,
  .stat-subtitle,
  .damage-line,
  .legendary-intro,
  .lair-intro {
    color: #333;
  }

  .codex-lore em,
  .codex-lore i {
    color: #111;
  }

  .stat-title,
  .stat-section-title,
  .entry b,
  .stat-line b {
    color: #111;
  }

  .stat-section-title {
    border-bottom-color: #999;
  }

  .stat-divider {
    background: #999;
    opacity: 1;
  }

  .ability-table th {
    color: #111;
  }

  .entry .meta-badge,
  .cr-badge {
    color: #111;
    border-color: #999;
  }

  .stat-portrait,
  .creature-card {
    break-inside: avoid;
  }

  .stat-section-title {
    break-after: avoid;
  }

  .entry {
    break-inside: avoid;
  }
}

.cr-filter {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px;
  font-size: 0.85rem;
}

.cr-select {
  background: transparent;
  border: 1px solid currentColor;
  border-radius: 6px;
  padding: 4px 8px;
  color: inherit;
}

.cr-clear {
  background: none;
  border: none;
  cursor: pointer;
  color: inherit;
  opacity: 0.7;
  font-size: 0.8rem;
}

.cr-clear:hover {
  opacity: 1;
}