/* Comments — redesign thread.
 *
 * Renders the thread + compose form for a commentable (Card/Todo/Step/
 * Meeting/Project), so these styles apply equally to the work-item detail
 * panel and the Project overview.
 */

@layer components {
  /* ------------------------------------------------------------------ */
  /* Thread container + header                                          */
  /* ------------------------------------------------------------------ */
  .cm-thread {
    display: flex;
    flex-direction: column;
    gap: var(--rd-sp-3);
  }
  .cm-thread-head {
    display: flex;
    align-items: center;
    gap: var(--rd-sp-2);
    font-size: var(--rd-text-2xs);
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--rd-fg-subtle);
  }
  .cm-thread-title {
    /* inherits from head */
  }
  .cm-thread-count {
    font-family: var(--rd-font-mono);
    font-size: var(--rd-text-2xs);
    color: var(--rd-fg-subtle);
    letter-spacing: 0;
    font-weight: 500;
  }

  .cm-list {
    display: flex;
    flex-direction: column;
    gap: var(--rd-sp-3);
  }
  .cm-list:empty { display: none; }

  .cm-empty {
    color: var(--rd-fg-subtle);
    font-style: italic;
    font-size: var(--rd-text-sm);
    margin: 0;
    padding: var(--rd-sp-3) 0;
  }

  /* ------------------------------------------------------------------ */
  /* Avatar shared between item and compose                             */
  /* ------------------------------------------------------------------ */
  .cm-av {
    width: 28px;
    height: 28px;
    border-radius: var(--rd-radius-sm);
    display: inline-grid;
    place-items: center;
    color: #fff;
    font-weight: 600;
    font-size: var(--rd-text-2xs);
    letter-spacing: 0.02em;
    flex: 0 0 auto;
    overflow: hidden;
  }
  img.cm-av {
    object-fit: cover;
    display: block;
    background: var(--rd-bg-sunken);
  }
  .cm-av-unknown {
    background: var(--rd-bg-active);
    color: var(--rd-fg-subtle);
  }

  /* ------------------------------------------------------------------ */
  /* Comment item                                                       */
  /* ------------------------------------------------------------------ */
  .cm-item {
    display: grid;
    grid-template-columns: 28px 1fr;
    gap: 10px;
    position: relative;
  }
  .cm-body {
    min-width: 0;
    background: var(--rd-bg-raised);
    border: 1px solid var(--rd-border);
    border-radius: var(--rd-radius-lg);
    padding: 10px 12px 8px;
  }
  .cm-item.is-mine .cm-body {
    background: var(--rd-accent-soft);
    border-color: oklch(0.68 0.22 0 / 0.18);
  }
  .cm-head {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
    flex-wrap: wrap;
  }
  .cm-name {
    font-size: var(--rd-text-sm);
    font-weight: 600;
    color: var(--rd-fg);
  }
  .cm-name-external {
    font-style: italic;
    color: var(--rd-fg-muted);
    font-weight: 500;
  }
  .cm-name-unknown {
    font-style: italic;
    color: var(--rd-fg-subtle);
    font-weight: 500;
  }
  .cm-when {
    font-size: var(--rd-text-xs);
    color: var(--rd-fg-subtle);
  }
  .cm-you-tag {
    font-size: var(--rd-text-2xs);
    font-weight: 600;
    letter-spacing: 0.05em;
    color: var(--rd-accent);
    background: var(--rd-bg-raised);
    padding: 1px 6px;
    border-radius: var(--rd-radius-sm);
    border: 1px solid var(--rd-border);
  }
  .cm-text {
    color: var(--rd-fg);
    font-size: var(--rd-text-base);
    line-height: var(--rd-leading-normal);
  }
  .cm-text > *:first-child { margin-top: 0; }
  .cm-text > *:last-child  { margin-bottom: 0; }
  .cm-text p { margin: 0 0 6px; }
  .cm-text p:last-child { margin-bottom: 0; }
  .cm-text a {
    color: var(--rd-accent);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
  }
  .cm-text blockquote {
    margin: 8px 0;
    padding: 4px 0 4px 12px;
    border-left: 3px solid var(--rd-accent);
    color: var(--rd-fg-muted);
    font-style: italic;
  }
  .cm-text code {
    background: var(--rd-bg-sunken);
    padding: 1px 5px;
    border-radius: var(--rd-radius-sm);
    font-family: var(--rd-font-mono);
    font-size: var(--rd-text-sm);
    border: 1px solid var(--rd-border);
  }
  .cm-text pre {
    background: var(--rd-bg-sunken);
    border: 1px solid var(--rd-border);
    border-radius: var(--rd-radius-md);
    padding: 10px 12px;
    font-family: var(--rd-font-mono);
    font-size: var(--rd-text-sm);
    margin: 8px 0;
    white-space: pre-wrap;
    overflow-x: auto;
  }

  /* Basecamp mention compatibility — content comes in as
     <figure><img class="avatar"><figcaption>Name</figcaption></figure> */
  .cm-text figure:has(> img.avatar) { display: inline; width: auto; margin: 0; }
  .cm-text figure:has(> img.avatar) figcaption {
    display: inline;
    font-weight: 600;
    color: var(--rd-accent);
  }
  .cm-text figure:has(> img.avatar) img.avatar {
    display: inline-block;
    width: 1.2em;
    height: 1.2em;
    border-radius: 50%;
    vertical-align: middle;
    margin-right: 2px;
  }
  .cm-text em.mentioned {
    display: inline;
    background: var(--rd-accent-soft);
    color: var(--rd-accent);
    font-weight: 600;
    font-style: normal;
    padding: 0 4px;
    border-radius: var(--rd-radius-sm);
    font-size: 0.96em;
  }

  /* Actions row (Edit / Delete) */
  .cm-actions {
    display: flex;
    gap: 4px;
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px solid var(--rd-border);
  }
  .cm-action-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    font-size: var(--rd-text-xs);
    color: var(--rd-fg-subtle);
    background: transparent;
    border: 0;
    border-radius: var(--rd-radius-sm);
    cursor: pointer;
    text-decoration: none;
    transition: background var(--rd-dur-fast) var(--rd-ease),
                color var(--rd-dur-fast) var(--rd-ease);
  }
  .cm-action-btn:hover {
    background: var(--rd-bg-hover);
    color: var(--rd-fg);
  }
  .cm-action-btn-danger:hover { color: var(--rd-danger); }
  /* button_to wraps in a form; remove default spacing */
  .cm-actions form { display: contents; }

  /* ------------------------------------------------------------------ */
  /* Compose                                                            */
  /* ------------------------------------------------------------------ */
  .cm-compose {
    display: grid;
    grid-template-columns: 28px 1fr;
    gap: 10px;
    align-items: flex-start;
  }
  .cm-compose-body {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: var(--rd-sp-2);
    border: 1px solid var(--rd-border);
    border-radius: var(--rd-radius-lg);
    background: var(--rd-bg-raised);
    /* No overflow:hidden — the @-mention popover needs to escape the frame.
       Rounded corners on the inner toolbar/footer handle the clip instead. */
    transition: border-color var(--rd-dur-fast) var(--rd-ease),
                box-shadow var(--rd-dur-fast) var(--rd-ease);
  }
  .cm-compose-body:focus-within {
    border-color: var(--rd-accent);
  }

  /* Editor region (contains Lexxy) */
  .cm-compose-editor {
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  /* Footer row: keyboard hint on the left, submit cluster on the right.
     No border separating it from the editor — the composer frame reads
     cleaner as one surface. */
  .cm-compose-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 8px 10px;
    background: var(--rd-bg);
    border-bottom-left-radius: calc(var(--rd-radius-lg) - 1px);
    border-bottom-right-radius: calc(var(--rd-radius-lg) - 1px);
  }
  .cm-compose-hint {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: var(--rd-text-2xs);
    color: var(--rd-fg-subtle);
  }
  .cm-compose-hint-text { letter-spacing: 0.02em; }
  .cm-kbd {
    font-family: var(--rd-font-mono);
    font-size: var(--rd-text-2xs);
    padding: 1px 5px;
    border: 1px solid var(--rd-border);
    border-radius: 0;
    color: var(--rd-fg-subtle);
    background: var(--rd-bg-raised);
  }
  .cm-kbd-mod::before { content: "Ctrl"; }
  html.is-mac .cm-kbd-mod::before { content: "⌘"; }
  .cm-compose-submit {
    display: inline-flex;
    gap: 6px;
    align-items: center;
  }

  /* Hide default Rails form field wrapping that Lexxy may add */
  .cm-compose-body form { display: contents; }
  /* form_with cm-compose-body is the form itself; restore its layout */
  form.cm-compose-body { display: flex; flex-direction: column; }

  /* Inline edit form only — subtle head row above the editor */
  .cm-item.is-editing .cm-body { display: contents; }
  .cm-compose-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px 0;
  }
  .cm-editing-badge {
    font-size: var(--rd-text-2xs);
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--rd-accent);
    background: var(--rd-accent-soft);
    padding: 2px 6px;
    border-radius: var(--rd-radius-sm);
  }
}
