/* agent-chat.css — khung Trợ lý AI (agent-chat.js) trên app chính. */
.agent-panel {
    position: fixed;
    /* env(): iPhone xoay ngang (> 600px) có Dynamic Island ở cạnh trái/phải. */
    right: calc(env(safe-area-inset-right, 0px) + 20px);
    bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
    z-index: 65;
    width: 420px;
    max-width: calc(100vw - 32px);
    height: min(640px, calc(100vh - 40px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)));
    display: flex;
    flex-direction: column;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.22);
    font-family: var(--font);
    overflow: hidden;
}
.agent-panel.hidden { display: none !important; }

.agent-head {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-subtle);
}
.agent-title-wrap { flex: 1; min-width: 0; }
.agent-title { font-size: 15px; font-weight: 700; }
.agent-model {
    display: block;
    max-width: 100%;
    margin-top: 2px;
    padding: 0;
    border: none;
    background: none;
    color: var(--text-muted);
    font: inherit;
    font-size: 11px;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}
.agent-model:hover { color: var(--accent); }
/* Scope của trang dưới tiêu đề (reader: sách đang mở; editor: tài liệu). */
.agent-scope {
    margin-top: 2px;
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
/* Phiên đang xem thuộc sách khác sách đang mở: dải báo giữa header và danh
   sách, không nằm trong .agent-list để không đụng cuộn và không trôi mất. */
.agent-scope-note {
    padding: 6px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-subtle);
    color: var(--text-secondary);
    font-size: 12px;
    line-height: 1.4;
}
.agent-scope-note[hidden] { display: none; }
.agent-icon-btn {
    border: 1px solid var(--border);
    background: var(--surface-card);
    color: var(--text-secondary);
    border-radius: 8px;
    padding: 4px 10px;
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}
.agent-icon-btn:hover { background: var(--surface-card-hover); color: var(--text); }
.agent-icon-btn:disabled { opacity: 0.5; cursor: default; }

.agent-list {
    flex: 1;
    overflow-y: auto;
    /* Điện thoại: panel cao 100dvh, cuộn tới đầu/cuối danh sách không chuyền
       xuống trang phía sau (cảm giác kẹt cuộn). */
    overscroll-behavior: contain;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.agent-empty { display: flex; flex-direction: column; gap: 8px; margin: auto 0; }
.agent-empty-title { font-size: 14px; font-weight: 700; }
.agent-empty-sub { font-size: 12px; color: var(--text-secondary); line-height: 1.5; margin-bottom: 4px; }
.agent-chip {
    text-align: left;
    border: 1px solid var(--border);
    background: var(--surface-card);
    color: var(--text);
    border-radius: 10px;
    padding: 8px 12px;
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}
.agent-chip:hover { border-color: var(--border-focus); background: var(--surface-card-hover); }

.agent-msg { display: flex; flex-direction: column; max-width: 92%; }
.agent-msg-user { align-self: flex-end; align-items: flex-end; }
.agent-msg-assistant { align-self: flex-start; }
.agent-bubble {
    padding: 9px 12px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.55;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
.agent-msg-user .agent-bubble {
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}
.agent-msg-assistant .agent-bubble {
    background: var(--surface-card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}
.agent-msg-assistant.is-pending .agent-bubble:empty { display: none; }
/* Nháp của bước trước, giữ mờ trong lúc công cụ chạy và chờ bước sau viết lại. */
.agent-msg-assistant.is-pending .agent-bubble.is-draft { opacity: 0.5; transition: opacity 0.2s; }
.agent-msg.is-error .agent-bubble { border-color: var(--orange); background: var(--orange-soft); }
.agent-msg.is-error > .agent-btn { margin-top: 6px; align-self: flex-start; }
/* Tin bị Dừng / lỗi giữa chừng (msg.interrupted): giữ phần đã viết, viền đứt + ghi chú. */
.agent-msg.is-interrupted .agent-bubble { border-style: dashed; }
.agent-note { margin-top: 4px; font-size: 12px; color: var(--orange); }

.agent-bubble p { margin: 0; }
.agent-bubble ul, .agent-bubble ol { margin: 2px 0; padding-left: 20px; }
.agent-bubble pre {
    margin: 6px 0;
    padding: 8px 10px;
    background: var(--surface);
    border-radius: 8px;
    overflow-x: auto;
    font-size: 12px;
    white-space: pre-wrap;
}
.agent-bubble code { font-size: 0.92em; background: var(--surface); padding: 1px 4px; border-radius: 4px; }
.agent-bubble pre code { background: none; padding: 0; }
.agent-md-h { font-weight: 700; margin-top: 4px; }
.agent-md-gap { height: 6px; }
.agent-bubble hr { border: none; border-top: 1px solid var(--border); margin: 8px 0; }
.agent-bubble blockquote {
    margin: 4px 0;
    padding: 2px 10px;
    border-left: 3px solid var(--border);
    color: var(--text-secondary);
}
.agent-bubble table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    border-collapse: collapse;
    margin: 6px 0;
    font-size: 12.5px;
}
.agent-bubble th, .agent-bubble td {
    border: 1px solid var(--border);
    padding: 4px 8px;
    text-align: left;
    vertical-align: top;
}
.agent-bubble th { background: var(--surface); font-weight: 700; }
.agent-bubble a { color: var(--accent); text-decoration: underline; }
.agent-bubble del { color: var(--text-muted); }

.agent-status { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.agent-trace { display: flex; flex-direction: column; gap: 2px; margin-bottom: 4px; }
.agent-trace-row { font-size: 11px; color: var(--text-muted); }
.agent-trace-row.is-failed { color: var(--orange); }
.agent-msg-actions { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px; margin-top: 3px; }
/* Số token của lượt (agent-chat.js formatUsage): vào / tỉ lệ prompt cache / ra. */
.agent-usage { font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; cursor: help; }
.agent-link-btn {
    border: none;
    background: none;
    padding: 0;
    color: var(--text-muted);
    font: inherit;
    font-size: 11px;
    cursor: pointer;
}
.agent-link-btn:hover { color: var(--accent); }
.agent-link-btn.is-armed { color: var(--orange); font-weight: 700; }
/* Sửa / Gửi lại / Tạo lại / Xoá (agent-chat.js messageActions): ẩn khi đang chạy. */
.agent-panel.is-busy .agent-act-mut { display: none; }
.agent-msg-user .agent-msg-actions { justify-content: flex-end; }
.agent-msg.is-editing { align-self: stretch; align-items: stretch; max-width: none; }
.agent-msg.is-editing > .agent-bubble,
.agent-msg.is-editing > .agent-msg-actions { display: none; }
.agent-edit { display: flex; flex-direction: column; gap: 6px; }
.agent-edit-input { width: 100%; box-sizing: border-box; min-height: 72px; resize: vertical; }
.agent-edit-note { font-size: 11px; color: var(--orange); }
.agent-edit-actions { display: flex; justify-content: flex-end; gap: 8px; }

.agent-confirm {
    align-self: stretch;
    border: 1px dashed var(--accent);
    border-radius: 12px;
    padding: 10px 12px;
    background: var(--accent-glow);
    font-size: 13px;
}
.agent-confirm-title { color: var(--text-secondary); }
/* Lô sua_tu_dien_rieng: mỗi mục một dòng, cuộn trong thẻ để nút Lưu/Bỏ qua
   vẫn trong tầm tay trên điện thoại khi có tới 50 dòng. */
.agent-confirm-entry { font-weight: 700; margin: 4px 0 8px; word-break: break-all; white-space: pre-line; max-height: 40vh; overflow: auto; }
.agent-confirm-actions { display: flex; gap: 8px; font-size: 12px; color: var(--text-muted); }
.agent-confirm.is-approved { border-style: solid; border-color: var(--green); background: var(--green-soft); }
.agent-confirm.is-declined { opacity: 0.6; }

.agent-form {
    position: relative; /* neo nút .agent-jump ngay phía trên */
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid var(--border);
}
.agent-input {
    flex: 1;
    resize: none;
    min-height: 38px;
    max-height: 160px;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface-card);
    color: var(--text);
    font: inherit;
    font-size: 14px;
    line-height: 1.4;
}
.agent-input:focus { outline: none; border-color: var(--border-focus); }
.agent-btn {
    border: 1px solid var(--border);
    background: var(--surface-card);
    color: var(--text);
    border-radius: 10px;
    padding: 8px 14px;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
}
.agent-btn:disabled { opacity: 0.5; cursor: default; }
.agent-btn-primary { background: var(--gradient-btn); color: #fff; border-color: transparent; }
.agent-btn-primary:hover:not(:disabled) { background: var(--gradient-btn-hover); }
.agent-send { min-height: 38px; }
.agent-send.is-stop { background: var(--orange); }
/* "Xuống cuối" (agent-chat.js scrollToEnd): hiện khi người dùng đã cuộn lên và
   token mới không còn kéo khung xuống đáy nữa. */
.agent-jump {
    position: absolute;
    left: 50%;
    bottom: calc(100% + 8px);
    transform: translateX(-50%);
    z-index: 1;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg);
    color: var(--text);
    font: inherit;
    font-size: 12px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
    cursor: pointer;
}
.agent-jump[hidden] { display: none; }

/* Lịch sử phiên (agent-chat.js showSessions): danh sách thay chỗ khung chat,
   ô nhập ẩn đi; bấm "Lịch sử" lần nữa hoặc mở một phiên để quay lại. */
.agent-icon-btn[aria-pressed="true"] { border-color: var(--accent); color: var(--text); background: var(--accent-glow); }
.agent-panel.is-sessions .agent-form { display: none; }
.agent-sessions { display: flex; flex-direction: column; gap: 6px; }
.agent-sessions-head {
    margin: 10px 2px 0;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
}
.agent-sessions-head:first-child { margin-top: 0; }
.agent-sessions-note { padding: 24px 8px; font-size: 13px; color: var(--text-muted); text-align: center; }
/* Ghi nhớ của sách / trang (agent-chat.js renderNotes), đầu khung Lịch sử. */
.agent-memos { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.agent-memos-head {
    margin: 0 2px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
}
.agent-memos-empty { padding: 4px 2px 8px; font-size: 12px; color: var(--text-muted); }
.agent-memo {
    display: flex;
    align-items: stretch;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-card);
    overflow: hidden;
}
.agent-memo-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; padding: 8px 12px; }
.agent-memo-title { font-size: 13px; font-weight: 700; overflow-wrap: anywhere; }
.agent-memo-text { font-size: 12px; color: var(--text-secondary); white-space: pre-line; max-height: 4.6em; overflow: auto; overflow-wrap: anywhere; }
.agent-session {
    display: flex;
    align-items: stretch;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-card);
    overflow: hidden;
}
.agent-session.is-current { border-color: var(--accent); }
.agent-session-open {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 8px 12px;
    border: none;
    background: none;
    color: var(--text);
    font: inherit;
    text-align: left;
    cursor: pointer;
}
.agent-session-open:hover { background: var(--surface-card-hover); }
.agent-session-title,
.agent-session-meta { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent-session-title { font-size: 13px; font-weight: 600; }
.agent-session-meta { font-size: 11px; color: var(--text-muted); }
.agent-session-del {
    flex: none;
    min-width: 40px;
    border: none;
    border-left: 1px solid var(--border);
    background: none;
    color: var(--text-muted);
    font: inherit;
    font-size: 15px;
    cursor: pointer;
}
.agent-session-del:hover { color: var(--orange); }
.agent-session-del.is-armed { min-width: 56px; font-size: 12px; font-weight: 700; color: #fff; background: var(--orange); }

@media (max-width: 600px) {
    .agent-panel {
        left: 0;
        right: 0;
        bottom: 0;
        width: auto;
        max-width: none;
        height: 100dvh;
        border-radius: 0;
        border: none;
    }
    /* index.html dùng viewport-fit=cover + status bar black-translucent: trên
       iPhone (PWA) nội dung tràn xuống dưới status bar / Dynamic Island và thanh
       Home. Đệm header và ô nhập vào vùng an toàn; nền header vẫn phủ status bar. */
    .agent-head {
        padding-top: calc(env(safe-area-inset-top, 0px) + 12px);
        padding-left: calc(env(safe-area-inset-left, 0px) + 14px);
        padding-right: calc(env(safe-area-inset-right, 0px) + 14px);
    }
    .agent-list,
    .agent-scope-note {
        padding-left: calc(env(safe-area-inset-left, 0px) + 14px);
        padding-right: calc(env(safe-area-inset-right, 0px) + 14px);
    }
    .agent-form {
        padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 10px);
        padding-left: calc(env(safe-area-inset-left, 0px) + 12px);
        padding-right: calc(env(safe-area-inset-right, 0px) + 12px);
    }
    /* 16px: iOS khong zoom khi focus; 44px: dich co cham toi thieu (quy uoc popup/lookup). */
    .agent-input { font-size: 16px; }
    .agent-send { min-height: 44px; }
    .agent-session-open { min-height: 44px; padding: 10px 12px; }
    .agent-session-del { min-width: 44px; }
    /* Thao tác dưới tin nhắn: chạm được bằng ngón tay. */
    .agent-msg-actions { gap: 4px; }
    .agent-msg-actions .agent-link-btn { min-height: 44px; padding: 0 8px; font-size: 13px; }
    .agent-edit-actions .agent-btn { min-height: 44px; }
    /* Nút header (+ Mới / Lịch sử / ×) ~27px trên desktop: nâng vùng chạm. */
    .agent-icon-btn { min-height: 44px; padding: 8px 12px; }
    .agent-jump { min-height: 40px; }
}
