/* Deal and task cards: usability layer (markup in deal-card.js, deal-activity.js, task-card.js).
   Loaded after cards.css. */

/* ---------- Deal: key facts under the title ---------- */
#modal-root .dc-facts {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--sp-2);
  margin: var(--sp-4) 0 var(--sp-2);
}
#modal-root .dc-facts > .tc-field {
  min-width: 0;
  margin: 0;
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface);
}
#modal-root .dc-facts > .tc-field .tc-label { display: block; margin-bottom: 2px; font-size: var(--fs-xs); color: var(--muted); }
#modal-root .dc-facts > .tc-field .tc-edit { width: 100%; min-height: 0; padding: 2px 0; background: none; }
#modal-root .dc-facts > .tc-field .tc-value { font-size: var(--fs-md); font-weight: 600; color: var(--ink); }
#modal-root .dc-facts > [data-deal-field="amount"] .tc-value { font-size: var(--fs-lg); font-weight: 700; font-variant-numeric: tabular-nums; }
#modal-root .dc-facts .tc-readonly { padding: 2px 0; font-size: var(--fs-md); font-weight: 600; color: var(--ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#modal-root .dc-facts .tc-readonly small { display: block; font-size: var(--fs-xs); font-weight: 500; color: var(--muted); }
#modal-root .dc-facts .dc-fact-missing .tc-readonly { color: var(--muted); font-weight: 500; white-space: normal; }
#modal-root .dc-facts .dc-fact-late { border-color: color-mix(in srgb, var(--danger-text) 35%, var(--line)); }
#modal-root .dc-facts .dc-fact-late .tc-readonly small { color: var(--danger-text); font-weight: 600; }
/* While a fact is being edited it takes the full row */
#modal-root .dc-facts > .tc-field:has(#dc-inline-form) { grid-column: 1 / -1; }

/* Text-style buttons inside cards ("Запланировать") */
#modal-root .btn.text-link {
  height: auto; min-height: 0; padding: 0; border: 0; background: none; box-shadow: none;
  color: var(--blue-text); font-size: var(--fs-sm); font-weight: 600;
}
#modal-root .btn.text-link:hover { text-decoration: underline; background: none; }

/* ---------- Deal: stage bar fits the width (current stage gets more room) ---------- */
@media (min-width: 761px) {
  #modal-root .deal-detail-modal .dc-stage-list { overflow: visible; gap: 4px; }
  #modal-root .deal-detail-modal .stage-arrow.dc-stage-button { flex: 1 1 0; min-width: 0; padding-inline: 8px 14px; }
  #modal-root .deal-detail-modal .stage-arrow.dc-stage-button[aria-pressed="true"] { flex-grow: 1.4; }
  /* Up to two lines so labels like «Выявление потребностей» stay readable */
  #modal-root .deal-detail-modal .dc-stage-label {
    display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; min-width: 0; overflow: hidden;
    white-space: normal; overflow-wrap: normal; word-break: normal; hyphens: auto; font-size: var(--fs-2xs); line-height: 1.15;
  }
}

/* ---------- Deal: upcoming activities inside the work block ---------- */
#modal-root .da-compose .da-upcoming {
  display: block;
  margin: var(--sp-3) 0 0;
  padding: var(--sp-3);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface-2);
  box-shadow: none;
}
#modal-root .da-compose .da-upcoming-head { display: flex; align-items: center; gap: var(--sp-2); margin-bottom: var(--sp-2); font-size: var(--fs-sm); font-weight: 650; color: var(--secondary); }
#modal-root .da-compose .da-upcoming-head b {
  display: inline-grid; place-items: center; min-width: 22px; height: 22px; padding: 0 7px; border-radius: var(--r-pill);
  background: var(--blue-soft); color: var(--blue-text); font-size: var(--fs-2xs); font-weight: 700;
}
#modal-root .da-compose .da-upcoming-empty { margin: 0; font-size: var(--fs-sm); color: var(--muted); }
#modal-root .da-compose .da-upcoming:not(:has(.da-upcoming-item)) .da-upcoming-head { margin-bottom: 4px; }

/* ---------- Deal: compact system changes in the feed ---------- */
#modal-root .da-event.da-compact { grid-template-columns: 30px minmax(0, 1fr); }
#modal-root .da-event.da-compact .da-event-icon { width: 22px; height: 22px; margin: 4px 4px 0; color: var(--muted); }
#modal-root .da-event.da-compact .da-event-icon .icon { width: 12px; height: 12px; }
#modal-root .da-event.da-compact .da-event-card { padding: 6px 10px; border: 0; background: transparent; }
#modal-root .da-event.da-compact .da-event-card > header { margin-bottom: 2px; }
#modal-root .da-event.da-compact .da-event-card > header > span { font-weight: 500; color: var(--muted); }
#modal-root .da-event.da-compact .da-event-body { font-size: var(--fs-xs); color: var(--secondary); }
#modal-root .da-event.da-compact .da-event-body b { font-weight: 600; color: var(--secondary); }
#modal-root .da-event.da-compact .da-event-body p { margin: 2px 0; }
#modal-root .da-event-body i { font-style: normal; color: var(--muted); }

/* Folded run of system changes */
#modal-root .da-fold { margin: -4px 0 0 40px; }
#modal-root .da-fold > summary {
  display: inline-flex; align-items: center; gap: 6px; min-height: 0; padding: 4px 10px;
  border-radius: var(--r-pill); background: var(--surface-3); color: var(--secondary);
  font-size: var(--fs-xs); font-weight: 600; cursor: pointer; list-style: none;
}
#modal-root .da-fold > summary::-webkit-details-marker { display: none; }
#modal-root .da-fold > summary::before { content: '▸'; }
#modal-root .da-fold[open] > summary::before { content: '▾'; }
#modal-root .da-fold[open] { display: grid; gap: 4px; margin-left: 0; }
#modal-root .da-fold[open] > summary { margin-left: 40px; width: max-content; }

/* ---------- Task card ---------- */
/* Overdue due date in the properties panel */
#modal-root .tc-field.tc-overdue .tc-edit,
#modal-root .tc-field.tc-overdue .tc-readonly { background: var(--danger-soft); }
#modal-root .tc-field.tc-overdue .tc-value,
#modal-root .tc-field.tc-overdue .tc-readonly { color: var(--danger-text); font-weight: 600; }
/* Empty files block is a single row "Файлы 0 · Прикрепить" (drag-and-drop onto it still works) */
#modal-root .tc-files:has(#task-file-list > .tc-empty:only-child) { padding-block: var(--sp-3); }
#modal-root .tc-files:has(#task-file-list > .tc-empty:only-child) #task-file-list { display: none; }
#modal-root .tc-files:has(#task-file-list > .tc-empty:only-child) .tc-section-head { margin: 0; }

/* "⋯" menu next to the primary task action (holds «В корзину») */
#modal-root .tc-more { position: relative; display: inline-flex; }
#modal-root .tc-more > summary { list-style: none; cursor: pointer; }
:root[data-theme] #modal-root .tc-more > summary.btn.icon-btn {
  width: var(--h-md); height: var(--h-md); min-height: var(--h-md); display: inline-grid; place-items: center;
  border: 1px solid var(--control-line); border-radius: var(--r-sm); background: var(--surface); color: var(--ink);
  box-shadow: 0 1px 2px var(--shadow-color);
}
:root[data-theme] #modal-root .tc-more > summary.btn.icon-btn:hover,
:root[data-theme] #modal-root .tc-more[open] > summary.btn.icon-btn { background: var(--surface-2); border-color: var(--blue-line); }
#modal-root .tc-more > summary::-webkit-details-marker { display: none; }
#modal-root .tc-more-menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 30; min-width: 180px; padding: 4px;
  border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface);
  box-shadow: 0 8px 24px var(--shadow-strong);
}
#modal-root .tc-more-item {
  display: flex; align-items: center; gap: var(--sp-2); width: 100%; height: 36px; padding: 0 var(--sp-3);
  border: 0; border-radius: var(--r-sm); background: none; color: var(--ink); font-size: var(--fs-sm); text-align: left; cursor: pointer;
}
#modal-root .tc-more-item:hover { background: var(--surface-2); }
#modal-root .tc-more-item.danger { color: var(--danger-text); }
#modal-root .tc-more-item .icon { width: 16px; height: 16px; }

/* ---------- Phones ---------- */
@media (max-width: 760px) {
  #modal-root .dc-facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #modal-root .da-fold, #modal-root .da-fold[open] > summary { margin-left: 0; }
}

/* ---------- Card actions in the header (deal, task, project, contact, company) ----------
   deal-rail.css pulls the action rail out to the left of the drawer on desktop; keep it inline at
   the right of the header instead: link · download · new tab · print … · close (last). */
@media (min-width: 901px) and (min-height: 395px) {
  :root #modal-root .modal .dc-action-rail {
    position: static;
    flex-direction: row;
    align-items: center;
    gap: var(--sp-1);
    margin-left: auto;
  }
  :root[data-theme] #modal-root .modal .modal-head .dc-action-rail .dc-rail-button {
    width: 36px; min-width: 36px; height: 36px; margin: 0;
    border: 1px solid var(--line); border-radius: var(--r-sm);
    background: var(--surface); color: var(--secondary); box-shadow: none;
  }
  :root[data-theme] #modal-root .modal .modal-head .dc-action-rail .dc-rail-button:hover {
    border-color: var(--control-line); background: var(--surface-2); color: var(--ink);
  }
  :root[data-theme] #modal-root .modal .modal-head .dc-action-rail .dc-rail-button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
  :root[data-theme] #modal-root .modal .modal-head .dc-action-rail .dc-rail-close {
    order: 99; width: 36px; height: 36px; margin: 0 0 0 var(--sp-2);
    border-color: var(--line); background: var(--surface); color: var(--ink);
  }
  :root[data-theme] #modal-root .modal .dc-action-rail .dc-rail-button > .icon { width: 18px; height: 18px; }
  /* Tooltip below the button instead of to the left */
  :root #modal-root .modal .dc-rail-button::after {
    inset-inline-end: 0; top: calc(100% + 6px); transform: none; z-index: 5;
    background: var(--ink); color: var(--surface);
  }
}

/* ---------- Deal card: less repetition ---------- */
/* The stage bar is self-explanatory: no «Стадия сделки» caption (read-only note stays) */
#modal-root .deal-detail-modal .dc-stage-selector-head > span { display: none; }
#modal-root .deal-detail-modal .dc-stage-selector-head:not(:has(small)) { display: none; }
/* Contacts list editor reads as an action under the contact cards */
#modal-root .dc-client [data-deal-field="contacts"] > .tc-label { display: none; }
:root[data-theme] #modal-root .dc-client [data-deal-field="contacts"] .tc-edit { background: none; padding-inline: 0; min-height: 32px; }
:root[data-theme] #modal-root .dc-client [data-deal-field="contacts"] .tc-value { color: var(--blue-text); font-weight: 600; font-size: var(--fs-sm); }
#modal-root .dc-client [data-deal-field="contacts"] .tc-pencil { display: none; }
#modal-root .dc-client [data-deal-field="contacts"]:has(#dc-inline-form) > .tc-label { display: block; }

/* ---------- Deal card: Дело / Звонок / Задача open in place ---------- */
#modal-root .dc-inline-slot:empty { display: none; }
#modal-root .dc-inline-slot { margin-top: var(--sp-3); }
#modal-root .dc-inline-action {
  padding: var(--sp-4); border: 1px solid var(--blue-line); border-radius: var(--r-md); background: var(--surface-2);
  gap: var(--sp-3);
}
#modal-root .dc-inline-action > h4 { margin: 0; font-size: var(--fs-md); }
#modal-root .dc-inline-action-buttons { display: flex; justify-content: flex-end; gap: var(--sp-2); }

/* ---------- Stage transition form (deal card) ---------- */
#modal-root .dc-stage-form .dc-transition-head {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px var(--sp-2); margin-bottom: var(--sp-3);
  font-size: var(--fs-md); color: var(--secondary);
}
#modal-root .dc-stage-form .dc-transition-head b { color: var(--ink); font-weight: 650; }
#modal-root .dc-stage-form .dc-transition-head small { flex-basis: 100%; font-size: var(--fs-xs); color: var(--muted); }
/* The target stage is already chosen on the bar: hide the select and the repeated captions */
#modal-root .dc-stage-form > .field:has(> select[name="stage"]),
#modal-root .dc-stage-form #dc-stage-fields > p.tc-empty { display: none; }
#modal-root .dc-stage-form:not(.is-attempted) .deal-stage-summary { display: none; }
/* Required fields stay neutral until the first attempt to save */
:root[data-theme] #modal-root .dc-stage-form:not(.is-attempted) .deal-stage-required :is(input:not([type=checkbox]), textarea, select, .filter-select-trigger) {
  border: 1px solid var(--control-line) !important; box-shadow: none !important;
}
#modal-root .dc-stage-form:not(.is-attempted) .deal-stage-field-hint { display: none; }
#modal-root .dc-stage-form:not(.is-attempted) :is(.dc-choices, .deal-contact-list).deal-stage-required { border-color: var(--line); }
#modal-root .dc-stage-form .dc-save-actions { gap: var(--sp-2); }
#modal-root .dc-stage-form .dc-save-actions > div { margin-right: auto; }

/* Undo in the toast after a stage change */
#toast .toast-undo {
  margin-left: var(--sp-3); padding: 2px 10px; border: 1px solid rgb(255 255 255 / .45); border-radius: var(--r-sm);
  background: transparent; color: inherit; font: inherit; font-weight: 700; cursor: pointer;
}
#toast .toast-undo:hover { background: rgb(255 255 255 / .15); }

/* ---------- КП files ---------- */
#modal-root .dc-kp { display: grid; gap: 6px; margin: var(--sp-2) 0 var(--sp-3); }
#modal-root .dc-kp-file { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px var(--sp-2); font-size: var(--fs-sm); }
#modal-root .dc-kp-file small { color: var(--muted); font-size: var(--fs-xs); }
#modal-root .dc-kp-upload { justify-self: start; display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
#modal-root .dc-kp-upload .icon { width: 15px; height: 15px; }
#modal-root .dc-kp-status { font-size: var(--fs-xs); color: var(--secondary); }
#modal-root .dc-kp-inline { margin-top: calc(var(--sp-2) * -1); grid-column: 1 / -1; }

/* Stage that needs no data: the transition form is not shown, the chosen stage pulses while saving */
#modal-root .dc-stage-edit-slot.dc-silent { display: none; }
#modal-root .dc-stage-button.dc-stage-pending { animation: dc-stage-pending 1s ease-in-out infinite; }
@keyframes dc-stage-pending { 50% { opacity: .55; } }
@media (prefers-reduced-motion: reduce) { #modal-root .dc-stage-button.dc-stage-pending { animation: none; opacity: .7; } }
