/* ==========================================================================
   CHAT WIDGET STYLES
   ========================================================================== */

:root{
  /* dynamicznie ustawiane przez JS */
  --chat-safe-bottom: 24px;
  --chat-safe-right: 24px;
  --chat-safe-left: 24px;
  --chat-safe-top: 24px;

  /* snap corner: br | bl | tr | tl */
  --chat-corner: br;

  /* animacja */
  --chat-ease: cubic-bezier(.2,.9,.2,1);
}

.chat-launcher { 
    position: fixed; 
    width: 56px; height: 56px;
    background: var(--primary-color); color: var(--secondary-color); border-radius: 999px;
    border: none; font-size: 28px; cursor: pointer; display: none; /* hidden by default, shown by JS */
    align-items: center; justify-content: center; 
    box-shadow: 0 10px 30px rgba(0,0,0,.12);
    z-index: 1055;
    transition: transform 240ms var(--chat-ease), opacity 180ms var(--chat-ease), bottom 260ms var(--chat-ease), right 260ms var(--chat-ease), left 260ms var(--chat-ease), top 260ms var(--chat-ease);
    will-change: transform, bottom, right, left, top;
}
.chat-launcher:hover { transform: translateY(-2px) scale(1.03); }
.chat-launcher.has-new-message { animation: pulse 1.5s infinite; }

.chat-popup {
    position: fixed; 
    width: 360px; height: 520px;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px); 
    background: var(--white); border-radius: 16px;
    box-shadow: 0 14px 50px rgba(0,0,0,.18); 
    display: flex; flex-direction: column; z-index: 1056;
    overflow: hidden;
    
    /* Animation state: closed */
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px) scale(.98);
    
    transition: transform 260ms var(--chat-ease), opacity 200ms var(--chat-ease), bottom 260ms var(--chat-ease), right 260ms var(--chat-ease), left 260ms var(--chat-ease), top 260ms var(--chat-ease);
    will-change: transform, opacity, bottom, right, left, top;
}
.chat-popup.open { 
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0) scale(1);
}

/* ===== SNAP CORNERS ===== */
/* BR (bottom-right) */
body[data-chat-corner="br"] .chat-launcher,
body[data-chat-corner="br"] .chat-popup{
  right: var(--chat-safe-right);
  bottom: calc(var(--chat-safe-bottom) + env(safe-area-inset-bottom));
}

/* BL (bottom-left) */
body[data-chat-corner="bl"] .chat-launcher,
body[data-chat-corner="bl"] .chat-popup{
  left: var(--chat-safe-left);
  bottom: calc(var(--chat-safe-bottom) + env(safe-area-inset-bottom));
}

/* TR (top-right) */
body[data-chat-corner="tr"] .chat-launcher,
body[data-chat-corner="tr"] .chat-popup{
  right: var(--chat-safe-right);
  top: var(--chat-safe-top);
}

/* TL (top-left) */
body[data-chat-corner="tl"] .chat-launcher,
body[data-chat-corner="tl"] .chat-popup{
  left: var(--chat-safe-left);
  top: var(--chat-safe-top);
}

/* Bonus: jak popup jest otwarty, launcher może lekko “uciec” */
body.chat-open #chat-launcher{
  transform: translateY(0) scale(.92);
  opacity: .9;
}

.chat-header {
    background: var(--secondary-color); color: var(--white); padding: 0.75rem 1rem;
    border-radius: 10px 10px 0 0; display: flex; justify-content: space-between;
    align-items: center; font-weight: bold; flex-shrink: 0;
}
.chat-close-btn, .chat-back-btn {
    background: none; border: none; color: var(--white); font-size: 24px;
    line-height: 1; cursor: pointer; padding: 0 .5rem;
}

.chat-messages {
    flex: 1 1 auto; padding: 1rem; overflow-y: auto; display: flex;
    flex-direction: column; gap: 1rem; background-color: #f5f5f5;
}

/* [FIX] Improved Message Layout */
.message { 
    display: flex; 
    align-items: flex-end; /* Avatar at bottom */
    gap: 8px; 
    max-width: 100%; 
    margin-bottom: 10px;
}
.chat-avatar { 
    width: 32px; 
    height: 32px; 
    border-radius: 50%; 
    object-fit: cover; 
    flex-shrink: 0; 
    margin-bottom: 2px;
}
.message.message-out { flex-direction: row-reverse; }
.message.message-in { flex-direction: row; }

.message .message-content { 
    padding: 10px 14px; 
    border-radius: 18px; 
    word-wrap: break-word; 
    max-width: 85%; /* Wider bubbles */
    position: relative;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.message-system-text { font-style: italic; color: #6c757d; font-size: 0.85rem; }

.message.message-in .message-content { 
    background-color: #ffffff; 
    border-bottom-left-radius: 4px; 
    color: #1a1a1a;
}
.message.message-out .message-content { 
    background-color: #dcf8c6; /* WhatsApp-like green */
    border-bottom-right-radius: 4px; 
    color: #1a1a1a;
}

.message-content strong { font-size: 0.8rem; color: var(--secondary-color); display: block; margin-bottom: 4px; }
.message-content p { margin: 0; line-height: 1.4; }
.message-content .timestamp { font-size: 0.7rem; color: #888; text-align: right; display: block; margin-top: 4px; }
.message-system { width: 100%; text-align: center; background-color: #feefc3; font-style: italic; font-size: 0.85rem; border-radius: 4px; padding: .5rem; }
.message .chat-image { max-width: 100%; height: auto; border-radius: 8px; margin-top: 5px; cursor: pointer; }
.chat-video { max-width: 100%; height: auto; border-radius: 8px; margin-top: 5px; }

/* Confirmation Buttons */
.message-actions { margin-top: 8px; display: flex; gap: 8px; flex-wrap: wrap; }
.btn-action { padding: 4px 12px; border-radius: 15px; border: 1px solid var(--primary-color); background: transparent; color: var(--primary-color); cursor: pointer; font-size: 0.85rem; transition: all 0.2s; }
.btn-action:hover { background: var(--primary-color); color: white; }
.btn-action.primary { background: var(--primary-color); color: white; }
.btn-action.secondary { border-color: #ccc; color: #666; }
.btn-action.secondary:hover { background: #eee; color: #333; }

.chat-input-area, .chat-input-wrapper {
    padding: 1rem; border-top: 1px solid var(--border-color);
    background-color: var(--light-gray-bg); flex-shrink: 0;
    position: relative; 
}
.chat-input-area-form { display: flex; align-items: center; gap: .5rem; }
.chat-input-area-form input[type="text"], .chat-input-area-form textarea { 
    flex-grow: 1; 
    border: 1px solid #ccc; 
    padding: 0.75rem; 
    border-radius: 20px; 
    font-family: inherit;
    resize: none;
    max-height: 150px;
    overflow-y: auto;
    line-height: 1.4;
    min-height: 40px; /* Ensure minimum height */
}
.chat-input-btn { border: none; background: transparent; cursor: pointer; font-size: 1.5rem; padding: .25rem; color: #555; display: inline-flex; align-items: center; justify-content: center;}
.chat-input-btn:hover { color: var(--primary-color); }
.chat-input-btn.is-recording { color: var(--danger-color); }

/* Admin Widget Specifics */
.chat-tabs { display: flex; border-bottom: 1px solid var(--border-color); }
.chat-tab-btn { flex: 1; padding: 0.75rem; background: #f1f1f1; border: none; cursor: pointer; font-weight: bold; }
.chat-tab-btn.active { background: var(--white); border-bottom: 3px solid var(--primary-color); }
.chat-tab-content { display: none; }
.chat-tab-content.active { display: block; max-height: 350px; overflow-y: auto;}
.chat-room-item { display: flex; align-items: center; padding: 0.75rem 1rem; border-bottom: 1px solid #f0f0f0; gap: 12px; }
.chat-room-item:hover { background-color: var(--light-gray-bg); }
.chat-room-name-text { font-weight: 600; color: var(--text-color); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-room-name-text a { color: inherit; text-decoration: none; }
.chat-initial-badge { width: 32px; height: 32px; border-radius: 50%; color: white; font-weight: bold; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 1rem; }
.chat-initial-badge.type-support { background-color: var(--danger-color); }
.chat-initial-badge.type-project { background-color: var(--info-color); }
.chat-initial-badge.type-direct { background-color: #fd7e14; }
.unread-badge { background-color: var(--primary-color); color: var(--secondary-color); font-size: 0.75rem; font-weight: bold; border-radius: 50%; width: 22px; height: 22px; display: inline-flex; align-items: center; justify-content: center; margin-left: auto; }
.chat-room-main-link { display: flex; align-items: center; gap: 12px; flex-grow: 1; text-decoration: none; }
.chat-item-actions { margin-left: auto; display: flex; align-items: center; }
.chat-action-btn { background: none; border: none; cursor: pointer; padding: 0.5rem; line-height: 1; color: var(--light-text-color); border-radius: 50%; }
.chat-action-btn:hover { background-color: #e9ecef; color: var(--text-color); }
.chat-action-btn svg { width: 20px; height: 20px; }
.chat-context-menu { position: relative; }
.chat-context-menu-content { display: none; position: absolute; right: 0; top: 100%; background-color: white; min-width: 180px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 10; border-radius: 4px; overflow: hidden; }
.chat-context-menu-content.show { display: block; }
.chat-context-menu-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; font-size: 0.9rem; }
.chat-context-menu-content a:hover { background-color: var(--light-gray-bg); }

/* Users Overlay */
.chat-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: white; z-index: 20; display: flex; flex-direction: column;
}
.chat-list-scroll {
    flex: 1; overflow-y: auto;
}

/* --- STYLE DLA ODTWARZACZA AUDIO W WIADOMOŚCI --- */
.message-content audio {
    width: 100%;
    max-width: 250px;
    margin-top: 8px;
    height: 40px;
}
.message-content audio::-webkit-media-controls-panel {
    background-color: #f1f3f4;
    border-radius: 8px;
    padding: 5px;
}
.message-out .message-content audio::-webkit-media-controls-panel {
    background-color: #e0edff;
}
.message-content audio::-webkit-media-controls-volume-slider,
.message-content audio::-webkit-media-controls-mute-button {
    display: none;
}
.message-content audio::-webkit-media-controls-timeline {
    margin-left: 5px;
    margin-right: 5px;
}

.btn-read-aloud { background: none; border: none; cursor: pointer; color: #6c757d; font-size: 1.1rem; margin-left: 8px; vertical-align: middle; opacity: 0.6; transition: opacity 0.2s, color 0.2s; padding: 0; }
.btn-read-aloud:hover { opacity: 1; color: var(--primary-color); }

@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(255, 196, 0, 0.7); } 70% { box-shadow: 0 0 0 10px rgba(255, 196, 0, 0); } 100% { box-shadow: 0 0 0 0 rgba(255, 196, 0, 0); } }

/* Mobile responsiveness for chat widget */
@media (max-width: 480px) {
    .chat-popup {
        width: 100%;
        height: 100%;
        bottom: 0;
        right: 0;
        max-height: 100vh;
        border-radius: 0;
    }
    .chat-header {
        border-radius: 0;
        padding-top: 1rem; /* Extra padding for status bar area if needed */
    }
    .chat-launcher {
        bottom: 20px;
        right: 20px;
    }
}
/* Admin Widget Mobile Fixes */
@media (max-width: 480px) {
    #admin-chat-popup.chat-popup {
        top: 0 !important; left: 0 !important; right: 0 !important; bottom: 0 !important;
        width: 100% !important; height: 100% !important;
        max-width: 100% !important; max-height: 100% !important;
        margin: 0 !important; border-radius: 0 !important;
        transform: none !important;
    }
    #admin-chat-full-page-link { display: none !important; }
}

/* --- Markdown & Table Styles --- */
.markdown-content {
    color: #2d3748; /* Ciemniejszy tekst dla czytelności */
}

.markdown-content h1, .markdown-content h2, .markdown-content h3 {
    margin-top: 0.5rem;
    margin-bottom: 0.25rem;
    font-size: 1.1rem;
    font-weight: bold;
    color: #1a202c;
}

/* Stylizacja list jako "ramek" */
.markdown-content ul.chat-list {
    list-style: none;
    padding: 0;
    margin: 0.5rem 0;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 8px;
    border: 1px solid rgba(0,0,0,0.05);
    overflow: hidden;
}

.markdown-content ul.chat-list li {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid rgba(0,0,0,0.05);
    font-size: 0.95rem;
}

.chat-table-wrapper {
    overflow-x: auto;
    margin-top: 0.5rem;
    margin-bottom: 0.5rem;
    border-radius: 4px;
}

.markdown-content table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
    background: rgba(255,255,255,0.5);
}

.markdown-content td, .markdown-content th {
    border: 1px solid rgba(0,0,0,0.1);
    padding: 4px 8px;
}

/* Archived Chat Styles */
.archived-content {
    filter: grayscale(0.8);
    opacity: 0.9;
}

/* --- Typing Indicator (AI Spinner) --- */
.typing-indicator-container {
    margin-bottom: 10px;
    animation: fadeIn 0.3s ease;
}

.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background-color: #e9ecef; /* Matches message-in background */
    border-radius: 15px;
    border-bottom-left-radius: 0;
    color: #555;
    font-size: 0.85rem;
    font-style: italic;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.typing-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid rgba(0,0,0,0.1);
    border-left-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ============================
   Premium SaaS Chat UI
   ============================ */

.chat-room-page .dashboard-main-content{
  padding: 1.5rem;
}

/* Header */
.chat-page-header{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:1rem;
  padding: .9rem 1rem;
  background: rgba(255,255,255,.9);
  border: 1px solid rgba(15,23,42,.08);
  border-radius: 14px;
  box-shadow: 0 10px 25px rgba(15,23,42,.08);
  margin-bottom: 1rem;
  backdrop-filter: blur(10px);
}

.chat-title{
  display:flex;
  align-items:center;
  gap:.9rem;
  min-width: 0;
}

.chat-title-avatar{
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display:flex;
  align-items:center;
  justify-content:center;
  background: rgba(255,153,0,.12);
  border: 1px solid rgba(255,153,0,.22);
  color:#0f172a;
  flex: 0 0 auto;
}

.chat-title-text{
  min-width:0;
}

.chat-title-main{
  font-weight: 900;
  color:#0f172a;
  letter-spacing: -.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-title-main a{
  color:#0f172a;
}

.chat-title-sub{
  display:flex;
  gap:.4rem;
  margin-top:.2rem;
  flex-wrap: wrap;
}

.chat-chip{
  display:inline-flex;
  align-items:center;
  padding: .18rem .55rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 900;
  border:1px solid rgba(15,23,42,.08);
  background:#f1f5f9;
  color:#64748b;
}

.chat-chip-brand{
  background: rgba(255,153,0,.12);
  border-color: rgba(255,153,0,.22);
  color:#0f172a;
}

.chat-actions{
  display:flex;
  align-items:center;
  gap:.4rem;
}

/* Icon buttons */
.icon-btn{
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid rgba(15,23,42,.08);
  background: #fff;
  display:flex;
  align-items:center;
  justify-content:center;
  color:#0f172a;
  box-shadow: 0 1px 2px rgba(15,23,42,.06);
  transition: transform .15s ease, box-shadow .15s ease, background .15s ease;
}

.icon-btn:hover{
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(15,23,42,.10);
  background: rgba(15,23,42,.02);
}

.icon-btn-danger{
  color:#ef4444;
  border-color: rgba(239,68,68,.20);
}

/* Frame */
.chat-frame{
  height: calc(100vh - 220px);
  /* Fallback for browsers that don't support dvh */
  height: calc(100dvh - 220px); 
  min-height: 520px;
  display:flex;
  flex-direction: column;
  border-radius: 18px;
  border: 1px solid rgba(15,23,42,.08);
  background: #fff;
  box-shadow: 0 20px 35px rgba(15,23,42,.10);
  overflow:hidden;
}

@media (max-width: 768px) {
  .chat-frame {
    height: calc(100dvh - 180px); /* Adjust for smaller headers on mobile */
    min-height: auto; /* Remove min-height constraint on mobile */
  }
}

.chat-frame.is-archived{
  opacity: .92;
}

/* Messages area */
.chat-messages{
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 1.2rem 1.2rem 1rem 1.2rem;
  background:
    radial-gradient(1200px 600px at 70% -10%, rgba(255,153,0,.10), transparent 50%),
    radial-gradient(1000px 500px at 0% 0%, rgba(15,23,42,.05), transparent 55%),
    #f8fafc;
}

/* Message bubbles (you will map classes in JS) */
.msg-row{
  display:flex;
  gap:.6rem;
  margin: .45rem 0;
  align-items:flex-end;
}

.msg-row.is-me{
  justify-content:flex-end;
}

.msg-avatar{
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 2px solid #fff;
  object-fit: cover;
  box-shadow: 0 8px 16px rgba(15,23,42,.10);
}

.bubble{
  max-width: min(680px, 78%);
  border-radius: 16px;
  padding: .65rem .8rem;
  border: 1px solid rgba(15,23,42,.08);
  box-shadow: 0 12px 24px rgba(15,23,42,.08);
  background:#fff;
}

.bubble.me{
  background: rgba(34,197,94,.12);
  border-color: rgba(34,197,94,.18);
}

.bubble .meta{
  display:flex;
  gap:.6rem;
  margin-top:.35rem;
  font-size: .72rem;
  color:#64748b;
  font-weight: 800;
}

/* Composer */
.chat-composer{
  border-top: 1px solid rgba(15,23,42,.08);
  background: rgba(255,255,255,.92);
  backdrop-filter: blur(10px);
  padding: .75rem;
}

.composer-form{
  display:flex;
  align-items:flex-end;
  gap:.6rem;
}

.composer-left{
  display:flex;
  gap:.4rem;
}

.composer-center{
  flex: 1 1 auto;
}

.composer-input{
  width: 100%;
  border-radius: 14px;
  border: 1px solid rgba(15,23,42,.10);
  background:#fff;
  padding: .75rem .9rem;
  resize: none;
  max-height: 160px;
  line-height: 1.35;
  font-weight: 700;
  color:#0f172a;
  box-shadow: 0 1px 2px rgba(15,23,42,.06);
}

.composer-input:focus{
  outline: none;
  box-shadow: 0 0 0 4px rgba(255,153,0,.18);
  border-color: rgba(255,153,0,.35);
}

.send-btn{
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 0;
  background: #ff9900;
  color:#0f172a;
  display:flex;
  align-items:center;
  justify-content:center;
  box-shadow: 0 12px 20px rgba(255,153,0,.25);
  transition: transform .15s ease, box-shadow .15s ease;
}

.send-btn:hover{
  transform: translateY(-1px);
  box-shadow: 0 18px 28px rgba(255,153,0,.30);
}

.archived-banner{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 1rem;
  padding: 1rem;
  background: #f8fafc;
}

/* ============================
   Lightbox & Deleted Messages
   ============================ */

.chat-image-thumb {
  width: 140px;
  height: 140px;
  object-fit: cover;
  border-radius: 8px;
  cursor: zoom-in;
  transition: transform .2s ease;
  display: block;
}

.chat-image-thumb:hover {
  transform: scale(1.03);
}

.message-deleted {
  font-style: italic;
  opacity: 0.6;
  color: #64748b;
  padding: 0.2rem 0;
}

.chat-image-viewer {
  position: fixed; inset: 0; background: rgba(0,0,0,.85); display: none; align-items: center; justify-content: center; z-index: 2000;
}
.chat-image-viewer.open { display: flex; }
.chat-image-viewer img { max-width: 92vw; max-height: 92vh; border-radius: 12px; box-shadow: 0 20px 50px rgba(0,0,0,0.5); }