:root {
    --sidebar: #203651;
    --bg: #f4f7fc;
    --bubble-out: #e1edfc;
    --bubble-in: #ffffff;
    --accent: #365f94;
}

body { background: var(--bg); font-family: -apple-system, "Segoe UI", Roboto, sans-serif; }

.layout { display: flex; min-height: 100vh; }

.sidebar {
    position: sticky;
    top: 0;
    align-self: flex-start;
    height: 100dvh;
    overflow-y: auto;
    width: 220px; background: var(--sidebar); color: #e5e7eb;
    display: flex; flex-direction: column; padding: 16px 10px; flex-shrink: 0;
}
.brand { font-weight: 700; font-size: 18px; padding: 6px 12px 18px; color: #fff; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar .nav-item {
    color: #cbd5e1; text-decoration: none; padding: 10px 12px; border-radius: 8px;
    display: flex; align-items: center; gap: 10px; font-size: 14px;
}
.sidebar .nav-item:hover { background: #2c4768; color: #fff; }
.sidebar-footer { margin-top: auto; padding: 10px 4px 0; }

.content { flex-grow: 1; padding: 24px 28px; overflow-x: auto; min-width: 0; }

.crm-toolbar { display: flex; margin-bottom: 12px; }
#crm-sidebar-toggle { min-width: 34px; }
html[data-sidebar-collapsed="true"] .sidebar { display: none; }

@media (max-width: 767.98px) {
    html:not([data-sidebar-collapsed="false"]) .sidebar { display: none; }
    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        z-index: 1020;
        overflow-y: auto;
        box-shadow: 2px 0 12px rgba(0, 0, 0, .2);
    }
    .content { padding: 12px; }
    .crm-toolbar { position: relative; z-index: 1021; width: max-content; }
    html[data-sidebar-collapsed="false"] .crm-toolbar { margin-left: 220px; }
}

.layout--inline-nav {
  --chat-inset: 12px;
  --chat-header-height: 44px;
  --chat-toggle-size: 32px;
  --chat-nav-width: 220px;
  --chat-nav-offset: 0px;
  --chat-heading-padding: 14px;
  --chat-toggle-left: calc(var(--chat-nav-width) - var(--chat-toggle-size) - 12px);
  --chat-toggle-color: #dbe5f5;
  --chat-toggle-border: #52617a;
  position: relative;
}

html[data-sidebar-collapsed="true"] .layout--inline-nav {
  --chat-nav-offset: calc(0px - var(--chat-nav-width));
  --chat-heading-padding: 52px;
  --chat-toggle-left: calc(var(--chat-inset) + 9px);
  --chat-toggle-color: #475569;
  --chat-toggle-border: #c3cdd9;
}

.layout--inline-nav .content { padding: var(--chat-inset); }

html .layout--inline-nav .sidebar,
html[data-sidebar-collapsed="true"] .layout--inline-nav .sidebar {
  display: flex;
  width: var(--chat-nav-width);
  margin-left: var(--chat-nav-offset);
  transition: margin-left .22s ease;
}

.layout--inline-nav .crm-toolbar,
html[data-sidebar-collapsed="false"] .layout--inline-nav .crm-toolbar {
  position: absolute;
  top: calc(var(--chat-inset) + 1px + (var(--chat-header-height) - var(--chat-toggle-size)) / 2);
  left: var(--chat-toggle-left);
  z-index: 1021;
  margin: 0;
  transition: left .22s ease;
}

.layout--inline-nav #crm-sidebar-toggle {
  display: grid;
  place-items: center;
  width: var(--chat-toggle-size);
  min-width: var(--chat-toggle-size);
  height: var(--chat-toggle-size);
  padding: 0;
  color: var(--chat-toggle-color);
  border-color: var(--chat-toggle-border);
}

.layout--inline-nav #crm-sidebar-toggle:hover { background-color: rgba(127, 145, 170, .18); }

@media (max-width: 767.98px) {
  html:not([data-sidebar-collapsed="false"]) .layout--inline-nav {
    --chat-nav-offset: calc(0px - var(--chat-nav-width));
    --chat-heading-padding: 52px;
    --chat-toggle-left: calc(var(--chat-inset) + 9px);
    --chat-toggle-color: #475569;
    --chat-toggle-border: #c3cdd9;
  }

  html:not([data-sidebar-collapsed="false"]) .layout--inline-nav .sidebar { display: flex; }
  .layout--inline-nav .sidebar { box-shadow: none; }
  html[data-sidebar-collapsed="false"] .layout--inline-nav .sidebar {
    box-shadow: 2px 0 12px rgba(0, 0, 0, .2);
  }
}

.crm-page-heading {
  display: flex;
  align-items: center;
  min-height: var(--chat-header-height);
  padding-left: var(--chat-heading-padding);
  transition: padding-left .22s ease;
}

.deal-switch-menu {
  width: min(22rem, calc(100vw - 2rem));
  max-height: min(24rem, 60vh);
  overflow-y: auto;
  overflow-wrap: anywhere;
}

@media (prefers-reduced-motion: reduce) {
  html .layout--inline-nav .sidebar,
  html[data-sidebar-collapsed="true"] .layout--inline-nav .sidebar,
  .layout--inline-nav .crm-toolbar,
  html[data-sidebar-collapsed="false"] .layout--inline-nav .crm-toolbar,
  .crm-page-heading { transition: none; }
}

.layout--inline-nav .content > .alert {
  margin-left: calc(var(--chat-heading-padding) - 14px);
}

/* ---------- канбан ---------- */
.board-filters { position: relative; }
.board-filters summary { list-style: none; }
.board-filters summary::-webkit-details-marker { display: none; }
.board-filter-panel {
    position: absolute;
    right: 0;
    top: calc(100% + 8px);
    z-index: 12;
    display: grid;
    gap: 12px;
    width: min(620px, calc(100vw - 36px));
    max-height: min(65dvh, 560px);
    overflow-y: auto;
    padding: 14px;
    background: var(--bs-body-bg, #fff);
    border: 1px solid var(--bs-border-color, #dce6f4);
    border-radius: 10px;
    box-shadow: 0 10px 28px rgba(12, 24, 43, .18);
}
.board-filter-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px 12px; }
@media (max-width: 420px) {
    .board-filter-fields { grid-template-columns: minmax(0, 1fr); }
}
.board { display: flex; gap: 12px; align-items: flex-start; overflow-x: auto; padding-bottom: 16px; }
.board-col { background: #e8eff8; border-radius: 10px; width: 260px; flex-shrink: 0; }
.board-col-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; font-weight: 600; font-size: 14px; }
.board-col-head .dot { width: 8px; height: 8px; border-radius: 50%; }
.board-col-body { padding: 0 8px 10px; min-height: 60px; display: flex; flex-direction: column; gap: 8px; }
.deal-card {
    background: #fff; border-radius: 8px; padding: 10px; color: inherit;
    text-decoration: none; font-size: 14px; box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
.deal-card:hover { box-shadow: 0 3px 10px rgba(0,0,0,.12); color: inherit; }
.deal-card-link { padding-inline-end: 5rem; }
.deal-card-products > summary { position: absolute; top: 10px; right: 10px; list-style: none; }
.deal-card-products > summary::-webkit-details-marker { display: none; }
.deal-ghost { opacity: .4; }

/* ---------- чаты ---------- */
/* flex-direction: row обязателен: Bootstrap у .card ставит column,
   из-за чего колонки чатов уезжали друг под друга */
.chat-layout {
    display: flex;
    flex-direction: row;
    height: calc(100vh - 100px);
    overflow: hidden;
    padding: 0;
}

.chat-sidebar {
    width: 340px;
    min-width: 300px;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    flex-shrink: 0;
}

.chat-list { overflow-y: auto; flex: 1 1 auto; }

.chat-item {
    display: flex; gap: 10px; align-items: center; padding: 10px 12px;
    text-decoration: none; color: inherit; border-bottom: 1px solid #f1f3f5;
}
.chat-item:hover, .chat-item.active { background: #eef4ff; }

.chat-avatar {
    width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; flex-shrink: 0; font-size: 15px;
}

.chat-empty { flex: 1 1 auto; display: flex; align-items: center; justify-content: center; }

.chat-main {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.chat-head {
    display: flex; align-items: center; gap: 10px; padding: 10px 16px;
    border-bottom: 1px solid #e5e7eb; flex-shrink: 0;
}

.chat-messages {
    flex: 1 1 auto;
    min-height: 0;                 /* без этого лента не прокручивается внутри колонки */
    overflow-y: auto;
    padding: 16px 22px;
    background: #e9edf2;
}

.chat-input {
    display: flex; gap: 8px; padding: 10px 14px;
    border-top: 1px solid #e5e7eb; flex-shrink: 0;
}

.bubble-row { display: flex; margin-bottom: 8px; }
.bubble-row.out { justify-content: flex-end; }
.bubble {
    max-width: 65%; padding: 7px 10px 18px; border-radius: 10px; position: relative;
    font-size: 14px; box-shadow: 0 1px 1px rgba(0,0,0,.06); word-wrap: break-word;
}
.bubble-in { background: var(--bubble-in); border-top-left-radius: 2px; }
.bubble-out { background: var(--bubble-out); border-top-right-radius: 2px; }
.bubble-meta .read { color: var(--accent); }
.bubble-file { display: block; font-size: 12px; margin-top: 4px; }

/* ---------- прочее ---------- */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #203651; }
.login-card { background: #fff; padding: 28px; border-radius: 12px; width: 340px; }

.deal-client-editor[open] + .deal-client-data {
    display: none;
}

/* Сводка */
.crm-dashboard {
    max-width: 1440px;
    margin-inline: auto;
    container-type: inline-size;
}

.dashboard-metrics,
.dashboard-panels,
.dashboard-panel-stack {
    display: grid;
    gap: .75rem;
    min-width: 0;
}

.dashboard-metrics {
    grid-template-columns: repeat(12, minmax(0, 1fr));
    grid-template-areas:
        "sales sales sales sales sales orders orders orders messages messages messages messages"
        "sales sales sales sales sales unread unread response response response response response";
}

.dashboard-panels { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); align-items: start; }

.dashboard-card {
    min-width: 0;
    border: 1px solid var(--bs-border-color);
    border-radius: 1rem;
    background: var(--bs-body-bg);
    box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .04);
    overflow: hidden;
}

.dashboard-metric {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: clamp(1rem, 2vw, 1.45rem);
}
.dashboard-metric-sales {
    grid-area: sales;
    min-height: 16rem;
    border-color: rgba(var(--crm-rgb), .35);
    background: linear-gradient(145deg, rgba(var(--crm-rgb), .23), rgba(var(--crm-rgb), .08)), var(--bs-body-bg);
}
.dashboard-metric-sales .dashboard-number { font-size: clamp(2.2rem, 4vw, 4rem); }
.dashboard-metric-orders { grid-area: orders; }
.dashboard-metric-messages { grid-area: messages; background: var(--bs-secondary-bg); }
.dashboard-metric-unread { grid-area: unread; background: var(--bs-secondary-bg); }
.dashboard-metric-response {
    grid-area: response;
    background: linear-gradient(rgba(var(--crm-rgb), .08), rgba(var(--crm-rgb), .08)), var(--bs-body-bg);
}

.dashboard-metric-label { font-weight: 600; overflow-wrap: anywhere; }
.dashboard-metric-label i { color: var(--crm-action); margin-inline-end: .3rem; }
.dashboard-number {
    font-size: clamp(1.5rem, 2.4vw, 2.4rem);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
    margin-block: .6rem .35rem;
    overflow-wrap: anywhere;
}

.dashboard-panel-title {
    margin: 0;
    padding: 1rem 1.25rem;
    font-size: 1rem;
    font-weight: 700;
    border-bottom: 1px solid var(--bs-border-color);
}

.dashboard-table > :not(caption) > * > * { padding: .7rem 1.25rem; }
.dashboard-table thead th { font-size: .82rem; color: var(--bs-secondary-color); }
.dashboard-table tbody tr:last-child td { border-bottom: 0; }

@container (max-width: 730px) {
    .dashboard-metrics {
        grid-template-columns: repeat(6, minmax(0, 1fr));
        grid-template-areas:
            "sales sales sales sales sales sales"
            "orders orders messages messages messages messages"
            "unread unread response response response response";
    }
    .dashboard-metric-sales { min-height: 10rem; }
    .dashboard-panels { grid-template-columns: minmax(0, 1fr); }
}

@container (max-width: 390px) {
    .dashboard-metrics {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-areas:
            "sales sales"
            "orders messages"
            "unread response";
    }
}
