/* ============================================================
 * TDFB Chat — Modern UI
 * Palette: warm cream + matcha accent, Claude-inspired layout
 * ============================================================ */

:root {
  /* surfaces */
  --bg:            #faf8f2;
  --surface:       #ffffff;
  --sidebar:       #f1ede2;
  --sidebar-hover: #e8e3d4;
  --sidebar-active:#dfd9c4;

  /* text */
  --text:          #1a1a17;
  --text-soft:     #3a3933;
  --muted:         #77736a;
  --muted-2:       #a9a497;

  /* bubbles */
  --user-bubble:       #3f7048;
  --user-bubble-text:  #ffffff;
  --assistant-bubble:  #ffffff;
  --assistant-border:  #ece7d6;

  /* accents */
  --accent:        #3f7048;
  --accent-hover:  #345d3c;
  --accent-soft:   rgba(63, 112, 72, 0.08);
  --accent-ring:   rgba(63, 112, 72, 0.22);
  --danger:        #b44848;
  --danger-soft:   rgba(180, 72, 72, 0.08);

  /* borders */
  --border:        #e5e0d0;
  --border-soft:   #eee9d9;

  /* radii */
  --r-xs: 6px;
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --r-xl: 20px;

  /* shadows */
  --shadow-sm: 0 1px 2px rgba(30, 30, 23, 0.04);
  --shadow-md: 0 4px 16px rgba(30, 30, 23, 0.06);
  --shadow-lg: 0 20px 48px rgba(30, 30, 23, 0.08);

  /* typography */
  --font-sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
               "Noto Sans Thai", "IBM Plex Sans Thai", "Helvetica Neue", Arial, sans-serif;

  color-scheme: light;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }

.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ============================================================
 * LOGIN PAGE — centered card
 * ============================================================ */

body.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 20% 10%, rgba(63,112,72,0.07), transparent 60%),
    radial-gradient(800px 500px at 80% 90%, rgba(180,160,80,0.06), transparent 60%),
    var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  padding: 40px 36px 32px;
  width: min(440px, 100%);
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.login-logo {
  width: 56px; height: 56px;
  margin: 0 auto 18px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4a8355 0%, #2f5635 100%);
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 20px;
  box-shadow: 0 8px 20px rgba(63,112,72,0.25);
  letter-spacing: 0.5px;
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 22px; font-weight: 700;
  letter-spacing: -0.01em;
}
.login-card .lead {
  margin: 0 0 28px;
  color: var(--muted);
  font-size: 14px;
}

.login-card form {
  display: flex; flex-direction: column; gap: 12px;
  text-align: left;
}

.input-label {
  font-size: 13px; font-weight: 500;
  color: var(--text-soft);
  margin-bottom: 4px;
}

input[type="email"], input[type="text"], textarea {
  width: 100%;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: 15px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input[type="email"]:focus, input[type="text"]:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-ring);
}

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 8px;
  padding: 11px 16px;
  border: 1px solid transparent;
  border-radius: var(--r-md);
  font-size: 14px; font-weight: 600;
  transition: background-color .15s, border-color .15s, transform .05s, box-shadow .15s;
  user-select: none;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow-sm);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-ghost {
  background: transparent;
  color: var(--text-soft);
  border-color: var(--border);
}
.btn-ghost:hover:not(:disabled) { background: var(--sidebar-hover); }

.btn-icon {
  padding: 6px;
  border-radius: var(--r-sm);
  background: transparent; border: 0;
  color: var(--muted);
  transition: background-color .12s, color .12s;
}
.btn-icon:hover:not(:disabled) { background: var(--sidebar-hover); color: var(--text); }
.btn-icon.danger:hover { background: var(--danger-soft); color: var(--danger); }

.btn-full { width: 100%; }

.status {
  margin-top: 14px;
  min-height: 20px;
  font-size: 13px;
  text-align: center;
}
.status.success { color: var(--accent); }
.status.error   { color: var(--danger); }

.login-foot {
  margin-top: 22px;
  font-size: 12px;
  color: var(--muted-2);
  text-align: center;
}

/* ============================================================
 * APP LAYOUT — sidebar + main
 * ============================================================ */

body.app {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: row;
}

/* ---------- sidebar ---------- */
#sidebar {
  width: 272px; min-width: 272px;
  height: 100vh;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}

.sidebar-brand {
  padding: 18px 16px 14px;
  display: flex; align-items: center; gap: 10px;
}
.sidebar-brand .logo {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4a8355 0%, #2f5635 100%);
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 13px;
  box-shadow: 0 2px 6px rgba(63,112,72,0.25);
}
.sidebar-brand .name {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.sidebar-new {
  padding: 0 12px 10px;
}
#new-chat {
  width: 100%;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--text-soft);
  font-size: 14px; font-weight: 500;
  display: inline-flex; align-items: center; gap: 8px;
  transition: background-color .12s, border-color .12s;
  box-shadow: var(--shadow-sm);
}
#new-chat:hover { background: var(--sidebar-hover); border-color: var(--muted-2); }
#new-chat svg { width: 16px; height: 16px; }

#conv-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 8px 16px;
}

.conv-group {
  padding: 14px 10px 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.conv-row {
  display: flex; align-items: center;
  padding: 2px 2px 2px 4px;
  margin-bottom: 2px;
  border-radius: var(--r-sm);
  transition: background-color .12s;
}
.conv-row:hover { background: var(--sidebar-hover); }
.conv-row.active { background: var(--sidebar-active); }

.conv-title {
  flex: 1; min-width: 0;
  padding: 8px 10px;
  background: none; border: 0;
  text-align: left;
  font-size: 14px;
  color: var(--text-soft);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  border-radius: var(--r-sm);
}
.conv-title:hover { color: var(--text); }
.conv-row.active .conv-title { color: var(--text); font-weight: 500; }

.conv-del {
  opacity: 0;
  margin-right: 4px;
  padding: 6px;
  background: transparent; border: 0;
  color: var(--muted);
  border-radius: var(--r-xs);
  transition: opacity .12s, background-color .12s, color .12s;
  line-height: 0;
}
.conv-row:hover .conv-del,
.conv-row.active .conv-del { opacity: 1; }
.conv-del:hover { background: var(--danger-soft); color: var(--danger); }
.conv-del svg { width: 14px; height: 14px; }

#admin-bar {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: rgba(63, 112, 72, 0.03);
  display: flex; flex-direction: column; gap: 4px;
}
#admin-bar[hidden] { display: none; }
.admin-btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 7px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  color: var(--text-soft);
  font-size: 13px;
  transition: background-color .12s, border-color .12s;
}
.admin-btn:hover:not(:disabled) {
  background: var(--sidebar-hover);
  border-color: var(--accent);
  color: var(--accent);
}
.admin-btn:disabled { opacity: 0.5; cursor: wait; }
.admin-btn.spinning svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.admin-meta {
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding-left: 2px;
  line-height: 1.3;
}
.admin-meta.error { color: var(--danger); }

.sidebar-foot {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 32px; height: 32px;
  flex-shrink: 0;
  border-radius: 50%;
  background: linear-gradient(135deg, #b8a66a 0%, #8a7944 100%);
  color: #fff;
  font-weight: 600; font-size: 13px;
  display: grid; place-items: center;
  text-transform: uppercase;
}
#user-email {
  flex: 1; min-width: 0;
  font-size: 13px;
  color: var(--text-soft);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- chat area ---------- */
#chat-area {
  flex: 1 1 auto;
  min-width: 0;
  display: flex; flex-direction: column;
  height: 100vh;
  background: var(--bg);
}

#chat-area header {
  flex: 0 0 auto;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border-soft);
  background: var(--bg);
}

.controls {
  display: flex; align-items: center; gap: 14px;
  flex-wrap: wrap;
}
.control {
  display: inline-flex; align-items: center; gap: 6px;
}
.control label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.controls select {
  padding: 7px 30px 7px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: 13px;
  color: var(--text-soft);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  appearance: none;
  -webkit-appearance: none;
  background-image:
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2377736a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  transition: border-color .15s, box-shadow .15s;
}
.controls select:hover { border-color: var(--muted-2); }
.controls select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-ring);
}

#messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 28px 20px 40px;
}
.messages-inner { max-width: 760px; margin: 0 auto; }

.msg {
  display: flex; margin-bottom: 18px;
  animation: fade .18s ease-out;
}
@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}
.msg.user     { justify-content: flex-end; }
.msg.assistant { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 12px 16px;
  border-radius: var(--r-lg);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.6;
  font-size: 15px;
}
.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 6px;
}
.msg.assistant .bubble {
  background: var(--assistant-bubble);
  color: var(--text);
  border: 1px solid var(--assistant-border);
  border-bottom-left-radius: 6px;
  box-shadow: var(--shadow-sm);
}

.bubble code {
  background: rgba(0,0,0,0.05);
  padding: 1px 6px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}
.msg.user .bubble code { background: rgba(255,255,255,0.18); }

.usage {
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.msg.user .usage { color: rgba(255,255,255,0.7); }

.pages-row {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
}
.pages-row summary {
  cursor: pointer;
  padding: 4px 0;
  list-style: none;
  user-select: none;
}
.pages-row summary::marker, .pages-row summary::-webkit-details-marker { display: none; }
.pages-row summary:hover { color: var(--text-soft); }
.pages-row ul {
  margin: 6px 0 0;
  padding-left: 18px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
}
.pages-row li { padding: 1px 0; }

.empty {
  color: var(--muted);
  text-align: center;
  margin-top: 60px;
  font-size: 14px;
}
.empty.error { color: var(--danger); }

/* composer */
#composer-wrap {
  flex: 0 0 auto;
  padding: 0 20px 20px;
  background: var(--bg);
}
form#composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex; gap: 8px;
  align-items: flex-end;
  padding: 8px 8px 8px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  transition: border-color .15s, box-shadow .15s;
}
form#composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-ring);
}
#input {
  flex: 1;
  border: 0;
  background: transparent;
  resize: none;
  outline: none;
  padding: 10px 4px;
  min-height: 24px;
  max-height: 200px;
  font-size: 15px;
  line-height: 1.5;
}
#send {
  flex-shrink: 0;
  width: 38px; height: 38px;
  padding: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: 0;
  display: grid; place-items: center;
  transition: background-color .15s, transform .05s;
}
#send:hover:not(:disabled) { background: var(--accent-hover); }
#send:disabled { opacity: 0.35; cursor: not-allowed; }
#send svg { width: 18px; height: 18px; }

/* scrollbar */
#conv-list::-webkit-scrollbar, #messages::-webkit-scrollbar { width: 10px; }
#conv-list::-webkit-scrollbar-thumb, #messages::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.08);
  border: 3px solid transparent;
  background-clip: padding-box;
  border-radius: 10px;
}
#conv-list::-webkit-scrollbar-thumb:hover, #messages::-webkit-scrollbar-thumb:hover {
  background-color: rgba(0,0,0,0.14);
  background-clip: padding-box;
}

/* ============================================================
 * Responsive
 * ============================================================ */
@media (max-width: 720px) {
  #sidebar { width: 240px; min-width: 240px; }
  .bubble { max-width: 92%; }
}
@media (max-width: 560px) {
  #sidebar { display: none; }
  #chat-area header { padding: 12px 16px; }
  #messages { padding: 20px 12px 32px; }
  #composer-wrap { padding: 0 12px 14px; }
}
