/*
 * Action Text + Trix — editorial override.
 *
 * Replaces the gem's default toolbar styling with Farlist's
 * design tokens. The toolbar markup itself is locked down to
 * italic + link + attach (see app/views/items/_trix_toolbar.html.erb);
 * this file styles those three buttons + the link dialog + the
 * .trix-content render path so attachments and prose look right
 * on the item detail page.
 *
 * Buttons that aren't in our custom toolbar (bold, strike, quote,
 * heading, code, lists, undo/redo, nesting) have their styles
 * intentionally omitted — keeps this file small and prevents
 * accidental "look how bold renders" surface area if the toolbar
 * is ever overridden upstream.
 */

trix-editor {
  display: block;
  border: 1px solid var(--rule-soft);
  border-radius: 0;
  margin: 0;
  padding: 14px 16px;
  min-height: 8em;
  outline: none;
  background: var(--input-bg);
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.65;
  color: var(--text-primary);
  transition: border-color 120ms ease;
}
trix-editor:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
trix-editor[disabled] {
  opacity: 0.6;
}

/* ── Toolbar (custom minimal markup) ──────────────────────────── */
trix-toolbar {
  display: block;
  margin-bottom: 0;
}
trix-toolbar * { box-sizing: border-box; }

trix-toolbar .trix-button-row {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 6px 4px;
  border-bottom: 0;
}
trix-toolbar .trix-button-group {
  display: inline-flex;
  gap: 4px;
}
trix-toolbar .trix-button {
  position: relative;
  width: 32px;
  height: 32px;
  padding: 0;
  margin: 0;
  border: 1px solid transparent;
  border-radius: 2px;
  background: transparent;
  color: var(--text-tertiary);
  font-size: 0;
  cursor: pointer;
  text-indent: -9999px;
  transition: color 120ms ease, background 120ms ease, border-color 120ms ease;
}
trix-toolbar .trix-button:hover {
  color: var(--text-primary);
  background: var(--bg-base);
  border-color: var(--rule-softer);
}
trix-toolbar .trix-button.trix-active {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: transparent;
}
trix-toolbar .trix-button:disabled {
  cursor: default;
  opacity: 0.3;
}

/* Icon glyphs — only the three we expose. Inherit color via CSS
   filter so the same SVG works against any theme background. */
trix-toolbar .trix-button--icon::before {
  content: "";
  position: absolute;
  inset: 6px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  opacity: 0.85;
}
trix-toolbar .trix-button.trix-active::before { opacity: 1; }

trix-toolbar .trix-button--icon-italic::before {
  background-image: url("data:image/svg+xml;utf8,%3Csvg width='24' height='24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M9 5h6.5v2h-2.23l-2.31 10H13v2H6v-2h2.461l2.306-10H9V5Z' fill='currentColor'/%3E%3C/svg%3E");
}
trix-toolbar .trix-button--icon-link::before {
  background-image: url("data:image/svg+xml;utf8,%3Csvg width='24' height='24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M18.948 5.258a4.337 4.337 0 0 0-6.108 0L11.217 6.87a.993.993 0 0 0 0 1.41c.392.39 1.027.39 1.418 0l1.623-1.613a2.323 2.323 0 0 1 3.271 0 2.29 2.29 0 0 1 0 3.251l-2.393 2.38a3.021 3.021 0 0 1-4.255 0l-.05-.049a1.007 1.007 0 0 0-1.418 0 .993.993 0 0 0 0 1.41l.05.049a5.036 5.036 0 0 0 7.091 0l2.394-2.38a4.275 4.275 0 0 0 0-6.072Zm-13.683 13.6a4.337 4.337 0 0 0 6.108 0l1.262-1.255a.993.993 0 0 0 0-1.41 1.007 1.007 0 0 0-1.418 0L9.954 17.45a2.323 2.323 0 0 1-3.27 0 2.29 2.29 0 0 1 0-3.251l2.344-2.331a2.579 2.579 0 0 1 3.631 0c.392.39 1.027.39 1.419 0a.993.993 0 0 0 0-1.41 4.593 4.593 0 0 0-6.468 0l-2.345 2.33a4.275 4.275 0 0 0 0 6.072Z' fill='currentColor'/%3E%3C/svg%3E");
}
trix-toolbar .trix-button--icon-attach::before {
  background-image: url("data:image/svg+xml;utf8,%3Csvg width='24' height='24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M10.5 18V7.5c0-2.25 3-2.25 3 0V18c0 4.125-6 4.125-6 0V7.5c0-6.375 9-6.375 9 0V18' stroke='currentColor' stroke-width='2' stroke-miterlimit='10' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

/* ── Link dialog ──────────────────────────────────────────────── */
trix-toolbar .trix-dialogs { position: relative; }
trix-toolbar .trix-dialog {
  position: absolute;
  top: 6px;
  left: 0;
  right: 0;
  z-index: 5;
  padding: 14px;
  background: var(--bg-card);
  border: 1px solid var(--rule-soft);
  border-radius: 2px;
  box-shadow: 0 4px 12px rgba(40, 52, 72, 0.08);
}
trix-toolbar .trix-input--dialog {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 0.825rem;
  padding: 6px 10px;
  margin: 0 8px 0 0;
  border: 1px solid var(--rule-soft);
  border-radius: 2px;
  background: var(--bg-base);
  outline: none;
}
trix-toolbar .trix-input--dialog:focus { border-color: var(--accent); }
trix-toolbar .trix-button--dialog {
  width: auto;
  height: auto;
  padding: 6px 12px;
  font-family: var(--font-mono);
  font-size: 0.706rem;
  letter-spacing: .12em;
  text-transform: uppercase;
  text-indent: 0;
  border: 1px solid var(--rule-soft);
  background: var(--bg-base);
  color: var(--text-secondary);
}
trix-toolbar .trix-button--dialog:hover {
  color: var(--accent);
  border-color: var(--accent);
}
trix-toolbar .trix-dialog__link-fields {
  display: flex;
  align-items: stretch;
}
trix-toolbar .trix-dialog__link-fields .trix-button-group {
  display: inline-flex;
  gap: 6px;
  margin: 0;
}

/* ── Editor internals ─────────────────────────────────────────── */
trix-editor [data-trix-mutable]:not(.attachment__caption-editor) {
  user-select: none;
}
trix-editor [data-trix-mutable]::selection,
trix-editor [data-trix-cursor-target]::selection,
trix-editor [data-trix-mutable] ::selection { background: none; }

trix-editor .attachment {
  position: relative;
  margin: 8px 0;
}
trix-editor .attachment__progress {
  position: absolute;
  z-index: 1;
  height: 6px;
  top: calc(50% - 3px);
  left: 5%;
  width: 90%;
  opacity: 0.9;
}
trix-editor .attachment__progress[value="100"] { opacity: 0; }

trix-editor .attachment__caption-editor {
  display: inline-block;
  width: 100%;
  margin: 0;
  padding: 4px 0;
  font-family: var(--font-body);
  font-size: 0.9rem;
  color: var(--text-tertiary);
  text-align: center;
  border: none;
  background: transparent;
  outline: none;
}

trix-editor .attachment__toolbar {
  position: absolute;
  z-index: 1;
  top: -2.2em;
  left: 0;
  width: 100%;
  text-align: center;
}
trix-editor .attachment__toolbar .trix-button-group { display: inline-flex; }
trix-editor .attachment__toolbar .trix-button {
  width: auto;
  height: auto;
  padding: 4px 8px;
  font-size: 0.706rem;
  letter-spacing: .12em;
  text-transform: uppercase;
  text-indent: 0;
  background: var(--bg-card);
  border: 1px solid var(--rule-soft);
  color: var(--text-secondary);
}
trix-editor .trix-button--remove {
  text-indent: -9999px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--rule-soft);
  background: var(--bg-card);
  padding: 0;
}
trix-editor .trix-button--remove::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml;utf8,%3Csvg width='18' height='18' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M14 4.41 12.59 3 9 6.59 5.41 3 4 4.41 7.59 8 4 11.59 5.41 13 9 9.41 12.59 13 14 11.59 10.41 8z' fill='%23545e75'/%3E%3C/svg%3E");
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0.7;
}
trix-editor .trix-button--remove:hover { border-color: var(--accent); }

/* ── Rendered content (item detail page) ──────────────────────── */
.trix-content {
  line-height: 1.65;
  overflow-wrap: break-word;
}
.trix-content > * { margin: 0; padding: 0; }
.trix-content p {
  margin: 0 0 1em;
}
.trix-content p:last-child { margin-bottom: 0; }
.trix-content em { font-style: italic; }
.trix-content a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid var(--accent-soft);
  transition: border-color 120ms ease;
}
.trix-content a:hover { border-bottom-color: var(--accent); }

.trix-content img {
  max-width: 100%;
  height: auto;
  border-radius: 2px;
  display: block;
  margin: 16px auto;
}
.trix-content .attachment {
  display: inline-block;
  position: relative;
  max-width: 100%;
  margin: 8px 0;
}
.trix-content .attachment a {
  color: inherit;
  text-decoration: none;
  border-bottom: none;
}
.trix-content .attachment__caption {
  text-align: center;
  font-family: var(--font-body);
  font-style: italic;
  font-size: 0.9rem;
  color: var(--text-tertiary);
  margin-top: 6px;
}
.trix-content .attachment__caption .attachment__name + .attachment__size::before {
  content: ' \2022 ';
}
.trix-content .attachment--preview {
  width: 100%;
  text-align: center;
}

/* Galleries: Trix wraps multi-image insertions in
   `.attachment-gallery` and our `<action-text-attachment>` nesting
   needs the override below. */
.trix-content .attachment-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.trix-content .attachment-gallery > action-text-attachment,
.trix-content .attachment-gallery > .attachment {
  flex: 1 0 calc(33% - 8px);
  max-width: calc(33% - 8px);
  padding: 0;
}
.trix-content .attachment-gallery.attachment-gallery--2 > action-text-attachment,
.trix-content .attachment-gallery.attachment-gallery--2 > .attachment,
.trix-content .attachment-gallery.attachment-gallery--4 > action-text-attachment,
.trix-content .attachment-gallery.attachment-gallery--4 > .attachment {
  flex-basis: calc(50% - 8px);
  max-width: calc(50% - 8px);
}
.trix-content action-text-attachment .attachment {
  padding: 0 !important;
  max-width: 100% !important;
}
