/* ==========================================================================
   AIC SYSTEM V2.1 — T-W5 MCA-05 AI UI & Session
   Brand tokens theo Mục 3 (Nhận diện thương hiệu chính thức) — Decision Lock,
   KHÔNG đổi màu tuỳ hứng.
   ========================================================================== */

:root {
  --color-primary: #004C6B;
  --color-secondary: #22E7B4;
  --color-accent: #FFCD92;
  --color-bg: #F4F8FA;
  --color-text: #173042;
  --color-muted: #60717D;
  --color-line: #D7E4E8;

  --color-surface: #FFFFFF;
  --color-user-bubble: var(--color-primary);
  --color-user-bubble-text: #FFFFFF;
  --color-ai-bubble: #FFFFFF;
  --color-ai-bubble-text: var(--color-text);

  --shell-max-width: 430px;
  --shell-min-width: 360px;

  --sheet-opened-height: 40vh;   /* trong dải 36-42% theo Mục 9/5.1 */
  --sheet-expanded-height: 90vh; /* tối đa ~90% theo Mục 5.1 */

  --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);

  --radius-sheet: 20px;
  --radius-bubble: 16px;
  --transition-sheet: height 320ms cubic-bezier(0.22, 1, 0.36, 1), transform 320ms cubic-bezier(0.22, 1, 0.36, 1);

  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: #0E1E27; /* nền trung tính ngoài shell trên màn rộng — Mục 4 */
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button, textarea, input {
  font-family: inherit;
}

/* Focus visibility rõ ràng cho accessibility (Mục 4 Accessibility) */
button:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--color-secondary);
  outline-offset: 2px;
}

/* --------------------------------------------------------------------------
   MOBILE SHELL — mobile-only, centered trên màn rộng, KHÔNG có layout desktop
   riêng (Mục 4, DL-MOBILE-01).
   -------------------------------------------------------------------------- */
.mobile-shell {
  position: relative;
  max-width: var(--shell-max-width);
  min-width: min(100vw, var(--shell-min-width));
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--color-bg);
  overflow-x: hidden;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  display: flex;
  flex-direction: column;
}

/* --------------------------------------------------------------------------
   HEADER
   -------------------------------------------------------------------------- */
.app-header {
  display: flex;
  align-items: center;
  padding: 14px 16px 6px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.02em;
}

.brand-name {
  font-weight: 700;
  font-size: clamp(15px, 4vw, 17px);
  letter-spacing: 0.04em;
  color: var(--color-primary);
}

/* --------------------------------------------------------------------------
   LANDING — first viewport tối giản (Mục 4.1)
   -------------------------------------------------------------------------- */
.landing-main {
  padding: 8px 20px 24px;
  flex: 1;
}

.video-frame {
  width: 100%;
  aspect-ratio: 9 / 16;
  max-height: 62vh;
  border-radius: 18px;
  overflow: hidden;
  background: linear-gradient(160deg, #04384f 0%, var(--color-primary) 55%, #0a5875 100%);
  position: relative;
  margin: 10px 0 18px;
  box-shadow: 0 18px 40px -20px rgba(0, 76, 107, 0.55);
}

.video-poster {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  color: #fff;
  padding: 24px;
  text-align: center;
}

.play-button {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  border: none;
  background: var(--color-secondary);
  color: #04303f;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 8px 24px -8px rgba(34, 231, 180, 0.65);
  transition: transform 160ms ease;
}
.play-button:active { transform: scale(0.94); }
.play-button svg { margin-left: 3px; }

.video-hint {
  margin: 0;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.86);
}

.video-progress {
  width: 70%;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.25);
  overflow: hidden;
}
.video-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--color-secondary);
  transition: width 120ms linear;
}

.headline {
  font-size: clamp(20px, 5.4vw, 24px);
  line-height: 1.32;
  font-weight: 700;
  color: var(--color-primary);
  margin: 0 0 8px;
}

.subheadline {
  font-size: clamp(14px, 3.6vw, 15px);
  line-height: 1.5;
  color: var(--color-muted);
  margin: 0;
  max-width: 34em;
}

.app-footer {
  padding: 10px 20px calc(16px + var(--safe-bottom));
  font-size: 12px;
  color: var(--color-muted);
  text-align: center;
}

/* --------------------------------------------------------------------------
   REOPEN CTA — Mục 5.1 / Mục 9 CLOSED_REOPEN
   -------------------------------------------------------------------------- */
.reopen-cta {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px;
  min-height: 44px;
  border: none;
  border-radius: 999px;
  background: var(--color-primary);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 12px 28px -12px rgba(0, 76, 107, 0.6);
  z-index: 40;
  max-width: calc(var(--shell-max-width) - 40px);
}

.reopen-cta-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-secondary);
  flex: none;
}

/* --------------------------------------------------------------------------
   OVERLAY
   -------------------------------------------------------------------------- */
.ai-overlay {
  position: fixed;
  inset: 0;
  background: rgba(9, 26, 34, 0.32);
  z-index: 45;
  opacity: 0;
  transition: opacity 280ms ease;
  pointer-events: none;
}
.ai-overlay.is-visible {
  opacity: 1;
  pointer-events: auto;
}

/* --------------------------------------------------------------------------
   AI BOTTOM SHEET — Mục 9 Mobile AI Window UI/UX
   -------------------------------------------------------------------------- */
.ai-sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%) translateY(100%);
  width: 100%;
  max-width: var(--shell-max-width);
  min-width: min(100vw, var(--shell-min-width));
  height: var(--sheet-opened-height);
  background: var(--color-bg);
  border-radius: var(--radius-sheet) var(--radius-sheet) 0 0;
  box-shadow: 0 -18px 44px -22px rgba(0, 22, 33, 0.45);
  z-index: 50;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: var(--transition-sheet), transform 320ms cubic-bezier(0.22, 1, 0.36, 1);
  touch-action: none;
}

.ai-sheet[data-visible="true"] {
  transform: translateX(-50%) translateY(0);
}

.ai-sheet[data-state="EXPANDED"] {
  height: var(--sheet-expanded-height);
}

.ai-sheet.is-dragging {
  transition: none;
}

.sheet-handle-zone {
  flex: none;
  padding: 10px 14px 8px;
  cursor: grab;
  background: var(--color-bg);
}

.sheet-handle-bar {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--color-line);
  margin: 0 auto 10px;
}

.sheet-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar-chip {
  position: relative;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(150deg, var(--color-primary), #0a6c8f);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  flex: none;
}

.avatar-chip-pulse {
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid var(--color-secondary);
  opacity: 0.55;
  animation: avatarPulse 2.6s ease-out infinite;
}

@keyframes avatarPulse {
  0% { transform: scale(0.88); opacity: 0.55; }
  70% { transform: scale(1.18); opacity: 0; }
  100% { opacity: 0; }
}

.sheet-title {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
  flex: 1;
  min-width: 0;
}
.sheet-title strong {
  font-size: 14px;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tier-badge {
  font-size: 11px;
  color: var(--color-muted);
  letter-spacing: 0.03em;
}

.icon-btn {
  width: 40px;
  height: 40px;
  min-width: 40px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--color-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--color-line); }

/* --------------------------------------------------------------------------
   TRANSCRIPT + BUBBLES
   -------------------------------------------------------------------------- */
.transcript {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 16px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  touch-action: pan-y;
}

.bubble-row {
  display: flex;
  width: 100%;
}
.bubble-row.role-user { justify-content: flex-end; }
.bubble-row.role-assistant { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--radius-bubble);
  font-size: 14.5px;
  line-height: 1.48;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble-row.role-user .bubble {
  background: var(--color-user-bubble);
  color: var(--color-user-bubble-text);
  border-bottom-right-radius: 4px;
}

.bubble-row.role-assistant .bubble {
  background: var(--color-ai-bubble);
  color: var(--color-ai-bubble-text);
  border: 1px solid var(--color-line);
  border-bottom-left-radius: 4px;
}

.bubble-row.role-system .bubble {
  background: transparent;
  border: none;
  color: var(--color-muted);
  font-size: 12.5px;
  text-align: center;
  margin: 0 auto;
}

/* Typing indicator — hiệu ứng gõ chữ khi AI đang xử lý (state SENDING) */
.typing-bubble {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 12px 14px;
}
.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-muted);
  animation: typingBounce 1.1s infinite ease-in-out;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* --------------------------------------------------------------------------
   STATUS LINE — hệ thống status ngoài bubble (Mục 9 ERROR_RETRYABLE / OFFLINE_TIMEOUT)
   -------------------------------------------------------------------------- */
.status-line {
  margin: 0;
  padding: 8px 16px;
  font-size: 12.5px;
  text-align: center;
  color: var(--color-muted);
  background: var(--color-bg);
  border-top: 1px solid var(--color-line);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex: none;
}
.status-line.is-error { color: #B3261E; }

.status-retry-btn {
  border: 1px solid currentColor;
  background: transparent;
  color: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 30px;
}

/* --------------------------------------------------------------------------
   COMPOSER — KHÔNG auto-focus, KHÔNG tự bật bàn phím khi OPENED (Mục 5.1)
   -------------------------------------------------------------------------- */
.composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: var(--color-surface);
  border-top: 1px solid var(--color-line);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 96px;
  min-height: 44px;
  padding: 11px 14px;
  border-radius: 14px;
  border: 1px solid var(--color-line);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 15px;
  line-height: 1.35;
}

.composer textarea:disabled {
  opacity: 0.6;
}

.send-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 50%;
  border: none;
  background: var(--color-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: none;
  transition: opacity 140ms ease, transform 140ms ease;
}
.send-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.send-btn:not(:disabled):active { transform: scale(0.92); }

/* --------------------------------------------------------------------------
   QA PANEL — chỉ Preview, gọn nhẹ, không cản trải nghiệm chính
   -------------------------------------------------------------------------- */
.qa-panel {
  position: fixed;
  right: max(10px, var(--safe-right));
  bottom: max(10px, var(--safe-bottom));
  z-index: 60;
  font-size: 12px;
}
.qa-toggle {
  min-height: 32px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--color-line);
  background: rgba(255, 255, 255, 0.92);
  color: var(--color-muted);
  cursor: pointer;
}
.qa-body {
  margin-top: 6px;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid var(--color-line);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 220px;
  box-shadow: 0 10px 24px -12px rgba(0,0,0,0.25);
}
.qa-note {
  margin: 0 0 4px;
  color: var(--color-muted);
  font-size: 11px;
}
.qa-body button {
  min-height: 30px;
  border-radius: 8px;
  border: 1px solid var(--color-line);
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  padding: 0 8px;
}

/* --------------------------------------------------------------------------
   ACCESSIBILITY: reduced motion (Mục 4 / Mục 3.2)
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .ai-sheet,
  .ai-overlay,
  .send-btn,
  .video-progress-bar {
    transition: none !important;
  }
  .avatar-chip-pulse,
  .typing-dot {
    animation: none !important;
  }
}

/* Màn hình rất hẹp (< 360px hiếm gặp) vẫn giữ dùng được */
@media (max-width: 359px) {
  :root { font-size: 15px; }
}
