/* ============================================================
   AI Assistant Chat Application - Main Stylesheet
   Version: 2.0 (Refactored & Mobile-Fixed)
   Last Updated: Phase 4.1 Complete
   ============================================================ */

/* ==================== 1. CSS Variables (Design Tokens) ==================== */
:root {
    --header-height: 60px;
    --sidebar-width: 280px;
    --sidebar-collapsed-width: 68px;
    --primary-color: #4b6bfb;
    --bg-light: #f8fafc;
    --border-color: #e2e8f0;
    --transition-speed: 0.3s;
}

/* ==================== 2. Base & Reset ==================== */
*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    font-family: 'Vazirmatn', sans-serif;
    background-color: var(--bg-light);
    margin: 0;
    padding: 0;
    /* ✅ استفاده از dvh برای موبایل + fallback برای مرورگرهای قدیمی */
    height: 100vh;
    height: 100dvh;
    width: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ==================== 3. Header ==================== */
.app-header {
    height: var(--header-height);
    background: white;
    border-bottom: 1px solid var(--border-color);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1040;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background: var(--primary-color);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ==================== 4. Layout Wrapper ==================== */
.layout-wrapper {
    display: flex;
    /* ✅ استفاده از dvh + fallback */
    height: calc(100vh - var(--header-height));
    height: calc(100dvh - var(--header-height));
    margin-top: var(--header-height);
    position: relative;
    width: 100%;
    overflow: hidden;
}

/* ==================== 5. Sidebar ==================== */
.sidebar {
    width: var(--sidebar-width);
    background: white;
    border-left: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: width var(--transition-speed) ease;
    flex-shrink: 0;
    z-index: 1030;
    height: 100%;
}

.sidebar-header {
    padding: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    height: 70px;
    border-bottom: 1px solid transparent;
}

.btn-new-chat {
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 10px;
    padding: 0.6rem;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-weight: 500;
    transition: all 0.2s;
    white-space: nowrap;
    overflow: hidden;
}

.btn-new-chat:hover {
    background: #3b5beb;
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 10px rgba(75, 107, 251, 0.2);
}

.btn-icon {
    width: 36px;
    height: 36px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #64748b;
    transition: all 0.2s;
}

.btn-icon:hover {
    background: #f1f5f9;
    color: var(--primary-color);
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 0.75rem;
}

.group-title {
    padding: 0 0.75rem;
    font-size: 0.75rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    display: block;
    opacity: 1;
    transition: opacity 0.2s;
    white-space: nowrap;
}

.chat-item {
    display: flex;
    align-items: center;
    padding: 0.65rem 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    color: #334155;
    margin-bottom: 2px;
    transition: all 0.2s;
    position: relative;
    height: 44px;
}

.chat-item:hover {
    background-color: #f1f5f9;
}

.chat-item.active {
    background-color: #eff6ff;
    color: var(--primary-color);
}

.item-icon {
    font-size: 1.1rem;
    margin-left: 0.75rem;
    flex-shrink: 0;
}

.item-text {
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex-grow: 1;
    transition: opacity 0.2s;
}

.sidebar-footer {
    border-top: 1px solid var(--border-color);
    padding: 0.75rem;
}

/* ==================== 6. Main Content Area ==================== */
.main-content {
    flex: 1;
    background: var(--bg-light);
    height: 100%;
    overflow: hidden;
    position: relative;
    width: 100%;
    padding: 0;
    display: flex;
    flex-direction: column;
}

.content-wrapper {
    padding: 2rem;
    max-width: 900px;
    margin: 0 auto;
}

.messages-area {
    flex: 1;
    overflow-y: auto;
    background-color: var(--bg-light);
}

.mw-800 {
    max-width: 800px;
}

.flip-rtl {
    transform: scaleX(-1);
}

/* ==================== 7. Sidebar Collapsed State (Desktop) ==================== */
body.sidebar-collapsed .sidebar {
    width: var(--sidebar-collapsed-width);
}

body.sidebar-collapsed .item-text,
body.sidebar-collapsed .group-title,
body.sidebar-collapsed .btn-new-chat .text {
    opacity: 0;
    width: 0;
    display: none;
}

body.sidebar-collapsed .sidebar-header {
    padding: 1rem 0.5rem;
    justify-content: center;
    flex-direction: column-reverse;
    height: auto;
    gap: 1rem;
}

body.sidebar-collapsed .btn-new-chat {
    width: 40px;
    height: 40px;
    padding: 0;
    border-radius: 50%;
}

body.sidebar-collapsed .chat-item {
    justify-content: center;
    padding: 0.65rem 0;
}

body.sidebar-collapsed .item-icon {
    margin-left: 0;
}

/* ==================== 8. Mobile Sidebar ==================== */
.sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1025;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s;
}

@media (max-width: 992px) {
    .sidebar {
        position: fixed;
        right: 0;
        top: var(--header-height);
        /* ✅ dvh + fallback */
        height: calc(100vh - var(--header-height));
        height: calc(100dvh - var(--header-height));
        transform: translateX(100%);
        box-shadow: -4px 0 15px rgba(0, 0, 0, 0.1);
    }

    body.mobile-open .sidebar {
        transform: translateX(0);
    }

    body.mobile-open .sidebar-backdrop {
        opacity: 1;
        visibility: visible;
    }

    #desktopToggle {
        display: none !important;
    }
}

/* ==================== 9. Custom Scrollbar ==================== */
.custom-scrollbar::-webkit-scrollbar {
    width: 5px;
}

.custom-scrollbar::-webkit-scrollbar-track {
    background: transparent;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 10px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* ==================== 10. SPA View Animations ==================== */
#dynamic-content > div {
    animation: fadeInView 0.3s ease-out forwards;
}

@keyframes fadeInView {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ==================== 11. Chat Header ==================== */
.chat-header {
    height: 60px;
    flex-shrink: 0;
}

/* ==================== 12. Legacy Input Styles (for compatibility) ==================== */
#msg-input {
    border-radius: 0 12px 12px 0;
    border: 2px solid #e2e8f0;
    padding: 10px 15px;
}

#msg-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(75, 107, 251, 0.1);
}

.btn-primary {
    border-radius: 12px 0 0 12px;
    padding-left: 20px;
    padding-right: 20px;
}

/* ==================== 13. Message Bubbles ==================== */
.message-bubble {
    padding: 1rem 1.25rem;
    border-radius: 12px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
    line-height: 1.6;
    font-size: 0.95rem;
}

.message-bubble.ai {
    background: white;
    border: 1px solid var(--border-color);
    border-bottom-right-radius: 2px;
}

.message-bubble.user {
    background: var(--primary-color);
    color: white;
    border-bottom-left-radius: 2px;
}

/* ==================== 14. Modern Composer ==================== */
.composer-container {
    padding: 1rem 2rem 1.5rem 2rem;
    background: linear-gradient(to top, #ffffff 80%, rgba(255, 255, 255, 0));
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    position: relative;
    z-index: 100;
    box-sizing: border-box; /* ✅ حیاتی برای جلوگیری از بیرون‌زدگی */
}

.composer-box {
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 1.5rem;
    transition: box-shadow 0.2s, border-color 0.2s;
    overflow: hidden;
}

.composer-box:focus-within {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    border-color: #cbd5e1;
}

.btn-model-selector {
    background: #f1f5f9;
    border: none;
    border-radius: 8px;
    font-size: 0.8rem;
    font-weight: 600;
    color: #475569;
    padding: 4px 12px;
    display: flex;
    align-items: center;
}

.btn-model-selector:hover,
.btn-model-selector.show {
    background: #e2e8f0;
    color: #334155;
}

/* ✅ Composer Input - یکبار تعریف شده (بدون تکرار) */
.composer-input {
    border: none;
    background: transparent;
    box-shadow: none;
    resize: none;
    padding: 0;
    min-height: 24px;
    max-height: 200px;
    font-size: 1rem;
    line-height: 1.5;
    width: 100%;
}

.composer-input:focus {
    outline: none;
}

.composer-input::-webkit-scrollbar {
    width: 6px;
    display: block;
}

.composer-input::-webkit-scrollbar-track {
    background: transparent;
}

.composer-input::-webkit-scrollbar-thumb {
    background-color: #cbd5e1;
    border-radius: 10px;
}

.composer-input::-webkit-scrollbar-thumb:hover {
    background-color: #94a3b8;
}

.btn-icon-sm {
    color: #94a3b8;
    border: none;
    background: transparent;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-icon-sm:hover {
    background: #f1f5f9;
    color: #4b6bfb;
}

.btn-send {
    width: 36px;
    height: 36px;
    background: #e2e8f0;
    color: white;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.btn-send:not(:disabled) {
    background: var(--primary-color);
    cursor: pointer;
}

.btn-send:not(:disabled):hover {
    background: #3b5beb;
    transform: scale(1.05);
}

.model-avatar {
    width: 36px;
    height: 36px;
    background: linear-gradient(135deg, #4b6bfb 0%, #8b5cf6 100%);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(75, 107, 251, 0.2);
}

.dropdown-item.active,
.dropdown-item:active {
    background-color: #f8fafc;
    color: #1e293b;
}

/* ✅ Composer Mobile Responsive */
@media (max-width: 576px) {
    .composer-container {
        padding: 0.75rem 1rem 1rem 1rem;
    }

    .composer-box {
        border-radius: 1rem;
    }
}

/* ==================== 15. Message Animations ==================== */
.message-animate {
    animation: slideIn 0.3s ease-out forwards;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-content.bg-primary {
    background-color: var(--primary-color) !important;
    color: white;
    border-bottom-left-radius: 4px !important;
}

.message-content.bg-white {
    background-color: #ffffff !important;
    border: 1px solid #e2e8f0;
    color: #1e293b;
    border-bottom-right-radius: 4px !important;
}

/* Typing Indicator */
.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background-color: #94a3b8;
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {
    0%,
    80%,
    100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1);
    }
}

/* ==================== 16. Pin Icon UX ==================== */
.pin-icon {
    cursor: pointer;
    transition: color 0.2s ease, transform 0.15s ease;
}

.unpinned-icon {
    color: #94a3b8;
}

.chat-item:hover .unpinned-icon {
    color: #64748b;
}

.pinned-icon {
    color: #facc15;
}

.pin-icon:hover {
    transform: scale(1.1);
}

.chat-item.active .pinned-icon {
    color: #facc15;
}

/* ==================== 17. Message Actions ==================== */
.message-actions {
    font-size: 0.85rem;
    opacity: 0.85;
    position: relative;
}

.message-actions button:hover {
    color: var(--primary-color);
}

.rate-btn {
    opacity: 0.75;
}

.rate-btn:hover {
    opacity: 1;
}

.copy-btn {
    opacity: 0.75;
}

.copy-btn:hover {
    opacity: 1;
}

.delete-msg-btn {
    opacity: 0.7;
}

.delete-msg-btn:hover {
    opacity: 1;
    color: var(--bs-danger);
}

.message-actions .info-tooltip {
    display: none;
    position: absolute;
    bottom: 120%;
    right: 0;
    background: #111;
    color: #fff;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 11px;
    white-space: nowrap;
    z-index: 100;
}

.message-actions:hover .info-tooltip {
    display: block;
}

/* ==================== 18. Markdown Rendering ==================== */
/* ✅ یکبار تعریف شده (بدون تکرار) */
.message-content {
    line-height: 1.8;
    word-break: break-word;
}

/* Code Blocks */
.message-content pre {
    background: #0d1117;
    border: 1px solid #30363d;
    padding: 2.6rem 1.2rem 1rem;
    border-radius: 14px;
    overflow-x: auto;
    font-size: 0.9rem;
    direction: ltr;
    margin: 0.8rem 0;
}

.message-content pre code {
    background: transparent;
    padding: 0;
}

/* Inline Code */
.message-content code:not(pre code) {
    background: #f3f4f6;
    color: #374151;
    padding: 0.15rem 0.4rem;
    border-radius: 6px;
    font-size: 0.85em;
    white-space: nowrap;
}

/* Tables */
.message-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0;
    font-size: 0.9rem;
    display: block;
    overflow-x: auto;
    white-space: nowrap;
}

.message-content th,
.message-content td {
    border: 1px solid #e5e7eb;
    padding: 0.6rem 0.75rem;
    text-align: center;
}

.message-content th {
    background: #f9fafb;
    font-weight: 600;
}

/* Lists */
.message-content ul,
.message-content ol {
    padding-right: 1.4rem;
    margin: 0.5rem 0;
}

/* Blockquote */
.message-content blockquote {
    border-right: 4px solid #c7d2fe;
    padding-right: 1rem;
    margin: 0.8rem 0;
    color: #334155;
    background: #eef2ff;
    border-radius: 8px;
}

/* ==================== 19. Code Block Actions ==================== */
.code-block-wrapper {
    position: relative;
}

.code-copy-btn {
    position: absolute;
    top: 10px;
    left: 10px;
    background: #161b22;
    color: #e6edf3;
    border: 1px solid #30363d;
    border-radius: 8px;
    font-size: 12px;
    padding: 4px 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
    z-index: 5;
    opacity: 0.9;
}

.code-copy-btn:hover {
    background: #1f2937;
}

.code-copy-btn.copied {
    background: #22c55e;
    color: #052e16;
    border-color: #22c55e;
}

/* ==================== 20. Model Dropdown Fix (Phase 4.1) ==================== */
/*
  NOTE:
  Dropdown uses position: fixed intentionally
  to escape composer stacking context.
  Safe for MVP. Can be replaced with Popper
  portal in UX polish phase.
*/

.composer-toolbar .dropdown-menu {
    position: fixed !important;
    z-index: 9999 !important;
    max-height: 320px;
    overflow-y: auto;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.15);
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    min-width: 220px;
    padding: 0.5rem 0;
    background: white;
}

.composer-toolbar .dropdown-menu::-webkit-scrollbar {
    width: 6px;
}

.composer-toolbar .dropdown-menu::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 10px;
}

.composer-toolbar .dropdown-menu::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

.composer-toolbar .dropdown-item {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    transition: background 0.15s;
    border-radius: 6px;
    margin: 0 0.25rem;
}

.composer-toolbar .dropdown-item:hover {
    background: #f1f5f9;
}

.composer-toolbar .dropdown-item.active {
    background: #eff6ff;
    color: var(--primary-color);
}

.composer-toolbar .dropdown-header {
    padding: 0.5rem 1rem;
    font-size: 0.75rem;
    color: #64748b;
    font-weight: 600;
}



/* ====================
   Conversation Rules Button State
==================== */

.rules-btn.active {
    background-color: rgba(75, 107, 251, 0.12);
    border-color: rgba(75, 107, 251, 0.6);
    color: var(--primary-color);
}

.rules-btn.active i {
    color: var(--primary-color);
}

/* Phase 5.6.A — Content Rendering */

.ea-search-card {
  background: #fff;
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 1.25rem 1.5rem;
}

.ea-card-header {
  margin-bottom: 1rem;
}

.ea-card-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: #0f172a;
  margin: 0 0 0.25rem 0;
}

.ea-card-meta {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 0.8rem;
  color: #64748b;
}

.ea-btn {
  border-radius: 10px;
  padding: 0.35rem 0.6rem;
  font-size: 0.75rem;
}

.ea-btn-link {
  background: transparent;
  color: #2563eb;
  border: 1px solid transparent;
}

.ea-btn-secondary {
  background: #f1f5f9;
  color: #0f172a;
  border: 1px solid #e2e8f0;
}

.ea-btn-ghost {
  background: transparent;
  color: #94a3b8;
  border: 1px dashed #e2e8f0;
}

.ea-card-content {
  font-size: 0.92rem;
  line-height: 1.85;
  color: #1f2937;
}

.ea-card-content p {
  margin: 0 0 0.9rem 0;
}

.ea-card-content strong {
  font-weight: 600;
  color: #020617;
}

.ea-card-actions {
  margin-top: 1rem;
  display: flex;
  gap: 0.75rem;
}


.lesson-notebook {
  height: 100%;
  overflow: hidden;
}

/* Lesson */
.lesson-panel {
  width: 42%;
  min-width: 360px;
  max-width: 520px;
  background: #fff;
}

.lesson-panel-inner {
  height: 100%;
  overflow-y: auto;
}

/* Chat */
.chat-panel {
  background: #f9fafb;
}

/* Mobile */
@media (max-width: 992px) {
  .lesson-notebook {
    flex-direction: column;
  }

  .lesson-panel {
    width: 100%;
    max-width: none;
    min-width: unset;
    height: 50%;
    border-end: none;
    border-bottom: 1px solid #e5e7eb;
  }
}

.lesson-header {
  flex-shrink: 0;
}

.lesson-actions {
  flex-shrink: 0;
}

.lesson-scroll-area {
  flex: 1;
}

.qg-card {
  border-radius: 16px;
  border: 1px solid #eee;
  background: #fff;
}

.qg-badge {
  font-size: 0.75rem;
  border-radius: 12px;
  padding: 4px 10px;
}

.qg-answer {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 12px;
  margin-top: 12px;
  display: none;
}

.qg-answer.visible {
  display: block;
}

.qg-toggle {
  cursor: pointer;
  font-size: 0.85rem;
  color: #0d6efd;
}

/* ================================
   Prompt Optimization Toggle
   Brand Version
   ================================ */

.optimize-toggle-btn {
  transition:
    background-color 0.2s ease,
    border-color 0.2s ease,
    box-shadow 0.2s ease,
    transform 0.15s ease;
}

/* Logo */
.optimize-toggle-btn .brand-logo {
  width: 18px;
  height: 18px;
  background-image: url("/static/images/team-logo.c4a460b84ccb.png");
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  display: inline-block;
  filter: grayscale(100%) opacity(0.6);
  transition:
    filter 0.2s ease,
    transform 0.2s ease;
}
.optimize-toggle-btn:hover {
  background-color: rgba(13, 110, 253, 0.08); /* subtle brand tint */
  border-color: rgba(13, 110, 253, 0.4);
}

.optimize-toggle-btn:hover .brand-logo {
  filter: grayscale(0%) opacity(0.9);
  transform: scale(1.05);
}
.optimize-toggle-btn.active {
  background-color: #0d6efd; /* brand primary */
  border-color: #0d6efd;
  color: #fff;
  box-shadow: 0 4px 14px rgba(13, 110, 253, 0.35);
}

.optimize-toggle-btn.active .brand-logo {
  filter: none;
  transform: scale(1.1);
}
@keyframes optimize-pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(13, 110, 253, 0.45);
  }
  70% {
    box-shadow: 0 0 0 6px rgba(13, 110, 253, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(13, 110, 253, 0);
  }
}

.optimize-toggle-btn.active {
  animation: optimize-pulse 0.6s ease-out;
}
.send-btn-optimized {
  background-color: #0d6efd !important;
  border-color: #0d6efd !important;
  box-shadow: 0 4px 12px rgba(13, 110, 253, 0.4);
}

.send-btn-optimized i {
  transform: translateY(-1px);
}
.optimize-toggle-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.35);
}
/* ==================== END OF STYLESHEET ==================== */