/*
 * Comment composer with entity attachments.
 *
 * Implements design 1a from docs/plans/comments-ui-handoff/. Non-colour values
 * (radii, type sizes, spacing) come from the handoff verbatim. Colours are mapped
 * onto the app's existing theme variables rather than transplanting the design's
 * own token set, so the composer sits correctly beside the rest of the UI in both
 * themes.
 *
 * The mapping lives in one place, on .comment-composer below. --cc-primary
 * follows --accent-color, which is user-configurable, so the composer picks up
 * whatever accent the member has chosen.
 */

.comment-composer {
  --cc-surface: var(--input-bg);
  --cc-raised: var(--dropdown-bg);
  --cc-control: var(--bg-color);
  --cc-hover: var(--hover-bg);
  --cc-border: var(--border-color);
  --cc-text: var(--text-color);
  --cc-primary: var(--accent-color);
  --cc-on-primary: var(--accent-contrast);
  --cc-shadow: var(--dropdown-shadow);
  --cc-danger: #d53e4f;

  position: relative;
}

/* Muted text tiers. The app has no variables for these, so they are opacity
   steps on the resolved text colour, which works in both themes. */
.cc-count,
.cc-chip-meta,
.cc-row-shortcut,
.cc-result-meta,
.cc-picker-footer,
.cc-panel-label {
  color: var(--cc-text);
  opacity: 0.6;
}

.cc-result-subtitle {
  color: var(--cc-text);
  opacity: 0.7;
}

.comment-composer-icons {
  display: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Card ---------------------------------------------------------------- */

.comment-composer-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  margin: 0;
  background-color: var(--cc-surface);
  border: 1px solid var(--cc-border);
  border-radius: 12px;
}

.cc-top {
  display: flex;
  gap: 12px;
}

.cc-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex: none;
}

.cc-avatar--initial {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

/*
 * The avatar squeezes the text box on a narrow screen, and it is the least
 * useful thing in the composer - the author already knows who they are.
 *
 * Width only, deliberately not the sheet's "(max-width: 640px), (pointer:
 * coarse)" query: a touchscreen laptop has plenty of room and should keep it.
 */
@media (max-width: 640px) {
  .cc-avatar {
    display: none;
  }
}

.cc-main {
  position: relative; /* containing block for the @mention dropdown */
  flex: 1;
  min-width: 0; /* required: chips and titles rely on this for ellipsis */
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cc-textarea {
  width: 100%;
  min-height: 44px;
  padding: 0;
  border: none;
  background: none;
  outline: none;
  resize: vertical;
  color: var(--cc-text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.55;
}

.cc-textarea::placeholder {
  color: var(--cc-text);
  opacity: 0.5;
}

/* --- Chips --------------------------------------------------------------- */

.cc-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.cc-chips:empty {
  display: none;
}

.cc-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 260px;
  padding: 5px 8px 5px 6px;
  background-color: var(--cc-control);
  border: 1px solid var(--cc-border);
  border-radius: 7px;
}

.cc-chip-icon,
.cc-chip-icon svg {
  width: 15px;
  height: 15px;
  flex: none;
  display: block;
}

.cc-chip-title {
  font-size: 13px;
  color: var(--cc-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cc-chip-meta {
  font-size: 12px;
  white-space: nowrap;
}

.cc-chip-remove {
  display: flex;
  padding: 1px;
  border: none;
  background: none;
  color: var(--cc-text);
  opacity: 0.6;
  cursor: pointer;
}

.cc-chip-remove:hover {
  color: var(--cc-danger);
  opacity: 1;
}

/* --- "Mentioned in" reverse lookup --------------------------------------- */

.mentioned-in-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.mentioned-in-item {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-color);
  text-decoration: none;
  transition: border-color 0.15s;
}

.mentioned-in-item:hover {
  border-color: var(--accent-color);
  text-decoration: none;
}

.mentioned-in-meta {
  font-size: 0.85em;
  opacity: 0.75;
}

.mentioned-in-body {
  font-size: 0.9em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- Cards on a posted comment ------------------------------------------- */
/*
 * The composer keeps chips compact because the composer is a workspace. A posted
 * comment gets the fuller card - kind, title, subtitle, meta - because a bare
 * title hides the thing being discussed.
 *
 * These render outside .comment-composer, so they use the app's theme variables
 * directly rather than the --cc-* mapping.
 */

/*
 * auto-fill rather than auto-fit: a lone card occupies one track and stays
 * compact instead of stretching across the whole comment. On a wide screen two
 * or three sit side by side; below ~260px of space they stack.
 */
.cc-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.cc-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  min-width: 0; /* lets the grid track shrink; the title ellipsis needs it */
  border: 1px solid var(--border-color);
  border-radius: 9px;
  background-color: var(--bg-color);
  color: var(--text-color);
  text-decoration: none;
}

.cc-card--link {
  transition: border-color 0.15s;
}

.cc-card--link:hover {
  border-color: var(--accent-color);
  text-decoration: none;
}

.cc-card--link:focus-visible {
  outline: 2px solid var(--accent-color);
  outline-offset: 2px;
}

/* The referenced record has been deleted. Inert, and visibly so. */
.cc-card--tombstone {
  opacity: 0.55;
}

.cc-card--tombstone .cc-card-title {
  font-style: italic;
}

/* No fill behind the icon: the colour icons carry their own shape and read
   better against the card than inside a tinted tile. */
.cc-card-thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex: none;
}

.cc-card-thumb svg {
  width: 26px;
  height: 26px;
  display: block;
}

/* Real artwork fills the slot; the icon variant sits loose in it. */
.cc-card-artwork {
  width: 32px;
  height: 32px;
  display: block;
  border-radius: 5px;
  object-fit: cover;
}

.cc-card-text {
  flex: 1;
  min-width: 0; /* required for the ellipsis below */
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.cc-card-title {
  font-size: 13.5px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cc-card-subtitle {
  font-size: 12px;
  opacity: 0.7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Right-aligned, since the kind header it used to sit beside is gone. */
.cc-card-meta {
  flex: none;
  font-size: 12px;
  opacity: 0.75;
  white-space: nowrap;
}

/* --- Footer -------------------------------------------------------------- */

.cc-footer {
  position: relative; /* the popover anchors to this */
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--cc-border);
}

.cc-footer-left,
.cc-footer-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.cc-add {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--cc-border);
  border-radius: 8px;
  background-color: var(--cc-control);
  color: var(--cc-text);
  opacity: 0.75;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
}

.cc-add:hover,
.cc-add[aria-expanded="true"] {
  background-color: var(--cc-hover);
  border-color: var(--cc-primary);
  color: var(--cc-primary);
  opacity: 1;
}

.cc-count {
  font-size: 12.5px;
}

.cc-submit {
  padding: 7px 16px;
  border: none;
  border-radius: 8px;
  background-color: var(--cc-primary);
  color: var(--cc-on-primary);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: filter 0.15s, opacity 0.15s;
}

.cc-submit:hover:not(:disabled) {
  filter: brightness(1.18);
}

.cc-submit:disabled {
  opacity: 0.45;
  cursor: default;
}

/* --- Popover ------------------------------------------------------------- */

/*
 * The popover is a <dialog>. Visibility is the browser's job via the `open`
 * attribute, so there is no [hidden] rule here to be out-specified by a :has()
 * selector. Everything below overrides the UA dialog defaults (border, padding,
 * margin: auto, background, max-width/height).
 *
 * These absolute-position values are the fallback path only, for browsers
 * without the Popover API. When shown with showPopover() the element is in the
 * top layer and positioned from JavaScript - see :popover-open below.
 */
.cc-popover {
  position: absolute;
  bottom: 46px;
  left: 0;
  inset-inline-end: auto;
  z-index: 30;
  width: 268px;
  max-width: calc(100vw - 32px);
  max-height: none;
  margin: 0;
  padding: 0;
  overflow: visible;
  color: var(--cc-text);
  background-color: var(--cc-raised);
  border: 1px solid var(--cc-border);
  border-radius: 12px;
  box-shadow: 0 18px 40px var(--cc-shadow);
  animation: cc-pop-in 0.14s ease-out;
}

/*
 * Top-layer popover (desktop). Positioned against the viewport by the controller
 * rather than by the offsets above, because the top layer is outside the normal
 * flow. This is what lets the panel escape .collapsible-content's overflow:
 * hidden and any backdrop-filter ancestor's containing block.
 */
.cc-popover:popover-open {
  position: fixed;
  inset: auto;
  margin: 0;
}

/* The picker is wider than the type list. */
.cc-popover:has(.cc-picker:not([hidden])) {
  width: 400px;
  overflow: hidden;
}

@keyframes cc-pop-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

.cc-sheet-handle {
  display: none;
}

.cc-types {
  padding: 6px;
}

.cc-panel-label {
  padding: 8px 10px 6px;
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* --- Rows (shared by the type list and the results) ---------------------- */

.cc-row {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 9px 10px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--cc-text);
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}

.cc-row:hover,
.cc-row.is-highlighted {
  background-color: var(--cc-hover);
}

.cc-row[hidden] {
  display: none;
}

.cc-row-icon,
.cc-row-icon svg {
  width: 17px;
  height: 17px;
  flex: none;
  display: block;
}

.cc-row-label {
  flex: 1;
  font-size: 14px;
}

.cc-row-shortcut {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

/* --- Picker -------------------------------------------------------------- */

.cc-picker[hidden] {
  display: none;
}

.cc-picker-header {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--cc-border);
}

.cc-icon-button {
  display: flex;
  padding: 0;
  border: none;
  background: none;
  color: var(--cc-text);
  opacity: 0.65;
  cursor: pointer;
}

.cc-icon-button:hover {
  opacity: 1;
}

.cc-picker-type-icon,
.cc-picker-type-icon svg {
  width: 15px;
  height: 15px;
  flex: none;
  display: block;
}

.cc-search {
  flex: 1;
  min-width: 0;
  padding: 0;
  border: none;
  background: none;
  outline: none;
  color: var(--cc-text);
  font-family: inherit;
  font-size: 14px;
}

.cc-results {
  max-height: 250px;
  overflow-y: auto;
  padding: 6px;
}

.cc-result-row.is-attached {
  opacity: 0.45;
  cursor: default;
}

.cc-result-thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex: none;
  overflow: hidden; /* clips artwork to the rounded corner */
  border-radius: 6px;
  background-color: var(--cc-control);
}

.cc-result-thumb svg {
  width: 15px;
  height: 15px;
  display: block;
}

/* Real artwork fills the thumbnail slot; the icon variant stays inset. */
.cc-result-artwork {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 6px;
  object-fit: cover;
}

.cc-result-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.cc-result-title {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cc-result-subtitle {
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cc-result-meta {
  font-size: 12px;
  white-space: nowrap;
}

.cc-results-message {
  padding: 16px 12px;
  font-size: 13px;
  color: var(--cc-text);
  opacity: 0.6;
}

.cc-picker-footer {
  padding: 8px 12px;
  border-top: 1px solid var(--cc-border);
  font-size: 12px;
}

/* --- Focus --------------------------------------------------------------- */
/* The prototype does not draw focus rings; production must. */

.comment-composer :focus-visible {
  outline: 2px solid var(--cc-primary);
  outline-offset: 2px;
}

.cc-textarea:focus-visible {
  outline-offset: 4px;
}

/* --- Mobile: bottom sheet ------------------------------------------------ */
/*
 * Below 640px, or on any coarse pointer, the popover becomes a bottom sheet.
 * Keep this breakpoint in step with MOBILE_QUERY in
 * comment_composer_controller.js.
 */

@media (max-width: 640px), (pointer: coarse) {
  /*
   * Opened with showModal(), so this is in the top layer and its position is
   * resolved against the viewport regardless of any backdrop-filter ancestor
   * (.glass-card and .message-detail both have one, which would otherwise make
   * them the containing block and leave the "sheet" floating mid-card).
   *
   * The UA modal defaults that need overriding: inset: 0, margin: auto,
   * max-width: calc(100% - 6px - 2em), max-height: calc(100% - 6px - 2em).
   */
  .cc-popover[open] {
    position: fixed;
    inset: auto 0 0 0;
    width: 100%;
    max-width: none;
    max-height: 78vh;
    margin: 0;
    display: flex;
    flex-direction: column;
    padding: 10px 0 calc(18px + env(safe-area-inset-bottom));
    overflow: hidden;
    border: none;
    border-top: 1px solid var(--cc-border);
    border-radius: 16px 16px 0 0;
    animation: cc-sheet-up 0.2s ease-out;
  }

  /* The scrim. A modal dialog gets this for free - no element required. */
  .cc-popover::backdrop {
    background-color: rgba(10, 11, 14, 0.6);
  }

  /* Neutralise the desktop picker width; the sheet is always full-bleed. */
  .cc-popover:has(.cc-picker:not([hidden])) {
    width: 100%;
    max-width: none;
  }

  @keyframes cc-sheet-up {
    from { transform: translateY(100%); }
    to   { transform: none; }
  }

  .cc-sheet-handle {
    display: block;
    width: 36px;
    height: 4px;
    margin: 0 auto 10px;
    border-radius: 2px;
    background-color: var(--cc-border);
  }

  .cc-types {
    padding: 0;
  }

  .cc-panel-label {
    padding: 0 18px 8px;
  }

  /* 44px minimum touch target. */
  .cc-row {
    gap: 13px;
    padding: 13px 18px;
    border-radius: 0;
  }

  .cc-row-icon,
  .cc-row-icon svg {
    width: 19px;
    height: 19px;
  }

  .cc-row-label {
    font-size: 15px;
  }

  /* Shortcuts are keyboard affordances; no keyboard, no point. */
  .cc-row-shortcut {
    display: none;
  }

  .cc-picker {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }

  .cc-picker-header {
    padding: 0 16px 10px;
  }

  .cc-results {
    max-height: none;
    flex: 1;
    padding: 6px 8px;
  }

  .cc-add {
    width: 44px;
    height: 44px;
    border-radius: 9px;
  }

  .cc-chip {
    gap: 6px;
    padding: 4px 7px;
    border-radius: 6px;
  }

  .cc-chip-icon,
  .cc-chip-icon svg {
    width: 13px;
    height: 13px;
  }

  .cc-chip-title {
    font-size: 12px;
  }

  /* Dropped to save width on a narrow screen. */
  .cc-chip-meta {
    display: none;
  }
}

/* --- Reduced motion ------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  .cc-popover {
    animation: none;
  }

  .cc-add,
  .cc-submit,
  .cc-row {
    transition: none;
  }
}
