:root {
  color-scheme: light;
  --bg: #f4f7f8;
  --surface: #ffffff;
  --surface-2: #eef3f4;
  --sidebar: #0d2430;
  --sidebar-2: #133540;
  --text: #14242b;
  --muted: #65767e;
  --line: #dce5e8;
  --primary: #087f79;
  --primary-hover: #066b67;
  --accent: #ec6b4e;
  --danger: #c33d3d;
  --shadow: 0 20px 50px rgba(18, 52, 62, .12);
  --radius: 16px;
  font-family: Inter, "Noto Sans Thai", "Leelawadee UI", Tahoma, system-ui, sans-serif;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #09161c;
  --surface: #10232b;
  --surface-2: #16313a;
  --sidebar: #071218;
  --sidebar-2: #102a33;
  --text: #e8f1f2;
  --muted: #9ab0b7;
  --line: #28414a;
  --primary: #23aaa2;
  --primary-hover: #32bdb5;
  --accent: #f18265;
  --danger: #ff7878;
  --shadow: 0 20px 50px rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body { min-height: 100vh; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid color-mix(in srgb, var(--primary) 35%, transparent); outline-offset: 2px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.eyebrow { margin: 0 0 6px; color: var(--primary); font-size: 12px; font-weight: 800; letter-spacing: .14em; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.auth-shell { min-height: 100vh; display: grid; place-items: center; padding: 76px 22px 32px; background: radial-gradient(circle at 20% 10%, rgba(8,127,121,.12), transparent 34%), radial-gradient(circle at 80% 90%, rgba(236,107,78,.11), transparent 32%), var(--bg); }
.auth-brand { position: absolute; top: 24px; left: 30px; display: flex; align-items: center; gap: 12px; }
.auth-brand strong, .brand strong { display: block; letter-spacing: -.02em; }
.auth-brand span, .brand span { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.brand-mark { width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center; color: white; font-weight: 900; background: linear-gradient(145deg, var(--primary), #09596e); box-shadow: 0 8px 24px rgba(8,127,121,.24); }
.auth-card { width: min(460px, 100%); padding: 36px; border: 1px solid var(--line); border-radius: 24px; background: color-mix(in srgb, var(--surface) 94%, transparent); box-shadow: var(--shadow); }
.setup-card { width: min(900px, 100%); }
.auth-card h1 { margin: 0; font-size: clamp(28px, 4vw, 40px); letter-spacing: -.04em; }
.auth-card > .muted { margin: 8px 0 28px; }
.auth-lang { position: absolute; top: 26px; right: 30px; }

label { display: grid; gap: 7px; color: var(--muted); font-size: 13px; font-weight: 700; }
input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 11px; color: var(--text); background: var(--surface); transition: border-color .18s, box-shadow .18s; }
input, select { height: 44px; padding: 0 13px; }
textarea { padding: 13px 54px 13px 15px; resize: none; line-height: 1.55; }
input:hover, select:hover, textarea:hover { border-color: color-mix(in srgb, var(--primary) 45%, var(--line)); }
fieldset { min-width: 0; margin: 0; padding: 20px; border: 1px solid var(--line); border-radius: var(--radius); display: grid; gap: 16px; }
legend { padding: 0 8px; color: var(--text); font-size: 14px; font-weight: 800; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.form-grid.compact { gap: 15px; }
.form-grid .full { grid-column: 1 / -1; }
.basic-fields > .form-grid { width: 100%; }
.check { display: flex; align-items: center; gap: 9px; min-height: 34px; }
.check input { width: 18px; height: 18px; accent-color: var(--primary); }
.align-end { align-self: end; }
label small { color: var(--muted); font-weight: 500; }
.form-error { min-height: 20px; margin: 12px 0; color: var(--danger); font-size: 13px; font-weight: 700; }
.tls-warning { color: #a65a00; border: 1px solid #dba34a; background: rgba(219,163,74,.1); border-radius: 10px; padding: 10px 12px; font-size: 13px; }

.primary, .secondary, .ghost { min-height: 40px; border-radius: 10px; padding: 0 16px; font-weight: 800; transition: background .18s, transform .18s; }
.primary { color: white; background: var(--primary); border: 1px solid var(--primary); }
.primary:hover { background: var(--primary-hover); }
.primary:active, .secondary:active { transform: translateY(1px); }
.secondary { color: var(--primary); background: transparent; border: 1px solid var(--primary); }
.ghost { color: var(--text); background: transparent; border: 1px solid var(--line); }
.wide { width: 100%; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 292px 1fr; }
.sidebar { position: relative; z-index: 20; height: 100vh; display: flex; flex-direction: column; overflow: hidden; padding: 22px 16px; color: #eaf4f5; background: linear-gradient(180deg, var(--sidebar), #091b23); }
.brand { display: flex; align-items: center; gap: 11px; padding: 0 8px 22px; }
.brand span { color: #8eabb3; }
.new-chat { display: flex; align-items: center; justify-content: center; gap: 9px; }
.sidebar-label { margin: 26px 9px 10px; color: #77949c; font-size: 11px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.chat-list { min-height: 0; flex: 1; overflow-y: auto; display: grid; align-content: start; gap: 4px; scrollbar-width: thin; }
.chat-item { position: relative; display: grid; grid-template-columns: 1fr auto; align-items: center; border-radius: 10px; }
.chat-item.active { background: var(--sidebar-2); }
.chat-open { min-width: 0; border: 0; padding: 10px; text-align: left; color: inherit; background: transparent; }
.chat-open strong { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 13px; }
.chat-open span { display: block; margin-top: 4px; color: #78969e; font-size: 11px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.chat-actions { display: flex; opacity: 0; transition: opacity .15s; }
.chat-item:hover .chat-actions, .chat-item:focus-within .chat-actions { opacity: 1; }
.chat-actions button { width: 28px; height: 30px; padding: 0; border: 0; color: #aec3c8; background: transparent; font-size: 15px; }
.chat-actions button:hover { color: white; }
.sidebar-footer { display: grid; gap: 4px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,.09); }
.nav-button { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px; border: 0; border-radius: 9px; color: #bfd0d4; background: transparent; text-align: left; }
.nav-button:hover { color: white; background: rgba(255,255,255,.06); }

.main { min-width: 0; height: 100vh; display: grid; grid-template-rows: 70px 1fr auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 0 26px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(14px); }
.profile-control { min-width: 0; display: flex; align-items: center; gap: 10px; }
.profile-control > span { color: var(--muted); font-size: 12px; font-weight: 700; }
.profile-control select { width: min(260px, 38vw); height: 38px; font-weight: 700; }
.top-actions { display: flex; align-items: center; gap: 9px; }
.badge { display: inline-flex; align-items: center; gap: 7px; padding: 7px 10px; color: var(--muted); background: var(--surface-2); border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge i { width: 7px; height: 7px; border-radius: 50%; background: #28a878; box-shadow: 0 0 0 3px rgba(40,168,120,.14); }
.badge.waiting i { background: #e59d29; box-shadow: 0 0 0 3px rgba(229,157,41,.14); animation: pulse 1.2s infinite; }
.icon-button { width: 38px; height: 38px; display: grid; place-items: center; padding: 0; border: 1px solid var(--line); border-radius: 10px; color: var(--text); background: var(--surface); }
.text-button { font-size: 12px; font-weight: 900; }
html[data-theme="dark"] .sun, html:not([data-theme="dark"]) .moon { display: none; }
.mobile-only { display: none; }

.conversation { min-height: 0; overflow-y: auto; scroll-behavior: smooth; }
.empty-state { min-height: 100%; display: grid; place-items: center; align-content: center; padding: 40px 20px 100px; text-align: center; }
.orb { width: 86px; height: 86px; margin-bottom: 26px; display: grid; place-items: center; border-radius: 28px; color: white; background: linear-gradient(145deg, #0f9f96, #0a526a); box-shadow: 0 22px 42px rgba(8,127,121,.22); transform: rotate(-4deg); }
.orb span { font-size: 36px; font-weight: 900; transform: rotate(4deg); }
.empty-state h1 { margin: 0; font-size: clamp(30px, 5vw, 50px); letter-spacing: -.05em; }
.empty-state > p:not(.eyebrow) { max-width: 560px; margin: 12px auto 0; color: var(--muted); }
.suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 30px; }
.suggestions button { padding: 10px 14px; color: var(--text); border: 1px solid var(--line); border-radius: 999px; background: var(--surface); }
.suggestions button:hover { border-color: var(--primary); color: var(--primary); }
.messages { width: min(920px, 100%); margin: 0 auto; padding: 38px 28px 80px; }
.message { display: grid; grid-template-columns: 36px minmax(0, 1fr); gap: 14px; margin-bottom: 28px; }
.message.user { grid-template-columns: minmax(0, 1fr) 36px; }
.message.user .avatar { grid-column: 2; background: var(--accent); }
.message.user .message-body { grid-column: 1; grid-row: 1; justify-self: end; max-width: min(720px, 90%); padding: 12px 16px; border-radius: 16px 5px 16px 16px; background: var(--surface-2); }
.avatar { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 11px; color: white; background: var(--primary); font-size: 13px; font-weight: 900; }
.message-body { min-width: 0; line-height: 1.65; }
.message-body > :first-child { margin-top: 4px; }
.message-body > :last-child { margin-bottom: 0; }
.message-body pre { overflow-x: auto; padding: 16px; border: 1px solid var(--line); border-radius: 12px; color: var(--text); background: var(--surface-2); }
.message-body code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .9em; }
.message-body :not(pre) > code { padding: 2px 5px; border-radius: 5px; background: var(--surface-2); }
.message-body table { width: 100%; border-collapse: collapse; display: block; overflow-x: auto; }
.message-body th, .message-body td { padding: 8px 10px; border: 1px solid var(--line); text-align: left; }
.message-body a { color: var(--primary); }
.pending-line, .error-line { display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: 13px; }
.error-line { color: var(--danger); }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }

.composer-wrap { padding: 0 26px 14px; background: linear-gradient(transparent, var(--bg) 20%); }
.composer { position: relative; width: min(920px, 100%); margin: 0 auto; border-radius: 17px; background: var(--surface); box-shadow: 0 10px 35px rgba(13,36,48,.12); }
.composer textarea { min-height: 56px; max-height: 180px; border-radius: 17px; }
.send-button { position: absolute; right: 8px; bottom: 8px; width: 40px; height: 40px; display: grid; place-items: center; padding: 0; color: white; border: 0; border-radius: 12px; background: var(--primary); }
.send-button:disabled { cursor: not-allowed; opacity: .45; }
.composer-wrap > p { margin: 9px auto 0; color: var(--muted); text-align: center; font-size: 11px; }

.settings-dialog { width: min(820px, calc(100% - 28px)); max-height: min(840px, calc(100vh - 28px)); padding: 0; color: var(--text); border: 1px solid var(--line); border-radius: 22px; background: var(--surface); box-shadow: var(--shadow); }
.settings-dialog::backdrop { background: rgba(3, 16, 22, .58); backdrop-filter: blur(4px); }
.dialog-head { display: flex; justify-content: space-between; align-items: center; padding: 25px 28px 18px; }
.dialog-head h2 { margin: 0; font-size: 26px; }
.tabs { display: flex; gap: 8px; padding: 0 28px; border-bottom: 1px solid var(--line); }
.tabs button { padding: 11px 3px; margin-right: 20px; color: var(--muted); border: 0; border-bottom: 2px solid transparent; background: transparent; font-weight: 800; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-panel { padding: 25px 28px 30px; overflow-y: auto; }
.section-head { display: flex; justify-content: space-between; align-items: start; gap: 20px; }
.section-head h3, .tab-panel > h3 { margin: 0; }
.section-head p { margin: 5px 0 0; color: var(--muted); font-size: 13px; }
.profile-list { display: grid; gap: 10px; margin-top: 20px; }
.profile-row { display: grid; grid-template-columns: minmax(0,1fr) auto; align-items: center; gap: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 13px; }
.profile-row strong { display: block; }
.profile-row span { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-row.inactive { opacity: .6; }
.row-actions { display: flex; gap: 7px; }
.row-actions button { min-height: 32px; padding: 0 9px; border: 1px solid var(--line); border-radius: 8px; color: var(--text); background: var(--surface); font-size: 12px; font-weight: 700; }
.profile-form { margin-top: 20px; padding: 18px; border-radius: 14px; background: var(--surface-2); }
.form-actions { display: flex; justify-content: flex-end; gap: 9px; }
.password-form { max-width: 440px; display: grid; gap: 15px; margin-top: 20px; }
.toast { position: fixed; z-index: 100; left: 50%; bottom: 26px; transform: translate(-50%, 20px); padding: 11px 16px; color: white; border-radius: 10px; background: #112d36; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.scrim { display: none; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { opacity: .45; } }

@media (max-width: 760px) {
  .form-grid { grid-template-columns: 1fr; }
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(310px, 86vw); transform: translateX(-102%); transition: transform .22s ease; }
  body.menu-open .sidebar { transform: translateX(0); }
  .scrim { position: fixed; z-index: 15; inset: 0; background: rgba(0,0,0,.45); }
  body.menu-open .scrim { display: block; }
  .mobile-only { display: grid; }
  .topbar { padding: 0 14px; }
  .profile-control > span, .badge { display: none; }
  .profile-control select { width: min(220px, 48vw); }
  .messages { padding: 28px 16px 70px; }
  .composer-wrap { padding-inline: 12px; }
  .auth-card { padding: 26px 20px; }
  .auth-brand { left: 20px; }
  .auth-lang { right: 20px; }
  .settings-dialog { max-height: calc(100vh - 12px); }
  .dialog-head, .tab-panel { padding-inline: 18px; }
  .tabs { padding-inline: 18px; }
  .section-head { align-items: center; }
  .profile-row { grid-template-columns: 1fr; }
  .row-actions { justify-content: flex-end; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
