/* =========================================================================
   App Server Chat — giao diện responsive
   Mốc thiết kế: máy tính (≥ 900px) và iPhone 11 (414 × 896 CSS px).
   ========================================================================= */

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1d2330;
  --muted: #6b7385;
  --border: #e1e4ea;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --accent-text: #ffffff;
  --user-bg: #2563eb;
  --user-text: #ffffff;
  --bot-bg: #ffffff;
  --row-alt: #f8f9fb;
  --row-hover: #eef3ff;
  --th-bg: #f1f3f7;
  --code-bg: #0f172a;
  --code-text: #e2e8f0;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --ok: #16a34a;
  --bad: #dc2626;
  --shadow: rgba(15, 23, 42, .18);

  --radius: 14px;
  --gutter: 16px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --panel: #161b22;
    --text: #e6edf3;
    --muted: #8b949e;
    --border: #30363d;
    --accent: #3b82f6;
    --accent-soft: #1c2a44;
    --bot-bg: #161b22;
    --row-alt: #1b2129;
    --row-hover: #1f2b3d;
    --th-bg: #21262d;
    --code-bg: #010409;
    --error-bg: #2d1416;
    --error-text: #ff7b72;
    --shadow: rgba(0, 0, 0, .55);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* ---- Khung chính ------------------------------------------------------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;          /* trình duyệt cũ */
  height: 100dvh;         /* tránh bị thanh địa chỉ Safari che */
  max-width: 1200px;
  margin: 0 auto;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) calc(var(--gutter) + var(--safe-right)) 10px calc(var(--gutter) + var(--safe-left));
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { font-size: 17px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--bad); }
.actions { display: flex; gap: 8px; flex: none; }

/* ---- Nút ---------------------------------------------------------------- */
button {
  font: inherit;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 10px;
  padding: 8px 16px;
  min-height: 40px;
  cursor: pointer;
  touch-action: manipulation;
}
button.ghost { background: transparent; color: var(--text); border-color: var(--border); padding: 6px 12px; }
button:disabled { opacity: .5; cursor: default; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.chip {
  font-size: 13px;
  min-height: 32px;
  padding: 4px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
}
.chip:hover { border-color: var(--accent); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.seg { display: inline-flex; }
.seg .chip { border-radius: 0; }
.seg .chip:first-child { border-radius: 8px 0 0 8px; }
.seg .chip:last-child { border-radius: 0 8px 8px 0; margin-left: -1px; }

/* ---- Danh sách tin nhắn ------------------------------------------------- */
.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: var(--gutter) calc(var(--gutter) + var(--safe-right)) var(--gutter) calc(var(--gutter) + var(--safe-left));
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: relative;
}

.msg { display: flex; flex-direction: column; max-width: min(760px, 85%); min-width: 0; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.bot { align-self: flex-start; }
.msg.wide { max-width: 100%; width: 100%; }   /* bảng & mã chiếm hết bề ngang */

.bubble {
  position: relative;
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border: 1px solid var(--border);
  background: var(--bot-bg);
  max-width: 100%;
  min-width: 0;
}
.msg.user .bubble { background: var(--user-bg); color: var(--user-text); border-color: transparent; border-bottom-right-radius: 4px; }
.msg.bot .bubble { border-bottom-left-radius: 4px; }
.msg.wide .bubble { white-space: normal; padding: 0; overflow: hidden; }
.msg.error .bubble { background: var(--error-bg); color: var(--error-text); --fade: var(--error-bg); }
.bubble.flush { padding: 0; white-space: normal; overflow: hidden; }
.meta { font-size: 12px; color: var(--muted); margin-top: 4px; padding: 0 4px; }

.typing .bubble { color: var(--muted); font-style: italic; }

/* ---- Thanh công cụ của khối kết quả ------------------------------------ */
.block-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 12px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.block-title { font-weight: 600; min-width: 0; }
.block-sub { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 6px; white-space: nowrap; }
.tools { display: flex; flex-wrap: wrap; gap: 6px; }

/* ---- Nội dung dài: thu gọn / mở rộng ------------------------------------ */
.collapsible { position: relative; }
.collapsible.collapsed { max-height: 22em; overflow: hidden; }
.collapsible.collapsed::after {
  content: "";
  position: absolute;
  inset: auto 0 0 0;
  height: 4em;
  background: linear-gradient(to bottom, transparent, var(--fade, var(--bot-bg)));
  pointer-events: none;
}
.more {
  display: block;
  width: 100%;
  min-height: 36px;
  border: 0;
  border-top: 1px solid var(--border);
  border-radius: 0;
  background: transparent;
  color: var(--accent);
  font-size: 14px;
}
.text-body { padding: 10px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---- Khối mã ------------------------------------------------------------ */
.code-wrap { --fade: var(--code-bg); background: var(--code-bg); }
.code-wrap pre {
  margin: 0;
  color: var(--code-text);
  padding: 12px 14px;
  overflow-x: auto;
  font: 13px/1.5 var(--mono);
  tab-size: 4;
  white-space: pre;
}
.code-wrap.wrap-lines pre { white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---- Bảng --------------------------------------------------------------- */
.table-scroll {
  overflow: auto;
  max-height: min(65vh, 640px);
  -webkit-overflow-scrolling: touch;
  /* Bóng mờ ở mép báo hiệu còn cột/dòng bị khuất */
  background:
    linear-gradient(to right, var(--bot-bg) 30%, transparent) left / 24px 100% no-repeat local,
    linear-gradient(to left, var(--bot-bg) 30%, transparent) right / 24px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, var(--shadow), transparent) left / 10px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, var(--shadow), transparent) right / 10px 100% no-repeat scroll;
}
.table-scroll.expanded { max-height: none; }

table.data {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content;
  min-width: 100%;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.data th, .data td {
  padding: 7px 12px;
  text-align: left;
  vertical-align: top;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  max-width: 28em;
  overflow: hidden;
  text-overflow: ellipsis;
}
.data td.long { white-space: normal; min-width: 14em; overflow-wrap: anywhere; }
.data th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--th-bg);
  font-weight: 600;
  font-size: 13px;
  user-select: none;
  cursor: pointer;
}
.data th .arrow { color: var(--muted); font-size: 11px; margin-left: 4px; }
.data th[aria-sort="ascending"] .arrow::after { content: "▲"; color: var(--accent); }
.data th[aria-sort="descending"] .arrow::after { content: "▼"; color: var(--accent); }
.data th:not([aria-sort]) .arrow::after { content: "↕"; }
.data tbody tr:nth-child(even) td { background: var(--row-alt); }
.data tbody tr:nth-child(odd) td { background: var(--bot-bg); }
.data tbody tr:hover td { background: var(--row-hover); }
.data .num { text-align: right; }

/* Cột đầu cố định khi cuộn ngang (chỉ khi bảng có nhiều cột) */
.data.sticky-first th:first-child,
.data.sticky-first td:first-child {
  position: sticky;
  left: 0;
  z-index: 1;
  box-shadow: 1px 0 0 var(--border);
}
.data.sticky-first th:first-child { z-index: 3; }

/* ---- Chế độ thẻ (mỗi dòng một thẻ) — dùng cho điện thoại --------------- */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 10px;
  padding: 10px;
  max-height: min(65vh, 640px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.cards.expanded { max-height: none; }
.card {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  overflow: hidden;
}
.card-head {
  padding: 8px 12px;
  background: var(--th-bg);
  font-weight: 600;
  overflow-wrap: anywhere;
}
.card dl {
  display: grid;
  grid-template-columns: minmax(7em, 40%) 1fr;
  margin: 0;
  font-size: 14px;
}
.card dt, .card dd {
  margin: 0;
  padding: 5px 12px;
  border-top: 1px solid var(--border);
  overflow-wrap: anywhere;
}
.card dt { color: var(--muted); }
.card dd { font-variant-numeric: tabular-nums; }

.empty { padding: 14px; color: var(--muted); }

/* ---- Ô nhập ------------------------------------------------------------- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px calc(var(--gutter) + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(var(--gutter) + var(--safe-left));
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  font: inherit;
  font-size: 16px;          /* ≥16px: Safari iOS không tự phóng to khi gõ */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  min-height: 44px;
  max-height: 160px;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.composer button { min-height: 44px; }

/* ---- Gợi ý lệnh (hiện phía trên ô nhập) -------------------------------- */
.composer { position: relative; }
.suggest {
  position: absolute;
  left: calc(var(--gutter) + var(--safe-left));
  right: calc(var(--gutter) + var(--safe-right));
  bottom: calc(100% + 6px);
  max-height: min(50vh, 380px);
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 24px var(--shadow);
  z-index: 5;
}
.suggest[hidden] { display: none; }
.suggest li {
  display: grid;
  grid-template-columns: minmax(9em, auto) 1fr;
  align-items: baseline;
  gap: 2px 14px;
  padding: 8px 10px;
  min-height: 44px;
  border-radius: 8px;
  cursor: pointer;
}
.suggest li[aria-selected="true"] { background: var(--accent-soft); }
.suggest li:hover:not(.hint):not(.empty-row) { background: var(--row-hover); }
.s-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; min-width: 0; }
.s-name { font: 600 14px/1.4 var(--mono); color: var(--accent); white-space: nowrap; }
.s-name mark { background: transparent; color: var(--text); }
.s-alias { font: 12px/1.4 var(--mono); color: var(--muted); }
.s-desc { font-size: 13px; color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggest li.hint, .suggest li.empty-row { cursor: default; display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: 13px; }
.suggest .hint code { font: 13px/1.4 var(--mono); color: var(--text); overflow-wrap: anywhere; }
.s-label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }

@media (max-width: 600px) {
  .suggest { max-height: 38vh; }       /* chừa chỗ cho bàn phím ảo */
  .suggest li { grid-template-columns: 1fr; gap: 0; padding: 7px 10px; }
  .s-desc { white-space: normal; }
}

/* ---- Thông báo nhỏ ------------------------------------------------------ */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  transform: translateX(-50%) translateY(10px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- Điện thoại (iPhone 11 = 414px; áp dụng tới 600px) ------------------ */
@media (max-width: 600px) {
  :root { --gutter: 10px; --radius: 12px; }
  body { font-size: 15px; }
  .brand h1 { font-size: 16px; }
  .actions button { padding: 4px 10px; font-size: 13px; min-height: 36px; }
  .messages { gap: 12px; }
  .msg { max-width: 92%; }
  .msg.wide { max-width: 100%; }
  .block-head { padding: 8px 10px; }
  .block-title { font-size: 14px; }
  .data th, .data td { padding: 6px 9px; max-width: 16em; }
  .data { font-size: 13px; }
  .table-scroll, .cards { max-height: 60vh; }
  .cards { grid-template-columns: 1fr; padding: 8px; gap: 8px; }
  .code-wrap pre { font-size: 12px; padding: 10px; }
  .collapsible.collapsed { max-height: 18em; }
}

/* Điện thoại xoay ngang: màn hình thấp, giảm chiều cao vùng cuộn */
@media (max-height: 500px) {
  .table-scroll, .cards { max-height: 75vh; }
  .topbar { padding-top: calc(6px + var(--safe-top)); padding-bottom: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}
