/* ─────────────────────────────────────────────────────────────────────────────
   BrokenTube Chat Widget  — complete stylesheet
   ───────────────────────────────────────────────────────────────────────────── */

/* 1. TOKENS ──────────────────────────────────────────────────────────────────*/
#toby-chat-widget {
    position: static !important;
    display: block !important;
    z-index: 2147483000 !important;
    isolation: isolate !important;
    --c-bg:          #000000;
    --c-header:      #6366F1;
    --c-user-bubble: #6366F1;
    --c-agent-bubble:#18181B;
    --c-agent-border:#27272A;
    --c-meta:        #68686D;
    --c-text:        #FFFFFF;
    --c-text-agent:  #E4E4E7;
    --c-input-bg:    #18181B;
    --c-input-field: #0a0a0a;
    --c-input-border:#27272A;
  
    --radius-widget: 16px;
    --radius-user:   16px 16px 6px 16px;
    --radius-agent:  16px 16px 16px 6px;
  
    --shadow-widget: 0 24px 80px rgba(0,0,0,0.72), 0 4px 16px rgba(0,0,0,0.4);
    --shadow-toggle: 0 10px 40px rgba(99,102,241,0.42), 0 3px 10px rgba(0,0,0,0.35);
  
    --font: Inter, "Segoe UI", system-ui, -apple-system, sans-serif;
    --font-header: Poppins, Inter, "Segoe UI", system-ui, -apple-system, sans-serif;
    --ease: cubic-bezier(.22,.68,0,1.2);
  }
  
  /* 2. BASE (scoped — do not style global html/body) ─────────────────────────*/
  
  /* 3. WIDGET ROOT ─────────────────────────────────────────────────────────────*/
  #toby-chat-widget #bt-root {
    position: fixed !important;
    right: 20px;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 170px);
    z-index: 2147483000 !important;
    display: flex !important;
    flex-direction: column-reverse; /* toggle stays bottom; chat panel stacks above without pushing toggle up */
    align-items: flex-end;
    gap: 12px;
  }
  
  /* 4. FLOATING TOGGLE BUTTON ─────────────────────────────────────────────────*/
  #toby-chat-widget #bt-toggle {
    width: 54px;
    height: 54px;
    border-radius: 999px;
    border: none;
    background: var(--c-header);
    box-shadow: var(--shadow-toggle);
    color: var(--c-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 160ms var(--ease), filter 160ms ease;
    flex-shrink: 0;
  }
  #toby-chat-widget #bt-toggle:hover { transform: translateY(-2px) scale(1.04); filter: brightness(1.06); }
  #toby-chat-widget #bt-toggle:active { transform: translateY(0)    scale(0.97); filter: brightness(0.96); }
  
  /* 5. CHAT WINDOW ─────────────────────────────────────────────────────────────*/
  #toby-chat-widget #chat-widget {
    width: 420px;
    max-width: calc(100vw - 40px);
    height: 580px;
    max-height: calc(100svh - 110px);
    background: var(--c-bg) !important;
    border-radius: var(--radius-widget);
    border: 1px solid rgba(255,255,255,0.07);
    box-shadow: var(--shadow-widget);
    display: flex !important;
    flex-direction: column;
    overflow: hidden;
  
    /* Closed state */
    opacity: 0;
    transform: scale(0.94) translateY(12px);
    transform-origin: bottom right;
    pointer-events: none;
    transition: opacity 180ms ease, transform 200ms var(--ease);
  }
  #toby-chat-widget #chat-widget.is-open {
    opacity: 1;
    transform: scale(1) translateY(0);
    pointer-events: auto;
  }
  
  /* 6. HEADER ──────────────────────────────────────────────────────────────────*/
  #toby-chat-widget #chat-header {
    background: var(--c-header) !important;
    padding: 0 18px;
    height: 62px;
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex-shrink: 0;
    /* subtle gloss line at top */
    box-shadow:
      inset 0 1px 0 rgba(255,255,255,0.14),
      inset 0 -1px 0 rgba(0,0,0,0.18);
  }
  
  #toby-chat-widget .hdr-left {
    display:flex;
    align-items:center;
    justify-content:flex-start;
    gap: 12px;
    min-width: 0;
  }
  #toby-chat-widget .hdr-right {
    display:flex;
    align-items:center;
    justify-content:flex-end;
    gap: 8px;
  }
  
  #toby-chat-widget .hdr-logo {
    width: 40px;
    height: 40px;
    border-radius: 999px;
    background: rgba(0,0,0,0.22);
    border: 2px solid rgba(255,255,255,0.28);
    box-shadow:
      0 8px 18px rgba(0,0,0,0.22),
      inset 0 0 0 1px rgba(0,0,0,0.08);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
  }
  #toby-chat-widget .hdr-logo img {
    width: 26px;
    height: 26px;
    display: block;
    object-fit: contain;
  }
  
  #toby-chat-widget .hdr-title {
    font-family: var(--font-header);
    font-size: 13.5px;
    font-weight: 500;
    color: var(--c-text);
    letter-spacing: 0.1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  #toby-chat-widget .hdr-icon-btn {
    width: 32px;
    height: 32px;
    border-radius: 999px;
    border: none;
    background: rgba(0,0,0,0.0);
    color: rgba(255,255,255,0.85);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 140ms ease, color 140ms ease;
  }
  #toby-chat-widget .hdr-icon-btn:hover { background: rgba(0,0,0,0.22); color: white; }
  #toby-chat-widget .hdr-icon-btn:active { background: rgba(0,0,0,0.34); }
  
  /* 7. MESSAGES WRAP (relative, holds chat-body + jump btn) ───────────────────*/
  #toby-chat-widget #messages-wrap {
    flex: 1;
    position: relative;
    min-height: 0;
    overflow: hidden;
  }
  
  /* 8. CHAT BODY (scrollable) ──────────────────────────────────────────────────*/
  #toby-chat-widget #chat-body {
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 14px 12px 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
  
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.18) transparent;
  }
  #toby-chat-widget #chat-body::-webkit-scrollbar { width: 6px; }
  #toby-chat-widget #chat-body::-webkit-scrollbar-track { background: transparent; }
  #toby-chat-widget #chat-body::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.16);
    border-radius: 999px;
  }
  
  /* 9. MESSAGE ROWS ────────────────────────────────────────────────────────────*/
  #toby-chat-widget .msg-row {
    display: flex;
    flex-direction: column;
    gap: 5px;
  }
  #toby-chat-widget .msg-row--user { align-items: flex-end; }
  #toby-chat-widget .msg-row--agent { align-items: flex-start; }
  
  /* 10. BUBBLES ────────────────────────────────────────────────────────────────*/
  #toby-chat-widget .user-msg, #toby-chat-widget .agent-msg {
    max-width: 92%;
    padding: 12px 16px;
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
  }
  
  /* User ─ indigo tail bottom-right */
  #toby-chat-widget .user-msg {
    background: var(--c-user-bubble);
    color: var(--c-text);
    border-radius: var(--radius-user);
  }
  
  /* Agent ─ dark gray tail bottom-left */
  #toby-chat-widget .agent-msg {
    background: var(--c-agent-bubble);
    color: var(--c-text-agent);
    border-radius: var(--radius-agent);
    border: 1px solid var(--c-agent-border);
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.04);
  }
  
  /* Rich message content (agent + user) */
  #toby-chat-widget .msg-content p {
    margin: 0 0 10px 0;
  }
  #toby-chat-widget .msg-content p:last-child {
    margin-bottom: 0;
  }
  #toby-chat-widget .msg-content ul, #toby-chat-widget .msg-content ol {
    margin: 8px 0 0 18px;
    padding: 0;
  }
  #toby-chat-widget .msg-content li {
    margin: 6px 0;
  }
  #toby-chat-widget .msg-content strong {
    font-weight: 600;
    color: #FAFAFA;
  }
  
  /* Marked.js rendering polish (requested selectors) */
  #toby-chat-widget .msg-bubble p { margin: 0 0 8px 0; }
  #toby-chat-widget .msg-bubble ul, #toby-chat-widget .msg-bubble ol { margin: 8px 0; padding-left: 20px; }
  #toby-chat-widget .msg-bubble li { margin-bottom: 4px; }
  #toby-chat-widget .msg-bubble strong { color: inherit; font-weight: 600; }
  
  /* Compact agent bubble spacing (Chatbase-like) */
  #toby-chat-widget .agent-msg p { margin: 0 0 8px 0; line-height: 1.5; font-size: 14px; }
  #toby-chat-widget .agent-msg p:last-child { margin-bottom: 0; }
  #toby-chat-widget .agent-msg ul, #toby-chat-widget .agent-msg ol { margin: 6px 0; padding-left: 20px; }
  #toby-chat-widget .agent-msg li { margin: 0 0 4px 0; padding: 0; font-size: 14px; }
  #toby-chat-widget .agent-msg li p { margin: 0; } /* CRITICAL: Prevents marked.js from adding double gaps */
  #toby-chat-widget .agent-msg a { color: #818CF8; text-decoration: underline; word-break: break-all; }
  
  /* Marked often wraps li text in <p>; keep it tight */
  #toby-chat-widget .agent-msg ul li:last-child, #toby-chat-widget .agent-msg ol li:last-child { margin-bottom: 0; }
  
  /* ── Force overrides (requested) ─────────────────────────────────────────── */
  /* Expand the chat bubble width */
  #toby-chat-widget .msg-container { max-width: 90% !important; }
  
  /* Fix gaps in agent messages */
  #toby-chat-widget .agent-msg { line-height: 1.4; font-size: 14px; }
  
  /* Reduce gap after introductory text */
  #toby-chat-widget .agent-msg p { margin: 0 0 6px 0 !important; }
  #toby-chat-widget .agent-msg p:last-child { margin-bottom: 0 !important; }
  
  /* Remove massive gap above and below the list */
  #toby-chat-widget .agent-msg ul, #toby-chat-widget .agent-msg ol { margin: 2px 0 8px 0 !important; padding-left: 20px !important; }
  
  /* Force tight list layout (prevents loose-list gaps) */
  #toby-chat-widget .agent-msg ul, #toby-chat-widget .agent-msg ol {
    display: flex !important;
    flex-direction: column !important;
    gap: 4px !important;
  }
  
  /* Reduce gap between individual bullet points */
  #toby-chat-widget .agent-msg li { margin-bottom: 4px !important; }
  
  /* CRITICAL: marked.js wraps list items in <p> tags which causes massive gaps. */
  #toby-chat-widget .agent-msg li p { margin: 0 !important; display: inline !important; }
  
  /* Links: underline + open in new tab via JS */
  #toby-chat-widget .msg-bubble a {
    color: inherit;
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
  }
  #toby-chat-widget .msg-bubble a:hover {
    color: #ffffff;
    text-decoration-thickness: 2px;
  }
  
  /* 11. MESSAGE METADATA ───────────────────────────────────────────────────────*/
  #toby-chat-widget .msg-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 4px;
    font-size: 12px;
    color: var(--c-meta);
    line-height: 1;
  }
  
  #toby-chat-widget .msg-time { letter-spacing: 0.1px; }
  
  #toby-chat-widget .msg-meta-sep {
    color: var(--c-meta);
    opacity: 0.55;
    font-size: 11px;
    user-select: none;
  }
  
  #toby-chat-widget .msg-reaction {
    border: none;
    background: transparent;
    color: var(--c-meta);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3px;
    border-radius: 5px;
    transition: color 130ms ease, background 130ms ease;
    line-height: 0;
  }
  #toby-chat-widget .msg-reaction:hover {
    color: #a1a1aa;
    background: rgba(255,255,255,0.07);
  }
  #toby-chat-widget .msg-reaction.is-active {
    color: var(--c-header);
  }
  
  /* 12. TYPING ANIMATION ───────────────────────────────────────────────────────*/
  #toby-chat-widget .typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 2px;
  }
  #toby-chat-widget .typing-dots span {
    width: 7px;
    height: 7px;
    border-radius: 999px;
    background: rgba(255,255,255,0.45);
    display: inline-block;
    animation: typing-bounce 900ms infinite ease-in-out;
  }
  #toby-chat-widget .typing-dots span:nth-child(2) { animation-delay: 130ms; }
  #toby-chat-widget .typing-dots span:nth-child(3) { animation-delay: 260ms; }
  
  @keyframes typing-bounce {
    0%, 80%, 100% { transform: translateY(0);    opacity: 0.45; }
    40%           { transform: translateY(-4px); opacity: 1;    }
  }
  
  /* 13. JUMP-TO-BOTTOM BUTTON ──────────────────────────────────────────────────*/
  #toby-chat-widget #bt-jump {
    position: absolute;
    right: 14px;
    bottom: 18px;
    width: 38px;
    height: 38px;
    border-radius: 999px;
    border: 1px solid rgba(255,255,255,0.12);
    background: rgba(10,10,10,0.82);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  
    opacity: 0;
    transform: translateY(6px);
    pointer-events: none;
    transition: opacity 140ms ease, transform 150ms ease;
  }
  #toby-chat-widget #bt-jump.is-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
  }
  #toby-chat-widget #bt-jump:hover { background: rgba(30,30,30,0.9); }
  
  /* 14. INPUT AREA ─────────────────────────────────────────────────────────────*/
  #toby-chat-widget #chat-input-area {
    flex-shrink: 0;
    background: var(--c-input-bg);
    border-top: 1px solid var(--c-input-border);
    padding: 12px 12px;
  }
  
  #toby-chat-widget #chat-form {
    display: flex;
    align-items: center;
    gap: 8px;
  }
  
  #toby-chat-widget .input-field-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    background: var(--c-input-field);
    border: 1px solid var(--c-input-border);
    border-radius: 12px;
    padding: 0 10px;
    height: 44px;
    transition: border-color 150ms ease;
  }
  #toby-chat-widget .input-field-wrap:focus-within {
    border-color: rgba(99,102,241,0.6);
  }
  
  #toby-chat-widget #chat-input {
    flex: 1;
    height: 100%;
    background: transparent;
    border: none;
    outline: none;
    color: var(--c-text);
    font-family: var(--font);
    font-size: 14px;
    min-width: 0;
  }
  #toby-chat-widget #chat-input::placeholder { color: rgba(255,255,255,0.35); }
  
  #toby-chat-widget .input-icon-btn {
    border: none;
    background: transparent;
    color: rgba(255,255,255,0.4);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px;
    border-radius: 6px;
    flex-shrink: 0;
    transition: color 130ms ease;
    line-height: 0;
  }
  #toby-chat-widget .input-icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }
  #toby-chat-widget .input-icon-btn:not(:disabled):hover { color: rgba(255,255,255,0.7); }
  
  /* Send button — indigo circle */
  #toby-chat-widget .send-btn {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    border-radius: 999px;
    border: none;
    background: var(--c-header);
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: filter 140ms ease, transform 130ms ease;
    box-shadow: 0 4px 14px rgba(99,102,241,0.35);
  }
  #toby-chat-widget .send-btn:hover { filter: brightness(1.1); transform: scale(1.04); }
  #toby-chat-widget .send-btn:active { filter: brightness(0.95); transform: scale(0.96); }
  #toby-chat-widget .send-btn:disabled {
    filter: grayscale(0.4);
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }
  
  /* 15. RESPONSIVE ─────────────────────────────────────────────────────────────*/
  @media (max-width: 440px) {
    #toby-chat-widget #chat-widget {
      width: calc(100vw - 28px);
      height: calc(100svh - 100px);
    }
    #toby-chat-widget #bt-root {
      right: 14px;
      bottom: calc(env(safe-area-inset-bottom, 0px) + 190px);
    }
  }
  
