.tag-color-teal { --tag-tone: #338378; }
.tag-color-amber { --tag-tone: #9c691d; }
.tag-color-coral { --tag-tone: #b3533c; }
.tag-color-rose { --tag-tone: #ad4f72; }
.tag-color-violet { --tag-tone: #7960ac; }
.tag-color-blue { --tag-tone: #437cb3; }
.tag-color-olive { --tag-tone: #687f34; }
.tag-color-slate { --tag-tone: #637787; }

.tag-chip, .tag-nav-icon {
  color: color-mix(in srgb, var(--text) 68%, var(--tag-tone));
  background: color-mix(in srgb, var(--tag-tone) 15%, var(--surface));
}
.tag-chip {
  display: inline-flex;
  min-width: 0;
  max-width: 100%;
  align-items: center;
  gap: 5px;
  border: 1px solid color-mix(in srgb, var(--tag-tone) 22%, var(--line));
  border-radius: 6px;
  padding: 3px 6px;
  font-size: 11px;
  line-height: 1.15;
}
.tag-chip .ui-icon { width: 12px; height: 12px; }
.tag-chip-name, .tag-navigation .nav-item > .tag-nav-name {
  display: block;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.tag-navigation .nav-item > .tag-nav-name { flex: 1; }
.tag-nav-count, .tag-manager-count {
  flex: none;
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.tag-nav-icon {
  width: 22px;
  height: 22px;
  border-radius: 6px;
}
.tag-nav-icon .ui-icon { width: 13px; height: 13px; }
.manage-tags { color: var(--accent-ink); }

.book-tag-row {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 4px;
  margin-top: 6px;
}
.book-tag-row .tag-chip { font-size: 10px; }
.tag-more { flex: none; color: var(--muted); font-size: 10px; }
.book-grid:not(.list-view) .book-card:has(.book-tag-row) .book-menu { bottom: 45px; }
.book-grid:has(.book-tag-row) > .book-card { align-self: start; }
.list-view .book-tag-row { grid-column: 1 / -1; margin-top: 0; }
.list-view .book-tag-row .tag-chip { font-size: 11px; }
.edit-book-tags { justify-self: start; }

.library-dialog.tag-manager { width: min(94vw, 710px); }
.tag-manager-layout {
  display: grid;
  grid-template-columns: minmax(0, .8fr) minmax(0, 1fr);
  gap: 20px;
}
.tag-manager-sidebar { min-width: 0; }
.tag-manager-sidebar > .button { width: 100%; }
.tag-manager-list, .tag-assignment-list {
  display: grid;
  align-content: start;
  min-width: 0;
  max-height: min(420px, 42vh);
  margin-top: 10px;
  padding: 2px;
  gap: 5px;
  overflow: auto;
  scrollbar-gutter: stable;
}
.tag-manager-item {
  min-width: 0;
  min-height: 35px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 7px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  text-align: left;
}
.tag-manager-item:hover, .tag-manager-item[aria-pressed="true"] { background: var(--accent-soft); }
.library-dialog .tag-form { min-width: 0; gap: 10px; }
.tag-form h3 { font: 400 20px/1.35 var(--serif); }
.tag-options { margin: 0; min-width: 0; padding: 0; border: 0; }
.tag-options legend { margin-bottom: 5px; font-size: 12px; font-weight: 700; }
.tag-palette { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-form .tag-option { position: relative; display: block; cursor: pointer; }
.library-dialog .tag-option input[type="radio"] {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 0;
  margin: 0;
  border: 0;
  opacity: 0;
  cursor: pointer;
}
.tag-option-preview {
  display: grid;
  place-items: center;
  width: 33px;
  height: 33px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}
.tag-option-preview::before {
  content: "";
  width: 16px;
  height: 16px;
  background: var(--tag-tone, transparent);
  border-radius: 50%;
}
#tag-icons .tag-option-preview::before { display: none; }
.tag-option-preview .ui-icon { width: 16px; height: 16px; }
.tag-option input:checked + .tag-option-preview { border-color: var(--ring); background: var(--accent-soft); }
.tag-option input:focus-visible + .tag-option-preview { outline: 2px solid var(--ring); outline-offset: 2px; }
.tag-color-picker {
  display: grid;
  grid-template-columns: minmax(94px, 144px) minmax(0, 1fr);
  align-items: center;
  gap: 14px;
  margin-top: 10px;
}
.tag-color-wheel {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle closest-side, #fff, #fff0),
    conic-gradient(from 90deg, red, yellow, lime, cyan, blue, magenta, red);
  background: radial-gradient(circle closest-side in srgb, #fff, #fff0),
    conic-gradient(from 90deg in srgb, red, yellow, lime, cyan, blue, magenta, red);
  cursor: crosshair;
  touch-action: none;
}
.tag-color-cursor {
  position: absolute;
  top: var(--wheel-y, 50%);
  left: var(--wheel-x, 50%);
  width: 15px;
  height: 15px;
  transform: translate(-50%, -50%);
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--wheel-color, #fff);
  box-shadow: 0 0 0 1px #32141e, 0 1px 5px #26141a88;
  pointer-events: none;
}
.tag-color-controls { display: grid; gap: 6px; min-width: 0; }
.library-dialog #tag-color-value {
  width: 100%;
  min-height: 22px;
  padding: 0;
  accent-color: var(--accent);
}
.library-dialog #tag-color-hex {
  width: 100%;
  min-height: 34px;
  font-variant-numeric: tabular-nums;
}
.tag-color-hex-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 40px;
  align-items: end;
  gap: 7px;
}
.tag-color-swatch {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.tag-dropper-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; }
.tag-dropper-actions .button { min-height: 34px; padding: 4px 8px; }
.tag-color-sample {
  display: grid;
  gap: 6px;
  margin-top: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.tag-color-sample img {
  display: block;
  justify-self: center;
  max-width: 100%;
  max-height: 200px;
  cursor: crosshair;
}
.tag-preview { min-height: 30px; padding: 3px 0; }
.tag-form-actions { justify-content: space-between; }
.tag-form-actions .danger-link { color: var(--danger); }
.tag-form-actions .danger-link:hover { border-color: var(--danger); }
.tag-dialog-actions { margin-top: 14px; }
.tag-assignment-controls { display: flex; align-items: end; gap: 10px; }
.tag-assignment-controls label { flex: 1; min-width: 0; }
.tag-assignment-list { margin-top: 0; }
#tag-assignment-form .tag-assignment-item {
  display: flex;
  min-height: 42px;
  min-width: 0;
  align-items: center;
  gap: 9px;
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
}
.tag-assignment-item input { flex: none; }
.tag-partial { margin-inline-start: auto; flex: none; color: var(--muted); font-weight: 400; }
.tag-assignment-item:focus-within { outline: 2px solid var(--ring); outline-offset: 1px; }

@media (max-width: 620px) {
  .tag-manager-layout { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .tag-manager-list { max-height: min(115px, 20vh); }
  .tag-manager-list:empty { display: none; }
  .book-tag-row .tag-chip { padding-inline: 4px; gap: 2px; }
  .book-tag-row .tag-chip .ui-icon { width: 10px; height: 10px; }
}
