/* ==========================================================================
   treasure.css — the Treasure section: a filterable, reference-only browser
   over the full 145-item unified treasure catalog (equipment + magic items).

   Layout MIRRORS the Bestiary (bestiary.css): a sticky filter rail (desktop) /
   collapsible drawer (mobile) on the left + a responsive grid of unified item
   cards on the right, with a page head (title + live count + sort + Clear). The
   structure and most rules are intentionally a 1:1 analogue of the Bestiary's
   so the two pages feel identical; class names use the treasure__* / treasure-
   card__* family. The detail modal reuses .action-modal / .enc-info-modal from
   encounter.css verbatim (via showInfoModal in treasure.js).

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

/* ---- Page header: title (left) + count/sort cluster (right) --------------- */

.treasure__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);
}

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

.treasure__page-head .treasure__title {
  margin: 0;
}

.treasure__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 ------------------------------------------------ */

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

/* Collapsed (desktop): the rail shrinks to a thin spine (its head only) and the
   results reflow into the freed width — the grid flips to `min-content 1fr`.
   Mirrors the shared roster_filter rail's rf-layout--collapsed behavior. */
.treasure__layout--collapsed {
  grid-template-columns: min-content minmax(0, 1fr);
}

.treasure__layout--collapsed .treasure__panel-body {
  display: none;
}

/* On the thin spine the chevron + active dot carry the meaning; keep the title
   for screen readers but visually hide it so the spine stays narrow. */
.treasure__layout--collapsed .treasure__panel-title,
.treasure__layout--collapsed .treasure__clear {
  display: none;
}

.treasure__layout--collapsed .treasure__panel {
  max-height: none;
  overflow: visible;
}

.treasure__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;
}

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

/* The chevron collapse trigger wraps the chevron + the title + the active dot so
   the whole head row reads as a heading but behaves as a button (mirrors the
   shared rf-collapse). */
.treasure__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;
}

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

/* Expanded: chevron points down (rotate the left-pointing glyph). Collapsed: it
   keeps its authored left/right direction. */
.treasure__collapse[aria-expanded="true"] .treasure__collapse-chevron {
  transform: rotate(-90deg);
}

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

.treasure__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;
}

/* Active-filter dot — a small accent pip that stays lit (even on the collapsed
   spine) whenever any filter is applied, so a narrowed roster still reads as
   filtered. */
.treasure__active-dot {
  width: 0.5rem;
  height: 0.5rem;
  flex: none;
  border-radius: 999px;
  background-color: var(--color-accent);
}

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

/* The body wraps search + the Crawling Kit toggle + facet groups so one collapse
   hides them all with no layout jump (re-establishes the column flow/gap the
   panel gave those children directly, so expanded looks identical to before). */
.treasure__panel-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  min-width: 0;
}

.treasure__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;
}

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

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

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

/* ---- Crawling Kit quick toggle — a prominent single control above the groups
   (Ken's quick-access affordance). Reads as actionable; active = accent. ----- */

.treasure__kit-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  width: 100%;
  font-family: var(--font-heading-ui);
  font-size: 0.78rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-text);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-border);
  padding: 0.5rem 0.7rem;
  cursor: pointer;
  transition: color 0.12s ease, border-color 0.12s ease, background-color 0.12s ease;
}

.treasure__kit-toggle svg {
  width: 1.1rem;
  height: 1.1rem;
  flex: none;
}

/* Component count, pushed to the trailing edge — makes it read as "show these N
   contained items", not "isolate the single kit entry". */
.treasure__kit-toggle-count {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--color-text-muted);
  flex: none;
}

.treasure__kit-toggle.is-active .treasure__kit-toggle-count {
  color: var(--color-accent);
}

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

.treasure__kit-toggle.is-active {
  color: var(--color-accent);
  border-color: var(--color-accent);
  border-left-color: var(--color-accent);
  background-color: var(--color-surface-hover);
}

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

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

.treasure__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;
}

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

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

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

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

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

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

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

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

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

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

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

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

.treasure__checks--inline .treasure__check-label {
  flex: 0 0 auto;
}

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

/* ---- Range (min/max) ------------------------------------------------------ */

.treasure__range {
  display: flex;
  align-items: flex-end;
  gap: var(--space-xs);
}

.treasure__range-field {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  flex: 1 1 0;
  min-width: 0;
}

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

.treasure__range-input {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  padding: 0.3rem 0.4rem;
}

.treasure__range-sep {
  color: var(--color-text-muted);
  padding-bottom: 0.4rem;
}

/* ---- Main column: count, sort, grid --------------------------------------- */

.treasure__main {
  min-width: 0;
}

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

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

/* Result grid — same auto-fill model as the Bestiary. The track minimum is a
   touch under the Bestiary's 17.5rem so two columns actually tile inside the
   ~578px main column the global 960px content cap leaves on desktop (the
   Bestiary's 17.5rem lands one px over and collapses to a single wide column);
   the compact, line-clamped cards read well at this width. min-width:0 lets the
   grid resolve tracks against the available inline space rather than the widest
   card's max-content — without it a child that can't shrink (a nowrap tag, a
   mono stat) inflates 1fr and collapses auto-fill back to one column. */
.treasure__results {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
  gap: var(--space-md);
  align-items: start;
  min-width: 0;
}

/* Grid children must also permit shrinking below their content size, or they
   re-inflate the track they sit in (classic CSS-grid/flex overflow fix). */
.treasure__results > * {
  min-width: 0;
}

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

/* ---- Item card ------------------------------------------------------------
   The card reuses the Bestiary's "clickable card" pattern: plain semantic markup
   with a stretched-link button inside the name <h3>. position:relative anchors
   the button's stretched ::after overlay. ----------------------------------- */

.treasure-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;
  /* Allow the card to shrink within its grid track instead of forcing the
     track to its (large) content width. */
  min-width: 0;
}

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

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

/* Icon well — a square frame (OSR sharp corners). The magic-item PNG fills it
   (cover); the resolver glyph PNG sits inside with breathing room; the inline
   SVG fallback centers a tasteful category mark. */
.treasure-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;
}

.treasure-card__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Resolver glyph PNGs are white line-art on transparent — inset them slightly so
   they don't crowd the frame, and don't crop them (contain, not cover). */
.treasure-card__img--glyph {
  object-fit: contain;
  padding: 0.4rem;
}

.treasure-card__glyph {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--color-text-muted);
}

.treasure-card__glyph svg {
  width: 55%;
  height: 55%;
  opacity: 0.7;
}

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

.treasure-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;
  /* A long single-word name must break rather than widen the track. */
  overflow-wrap: anywhere;
}

/* The open control — visually just the item name, but a genuine focusable,
   screen-reader-announced button. Reset UA chrome and let it sit inline. */
.treasure-card__open {
  display: inline;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  letter-spacing: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

/* Stretch the button's hit area over the entire card ("stretched link"). */
.treasure-card__open::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
}

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

/* Compact stat line under the name (DMG / AC / Cost / Slots). */
.treasure-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);
}

.treasure-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);
}

/* The attribute "i" tooltip rides on the stat label. Raise it above the card's
   stretched open-overlay (z-index:1 on .treasure-card__open::after) so the info
   button stays independently clickable. */
.treasure-card__stat-label .info-icon {
  position: relative;
  z-index: 2;
}

/* Magic base-item restatement, e.g. "+2 Greataxe" — sits between the stat line
   and the tags. Quiet accent so it reads as a subtitle, not a competing stat. */
.treasure-card__base {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-style: italic;
  color: var(--color-accent);
}

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

.treasure-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;
}

.treasure-card__tag--magic {
  color: var(--color-accent);
  border-color: var(--color-accent);
}

/* Homebrew badge — marks custom items built in Forge > Items. Old-coin gold so
   it reads as "yours, not core" and stays distinct from the accent-blue Magic tag. */
.treasure-card__tag--homebrew {
  color: var(--color-luck);
  border-color: var(--color-luck);
}

/* Cursed badge — danger-red so a cursed item reads as a hazard at a glance,
   distinct from the accent-blue Magic and gold Homebrew tags. */
.treasure-card__tag--cursed {
  color: var(--color-danger);
  border-color: var(--color-danger);
  background-color: var(--color-danger-bg);
}

.treasure-sheet__k--cursed {
  color: var(--color-danger);
}

/* Compact preview only — the full description lives in the detail modal. Clamp
   to 3 lines so a long item blurb can't drive a card into a full-width row (the
   layout driver behind the desktop one-wide-column / mobile overflow bug). The
   line-clamp also caps height; min-width:0 + overflow-wrap keep long tokens in
   bounds. */
.treasure-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;
}

/* ---- Detail sheet (inside the info modal) --------------------------------- */

.treasure-sheet {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.treasure-sheet__header {
  display: flex;
  align-items: flex-start;
  gap: var(--space-md);
}

.treasure-sheet__media {
  position: relative;
  flex: none;
  width: 6rem;
  height: 6rem;
  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;
}

.treasure-sheet__media .treasure-card__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.treasure-sheet__media .treasure-card__img--glyph {
  object-fit: contain;
  padding: 0.6rem;
}

.treasure-sheet__media .treasure-card__glyph svg {
  width: 55%;
  height: 55%;
}

.treasure-sheet__header-main {
  flex: 1 1 auto;
  min-width: 0;
}

.treasure-sheet__meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs) var(--space-md);
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--color-text);
}

.treasure-sheet__k {
  font-family: var(--font-heading-ui);
  font-size: 0.62rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-text-muted);
}

/* Supplementary one-line effect highlight above the full description. The inline
   "Effect" key reuses the .treasure-sheet__k cap style; the body sits on the
   accent-tinted surface so it reads as a pull-out, not a duplicate of the prose. */
.treasure-sheet__effect {
  margin: 0;
  font-family: var(--font-body);
  font-size: 0.92rem;
  color: var(--color-text);
  line-height: var(--lh-body);
  padding: var(--space-xs) var(--space-sm);
  border-left: 3px solid var(--color-accent);
  background-color: var(--color-surface);
}

.treasure-sheet__effect .treasure-sheet__k {
  display: block;
  margin-bottom: 0.1rem;
}

.treasure-sheet__desc {
  margin: 0;
  font-family: var(--font-body);
  color: var(--color-text);
  line-height: var(--lh-body);
}

.treasure-sheet__section {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.treasure-sheet__h {
  margin: 0;
  font-family: var(--font-heading-ui);
  font-size: 0.78rem;
  letter-spacing: var(--ls-cinzel-caps);
  text-transform: uppercase;
  color: var(--color-heading);
}

.treasure-sheet__tags {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-xs);
}

.treasure-sheet__tag {
  font-family: var(--font-heading-ui);
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--color-text-muted);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  padding: 0.1rem 0.45rem;
}

.treasure-sheet__source {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  font-style: italic;
  color: var(--color-text-muted);
}

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

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

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

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

/* ---- Loading state (#8) ---------------------------------------------------
   The catalog fetch can be slow. The busy read is the SHARED animated-text idiom
   (`.roster-loading`, styled in roster_filter.css) — a pulsing muted line, never
   a rotating spinner (Alex Do-Not #11). This wrapper just centers that line in
   the parchment panel. ------------------------------------------------------ */

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

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

.treasure__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;
}

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

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

.treasure__filter-toggle:hover,
.treasure__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 (mirrors the Bestiary breakpoint exactly).
   ========================================================================== */

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

  /* The desktop rail-collapse spine doesn't apply on the stacked drawer layout:
     restore the body + title so the mobile Filters drawer shows its full panel. */
  .treasure__layout--collapsed .treasure__panel-body {
    display: flex;
  }

  .treasure__layout--collapsed .treasure__panel-title {
    display: inline;
  }

  /* Collapse the results to a single column at the drawer breakpoint. An
     auto-fill grid sizes its tracks from the container's min-content, and with
     the rail gone the cards' min-content (~2 columns) otherwise propagates back
     up through #app-content (width:100% but min-width-bound by content) and
     pushes the page wider than the viewport — the mobile horizontal-scroll bug.
     One explicit column lets the whole chain shrink to the viewport. */
  .treasure__results {
    grid-template-columns: 1fr;
  }

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

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

  .treasure--filters-open .treasure__panel {
    display: flex;
  }

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

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

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

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

@media (prefers-reduced-motion: reduce) {
  .treasure__filter-toggle,
  .treasure__clear,
  .treasure__kit-toggle,
  .treasure__collapse-chevron,
  .treasure-card {
    transition: none;
  }

}
