/* ===================== Trợ Thủ AI – giao diện ===================== */

/* ---------- Màu & kích thước ---------- */
:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #f0eef8;
  --bubble: #ebe9f7;
  --text: #17161f;
  --muted: #6b6a7b;
  --line: #e4e1ef;
  --line-soft: #ece9f5;
  --accent: #4f46e5;
  --accent-2: #9333ea;
  --accent-soft: #eceafe;
  --ok: #15803d;  --ok-soft: #e4f6ea;
  --bad: #c2410c; --bad-soft: #fdece4;
  --grad: linear-gradient(135deg, var(--accent), var(--accent-2));
  --shadow-sm: 0 1px 2px rgba(23, 22, 31, .05);
  --shadow: 0 1px 2px rgba(23, 22, 31, .04), 0 8px 24px -6px rgba(23, 22, 31, .10);
  --shadow-pop: 0 16px 40px -10px rgba(10, 10, 25, .28);
  --ring: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent);
  --appbar-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1015; --surface: #181922; --surface-2: #20212c; --bubble: #262739;
    --text: #ececf3; --muted: #9a9bb0; --line: #2a2b38; --line-soft: #222330;
    --accent: #8b85ff; --accent-2: #c084fc; --accent-soft: #23223f;
    --ok: #4ade80; --ok-soft: #13291d; --bad: #fb923c; --bad-soft: #2f1c12;
    --shadow-sm: none; --shadow: 0 8px 24px -8px rgba(0, 0, 0, .6); --shadow-pop: 0 16px 40px -8px rgba(0, 0, 0, .7);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1015; --surface: #181922; --surface-2: #20212c; --bubble: #262739;
  --text: #ececf3; --muted: #9a9bb0; --line: #2a2b38; --line-soft: #222330;
  --accent: #8b85ff; --accent-2: #c084fc; --accent-soft: #23223f;
  --ok: #4ade80; --ok-soft: #13291d; --bad: #fb923c; --bad-soft: #2f1c12;
  --shadow-sm: none; --shadow: 0 8px 24px -8px rgba(0, 0, 0, .6); --shadow-pop: 0 16px 40px -8px rgba(0, 0, 0, .7);
  color-scheme: dark;
}

/* ---------- Nền tảng ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; overflow-anchor: none; touch-action: pan-x pan-y; } /* app: không phóng to bằng 2 ngón / chạm 2 lần */ /* tắt scroll anchoring: không tự trôi khi chữ đang chạy */
body {
  margin: 0; min-height: 100dvh; overflow-anchor: none;
  background: var(--bg); color: var(--text);
  font: 16px/1.65 "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ic { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.ic.inline { width: 1.1em; height: 1.1em; vertical-align: -.2em; }

/* ---------- Thanh trên ---------- */
.appbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: calc(var(--appbar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line-soft);
}
/* Nền mờ tách ra lớp riêng: đặt backdrop-filter thẳng lên .appbar sẽ "neo" các phần tử position: fixed vào nó */
.appbar::before {
  content: ""; position: absolute; inset: 0; z-index: -1;
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: saturate(1.6) blur(16px); -webkit-backdrop-filter: saturate(1.6) blur(16px);
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: inherit; text-decoration: none; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand img { border-radius: 9px; box-shadow: 0 4px 12px -4px color-mix(in srgb, var(--accent) 60%, transparent); }
.actions { display: flex; gap: 4px; }
.iconbtn {
  display: inline-grid; grid-auto-flow: column; align-items: center; gap: 6px;
  min-width: 38px; height: 38px; padding: 0 9px; border: 0; border-radius: 12px;
  background: none; color: var(--muted); transition: background .15s, color .15s;
}
.iconbtn:hover { background: var(--surface-2); color: var(--text); }
.iconbtn.install { background: var(--accent-soft); color: var(--accent); font-size: 14px; font-weight: 600; padding: 0 12px; }

/* ---------- Khung chính ---------- */
main { width: 100%; max-width: 800px; margin: 0 auto; padding: 0 max(16px, env(safe-area-inset-left)); }
.panel { display: flex; flex-direction: column; min-height: calc(100dvh - var(--appbar-h) - env(safe-area-inset-top, 0px)); }

/* Màn chào */
.intro { text-align: center; padding: clamp(28px, 7vh, 72px) 0 20px; animation: rise .4s ease-out both; }
.hero-logo { display: block; margin: 0 auto 18px; border-radius: 20px; box-shadow: 0 14px 30px -12px color-mix(in srgb, var(--accent) 70%, transparent); }
.intro > p { margin: 0 auto 24px; max-width: 520px; color: var(--muted); }
.panel.has-chat .intro { display: none; }

/* ---------- Tin nhắn ---------- */
.messages { flex: 1; display: flex; flex-direction: column; gap: 22px; padding: 24px 0 16px; }
.msg { max-width: 100%; min-width: 0; }
.msg.user { align-self: flex-end; max-width: min(85%, 600px); display: grid; justify-items: end; gap: 6px; }
.msg.user .body {
  background: var(--bubble); padding: 10px 16px; border-radius: 22px 22px 6px 22px;
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 22em; overflow-y: auto;
}
.msg-atts { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
.msg-atts img {
  display: block; width: 132px; height: 132px; object-fit: cover; border-radius: 16px;
  border: 1px solid var(--line); background: var(--surface-2); cursor: zoom-in;
}
.msg-atts img:only-child { width: auto; height: auto; max-width: min(260px, 100%); max-height: 260px; }
.fchip {
  display: inline-flex; align-items: center; gap: 10px; max-width: 260px; padding: 8px 12px 8px 8px;
  border: 1px solid var(--line); border-radius: 14px; background: var(--surface); font-size: 14px;
}
.fchip .fi { flex: none; width: 34px; height: 34px; display: grid; place-items: center; border-radius: 10px; background: var(--grad); color: #fff; }
.fchip .fi .ic { width: 18px; height: 18px; }
.fchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.msg.assistant { display: grid; grid-template-columns: 30px minmax(0, 1fr); column-gap: 14px; align-items: start; }
.msg.assistant::before {
  content: ""; width: 30px; height: 30px; margin-top: 2px; border-radius: 9px;
  background: url("/icons/icon.svg") center / cover;
}
.msg.assistant .body { grid-column: 2; overflow-wrap: anywhere; min-width: 0; }
.msg.assistant .tools { grid-column: 2; display: flex; gap: 2px; margin-top: 4px; opacity: .85; }
.msg.typing .tools { visibility: hidden; }
.tool {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 8px;
  border: 0; border-radius: 9px; background: none; color: var(--muted); font-size: 13px;
}
.tool .ic { width: 16px; height: 16px; }
.tool:hover { background: var(--surface-2); color: var(--text); }
.msg.assistant.error .body { background: var(--bad-soft); color: var(--bad); padding: 10px 14px; border-radius: 14px; }
.waiting { margin: 0; color: var(--muted); }

/* chấm nhấp nháy khi đang chờ / đang viết */
.msg.typing .body:empty::after,
.msg.typing .body > :last-child::after {
  content: ""; display: inline-block; width: 9px; height: 9px; margin-left: 6px; border-radius: 50%;
  background: var(--text); vertical-align: middle; animation: pulse 1s ease-in-out infinite;
}
.msg.typing .body:empty { min-height: 28px; display: flex; align-items: center; }
.msg.typing .body:empty::after { margin-left: 0; }
.msg.typing .body > section:last-child::after { display: none; } /* khối đang viết dở: chấm nằm trong khối, không lơ lửng ngoài */
.msg.typing .body > section:last-child > :last-child::after {
  content: ""; display: inline-block; width: 9px; height: 9px; margin-left: 6px; border-radius: 50%;
  background: var(--text); vertical-align: middle; animation: pulse 1s ease-in-out infinite;
}

/* ---------- Câu trả lời chia khối (mỗi tiêu đề ### là một khối) ---------- */
.md .sec {
  margin: 12px 0; padding: 12px 16px 14px;
  border: 1px solid var(--line); border-radius: 16px; background: var(--surface);
}
.md .sec > :is(h2, h3, h4, h5):first-child {
  display: flex; align-items: center; gap: 8px; margin: 0 0 6px;
  font-size: 13px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--accent);
}
.md .sec > :is(h2, h3, h4, h5):first-child::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.md .sec > :nth-child(2) { margin-top: 0; }
.md .sec > :last-child { margin-bottom: 0; }
.md .sec .katex-display { margin: 8px 0; }
.md .sec-answer { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); font-weight: 600; }
.md .sec-answer .katex { font-size: 1.15em; }
.md .sec-check { background: transparent; border-style: dashed; }

/* ---------- Bộ đề ôn tập ngay trong cuộc trò chuyện ---------- */
.quiz-head { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); }
.quiz-head .ic { width: 16px; height: 16px; }
.quiztitle { margin: 4px 0 12px; font-size: 20px; letter-spacing: -.02em; line-height: 1.35; }
.score { display: flex; align-items: center; gap: 12px; margin: 0 0 14px; font-size: 14px; font-weight: 600; color: var(--muted); }
.progress { flex: 1; height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--grad); border-radius: inherit; transition: width .4s ease; }
.questions { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; counter-reset: q; }
.q { counter-increment: q; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 14px; box-shadow: var(--shadow-sm); }
.qtext { margin: 0 0 10px; font-weight: 600; }
.qtext::before { content: "Câu " counter(q); display: block; font-size: 12px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: .06em; margin-bottom: 2px; }
.opts { display: grid; gap: 8px; }
.opts button {
  display: flex; align-items: center; gap: 12px; text-align: left;
  padding: 9px 12px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface);
  transition: border-color .15s, background .15s, opacity .15s;
}
.opts button b { flex: none; width: 28px; height: 28px; display: grid; place-items: center; border-radius: 9px; background: var(--surface-2); font-size: 13px; }
.q:not(.done) .opts button:hover { border-color: var(--accent); background: var(--accent-soft); }
.opts button.right { border-color: var(--ok); background: var(--ok-soft); }
.opts button.right b { background: var(--ok); color: #fff; }
.opts button.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opts button.wrong b { background: var(--bad); color: #fff; }
.q.done .opts button { cursor: default; }
.q.done .opts button:not(.right):not(.wrong) { opacity: .6; }
.explain { margin: 10px 0 0; padding: 10px 12px; border-radius: 12px; background: var(--surface-2); font-size: 14px; animation: rise .25s ease-out both; }
.fc-title { margin: 24px 0 10px; font-size: 16px; }
.fc-title small { color: var(--muted); font-weight: 400; font-size: 13px; margin-left: 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.cards .card { position: relative; height: 140px; padding: 0; border: 0; background: none; perspective: 900px; }
.cards .card .front, .cards .card .back {
  position: absolute; inset: 0; display: grid; place-items: center; padding: 14px; overflow: auto;
  border-radius: 18px; text-align: center; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  transition: transform .5s cubic-bezier(.2, .7, .2, 1);
}
.cards .card .front { background: var(--surface); border: 1px solid var(--line); font-weight: 600; box-shadow: var(--shadow-sm); }
.cards .card .back { background: var(--grad); color: #fff; font-size: 14px; transform: rotateY(180deg); }
.cards .card.flipped .front { transform: rotateY(-180deg); }
.cards .card.flipped .back { transform: rotateY(0); }

/* ---------- Ô nhập ---------- */
.dock {
  position: sticky; bottom: 0; z-index: 7; /* trên nút ↓ (z-index 6) để menu "+" không bị nút ↓ đè */
  padding: 10px 0 calc(10px + var(--safe-b)); /* chừa vạch Home của iPhone */
  background: linear-gradient(to top, var(--bg) 72%, transparent);
}
.composer {
  background: var(--surface); border: 1px solid var(--line); border-radius: 26px;
  padding: 6px; box-shadow: var(--shadow); transition: border-color .15s, box-shadow .15s, background .15s;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); box-shadow: var(--shadow), var(--ring); }
.composer.drag { border-color: var(--accent); background: var(--accent-soft); }
.cbar { display: flex; align-items: flex-end; gap: 4px; }
.composer textarea {
  flex: 1; min-width: 0; border: 0; outline: 0; background: none; resize: none;
  font-size: 16px; line-height: 1.5; padding: 8px 4px; max-height: 200px;
}
.composer textarea::placeholder { color: var(--muted); }
.send, .plus {
  width: 40px; height: 40px; flex: none; display: grid; place-items: center; border: 0; border-radius: 50%;
  transition: background .15s, transform .1s, opacity .15s;
}
.send { background: var(--text); color: var(--bg); }
.send:not(:disabled):active { transform: scale(.94); }
.send:disabled { background: var(--surface-2); color: var(--muted); }
.send .ic { stroke-width: 2.4; }
.plus { background: none; color: var(--text); }
.plus .ic { width: 22px; height: 22px; transition: transform .2s; }
.plus:hover, .plus[aria-expanded="true"] { background: var(--surface-2); }
.plus[aria-expanded="true"] .ic { transform: rotate(45deg); }
.disclaimer { margin: 7px 0 0; text-align: center; font-size: 12px; color: var(--muted); }

/* Chip công cụ đang bật (vd Tạo đề ôn tập) */
.toolrow { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; padding: 6px 8px 2px; }
.toolchip {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 8px 0 10px;
  border: 0; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-size: 13.5px; font-weight: 600;
}
.toolchip .ic { width: 16px; height: 16px; }
.toolchip .x { width: 14px; height: 14px; opacity: .65; }
.toolchip:hover .x { opacity: 1; }
.toolhint { font-size: 12.5px; color: var(--muted); }

/* Menu của nút "+" – mở lên phía trên ô nhập */
.plus-wrap { position: relative; flex: none; }
.plus-menu {
  position: absolute; left: 0; bottom: calc(100% + 12px); z-index: 30; width: 260px; padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px;
  box-shadow: var(--shadow-pop); animation: popup .16s ease-out both;
}
.plus-menu button {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 10px 12px;
  border: 0; border-radius: 12px; background: none; font-size: 15px; text-align: left;
}
.plus-menu button:hover, .plus-menu button:focus-visible { background: var(--surface-2); outline: none; }
.plus-menu button > .ic:first-child { color: var(--muted); }
.plus-menu .on { margin-left: auto; color: var(--accent); stroke-width: 2.4; visibility: hidden; }
.plus-menu [aria-checked="true"] .on { visibility: visible; }
.plus-menu [aria-checked="true"] > .ic:first-child { color: var(--accent); }
.pm-sep { height: 1px; margin: 4px 8px; background: var(--line-soft); }
.pm-hint { margin: 0; padding: 6px 12px 4px; font-size: 12.5px; color: var(--muted); }
.pm-hint kbd { font: inherit; font-weight: 600; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); white-space: nowrap; }
.file-in { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* Ảnh / tệp đang chờ gửi, hiện ngay trong ô nhập */
.atts { display: flex; gap: 10px; padding: 8px 8px 6px; overflow-x: auto; scrollbar-width: none; }
.att { position: relative; flex: none; animation: rise .2s ease-out both; }
.att img { display: block; width: 64px; height: 64px; object-fit: cover; border-radius: 14px; border: 1px solid var(--line); }
.att.file { display: flex; align-items: center; gap: 10px; height: 64px; max-width: 230px; padding: 0 14px 0 10px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface-2); }
.att.file .fi { flex: none; width: 38px; height: 38px; display: grid; place-items: center; border-radius: 10px; background: var(--grad); color: #fff; }
.att.file .fi .ic { width: 19px; height: 19px; }
.att.file .meta { display: grid; min-width: 0; line-height: 1.3; }
.att.file b { font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.att.file small { font-size: 12px; color: var(--muted); }
.att .x {
  position: absolute; top: -7px; right: -7px; width: 22px; height: 22px; display: grid; place-items: center;
  border: 2px solid var(--surface); border-radius: 50%; background: var(--text); color: var(--bg); padding: 0;
}
.att .x .ic { width: 11px; height: 11px; stroke-width: 3.2; }

/* nút ↓ cuộn xuống */
.jump {
  position: fixed; left: 50%; z-index: 6; width: 38px; height: 38px; display: grid; place-items: center;
  border-radius: 50%; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transform: translate(-50%, 8px); transition: opacity .2s ease, transform .2s ease;
}
.jump .ic { width: 18px; height: 18px; }
.jump.show { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }

/* ---------- Markdown trong câu trả lời ---------- */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h2, .md h3, .md h4, .md h5 { margin: 20px 0 8px; line-height: 1.35; letter-spacing: -.01em; }
.md h2 { font-size: 20px; } .md h3 { font-size: 18px; } .md h4, .md h5 { font-size: 16px; }
.md p { margin: 10px 0; }
.md ul, .md ol { padding-left: 22px; margin: 10px 0; }
.md li { margin: 4px 0; }
.md li::marker { color: var(--muted); }
.md strong { font-weight: 650; }
.md code { background: var(--surface-2); padding: 2px 6px; border-radius: 6px; font-size: .9em; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.md pre { background: var(--surface-2); padding: 14px 16px; border-radius: 14px; overflow-x: auto; font-size: 14px; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 12px 0; padding: 8px 14px; border-left: 3px solid var(--accent); background: var(--accent-soft); border-radius: 0 12px 12px 0; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }
.md a { color: var(--accent); }
.tablewrap { overflow-x: auto; margin: 12px 0; border: 1px solid var(--line); border-radius: 14px; }
.md table { border-collapse: collapse; width: 100%; font-size: 14.5px; }
.md th, .md td { padding: 9px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
.md tr:last-child td { border-bottom: 0; }
.md th { background: var(--surface-2); font-weight: 600; }
.md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; margin: 12px 0; }
.md .katex { font-size: 1.08em; }

/* ---------- Thông báo & hộp thoại ---------- */
.toasts { position: fixed; top: calc(var(--appbar-h) + env(safe-area-inset-top, 0px) + 10px); left: 50%; transform: translateX(-50%); z-index: 50; display: grid; gap: 8px; width: max-content; max-width: calc(100vw - 32px); }
.toast {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px 10px 16px; border-radius: 14px;
  background: var(--text); color: var(--bg); font-size: 14px; box-shadow: var(--shadow); animation: rise .25s ease-out both;
}
.toast button { border: 0; border-radius: 9px; padding: 5px 10px; background: color-mix(in srgb, var(--bg) 18%, transparent); color: inherit; font-weight: 600; font-size: 13px; }
.toast.out { animation: fade .2s ease-in both; }

dialog.sheet {
  border: 0; border-radius: 24px; padding: 28px 24px 20px; width: min(380px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: var(--shadow); text-align: center;
}
dialog.sheet::backdrop { background: rgba(10, 10, 20, .45); backdrop-filter: blur(4px); }
dialog.sheet img { border-radius: 16px; }
dialog.sheet h2 { font-size: 19px; margin: 12px 0 8px; }
dialog.sheet ol { text-align: left; padding-left: 22px; margin: 0; line-height: 1.9; }
dialog.sheet .close { position: absolute; top: 10px; right: 10px; }

dialog.lightbox { padding: 0; border: 0; background: none; max-width: 94vw; max-height: 92vh; overflow: visible; }
dialog.lightbox::backdrop { background: rgba(5, 5, 12, .82); backdrop-filter: blur(6px); }
dialog.lightbox img { display: block; max-width: 94vw; max-height: 92vh; border-radius: 14px; cursor: zoom-out; }

/* ===================== Tài khoản ===================== */
.lead { display: flex; align-items: center; gap: 4px; margin-left: -6px; }
.avatar {
  display: inline-grid; place-items: center; width: 34px; height: 34px; margin-left: 4px; border: 0; border-radius: 50%;
  background: var(--grad); color: #fff; font-weight: 700; font-size: 15px; box-shadow: var(--shadow-sm);
}
button.avatar:hover { filter: brightness(1.08); }
.avatar.big { width: 48px; height: 48px; font-size: 20px; margin: 0; flex: none; }

/* Form chung */
.field { display: grid; gap: 6px; text-align: left; font-size: 14px; font-weight: 600; }
.field span { color: var(--muted); font-weight: 500; }
.field input, .field select {
  width: 100%; height: 46px; padding: 0 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font-size: 16px; font-weight: 400; transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
.primary, .secondary, .danger-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 46px; padding: 0 18px; border: 0; border-radius: 12px; font-weight: 600; font-size: 15px;
}
.primary { background: var(--grad); color: #fff; box-shadow: 0 8px 20px -10px color-mix(in srgb, var(--accent) 80%, transparent); }
.primary:disabled { opacity: .6; }
.secondary { background: var(--surface-2); color: var(--text); }
.secondary:hover { background: var(--bubble); }
.danger-btn { background: var(--bad-soft); color: var(--bad); }
.wide { width: 100%; }
.form-err { margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--bad-soft); color: var(--bad); font-size: 14px; text-align: left; }
.hint { margin: 0; color: var(--muted); font-size: 13.5px; text-align: left; }

/* Màn đăng nhập */
body.signed-out .appbar { border-bottom-color: transparent; }
.auth { display: grid; place-items: center; min-height: calc(100dvh - var(--appbar-h) - env(safe-area-inset-top, 0px)); padding: 16px 16px calc(32px + var(--safe-b)); }
.auth-card {
  display: grid; gap: 14px; width: min(400px, 100%); padding: 32px 26px 24px; border-radius: 24px;
  background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--line-soft); text-align: center; animation: rise .35s ease-out both;
}
.auth-card img { margin: 0 auto; border-radius: 16px; box-shadow: 0 12px 26px -12px color-mix(in srgb, var(--accent) 70%, transparent); }
.auth-card h1 { margin: 2px 0 0; font-size: 24px; letter-spacing: -.02em; }
.auth-sub { margin: -6px 0 4px; color: var(--muted); font-size: 14.5px; }
.auth-card .primary { margin-top: 4px; }
.auth-note { margin: 0; color: var(--muted); font-size: 12.5px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 13px; background: var(--surface-2); }
.seg button { height: 38px; border: 0; border-radius: 10px; background: none; color: var(--muted); font-weight: 600; font-size: 14.5px; }
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }

/* Ngăn kéo lịch sử */
.drawer {
  position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; width: min(320px, 86vw);
  display: flex; flex-direction: column; padding: calc(12px + env(safe-area-inset-top, 0px)) 10px calc(12px + var(--safe-b)) max(10px, env(safe-area-inset-left));
  background: var(--surface); border-right: 1px solid var(--line-soft); box-shadow: var(--shadow-pop); animation: slidein .22s ease-out both;
}
.scrim { position: fixed; inset: 0; z-index: 39; background: rgba(10, 10, 20, .35); animation: fadein .2s ease-out both; }
body.drawer-open { overflow: hidden; }
.drawer-head { display: flex; align-items: center; gap: 6px; }
.newbtn {
  flex: 1; display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); font-weight: 600; font-size: 14.5px;
}
.newbtn:hover { background: var(--surface-2); }
.drawer-label { margin: 18px 10px 6px; color: var(--muted); font-size: 12.5px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.chatlist { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chatlist li { display: flex; align-items: center; border-radius: 10px; }
.chatlist li:hover, .chatlist li.on { background: var(--surface-2); }
.chatlist li.on .chat-open span { font-weight: 600; }
.chat-open { flex: 1; min-width: 0; display: grid; gap: 1px; padding: 9px 10px; border: 0; background: none; text-align: left; }
.chat-open span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14.5px; }
.chat-open small { color: var(--muted); font-size: 12px; }
.chat-del { flex: none; display: grid; place-items: center; width: 34px; height: 34px; margin-right: 4px; border: 0; border-radius: 9px; background: none; color: var(--muted); opacity: 0; }
.chat-del .ic { width: 17px; height: 17px; }
.chatlist li:hover .chat-del, .chat-del:focus-visible { opacity: 1; }
.chat-del:hover { color: var(--bad); background: var(--bad-soft); }
.chatlist .empty { padding: 10px; color: var(--muted); font-size: 14px; }
@media (hover: none) { .chat-del { opacity: .7; } }

/* Hộp thoại tài khoản */
dialog.sheet.account { width: min(440px, calc(100vw - 24px)); max-height: calc(100dvh - 32px); overflow-y: auto; text-align: left; padding: 22px 20px 20px; }
.account[open] { display: grid; gap: 16px; }
.acc-head { display: flex; align-items: center; gap: 12px; padding-right: 36px; }
.acc-head div { display: grid; min-width: 0; }
.acc-head b { font-size: 17px; }
.acc-head small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.quota { display: grid; gap: 8px; padding: 14px; border-radius: 16px; background: var(--accent-soft); }
.quota-row { display: flex; justify-content: space-between; gap: 10px; font-size: 14.5px; }
.quota .progress { flex: none; background: color-mix(in srgb, var(--surface) 70%, transparent); }
.quota small { color: var(--muted); font-size: 12.5px; }
.profile, .pwform, .delform { display: grid; gap: 12px; }
.profile h3 { margin: 4px 0 -6px; font-size: 16px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.more { border-top: 1px solid var(--line-soft); padding-top: 12px; }
.more summary { cursor: pointer; font-weight: 600; font-size: 14.5px; color: var(--muted); }
.more[open] summary { margin-bottom: 12px; color: var(--text); }
.more.danger summary { color: var(--bad); }
#logout .ic { width: 18px; height: 18px; }

@keyframes slidein { from { transform: translateX(-100%); } to { transform: none; } }
@keyframes fadein { from { opacity: 0; } }

/* ---------- Hiệu ứng ---------- */
.msg.enter { animation: rise .3s ease-out both; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes fade { to { opacity: 0; transform: translateY(-6px); } }
@keyframes pulse { 0%, 100% { opacity: .35; transform: scale(.8); } 50% { opacity: 1; transform: scale(1); } }
@keyframes popup { from { opacity: 0; transform: translateY(6px) scale(.98); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

/* ---------- Điện thoại ---------- */
@media (max-width: 720px) {
  :root { --appbar-h: 56px; }
  .iconbtn.install span { display: none; }
  .intro { padding-top: 28px; }
  .msg.user { max-width: 88%; }
  .msg.assistant { grid-template-columns: minmax(0, 1fr); }
  .msg.assistant::before { display: none; }
  .msg.assistant .body, .msg.assistant .tools { grid-column: 1; }
  .md .sec { padding: 11px 13px 12px; }
}
/* App đã cài, mở toàn màn hình: không cần nút cài */
@media (display-mode: standalone) { #install { display: none !important; } }

/* Nút Google trên màn đăng nhập */
.gsi { display: grid; gap: 14px; }
.gbtn { display: flex; justify-content: center; min-height: 44px; }
.or { display: flex; align-items: center; gap: 12px; margin: 0; color: var(--muted); font-size: 13px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.auth-note a { color: inherit; }

/* Sơ đồ (Mermaid) & đồ thị hàm số trong câu trả lời */
.md .diagram { margin: 12px 0; padding: 14px; border: 1px solid var(--line-soft); border-radius: 16px; background: var(--surface); overflow-x: auto; text-align: center; }
.md .diagram svg { max-width: 100%; height: auto; }
.md .diagram[data-kind="graph"] svg { width: 100%; }
.md .dg-wait { margin: 0; padding: 18px 0; color: var(--muted); font-size: 14px; animation: pulse 1.4s ease-in-out infinite; }
.md .diagram.ok .dg-wait { display: none; }
.md .dg-title { margin: 0 0 6px; font-weight: 600; font-size: 15px; }
.md .dg-legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 16px; margin-top: 6px; font-size: 13.5px; color: var(--muted); }
.md .dg-legend i { display: inline-block; width: 14px; height: 3px; margin-right: 6px; border-radius: 2px; vertical-align: middle; }
.md .diagram.dg-error { text-align: left; }
.md .dg-note { margin: 0 0 8px; color: var(--muted); font-size: 13.5px; }

/* 👍/👎 */
.tool.rate { padding: 0 7px; }
.tool.rate.on { color: var(--accent); background: var(--accent-soft); }
.msg.assistant .reasons { grid-column: 2; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 6px; font-size: 13px; color: var(--muted); animation: rise .2s ease-out both; }
.msg.assistant .reasons button { height: 30px; padding: 0 11px; border: 1px solid var(--line); border-radius: 99px; background: var(--surface); font-size: 13px; }
.msg.assistant .reasons button:hover { border-color: var(--accent); color: var(--accent); }
@media (max-width: 720px) { .msg.assistant .reasons { grid-column: 1; } }
