/* ==========================================================================
   grimoire.css — the Grimoire section: a filterable, reference-only spell
   browser. Bestiary's sibling under Lore — SAME layout (filter sidebar /
   collapsible drawer + a responsive card grid), spell data instead of
   monster data. Adapted 1:1 from bestiary.css (rename bestiary -> grimoire,
   bestiary-card -> spell-card); keep the two files in sync on any shared
   layout/chrome change.

   UNLIKE the Bestiary, a spell card has no PER-SPELL art — the
   .spell-card__media well shows an inline-SVG category glyph by default, but
   (like Bestiary's token image) an admin-uploaded category override renders
   as .spell-card__img instead, glyph as its img-fallback.js onerror target.

   No new design tokens are introduced — all color/space/type values come from
   base.css custom properties.
   ========================================================================== */

/* ---- Page header: title on the LEFT, count + sort clustered on the RIGHT,
   matching bestiary__page-head / the Encounters page header. ---------------- */

.grimoire__page-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-md);
  flex-wrap: wrap;
  margin: 0;
  padding-bottom: var(--space-sm);
  border-bottom: 1px solid var(--color-border);
}

.grimoire__head-left {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--space-xs) var(--space-md);
  min-width: 0;
}

.grimoire__page-head .grimoire__title {
  margin: 0;
}

.grimoire__head-right {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--space-xs) var(--space-md);
  margin-left: auto;
  min-width: 0;
}

/* ---- Layout: sidebar + main ------------------------------------------------ */

.grimoire__layout {
  display: grid;
  grid-template-columns: 17rem 1fr;
  gap: var(--space-lg);
  align-items: start;
}

.grimoire__layout--collapsed {
  grid-template-columns: min-content minmax(0, 1fr);
}

.grimoire__layout--collapsed .grimoire__panel-body {
  display: none;
}

.grimoire__layout--collapsed .grimoire__panel-title,
.grimoire__layout--collapsed .grimoire__clear {
  display: none;
}

.grimoire__layout--collapsed .grimoire__panel {
  max-height: none;
  overflow: visible;
}

.grimoire__panel {
  position: sticky;
  top: var(--space-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  padding: var(--space-md);
  border: 1px solid var(--color-border);
  background-color: var(--color-surface);
  max-height: calc(100vh - 2 * var(--space-md));
  overflow-y: auto;
}

.grimoire__panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
}

.grimoire__collapse {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  flex: 1 1 auto;
  min-width: 0;
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
  color: inherit;
  text-align: left;
}

.grimoire__collapse-chevron {
  width: 0.85rem;
  height: 0.85rem;
  flex: none;
  color: var(--color-text-muted);
  transition: transform var(--transition-fast, 0.12s ease);
}

.grimoire__collapse[aria-expanded="true"] .grimoire__collapse-chevron {
  transform: rotate(-90deg);
}

.grimoire__collapse:hover .grimoire__collapse-chevron,
.grimoire__collapse:focus-visible .grimoire__collapse-chevron {
  color: var(--color-accent);
}

.grimoire__panel-title {
  margin: 0;
  font-family: var(--font-heading-ui);
  font-size: 0.95rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-heading);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.grimoire__active-dot {
  width: 0.5rem;
  height: 0.5rem;
  flex: none;
  border-radius: 999px;
  background-color: var(--color-accent);
}

.grimoire__active-dot[hidden] {
  display: none;
}

.grimoire__panel-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  min-width: 0;
}

.grimoire__clear {
  font-family: var(--font-heading-ui);
  font-size: 0.72rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-accent);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.1rem 0.2rem;
}

.grimoire__clear:hover:not(:disabled),
.grimoire__clear:focus-visible:not(:disabled) {
  color: var(--color-accent-hover);
}

.grimoire__clear:disabled {
  color: var(--color-text-muted);
  opacity: 0.4;
  cursor: default;
}

.grimoire__search {
  margin-bottom: var(--space-xs);
}

/* ---- Filter groups (collapsible <details>) -------------------------------- */

.grimoire__group {
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-xs);
}

.grimoire__group-summary {
  font-family: var(--font-heading-ui);
  font-size: 0.78rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 0.35rem 0;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.grimoire__group-summary::-webkit-details-marker { display: none; }

.grimoire__group-summary::after {
  content: "+";
  font-family: var(--font-mono);
  color: var(--color-text-muted);
}

.grimoire__group[open] > .grimoire__group-summary::after {
  content: "–";
}

.grimoire__group-summary:hover,
.grimoire__group[open] > .grimoire__group-summary {
  color: var(--color-text);
}

.grimoire__group-body {
  padding: var(--space-xs) 0 var(--space-sm);
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.grimoire__group-note {
  margin: 0;
  font-size: 0.72rem;
  font-style: italic;
  color: var(--color-text-muted);
}

/* ---- Checkboxes ----------------------------------------------------------- */

.grimoire__checks {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.grimoire__checks--inline {
  flex-direction: row;
  flex-wrap: wrap;
  gap: var(--space-sm);
}

.grimoire__check {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  font-size: 0.85rem;
  color: var(--color-text);
  cursor: pointer;
}

.grimoire__check input {
  accent-color: var(--color-accent);
  cursor: pointer;
  flex: none;
}

.grimoire__check-label {
  flex: 1 1 auto;
  min-width: 0;
}

.grimoire__checks--inline .grimoire__check-label {
  flex: 0 0 auto;
}

.grimoire__check-count {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--color-text-muted);
  flex: none;
}

/* ---- Main column: result bar + grid --------------------------------------- */

.grimoire__main {
  min-width: 0;
}

.grimoire__count {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--color-text);
  align-self: center;
}

.grimoire__sort {
  flex: none;
  min-width: 11rem;
  align-self: center;
}

/* Result grid — same auto-fill model as Bestiary/Treasure so every browse
   surface tiles identically. */
.grimoire__results {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: var(--space-md);
  align-items: start;
  min-width: 0;
}

.grimoire__results > * {
  min-width: 0;
}

.grimoire__results.is-empty {
  display: block;
}

/* ---- Reference card --------------------------------------------------------
   Self-contained card styled 1:1 with .bestiary-card: a square media well
   (a category glyph — NEVER an image, spells carry no art), the spell name as
   a stretched-link button inside an <h3>, a Tier/Range/Duration stat line, a
   class+category tag row, and a line-clamped description preview.
   position:relative anchors the button's stretched ::after overlay. -------- */

.spell-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  padding: var(--space-md);
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-accent);
  background-color: var(--color-surface);
  cursor: pointer;
  transition: background-color 0.12s ease, border-color 0.12s ease;
  min-width: 0;
}

.spell-card:hover,
.spell-card:focus-within {
  background-color: var(--color-surface-hover);
  border-color: var(--color-accent);
}

.spell-card__header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-md);
  min-width: 0;
}

/* Media well — a square frame (OSR sharp corners) holding ONLY the category
   glyph. Matches the Bestiary/Treasure card's media well dimensions. */
.spell-card__media {
  position: relative;
  flex: none;
  width: 3.75rem;
  height: 3.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--color-oracle-bg, var(--color-surface));
  border: 1px solid var(--color-border);
  overflow: hidden;
}

.spell-card__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 50% 20%;
  display: block;
}

.spell-card__emblem {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--color-text-muted);
}

.spell-card__emblem svg {
  width: 55%;
  height: 55%;
  opacity: 0.7;
}

.spell-card__heading {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.spell-card__name {
  margin: 0;
  font-family: var(--font-heading-ui);
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--color-heading);
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.spell-card__open {
  display: inline;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  letter-spacing: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

.spell-card__open::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
}

.spell-card__open:focus-visible {
  outline-offset: 2px;
}

/* Compact stat line under the name (Tier / Range / Duration). */
.spell-card__stats {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs) var(--space-sm);
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--color-text);
}

.spell-card__stat-label {
  font-family: var(--font-heading-ui);
  font-size: 0.6rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-text-muted);
}

/* Tags row (class + category). */
.spell-card__tags {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
}

.spell-card__tag {
  font-family: var(--font-heading-ui);
  font-size: 0.65rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  padding: 0.08rem 0.4rem;
  white-space: nowrap;
}

.spell-card__tag--category {
  color: var(--color-accent);
  border-color: var(--color-accent);
}

/* Compact preview only — the full description lives in the spell-card modal.
   Clamp to 3 lines so a long blurb can't drive a card into a full-width row. */
.spell-card__desc {
  margin: 0;
  font-family: var(--font-body);
  font-size: 0.9rem;
  color: var(--color-text-muted);
  line-height: var(--lh-body);
  min-width: 0;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  overflow: hidden;
}

/* ---- Grimoire / My Spells tabs --------------------------------------------
   Mirrors the .bestiary__tabs OSR pattern (saved.css: muted label, accent-
   underline active tab, bottom rule) — each page owns its own copy rather than
   sharing the bestiary-prefixed class (admin.css's .admin-tabs is the same
   precedent). No new tokens. */
.grimoire__tabs {
  display: flex;
  gap: var(--space-xs);
  margin-bottom: var(--space-md);
  border-bottom: 1px solid var(--color-border);
}

.grimoire__tab {
  appearance: none;
  background: none;
  border: 1px solid transparent;
  border-bottom: none;
  color: var(--color-text-muted);
  font-family: var(--font-heading-ui);
  font-size: 0.85rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--ls-cinzel-caps);
  padding: 0.5rem 1rem;
  cursor: pointer;
  margin-bottom: -1px;
  transition: color var(--transition-fast), border-color var(--transition-fast);
}

.grimoire__tab:hover,
.grimoire__tab:focus-visible {
  color: var(--color-accent);
}

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

.grimoire__tab.is-active {
  color: var(--color-accent);
  border-color: var(--color-border);
  border-bottom: 2px solid var(--color-accent);
}

/* ---- Saved-spell card (My Spells tab) --------------------------------------
   The My Spells library renders the SAME .spell-card as the browse list
   (buildSavedSpellCard in grimoire.js), painted into the shared .char-card-grid
   (components.css). The only extra chrome is a raised corner Delete that sits
   ABOVE the card's stretched open overlay (z-index:1 on .spell-card__open::after)
   so its own click doesn't also open the row. Mirrors .bestiary-card--saved /
   .bestiary-card__delete 1:1. */
.spell-card--saved {
  padding-bottom: calc(var(--space-md) + 1.6rem);
}

.spell-card__delete {
  position: absolute;
  z-index: 2;
  bottom: var(--space-sm);
  right: var(--space-sm);
  padding: 0.15rem 0.55rem;
  font-size: 0.7rem;
  line-height: 1.4;
}

/* ---- Empty state ---------------------------------------------------------- */

.grimoire__empty {
  text-align: center;
  padding: var(--space-2xl) var(--space-md);
  border: 1px dashed var(--color-border);
  background-color: var(--color-surface);
}

.grimoire__empty-title {
  font-family: var(--font-heading);
  font-size: 1.3rem;
  color: var(--color-heading);
  margin: 0 0 var(--space-xs);
}

.grimoire__empty-sub {
  color: var(--color-text-muted);
  font-style: italic;
  margin: 0 0 var(--space-md);
}

/* ---- Loading state ---------------------------------------------------------
   Shared animated-text idiom (`.roster-loading` family) — a pulsing muted
   line, never a spinner. This wrapper just centers that line. */

.grimoire__loading {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

/* ---- Mobile filter toggle (hidden on desktop) ----------------------------- */

.grimoire__filter-toggle {
  display: none;
  align-items: center;
  gap: var(--space-xs);
  font-family: var(--font-heading-ui);
  font-size: 0.8rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-text);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  padding: 0.5rem 0.85rem;
  cursor: pointer;
}

.grimoire__filter-toggle svg {
  width: 1rem;
  height: 1rem;
  flex: none;
}

.grimoire__filter-toggle-count {
  color: var(--color-accent);
  font-size: 1rem;
  line-height: 1;
}

.grimoire__filter-toggle:hover,
.grimoire__filter-toggle:focus-visible {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

/* ==========================================================================
   Responsive — below 820px the filter rail collapses into a top drawer toggled
   by the Filters button. The panel becomes a full-width block above results.
   ========================================================================== */

@media (max-width: 820px) {
  .grimoire__layout,
  .grimoire__layout--collapsed {
    grid-template-columns: 1fr;
  }

  .grimoire__layout--collapsed .grimoire__panel-body {
    display: flex;
  }

  .grimoire__layout--collapsed .grimoire__panel-title {
    display: inline;
  }

  .grimoire__results {
    grid-template-columns: 1fr;
  }

  .grimoire__filter-toggle {
    display: inline-flex;
    margin-bottom: var(--space-sm);
  }

  .grimoire__panel {
    position: static;
    max-height: none;
    display: none;
    margin-bottom: var(--space-md);
  }

  .grimoire--filters-open .grimoire__panel {
    display: flex;
  }

  .grimoire__page-head {
    flex-direction: column;
    align-items: stretch;
  }

  .grimoire__head-right {
    margin-left: 0;
  }

  .grimoire__sort {
    min-width: 0;
    flex: 1 1 auto;
  }
}

@media (max-width: 480px) {
  .grimoire__results {
    grid-template-columns: 1fr;
  }
}

/* ==========================================================================
   Reduced motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  .grimoire__filter-toggle,
  .grimoire__clear,
  .grimoire__collapse-chevron,
  .spell-card {
    transition: none;
  }
}
