/* =============================================================================
   AI APP — ai.hkietmail.top
   Layout: sidebar (lịch sử) + main (thread + composer)
   ============================================================================= */

body {
  overflow: hidden;
  height: 100vh;
}

.ai-shell {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

/* ===== SIDEBAR ===== */
.ai-sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--bg2);
  border-right: 2px solid var(--line);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.2s ease, transform 0.2s ease;
}
.ai-sidebar.collapsed {
  margin-left: -280px;
}

.ai-side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 2px solid var(--line);
  flex-shrink: 0;
}
.ai-side-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font: 800 0.95rem "Inter", system-ui, sans-serif;
  color: var(--grass);
  text-decoration: none;
  text-shadow: 0 0 8px rgba(74, 222, 128, 0.4);
}
.ai-side-logo img { height: 26px; width: auto; border-radius: 5px; }

.ai-icon-btn {
  background: transparent;
  border: 1.5px solid transparent;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
  padding: 5px 8px;
  border-radius: 6px;
  line-height: 1;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.ai-icon-btn:hover {
  background: var(--hover);
  color: var(--text);
  border-color: var(--line);
}

.ai-new-chat {
  margin: 12px 14px;
  padding: 10px 14px;
  background: var(--grass);
  color: #fff;
  border: 2px solid var(--grass-dk);
  border-radius: 8px;
  font: 800 0.85rem "Inter", system-ui, sans-serif;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 3px 0 var(--grass-deep);
  transition: transform 0.1s, box-shadow 0.1s;
}
.ai-new-chat:hover { transform: translateY(2px); box-shadow: 0 1px 0 var(--grass-deep); }
.ai-new-chat:active { transform: translateY(3px); box-shadow: none; }

.ai-side-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px 10px;
  scrollbar-width: thin;
}
.ai-side-list::-webkit-scrollbar { width: 5px; }
.ai-side-list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

.ai-side-title {
  font: 700 10px "Inter", system-ui, sans-serif;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1.2px;
  padding: 8px 6px;
}
.ai-side-empty {
  font: 500 12px "Inter", system-ui, sans-serif;
  color: var(--muted);
  padding: 6px;
  opacity: 0.75;
}

.ai-side-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  margin-bottom: 3px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--text);
  font: 500 12.5px/1.4 "Inter", system-ui, sans-serif;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s;
}
.ai-side-item:hover { background: var(--hover); border-color: var(--line); }
.ai-side-item.active {
  background: var(--selected);
  border-color: var(--grass-dk);
  color: var(--grass);
}

.ai-side-foot {
  padding: 10px 12px;
  border-top: 2px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex-shrink: 0;
}
.ai-side-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border-radius: 6px;
  color: var(--muted);
  font: 600 12.5px "Inter", system-ui, sans-serif;
  text-decoration: none;
  transition: background 0.12s, color 0.12s;
}
.ai-side-link:hover { background: var(--hover); color: var(--text); }

.ai-theme-btn {
  background: var(--bg3);
  border: 2px solid var(--line);
  color: var(--text);
  padding: 6px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  transition: border-color 0.12s;
}
.ai-theme-btn:hover { border-color: var(--grass); }

/* ===== MAIN ===== */
.ai-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.ai-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 2px solid var(--line);
  background: var(--bg);
  flex-shrink: 0;
}

.ai-model-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}
.ai-model-select {
  background: var(--bg3);
  color: var(--text);
  border: 2px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  font: 700 0.8rem "Inter", system-ui, sans-serif;
  cursor: pointer;
  outline: none;
  max-width: 280px;
}
.ai-model-select:focus { border-color: var(--grass); }
.ai-model-hint {
  font: 500 11px "Inter", system-ui, sans-serif;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-top-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.ai-quota-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  background: var(--bg3);
  border: 1.5px solid var(--line);
  border-radius: 99px;
  font: 600 10.5px "Inter", system-ui, sans-serif;
  color: var(--muted);
}
.ai-quota-bar {
  width: 54px;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}
.ai-quota-fill {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--grass);
  border-radius: 2px;
  transition: width 0.25s;
}
.ai-quota-fill.warn { background: var(--warn); }
.ai-quota-fill.danger { background: var(--danger); }

/* ===== THREAD ===== */
.ai-thread {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 8px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
}
.ai-thread::-webkit-scrollbar { width: 7px; }
.ai-thread::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }

.ai-thread-inner {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.ai-welcome {
  max-width: 860px;
  margin: 0 auto;
  text-align: center;
  padding: 30px 0 10px;
}
.ai-welcome-icon { font-size: 46px; line-height: 1; margin-bottom: 14px; }
.ai-welcome-title {
  font: 800 1.5rem/1.3 "Inter", system-ui, sans-serif;
  color: var(--text);
  margin: 0 0 8px;
}
.ai-welcome-sub {
  font: 500 0.9rem "Inter", system-ui, sans-serif;
  color: var(--muted);
  margin: 0 0 26px;
}

.ai-prompt-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 10px;
  text-align: left;
}
.ai-prompt-card {
  background: var(--bg2);
  border: 2px solid var(--line);
  border-radius: 10px;
  padding: 13px 15px;
  cursor: pointer;
  transition: border-color 0.14s, transform 0.14s, background 0.14s;
  box-shadow: 0 3px 0 var(--line);
}
.ai-prompt-card:hover {
  border-color: var(--grass);
  transform: translateY(-2px);
  box-shadow: 0 5px 0 var(--grass-deep);
}
.ai-prompt-card .ic { font-size: 19px; margin-bottom: 6px; }
.ai-prompt-card .tt {
  font: 700 0.82rem "Inter", system-ui, sans-serif;
  color: var(--text);
  margin-bottom: 3px;
}
.ai-prompt-card .dd {
  font: 500 0.75rem/1.45 "Inter", system-ui, sans-serif;
  color: var(--muted);
}

/* ===== MESSAGES ===== */
.ai-msg {
  display: flex;
  gap: 12px;
  max-width: 100%;
}
.ai-msg.user { flex-direction: row-reverse; }
.ai-msg-av {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  background: var(--bg2);
  border: 1.5px solid var(--line);
}
.ai-msg.user .ai-msg-av {
  background: linear-gradient(135deg, #4ade80, #16a34a);
  border-color: var(--grass-dk);
}
.ai-msg-body {
  min-width: 0;
  max-width: min(760px, 88%);
}
.ai-msg.user .ai-msg-body {
  display: flex;
  justify-content: flex-end;
}
.ai-bubble {
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--bg2);
  border: 1.5px solid var(--line);
  color: var(--text);
  font: 500 0.9rem/1.65 "Inter", system-ui, sans-serif;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.ai-msg.user .ai-bubble {
  background: rgba(74, 222, 128, 0.13);
  border-color: var(--grass-dk);
  border-bottom-right-radius: 4px;
}
.ai-msg.model .ai-bubble { border-bottom-left-radius: 4px; }

.ai-bubble code {
  font-family: "JetBrains Mono", monospace;
  font-size: 0.82rem;
  background: rgba(0, 0, 0, 0.3);
  padding: 1px 5px;
  border-radius: 4px;
}
html.light .ai-bubble code { background: rgba(0, 0, 0, 0.08); }
.ai-bubble pre {
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 10px 0;
}
html.light .ai-bubble pre { background: rgba(0, 0, 0, 0.06); }
.ai-bubble pre code { background: transparent; padding: 0; font-size: 0.8rem; line-height: 1.55; }
.ai-bubble a { color: var(--grass); text-decoration: underline; }
.ai-bubble strong { color: var(--text); font-weight: 800; }
.ai-bubble ul, .ai-bubble ol { margin: 8px 0; padding-left: 22px; }
.ai-bubble li { margin: 3px 0; }
.ai-bubble blockquote {
  margin: 8px 0;
  padding: 6px 12px;
  border-left: 3px solid var(--grass);
  background: rgba(74, 222, 128, 0.06);
  border-radius: 0 6px 6px 0;
}

.ai-msg-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 5px;
  font: 500 10.5px "Inter", system-ui, sans-serif;
  color: var(--muted);
}
.ai-msg-foot button {
  background: transparent;
  border: none;
  color: var(--muted);
  font: inherit;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
}
.ai-msg-foot button:hover { color: var(--grass); background: var(--hover); }

.ai-typing { display: flex; gap: 4px; padding: 6px 2px; }
.ai-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: aiBounce 1.2s infinite;
}
.ai-typing span:nth-child(2) { animation-delay: 0.15s; }
.ai-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes aiBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-5px); opacity: 1; }
}

.ai-error-bubble {
  background: rgba(229, 57, 53, 0.1);
  border-color: var(--danger);
  color: var(--danger);
}

/* ===== COMPOSER ===== */
.ai-composer-wrap {
  flex-shrink: 0;
  padding: 8px 16px 14px;
  background: linear-gradient(transparent, var(--bg) 26%);
}
.ai-composer {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg2);
  border: 2px solid var(--line);
  border-radius: 16px;
  padding: 8px 8px 8px 16px;
  box-shadow: 0 4px 0 var(--line);
  transition: border-color 0.15s;
}
.ai-composer:focus-within { border-color: var(--grass); }

.ai-input {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font: 500 0.92rem/1.55 "Inter", system-ui, sans-serif;
  resize: none;
  padding: 8px 0;
  max-height: 200px;
  min-height: 26px;
  box-shadow: none;
}
.ai-input:focus { border: none; box-shadow: none; }

.ai-send-btn {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 10px;
  background: linear-gradient(135deg, #4ade80, #16a34a);
  border: 2px solid var(--grass-dk);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, opacity 0.15s;
}
.ai-send-btn:hover { transform: translateY(-2px); }
.ai-send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.ai-composer-foot {
  max-width: 860px;
  margin: 8px auto 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font: 500 10.5px "Inter", system-ui, sans-serif;
  color: var(--muted);
}

/* ===== ACCESS GATE ===== */
.ai-gate {
  max-width: 520px;
  margin: 60px auto;
  padding: 34px 28px;
  background: var(--bg2);
  border: 2px dashed var(--amber);
  border-radius: 14px;
  text-align: center;
}
.ai-gate .ic { font-size: 42px; margin-bottom: 14px; }
.ai-gate h2 {
  font: 800 1.3rem "Inter", system-ui, sans-serif;
  color: var(--text);
  margin: 0 0 10px;
}
.ai-gate p {
  font: 500 0.9rem/1.65 "Inter", system-ui, sans-serif;
  color: var(--muted);
  margin: 0 0 22px;
}
.ai-gate .cta {
  display: inline-block;
  padding: 12px 26px;
  background: var(--grass);
  color: #fff;
  border: 2px solid var(--grass-dk);
  border-radius: 8px;
  font: 800 0.85rem "Inter", system-ui, sans-serif;
  text-decoration: none;
  box-shadow: 0 3px 0 var(--grass-deep);
}

/* ===== USER MENU trong sidebar AI =====
   theme.css scope dropdown vào .brand-header, nhưng trang AI dùng .ai-sidebar
   → phải khai báo lại các rule tương ứng ở đây. */
.ai-side-foot #userMenu { position: relative; display: inline-flex; align-items: center; width: 100%; }
.ai-side-foot .user-menu { position: relative; display: inline-flex; align-items: center; width: 100%; }
.ai-side-foot .user-trigger {
  display: inline-flex !important; align-items: center !important;
  gap: 8px !important; padding: 6px 10px !important; width: 100% !important;
  background: rgba(74, 222, 128, 0.12) !important;
  border: 1.5px solid var(--grass) !important;
  box-shadow: 0 3px 0 var(--grass-deep) !important;
  border-radius: 6px !important;
  color: var(--grass) !important;
  cursor: pointer; transition: all 0.1s;
  text-decoration: none; line-height: 1;
  font: 800 0.82rem "Inter", system-ui, sans-serif !important;
  white-space: nowrap;
}
.ai-side-foot .user-trigger:hover { transform: translateY(2px); box-shadow: 0 1px 0 var(--grass-deep) !important; }
.ai-side-foot .user-trigger:active { transform: translateY(3px); box-shadow: 0 0 0 var(--grass-deep) !important; }
.ai-side-foot .user-avatar {
  display: inline-flex !important; align-items: center !important; justify-content: center !important;
  width: 26px !important; height: 26px !important; border-radius: 50% !important;
  background: linear-gradient(135deg, #4ade80, #16a34a) !important;
  color: #fff !important; font: 800 12px "Inter", system-ui, sans-serif !important;
  text-transform: uppercase; flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(74, 222, 128, 0.5);
}
.ai-side-foot .user-name {
  font: 700 12px "Inter", system-ui, sans-serif !important;
  max-width: 110px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; color: var(--text); flex: 1; text-align: left;
}
.ai-side-foot .user-caret { font: 800 11px "Inter", system-ui, sans-serif !important; color: var(--muted); transition: transform 0.15s; }
.ai-side-foot .user-menu.open .user-caret { transform: rotate(180deg); }
.ai-side-foot .user-dropdown {
  display: none;
  position: absolute; left: 0; right: 0; bottom: calc(100% + 8px);
  min-width: 0; max-width: none;
  background: var(--bg); border: 1.5px solid var(--line);
  border-radius: 10px; padding: 6px 0;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  z-index: 200;
  transform-origin: bottom left;
  animation: aiUdFadeIn 0.18s ease-out;
}
.ai-side-foot .user-menu.open .user-dropdown { display: block; }
@keyframes aiUdFadeIn {
  from { opacity: 0; transform: scale(0.95) translateY(4px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
.ai-side-foot .ud-head { display: flex; gap: 10px; padding: 12px 14px; align-items: center; background: var(--bg); border-bottom: 1px solid var(--line); }
.ai-side-foot .ud-avatar {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 50%;
  background: linear-gradient(135deg, var(--grass), var(--grass-dk));
  color: #fff; font: 800 17px; text-transform: uppercase; flex-shrink: 0;
}
.ai-side-foot .ud-divider { height: 1px; background: var(--line); margin: 4px 0; }
.ai-side-foot .ud-item {
  display: block; width: 100%; text-align: left; padding: 9px 14px;
  font: 600 13px "Inter", system-ui, sans-serif; color: var(--text);
  background: transparent; border: 0; cursor: pointer; text-decoration: none;
  transition: background 0.12s; font-family: inherit;
}
.ai-side-foot .ud-item:hover { background: var(--hover); }
.ai-side-foot .ud-danger { color: var(--danger); font-weight: 700; }
.ai-side-foot .ud-danger:hover { background: rgba(229, 57, 53, 0.15) !important; }

/* ===== MOBILE ===== */
@media (max-width: 820px) {
  .ai-sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 900;
    margin-left: -280px;
    box-shadow: 6px 0 24px rgba(0, 0, 0, 0.4);
  }
  .ai-sidebar.open { margin-left: 0; }
  .ai-only-mobile { display: inline-flex !important; }
  .ai-thread { padding: 16px 12px 4px; }
  .ai-composer-wrap { padding: 6px 12px 10px; }
  .ai-model-select { max-width: 150px; font-size: 0.72rem; }
  .ai-model-hint { display: none; }
  .ai-quota-chip { padding: 4px 8px; }
  .ai-quota-bar { width: 34px; }
  .ai-msg-body { max-width: 92%; }
}
@media (min-width: 821px) {
  .ai-only-mobile { display: none !important; }
}
