:root {
    --bg: #f6f7f8;
    --surface: #ffffff;
    --surface-soft: #f8f9fa;
    --text: #1f2933;
    --muted: #677481;
    --line: #d8dee4;
    --line-strong: #c7d0d9;
    --brand: #0f8f67;
    --brand-strong: #0b6f51;
    --accent: #eef7f3;
    --warn: #e5942a;
    --danger: #d9534f;
    --bubble-in: #ffffff;
    --bubble-out: #dcf8c6;
    --shadow: 0 12px 30px rgba(17, 24, 39, 0.06);
    --radius: 14px;
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
}

body {
    margin: 0;
    font-family: "Segoe UI", Tahoma, sans-serif;
    font-size: 13px;
    color: var(--text);
    background: linear-gradient(180deg, #fafbfc 0%, #f4f6f8 100%);
}

body.overlay-open {
    overflow: hidden;
}

a {
    color: inherit;
    text-decoration: none;
}

svg {
    width: 14px;
    height: 14px;
    fill: currentColor;
    display: block;
}

.auth-page {
    display: grid;
    place-items: center;
    min-height: 100vh;
    padding: 16px;
}

.auth-shell {
    width: min(100%, 420px);
}

.auth-shell--wide {
    width: min(100%, 960px);
}

.auth-card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 18px;
    box-shadow: var(--shadow);
    padding: 20px;
}

.auth-card__intro h1,
.company-card h2,
.thread-list-card__header h2,
.conversation-empty h3,
.overlay__header h3,
.conversation-detail__header h3 {
    margin: 0;
}

.auth-card__intro p:last-child {
    color: var(--muted);
}

.eyebrow {
    margin: 0 0 4px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
}

.auth-form,
.field,
.overlay-form {
    display: grid;
    gap: 8px;
}

.auth-form {
    margin-top: 16px;
}

.auth-form input,
.field input,
.field select,
.field textarea,
.thread-search input,
.quick-send-form input,
.quick-send-form select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 9px 10px;
    background: #fff;
    color: var(--text);
    font: inherit;
}

.field textarea {
    resize: vertical;
}

.primary-btn,
.toolbar-btn,
.mini-btn {
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    cursor: pointer;
    font: inherit;
}

.toolbar-btn {
    position: relative;
    z-index: 4;
}

.primary-btn,
.toolbar-btn {
    border-radius: 10px;
    padding: 9px 12px;
    font-weight: 600;
}

.toolbar-btn--primary,
.primary-btn {
    background: var(--brand);
    border-color: var(--brand);
    color: #fff;
}

.mini-btn {
    border-radius: 8px;
    padding: 6px 10px;
}

.error-box {
    margin-top: 10px;
    padding: 10px 12px;
    border-radius: 10px;
    background: rgba(217, 83, 79, 0.1);
    color: #8c2522;
}

.company-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 10px;
    margin-top: 16px;
}

.company-card {
    display: block;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: var(--shadow);
    padding: 14px;
}

.text-link {
    display: inline-flex;
    margin-top: 14px;
    color: var(--brand-strong);
    font-weight: 600;
}

.crm-shell {
    min-height: 100vh;
    display: grid;
    grid-template-rows: auto 1fr;
}

.crm-header {
    padding: 8px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}

.crm-header__line {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 28px;
    white-space: nowrap;
    overflow-x: auto;
}

.crm-header__item,
.crm-header__link,
.crm-header__divider {
    font-size: 12px;
}

.crm-header__item--brand,
.crm-header__link {
    font-weight: 600;
}

.crm-header__link {
    color: var(--brand-strong);
}

.crm-main {
    padding: 10px;
}

.crm-app {
    display: grid;
    gap: 10px;
}

.crm-toolbar {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}

.crm-menu {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.crm-menu__item {
    position: relative;
}

.crm-menu__trigger {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--text);
    border-radius: 10px;
    padding: 8px 10px;
    cursor: pointer;
    font: inherit;
    font-weight: 600;
    position: relative;
    z-index: 2;
}

.crm-menu__trigger.is-active,
.crm-menu__link.is-active {
    background: var(--accent);
    border-color: #b9d8cb;
    color: var(--brand-strong);
}

.crm-menu__dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    min-width: 170px;
    display: none;
    z-index: 20;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: var(--shadow);
    padding: 4px;
    z-index: 3;
}

.crm-menu__item:hover .crm-menu__dropdown,
.crm-menu__item:focus-within .crm-menu__dropdown {
    display: grid;
}

.crm-menu__link {
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    color: var(--text);
    border-radius: 8px;
    padding: 7px 9px;
    cursor: pointer;
    font: inherit;
}

.crm-toolbar__actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.crm-workspace {
    display: grid;
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    gap: 10px;
    min-height: calc(100vh - 120px);
    height: calc(100vh - 120px);
    max-height: calc(100vh - 120px);
    overflow: hidden;
}

.thread-list-card,
.conversation-card,
.overlay__panel {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.thread-list-card {
    display: grid;
    grid-template-rows: auto auto 1fr;
    min-height: 0;
    height: 100%;
}

.thread-list-card__header,
.conversation-detail__header,
.overlay__header {
    display: flex;
    justify-content: space-between;
    gap: 10px;
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid var(--line);
}

.overlay__header {
    position: sticky;
    top: 0;
    z-index: 3;
    background: var(--surface);
}

.thread-list-card__header p,
.conversation-empty p,
.template-row p,
.conversation-detail__header p {
    margin: 2px 0 0;
    color: var(--muted);
}

.thread-search {
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
}

.thread-list {
    min-height: 0;
    overflow: auto;
    padding: 6px;
    display: grid;
    gap: 6px;
}

.thread-row {
    width: 100%;
    text-align: left;
    border: 1px solid transparent;
    background: var(--surface-soft);
    border-radius: 10px;
    padding: 10px;
    cursor: pointer;
    font: inherit;
}

.thread-row.is-active {
    border-color: #b9d8cb;
    background: var(--accent);
}

.thread-row__top {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: center;
}

.thread-row__meta,
.thread-row__preview {
    color: var(--muted);
    margin-top: 4px;
    line-height: 1.35;
}

.thread-row__badge {
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--brand);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
}

.conversation-card {
    display: grid;
    min-height: 0;
    height: 100%;
    overflow: hidden;
}

.conversation-empty {
    display: grid;
    place-items: center;
    height: 100%;
    min-height: 0;
    padding: 24px;
    text-align: center;
}

.conversation-detail {
    display: grid;
    grid-template-rows: auto auto 1fr auto;
    min-height: 0;
    height: 100%;
    overflow: hidden;
}

.conversation-detail.is-hidden,
.overlay.is-hidden {
    display: none;
}

.conversation-detail__actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.conversation-info {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    padding: 10px 12px;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.conversation-info__chip {
    display: inline-flex;
    align-items: center;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 5px 9px;
    background: var(--surface-soft);
    color: var(--muted);
}

.conversation-messages {
    background:
        linear-gradient(rgba(255,255,255,0.86), rgba(255,255,255,0.86)),
        linear-gradient(45deg, #f7f7f7 25%, transparent 25%, transparent 50%, #f7f7f7 50%, #f7f7f7 75%, transparent 75%, transparent);
    background-size: auto, 14px 14px;
    overflow: auto;
    min-height: 0;
    padding: 14px 16px;
    display: grid;
    gap: 10px;
}

.message-bubble {
    max-width: min(70%, 560px);
    border-radius: 10px;
    padding: 9px 10px 7px;
    box-shadow: 0 1px 2px rgba(17, 24, 39, 0.08);
}

.message-bubble--incoming {
    justify-self: start;
    background: var(--bubble-in);
}

.message-bubble--outgoing {
    justify-self: end;
    background: var(--bubble-out);
}

.message-bubble__text {
    white-space: pre-wrap;
    line-height: 1.45;
}

.message-bubble__meta {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 6px;
    color: var(--muted);
    font-size: 11px;
}

.message-bubble__error {
    margin-top: 5px;
    color: var(--danger);
    font-size: 11px;
}

.conversation-composer {
    padding: 10px 12px;
    border-top: 1px solid var(--line);
    background: var(--surface);
    position: sticky;
    bottom: 0;
    z-index: 2;
}

.quick-send-form {
    display: grid;
    grid-template-columns: 180px minmax(0, 1fr) auto;
    gap: 8px;
    align-items: end;
}

.quick-send-form textarea,
.quick-send-form select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 9px 10px;
    background: #fff;
    color: var(--text);
    font: inherit;
}

.quick-send-form textarea {
    min-height: 42px;
    max-height: 112px;
    resize: vertical;
}

.overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
}

.overlay__backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.38);
}

.overlay__panel {
    position: absolute;
    top: 50%;
    left: 50%;
    width: min(720px, calc(100vw - 24px));
    max-height: calc(100vh - 24px);
    overflow: hidden;
    transform: translate(-50%, -50%);
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
}

.overlay__panel--wide {
    width: min(1020px, calc(100vw - 24px));
}

.overlay__panel--templates {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
}

.overlay__panel--campaigns {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
}

.overlay__body {
    padding: 12px;
    min-height: 0;
    overflow: auto;
}

.overlay__body--contacts {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 10px;
    max-height: calc(100vh - 140px);
    min-height: 420px;
    overflow: hidden;
}

.overlay__body--split {
    display: grid;
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    gap: 12px;
    max-height: calc(100vh - 120px);
    min-height: 460px;
    min-width: 0;
    overflow: hidden;
    padding: 12px;
}

.overlay__close {
    border: 0;
    background: transparent;
    font-size: 22px;
    line-height: 1;
    cursor: pointer;
    color: var(--muted);
}

.template-list-panel,
.template-form-panel {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
    background: var(--surface-soft);
    min-width: 0;
}

.template-list-panel {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    overflow: hidden;
}

.template-form-panel {
    display: grid;
    grid-template-rows: minmax(0, 1fr);
    overflow: hidden;
}

.template-list-panel__toolbar,
.overlay-form__actions,
.template-row__actions,
.field-grid {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.template-list-panel__toolbar {
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.template-list-panel__toolbar-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.template-list {
    display: grid;
    gap: 8px;
    overflow: auto;
    min-height: 0;
    padding-right: 2px;
}

#crmCampaignList {
    max-height: 300px;
}

.template-row {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: flex-start;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 9px;
    background: #fff;
    cursor: pointer;
}

.template-row.is-active {
    border-color: #b9d8cb;
    background: var(--accent);
}

.template-row strong {
    display: block;
}

.field-grid > .field {
    flex: 1 1 220px;
}

.template-form-panel .overlay-form {
    min-height: 0;
    overflow: auto;
    padding-right: 2px;
}

.field--hidden {
    display: none;
}

.field--inline {
    display: flex;
    align-items: center;
    gap: 8px;
}

.field--inline input {
    width: auto;
}

.mapping-panel {
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    overflow: hidden;
}

.mapping-panel__head {
    padding: 10px;
    border-bottom: 1px solid var(--line);
    background: var(--surface-soft);
    display: grid;
    gap: 2px;
}

.mapping-panel__head span {
    color: var(--muted);
    font-size: 12px;
}

.mapping-rows {
    display: grid;
    max-height: 320px;
    overflow: auto;
}

.mapping-row {
    display: grid;
    grid-template-columns: minmax(120px, 1fr) minmax(110px, 0.75fr) minmax(150px, 1fr) minmax(150px, 1fr) minmax(140px, 0.9fr);
    gap: 8px;
    padding: 10px;
    border-top: 1px solid var(--line);
    align-items: center;
}

.mapping-row:first-child {
    border-top: 0;
}

.mapping-row__label {
    display: grid;
    gap: 2px;
    font-weight: 600;
}

.mapping-row__label small {
    color: var(--muted);
    font-size: 11px;
    font-weight: 400;
}

.mapping-row select,
.mapping-row input {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 8px 9px;
    background: #fff;
    color: var(--text);
    font: inherit;
}

.mapping-row__preview {
    border: 1px dashed var(--line-strong);
    border-radius: 8px;
    padding: 8px 9px;
    min-height: 36px;
    background: #fafcfd;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.35;
    word-break: break-word;
}

.message-preview-panel {
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    min-height: 140px;
    padding: 12px;
    white-space: pre-wrap;
    line-height: 1.5;
    color: var(--text);
}

.empty-state {
    color: var(--muted);
    text-align: center;
    padding: 16px;
}

.contacts-toolbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 2;
    background: var(--surface);
    padding-bottom: 4px;
}

.contacts-toolbar__left,
.contacts-toolbar__actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.contacts-toolbar__left {
    min-width: 0;
}

.contacts-toolbar input,
.contacts-toolbar select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 9px 10px;
    background: #fff;
    color: var(--text);
    font: inherit;
}

#crmContactsPerPage {
    width: 86px;
    flex: 0 0 86px;
}

.contacts-table-wrap {
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 10px;
    min-height: 0;
}

.contacts-table {
    width: 100%;
    border-collapse: collapse;
}

.contacts-table th,
.contacts-table td {
    padding: 9px 10px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    font-size: 12px;
}

.contacts-table th {
    background: var(--surface-soft);
    position: sticky;
    top: 0;
}

.contacts-table__sortable {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

.contacts-table__sortable::after {
    content: attr(data-sort-icon);
    margin-left: 6px;
    color: var(--muted);
    font-size: 11px;
}

.contacts-pagination {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    position: sticky;
    bottom: 0;
    background: var(--surface);
    padding-top: 4px;
}

.meta-status {
    min-height: 20px;
    color: var(--brand-strong);
    font-size: 12px;
}

.icon-btn {
    min-width: 34px;
    height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
}

.toast-stack {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 90;
    display: grid;
    gap: 8px;
    width: min(320px, calc(100vw - 32px));
}

.toast {
    border-radius: 10px;
    padding: 10px 12px;
    color: #fff;
    box-shadow: var(--shadow);
    transform: translateY(0);
    opacity: 1;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast--success {
    background: var(--brand);
}

.toast--error {
    background: var(--danger);
}

.toast--info {
    background: #334155;
}

.toast.is-leaving {
    opacity: 0;
    transform: translateY(-6px);
}

@media (max-width: 900px) {
    .crm-workspace,
    .overlay__body--split {
        grid-template-columns: 1fr;
    }

    .quick-send-form {
        grid-template-columns: 1fr;
    }

    .contacts-toolbar {
        grid-template-columns: 1fr;
    }

    .contacts-toolbar__left,
    .contacts-toolbar__actions {
        flex-wrap: wrap;
    }

    .mapping-row {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 640px) {
    body {
        font-size: 12px;
    }

    .crm-toolbar {
        align-items: stretch;
    }

    .crm-menu {
        width: 100%;
    }

    .crm-toolbar__actions {
        width: 100%;
    }

    .toolbar-btn {
        flex: 1 1 auto;
    }

    .message-bubble {
        max-width: 88%;
    }
}
