/*
 * CRM core styles (public/js/crm-core.js). GWiZ(TM) -- shared byte-for-byte by
 * gwiz-hub, hollend-hub-backend and STRUCTiO. Nothing here names an app's colours:
 * each app sets the --crm-* properties below from its own tokens (the hub does it
 * in style.css), and the fallbacks are only there so the drawer is legible if an
 * app forgets one.
 *
 *   --crm-surface         drawer background          --crm-accent        primary action
 *   --crm-surface-raised  cards, inputs              --crm-accent-text   text on the accent
 *   --crm-text            body text                  --crm-success / --crm-warning /
 *   --crm-muted           secondary text               --crm-danger / --crm-info
 *   --crm-border          hairlines                  --crm-radius        corner radius
 *   --crm-backdrop        behind the drawer          --crm-font          font stack
 *   --crm-focus           focus ring                 --crm-shadow        drawer shadow
 */

.crm-drawer {
  --_surface: var(--crm-surface, #ffffff);
  --_raised: var(--crm-surface-raised, #f4f6fa);
  --_text: var(--crm-text, #111827);
  --_muted: var(--crm-muted, #5b6474);
  --_border: var(--crm-border, rgba(0, 0, 0, 0.12));
  --_accent: var(--crm-accent, #4f46e5);
  --_accent-text: var(--crm-accent-text, #ffffff);
  --_success: var(--crm-success, #0f7b55);
  --_warning: var(--crm-warning, #9a5b00);
  --_danger: var(--crm-danger, #c0262d);
  --_info: var(--crm-info, #0369a1);
  --_radius: var(--crm-radius, 10px);
  --_focus: var(--crm-focus, #4f46e5);
  position: fixed;
  inset: 0 0 0 auto;
  margin: 0 0 0 auto;
  width: min(600px, 100vw);
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--_border);
  background: var(--_surface);
  color: var(--_text);
  font-family: var(--crm-font, inherit);
  box-shadow: var(--crm-shadow, -12px 0 40px rgba(0, 0, 0, 0.25));
  overflow: hidden;
}
.crm-drawer[open] { display: flex; animation: crm-slide-in 0.22s ease-out; }
.crm-drawer::backdrop { background: var(--crm-backdrop, rgba(15, 23, 42, 0.45)); }
@keyframes crm-slide-in { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .crm-drawer[open] { animation: none; } }

/* Phones and iPad portrait: a full-width sheet. */
@media (max-width: 640px), (max-width: 1024px) and (orientation: portrait) {
  .crm-drawer { width: 100vw; border-left: 0; }
}

.crm-drawer__panel { display: flex; flex-direction: column; width: 100%; min-height: 0; }
.crm-drawer__head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 20px 24px 16px; border-bottom: 1px solid var(--_border);
}
.crm-drawer__heading { min-width: 0; }
.crm-drawer__eyebrow { margin: 0 0 6px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 0.8125rem; }
.crm-drawer__title { margin: 0; font-size: 1.375rem; line-height: 1.25; overflow-wrap: anywhere; }
.crm-drawer__title:focus { outline: none; }
.crm-drawer__title:focus-visible { outline: 2px solid var(--_focus); outline-offset: 2px; }
.crm-drawer__sub { margin: 4px 0 0; color: var(--_muted); font-size: 0.9rem; overflow-wrap: anywhere; }
.crm-drawer__close {
  flex: none; display: inline-grid; place-items: center; width: 40px; height: 40px;
  border-radius: 50%; border: 1px solid var(--_border); background: var(--_raised); color: var(--_text); cursor: pointer; font-size: 1.25rem;
}
.crm-drawer__body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 8px 24px 32px; }
@media (max-width: 640px) {
  .crm-drawer__head { padding: 16px; }
  .crm-drawer__body { padding: 4px 16px 24px; }
}

.crm-notice { margin: 12px 24px 0; padding: 10px 12px; border-radius: var(--_radius); font-size: 0.875rem; border: 1px solid var(--_border); background: var(--_raised); }
.crm-notice[data-kind="warning"] { border-color: var(--_warning); color: var(--_text); background: color-mix(in srgb, var(--_warning) 12%, var(--_surface)); }
.crm-notice[data-kind="error"] { border-color: var(--_danger); background: color-mix(in srgb, var(--_danger) 10%, var(--_surface)); }

.crm-section { padding: 18px 0; border-bottom: 1px solid var(--_border); }
.crm-section:last-child { border-bottom: 0; }
.crm-section__head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.crm-section__head h3 { margin: 0; font-size: 0.8125rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--_muted); }
.crm-subhead { margin: 14px 0 6px; font-size: 0.875rem; }

/* Badges: stage tones by position, won and lost; sources. */
.crm-badge {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; border-radius: 999px;
  font-size: 0.75rem; font-weight: 600; line-height: 1.6; white-space: nowrap;
  --_tone: var(--_accent, var(--crm-accent, #4f46e5));
  color: var(--_tone); background: color-mix(in srgb, var(--_tone) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--_tone) 40%, transparent);
}
[data-crm-tone="open-0"] { --_tone: var(--crm-accent, #4f46e5); }
[data-crm-tone="open-1"] { --_tone: var(--crm-info, #0369a1); }
[data-crm-tone="open-2"] { --_tone: var(--crm-warning, #9a5b00); }
[data-crm-tone="open-3"], [data-crm-tone="open-4"], [data-crm-tone="open-5"] { --_tone: var(--crm-info, #0369a1); }
[data-crm-tone="won"] { --_tone: var(--crm-success, #0f7b55); }
[data-crm-tone="lost"] { --_tone: var(--crm-danger, #c0262d); }
[data-crm-tone="neutral"] { --_tone: var(--crm-muted, #5b6474); }
.crm-badge--source { --_tone: var(--crm-muted, #5b6474); font-weight: 500; }
.crm-badge--source[data-crm-source="email"] { --_tone: var(--crm-info, #0369a1); }

/* The pipeline stepper. */
.crm-stepper { list-style: none; margin: 0 0 12px; padding: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; }
.crm-step {
  width: 100%; min-height: 48px; display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  border-radius: var(--_radius); border: 1px solid var(--_border); background: var(--_raised); color: var(--_text);
  font: inherit; font-size: 0.875rem; text-align: left; cursor: pointer;
}
.crm-step:hover:not(:disabled) { border-color: var(--_tone, var(--_accent)); }
.crm-step:disabled { cursor: default; opacity: 0.55; }
.crm-step.is-done { opacity: 0.8; }
.crm-step.is-current { opacity: 1; border-color: var(--_tone); background: color-mix(in srgb, var(--_tone) 16%, var(--_surface)); font-weight: 600; }
.crm-step__dot {
  flex: none; display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  font-size: 0.75rem; background: color-mix(in srgb, var(--_tone, var(--_accent)) 22%, transparent); color: var(--_tone, var(--_accent));
}
.crm-step.is-current .crm-step__dot { background: var(--_tone); color: var(--_surface); }

/* Buttons and inputs. */
.crm-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; padding: 8px 14px;
  border-radius: var(--_radius); border: 1px solid transparent; font: inherit; font-size: 0.875rem; font-weight: 600;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.crm-btn--sm { min-height: 34px; padding: 5px 10px; font-size: 0.8125rem; }
.crm-btn--primary { background: var(--_accent); color: var(--_accent-text); }
.crm-btn--secondary { background: var(--_raised); color: var(--_text); border-color: var(--_border); }
.crm-btn--ghost { background: transparent; color: var(--_muted); }
.crm-btn--ghost:hover { color: var(--_text); background: var(--_raised); }
.crm-btn--danger-ghost { background: transparent; color: var(--_danger); border-color: color-mix(in srgb, var(--_danger) 45%, transparent); }
.crm-btn--danger { background: var(--_danger); color: var(--_surface); }
.crm-btn .material-symbols-outlined, .crm-chip .material-symbols-outlined, .crm-due .material-symbols-outlined,
.crm-muted .material-symbols-outlined { font-size: 1.05em; }

.crm-drawer :focus-visible, .crm-confirm :focus-visible { outline: 2px solid var(--_focus); outline-offset: 2px; }

.crm-input {
  width: 100%; min-height: 40px; padding: 8px 10px; border-radius: var(--_radius); border: 1px solid var(--_border);
  background: var(--_raised); color: var(--_text); font: inherit; font-size: 0.9rem; box-sizing: border-box;
}
.crm-input--inline { width: auto; min-width: 200px; }
.crm-textarea { resize: vertical; min-height: 76px; line-height: 1.45; }
.crm-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.crm-field label { font-size: 0.8125rem; color: var(--_muted); font-weight: 600; }
.crm-field--inline { flex-direction: row; align-items: center; gap: 10px; margin-top: 12px; }
.crm-form { display: flex; flex-direction: column; gap: 8px; }
.crm-form--task {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; align-items: end;
  margin-top: 12px; padding: 12px; border: 1px dashed var(--_border); border-radius: var(--_radius);
}
.crm-form--task .crm-field--grow { grid-column: 1 / -1; }
.crm-form--task > .crm-btn, .crm-form--task > .crm-row { grid-column: 1 / -1; justify-self: start; }
.crm-form--edit { margin-top: 0; }
@media (max-width: 480px) { .crm-form--task { grid-template-columns: 1fr; } }

.crm-row { display: flex; align-items: center; gap: 8px; }
.crm-row--wrap { flex-wrap: wrap; }
.crm-row--end { justify-content: space-between; flex-wrap: wrap; }
.crm-hint { margin: 0; font-size: 0.8125rem; color: var(--_muted); }
.crm-muted { color: var(--_muted); display: inline-flex; align-items: center; gap: 4px; }
.crm-count { font-size: 0.8125rem; color: var(--_muted); }
.crm-empty { color: var(--_muted); font-size: 0.875rem; padding: 6px 0; }

.crm-details { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 16px; margin: 0; font-size: 0.9rem; }
.crm-details dt { color: var(--_muted); }
.crm-details dd { margin: 0; overflow-wrap: anywhere; }
.crm-details a { color: var(--_accent); }
.crm-message, .crm-note { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5; font-size: 0.9rem; }
.crm-message { padding: 12px; border-radius: var(--_radius); background: var(--_raised); max-height: 280px; overflow-y: auto; }

/* Tasks. */
.crm-tasks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.crm-task { padding: 10px 12px; border: 1px solid var(--_border); border-radius: var(--_radius); background: var(--_raised); display: flex; flex-direction: column; gap: 6px; }
.crm-task__main { display: flex; align-items: flex-start; gap: 10px; }
.crm-task__main input[type="checkbox"] { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--_accent); flex: none; }
.crm-task__title { flex: 1; min-width: 0; font-weight: 600; overflow-wrap: anywhere; cursor: pointer; }
.crm-task.is-done .crm-task__title { text-decoration: line-through; color: var(--_muted); font-weight: 500; }
.crm-task__meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; padding-left: 30px; font-size: 0.8125rem; }
.crm-task__tools { display: flex; gap: 2px; margin: -4px -6px 0 auto; flex: none; }
.crm-due { display: inline-flex; align-items: center; gap: 4px; color: var(--_text); }
.crm-due--soon { color: var(--_warning); font-weight: 600; }
.crm-due--overdue { color: var(--_danger); font-weight: 600; }
.crm-chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 0.75rem; font-weight: 600; }
.crm-chip--success { color: var(--_success); background: color-mix(in srgb, var(--_success) 14%, transparent); }
.crm-chip--warning { color: var(--_warning); background: color-mix(in srgb, var(--_warning) 14%, transparent); }

/* Activity. */
.crm-activity { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; }
.crm-activity__item { display: flex; gap: 10px; padding: 10px 0; border-top: 1px solid var(--_border); }
.crm-activity__icon {
  flex: none; display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
  background: var(--_raised); color: var(--_muted); font-size: 0.9rem;
}
.crm-activity__icon .material-symbols-outlined { font-size: 16px; }
.crm-activity__item--note .crm-activity__icon { color: var(--_accent); }
.crm-activity__item--stage .crm-activity__icon { color: var(--_info); }
.crm-activity__item--warning .crm-activity__icon { color: var(--_warning); }
.crm-activity__main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.crm-activity__line { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 8px; font-size: 0.875rem; }
.crm-activity__item--note .crm-activity__text { font-weight: 600; }
.crm-activity__time { color: var(--_muted); font-size: 0.75rem; }
.crm-note { padding: 8px 10px; border-radius: var(--_radius); background: var(--_raised); }

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

/* The page behind does not scroll while the drawer is open. */
html.crm-drawer-open, html.crm-drawer-open body { overflow: hidden; }

/* The drawer's own confirmation (no native confirm()): a small modal above it. */
.crm-confirm {
  --_surface: var(--crm-surface, #ffffff);
  --_raised: var(--crm-surface-raised, #f4f6fa);
  --_text: var(--crm-text, #111827);
  --_muted: var(--crm-muted, #5b6474);
  --_border: var(--crm-border, rgba(0, 0, 0, 0.12));
  --_accent: var(--crm-accent, #4f46e5);
  --_accent-text: var(--crm-accent-text, #ffffff);
  --_danger: var(--crm-danger, #c0262d);
  --_radius: var(--crm-radius, 10px);
  --_focus: var(--crm-focus, #4f46e5);
  /* Centred explicitly: an app's reset (margin: 0 on everything) would otherwise
     pin a modal <dialog> to the top-left corner. */
  position: fixed; inset: 0; margin: auto; height: fit-content; max-height: calc(100dvh - 32px); overflow: auto;
  box-sizing: border-box; width: min(420px, calc(100vw - 32px)); padding: 20px; border: 1px solid var(--_border); border-radius: var(--_radius);
  background: var(--_surface); color: var(--_text); font-family: var(--crm-font, inherit);
  box-shadow: var(--crm-shadow, 0 12px 40px rgba(0, 0, 0, 0.25));
}
.crm-confirm::backdrop { background: var(--crm-backdrop, rgba(15, 23, 42, 0.45)); }
.crm-confirm__title { margin: 0 0 8px; font-size: 1.125rem; line-height: 1.3; }
.crm-confirm__msg { margin: 0 0 18px; color: var(--_muted); line-height: 1.5; overflow-wrap: anywhere; white-space: pre-wrap; }
.crm-confirm__actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.crm-confirm .crm-btn { min-height: 44px; min-width: 44px; padding: 10px 16px; }
