@import url("/shared/ui-styles/tokens.css");
@import url("/shared/ui-styles/fonts.css");
@import url("/shared/ui-styles/header.css");
@import url("/shared/ui-styles/buttons.css");
@import url("/shared/ui-styles/modal.css");
@import url("/shared/ui-styles/form.css");
@import url("/shared/ui-styles/borders.css");
@import url("/shared/ui-styles/utilities.css");
@import url("/shared/ui-styles/icons.css");
@import url("/shared/ui-styles/base.css");
@import url("/shared/ui-styles/gadgetui.css");
@import url("/shared/ui-styles/systemMessage.css");

body {
    background-color: var(--main-bg-color);
    color: var(--fore-color);
    margin: 0;
    font-family:
        system-ui,
        -apple-system,
        "Segoe UI",
        Roboto,
        sans-serif;
}

/* App layout. Body is a 2-row grid (header / 1fr) with overflow:hidden
 * — see packages/ui-styles/base.css — so the main content needs its own
 * scroll container or it just gets clipped when it overflows.
 *
 * Two-tier flex: root fills the 1fr grid row, .contacts-page fills the
 * root, and inside the page the toolbar stays put while the list (or
 * the detail form) takes the remaining space and scrolls on its own.
 * min-height:0 on the flex children is the load-bearing bit — without
 * it, the inner overflow-y has nothing to overflow against. */
.contact-list-root {
    align-items: center;
    padding: 24px 0 0;
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    /* Grid items default to min-height: auto, which makes them grow to
     * fit their content's intrinsic height — breaking the inner flex
     * chain's scroll containment. Force min-height: 0 so the height: 100%
     * actually constrains us to the 1fr grid track. */
    min-height: 0;
    overflow: hidden;
    max-width: 880px;
    margin: 0 auto;
}

.contacts-page {
    display: flex;
    flex-direction: column;
    gap: 16px;
    flex: 1;
    min-height: 0;
    /* Without min-width:0, flex/grid items can be pushed wider than the
     * container by intrinsic-content children (a long contact name in
     * the toolbar, a long token in the notes textarea, etc.) — that's
     * the "form width varies per contact" symptom. */
    min-width: 0;
    width: 100%;
}

.contacts-list,
.contact-form {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
    min-width: 0;
    /* Pin both views to exactly the parent column width. Combined with
     * box-sizing:border-box, this is the brute-force guarantee that
     * neither view can be wider (or narrower) than its container —
     * intrinsic content can't push, padding can't bleed out. */
    width: 100%;
    box-sizing: border-box;
    padding-bottom: 24px;
}

/* List mode: tag-cloud sidebar on the left, contacts column on the right.
 * Both share the 1fr grid track's height; each scrolls independently.
 * min-height/min-width:0 keep the inner overflow chains honest (same
 * reasoning as .contacts-page above). */
.contacts-layout {
    display: flex;
    flex-direction: row;
    gap: 16px;
    flex: 1;
    min-height: 0;
    min-width: 0;
    width: 100%;
}

.contacts-main {
    display: flex;
    flex-direction: column;
    gap: 16px;
    flex: 1;
    min-height: 0;
    min-width: 0;
}

.contacts-tag-sidebar {
    flex: 0 0 190px;
    min-height: 0;
    overflow-y: auto;
    padding-right: 8px;
    border-right: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.08));
}

/* Drawer toggle + scrim for the phone-sized groups/tags sidebar. Both are
 * inert on desktop, where the sidebar is a permanent column; the mobile
 * media query at the bottom of this file turns them on. */
/* !important because .contacts-secondary-btn (same specificity, later in
 * the file) sets display:flex — the toggle wears both classes. */
.contacts-drawer-toggle {
    display: none !important;
}

/* Active-tag chip in the toolbar — phone only (the drawer hides the
 * sidebar's own pill there). Shown in the mobile query below. */
.contacts-filter-bar {
    display: none;
}

.contacts-filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    border-radius: 12px;
    background: var(--button-primary-back-color, #3498db);
    color: var(--button-primary-fore-color, #fff);
    font-size: 13px;
    font-weight: 600;
}

.contacts-filter-chip a {
    display: inline-flex;
    align-items: center;
    color: inherit;
    cursor: pointer;
}

.contacts-filter-chip .feather {
    width: 14px;
    height: 14px;
}

.sidebar-drawer-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 999;
}

body.sidebar-drawer-open .sidebar-drawer-backdrop {
    display: block;
}

.contacts-tag-cloud-title {
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--fore-color-secondary, #888);
    margin-bottom: 10px;
}

.contacts-active-tag {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    font-weight: 600;
    font-size: 13px;
    margin-bottom: 10px;
}

.contacts-active-tag a {
    display: inline-flex;
    align-items: center;
    color: #c62828;
    cursor: pointer;
}

.contacts-active-tag .feather {
    width: 14px;
    height: 14px;
}

.contacts-tag-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.contacts-tag-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: 12px;
    background: color-mix(in srgb, var(--fore-color) 8%, transparent);
    color: var(--fore-color);
    font-size: 12px;
    cursor: pointer;
    transition: background 0.1s;
}

.contacts-tag-chip:hover {
    background: color-mix(in srgb, var(--fore-color) 16%, transparent);
}

.contacts-tag-chip.on {
    background: var(--button-primary-back-color, #3498db);
    color: var(--button-primary-fore-color, #fff);
}

.contacts-tag-chip em {
    font-style: normal;
    opacity: 0.6;
    font-size: 11px;
}

.contacts-no-tags {
    font-size: 13px;
    color: var(--fore-color-secondary, #888);
}

/* Detail form: comma-separated tag input + its hint, and the small
 * per-row chips shown on each contact in the list. */
.contact-tags-hint {
    font-weight: 400;
    color: var(--fore-color-secondary, #888);
    font-size: 0.85em;
}

.contact-row-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 4px;
}

.contact-row-tag {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 10px;
    background: color-mix(in srgb, var(--fore-color) 8%, transparent);
    color: var(--fore-color-secondary, #888);
    font-size: 11px;
}

/* Toolbar — search + new button. Stacks on narrow screens. */
.contacts-toolbar {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

/* Width lives here rather than in LookupListInput's `width` option — that
 * option writes inline styles the mobile rules can't override. The
 * .gadgetui-lookuplist-input wrapper is injected by that component around
 * the input at render time. */
.contacts-search-wrap {
    position: relative;
    width: 320px;
    max-width: 100%;
    min-width: 0;
}

.contacts-search-wrap .gadgetui-lookuplist-input {
    width: 100%;
}

.contacts-search-input {
    background: none;
    box-sizing: border-box;
    width: 100%;
}

.contacts-search-clear {
    position: absolute;
    right: 1px;
    top: 30%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: var(--fore-color-secondary, #888);
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
}

/* Base feather-icon look — copied from chatter so icons across the
 * Peopoli apps share scaling and color behavior. Relative em sizing
 * tracks the parent button's font-size; currentColor lets the icon
 * inherit the button text color (primary, danger, secondary, etc.). */
.feather {
    width: 1.2em;
    height: 1.2em;
    stroke: currentColor;
}

.feather_bigger {
    width: 1.6em;
    height: 1.6em;
}

.contacts-search-clear .feather {
    width: 16px;
    height: 16px;
}

.contacts-new-btn,
.contacts-primary-btn,
.contacts-danger-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    border: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.18));
    border-radius: 6px;
    background: var(--button-back-color, transparent);
    color: inherit;
    cursor: pointer;
    font: inherit;
    font-size: 14px;
}

.contacts-new-btn,
.contacts-primary-btn {
    background: var(--button-primary-back-color, #3498db);
    color: var(--button-primary-fore-color, #fff);
    border-color: transparent;
}

.contacts-danger-btn {
    background: transparent;
    color: #c62828;
    border-color: #c62828;
}

/* Section-add (+) button — transparent, borderless, inherits page text
 * color so it works on any theme. Same pattern as .contacts-back-btn. */
.contacts-add-btn {
    background: transparent;
    border: none;
    color: var(--fore-color);
    cursor: pointer;
    padding: 5px;
}

/* Back arrow — matches chatter's .back-btn: transparent, borderless,
 * inherits page text color. Lives at the top-left of the detail view. */
.contacts-back-btn {
    background: transparent;
    border: none;
    color: var(--fore-color);
    cursor: pointer;
    padding: 5px;
}

/* Import / export toolbar buttons — match chatter's .toolbar-icon-btn:
 * 48x48 primary-colored square with a white feather icon centered. */
.contacts-secondary-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    padding: 0;
    background: var(--button-primary-back-color);
    color: var(--button-primary-fore-color);
    border: none;
    border-radius: 8px;
    cursor: pointer;
}

.contacts-secondary-btn:hover {
    background: var(--button-primary-hover-back-color);
}

.contacts-secondary-btn .feather {
    width: 24px;
    height: 24px;
}

/* List rows */
.contacts-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
    border-top: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.08));
}

.contact-row {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 12px 12px;
    border-bottom: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.08));
    cursor: pointer;
    transition: background 0.1s;
}

.contact-row:hover {
    background: color-mix(in srgb, var(--fore-color) 6%, transparent);
}

.contact-row-name {
    font-weight: 600;
    font-size: 15px;
}

.contact-row-sub {
    font-size: 13px;
    color: var(--fore-color-secondary, #888);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.contacts-loading,
.contacts-empty {
    padding: 24px 12px;
    color: var(--fore-color-secondary, #888);
    text-align: center;
}

/* Detail / edit form */
.contact-detail-title {
    flex: 1;
    /* min-width:0 is the load-bearing bit — without it the flex item's
     * default min-width:auto lets nowrap content push the toolbar (and
     * thus the whole form column) wider than the viewport, defeating
     * the ellipsis below and creating per-contact form-width drift. */
    min-width: 0;
    font-weight: 600;
    font-size: 18px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.contact-detail-actions {
    display: flex;
    gap: 8px;
}

.contact-form {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.contact-form-row {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

/* Field + section-header labels — bare bold text, matches the look used
 * by note/social/chatter (no all-caps gimmick, no secondary color). */
.contact-form-row label,
.contact-form-section-header label {
    font-weight: 600;
}

.contact-form-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.contact-form-section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

/* form.css sets `input { width: 100% }` library-wide but does NOT cover
 * textarea. Apply border-box to all form inputs/textareas, AND give
 * textareas an explicit width:100% so a long unbroken token in the
 * value can't stretch the textarea (and thus the whole form column)
 * past the container. */
.contact-form input,
.contact-form textarea {
    box-sizing: border-box;
    max-width: 100%;
    width: 99%;
}

.contact-form textarea {
    width: 99%;
}

/* Multi-row: laid out as a mini.css .row (flex with wrap). Each child
 * is a `col-sm-12 col-md-N` cell. align-items keeps inputs vertically
 * centered with the small radio/trash buttons in the last cells. */
.contact-multi-row {
    align-items: center;
}

.contact-multi-primary-cell {
    text-align: center;
}

.contact-multi-remove-cell {
    text-align: center;
}

/* "Primary" header cell sits above the (radio + trash) col-md-3 span,
 * so center the label text across that combined area. */
.contact-multi-header {
    align-items: center;
    font-size: 0.85em;
    color: var(--fore-color-secondary, #888);
}

.contact-multi-header-primary {
    font-weight: 600;
    font-size: 0.9em;
    padding-left: 2.5em;
}

.contact-primary-toggle {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: var(--fore-color-secondary, #888);
    white-space: nowrap;
}

/* The radio is an <input>, so `.contact-form input { width: 99% }`
 * above hits it and stretches the appearance:none box across the cell
 * (border-only rectangle, no visible circle). Restore mini.css's
 * intrinsic radio size — same calc it uses internally. Specificity
 * 0,2,1 beats the `.contact-form input` rule (0,1,1). */
.contact-form input[type="radio"].contact-primary-radio {
    width: calc(1rem + var(--universal-padding) / 2);
    height: calc(1rem + var(--universal-padding) / 2);
    margin: 0;
    flex: 0 0 auto;
}

/* Desktop: the column header replaces the inline label text in each
 * row. Mobile (below) shows it again since the row collapses and the
 * column header is hidden. */
.contact-primary-text {
    display: none;
}

.contact-multi-remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
    border: 1px solid transparent;
    border-radius: 4px;
    background: transparent;
    color: var(--fore-color-secondary, #888);
    cursor: pointer;
}

.contact-multi-remove:hover {
    color: #c62828;
    border-color: #c62828;
}

/* --- Contact groups ---------------------------------------------------
 * Sidebar block (above the tag cloud) + the group editor. The editor
 * borrows .contact-form-row / .contact-form-section for layout but wears
 * .group-form so the contact autosave code can't bind to it. */
.contacts-groups-title {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.contacts-group-rows {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-bottom: 18px;
}

.contacts-group-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 13px;
    cursor: pointer;
}

.contacts-group-row:hover {
    background: color-mix(in srgb, var(--fore-color) 8%, transparent);
}

.contacts-group-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.contacts-group-row em {
    font-style: normal;
    opacity: 0.6;
    font-size: 11px;
}

.group-form {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.group-form input {
    box-sizing: border-box;
    max-width: 100%;
    width: 99%;
}

/* The suggestion box is absolutely positioned over the member list, so a
 * long list of matches doesn't shove the page around while typing. */
.group-add-wrap {
    position: relative;
}

.group-suggestions {
    display: none;
    position: absolute;
    z-index: 5;
    left: 0;
    right: 0;
    max-height: 260px;
    overflow-y: auto;
    background: var(--back-color, #fff);
    border: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.15));
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}

.group-suggestions.open {
    display: block;
}

.group-suggestion {
    display: flex;
    align-items: baseline;
    gap: 8px;
    padding: 6px 10px;
    cursor: pointer;
}

.group-suggestion:hover {
    background: color-mix(in srgb, var(--fore-color) 8%, transparent);
}

.group-suggestion-source {
    margin-left: auto;
    font-size: 11px;
    opacity: 0.6;
    white-space: nowrap;
}

.group-member-list {
    display: flex;
    flex-direction: column;
}

.group-member-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 0;
    border-bottom: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.08));
}

.group-member-detail {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.group-member-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.group-member-email {
    font-size: 12px;
    color: var(--fore-color-secondary, #888);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

@media (max-width: 600px) {
    .contact-list-root {
        margin: 0;
    }
    /* Groups + tags become an off-canvas drawer sliding in from the left
     * (the same pattern as mail's folder panel), so the contact list gets
     * the whole viewport until the user asks for the sidebar. */
    .contacts-layout {
        flex-direction: column;
    }
    .contacts-tag-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: 75%;
        max-width: 320px;
        flex: 0 0 auto;
        box-sizing: border-box;
        padding: 16px 12px;
        background: var(--secondary-back-color, #f0f0f0);
        border-right: 1px solid var(--border-color-primary, rgba(0, 0, 0, 0.08));
        box-shadow: 2px 0 16px rgba(0, 0, 0, 0.3);
        overflow-y: auto;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 1000;
    }
    body.sidebar-drawer-open .contacts-tag-sidebar {
        transform: translateX(0);
    }
    /* Hamburger is the only way into the drawer, so it's the first thing
     * in the toolbar. */
    .contacts-drawer-toggle {
        display: flex !important;
    }
    /* Let the search box absorb whatever the buttons leave, so the extra
     * hamburger doesn't push the import/export pair onto a second row. */
    .contacts-search-wrap {
        flex: 1 1 0;
        width: auto;
    }
    /* Full-basis so the chip wraps onto its own line under the buttons
     * rather than squeezing the search box. */
    .contacts-filter-bar {
        display: flex;
        flex: 1 0 100%;
    }
    /* Tap targets in the drawer get phone-sized padding — the desktop
     * sidebar's compact rows are hard to hit accurately. */
    .contacts-tag-sidebar .contacts-group-row {
        padding: 10px 4px;
    }
    .contacts-tag-sidebar .contacts-tag-chip {
        padding: 6px 12px;
        font-size: 13px;
    }
    .contact-multi-header {
        display: none;
    }
    .contact-multi-primary-cell,
    .contact-multi-remove-cell {
        text-align: left;
    }
    .contact-primary-text {
        display: inline;
    }
    /* Icon-only toolbar buttons on mobile — hide the text and tighten
     * padding so each button shrinks to its icon. `title` attributes
     * still give the screen-reader / tooltip text. */
    .contacts-new-btn .btn-label,
    .contacts-primary-btn .btn-label,
    .contacts-danger-btn .btn-label {
        display: none;
    }
    .contacts-new-btn,
    .contacts-primary-btn,
    .contacts-danger-btn {
        padding: 8px 10px;
    }
}
