/*
 * Cite module. Feature-specific styles only. Shared components (tabs, modal,
 * toast, ghost buttons) get fallbacks wrapped in :where(), which has zero
 * specificity: any rule the global stylesheet writes for .tab, .modal or .toast
 * outranks them, while they still beat the bare "*" reset by coming later.
 */

:where(.cite-widget .tabs) { display: flex; gap: 2px; border-bottom: 1px solid var(--border); }
:where(.cite-widget .tab) {
  appearance: none; background: none; border: 0; border-bottom: 2px solid transparent;
  margin: 0 0 -1px; padding: .65em .95em; font: inherit; font-size: .9rem; font-weight: 600;
  color: var(--ink-soft); cursor: pointer; border-radius: 6px 6px 0 0; white-space: nowrap;
}
:where(.cite-widget .tab:hover) { color: var(--primary); background: color-mix(in srgb, var(--primary) 7%, transparent); }
:where(.cite-widget .tab[aria-selected="true"]) { color: var(--primary); border-bottom-color: var(--primary); }
:where(.cite-widget .tab:focus-visible) { outline: 2px solid var(--primary); outline-offset: -2px; }
:where(dialog.modal) {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 0;
  background: var(--card); color: var(--ink); box-shadow: 0 24px 64px rgba(10, 16, 30, .28);
  width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 48px); margin: auto;
}
:where(dialog.modal)::backdrop { background: rgba(10, 16, 30, .55); }
:where(.toast) {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 1000;
  background: var(--ink); color: var(--card); padding: .65em 1.1em; border-radius: 8px;
  box-shadow: 0 8px 24px rgba(10, 16, 30, .25); font-size: .9rem; max-width: calc(100vw - 32px);
}
:where(.toast[hidden]) { display: none; }
:where(.btn-ghost) { background: transparent; border-color: transparent; }
:where(.btn-ghost:hover) { background: color-mix(in srgb, var(--ink) 6%, transparent); }

/* ---------- Record page: "Cite this record" ---------- */

.cite-section { scroll-margin-top: 84px; }
.cite-widget {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden;
}
.cite-tabs { padding: 0 10px; overflow-x: auto; scrollbar-width: none; flex-wrap: nowrap; }
/* Neither scrolling strip may widen the page: inline-size containment keeps their
   content out of the record column's min-content width on phones. */
.cite-tabs, .cite-bib-code { contain: inline-size; }
.cite-tabs::-webkit-scrollbar { display: none; }
.cite-tabs.is-scrollable:not(.at-end) {
  -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent);
  mask-image: linear-gradient(to right, #000 calc(100% - 40px), transparent);
}
.cite-panel { padding: 16px 18px 14px; }
.cite-panel[hidden] { display: none; }
.cite-text {
  margin: 0; color: var(--ink); font-size: .97rem; line-height: 1.65;
  padding-left: 1.6em; text-indent: -1.6em; overflow-wrap: anywhere;
}
.cite-panel-foot { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 14px; margin-top: 14px; }
.cite-icon { flex: none; vertical-align: -3px; }
.cite-copy, .cite-open, .cite-export-link { display: inline-flex; align-items: center; gap: 6px; }
.cite-copy.is-copied,
.cite-copy.is-copied:hover { background: var(--accent); border-color: var(--accent); color: #fff; }

.cite-export {
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px 14px; padding: 12px 18px;
  border-top: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 55%, var(--card));
}
.cite-export p { margin: 0; }
.cite-export-label { font-size: .74rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.cite-export-list { list-style: none; display: flex; flex-wrap: wrap; gap: 8px; margin: 0; padding: 0; }

.cite-bib { border-top: 1px solid var(--border); padding: 10px 18px; }
.cite-bib > summary { cursor: pointer; font-weight: 600; font-size: .9rem; color: var(--primary); width: fit-content; }
.cite-bib > summary:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; border-radius: 4px; }
.cite-bib-code {
  margin: 10px 0; padding: 12px 14px; max-height: 320px; overflow: auto;
  background: var(--bg); color: var(--ink); border: 1px solid var(--border); border-radius: 8px;
  font: .8rem/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre;
}
.cite-connect { margin: 0; padding: 0 18px 14px; }
.cite-bib + .cite-connect { padding-top: 2px; }

/* ---------- Cite dialog ---------- */

.cite-dialog-inner { padding: 18px 20px 20px; }
.cite-dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.cite-dialog-head h2 { margin: 0; font-size: 1.3rem; }
.cite-dialog-close {
  display: inline-flex; align-items: center; justify-content: center; padding: .4em; line-height: 1;
  background: transparent; border-color: transparent; color: var(--ink-soft);
}
.cite-dialog-close:hover { background: color-mix(in srgb, var(--ink) 7%, transparent); border-color: transparent; color: var(--ink); }
.cite-dialog-sub { margin: 4px 0 14px; }
.cite-dialog .cite-widget { box-shadow: none; }
.cite-dialog .cite-connect { display: none; }
.cite-dialog .cite-bib-code { max-height: 220px; }
body:has(dialog.cite-dialog[open]) { overflow: hidden; }

@media (max-width: 600px) {
  dialog.cite-dialog {
    width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0;
    border-radius: 16px 16px 0 0; border-bottom: 0;
  }
  .cite-dialog-inner { padding: 14px 14px 18px; }
  .cite-panel, .cite-export, .cite-bib { padding-left: 14px; padding-right: 14px; }
  .cite-connect { padding-left: 14px; padding-right: 14px; }
  .cite-text { font-size: .94rem; }
}

/* ---------- Listing pages: "Export results" ---------- */

.cite-count-row { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 6px 16px; }
.cite-count-row > .cite-export-menu { margin-left: auto; }
.cite-export-menu { position: relative; }
.cite-export-menu > summary {
  list-style: none; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  font-size: .85rem; line-height: 1.4; padding: .15em .75em;
}
.cite-export-menu > summary::-webkit-details-marker { display: none; }
@media (max-width: 480px) { .cite-export-menu .cite-wide-only { display: none; } }
.cite-export-menu > summary:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.cite-export-pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 40; width: 280px; max-width: calc(100vw - 32px);
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 14px 36px rgba(10, 16, 30, .16); padding: 6px;
}
.cite-export-pop p { margin: 0; padding: 6px 10px 6px; font-size: .76rem; color: var(--muted); }
.cite-export-pop a {
  display: flex; flex-direction: column; gap: 1px; padding: 8px 10px; border-radius: 6px;
  color: var(--ink); font-weight: 600; font-size: .9rem; text-decoration: none;
}
.cite-export-pop a span { font-size: .76rem; font-weight: 400; color: var(--muted); }
.cite-export-pop a:hover, .cite-export-pop a:focus-visible { background: color-mix(in srgb, var(--primary) 8%, var(--card)); outline: none; }
.cite-export-pop hr { border: 0; border-top: 1px solid var(--border); margin: 4px 6px; }

/* ---------- /export page ---------- */

.cite-format-set { border: 0; margin: 0; padding: 0; min-width: 0; }
.cite-format-set legend { font-weight: 600; font-size: .92rem; margin-bottom: 8px; padding: 0; }
.cite-format-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.cite-format-option {
  position: relative; display: flex; flex-direction: column; gap: 2px; padding: 12px 14px 12px 40px;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer;
}
.cite-format-option input { position: absolute; left: 14px; top: 15px; margin: 0; accent-color: var(--primary); }
.cite-format-option:has(input:checked) { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.cite-format-option:focus-within { outline: 2px solid var(--primary); outline-offset: 2px; }
.cite-format-name { font-weight: 600; font-size: .95rem; }
.cite-export-help { margin-top: 2.6rem; }
.cite-export-help h2 { font-size: 1.15rem; margin: 1.6em 0 .6em; }
.cite-help-list { margin: 0 0 0 1.2em; color: var(--ink-soft); }
.cite-help-list li { margin-bottom: .5em; }
.cite-api-list dd { margin-bottom: .4em; }
.cite-api-list code { word-break: break-all; }

.cite-sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

@media (prefers-reduced-motion: no-preference) {
  .cite-copy { transition: background-color .15s ease, border-color .15s ease, color .15s ease; }
}
