/* NCD Plus SU brand palette (teal) */
:root {
  --brand-950: #042f2e;
  --brand-900: #134e4a;
  --brand-800: #115e59;
  --brand-700: #0f766e;
  --brand-600: #0d9488;
  --brand-500: #14b8a6;
  --brand-400: #2dd4bf;
  --brand-100: #ccfbf1;
  --brand-50: #f0fdfa;
  --radius: 16px;
  --radius-sm: 10px;
  --app-max: 720px;
  --pad-x: 1rem;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font-sans: "Kanit", "Noto Sans Thai", "Sarabun", sans-serif;
  --composer-lines: 4;
}

/* Light mode (default) */
:root,
[data-theme="light"] {
  --bg: var(--brand-50);
  --bg-elevated: #ffffff;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-solid: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --accent: var(--brand-600);
  --accent-deep: var(--brand-700);
  --accent-soft: rgba(13, 148, 136, 0.12);
  --accent-border: rgba(13, 148, 136, 0.35);
  --user-bubble: linear-gradient(135deg, var(--brand-700), var(--brand-500));
  --user-shadow: 0 4px 14px rgba(15, 118, 110, 0.28);
  --danger: #dc2626;
  --border: rgba(15, 118, 110, 0.12);
  --header-bg: rgba(255, 255, 255, 0.94);
  --composer-bg: rgba(255, 255, 255, 0.96);
  --app-bg: rgba(255, 255, 255, 0.82);
  --page-grad-1: rgba(20, 184, 166, 0.14);
  --page-grad-2: rgba(13, 148, 136, 0.08);
  --hover-bg: rgba(13, 148, 136, 0.06);
  --error-bg: rgba(220, 38, 38, 0.08);
  --error-border: rgba(220, 38, 38, 0.35);
  --error-text: #b91c1c;
  --shadow-app: 0 20px 50px rgba(15, 118, 110, 0.12), 0 0 0 1px rgba(13, 148, 136, 0.08);
  --theme-meta: #f0fdfa;
}

[data-theme="dark"] {
  --bg: var(--brand-950);
  --bg-elevated: #0a2e2a;
  --surface: rgba(19, 78, 74, 0.88);
  --surface-solid: var(--brand-900);
  --text: #f0fdfa;
  --muted: #94a3b8;
  --accent: var(--brand-400);
  --accent-deep: var(--brand-500);
  --accent-soft: rgba(45, 212, 191, 0.16);
  --accent-border: rgba(45, 212, 191, 0.38);
  --user-bubble: linear-gradient(135deg, var(--brand-800), var(--brand-500));
  --user-shadow: 0 4px 16px rgba(20, 184, 166, 0.3);
  --danger: #f87171;
  --border: rgba(148, 163, 184, 0.14);
  --header-bg: rgba(4, 47, 46, 0.94);
  --composer-bg: rgba(4, 47, 46, 0.96);
  --app-bg: rgba(4, 47, 46, 0.72);
  --page-grad-1: rgba(45, 212, 191, 0.12);
  --page-grad-2: rgba(13, 148, 136, 0.1);
  --hover-bg: rgba(148, 163, 184, 0.08);
  --error-bg: rgba(248, 113, 113, 0.08);
  --error-border: rgba(248, 113, 113, 0.4);
  --error-text: #fecaca;
  --shadow-app: 0 24px 60px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(45, 212, 191, 0.1);
  --theme-meta: #042f2e;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
  font-family: var(--font-sans);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  touch-action: manipulation;
  -ms-touch-action: manipulation;
  color-scheme: light;
}

button,
input,
textarea,
select {
  font-family: inherit;
  font-feature-settings: "liga" 1;
}

html[data-theme="dark"] {
  color-scheme: dark;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  min-height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  -ms-touch-action: manipulation;
  transition: background-color 0.2s ease, color 0.2s ease;
}

.app,
.composer textarea,
.topic-btn,
.btn-send,
.btn-theme,
.chip {
  touch-action: manipulation;
  -ms-touch-action: manipulation;
}

.page-bg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(ellipse 70% 45% at 50% -15%, var(--page-grad-1), transparent),
    radial-gradient(ellipse 50% 35% at 100% 60%, var(--page-grad-2), transparent),
    var(--bg);
  transition: background 0.2s ease;
}

.app-shell {
  height: 100dvh;
  height: 100vh;
  padding: 0;
  display: flex;
  justify-content: center;
}

.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: var(--app-max);
  height: 100%;
  min-height: 0;
  background: var(--app-bg);
  transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

/* Header */
.header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding: calc(0.55rem + var(--safe-t)) max(var(--pad-x), var(--safe-l)) 0.55rem max(var(--pad-x), var(--safe-r));
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
  flex: 1;
}

.header-logo {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--accent-border);
  background: #fff;
  box-shadow: 0 2px 10px rgba(15, 118, 110, 0.18);
}

[data-theme="dark"] .header-logo {
  background: var(--brand-900);
}

.header-text {
  min-width: 0;
  flex: 1;
}

.header-text h1 {
  font-family: var(--font-sans);
  font-size: clamp(0.9rem, 3.5vw, 1.05rem);
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.25;
  overflow-wrap: anywhere;
}

.header-text p {
  font-size: clamp(0.65rem, 2.8vw, 0.72rem);
  color: var(--muted);
  margin-top: 0.12rem;
  line-height: 1.3;
}

.btn-theme {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-solid);
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-theme:hover {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent-border);
}

.btn-theme svg {
  width: 18px;
  height: 18px;
}

[data-theme="light"] .btn-theme .icon-sun {
  display: none;
}

[data-theme="light"] .btn-theme .icon-moon {
  display: block;
}

[data-theme="dark"] .btn-theme .icon-moon {
  display: none;
}

[data-theme="dark"] .btn-theme .icon-sun {
  display: block;
}

.status-dot {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.68rem;
  color: var(--muted);
  max-width: 32%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-dot::before {
  content: "";
  flex-shrink: 0;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
}

.status-dot.online::before {
  background: #34d399;
  box-shadow: 0 0 8px rgba(52, 211, 153, 0.5);
}

.status-dot.offline::before {
  background: var(--danger);
}

/* Toolbar */
.toolbar {
  flex-shrink: 0;
  padding: 0.5rem max(var(--pad-x), var(--safe-l)) 0.5rem max(var(--pad-x), var(--safe-r));
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.topic-group {
  display: flex;
  width: 100%;
  gap: 0.3rem;
  padding: 0.28rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-solid);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.topic-btn {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0.45rem 0.35rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-family: var(--font-sans);
  font-size: clamp(0.8rem, 2.8vw, 0.875rem);
  font-weight: 500;
  line-height: 1.25;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.topic-btn:hover:not(:disabled):not(.active) {
  background: var(--hover-bg);
  color: var(--text);
}

.topic-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
  box-shadow: 0 1px 0 var(--accent-border);
}

.topic-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Messages */
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.85rem max(var(--pad-x), var(--safe-l)) 0.85rem max(var(--pad-x), var(--safe-r));
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
  -ms-touch-action: pan-y;
}

.welcome {
  text-align: center;
  padding: clamp(1rem, 5vw, 2rem) 0.5rem;
  color: var(--muted);
  margin: auto 0;
}

.welcome-logo {
  width: clamp(56px, 14vw, 72px);
  height: clamp(56px, 14vw, 72px);
  border-radius: 50%;
  object-fit: cover;
  margin: 0 auto 0.75rem;
  display: block;
  border: 3px solid var(--accent-border);
  background: #fff;
  box-shadow: 0 6px 20px rgba(15, 118, 110, 0.2);
}

[data-theme="dark"] .welcome-logo {
  background: var(--brand-900);
}

.welcome h2 {
  font-family: var(--font-sans);
  font-size: clamp(1rem, 4.5vw, 1.15rem);
  color: var(--text);
  font-weight: 500;
  margin-bottom: 0.35rem;
}

.welcome p {
  font-family: var(--font-sans);
  font-size: clamp(0.8125rem, 3.2vw, 0.875rem);
  font-weight: 400;
  line-height: 1.55;
  max-width: 36ch;
  margin: 0 auto;
  padding: 0 0.25rem;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem;
  margin-top: clamp(0.85rem, 3vw, 1.25rem);
  padding: 0 0.15rem;
}

.chip {
  padding: 0.5rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-solid);
  color: var(--accent-deep);
  font-family: var(--font-sans);
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.45;
  letter-spacing: 0.01em;
  cursor: pointer;
  text-align: center;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  max-width: 100%;
  -webkit-font-smoothing: antialiased;
}

.chip:hover {
  background: var(--accent-soft);
  border-color: var(--accent-border);
}

.chip:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.msg-row {
  display: flex;
  width: 100%;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.assistant {
  justify-content: flex-start;
  align-items: flex-start;
  gap: 0.5rem;
}

.msg-avatar {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--accent-border);
  background: #fff;
  box-shadow: 0 2px 8px rgba(15, 118, 110, 0.15);
  margin-top: 0.15rem;
}

[data-theme="dark"] .msg-avatar {
  background: var(--brand-900);
}

.msg-row.assistant .bubble {
  max-width: min(calc(92% - 44px), 480px);
  flex: 1;
  min-width: 0;
}

.bubble {
  max-width: min(92%, 520px);
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius);
  font-family: var(--font-sans);
  font-size: clamp(0.875rem, 3.5vw, 0.9375rem);
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.bubble.user {
  background: var(--user-bubble);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: var(--user-shadow);
}

.bubble.assistant {
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.bubble.assistant .label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 0.35rem;
}

.bubble.error {
  border-color: var(--error-border);
  background: var(--error-bg);
  color: var(--error-text);
}

.typing-row .typing {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  margin-top: 0.15rem;
  border-radius: var(--radius);
  background: var(--surface-solid);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.8rem;
  font-family: var(--font-sans);
}

.typing-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.2s ease infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-4px); opacity: 1; }
}

/* Composer */
.composer-wrap {
  flex-shrink: 0;
  padding: 0.55rem max(var(--pad-x), var(--safe-l)) calc(0.65rem + var(--safe-b)) max(var(--pad-x), var(--safe-r));
  border-top: 1px solid var(--border);
  background: var(--composer-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.disclaimer {
  font-size: clamp(0.62rem, 2.5vw, 0.68rem);
  color: var(--muted);
  text-align: center;
  margin-bottom: 0.45rem;
  line-height: 1.4;
  padding: 0 0.15rem;
}

.composer {
  display: flex;
  gap: 0.45rem;
  align-items: stretch;
  font-size: max(16px, 0.88rem);
  --composer-height: calc(1.45em * var(--composer-lines) + 1.2rem);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  height: var(--composer-height);
  min-height: var(--composer-height);
  max-height: var(--composer-height);
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-solid);
  color: var(--text);
  font-family: inherit;
  font-size: inherit;
  line-height: 1.45;
  resize: none;
  overflow-y: auto;
  outline: none;
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.composer textarea::placeholder {
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 300;
  color: var(--muted);
  opacity: 0.8;
  line-height: 1.45;
}

.composer textarea:focus {
  border-color: var(--accent-border);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer textarea:disabled {
  opacity: 0.6;
}

.btn-send {
  flex-shrink: 0;
  align-self: stretch;
  width: 3.25rem;
  min-width: 3.25rem;
  height: auto;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent-deep);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s, transform 0.1s, background 0.15s;
}

.btn-send:hover:not(:disabled) {
  background: var(--accent);
}

.btn-send:active:not(:disabled) {
  transform: scale(0.96);
}

.btn-send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-send svg {
  width: 20px;
  height: 20px;
}

@media (max-width: 380px) {
  :root,
  [data-theme="light"],
  [data-theme="dark"] {
    --pad-x: 0.75rem;
  }

  .status-dot {
    font-size: 0;
    max-width: none;
    gap: 0;
  }

  .chip {
    flex: 1 1 100%;
    max-width: 100%;
  }

  .bubble {
    max-width: 96%;
  }

  .msg-avatar {
    width: 32px;
    height: 32px;
  }

  .msg-row.assistant .bubble {
    max-width: min(calc(96% - 38px), 480px);
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .welcome {
    padding: 0.5rem;
    margin: 0;
  }

  .welcome-icon {
    font-size: 1.75rem;
    margin-bottom: 0.35rem;
  }

  .chips {
    margin-top: 0.5rem;
  }

  .messages {
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
  }
}

@media (min-width: 640px) {
  :root,
  [data-theme="light"],
  [data-theme="dark"] {
    --pad-x: 1.25rem;
    --app-max: 680px;
  }

  .bubble {
    max-width: min(85%, 480px);
  }

  .chip {
    max-width: calc(50% - 0.25rem);
  }
}

@media (min-width: 768px) {
  :root,
  [data-theme="light"],
  [data-theme="dark"] {
    --app-max: 760px;
    --pad-x: 1.5rem;
  }

  .app-shell {
    padding: 1rem;
    padding-top: max(1rem, var(--safe-t));
    padding-bottom: max(1rem, var(--safe-b));
    box-sizing: border-box;
  }

  .app {
    height: 100%;
    max-height: 900px;
    border-radius: 20px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-app);
    overflow: hidden;
  }

  .header {
    border-radius: 20px 20px 0 0;
  }

  .header-logo {
    width: 44px;
    height: 44px;
  }

  .btn-theme {
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
  }

  .messages {
    gap: 0.9rem;
    padding-top: 1rem;
    padding-bottom: 1rem;
  }

  .composer-wrap {
    border-radius: 0 0 20px 20px;
  }
}

@media (min-width: 1024px) {
  :root,
  [data-theme="light"],
  [data-theme="dark"] {
    --app-max: 820px;
  }

  .welcome p {
    max-width: 42ch;
  }
}

@media (prefers-reduced-motion: reduce) {
  .typing-dots span {
    animation: none;
    opacity: 0.7;
  }

  .messages {
    scroll-behavior: auto;
  }

  body,
  .page-bg,
  .app,
  .header,
  .toolbar,
  .composer-wrap,
  .bubble,
  .composer textarea {
    transition: none;
  }
}
