:root {
    --bg: #ffffff;
    --bg1: #f7f8fc;
    --bg2: #eef0f6;
    --border: #e2e5ef;
    --text: #111827;
    --muted: #6b7280;
    --accent: #6d28d9;
    --accent2: #7c3aed;
    --accent-lt:#ede9fe;
    --send-bg: linear-gradient(135deg,#6d28d9,#818cf8);
    --header-h: 64px;
    --radius: 18px;
    --shadow: 0 2px 16px rgba(109,40,217,0.08);
    }
    
    *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
    html, body { height: 100%; overflow: hidden; }
    
    body {
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg1);
    color: var(--text);
    }
    
    /* ── APP SHELL ── */
    .app {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    }
    
    /* ── HEADER ── */
    header {
    height: var(--header-h);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    gap: 12px;
    background: #ffffff;
    border-bottom: 1px solid var(--border);
    box-shadow: 0 1px 6px rgba(0,0,0,0.06);
    z-index: 20;
    }
    
    .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    user-select: none;
    text-decoration: none;
    }
    
    .brand-avatar {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--send-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    }
    
    .brand-avatar svg { display: block; }
    
    .brand-info { display: flex; flex-direction: column; gap: 1px; }
    .brand-name { font-size: 15px; font-weight: 700; color: var(--text); line-height: 1.2; }
    .brand-sub  { font-size: 11px; color: var(--muted); line-height: 1.2; }
    
    .header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    flex-shrink: 1;
    overflow: hidden;
    }
    
    .online-badge {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    color: var(--muted);
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
    padding: 4px 10px;
    border-radius: 999px;
    white-space: nowrap;
    }
    
    .online-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: #16a34a;
    box-shadow: 0 0 6px rgba(22,163,74,0.5);
    animation: pulse 2s ease-in-out infinite;
    }
    
    @keyframes pulse {
    0%,100% { opacity:1; }
    50%      { opacity:.4; }
    }
    
    .header-actions { display: flex; align-items: center; gap: 6px; }
    
    .icon-btn {
    width: 36px; height: 36px;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--bg1);
    color: var(--muted);
    cursor: pointer;
    transition: background 120ms, border-color 120ms, color 120ms, transform 120ms;
    text-decoration: none;
    flex-shrink: 0;
    }
    
    .icon-btn:hover {
    background: var(--accent-lt);
    border-color: #c4b5fd;
    color: var(--accent);
    transform: translateY(-1px);
    }
    
    .icon-btn:active { transform: scale(.94); }
    .icon-btn svg { display: block; }
    
    .header-btn {
    appearance: none;
    border: 1px solid var(--border);
    background: var(--bg1);
    color: var(--text);
    padding: 7px 14px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: background 120ms, border-color 120ms, transform 120ms;
    line-height: 1;
    white-space: nowrap;
    flex-shrink: 0;
    }
    
    .header-btn:hover {
    background: var(--accent-lt);
    border-color: #c4b5fd;
    color: var(--accent);
    }
    
    .user-pill {
    display: none;
    align-items: center;
    gap: 5px;
    padding: 5px 8px 5px 11px;
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: 12px;
    color: var(--muted);
    background: var(--bg1);
    max-width: 130px;
    min-width: 0;
    flex-shrink: 1;
    overflow: hidden;
    }
    
    .user-pill span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-width: 0;
    flex: 1;
    }
    
    .user-pill.visible { display: flex; }
    
    .user-pill button {
    background: none; border: none; cursor: pointer;
    color: #9ca3af; font-size: 13px; padding: 0; line-height: 1;
    transition: color 150ms; flex-shrink: 0;
    }
    
    .user-pill button:hover { color: var(--accent); }
    
    /* ── MAIN ── */
    main {
    flex: 1;
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg1);
    }
    
    /* ── EMAIL GATE ── */
    #email-gate {
    display: none;
    position: absolute; inset: 0; z-index: 30;
    background: rgba(247,248,252,0.85);
    backdrop-filter: blur(16px);
    align-items: center; justify-content: center;
    }
    
    #email-gate.show { display: flex; }
    
    .gate-card {
    width: min(420px, 92vw);
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 40px 32px;
    box-shadow: 0 20px 60px rgba(109,40,217,0.12), 0 4px 16px rgba(0,0,0,0.06);
    }
    
    .gate-icon {
    width: 52px; height: 52px;
    border-radius: 14px;
    margin-bottom: 20px;
    background: var(--send-bg);
    display: flex; align-items: center; justify-content: center;
    }
    
    .gate-card h2 { font-size: 21px; font-weight: 700; margin-bottom: 6px; color: var(--text); }
    .gate-card p  { font-size: 13px; color: var(--muted); margin-bottom: 28px; line-height: 1.6; }
    
    .field-label {
    display: block;
    font-size: 11px; font-weight: 600;
    text-transform: uppercase; letter-spacing: .6px;
    color: var(--muted); margin-bottom: 7px;
    }
    
    .field-input {
    width: 100%; padding: 12px 15px; margin-bottom: 15px;
    background: var(--bg1);
    border: 1px solid var(--border);
    border-radius: 12px; color: var(--text); font-size: 14px;
    outline: none; transition: border-color 160ms, box-shadow 160ms;
    font-family: inherit;
    }
    
    .field-input:focus {
    border-color: var(--accent2);
    box-shadow: 0 0 0 3px rgba(124,58,237,0.12);
    }
    
    .field-input::placeholder { color: #d1d5db; }
    
    .btn-primary {
    width: 100%; padding: 13px; border: none; border-radius: 12px;
    background: var(--send-bg); color: #fff;
    font-size: 14px; font-weight: 700; cursor: pointer;
    transition: opacity 160ms, transform 100ms; margin-top: 4px;
    font-family: inherit;
    }
    
    .btn-primary:hover   { opacity: .9; transform: translateY(-1px); }
    .btn-primary:active  { transform: scale(.98); opacity: 1; }
    .btn-primary:disabled{ opacity: .4; cursor: not-allowed; transform: none; }
    
    /* ── CHAT UI ── */
    #chat-ui { display: none; flex: 1; flex-direction: column; overflow: hidden; }
    #chat-ui.show { display: flex; }
    
    #messages {
    flex: 1; overflow-y: auto;
    padding: 20px 16px;
    display: flex; flex-direction: column; gap: 4px;
    scrollbar-width: thin;
    scrollbar-color: #d1d5db transparent;
    }
    
    #messages::-webkit-scrollbar { width: 5px; }
    #messages::-webkit-scrollbar-track { background: transparent; }
    #messages::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 99px; }
    
    /* ── DIVIDER ── */
    .divider {
    display: flex; align-items: center; gap: 10px;
    font-size: 11px; color: #9ca3af;
    margin: 12px 0 8px;
    }
    
    .divider::before, .divider::after {
    content: ""; flex: 1; height: 1px; background: var(--border);
    }
    
    /* ── MESSAGE ROWS ── */
    .msg-row {
    display: flex; flex-direction: column;
    max-width: 70%;
    }
    
    .msg-row.customer { align-self: flex-end; align-items: flex-end; }
    .msg-row.operator { align-self: flex-start; align-items: flex-start; }
    
    .msg-sender {
    font-size: 11px; color: var(--muted);
    margin-bottom: 3px; padding: 0 4px;
    display: flex; align-items: center; gap: 5px;
    }
    
    .op-avatar {
    width: 20px; height: 20px;
    border-radius: 50%;
    background: var(--send-bg);
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 10px; color: #fff; font-weight: 700;
    flex-shrink: 0;
    }
    
    .msg-bubble {
    padding: 11px 16px;
    border-radius: 20px;
    font-size: 14px; line-height: 1.5;
    word-break: break-word; max-width: 100%;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    }
    
    .msg-bubble a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
    
    .msg-row.customer .msg-bubble {
    background: var(--send-bg);
    color: #fff;
    border-bottom-right-radius: 5px;
    }
    
    .msg-row.operator .msg-bubble {
    background: #ffffff;
    color: var(--text);
    border: 1px solid var(--border);
    border-bottom-left-radius: 5px;
    }
    
    .msg-time {
    font-size: 10px; color: #9ca3af;
    margin-top: 4px; padding: 0 4px;
    }
    
    /* ── TYPING ── */
    #typing-row {
    display: none;
    align-self: flex-start; align-items: flex-end;
    gap: 8px; margin-top: 4px;
    }
    
    #typing-row.show { display: flex; }
    
    .typing-bubble {
    padding: 12px 16px;
    border-radius: 20px; border-bottom-left-radius: 5px;
    background: #ffffff; border: 1px solid var(--border);
    display: flex; align-items: center; gap: 4px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.06);
    }
    
    .typing-bubble span {
    width: 6px; height: 6px;
    border-radius: 50%; background: #9ca3af;
    animation: bounce 1.2s ease-in-out infinite;
    }
    
    .typing-bubble span:nth-child(2) { animation-delay: .15s; }
    .typing-bubble span:nth-child(3) { animation-delay: .30s; }
    
    @keyframes bounce {
    0%,80%,100% { transform: translateY(0); }
    40%          { transform: translateY(-5px); }
    }
    
    /* ── EMPTY STATE ── */
    .chat-empty {
    flex: 1; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    color: var(--muted); text-align: center;
    gap: 12px; padding: 32px;
    }
    
    .chat-empty .empty-icon {
    width: 56px; height: 56px; border-radius: 16px;
    background: var(--accent-lt);
    display: flex; align-items: center; justify-content: center;
    font-size: 26px;
    }
    
    .chat-empty strong { font-size: 15px; color: var(--text); font-weight: 600; }
    .chat-empty p { font-size: 13px; line-height: 1.6; max-width: 240px; }
    
    /* ── INPUT BAR ── */
    #input-bar {
    flex-shrink: 0;
    display: flex; align-items: flex-end;
    gap: 8px; padding: 10px 14px 12px;
    border-top: 1px solid var(--border);
    background: #ffffff;
    box-shadow: 0 -2px 12px rgba(0,0,0,0.04);
    }
    
    #msg-input {
    flex: 1; padding: 11px 16px;
    background: var(--bg1);
    border: 1px solid var(--border);
    border-radius: 22px; color: var(--text);
    font-size: 14px; outline: none;
    resize: none; max-height: 110px; line-height: 1.45;
    transition: border-color 160ms, box-shadow 160ms;
    font-family: inherit;
    }
    
    #msg-input:focus {
    border-color: var(--accent2);
    box-shadow: 0 0 0 3px rgba(124,58,237,0.1);
    background: #fff;
    }
    
    #msg-input::placeholder { color: #9ca3af; }
    
    #send-btn, #attach-btn {
    width: 42px; height: 42px; flex-shrink: 0;
    border: none; border-radius: 50%;
    background: var(--send-bg); color: #fff;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: opacity 160ms, transform 100ms;
    box-shadow: 0 2px 8px rgba(109,40,217,0.25);
    }
    
    #attach-btn {
    background: var(--bg1);
    border: 1px solid var(--border);
    color: var(--muted);
    box-shadow: none;
    }
    
    #attach-btn:hover { background: var(--accent-lt); border-color: #c4b5fd; color: var(--accent); }
    #send-btn:hover   { opacity: .88; }
    #send-btn:active, #attach-btn:active { transform: scale(.92); }
    #send-btn:disabled { opacity: .35; cursor: not-allowed; }
    
    /* ── POWERED BAR ── */
    .powered {
    text-align: center; font-size: 10px;
    color: #9ca3af; padding: 4px 0 5px; flex-shrink: 0;
    background: #fff; border-top: 1px solid var(--border);
    }
    
    /* ── UPLOAD TOAST ── */
    #upload-toast {
    display: none;
    position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
    background: #ffffff; border: 1px solid var(--border);
    border-radius: 999px; padding: 9px 18px;
    font-size: 13px; color: var(--text);
    z-index: 100; box-shadow: var(--shadow);
    gap: 8px; align-items: center; white-space: nowrap;
    }
    
    #upload-toast.show { display: flex; }
    
    .toast-spinner {
    width: 14px; height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--accent2);
    border-radius: 50%;
    animation: spin 0.7s linear infinite; flex-shrink: 0;
    }
    
    @keyframes spin { to { transform: rotate(360deg); } }
    
    /* ── ATTACHMENTS ── */
    .msg-bubble .attach-img {
    max-width: 220px; width: 100%; height: auto;
    border-radius: 12px; display: block; margin-top: 6px;
    cursor: pointer; border: 1px solid rgba(0,0,0,0.08);
    }
    
    .msg-bubble .attach-link {
    display: inline-flex; align-items: center; gap: 6px;
    color: inherit; text-decoration: none;
    font-size: 13px; padding: 6px 10px;
    background: rgba(0,0,0,0.05); border-radius: 8px;
    margin-top: 6px; border: 1px solid rgba(0,0,0,0.08);
    }
    
    .msg-row.customer .attach-link { background: rgba(255,255,255,0.2); border-color: rgba(255,255,255,0.3); }
    .msg-bubble .attach-link:hover { opacity: .8; }
    
    /* ── HIDE CRISP NATIVE WIDGET ── */
    .crisp-client,
    .crisp-client .cc-1brb6,
    .crisp-client .cc-kv6t,
    #crisp-chatbox {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
    }
    
    /* ── RESPONSIVE ── */
    @media (max-width: 640px) {
      header { padding: 0 12px; gap: 6px; }
      .online-badge { display: none; }
      .brand-sub { display: none; }
      .user-pill.visible { display: none !important; }
      .header-actions { gap: 4px; }
      .msg-row { max-width: 85%; }
      #messages { padding: 14px 10px; }
      #input-bar { padding: 8px 10px 10px; }
      .gate-card { padding: 30px 20px; border-radius: 20px; }
    }
    
    @media (max-width: 400px) {
      .brand-name { font-size: 14px; }
      .header-btn { padding: 5px 8px; font-size: 11px; }
      .icon-btn { width: 30px; height: 30px; }
      #send-btn, #attach-btn { width: 36px; height: 36px; }
      .header-actions { gap: 3px; }
    }
    
    @media (max-height: 500px) and (orientation: landscape) {
      header { height: 50px; }
      :root { --header-h: 50px; }
      #input-bar { padding: 6px 12px; }
    }
    
    @media (prefers-reduced-motion: reduce) {
      .online-dot, .typing-bubble span { animation: none; }
    }