/* ========================================================= */
/* AI Chat STV - Responsive Dark Theme (PC & Mobile) */
/* Giao diện Dark Mode chuyên nghiệp và tối ưu cho mọi thiết bị */
/* ========================================================= */

/* Biến màu sắc và kiểu dáng (Design Tokens) */
:root {
  /* Khai báo hỗ trợ Safe Area cho iOS và các thiết bị có notch */
  /* CẦN THÊM <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"> VÀO HTML */
  --bg-main: #0a0a0a;
  --bg-sidebar: #121212;
  --bg-chat-message-bot: #1e1e1e;
  --bg-chat-message-user: #4285F4;
  --bg-input: #1e1e1e;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0a0;
  --accent-color: #4285F4;
  --accent-color-hover: #3a7ae8;
  --accent-color-dark: #3367d6; 
  --border-color: #2c2c2c;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 24px;
  --header-height: 50px;
}

/* Global Reset & Base Styling */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Inter', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg-main);
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  font-size: 16px;
  /* Mở rộng nội dung ra ngoài khu vực an toàn - Fix tổng thể cho body */
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

/* Icons (Giữ nguyên) */
.icon {
  width: 20px;
  height: 20px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-sm {
  width: 16px;
  height: 16px;
}

/* General Buttons (Giữ nguyên) */
.btn {
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  transition: background-color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

/* Primary/Action Buttons (Giữ nguyên) */
.btn-primary {
  background: var(--accent-color);
  color: white;
  padding: 10px 16px;
  border-radius: var(--radius-md);
  font-weight: 500;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary:hover {
  background: var(--accent-color-hover);
  box-shadow: 0 4px 10px rgba(66, 133, 244, 0.2);
}

.btn-secondary {
  background-color: var(--bg-chat-message-bot);
  color: var(--text-primary);
}

.btn-secondary:hover {
  background-color: #252525;
}

.btn-danger {
  background-color: #dc2626;
  color: white;
}

.btn-danger:hover {
  background-color: #b91c1c;
}

/* ========================================= */
/* 1. Header & Layout */
/* ========================================= */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;

  /* FIX LỖI TRÀN LÊN TRÊN: Ưu tiên constant() trước env() */
  height: calc(var(--header-height) + constant(safe-area-inset-top));
  height: calc(var(--header-height) + env(safe-area-inset-top));

  padding-top: constant(safe-area-inset-top);
  padding-top: env(safe-area-inset-top);
  
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border-color);
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-title {
  font-size: 1.3rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 1px; 
}

/* Avatar (Giữ nguyên) */
#user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 15px;
  object-fit: cover;
  border: 2px solid var(--accent-color);
}

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg-main);
  width: 100%;
  /* Điều chỉnh padding-top dựa trên chiều cao MỚI của header */
  padding-top: calc(var(--header-height) + constant(safe-area-inset-top));
  padding-top: calc(var(--header-height) + env(safe-area-inset-top));
}

/* ========================================= */
/* 2. Sidebar (Mobile First) */
/* ========================================= */
.sidebar {
  width: 280px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease-out;
  flex-shrink: 0;
  
  /* Mobile Settings - Thêm constant() */
  position: fixed;
  top: calc(var(--header-height) + constant(safe-area-inset-top));
  top: calc(var(--header-height) + env(safe-area-inset-top));
  bottom: 0;
  left: 0;
  height: calc(100vh - var(--header-height) - constant(safe-area-inset-top));
  height: calc(100vh - var(--header-height) - env(safe-area-inset-top));
  z-index: 20;
  transform: translateX(-100%);
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.5);
}

.sidebar.active {
  transform: translateX(0);
}

.sidebar-overlay {
  position: fixed;
  /* Điều chỉnh top và height để khớp với Header mới */
  top: calc(var(--header-height) + constant(safe-area-inset-top));
  top: calc(var(--header-height) + env(safe-area-inset-top));
  left: 0;
  height: calc(100vh - var(--header-height) - constant(safe-area-inset-top));
  height: calc(100vh - var(--header-height) - env(safe-area-inset-top));
  width: 100%;
  background: rgba(0, 0, 0, 0.7);
  z-index: 19;
  display: none;
}

.sidebar-overlay.active {
  display: block;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.sidebar-divider {
    border: none;
    height: 1px;
    background-color: var(--border-color);
    margin: 12px 16px;
}

.sidebar-title {
    padding: 0 16px;
    margin-bottom: 10px;
    font-size: 1.1rem;
    font-weight: 600;
}

.sidebar-footer {
  padding: 15px;
  /* FIX LỖI LÚN XUỐNG: Thêm constant() */
  padding-bottom: calc(15px + constant(safe-area-inset-bottom)); 
  padding-bottom: calc(15px + env(safe-area-inset-bottom)); 
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.sidebar-footer .btn-logout-sidebar {
  width: 100%;
}

/* Chat History Items (Giữ nguyên) */
.chat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  margin: 0 8px 6px 8px;
  transition: background-color 0.2s;
}

.chat-item:hover {
  background: rgba(255, 255, 255, 0.05);
}

.chat-item.active {
  background: var(--accent-color);
  color: white;
  font-weight: 500;
}

.chat-item-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  padding-right: 10px;
}

.chat-item-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}

.chat-item:hover .chat-item-actions,
.sidebar.active .chat-item-actions {
    opacity: 1;
}

.chat-item.active .chat-item-actions .btn {
    color: white;
}

/* Model Selector Styling (Giữ nguyên) */
.model-select {
    width: 100%; 
    appearance: none; 
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 10px 14px;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    margin-bottom: 15px;
}

.model-select:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: -2px;
}


/* ========================================= */
/* 3. Chat Box & Messages (Giữ nguyên) */
/* ========================================= */
.chat-box {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
  max-width: 900px;
  margin: 0 auto; 
  width: 100%;
}

/* Welcome State (Giữ nguyên) */
.welcome-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  text-align: center;
}
.welcome-content {
  max-width: 500px;
  color: var(--text-secondary);
  padding-bottom: 100px; 
}

.welcome-icon {
  width: 70px;
  height: 70px;
  margin-bottom: 25px;
  stroke: var(--text-secondary);
  fill: none;
  stroke-width: 1.5;
}

.welcome-title {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 10px;
}

.welcome-subtitle {
  font-size: 1.1rem;
  color: var(--text-secondary);
}

/* Message Bubbles (Giữ nguyên) */
.message {
  max-width: 95%; 
  margin-bottom: 18px;
  padding: 12px 18px;
  border-radius: var(--radius-md);
  line-height: 1.6;
  font-size: 1rem;
}

.message.user {
  margin-left: auto;
  background: var(--bg-chat-message-user);
  color: white;
  border-top-right-radius: 4px;
}

.message.bot {
  margin-right: auto;
  background: var(--bg-chat-message-bot);
  color: var(--text-primary);
  border-top-left-radius: 4px;
}
.message.bot p { margin-bottom: 1em; font-weight: 300; }
.message.bot p:last-child { margin-bottom: 0; }
.message.bot h2, .message.bot h3 { margin-top: 1.5em; margin-bottom: 0.5em; font-weight: 600; }
.message.bot h2 { font-size: 1.35rem; }
.message.bot h3 { font-size: 1.15rem; }
.message.bot ul { list-style-type: disc; padding-left: 25px; margin-bottom: 1em; }
.message.bot li { margin-bottom: 0.5em; }

.message.bot code { 
    background-color: rgba(255, 255, 255, 0.1); 
    padding: 3px 6px; 
    border-radius: 4px; 
    font-family: monospace; 
    font-size: 0.9em; 
}

.code-block-container {
    margin: 10px 0;
    position: relative;
}

.message.bot pre {
    background-color: #000;
    padding: 15px;
    border-radius: var(--radius-md);
    overflow-x: auto;
    font-family: monospace;
    font-size: 0.9em;
    border: 1px solid var(--border-color);
}

.copy-btn {
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(255, 255, 255, 0.15);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8em;
    display: flex;
    align-items: center;
    transition: background-color 0.2s;
}

.copy-btn:hover {
    background: rgba(255, 255, 255, 0.25);
}

.chat-link {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 500;
}

.chat-link:hover {
    text-decoration: underline;
}

.message.bot.typing {
    background: var(--bg-chat-message-bot);
    display: inline-block;
    width: auto;
    min-width: 70px;
}

.typing-indicator {
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 40px;
    height: 15px;
}

.typing-dot {
    width: 7px;
    height: 7px;
    background-color: var(--text-primary);
    border-radius: 50%;
    opacity: 0.5;
    animation: typing 1s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0% { transform: translateY(0); opacity: 0.5; }
    50% { transform: translateY(-4px); opacity: 1; }
    100% { transform: translateY(0); opacity: 0.5; }
}


/* ========================================= */
/* 4. Input Area */
/* ========================================= */
.input-area {
  padding: 10px 15px; 
  border-top: 1px solid var(--border-color);
  background: var(--bg-main);
  flex-shrink: 0;
  
  /* FIX LỖI LÚN XUỐNG: Thêm constant() */
  padding-bottom: calc(10px + constant(safe-area-inset-bottom));
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
}

.input-form {
  display: flex;
  align-items: flex-end;
  background: var(--bg-input);
  border-radius: var(--radius-lg); 
  padding: 6px; 
  border: 1px solid var(--border-color);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  max-width: 900px;
  margin: 0 auto; 
}

.input-field {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  padding: 8px 16px;
  font-size: 16px;
  resize: none;
  overflow-y: hidden;
  max-height: 200px; 
  line-height: 1.5;
}
.input-field:focus {
  outline: none;
}

.send-btn {
  background: var(--accent-color);
  border: none;
  color: white;
  padding: 10px; 
  border-radius: 50%;
  cursor: pointer;
  transition: background-color 0.2s;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  margin-right: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-color-hover);
}

.send-btn:disabled {
  background: #333333;
  color: #a0a0a0;
  cursor: not-allowed;
}

.send-btn .icon {
  width: 20px;
  height: 20px;
  stroke: white;
  fill: white;
}


/* ========================================= */
/* 5. Modal & Loading (Giữ nguyên) */
/* ========================================= */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.85); 
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.3s;
}
.modal-overlay.active {
  display: flex;
  opacity: 1;
}

.modal-content {
  background: var(--bg-sidebar);
  padding: 30px;
  border-radius: var(--radius-md);
  width: 90%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
}

.modal-title {
  font-size: 1.5rem;
  margin-bottom: 12px;
  color: var(--text-primary);
}

.modal-message {
  color: var(--text-secondary);
  margin-bottom: 25px;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
}

/* ========================================================= */
/* MEDIA QUERY: Desktop/PC (min-width: 769px) */
/* ========================================================= */
@media (min-width: 769px) {
    /* Header: Ẩn nút toggle sidebar */
    .header .btn:first-child {
        display: none; 
    }
    /* Xóa các thuộc tính env()/constant() để trở về chế độ PC */
    .header {
        height: var(--header-height);
        padding-top: 0;
    }
    .chat-container {
        padding-top: var(--header-height);
        padding-bottom: 20px;
    }
    .sidebar {
        top: 0;
        height: 100%;
    }
    .sidebar-footer {
        padding-bottom: 15px; /* Trả lại padding mặc định */
    }
    
    /* Sidebar: Luôn hiển thị */
    .sidebar {
        position: relative; 
        transform: translateX(0); 
        height: 100%;
        top: 0;
        bottom: 0;
    }
    
    .sidebar-overlay {
        display: none !important;
    }
    
    /* Message Bubbles: Giới hạn độ rộng hợp lý hơn cho PC */
    .message {
        max-width: 70%; 
    }

    /* Input Area: Cố định vị trí ở dưới cùng */
    .input-area {
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        padding: 20px 0; 
        /* Xóa padding-bottom safe area cho PC */
        padding-bottom: 20px; 
    }
    
    /* Input Form: Giới hạn độ rộng, căn giữa */
    .input-form {
        max-width: 800px; 
        padding: 10px;
    }

    /* Welcome State: Tăng padding dưới khi có Input cố định */
    .welcome-content {
        padding-bottom: 200px; 
    }
}
