/* Commenting layer: the badge/icon, the per-record modal, and the /comments
   browse page. Uses the same tokens as the rest of the app so both themes come
   for free. */

/* ---------------------------------------------------------------- badge --- */

.comment-badge-wrap {
  display: inline-flex;
}

.comment-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.comment-badge:hover:not(:disabled) {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-soft);
}

.comment-badge:disabled {
  opacity: 0.45;
  cursor: default;
}

.comment-badge--has {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 35%, var(--border));
}

.comment-badge__count {
  font-size: 0.72rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Row-level icons sit in dense tables, so they shed their chrome. */
.comment-badge--bare {
  padding: 0.15rem;
  border-color: transparent;
  background: transparent;
}

/* ---------------------------------------------------------------- modal --- */

.comment-modal {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: flex;
  align-items: center;
  justify-content: center;
}

.comment-modal__backdrop {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, black 45%, transparent);
}

.comment-modal__panel {
  position: relative;
  width: min(560px, calc(100vw - 2rem));
  max-height: min(680px, calc(100vh - 4rem));
  display: flex;
  flex-direction: column;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
}

.comment-modal__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.comment-modal__eyebrow {
  display: block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.comment-modal__title {
  margin: 0.15rem 0 0;
  font-size: 1rem;
}

.comment-modal__close {
  width: 26px;
  height: 26px;
  flex: none;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
}

.comment-modal__close:hover {
  color: var(--text);
  background: var(--accent-soft);
}

.comment-modal__error {
  margin: 0.75rem 1rem 0;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  background: var(--bad-soft);
  color: var(--bad);
  font-size: 0.8rem;
}

/* ----------------------------------------------------------------- tabs --- */

.comment-tabs {
  display: flex;
  gap: 0.25rem;
  padding: 0.6rem 1rem 0;
}

.comment-tabs__tab {
  padding: 0.35rem 0.75rem;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
}

.comment-tabs__tab:hover {
  color: var(--text);
  background: var(--accent-soft);
}

.comment-tabs__tab--active {
  background: var(--accent);
  color: var(--accent-contrast);
}

.comment-tabs__tab--active:hover {
  background: var(--accent);
  color: var(--accent-contrast);
}

/* ----------------------------------------------------------------- list --- */

.comment-list {
  flex: 1;
  overflow-y: auto;
  /* Anchor the scroll position while the list re-renders, so adding a comment
     does not visibly yank the view. */
  overflow-anchor: auto;
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.comment-list__empty {
  margin: 1.5rem 0;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.82rem;
}

.comment {
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

/* A private comment is a draft only its author sees — the dashed edge says so
   at a glance without needing to read the badge. */
.comment--private {
  border-style: dashed;
}

.comment__head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.comment__code {
  padding: 0.1rem 0.4rem;
  border-radius: 5px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
  font-size: 0.7rem;
  letter-spacing: 0.02em;
  text-decoration: none;
}

.comment__code:hover {
  text-decoration: underline;
}

.comment__code--none {
  background: transparent;
  color: var(--text-muted);
  font-weight: 400;
  font-style: italic;
}

.comment__code--static {
  margin-left: 0.4rem;
}

.comment__author {
  font-weight: 600;
  color: var(--text);
}

.comment__visibility {
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.comment__visibility--public {
  border-color: color-mix(in srgb, var(--good) 35%, var(--border));
  background: var(--good-soft);
  color: var(--good);
}

.comment__actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.15rem;
}

.comment__inline-form {
  display: inline-flex;
}

.comment__action {
  display: inline-flex;
  padding: 0.2rem;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.comment__action:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.comment__action--danger:hover {
  color: var(--bad);
  background: var(--bad-soft);
}

.comment__title {
  margin: 0.4rem 0 0.15rem;
  font-size: 0.85rem;
}

.comment__body {
  margin: 0.3rem 0 0;
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ----------------------------------------------------------------- form --- */

.comment-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.comment-form--new {
  padding: 0.75rem 1rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
  border-radius: 0 0 12px 12px;
}

.comment-form--edit {
  margin-top: 0.5rem;
}

.comment-form__row {
  display: flex;
  gap: 0.5rem;
}

.comment-form__code {
  flex: 0 0 11rem;
}

.comment-form__title {
  flex: 1;
}

.comment-form input[type="text"],
.comment-form input:not([type]),
.comment-form input[type="search"],
.comment-form textarea {
  width: 100%;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-raised);
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
}

.comment-form textarea {
  resize: vertical;
  min-height: 4.5rem;
}

.comment-form input:focus,
.comment-form textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.comment-form__foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.comment-form__public {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  cursor: pointer;
}

.comment-form__buttons {
  display: flex;
  gap: 0.4rem;
}

/* .btn--primary / .btn--ghost are defined here because the comment forms are
   their first use in the app; they follow .btn's existing geometry. */
.btn--primary,
.btn--ghost {
  padding: 0.45rem 0.9rem;
  border-radius: 8px;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

.btn--primary:hover {
  background: color-mix(in srgb, var(--accent) 88%, black);
}

.btn--ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}

.btn--ghost:hover {
  color: var(--text);
  background: var(--accent-soft);
}

/* --------------------------------------------------------- browse page --- */

.comment-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.comment-filters__field {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.comment-filters__field--grow {
  flex: 1;
  min-width: 14rem;
}

.comment-filters__field select,
.comment-filters__field input {
  padding: 0.4rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-raised);
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
}

.comment-filters__clear {
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
}

.comment-filters__clear:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.comment-browse__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.comment-browse {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.comment-browse__row {
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.comment-browse__row--private {
  border-style: dashed;
}

.comment-browse__meta,
.comment-browse__foot {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  font-size: 0.72rem;
  color: var(--text-muted);
}

.comment-browse__foot {
  margin-top: 0.45rem;
}

.comment-browse__type {
  padding: 0.1rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 0.68rem;
}

.comment-browse__record {
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
}

.comment-browse__record:hover {
  color: var(--accent);
  text-decoration: underline;
}

.comment-browse__content {
  margin-top: 0.35rem;
}

@media (max-width: 720px) {
  .comment-form__row {
    flex-direction: column;
  }

  .comment-form__code {
    flex: 1;
  }

  .comment-filters__field--grow {
    min-width: 100%;
  }
}

/* The comment icon sits inline with a detail page's title. */
.detail-header__titlerow {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.detail-header__titlerow h1 {
  margin: 0;
}

/* Comment column in the reconciliation tables — narrow, right-aligned, and
   quiet enough not to compete with the data. */
.recon-comment-cell {
  width: 1%;
  white-space: nowrap;
  text-align: right;
}

/* The column header's label uses .visually-hidden, already defined in
   reconciliation.css. */

/* --------------------------------------------------- swap stability ------ *
 *
 * The modal re-renders wholesale on every tab switch, post, edit and delete.
 * htmx's default swap briefly leaves the incoming content mid-transition,
 * which on a full-dialog swap reads as a flash rather than a settle. Pinning
 * opacity through the swap and settle phases keeps the dialog visually static
 * while its contents change underneath.
 */
#comment-modal-host .comment-modal.htmx-swapping,
#comment-modal-host .comment-modal.htmx-settling,
#comment-modal-host .comment-modal.htmx-added {
  opacity: 1;
  transition: none;
  animation: none;
}

/* The backdrop must never re-animate — it is the same overlay throughout the
   modal's life, so any fade on it reads as the whole page blinking. */
.comment-modal__backdrop {
  animation: none;
  transition: none;
}

/* ------------------------------------------------ test-code suggest ------ */

.comment-form__code-wrap {
  position: relative;
  flex: 0 0 11rem;
}

.comment-form__code-wrap .comment-form__code {
  flex: 1;
  width: 100%;
}

/* Floats over the body textarea rather than pushing the form around as
   suggestions appear and disappear on each keystroke. */
.code-suggest-host {
  position: absolute;
  top: calc(100% + 2px);
  left: 0;
  right: 0;
  z-index: 5;
}

.code-suggest {
  margin: 0;
  padding: 0.2rem;
  list-style: none;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-md);
  max-height: 12rem;
  overflow-y: auto;
}

.code-suggest__item {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  padding: 0.35rem 0.45rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.78rem;
  text-align: left;
  cursor: pointer;
}

.code-suggest__item:hover {
  background: var(--accent-soft);
}

.code-suggest__code {
  font-weight: 600;
  color: var(--accent);
}

.code-suggest__count {
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ------------------------------------------- inline comment bodies ------- */

/* Long comments are clamped rather than truncated server-side: the full text is
   in the DOM, so expanding is instant and nothing needs re-fetching. */
.comment__body--clamped {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  line-clamp: 3;
  overflow: hidden;
}

.comment__body--open {
  -webkit-line-clamp: unset;
  line-clamp: unset;
  overflow: visible;
}

.comment__more {
  margin-top: 0.25rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--accent);
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

.comment__more:hover {
  text-decoration: underline;
}

/* Pushes author/time/visibility to the right of the meta row, so each comment
   reads as one line of provenance rather than two stacked rows. */
.comment-browse__spacer {
  flex: 1;
}

.comment-browse__pageinfo {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* The test code in a result row filters the list, so it is a button — reset the
   button chrome to keep it looking like the chip it replaces. */
button.comment__code {
  border: none;
  font-family: inherit;
  cursor: pointer;
}

/* While results reload, dim them slightly instead of collapsing the list —
   the filter bar above stays perfectly still. */
#comments-results.htmx-request {
  opacity: 0.55;
  transition: opacity 0.12s ease-out;
}

/* -------------------------------------------------------------- replies --- */

/* A thread is one level deep, so a single indent rail is enough to show what
   answers what — no recursive nesting to keep legible in a narrow modal. */
.comment__replies {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin: 0.55rem 0 0;
  padding-left: 0.7rem;
  border-left: 2px solid var(--border);
}

.comment-reply {
  padding: 0.4rem 0.5rem;
  border-radius: 8px;
  background: var(--surface-raised);
}

.comment-reply__head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.comment-reply__body {
  margin: 0.25rem 0 0;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.comment__reply-bar {
  margin-top: 0.45rem;
}

.comment__reply-toggle {
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
}

.comment__reply-toggle:hover {
  color: var(--accent);
}

.comment-form--reply {
  margin-top: 0.5rem;
}

/* A reply has no visibility control of its own — it inherits the root's — so
   the foot states what it will be instead of offering a checkbox that would
   imply otherwise. */
.comment-form__inherit {
  font-size: 0.7rem;
  color: var(--text-muted);
}
