/* ================= ธีม / ตัวแปร ================= */
:root {
  --accent: #19c37d;
  --accent-strong: #0fa968;
  --brand: #76b900;
  --danger: #ef4444;
  --radius: 14px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", "Noto Sans Thai",
    "Sarabun", "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

[data-theme="dark"] {
  --bg: #0d0f12;
  --bg-2: #14161a;
  --bg-3: #1c1f26;
  --bg-4: #242832;
  --text: #e9ecef;
  --muted: #9aa3ad;
  --border: #262a31;
  --border-strong: #343a44;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --code-bg: #0b0d10;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #ffffff;
  --bg-2: #f7f7f8;
  --bg-3: #eef0f3;
  --bg-4: #e3e6ea;
  --text: #1d2129;
  --muted: #667085;
  --border: #e4e7ec;
  --border-strong: #cfd4dc;
  --shadow: 0 12px 40px rgba(16, 24, 40, 0.12);
  --code-bg: #0b0d10;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

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

/* ================= ปุ่มพื้นฐาน ================= */
.btn-primary {
  background: var(--accent);
  color: #06231a;
  border: 0;
  border-radius: 10px;
  padding: 11px 18px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
}
.btn-primary:hover { background: var(--accent-strong); }
.btn-primary:active { transform: scale(0.99); }

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
}
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }

/* ================= โครงหลัก ================= */
.app {
  display: flex;
  height: 100%;
  height: 100dvh;
}

/* ================= Sidebar ================= */
.sidebar {
  width: 272px;
  flex: 0 0 272px;
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  padding: 12px 10px;
  gap: 10px;
  z-index: 30;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px;
}

.brand-logo { display: block; flex: 0 0 auto; }

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 12px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
}
.new-chat:hover { background: var(--bg-3); }

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
}
.search-box input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: none;
  background: transparent;
  font-size: 13px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 2px;
  scrollbar-width: thin;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 9px;
  cursor: pointer;
  color: var(--text);
  font-size: 13.5px;
  border: 0;
  background: transparent;
  text-align: left;
  width: 100%;
}
.conv-item:hover { background: var(--bg-3); }
.conv-item.active { background: var(--bg-4); }

.conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-actions {
  display: none;
  gap: 2px;
}
.conv-item:hover .conv-actions,
.conv-item.active .conv-actions { display: flex; }

.conv-actions button {
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 24px;
  height: 24px;
  border-radius: 6px;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 14px;
}
.conv-actions button:hover { background: var(--bg-2); color: var(--text); }

.conv-empty {
  color: var(--muted);
  font-size: 13px;
  padding: 14px 10px;
  text-align: center;
}

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.foot-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 10px;
  background: transparent;
  border: 0;
  border-radius: 9px;
  color: var(--muted);
  cursor: pointer;
  font-size: 13.5px;
  text-align: left;
}
.foot-btn:hover { background: var(--bg-3); color: var(--text); }

.guest-box { padding: 4px 8px 10px; }
.guest-box p { margin: 0 0 8px; }
#googleBtn { min-height: 40px; color-scheme: light; }
/* กรอบเรนโบว์ของ Saturn 5 Super Ultra */
.model-frame { display: inline-block; max-width: 100%; border-radius: 12px; }
.model-frame.rainbow, .msg.saturn .avatar {
  background: linear-gradient(90deg, #ff3b5c, #ff9f1c, #ffe14d, #3ddc84, #2bc4ff, #7a6bff, #ff4fd8, #ff3b5c) 0 50% / 300% 100%;
  animation: rainbow 4s linear infinite;
}
.model-frame.rainbow { padding: 2px; box-shadow: 0 0 14px rgba(122, 107, 255, .45); }
.model-frame.rainbow select { border-color: transparent; }
.msg.assistant.saturn .avatar { position: relative; isolation: isolate; }
.msg.assistant.saturn .avatar::before { content: ''; position: absolute; inset: 2px; border-radius: 7px; background: var(--bg-2); z-index: -1; }
@keyframes rainbow { to { background-position: 300% 50%; } }
@media (prefers-reduced-motion: reduce) { .model-frame.rainbow, .msg.saturn .avatar { animation: none; } }

.msg-status { margin-bottom: 8px; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }
.msg-sources { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 10px; font-size: 12px; color: var(--muted); }
.msg-sources a { color: inherit; padding: 3px 9px; border: 1px solid var(--border); border-radius: 99px; text-decoration: none; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg-sources a:hover { color: var(--text); border-color: var(--border-strong); }
.check-row { display: flex; gap: 9px; align-items: flex-start; font-size: 13.5px; cursor: pointer; }
.code-head .run-btn { margin-left: auto; color: var(--accent); font-weight: 600; }
.code-block.collapsed pre { max-height: 180px; overflow: auto; }

.canvas { flex: 1 1 52%; min-width: 0; display: flex; flex-direction: column; border-left: 1px solid var(--border); background: var(--bg-2); }
.canvas-open .main { flex: 1 1 40%; min-width: 320px; }
.canvas.full { position: fixed; inset: 0; z-index: 60; border: 0; }
.canvas-head { display: flex; align-items: center; gap: 2px; padding: 8px 10px; border-bottom: 1px solid var(--border); }
.canvas-title { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; font-weight: 600; }
.canvas-head .icon-btn:disabled { opacity: .3; cursor: default; }
.canvas-tabs { display: inline-flex; background: var(--bg-4); border-radius: 99px; padding: 2px; margin: 0 6px; }
.canvas-tab { border: 0; background: transparent; color: var(--muted); padding: 5px 12px; border-radius: 99px; cursor: pointer; font: inherit; font-size: 12.5px; white-space: nowrap; }
.canvas-tab.active { background: var(--bg); color: var(--text); }
.canvas-code { flex: 1; margin: 0; padding: 14px; overflow: auto; font-size: 12.5px; line-height: 1.55; white-space: pre; background: var(--bg); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.canvas-frame { flex: 1; min-height: 0; background: #fff; }
.canvas-frame iframe { width: 100%; height: 100%; border: 0; display: block; }
.canvas-error { display: flex; gap: 10px; align-items: center; justify-content: space-between; padding: 8px 12px; font-size: 12.5px; color: var(--danger); background: var(--bg-3); overflow-wrap: anywhere; }
@media (max-width: 1000px) { .canvas { position: fixed; inset: 0; z-index: 60; border: 0; } }
@media (max-width: 520px) { .canvas-head .icon-btn { padding: 4px; width: 30px; } #canvasCopy, #canvasFull { display: none; } .canvas-tabs { margin: 0 2px; } .canvas-tab { padding: 5px 8px; } }

.quota { padding: 6px 10px 10px; }
.quota-head { display: flex; justify-content: space-between; font-size: 12.5px; margin-bottom: 6px; color: var(--muted); }
.quota-head b { color: var(--text); }
.quota .quota-track { display: block; width: 100%; height: 7px; }
.quota-detail { margin-top: 5px; font-size: 11px; color: var(--muted); }
.quota-track { display: inline-block; width: 72px; height: 5px; border-radius: 99px; background: var(--bg-4); overflow: hidden; flex: 0 0 auto; }
.quota-fill { display: block; height: 100%; width: 0; border-radius: 99px; background: var(--accent); transition: width 1.2s ease; }
.quota-fill.warn { background: #e8b04a; }
.quota-fill.full { background: var(--danger); }

.adm { width: min(760px, calc(100vw - 24px)); max-width: none; }
.adm-head { display: flex; justify-content: space-between; align-items: center; }
.adm-head h2 { margin: 0; }
.adm-tabs { display: flex; gap: 6px; overflow-x: auto; }
.adm-tab { padding: 7px 12px; border-radius: 9px; border: 0; background: transparent; color: var(--muted); cursor: pointer; white-space: nowrap; font: inherit; font-size: 13.5px; }
.adm-tab.active { background: var(--bg-3); color: var(--text); }
.adm-body { min-height: 240px; max-height: 60vh; overflow: auto; }
.adm-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.adm-card { background: var(--bg-3); border-radius: 12px; padding: 12px; display: flex; flex-direction: column; gap: 2px; }
.adm-card b { font-size: 18px; }
.adm-card span { color: var(--muted); font-size: 12px; }
.adm-input { width: 100%; padding: 9px 12px; background: var(--bg); color: var(--text); border: 1px solid var(--border-strong); border-radius: 9px; outline: none; font: inherit; }
.adm-list { display: flex; flex-direction: column; margin-top: 8px; }
.adm-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; padding: 10px 2px; border-bottom: 1px solid var(--border); }
.adm-who { flex: 1 1 260px; min-width: 0; overflow-wrap: anywhere; }
.adm-usage { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.adm-acts { display: flex; flex-wrap: wrap; gap: 6px; }
.adm-btn { padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border-strong); background: transparent; color: var(--text); cursor: pointer; font: inherit; font-size: 12.5px; }
.adm-btn.bad { color: var(--danger); }
.adm-btn:disabled { opacity: .5; }
.adm-tag { font-size: 11px; padding: 2px 7px; border-radius: 99px; background: var(--bg-4); }
.adm-tag.bad { color: var(--danger); }
.adm-add { display: flex; gap: 8px; }
.adm-add .btn-primary { white-space: nowrap; }
.adm-levels { display: flex; flex-wrap: wrap; gap: 8px; }
.adm-levels label { display: flex; flex-direction: column; gap: 3px; }
.adm-level { padding: 6px 8px; }
.adm-save { position: sticky; bottom: 0; padding: 10px 0 2px; background: var(--bg-2); }
.conv-actions .pin { filter: grayscale(1); opacity: .6; }
.conv-actions .pin.on { filter: none; opacity: 1; }
.adm-slot { flex: 1 1 100%; }
.adm-slot:empty { display: none; }
.adm-picker { display: flex; flex-direction: column; gap: 8px; }
.adm-picks { max-height: 220px; overflow: auto; display: flex; flex-direction: column; }
.adm-pick { display: flex; gap: 8px; align-items: baseline; padding: 4px 2px; overflow-wrap: anywhere; cursor: pointer; }
.adm-pick[hidden], .adm-form[hidden] { display: none; }
.adm-form { display: flex; flex-direction: column; gap: 12px; max-width: 380px; }

.account { display: flex; align-items: center; gap: 10px; padding: 6px 10px 8px; min-width: 0; }
.account-pic { width: 32px; height: 32px; border-radius: 50%; flex: 0 0 auto; }
.account-text { min-width: 0; }
.account-name, .account-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-name { font-size: 13.5px; }

/* ================= Main ================= */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  z-index: 10;
}

.model-wrap { min-width: 0; flex: 1; }

#modelSelect {
  max-width: 100%;
  padding: 8px 34px 8px 12px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 17px) 52%, calc(100% - 12px) 52%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}
#modelSelect:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); }

.model-note {
  font-size: 11.5px;
  color: var(--muted);
  margin: 4px 2px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.gen-btn {
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--brand) 45%, var(--border-strong));
  background: color-mix(in srgb, var(--brand) 12%, var(--bg-2));
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.gen-btn:hover { background: color-mix(in srgb, var(--brand) 22%, var(--bg-2)); }
.gen-btn:disabled { opacity: 0.5; cursor: wait; }

/* switch */
.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 12.5px;
  color: var(--muted);
  user-select: none;
}
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track {
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: var(--bg-4);
  border: 1px solid var(--border-strong);
  position: relative;
  transition: background 0.15s ease;
}
.switch-dot {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s ease, background 0.15s ease;
}
.switch input:checked + .switch-track { background: color-mix(in srgb, var(--accent) 35%, var(--bg-4)); }
.switch input:checked + .switch-track .switch-dot { transform: translateX(14px); background: var(--accent); }

/* ================= Messages ================= */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 26px 16px 40px;
  scroll-behavior: smooth;
}

.msg {
  max-width: 860px;
  margin: 0 auto 22px;
  display: flex;
  gap: 12px;
  animation: fadeIn 0.18s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.avatar {
  flex: 0 0 30px;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 700;
  margin-top: 2px;
}
.msg.assistant:not(.saturn) .avatar { background: color-mix(in srgb, var(--brand) 18%, var(--bg-3)); color: var(--brand); }
.msg.user .avatar { background: var(--bg-4); color: var(--muted); }

.msg-body { flex: 1; min-width: 0; }

.msg.user { flex-direction: row-reverse; }
.msg.user .msg-body {
  flex: 0 1 auto;
  max-width: 82%;
  background: var(--bg-3);
  border-radius: 16px 16px 4px 16px;
  padding: 11px 15px;
}
.msg.user .avatar { margin-left: 0; }

.msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11.5px;
  color: var(--muted);
  margin: 6px 0 0;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.msg:hover .msg-meta { opacity: 1; }
.msg-meta button {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 11.5px;
}
.msg-meta button:hover { background: var(--bg-3); color: var(--text); }

/* จอสัมผัสไม่มีเมาส์ให้ชี้ — แสดงปุ่มใต้ข้อความตลอด */
@media (hover: none), (pointer: coarse) { .msg-meta { opacity: 0.9; flex-wrap: wrap; } }

.msg.user.editing .msg-body { flex: 1 1 auto; max-width: 100%; min-width: 0; }
.edit-box textarea { width: 100%; resize: none; min-height: 44px; padding: 8px 10px; background: var(--bg); color: var(--text); border: 1px solid var(--border-strong); border-radius: 10px; outline: none; font: inherit; line-height: 1.6; }
.edit-box textarea:focus { border-color: var(--accent); }
.edit-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.edit-actions .small { margin-right: auto; }
.edit-actions .btn-primary { padding: 7px 14px; font-size: 13px; }

/* markdown */
.md { line-height: 1.68; font-size: 15px; overflow-wrap: anywhere; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { margin: 20px 0 10px; line-height: 1.3; }
.md h1 { font-size: 22px; }
.md h2 { font-size: 19px; }
.md h3 { font-size: 16.5px; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 24px; }
.md li { margin: 4px 0; }
.md li > p { margin: 0; }
.md blockquote {
  margin: 0 0 12px;
  padding: 6px 14px;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
}
.md a { color: #58a6ff; text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }
.md table { border-collapse: collapse; margin: 0 0 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border-strong); padding: 7px 11px; text-align: left; }
.md th { background: var(--bg-3); }
.md img { max-width: 100%; border-radius: 10px; }
.md code:not(pre code) {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1.5px 5px;
  font-family: var(--mono);
  font-size: 0.88em;
}

/* code block */
.code-block {
  margin: 0 0 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  overflow: hidden;
  background: var(--code-bg);
}
.code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  background: #14171c;
  border-bottom: 1px solid #22262e;
  color: #9aa3ad;
  font-size: 11.5px;
  font-family: var(--mono);
}
.code-head button {
  border: 0;
  background: transparent;
  color: #9aa3ad;
  cursor: pointer;
  font-size: 11.5px;
  padding: 2px 6px;
  border-radius: 5px;
}
.code-head button:hover { background: #22262e; color: #e9ecef; }
.code-block pre {
  margin: 0;
  padding: 13px 15px;
  overflow-x: auto;
  background: var(--code-bg);
}
/* พื้นมืดทั้งสองธีม ตัวอักษรจึงต้องสว่างเสมอ */
.code-block pre, .code-block pre code { color: #e6edf3; }
.code-block pre code {
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  background: transparent;
  padding: 0;
}

/* thinking */
.thinking {
  margin: 0 0 12px;
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  background: color-mix(in srgb, var(--bg-3) 60%, transparent);
  overflow: hidden;
}
.thinking summary {
  cursor: pointer;
  padding: 8px 12px;
  font-size: 12.5px;
  color: var(--muted);
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 7px;
}
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary::before {
  content: "▸";
  font-size: 10px;
  transition: transform 0.15s ease;
}
.thinking[open] summary::before { transform: rotate(90deg); }
.thinking .thinking-body {
  padding: 0 14px 12px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-wrap;
  max-height: 420px;
  overflow-y: auto;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

/* streaming cursor */
.cursor {
  display: inline-block;
  width: 7px;
  height: 1.05em;
  margin-left: 2px;
  vertical-align: -0.16em;
  background: var(--accent);
  border-radius: 1px;
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ข้อความ error */
.msg-error {
  border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent);
  background: color-mix(in srgb, var(--danger) 9%, transparent);
  color: color-mix(in srgb, var(--danger) 85%, var(--text));
  border-radius: 10px;
  padding: 10px 13px;
  font-size: 13.5px;
}

/* empty state */
.empty-state {
  max-width: 700px;
  margin: 8vh auto 0;
  text-align: center;
  padding: 0 16px;
}
.empty-state h2 { font-size: 26px; margin: 0 0 8px; }
.empty-state p { color: var(--muted); margin: 0 0 26px; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.chip {
  border: 1px solid var(--border-strong);
  background: var(--bg-2);
  color: var(--text);
  border-radius: 999px;
  padding: 9px 15px;
  font-size: 13.5px;
  cursor: pointer;
}
.chip:hover { background: var(--bg-3); border-color: var(--accent); }

/* ================= Composer ================= */
.composer-wrap {
  padding: 6px 16px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 72%, transparent);
}

.composer {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 8px 8px 6px;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.16);
  transition: border-color 0.15s ease;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--border-strong)); }

#input {
  flex: 1;
  min-height: 40px;
  max-height: 200px;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  padding: 10px 6px;
  line-height: 1.5;
  font-size: 15px;
}

.send-btn {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #06231a;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
}
.send-btn:hover { background: var(--accent-strong); }
.send-btn:disabled { opacity: 0.45; cursor: default; }
.send-btn.stop { background: var(--bg-4); color: var(--text); }

.composer-hint {
  max-width: 860px;
  margin: 7px auto 0;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
}

.attachments {
  max-width: 860px;
  margin: 0 auto 8px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.thumb {
  position: relative;
  width: 62px;
  height: 62px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  background: var(--bg-3);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb button {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 19px;
  height: 19px;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.msg-images { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.msg-images img {
  width: 92px;
  height: 92px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
  cursor: zoom-in;
}

/* ================= ตั้งค่า ================= */
dialog.settings {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg-2);
  color: var(--text);
  padding: 0;
  width: min(480px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
dialog.settings::backdrop { background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(2px); }
.settings-inner { padding: 22px; display: flex; flex-direction: column; gap: 16px; }
.settings-inner h2 { margin: 0; font-size: 18px; }

.field { display: flex; flex-direction: column; gap: 7px; font-size: 13.5px; }
.field > span { color: var(--muted); }
.field input[type="range"] { accent-color: var(--accent); }
.field textarea,
.field input[type="number"] {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  padding: 9px 11px;
  outline: none;
  font-size: 13.5px;
}
.field textarea:focus,
.field input:focus { border-color: var(--accent); }

.settings-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
}

.gen-select {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  padding: 9px 11px;
  font-size: 13.5px;
  outline: none;
}

.gen-result {
  min-height: 40px;
  max-height: 50vh;
  overflow-y: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  padding: 10px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}
.gen-result img,
.gen-result video {
  max-width: 100%;
  max-height: 42vh;
  border-radius: 10px;
  display: block;
  margin: 0 auto;
}
.gen-result .gen-file {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.gen-result a {
  color: var(--accent);
  font-size: 12.5px;
  text-decoration: none;
}

.msg-media {
  margin: 8px 0 12px;
}
.msg-media img,
.msg-media video {
  max-width: 100%;
  max-height: 440px;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: block;
}
.msg-media a {
  display: inline-block;
  margin-top: 6px;
  color: var(--accent);
  font-size: 12px;
  text-decoration: none;
}

/* ================= Toast ================= */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--bg-4);
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13.5px;
  box-shadow: var(--shadow);
  z-index: 100;
  max-width: calc(100vw - 40px);
}
.toast.error { border-color: color-mix(in srgb, var(--danger) 60%, transparent); }

/* ================= Responsive ================= */
.only-mobile { display: none; }

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 25;
}

@media (max-width: 820px) {
  .only-mobile { display: inline-flex; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    width: 286px;
    flex-basis: 286px;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }

  .messages { padding: 18px 12px 30px; }
  .msg { gap: 9px; }
  .msg.user .msg-body { max-width: 88%; }
  .md { font-size: 14.5px; }
  .switch-label { display: none; }
  .model-note { display: none; }
  .composer-wrap { padding: 6px 10px calc(8px + env(safe-area-inset-bottom)); }
}

/* scrollbar */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--muted); background-clip: content-box; }
::-webkit-scrollbar-track { background: transparent; }

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