:root{
    --bg-app: #F4F1EA;
    --bg-sidebar: #EEEAE1;
    --bg-panel: #FFFFFF;
    --text-main: #2B2823;
    --text-muted: #7A7468;
    --border: #E4DFD3;
    --accent: #C96442;
    --accent-hover: #B5573A;
    --accent-soft: #F1E4DC;
    --bubble-user: #EDE6D8;
    --success: #2E7D5B;
    --success-soft: #E5F2EC;
    --danger: #B53B3B;
    --danger-soft: #F7E4E4;
    --radius-lg: 18px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --font-serif: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  }
  
  *{ box-sizing: border-box; }
  html, body{ height: 100%; }
  body{
    margin:0;
    font-family: var(--font-sans);
    background: var(--bg-app);
    color: var(--text-main);
    -webkit-font-smoothing: antialiased;
  }
  
  .app{
    display:flex;
    height:100vh;
    width:100%;
    overflow:hidden;
  }
  
  /* ---------------- Sidebar ---------------- */
  .sidebar{
    width: 268px;
    min-width: 268px;
    background: var(--bg-sidebar);
    display:flex;
    flex-direction:column;
    padding: 14px 10px;
    border-right: 1px solid var(--border);
    transition: margin-left .25s ease;
  }
  .sidebar.collapsed{ margin-left: -268px; }
  
  .brand{
    display:flex;
    align-items:center;
    gap:8px;
    padding: 6px 8px 16px 8px;
  }
  .brand-mark{
    width:26px; height:26px;
    border-radius: 7px;
    background: var(--accent);
    display:flex; align-items:center; justify-content:center;
    color:#fff; font-weight:700; font-size:14px;
    font-family: var(--font-serif);
  }
  .brand-name{
    font-weight:600;
    font-size:15px;
    letter-spacing: .2px;
  }
  
  .new-chat-btn{
    display:flex;
    align-items:center;
    gap:8px;
    background: var(--bg-panel);
    border:1px solid var(--border);
    color: var(--text-main);
    padding: 9px 12px;
    border-radius: var(--radius-md);
    font-size: 13.5px;
    cursor:pointer;
    font-weight:500;
    margin-bottom: 14px;
    transition: background .15s ease, box-shadow .15s ease;
  }
  .new-chat-btn:hover{ background: var(--accent-soft); border-color: var(--accent); }
  .new-chat-btn svg{ width:15px; height:15px; flex-shrink:0; }
  
  .sidebar-section-label{
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    padding: 10px 10px 6px 10px;
  }
  
  .history-list{
    flex:1;
    overflow-y:auto;
    display:flex;
    flex-direction:column;
    gap:2px;
    padding: 0 2px;
  }
  .history-item{
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    font-size: 13.5px;
    color: var(--text-main);
    cursor:pointer;
    white-space:nowrap;
    overflow:hidden;
    text-overflow: ellipsis;
    transition: background .12s ease;
  }
  .history-item:hover{ background: rgba(0,0,0,0.045); }
  .history-item.active{ background: var(--accent-soft); color: var(--accent-hover); font-weight:600; }
  
  .sidebar-footer{
    display:flex;
    align-items:center;
    gap:8px;
    padding: 10px 8px 4px 8px;
    border-top: 1px solid var(--border);
    margin-top: 8px;
    cursor:pointer;
    border-radius: var(--radius-sm);
  }
  .sidebar-footer:hover{ background: rgba(0,0,0,0.04); }
  .avatar{
    width:26px; height:26px;
    border-radius:50%;
    background: #6B5B4D;
    color:#fff;
    display:flex; align-items:center; justify-content:center;
    font-size:12px; font-weight:600;
  }
  .footer-name{ font-size: 13px; font-weight:500; }
  .footer-plan{ font-size: 11px; color: var(--text-muted); }
  
  /* ---------------- Main ---------------- */
  .main{
    flex:1;
    display:flex;
    flex-direction:column;
    min-width:0;
    background: var(--bg-app);
    position:relative;
  }
  
  .topbar{
    display:flex;
    align-items:center;
    justify-content:space-between;
    padding: 10px 18px;
    gap: 10px;
  }
  .topbar-left{ display:flex; align-items:center; gap:10px; }
  .topbar-title{ display:flex; flex-direction:column; }
  .topbar-title-main{
    font-family: var(--font-serif);
    font-size: 17px;
    font-weight: 600;
    color: var(--text-main);
    line-height: 1.1;
  }
  .topbar-title-sub{
    font-size: 11.5px;
    color: var(--text-muted);
    display:flex;
    align-items:center;
    gap:6px;
    margin-top: 2px;
  }
  .dot{
    width:7px; height:7px;
    border-radius:50%;
    background: var(--text-muted);
    display:inline-block;
  }
  .dot-loading{
    background: #C2BBA9;
    animation: pulse 1.2s ease-in-out infinite;
  }
  .dot-ok{ background: var(--success); }
  .dot-err{ background: var(--danger); }
  @keyframes pulse{
    0%, 100%{ opacity: 0.4; }
    50%{ opacity: 1; }
  }
  
  .icon-btn{
    width:32px; height:32px;
    display:flex; align-items:center; justify-content:center;
    border-radius: var(--radius-sm);
    cursor:pointer;
    color: var(--text-muted);
    background:transparent;
    border:none;
  }
  .icon-btn:hover{ background: rgba(0,0,0,0.05); color: var(--text-main); }
  .icon-btn:disabled{ opacity:.4; cursor:default; }
  .icon-btn svg{ width:18px; height:18px; }
  
  .chat-scroll{
    flex:1;
    overflow-y: auto;
    overflow-x: hidden;
    display:flex;
    justify-content:center;
  }
  .chat-inner{
    width:100%;
    max-width: 780px;
    /* padding: 20px 24px 180px 24px; */
    display:flex;
    flex-direction:column;
    gap: 15px;
  }
  
  /* Empty / hero state */
  .hero{
    flex:1;
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:center;
    text-align:center;
    gap: 22px;
    min-height: calc(100vh - 220px);
  }
  .hero-mark{
    width:46px; height:46px;
    border-radius: 12px;
    background: var(--accent);
    color:#fff;
    display:flex; align-items:center; justify-content:center;
    font-family: var(--font-serif);
    font-size:24px;
    font-weight:700;
  }
  .hero-title{
    font-family: var(--font-serif);
    font-size: 30px;
    font-weight: 500;
    color: var(--text-main);
    letter-spacing: .1px;
  }
  .hero-title span{ color: var(--accent); }
  .hero-sub{
    font-size: 14.5px;
    color: var(--text-muted);
    max-width: 560px;
    line-height: 1.55;
  }
  
  .suggestion-row{
    display:flex;
    gap:10px;
    flex-wrap:wrap;
    justify-content:center;
    max-width: 660px;
  }
  .suggestion-chip{
    border: 1px solid var(--border);
    background: var(--bg-panel);
    padding: 9px 14px;
    border-radius: 999px;
    font-size: 13px;
    color: var(--text-main);
    cursor:pointer;
    transition: border-color .15s ease, background .15s ease;
  }
  .suggestion-chip:hover{ border-color: var(--accent); background: var(--accent-soft); }
  
  /* Messages */
  .msg-row{
    display:flex;
    gap: 12px;
    align-items:flex-start;
  }
  .msg-row.user{ justify-content:flex-end; }
  
  .msg-avatar{
    width:26px; height:26px;
    border-radius:7px;
    background: var(--accent);
    color:#fff;
    display:flex; align-items:center; justify-content:center;
    font-size:13px; font-weight:700;
    font-family: var(--font-serif);
    flex-shrink:0;
    margin-top:2px;
  }
  
  .msg-bubble{
    font-size: 15px;
    line-height: 1.6;
    max-width: 640px;
    overflow-x: hidden;
  }
  .msg-row.user .msg-bubble{
    background: var(--bubble-user);
    padding: 10px 16px;
    border-radius: var(--radius-lg);
    border-top-right-radius: 6px;
  }
  .msg-row.assistant .msg-bubble{
    padding-top: 2px;
    max-width: 100%;
  }
  .msg-bubble p{ margin: 0 0 10px 0; }
  .msg-bubble p:last-child{ margin-bottom:0; }
  .msg-bubble code{
    background: rgba(0,0,0,0.06);
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'SF Mono', Consolas, monospace;
  }
  
  /* User file meta */
  .msg-row.user .msg-bubble .msg-file{
    display:inline-flex;
    align-items:center;
    gap:6px;
    background: rgba(255,255,255,0.55);
    border: 1px solid rgba(0,0,0,0.06);
    border-radius: 999px;
    padding: 2px 10px;
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 6px;
  }
  .msg-row.user .msg-bubble .msg-file svg{ width:12px; height:12px; }
  
  /* Result block */
  .msg-bubble--result .result-block{
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 14px 16px;
    margin-top: 4px;
    width: 100%;
    max-width: 640px;
    box-sizing: border-box;
    overflow-x: auto;
  }
  .result-head{
    display:flex;
    align-items:center;
    gap:8px;
    font-weight: 600;
    color: var(--success);
    font-size: 14.5px;
  }
  .result-icon{
    display:inline-flex;
    align-items:center; justify-content:center;
    width:18px; height:18px;
    background: var(--success-soft);
    color: var(--success);
    border-radius:50%;
    font-size: 11px;
  }
  .result-meta{
    font-size: 12px;
    color: var(--text-muted);
    margin: 6px 0 10px 26px;
  }
  .result-files{
    display:flex;
    flex-direction:column;
    gap:6px;
    margin-left: 26px;
  }
  .result-file{
    display:inline-flex;
    align-items:center;
    gap:8px;
    padding: 8px 14px;
    border: 1px solid var(--accent);
    background: var(--accent-soft);
    color: var(--accent-hover);
    border-radius: 999px;
    font-size: 13.5px;
    font-weight: 500;
    text-decoration: none;
    transition: background .15s ease, color .15s ease;
    align-self:flex-start;
  }
  .result-file:hover{
    background: var(--accent);
    color: #fff;
  }
  .result-summary{
    margin: 12px 0 0 26px;
    font-size: 13px;
    color: var(--text-muted);
  }
  
  /* Error bubble */
  .msg-bubble--error{
    background: var(--danger-soft);
    border: 1px solid #E7B5B5;
    color: var(--danger);
    padding: 10px 14px;
    border-radius: var(--radius-md);
    font-size: 14px;
  }
  
  /* Typing */
  .typing-dots{
    display:flex;
    gap:4px;
    padding: 8px 0;
  }
  .typing-dots span{
    width:6px; height:6px;
    border-radius:50%;
    background: var(--text-muted);
    animation: bounce 1.2s infinite ease-in-out;
  }
  .typing-dots span:nth-child(2){ animation-delay: .15s; }
  .typing-dots span:nth-child(3){ animation-delay: .3s; }
  @keyframes bounce{
    0%, 60%, 100%{ transform: translateY(0); opacity:.5; }
    30%{ transform: translateY(-4px); opacity:1; }
  }
  
  /* Composer */
  .composer-wrap{
    position:absolute;
    left:0; right:0; bottom:0;
    display:flex;
    justify-content:center;
    padding: 14px 24px 20px 24px;
    background: linear-gradient(to top, var(--bg-app) 55%, rgba(244,241,234,0));
    pointer-events:none;
  }
  .composer{
    pointer-events:all;
    width:100%;
    max-width: 780px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 4px 18px rgba(60, 50, 35, 0.07);
    padding: 10px 10px 8px 16px;
    display:flex;
    flex-direction:column;
    gap: 6px;
  }
  .composer textarea{
    width:100%;
    border:none;
    outline:none;
    resize:none;
    background:transparent;
    font-family: inherit;
    font-size: 15px;
    line-height:1.5;
    color: var(--text-main);
    max-height: 200px;
    padding-top: 6px;
  }
  .composer textarea:disabled{ opacity:.6; }
  .composer textarea::placeholder{ color: var(--text-muted); }
  
  /* File chip */
  .file-chips{
    display:flex;
    flex-wrap:wrap;
    gap:6px;
    padding-top: 4px;
  }
  .file-chip{
    display:inline-flex;
    align-items:center;
    gap:8px;
    background: var(--accent-soft);
    border: 1px solid var(--accent);
    color: var(--accent-hover);
    border-radius: 999px;
    padding: 4px 6px 4px 10px;
    font-size: 12.5px;
    max-width: 100%;
  }
  .file-chip-icon{ width:14px; height:14px; flex-shrink:0; }
  .file-chip-name{
    font-weight: 500;
    white-space:nowrap;
    overflow:hidden;
    text-overflow: ellipsis;
    max-width: 240px;
  }
  .file-chip-size{
    font-size: 11.5px;
    color: var(--text-muted);
  }
  .file-chip-remove{
    display:inline-flex; align-items:center; justify-content:center;
    width:20px; height:20px;
    border-radius:50%;
    border:none;
    background: rgba(0,0,0,0.06);
    color: var(--accent-hover);
    cursor:pointer;
  }
  .file-chip-remove:hover{ background: var(--accent); color: #fff; }
  .file-chip-remove svg{ width:11px; height:11px; }
  
  .composer.composer--drag{
    outline: 2px dashed var(--accent);
    outline-offset: 2px;
    background: var(--accent-soft);
  }
  
  .result-file-tag{
    display:inline-block;
    background: var(--accent-soft);
    border: 1px solid var(--accent);
    color: var(--accent-hover);
    border-radius: 999px;
    padding: 2px 10px;
    font-size: 12px;
    margin-right: 6px;
    margin-bottom: 4px;
  }
  
  .composer-actions{
    display:flex;
    align-items:center;
    justify-content:space-between;
  }
  .composer-actions-left{ display:flex; gap:4px; align-items:center; }
  .composer-hint{
    font-size: 11.5px;
    color: var(--text-muted);
    margin-left: 6px;
  }
  .send-btn{
    width:32px; height:32px;
    border-radius:50%;
    background: var(--accent);
    border:none;
    color:#fff;
    display:flex; align-items:center; justify-content:center;
    cursor:pointer;
    transition: background .15s ease, transform .1s ease;
  }
  .send-btn:hover{ background: var(--accent-hover); }
  .send-btn:disabled{ background: #D9D2C4; cursor:default; }
  .send-btn.is-loading{
    background: var(--accent-hover);
    animation: pulse 1s ease-in-out infinite;
  }
  .send-btn svg{ width:15px; height:15px; }
  
  .disclaimer{
    text-align:center;
    font-size: 11.5px;
    color: var(--text-muted);
    margin-top: 8px;
  }
  
  /* Scrollbars */
  .history-list::-webkit-scrollbar, .chat-scroll::-webkit-scrollbar{ width:8px; }
  .history-list::-webkit-scrollbar-thumb, .chat-scroll::-webkit-scrollbar-thumb{
    background: rgba(0,0,0,0.12);
    border-radius: 10px;
  }
  
  @media (max-width: 780px){
    .sidebar{ position:fixed; z-index:20; height:100vh; }
    /* Bỏ box-shadow backdrop cũ vì đã dùng .sidebar-backdrop thật. */
    .sidebar.collapsed{ margin-left:-268px; }
    .topbar-title{ display:none; }
    .result-files{ margin-left: 0; }
    .composer-case-row{ flex-direction: column; align-items: stretch; }
  }
  
  /* Backdrop thật (chỉ hiện trên mobile khi sidebar mở).
     - hidden: display:none + không bắt sự kiện.
     - Mặc định trên desktop: hidden = display:none (luôn ẩn). */
  .sidebar-backdrop{
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.35);
    z-index: 15; /* dưới sidebar (20) nhưng trên chat (mặc định 0) */
    opacity: 0;
    transition: opacity .2s ease;
  }
  .sidebar-backdrop[hidden]{ display: none !important; }
  /* Trên desktop KHÔNG hiện backdrop — sidebar vẫn chiếm chỗ và không cần che. */
  @media (min-width: 781px){
    .sidebar-backdrop{ display: none !important; }
  }
  /* Trên mobile: hiện khi sidebar mở (KHÔNG có class .collapsed). */
  @media (max-width: 780px){
    .sidebar:not(.collapsed) ~ .sidebar-backdrop{
      display: block;
      opacity: 1;
    }
  }
  /* Topbar z-index cao hơn sidebar để nút toggle luôn click được khi sidebar mở. */
  .topbar{ position: relative; z-index: 25; }
  
  /* ============== Sidebar history items (case) ============== */
  .sidebar-section-label{
    display:flex;
    justify-content: space-between;
    align-items: center;
    color: var(--text-muted);
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin: 14px 8px 8px;
  }
  .sidebar-count{
    font-weight: 600;
    color: var(--accent);
  }
  .history-item{
    padding: 10px 12px;
    margin-bottom: 4px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color .15s ease;
    border: 1px solid transparent;
  }
  .history-item:hover{ background: rgba(0,0,0,0.04); }
  .history-item.active{
    background: var(--accent-soft);
    border-color: rgba(201, 100, 66, 0.25);
  }
  .history-item-code{
    font-weight: 600;
    font-size: 13px;
    color: var(--text-main);
    font-family: 'SF Mono', Menlo, Consolas, monospace;
    letter-spacing: 0.02em;
  }
  .history-item-title{
    font-size: 12.5px;
    color: var(--text-muted);
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .history-item-meta{
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 4px;
    opacity: 0.85;
  }
  .history-item{
    /* Bật flex layout để nút xoá canh phải. */
    display: flex;
    align-items: flex-start;
    gap: 6px;
  }
  .history-item-body{
    flex: 1 1 auto;
    min-width: 0; /* cho phép ellipsis trên title */
  }
  .history-item-delete{
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    margin-top: 2px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted, #8a8a8a);
    cursor: pointer;
    opacity: 0;
    transition: background-color .12s ease, opacity .12s ease, color .12s ease;
  }
  .history-item:hover .history-item-delete,
  .history-item:focus-within .history-item-delete,
  .history-item-delete:focus-visible{
    opacity: 1;
  }
  .history-item-delete:hover{
    background: rgba(220, 50, 47, 0.12);
    color: #c0392b;
  }
  .history-item.active .history-item-delete{
    /* Vẫn hiện khi item active để có thể xoá. */
    opacity: 1;
  }
  
  /* ============== Composer: case_code row ============== */
  .composer-case-row{
    display:flex;
    align-items: center;
    gap: 8px;
    padding: 0 2px 10px 2px;
  }
  .composer-case-label{
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 600;
    white-space: nowrap;
  }
  #caseCodeInput{
    flex: 1;
    padding: 6px 10px;
    font-size: 13px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-panel);
    color: var(--text-main);
    font-family: 'SF Mono', Menlo, Consolas, monospace;
  }
  #caseCodeInput:focus{
    outline: none;
    border-color: var(--accent);
  }
  .ghost-btn{
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--border);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    font-size: 12.5px;
    cursor: pointer;
    transition: all .15s ease;
  }
  .ghost-btn:hover{
    color: var(--text-main);
    border-color: var(--accent);
  }
  .ghost-btn--small{ padding: 4px 10px; font-size: 12px; }
  .ghost-btn.hidden{ display: none; }
  .primary-btn{
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .15s ease;
  }
  .primary-btn:hover{ background: var(--accent-hover); }
  .primary-btn:disabled{ opacity: 0.5; cursor: not-allowed; }
  
  /* ============== Topbar: case title + close ============== */
  .topbar-right{ margin-left: auto; }
  .topbar-title-main{ font-weight: 600; }
  .topbar-divider{ color: var(--text-muted); margin: 0 4px; }
  .topbar-case-title-text{ font-weight: 500; }
  
  /* ============== Case banner trong chat ============== */
  .case-banner{
    background: linear-gradient(135deg, var(--accent-soft), #F5E6DC);
    border: 1px solid rgba(201, 100, 66, 0.25);
    border-radius: var(--radius-md);
    padding: 14px 18px;
    margin-bottom: 16px;
  }
  .case-banner-code{
    font-family: 'SF Mono', Menlo, Consolas, monospace;
    font-size: 13px;
    font-weight: 700;
    color: var(--accent);
    letter-spacing: 0.02em;
  }
  .case-banner-title{
    font-size: 15px;
    color: var(--text-main);
    font-weight: 600;
    margin-top: 4px;
  }
  .case-banner-meta{
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
  }
  
  /* ============== Modal tạo / cập nhật case ============== */
  .modal-overlay{
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(2px);
  }
  .modal-overlay.hidden{ display: none; }
  .modal{
    background: var(--bg-panel);
    border-radius: var(--radius-lg);
    width: min(540px, 92vw);
    max-height: 90vh;
    overflow: auto;
    box-shadow: 0 12px 40px rgba(0,0,0,0.18);
  }
  .modal-header{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
  }
  .modal-header h2{
    font-size: 16px;
    font-weight: 600;
    margin: 0;
  }
  .modal-body{
    padding: 18px 20px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
  }
  .modal-body label{
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 13px;
    color: var(--text-main);
  }
  .modal-body label > span{ font-weight: 600; }
  .modal-body label em{ font-weight: 400; color: var(--text-muted); font-style: normal; }
  .modal-body input, .modal-body textarea{
    font-family: inherit;
    font-size: 13px;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-panel);
    color: var(--text-main);
    resize: vertical;
  }
  .modal-body input:focus, .modal-body textarea:focus{
    outline: none;
    border-color: var(--accent);
  }
  .modal-body small{
    font-size: 11.5px;
    color: var(--text-muted);
  }
  .modal-body code{
    background: var(--accent-soft);
    color: var(--accent);
    padding: 1px 5px;
    border-radius: 3px;
    font-size: 11px;
  }
  .modal-actions{
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 6px;
  }
  
  