/* ------------------------------------------------------------------
   Smart Library — components: buttons, fields, cards, menus, dialog,
   palette, tables, notes, jobs, reader
   ------------------------------------------------------------------ */

/* ================= buttons ================= */
.btn {
  --btn-bg: var(--paper-2);
  --btn-ink: var(--ink);
  --btn-line: var(--rule-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--btn-line);
  border-radius: var(--r-md);
  background: var(--btn-bg);
  color: var(--btn-ink);
  font: inherit;
  font-size: var(--t-sm);
  font-weight: 500;
  line-height: 1;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.14s ease, border-color 0.14s ease, color 0.14s ease,
    transform 0.08s ease, box-shadow 0.14s ease;
}
.btn:hover { background: var(--paper-3); color: var(--ink); }
.btn:active { transform: translateY(1px); }
.btn:disabled,
.btn[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn .i { flex: none; }

.btn--primary {
  --btn-bg: var(--accent);
  --btn-ink: #fbfaf6;
  --btn-line: var(--accent);
  box-shadow: var(--sh-1);
}
.btn--primary:hover { --btn-bg: var(--accent-hover); --btn-line: var(--accent-hover); color: #fff; }

.btn--quiet {
  --btn-bg: transparent;
  --btn-line: transparent;
  --btn-ink: var(--ink-2);
  padding-inline: 10px;
}
.btn--quiet:hover { --btn-bg: var(--paper-3); color: var(--ink); }

.btn--sm { min-height: 32px; padding: 0 10px; font-size: var(--t-xs); }
.btn--lg { min-height: 46px; padding: 0 20px; font-size: 1rem; }
.btn--block { width: 100%; }

.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 15px; height: 15px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  color: var(--btn-ink);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* icon-only control */
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 38px; height: 38px;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
}
.icon-btn:hover { background: var(--paper-3); color: var(--ink); }

/* compact segmented control */
.segmented {
  display: inline-flex;
  padding: 2px;
  gap: 2px;
  border: 1px solid var(--rule-2);
  border-radius: var(--r-md);
  background: var(--paper-3);
}
.segmented button {
  min-height: 28px;
  padding: 0 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: var(--t-xs);
  cursor: pointer;
}
.segmented button[aria-pressed="true"] {
  background: var(--paper-2);
  color: var(--ink);
  box-shadow: var(--sh-1);
}

/* ================= fields ================= */
.field { display: grid; gap: 6px; min-width: 0; }
.field > label,
.field__label {
  font-size: var(--t-xs);
  font-weight: 500;
  color: var(--ink-2);
}
.field__hint { font-size: var(--t-xs); color: var(--muted); }
.field__error { font-size: var(--t-xs); color: var(--danger); }

input[type="text"],
input[type="url"],
input[type="email"],
input[type="number"],
input[type="search"],
input[type="password"],
select,
textarea {
  width: 100%;
  min-height: 38px;
  padding: 7px 10px;
  font: inherit;
  font-size: var(--t-sm);
  color: var(--ink);
  background: var(--paper-2);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-md);
  transition: border-color 0.14s ease, box-shadow 0.14s ease, background-color 0.14s ease;
}
textarea { min-height: 92px; resize: vertical; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: #9a8d7b; }
input:hover, select:hover, textarea:hover { border-color: #b6a68c; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--danger); }

select {
  appearance: none;
  padding-right: 30px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b5f50' stroke-width='2.2' stroke-linecap='round'%3E%3Cpath d='M5 9l7 7 7-7'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

input[type="file"] {
  width: 100%;
  font: inherit;
  font-size: var(--t-sm);
  color: var(--muted);
  padding: 0;
  border: 0;
  background: none;
  min-height: 0;
}
input[type="file"]::file-selector-button {
  font: inherit;
  font-size: var(--t-xs);
  font-weight: 500;
  margin-right: 10px;
  padding: 7px 12px;
  color: var(--ink);
  background: var(--paper-2);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-md);
  cursor: pointer;
}
input[type="file"]::file-selector-button:hover { background: var(--paper-3); }

.check {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  font-size: var(--t-sm);
  color: var(--ink-2);
}
.check input { margin: 2px 0 0; accent-color: var(--accent); width: 15px; height: 15px; }

/* drop zone */
.drop {
  display: grid;
  gap: 4px;
  justify-items: center;
  text-align: center;
  padding: var(--s-5) var(--s-4);
  border: 1px dashed var(--rule-2);
  border-radius: var(--r-lg);
  background: var(--paper-2);
  cursor: pointer;
  transition: border-color 0.14s ease, background-color 0.14s ease;
}
.drop:hover, .drop.is-over { border-color: var(--accent); background: var(--accent-soft); }
.drop .i { color: var(--accent); }
.drop__title { font-size: var(--t-sm); font-weight: 500; }
.drop__hint { font-size: var(--t-xs); color: var(--muted); }
.drop__file {
  margin-top: 6px;
  font-size: var(--t-xs);
  color: var(--ink-2);
  font-family: var(--font-mono);
  word-break: break-all;
}

/* ================= the search field ================= */
.search {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px 0 14px;
  min-height: 56px;
  background: var(--paper-2);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-lg);
  box-shadow: var(--sh-1);
  transition: border-color 0.14s ease, box-shadow 0.14s ease;
}
.search:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), var(--sh-1);
}
.search > .i { color: var(--muted); flex: none; }
.search input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 54px;
  padding: 0;
  border: 0;
  background: none;
  font-size: 1.0625rem;
  box-shadow: none;
}
.search input:focus { box-shadow: none; }
.search input::-webkit-search-cancel-button { margin-inline-end: 4px; }
.search__scope {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-left: 10px;
  border-left: 1px solid var(--rule);
}
.search__scope select {
  min-height: 32px;
  width: auto;
  max-width: 190px;
  border-color: transparent;
  background-color: transparent;
  font-size: var(--t-xs);
  color: var(--ink-2);
}
.search__scope select:hover { border-color: var(--rule-2); background-color: var(--paper-3); }
.search__key { flex: none; color: inherit; background: rgba(255, 255, 255, 0.16); border-color: rgba(255, 255, 255, 0.3); }
.search__go {
  flex: none;
  min-height: 40px;
  border-radius: var(--r-md);
}

@media (max-width: 700px) {
  /* Field on one row, scope + go on the next — a 140px input is useless. */
  .search {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "icon input input"
      "scope scope go";
    align-items: center;
    gap: 8px;
    min-height: 0;
    padding: 10px 10px 10px 12px;
  }
  .search > .i { grid-area: icon; }
  .search input { grid-area: input; min-height: 34px; min-width: 0; }
  .search__scope {
    grid-area: scope;
    min-width: 0;
    border-left: 0;
    padding-left: 0;
  }
  .search__scope select { max-width: none; flex: 1; }
  .search__key { display: none; }
  .search__go { grid-area: go; min-height: var(--tap); }
}

/* ================= chips, badges, tags ================= */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 7px;
  border-radius: var(--r-sm);
  font-size: var(--t-label);
  font-weight: 500;
  letter-spacing: 0.01em;
  line-height: 1.6;
  color: var(--ink-2);
  background: var(--paper-3);
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge--ok { color: var(--ok); background: var(--ok-soft); }
.badge--run { color: var(--accent-ink); background: var(--accent-soft); }
.badge--fail { color: var(--danger); background: var(--danger-soft); }
.badge--warn { color: var(--warn); background: var(--warn-soft); }
.badge--line { background: transparent; border-color: var(--rule-2); color: var(--muted); }

.dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}

/* ================= cards ================= */
.card {
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
}

/* ================= library card ================= */
.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: var(--s-3);
}

.lib-card {
  --lib-hue: 178;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-height: 176px;
  padding: var(--s-4);
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-radius: var(--r-lg);
  color: var(--ink);
  font: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.16s ease, transform 0.16s ease, box-shadow 0.16s ease;
}
.lib-card::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  background: hsl(var(--lib-hue) 30% 42% / 0.85);
  opacity: 0;
  transition: opacity 0.16s ease;
}
.lib-card:hover {
  border-color: var(--rule-2);
  transform: translateY(-2px);
  box-shadow: var(--sh-2);
  color: var(--ink);
}
.lib-card:hover::before { opacity: 1; }

.lib-card__icon {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: var(--r-md);
  color: hsl(var(--lib-hue) 34% 27%);
  background:
    radial-gradient(120% 120% at 20% 0%, hsl(var(--lib-hue) 40% 92%), hsl(var(--lib-hue) 24% 86%));
  border: 1px solid hsl(var(--lib-hue) 22% 78%);
  margin-bottom: var(--s-3);
}
.lib-card__icon svg { width: 25px; height: 25px; }

.lib-card__name {
  display: block;
  font-family: var(--font-display);
  font-size: 1.1875rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
  margin-bottom: 2px;
  overflow-wrap: anywhere;
}
.lib-card__meta {
  font-size: var(--t-xs);
  color: var(--muted);
  margin-top: auto;
  padding-top: var(--s-3);
  display: flex;
  align-items: center;
  gap: 6px;
}
.lib-card__desc {
  display: block;
  font-size: var(--t-xs);
  color: var(--muted);
  margin-top: 4px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.lib-card--new {
  align-items: flex-start;
  justify-content: center;
  border-style: dashed;
  background: transparent;
  color: var(--muted);
  gap: 4px;
}
.lib-card--new .lib-card__icon {
  background: transparent;
  border-style: dashed;
  color: var(--muted);
  width: 44px; height: 44px;
  margin-bottom: var(--s-2);
}

@media (max-width: 560px) {
  .lib-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s-3); }
  .lib-card { min-height: 190px; padding: var(--s-3); }
  .lib-card__icon { width: 46px; height: 46px; margin-bottom: var(--s-3); }
  .lib-card__icon svg { width: 25px; height: 25px; }
  .lib-card__name { font-size: 1rem; }
}

/* ================= lists / rows ================= */
.rows { display: grid; gap: 0; border-top: 1px solid var(--rule); }
.row-item {
  display: flex;
  align-items: baseline;
  gap: var(--s-3);
  padding: 9px var(--s-2);
  border-bottom: 1px solid var(--rule);
  text-decoration: none;
  color: var(--ink);
  font-size: var(--t-sm);
  min-height: 40px;
}
.row-item:hover { background: var(--paper-3); color: var(--ink); }
.row-item__main { flex: 1 1 auto; min-width: 0; }
.row-item__title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-item__sub { display: block; font-size: var(--t-xs); color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-item__aside { flex: none; font-size: var(--t-xs); color: var(--muted); }

/* ================= search hit (shared by home and library) ================= */
.hit {
  display: grid;
  gap: 5px;
  padding: var(--s-3) var(--s-2);
  border-bottom: 1px solid var(--rule);
  text-decoration: none;
  color: var(--ink);
}
.hit:hover { background: var(--paper-3); color: var(--ink); }
.hit__top {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  flex-wrap: wrap;
  font-size: var(--t-xs);
  color: var(--muted);
}
.hit__title {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.hit:hover .hit__title { color: var(--accent); }
.hit__snippet {
  font-size: var(--t-sm);
  color: var(--ink-2);
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.hit__path {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: #8b7f6e;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ================= section headings ================= */
.section { padding-top: var(--s-7); }
.section__head {
  display: flex;
  align-items: baseline;
  gap: var(--s-3);
  margin-bottom: var(--s-4);
}
.section__head h2 { flex: none; }
.section__head .muted { font-size: var(--t-sm); }
.section__head .spacer { flex: 1 1 auto; }
.section__count {
  font-family: var(--font-mono);
  font-size: var(--t-xs);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* ================= empty / note ================= */
.empty {
  display: grid;
  justify-items: start;
  gap: 6px;
  padding: var(--s-6) var(--s-4);
  border: 1px dashed var(--rule-2);
  border-radius: var(--r-lg);
  color: var(--muted);
  font-size: var(--t-sm);
}
.empty strong { color: var(--ink); font-weight: 500; }

.note {
  display: flex;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--r-md);
  font-size: var(--t-xs);
  line-height: 1.5;
  background: var(--paper-3);
  color: var(--ink-2);
  border: 1px solid var(--rule);
}
.note--ok { background: var(--ok-soft); border-color: #c9dcc6; color: #245034; }
.note--warn { background: var(--warn-soft); border-color: #e6d3ac; color: #6d470c; }
.note--fail { background: var(--danger-soft); border-color: #e6c4be; color: #71211d; }
.note .i { flex: none; margin-top: 1px; }

/* ================= menu (popover) ================= */
.menu-wrap { position: relative; }
.menu {
  position: absolute;
  inset-inline-end: 0;
  top: calc(100% + 6px);
  z-index: 50;
  min-width: 220px;
  padding: 5px;
  background: var(--paper-2);
  border: 1px solid var(--rule-2);
  border-radius: var(--r-md);
  box-shadow: var(--sh-2);
}
.menu__item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--ink);
  font: inherit;
  font-size: var(--t-sm);
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}
.menu__item:hover { background: var(--paper-3); color: var(--ink); }
.menu__item .i { color: var(--muted); flex: none; }
.menu__item--primary .i { color: var(--accent); }
.menu__label {
  padding: 8px 10px 4px;
  font-size: var(--t-label);
  color: var(--muted);
}
.menu__sep { height: 1px; margin: 5px 0; background: var(--rule); }

/* ================= dialog ================= */
dialog {
  padding: 0;
  border: 1px solid var(--rule-2);
  border-radius: var(--r-lg);
  background: var(--paper-2);
  color: var(--ink);
  box-shadow: 0 40px 90px -30px rgba(29, 26, 21, 0.45);
  width: min(640px, calc(100vw - 32px));
  max-height: min(86vh, 760px);
}
dialog::backdrop { background: rgba(29, 26, 21, 0.42); backdrop-filter: blur(2px); }
dialog[open] { display: flex; flex-direction: column; }

.dialog__head {
  display: flex;
  align-items: center;
  gap: var(--s-3);
  padding: var(--s-4) var(--s-5);
  border-bottom: 1px solid var(--rule);
}
.dialog__head h2 { font-size: 1.125rem; flex: 1 1 auto; }
.dialog__body {
  padding: var(--s-5);
  overflow: auto;
  display: grid;
  gap: var(--s-4);
}
.dialog__foot {
  display: flex;
  gap: var(--s-2);
  align-items: center;
  padding: var(--s-4) var(--s-5);
  border-top: 1px solid var(--rule);
  background: var(--paper);
}
.dialog__foot .spacer { flex: 1 1 auto; }
.dialog--wide { width: min(880px, calc(100vw - 32px)); }
.dialog .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--s-4); }

details > summary { list-style: none; }
details > summary::-webkit-details-marker { display: none; }
details > summary::after { content: " +" ; color: var(--muted); }
details[open] > summary::after { content: " −"; }

@media (max-width: 620px) {
  dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 92vh;
    margin: auto 0 0;
    border-radius: var(--r-lg) var(--r-lg) 0 0;
  }
  .dialog__body { padding: var(--s-4); }
  .dialog__head, .dialog__foot { padding: var(--s-3) var(--s-4); }
  .dialog .grid-2 { grid-template-columns: 1fr; }
}

/* ================= palette (search dialog) ================= */
.palette { width: min(720px, calc(100vw - 32px)); }
.palette .search {
  border: 0;
  border-radius: 0;
  box-shadow: none;
  border-bottom: 1px solid var(--rule);
  background: transparent;
}
.palette .search:focus-within { box-shadow: none; }
.palette__results { overflow: auto; max-height: min(60vh, 520px); padding: var(--s-2); }
.palette__group { padding: 8px 10px 4px; font-size: var(--t-label); color: var(--muted); }
.palette__item {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 9px 10px;
  border-radius: 4px;
  text-decoration: none;
  color: var(--ink);
  font-size: var(--t-sm);
  cursor: pointer;
}
.palette__item[aria-selected="true"] { background: var(--accent-soft); color: var(--accent-ink); }
.palette__item .path { font-size: var(--t-xs); color: var(--muted); }
.palette__thumb {
  flex: none;
  display: grid;
  place-items: center;
  width: 26px; height: 26px;
  border-radius: 4px;
  background: var(--paper-3);
  color: var(--muted);
}
.palette__item[aria-selected="true"] .palette__thumb { background: #fff; color: var(--accent); }
.palette__foot {
  display: flex;
  gap: var(--s-4);
  padding: 8px var(--s-4);
  border-top: 1px solid var(--rule);
  font-size: var(--t-xs);
  color: var(--muted);
  background: var(--paper);
}
.palette__foot span { display: inline-flex; align-items: center; gap: 5px; }

mark { background: #f2e2a8; color: inherit; padding: 0 1px; border-radius: 2px; }

/* ================= jobs / progress ================= */
.job {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: var(--s-3);
  align-items: center;
  padding: 10px var(--s-2);
  border-bottom: 1px solid var(--rule);
  font-size: var(--t-sm);
}
.job:last-child { border-bottom: 0; }
.job__title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job__sub { font-size: var(--t-xs); color: var(--muted); }
.job__icon { color: var(--muted); }
.job--running .job__icon { color: var(--accent); }
.job--running .job__icon .i { animation: spin 1.1s linear infinite; }

/* ================= reader ================= */
.reader {
  font-family: var(--font-display);
  font-size: 1.0625rem;
  line-height: 1.72;
  max-width: 62ch;
  color: var(--ink-2);
}
.reader p { margin-bottom: 0.8em; }
.reader h3 { margin-top: 1.4em; }

/* ================= misc ================= */
.pagination {
  display: flex;
  align-items: center;
  gap: var(--s-2);
  justify-content: center;
  padding-top: var(--s-4);
  font-size: var(--t-xs);
  color: var(--muted);
}

.toast-stack {
  position: fixed;
  z-index: 90;
  inset-inline-end: var(--s-4);
  bottom: var(--s-4);
  display: grid;
  gap: var(--s-2);
  width: min(380px, calc(100vw - 32px));
}
.toast {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 11px 13px;
  border-radius: var(--r-md);
  background: var(--ink);
  color: var(--paper-2);
  font-size: var(--t-sm);
  box-shadow: var(--sh-2);
  animation: toast-in 0.18s ease-out;
}
.toast .i { flex: none; margin-top: 1px; }
.toast--ok .i { color: #92c9a2; }
.toast--fail .i { color: #e7a49e; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

.i { width: 18px; height: 18px; stroke-width: 1.7; }
.i--sm { width: 15px; height: 15px; }
.i--lg { width: 24px; height: 24px; }
