:root {
    /* EcoTech CRM palette — deeper green + orange, cleaned up */
    --ecotech-green: #2f7a38;
    --ecotech-green-deep: #24632c;
    --ecotech-green-soft: #e9f4eb;
    --ecotech-green-mid: #4a9a52;
    --ecotech-orange: #e8912a;
    --ecotech-orange-deep: #d4781a;
    --ecotech-orange-soft: #fdf3e6;

    --accent-subtle: var(--ecotech-green-soft);
    --bg: #f4f6f4;
    --surface: #ffffff;
    --border: #d5ddd6;
    --text: #1a1f1a;
    --text-muted: #5c6b5e;
    --user-bubble: #e9f4eb;
    --assistant-bubble: #ffffff;
    --shadow: 0 1px 3px rgba(36, 99, 44, 0.06);
    --radius: 3px;
    --font: "Segoe UI", system-ui, -apple-system, sans-serif;

    /* Aliases used elsewhere */
    --brand-navy: var(--ecotech-green-deep);
    --brand-royal: var(--ecotech-green);
    --brand-royal-light: var(--ecotech-green-mid);
    --ecotech-gold: var(--ecotech-orange);
    --ecotech-gold-soft: var(--ecotech-orange-soft);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    height: 100%;
    overflow: hidden;
}

body {
    margin: 0;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    height: 100%;
    overflow: hidden;
    position: fixed;
    inset: 0;
    width: 100%;
}

.crm-tab-shell {
    width: 100%;
    height: 100%;
    margin: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.tab-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.85rem 1.25rem;
    background: var(--surface);
    color: var(--text);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.tab-header h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.01em;
    color: var(--ecotech-green-deep);
}

.subtitle {
    margin: 0.15rem 0 0;
    font-size: 0.78rem;
    color: var(--text-muted);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.55rem;
}

.auth-user {
    font-size: 0.8rem;
    color: var(--text-muted);
    max-width: 160px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 0.25rem;
}

.region-select {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    opacity: 0.9;
}

.region-select select {
    padding: 0.4rem 0.6rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.85rem;
    min-width: 130px;
}

.region-select select option {
    color: var(--text);
}

/* CRM-style boxy solid buttons */
.btn-primary,
.btn-secondary,
.btn-accent {
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    transition: background 0.12s, border-color 0.12s;
    letter-spacing: 0.01em;
}

.btn-primary {
    background: var(--ecotech-green);
    color: #fff;
    padding: 0.6rem 1.1rem;
    align-self: flex-end;
}

.btn-primary:hover:not(:disabled) {
    background: var(--ecotech-green-deep);
}

.btn-primary:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.btn-accent {
    background: var(--ecotech-orange);
    color: #fff;
    padding: 0.45rem 0.85rem;
}

.btn-accent:hover:not(:disabled) {
    background: var(--ecotech-orange-deep);
}

.btn-secondary {
    background: var(--surface);
    color: var(--ecotech-green-deep);
    padding: 0.45rem 0.85rem;
    border: 1px solid var(--border);
}

.btn-secondary:hover {
    background: var(--ecotech-green-soft);
    border-color: #b5d0b8;
}

.chat-layout {
    flex: 1 1 0;
    display: grid;
    grid-template-columns: 240px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    gap: 0;
    min-height: 0;
    overflow: hidden;
}

.history-panel {
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem 0.75rem;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
    max-height: 100%;
    -webkit-overflow-scrolling: touch;
}

.history-panel h2 {
    margin: 0 0 0.75rem 0.5rem;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}

.session-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.session-item {
    display: flex;
    align-items: center;
    gap: 0.15rem;
    border-radius: var(--radius);
    margin-bottom: 0.15rem;
}

.session-item:hover,
.session-item.active {
    background: var(--accent-subtle);
}

.session-item.active .session-select {
    color: var(--ecotech-green-deep);
    font-weight: 600;
}

.session-select {
    flex: 1;
    min-width: 0;
    text-align: left;
    padding: 0.55rem 0.35rem 0.55rem 0.6rem;
    border: none;
    background: transparent;
    border-radius: var(--radius);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.82rem;
    color: var(--text);
    line-height: 1.3;
}

.session-delete {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    margin-right: 0.25rem;
    padding: 0;
    border: none;
    border-radius: var(--radius);
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.1rem;
    line-height: 1;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
}

.session-item:hover .session-delete,
.session-item:focus-within .session-delete {
    opacity: 1;
}

.session-delete:hover {
    color: #8a3b3b;
    background: rgba(0, 0, 0, 0.06);
}

.session-delete:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.session-list .session-date {
    display: block;
    font-size: 0.68rem;
    color: var(--text-muted);
    margin-top: 0.15rem;
}

.session-item .session-title {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-panel {
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto;
    min-height: 0;
    max-height: 100%;
    overflow: hidden;
    background: var(--bg);
    position: relative;
}

.messages-scroll {
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.messages {
    padding: 1.25rem;
    padding-bottom: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message.assistant {
    align-self: flex-start;
    background: var(--assistant-bubble);
    border: 1px solid var(--border);
    max-width: 100%;
}

.message.assistant .body {
    overflow-x: auto;
    max-width: 100%;
}

.message {
    max-width: 85%;
    padding: 0.85rem 1rem;
    border-radius: var(--radius);
    line-height: 1.5;
    font-size: 0.92rem;
    box-shadow: var(--shadow);
}

.message.user {
    align-self: flex-end;
    background: var(--user-bubble);
    border: 1px solid #b8d4bc;
}

.message .meta {
    font-size: 0.68rem;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
}

.message.assistant table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.85rem;
}

.message.assistant th,
.message.assistant td {
    border: 1px solid var(--border);
    padding: 0.4rem 0.55rem;
    text-align: left;
}

.message.assistant th {
    background: var(--user-bubble);
    font-weight: 600;
}

.message.assistant a.crm-link {
    color: var(--ecotech-green-deep);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.message.assistant a.crm-link:hover {
    color: var(--ecotech-orange-deep);
}

.chart-card {
    margin: 0.85rem 0 0.35rem;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fafcfb;
    max-width: 520px;
}

.chart-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--ecotech-green-deep);
    margin-bottom: 0.5rem;
}

.report-actions {
    margin-top: 0.75rem;
}

.report-actions .btn-accent {
    font-size: 0.85rem;
}

.message.assistant ul,
.message.assistant ol {
    margin: 0.4rem 0;
    padding-left: 1.25rem;
}

.welcome-hero {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    min-height: min(52vh, 420px);
    padding: 2rem 1.5rem 1.5rem;
    gap: 0.75rem;
}

.welcome-hero h2 {
    margin: 0;
    font-size: clamp(1.75rem, 4vw, 2.35rem);
    font-weight: 600;
    letter-spacing: -0.02em;
    color: var(--ecotech-green-deep);
    line-height: 1.2;
}

.welcome-hero p {
    margin: 0;
    max-width: 28rem;
    color: var(--text-muted);
    font-size: 1rem;
    line-height: 1.5;
}

.welcome-suggestions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
    margin-top: 1.25rem;
    max-width: 36rem;
}

.welcome-chip {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: var(--radius);
    padding: 0.5rem 0.85rem;
    font: inherit;
    font-size: 0.82rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.welcome-chip:hover {
    background: var(--ecotech-green-soft);
    border-color: var(--ecotech-green-mid);
    color: var(--ecotech-green-deep);
}

.messages:has(.welcome-hero) {
    flex: 1;
    min-height: 100%;
}

.loading {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.5rem 1.25rem 0;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.composer-footer {
    flex-shrink: 0;
    z-index: 10;
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: 0 -4px 12px rgba(36, 99, 44, 0.05);
}

.composer {
    display: flex;
    gap: 0.65rem;
    padding: 0.85rem 1.25rem 1rem;
    background: var(--surface);
}

.loading.hidden,
.hidden {
    display: none !important;
}

/* Settings modal */
.modal[hidden] {
    display: none !important;
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-items: center;
    padding: 1rem;
}

.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, 0.4);
}

.modal-panel {
    position: relative;
    width: min(860px, 100%);
    max-height: min(88vh, 720px);
    background: var(--surface);
    border-radius: 4px;
    border: 1px solid var(--border);
    box-shadow: 0 12px 40px rgba(28, 28, 28, 0.16);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
    padding: 1.15rem 1.25rem 0.75rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.modal-header h2 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--ecotech-green-deep);
}

.modal-sub {
    margin: 0.35rem 0 0;
    font-size: 0.78rem;
    color: var(--text-muted);
    max-width: 36rem;
    line-height: 1.4;
}

.modal-close {
    border: none;
    background: transparent;
    font-size: 1.5rem;
    line-height: 1;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.15rem 0.4rem;
    border-radius: var(--radius);
}

.modal-close:hover {
    background: var(--accent-subtle);
    color: var(--text);
}

.settings-tabs {
    display: flex;
    gap: 0.25rem;
    padding: 0.65rem 1.25rem 0;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.settings-tab {
    border: none;
    background: transparent;
    padding: 0.55rem 0.85rem;
    font: inherit;
    font-size: 0.85rem;
    color: var(--text-muted);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}

.settings-tab.active {
    color: var(--ecotech-green-deep);
    font-weight: 600;
    border-bottom-color: var(--ecotech-green);
}

.modal-body {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 1rem 1.25rem 1.25rem;
}

.settings-lead {
    margin: 0 0 0.85rem;
    font-size: 0.88rem;
    color: var(--text-muted);
}

.profile-edit {
    margin-bottom: 1.25rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.profile-edit h3 {
    margin: 0 0 0.55rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.profile-edit.manage-profile {
    margin-bottom: 0.85rem;
    padding-bottom: 0.85rem;
}

.name-fields {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
}

.name-fields label {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
}

.name-fields input {
    font: inherit;
    font-size: 0.9rem;
    font-weight: 400;
    color: var(--text);
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface);
}

.name-fields input:focus {
    outline: 2px solid rgba(47, 122, 56, 0.28);
    outline-offset: 1px;
    border-color: var(--ecotech-green-mid);
}

.profile-footer {
    margin-top: 0.75rem;
    padding-top: 0;
    border-top: none;
}

@media (max-width: 520px) {
    .name-fields {
        grid-template-columns: 1fr;
    }
}

.endpoint-groups {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.endpoint-group h4 {
    margin: 0 0 0.45rem;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}

.endpoint-row {
    display: flex;
    align-items: flex-start;
    gap: 0.65rem;
    padding: 0.55rem 0.65rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 0.35rem;
    background: #fafbfd;
}

.endpoint-row label {
    flex: 1;
    cursor: pointer;
    min-width: 0;
}

.endpoint-row .ep-name {
    display: block;
    font-size: 0.86rem;
    font-weight: 600;
    color: var(--text);
}

.endpoint-row .ep-desc {
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
}

.endpoint-row input[type="checkbox"] {
    margin-top: 0.2rem;
    accent-color: var(--ecotech-green);
}

.endpoint-row.readonly {
    background: var(--surface);
}

.endpoint-row .ep-badge {
    flex-shrink: 0;
    font-size: 0.68rem;
    font-weight: 600;
    padding: 0.15rem 0.45rem;
    border-radius: 4px;
    background: var(--ecotech-green-soft);
    color: var(--ecotech-green-deep);
}

.endpoint-row .ep-badge.off {
    background: #f3e8e8;
    color: #8a3b3b;
}

.manage-layout {
    display: grid;
    grid-template-columns: 200px minmax(0, 1fr);
    gap: 1rem;
    min-height: 320px;
}

.user-picker h3,
.user-endpoints-header h3 {
    margin: 0 0 0.55rem;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.user-search {
    width: 100%;
    margin-bottom: 0.55rem;
    padding: 0.45rem 0.55rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
    font-size: 0.85rem;
    background: var(--surface);
    color: var(--text);
}

.user-search:focus {
    outline: 2px solid rgba(47, 122, 56, 0.28);
    outline-offset: 1px;
    border-color: var(--ecotech-green-mid);
}

.user-list {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 420px;
    overflow-y: auto;
}

.user-list-item {
    width: 100%;
    text-align: left;
    border: 1px solid transparent;
    background: transparent;
    border-radius: var(--radius);
    padding: 0.55rem 0.65rem;
    cursor: pointer;
    font: inherit;
    margin-bottom: 0.25rem;
}

.user-list-item:hover {
    background: var(--accent-subtle);
}

.user-list-item.active {
    background: var(--accent-subtle);
    border-color: #b5d0b8;
}

.user-list-empty {
    padding: 0.65rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.user-list-item .u-meta {
    display: block;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
}

.user-endpoints {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.user-endpoints-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
}

.endpoint-bulk {
    display: flex;
    gap: 0.5rem;
}

.btn-text {
    border: none;
    background: transparent;
    color: var(--ecotech-green);
    font: inherit;
    font-size: 0.78rem;
    cursor: pointer;
    padding: 0.2rem 0.35rem;
}

.btn-text:hover {
    text-decoration: underline;
}

.settings-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
    margin-top: 1rem;
    padding-top: 0.85rem;
    border-top: 1px solid var(--border);
}

.settings-save-hint {
    margin: 0;
    font-size: 0.78rem;
    color: #9a6b00;
}

@media (max-width: 700px) {
    .manage-layout {
        grid-template-columns: 1fr;
    }
}

.auth-gate {
    flex: 1;
    display: grid;
    place-items: center;
    padding: 2rem;
    min-height: 55vh;
}

.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem 1.75rem;
    max-width: 400px;
    width: 100%;
    box-shadow: var(--shadow);
}

.auth-card h2 {
    margin: 0 0 0.35rem;
    font-size: 1.15rem;
    color: var(--ecotech-green);
}

.auth-card p {
    margin: 0 0 1rem;
    color: var(--text-muted);
    font-size: 0.88rem;
}

.auth-card label {
    display: block;
    margin-bottom: 0.85rem;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
}

.auth-card input {
    display: block;
    width: 100%;
    margin-top: 0.3rem;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
}

.auth-card .btn-primary {
    width: 100%;
    margin-top: 0.25rem;
}

.auth-error {
    color: #b42318;
    font-size: 0.82rem;
    margin: 0 0 0.5rem;
}

.auth-hint {
    margin-top: 1rem !important;
    font-size: 0.75rem !important;
}

.chat-layout.hidden {
    display: none;
}


.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--ecotech-green);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}


.composer textarea {
    flex: 1;
    resize: none;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 0.9rem;
    font-family: inherit;
    font-size: 0.92rem;
    line-height: 1.4;
}

.composer textarea:focus {
    outline: 2px solid rgba(47, 122, 56, 0.28);
    outline-offset: 1px;
    border-color: var(--ecotech-green-mid);
}

@media (max-width: 768px) {
    .chat-layout {
        grid-template-columns: 1fr;
    }

    .history-panel {
        display: none;
    }

    .tab-header {
        flex-direction: column;
        align-items: flex-start;
    }
}
