:root{
  --bg0:#f4f7fb;
  --bg1:#eef3f9;
  --card:#ffffff;
  --text:#0f172a;
  --muted:#64748b;
  --line:#e5eaf2;

  --brand:#0b3a66;
  --brand2:#123f73;

  --primary:#0b3a66;
  --primaryHover:#082f52;
  
  --secondary-bg: rgba(255,255,255,0.12);

  --shadow: 0 10px 30px rgba(2, 12, 27, 0.08);
  --shadow2: 0 6px 16px rgba(2, 12, 27, 0.10);

  --radius:16px;
  --radius2:12px;
  --radius-sm: 8px;

  --max: 1200px; /* Increased for sidebar */
  --sidebar-width: 320px;
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", Arial, sans-serif;
  color:var(--text);
  background:
    radial-gradient(1200px 700px at 20% 0%, rgba(11,58,102,0.10), transparent 55%),
    radial-gradient(900px 600px at 90% 10%, rgba(18,63,115,0.10), transparent 55%),
    linear-gradient(180deg, var(--bg0), var(--bg1));
}

/* Layout */
.app-layout {
    display: flex;
    width: min(var(--max), calc(100% - 32px));
    height: calc(100vh - 36px);
    margin: 18px auto;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
    position: relative;
}

/* FAQ Panel */
.faq-panel {
    width: var(--sidebar-width);
    background: #f8fafc;
    border-right: 1px solid var(--line);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.3s ease;
    position: relative;
    flex-shrink: 0;
}

.faq-panel.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
}

.faq-header {
    padding: 16px;
    border-bottom: 1px solid var(--line);
}

.faq-header h2 {
    margin: 0 0 12px 0;
    font-size: 16px;
    color: var(--brand);
}

.faq-search input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    font-size: 13px;
    outline: none;
}
.faq-search input:focus {
    border-color: var(--brand);
}

.faq-tabs {
    display: flex;
    flex-wrap: wrap;
    padding: 8px 16px;
    gap: 6px;
    border-bottom: 1px solid var(--line);
}

.faq-tab {
    background: transparent;
    border: 1px solid transparent;
    padding: 4px 10px;
    font-size: 12px;
    border-radius: 20px;
    cursor: pointer;
    color: var(--muted);
    transition: all 0.2s;
}
.faq-tab:hover {
    background: #e2e8f0;
}
.faq-tab.active {
    background: var(--brand);
    color: #fff;
}

.faq-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.faq-item {
    background: #fff;
    border: 1px solid var(--line);
    border-radius: var(--radius2);
    margin-bottom: 10px;
    padding: 12px;
    cursor: pointer;
    transition: all 0.2s;
}
.faq-item:hover {
    border-color: #cbd5e1;
    box-shadow: 0 2px 8px rgba(0,0,0,0.03);
}

.faq-question {
    font-weight: 500;
    font-size: 14px;
    color: var(--text);
    margin-bottom: 4px;
}

.faq-answer-preview {
    font-size: 12px;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.faq-details {
    display: none;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--line);
    font-size: 13px;
    color: #475569;
    line-height: 1.5;
}

.faq-item.expanded .faq-details {
    display: block;
}
.faq-item.expanded .faq-answer-preview {
    display: none;
}

.faq-actions {
    margin-top: 10px;
    display: flex;
    gap: 8px;
}

.faq-btn {
    background: #f1f5f9;
    border: none;
    padding: 4px 8px;
    font-size: 11px;
    border-radius: 4px;
    cursor: pointer;
    color: var(--primary);
}
.faq-btn:hover {
    background: #e2e8f0;
}

.faq-toggle-btn {
    position: absolute;
    right: -24px; /* Outside sidebar */
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 48px;
    background: #f8fafc;
    border: 1px solid var(--line);
    border-left: none;
    border-radius: 0 8px 8px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--muted);
    z-index: 10;
}
.faq-toggle-btn:hover {
    background: #f1f5f9;
}
.faq-panel.collapsed .arrow {
    transform: rotate(180deg);
}


/* Chat Container */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0; /* Prevent flex overflow */
}

/* Header */
.chat-header {
    height: 64px;
    padding: 0 24px;
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #fff;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}
.brand-mark {
    width: 32px;
    height: 32px;
    background: var(--brand);
    border-radius: 8px;
    position: relative;
}
.brand-mark::after {
    content: '';
    position: absolute;
    top: 8px; left: 8px; right: 8px; bottom: 8px;
    border: 2px solid rgba(255,255,255,0.8);
    border-radius: 4px;
}

.brand-text { display: flex; flex-direction: column; }
.brand-title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: var(--brand);
}
.brand-subtitle {
    font-size: 11px;
    color: var(--muted);
    letter-spacing: 0.5px;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 16px;
}

.control {
    display: flex;
    align-items: center;
    gap: 8px;
}
.control-label {
    font-size: 12px;
    color: var(--muted);
}
select {
    padding: 6px 10px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 13px;
    color: var(--text);
    outline: none;
    background: #fff;
}
select:focus { border-color: var(--brand); }

.btn {
    padding: 8px 16px;
    border-radius: 6px;
    border: none;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}
.btn-primary {
    background: var(--primary);
    color: #fff;
}
.btn-primary:hover { background: var(--primaryHover); }
.btn-primary:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-secondary {
    background: #f1f5f9;
    color: var(--text);
}
.btn-secondary:hover { background: #e2e8f0; }

.btn-outline {
    background: transparent;
    border: 1px solid var(--line);
    color: var(--muted);
}
.btn-outline:hover {
    border-color: var(--brand);
    color: var(--brand);
}
.btn-sm {
    padding: 4px 10px;
    font-size: 12px;
}


/* Chat History */
.chat-history {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    background: #fff;
    scroll-behavior: smooth;
}

.system-message {
    text-align: center;
    font-size: 12px;
    color: #94a3b8;
    margin: 20px 0;
}

.message {
    display: flex;
    margin-bottom: 24px;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity:0; transform:translateY(10px); } to { opacity:1; transform:translateY(0); } }

.message.user { justify-content: flex-end; }
.message.assistant { justify-content: flex-start; }

.bubble {
    max-width: 80%;
    padding: 12px 18px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.6;
    position: relative;
    word-break: break-word;
}

.message.user .bubble {
    background: var(--brand);
    color: #fff;
    border-bottom-right-radius: 2px;
    box-shadow: 0 4px 12px rgba(11, 58, 102, 0.15);
}

.message.assistant .bubble {
    background: #f8fafc;
    color: var(--text);
    border: 1px solid var(--line);
    border-bottom-left-radius: 2px;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 18px;
    background: #f8fafc;
    border-radius: 12px;
    width: fit-content;
    margin-bottom: 20px;
}
.dot {
    width: 6px; height: 6px;
    background: #cbd5e1;
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}
.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }
@keyframes bounce { 0%, 80%, 100% { transform: scale(0); } 40% { transform: scale(1); } }


/* Input Area */
.chat-input-area {
    padding: 20px 24px;
    background: #fff;
    border-top: 1px solid var(--line);
}

.input-actions {
    display: flex;
    margin-bottom: 8px;
}

.input-wrapper {
    display: flex;
    gap: 12px;
    background: #f8fafc;
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px;
    transition: border-color 0.2s;
}
.input-wrapper:focus-within { border-color: var(--brand); }

textarea {
    flex: 1;
    border: none;
    background: transparent;
    resize: none;
    outline: none;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.5;
    max-height: 200px;
    overflow-y: auto;
}

.footer-hint {
    margin-top: 12px;
    text-align: center;
    font-size: 11px;
    color: #94a3b8;
}

.error-message {
    color: #ef4444;
    font-size: 13px;
    margin-top: 8px;
    text-align: center;
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}
.modal-content {
    background: #fff;
    border-radius: var(--radius);
    width: 500px;
    max-width: 90%;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
}
.modal-header {
    padding: 16px 24px;
    border-bottom: 1px solid var(--line);
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.modal-header h3 { margin: 0; font-size: 16px; color: var(--brand); }
.close-modal-btn { background:none; border:none; font-size:20px; cursor:pointer; color:var(--muted); }

.modal-body { padding: 24px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text); }
.form-group input, .form-group textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 14px;
    outline: none;
}
.form-group input:focus, .form-group textarea:focus { border-color: var(--brand); }

.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--line);
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

/* --- Markdown Styles for Assistant Bubble --- */
.message.assistant .bubble p {
    margin: 0 0 10px 0;
}
.message.assistant .bubble p:last-child {
    margin-bottom: 0;
}

.message.assistant .bubble h1,
.message.assistant .bubble h2,
.message.assistant .bubble h3,
.message.assistant .bubble h4,
.message.assistant .bubble h5,
.message.assistant .bubble h6 {
    margin: 16px 0 8px 0;
    font-weight: 600;
    line-height: 1.3;
    color: var(--brand);
}
.message.assistant .bubble h1:first-child,
.message.assistant .bubble h2:first-child,
.message.assistant .bubble h3:first-child {
    margin-top: 0;
}
.message.assistant .bubble h1 { font-size: 1.4em; border-bottom: 1px solid #e2e8f0; padding-bottom: 4px; }
.message.assistant .bubble h2 { font-size: 1.25em; }
.message.assistant .bubble h3 { font-size: 1.1em; }

.message.assistant .bubble ul,
.message.assistant .bubble ol {
    margin: 0 0 10px 0;
    padding-left: 20px;
}
.message.assistant .bubble li {
    margin-bottom: 4px;
}

.message.assistant .bubble pre {
    background: #1e293b;
    color: #f8fafc;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
    font-size: 13px;
    margin: 10px 0;
}
.message.assistant .bubble code {
    background: #e2e8f0;
    color: #0f172a;
    padding: 2px 5px;
    border-radius: 4px;
    font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
    font-size: 0.9em;
}
.message.assistant .bubble pre code {
    background: transparent;
    color: inherit;
    padding: 0;
    border-radius: 0;
}

.message.assistant .bubble blockquote {
    margin: 10px 0;
    padding: 0 12px;
    border-left: 3px solid var(--brand);
    color: #64748b;
    background: #f1f5f9;
    border-radius: 2px;
}

.message.assistant .bubble a {
    color: var(--brand);
    text-decoration: none;
    border-bottom: 1px dotted var(--brand);
}
.message.assistant .bubble a:hover {
    border-bottom-style: solid;
}

.message.assistant .bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
    font-size: 0.9em;
}
.message.assistant .bubble th,
.message.assistant .bubble td {
    border: 1px solid #e2e8f0;
    padding: 6px 10px;
    text-align: left;
}
.message.assistant .bubble th {
    background: #f1f5f9;
    font-weight: 600;
}

.message.assistant .bubble hr {
    border: none;
    border-top: 1px solid #e2e8f0;
    margin: 16px 0;
}

.message.assistant .bubble img {
    max-width: 100%;
    border-radius: 8px;
    margin: 8px 0;
}