/**
 * Take Down Braids — per-order chat UI
 */
:root {
    --tdb-chat-gold: #7b5301;
    --tdb-chat-navy: #0a132e;
    --tdb-chat-cream: #fdfbf7;
    --tdb-chat-border: rgba(123, 83, 1, 0.15);
}

html.tdb-chat-root,
html.tdb-chat-root body.tdb-chat-body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
}

.tdb-chat-page {
    --tdb-chat-gold: #7b5301;
    --tdb-chat-navy: #0a132e;
    --tdb-chat-cream: #fdfbf7;
    --tdb-chat-border: rgba(123, 83, 1, 0.15);
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    background: linear-gradient(160deg, #fdfbf7 0%, #f3ece0 100%);
    display: flex;
    flex-direction: column;
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    color: #333;
    box-sizing: border-box;
}

.tdb-chat-shell {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    max-width: 720px;
    width: 100%;
    margin: 0 auto;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 0 40px rgba(10, 19, 46, 0.08);
}

@media (min-width: 768px) {
    .tdb-chat-page {
        padding: 1.5rem;
    }
    .tdb-chat-shell {
        border-radius: 20px;
    }
}

.tdb-chat-header {
    flex-shrink: 0;
    padding: 1rem 1.1rem;
    background: linear-gradient(135deg, var(--tdb-chat-navy) 0%, #1a2d52 100%);
    color: #fff;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    border-bottom: 3px solid var(--tdb-chat-gold);
}

.tdb-chat-header-back {
    color: rgba(255, 255, 255, 0.85);
    text-decoration: none;
    font-size: 1.25rem;
    line-height: 1;
    padding: 0.25rem;
}

.tdb-chat-header-back:hover {
    color: #fff;
}

.tdb-chat-header-main {
    flex: 1;
    min-width: 0;
}

.tdb-chat-header h1 {
    font-size: 1.05rem;
    font-weight: 700;
    margin: 0 0 0.15rem;
    color: #fff;
}

.tdb-chat-header-avatar {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    overflow: hidden;
    border: 2px solid rgba(255, 255, 255, 0.35);
    background: rgba(255, 255, 255, 0.12);
    display: flex;
    align-items: center;
    justify-content: center;
}

.tdb-chat-header-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.tdb-chat-header-initials {
    font-size: 0.85rem;
    font-weight: 700;
    color: #fff;
    letter-spacing: 0.04em;
}

.tdb-chat-header .tdb-chat-order {
    font-size: 0.75rem;
    opacity: 0.8;
    font-family: ui-monospace, monospace;
}

.tdb-chat-privacy {
    flex-shrink: 0;
    padding: 0.55rem 1rem;
    background: #fff9ee;
    border-bottom: 1px solid var(--tdb-chat-border);
    font-size: 0.75rem;
    color: #6b5a3e;
    display: flex;
    align-items: flex-start;
    gap: 0.4rem;
}

.tdb-chat-privacy i {
    margin-top: 0.1rem;
    color: var(--tdb-chat-gold);
}

.tdb-chat-panel {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
}

.tdb-chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    background: var(--tdb-chat-cream);
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

.tdb-chat-empty {
    text-align: center;
    color: #8a8a8a;
    font-size: 0.9rem;
    margin: auto;
    padding: 2rem 1rem;
}

.tdb-chat-bubble-wrap {
    display: flex;
    flex-direction: column;
    max-width: 85%;
}

.tdb-chat-bubble-wrap.is-mine {
    align-self: flex-end;
    align-items: flex-end;
}

.tdb-chat-bubble-wrap.is-theirs {
    align-self: flex-start;
    align-items: flex-start;
}

.tdb-chat-sender {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: #8a8a8a;
    margin-bottom: 0.2rem;
    padding: 0 0.35rem;
}

.tdb-chat-bubble {
    padding: 0.65rem 0.9rem;
    border-radius: 16px;
    font-size: 0.9375rem;
    line-height: 1.45;
    word-break: break-word;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

.tdb-chat-bubble-wrap.is-mine .tdb-chat-bubble {
    background: linear-gradient(135deg, var(--tdb-chat-gold) 0%, #694600 100%);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.tdb-chat-bubble-wrap.is-theirs .tdb-chat-bubble {
    background: #fff;
    color: var(--tdb-chat-navy);
    border: 1px solid var(--tdb-chat-border);
    border-bottom-left-radius: 4px;
}

/* Admin view — customer messages (left, blue) */
.tdb-chat-bubble-wrap.is-client-msg .tdb-chat-sender-badge {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: none;
    letter-spacing: 0;
    color: #1e40af;
    background: #dbeafe;
    border: 1px solid #93c5fd;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
}

.tdb-chat-bubble-wrap.is-client-msg .tdb-chat-bubble {
    background: #eff6ff;
    color: #1e3a5f;
    border: 1.5px solid #93c5fd;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 4px rgba(37, 99, 235, 0.08);
}

/* Admin view — unbraider messages (right, gold sender) */
.tdb-chat-bubble-wrap.is-unbraider-msg .tdb-chat-sender-badge {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: none;
    letter-spacing: 0;
    color: #fff;
    background: linear-gradient(135deg, var(--tdb-chat-gold) 0%, #694600 100%);
    border: 1px solid #694600;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
}

.tdb-chat-bubble-wrap.is-unbraider-msg.is-mine .tdb-chat-bubble {
    background: linear-gradient(135deg, var(--tdb-chat-gold) 0%, #694600 100%);
    color: #fff;
    border: none;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 6px rgba(123, 83, 1, 0.2);
}

.tdb-chat-role-legend {
    flex-shrink: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.75rem 1.25rem;
    padding: 0.55rem 1rem;
    background: #f3f0ea;
    border-bottom: 1px solid var(--tdb-chat-border);
    font-size: 0.78rem;
    color: #5c5346;
}

.tdb-chat-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    font-weight: 600;
}

.tdb-chat-legend-swatch {
    width: 14px;
    height: 14px;
    border-radius: 4px;
    flex-shrink: 0;
}

.tdb-chat-legend-swatch.is-client {
    background: #93c5fd;
    border: 1px solid #60a5fa;
}

.tdb-chat-legend-swatch.is-unbraider {
    background: #7b5301;
    border: 1px solid #694600;
}

.tdb-chat-time {
    font-size: 0.65rem;
    color: #aaa;
}

.tdb-chat-meta {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.2rem;
    padding: 0 0.35rem;
}

.tdb-chat-bubble-wrap.is-mine .tdb-chat-meta {
    justify-content: flex-end;
}

.tdb-chat-ticks {
    display: inline-flex;
    align-items: center;
    gap: 0;
    font-size: 0.72rem;
    line-height: 1;
    font-weight: 700;
}

.tdb-chat-ticks span {
    display: inline-block;
    transform: scale(0.92);
}

.tdb-chat-ticks span + span {
    margin-left: -0.28em;
}

.tdb-chat-ticks.is-sent {
    color: #b8b8b8;
}

.tdb-chat-ticks.is-delivered {
    color: #9ca3af;
}

.tdb-chat-ticks.is-read {
    color: #7b5301;
}

.tdb-chat-send.is-sending {
    opacity: 0.85;
}

.tdb-chat-compose {
    flex-shrink: 0;
    padding: 0.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom));
    background: #fff;
    border-top: 1px solid var(--tdb-chat-border);
}

.tdb-chat-compose.tdb-chat-closed {
    opacity: 0.7;
}

.tdb-chat-error {
    display: none;
    font-size: 0.8rem;
    color: #b91c1c;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 8px;
    padding: 0.45rem 0.65rem;
    margin-bottom: 0.5rem;
}

.tdb-chat-error.is-visible {
    display: block;
}

.tdb-chat-input-row {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

.tdb-chat-input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 14px;
    padding: 0.65rem 0.9rem;
    font-size: 1rem;
    resize: none;
    min-height: 44px;
    max-height: 120px;
    line-height: 1.4;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.tdb-chat-input:focus {
    border-color: rgba(123, 83, 1, 0.45);
    box-shadow: 0 0 0 3px rgba(123, 83, 1, 0.12);
}

.tdb-chat-input.is-blocked {
    border-color: #f87171;
    background: #fff5f5;
}

.tdb-chat-send {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--tdb-chat-gold);
    color: #fff;
    font-size: 1.15rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.15s, transform 0.1s;
}

.tdb-chat-send:hover:not(:disabled) {
    transform: scale(1.05);
}

.tdb-chat-send.is-ready {
    opacity: 1;
}

.tdb-chat-send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.tdb-chat-btn-link {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.5rem 1rem;
    border-radius: 999px;
    background: var(--tdb-chat-gold, #7b5301);
    color: #fff !important;
    text-decoration: none !important;
    font-weight: 600;
    font-size: 0.875rem;
    border: none;
    transition: background 0.15s;
}

.tdb-chat-btn-link:hover {
    background: #694600;
    color: #fff;
}

.tdb-chat-btn-outline {
    background: transparent;
    color: var(--tdb-chat-gold) !important;
    border: 1px solid rgba(123, 83, 1, 0.35);
}

.tdb-chat-btn-outline:hover {
    background: #fff9ee;
}

.tdb-chat-actions-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    margin-top: 1rem;
}

.tdb-chat-preview {
    max-height: 200px;
    overflow-y: auto;
    background: #f8f6f2;
    border-radius: 10px;
    padding: 0.75rem;
    font-size: 0.85rem;
}

.tdb-chat-preview .tdb-chat-bubble-wrap {
    max-width: 100%;
}

.tdb-chat-preview .tdb-chat-bubble {
    font-size: 0.8rem;
    padding: 0.45rem 0.65rem;
}

.tdb-chat-preview-line {
    padding: 0.35rem 0;
    border-bottom: 1px solid rgba(123, 83, 1, 0.08);
    line-height: 1.4;
}

.tdb-chat-preview-line:last-child {
    border-bottom: none;
}

.tdb-chat-icon-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.35rem 0.75rem;
    font-size: 0.8125rem;
    border-radius: 999px;
    border: 1px solid rgba(123, 83, 1, 0.3);
    background: #fff9ee;
    color: #7b5301;
    text-decoration: none;
    font-weight: 600;
}

.tdb-chat-icon-btn:hover {
    background: #7b5301;
    color: #fff;
}

/* Unbraider chat hub — sidebar + main */
.tdb-chat-page-provider {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    padding: 0;
    box-sizing: border-box;
}

.tdb-chat-layout {
    display: flex;
    width: 100%;
    max-width: 1100px;
    margin: 0 auto;
    height: 100%;
    max-height: 100%;
    min-height: 0;
    overflow: hidden;
    background: #fff;
    box-shadow: 0 0 40px rgba(10, 19, 46, 0.08);
}

@media (min-width: 901px) {
    .tdb-chat-page-provider {
        padding: 1rem;
    }
    .tdb-chat-layout {
        border-radius: 20px;
    }
}

.tdb-chat-sidebar {
    flex-shrink: 0;
    width: 280px;
    background: #f8f6f2;
    border-right: 1px solid var(--tdb-chat-border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.tdb-chat-sidebar-head {
    display: flex;
    align-items: flex-start;
    gap: 0.65rem;
    padding: 1rem;
    border-bottom: 1px solid var(--tdb-chat-border);
    background: linear-gradient(135deg, var(--tdb-chat-navy) 0%, #1a2d52 100%);
    color: #fff;
}

.tdb-chat-sidebar-head h2 {
    font-size: 1rem;
    margin: 0 0 0.15rem;
    color: #fff;
}

.tdb-chat-sidebar-head p {
    margin: 0;
    font-size: 0.72rem;
    opacity: 0.75;
}

.tdb-chat-sidebar-back {
    color: rgba(255, 255, 255, 0.85);
    text-decoration: none;
    font-size: 1.2rem;
    line-height: 1;
    padding-top: 0.1rem;
}

.tdb-chat-sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
}

.tdb-chat-sidebar-empty {
    padding: 1.25rem 0.75rem;
    font-size: 0.85rem;
    color: #8a8a8a;
    text-align: center;
}

.tdb-chat-sidebar-item {
    display: block;
    padding: 0.75rem 0.85rem;
    border-radius: 12px;
    text-decoration: none;
    color: var(--tdb-chat-navy);
    margin-bottom: 0.35rem;
    border: 1px solid transparent;
    transition: background 0.15s, border-color 0.15s;
}

.tdb-chat-sidebar-item:hover {
    background: #fff;
    border-color: var(--tdb-chat-border);
}

.tdb-chat-sidebar-item.is-active {
    background: #fff;
    border-color: rgba(123, 83, 1, 0.35);
    box-shadow: 0 2px 8px rgba(123, 83, 1, 0.08);
}

.tdb-chat-sidebar-order {
    display: block;
    font-family: ui-monospace, monospace;
    font-size: 0.8125rem;
    font-weight: 700;
    color: var(--tdb-chat-gold);
    margin-bottom: 0.2rem;
}

.tdb-chat-sidebar-preview {
    display: block;
    font-size: 0.78rem;
    color: #666;
    line-height: 1.35;
}

.tdb-chat-sidebar-preview.is-muted {
    color: #aaa;
    font-style: italic;
}

.tdb-chat-main {
    flex: 1;
    min-width: 0;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.tdb-chat-page-embed {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
}

.tdb-chat-page-embed .tdb-chat-shell {
    flex: 1 1 auto;
    min-height: 0;
    height: 100%;
    max-height: 100%;
    max-width: none;
    margin: 0;
    box-shadow: none;
    border-radius: 0;
}

.tdb-chat-shell-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-height: 100%;
}

.tdb-chat-empty-state {
    text-align: center;
    padding: 2rem 1.5rem;
    max-width: 320px;
}

.tdb-chat-empty-icon {
    font-size: 2.5rem;
    margin-bottom: 0.75rem;
}

.tdb-chat-empty-state h1 {
    font-size: 1.15rem;
    margin: 0 0 0.5rem;
    color: var(--tdb-chat-navy);
}

.tdb-chat-empty-state p {
    color: #8a8a8a;
    font-size: 0.9rem;
    margin: 0 0 1rem;
}

.tdb-chat-sidebar-toggle {
    display: none;
    position: fixed;
    bottom: 1rem;
    left: 1rem;
    z-index: 30;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: var(--tdb-chat-gold);
    color: #fff;
    font-size: 1.25rem;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    cursor: pointer;
}

@media (max-width: 900px) {
    .tdb-chat-layout {
        position: relative;
    }
    .tdb-chat-sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 25;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 4px 0 24px rgba(0, 0, 0, 0.12);
    }
    .tdb-chat-sidebar.is-open {
        transform: translateX(0);
    }
    .tdb-chat-sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }
}
