/* ════════════════════════════════════════════════════════════
   Ollama Chat — Stylesheet
   ════════════════════════════════════════════════════════════ */

/* ---------- Theme tokens ---------- */
:root {
  --font-sans: 'Inter', 'IBM Plex Sans Thai', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, 'SF Mono', monospace;
  --radius: 14px;
  --radius-sm: 10px;
  --sidebar-w: 280px;
  --accent: #2e9bff;
  --accent-2: #19d3da;
  --accent-grad: linear-gradient(135deg, #2e9bff 0%, #19d3da 100%);
}

html[data-theme="dark"] {
  --bg: #0e0f13;
  --bg-elev: #15171d;
  --bg-elev-2: #1c1f27;
  --bg-hover: #232732;
  --surface: #15171d;
  --border: #262a35;
  --text: #e8eaf0;
  --text-dim: #9aa0ad;
  --text-faint: #6b7180;
  --bubble-user: linear-gradient(135deg, #6c63ff 0%, #5a6cff 100%);
  --bubble-user-text: #fff;
  --code-bg: #0b0c10;
  --think-bg: #14161c;
  --think-border: #2a2f3d;
  --shadow: 0 10px 40px -12px rgba(0,0,0,.6);
}

html[data-theme="light"] {
  --bg: #f6f7fb;
  --bg-elev: #ffffff;
  --bg-elev-2: #f0f2f8;
  --bg-hover: #eceef5;
  --surface: #ffffff;
  --border: #e3e6ef;
  --text: #1a1c24;
  --text-dim: #5a6072;
  --text-faint: #8b91a3;
  --bubble-user: linear-gradient(135deg, #6c63ff 0%, #5a6cff 100%);
  --bubble-user-text: #fff;
  --code-bg: #1b1e27;
  --think-bg: #f4f6fc;
  --think-border: #e0e4f0;
  --shadow: 0 10px 40px -16px rgba(60,70,120,.25);
}

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

html, body { height: 100%; }

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

.ic { width: 20px; height: 20px; fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform .28s cubic-bezier(.4,0,.2,1);
  z-index: 40;
}
.sidebar-head { display: flex; gap: 8px; padding: 14px; }
.btn-new {
  flex: 1;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 14px;
  background: var(--accent-grad);
  color: #fff; border: none; border-radius: var(--radius-sm);
  font-family: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
  box-shadow: 0 4px 16px -4px rgba(108,99,255,.5);
  transition: transform .12s, filter .12s;
}
.btn-new:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn-new:active { transform: translateY(0); }

.history { flex: 1; overflow-y: auto; padding: 6px 10px; }
.history-item {
  display: flex; align-items: center; gap: 9px;
  padding: 10px 11px; margin-bottom: 3px;
  border-radius: var(--radius-sm); cursor: pointer;
  color: var(--text-dim); font-size: 13.5px;
  transition: background .14s, color .14s;
  position: relative;
}
.history-item:hover { background: var(--bg-hover); color: var(--text); }
.history-item.active { background: var(--bg-hover); color: var(--text); }
.history-item .ti-title {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.history-item .ti-del {
  opacity: 0; flex-shrink: 0; width: 26px; height: 26px;
  display: grid; place-items: center; border-radius: 7px;
  color: var(--text-faint); transition: opacity .14s, background .14s, color .14s;
}
.history-item:hover .ti-del { opacity: 1; }
.history-item .ti-del:hover { background: rgba(255,80,80,.15); color: #ff6b6b; }
.history-item .ti-del .ic { width: 15px; height: 15px; }

.history-day {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .04em; color: var(--text-faint);
  padding: 12px 12px 5px;
}

.sidebar-foot { padding: 12px 14px; border-top: 1px solid var(--border); }
.conn { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--text-dim); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-faint); flex-shrink: 0; }
.dot.ok { background: #29d27e; box-shadow: 0 0 0 3px rgba(41,210,126,.18); }
.dot.bad { background: #ff5d5d; box-shadow: 0 0 0 3px rgba(255,93,93,.18); }
.dot.wait { background: #ffb020; animation: pulse 1.2s infinite; }

@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.35} }

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(12px);
  z-index: 10;
}
.model-pick {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 0 12px;
  color: var(--text-dim);
}
.model-pick .ic { width: 17px; height: 17px; color: var(--accent); }
#modelSelect {
  background: transparent; border: none; outline: none;
  font-family: inherit; font-size: 13.5px; font-weight: 500;
  color: var(--text); padding: 9px 4px; cursor: pointer; max-width: 240px;
}
#modelSelect option { background: var(--bg-elev); color: var(--text); }
.topbar-actions { margin-left: auto; display: flex; gap: 6px; }

.icon-btn {
  width: 40px; height: 40px; flex-shrink: 0;
  display: grid; place-items: center;
  background: transparent; border: 1px solid transparent; border-radius: 10px;
  color: var(--text-dim); cursor: pointer; transition: background .14s, color .14s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }

/* ---------- Chat area ---------- */
.chat {
  flex: 1; overflow-y: auto; scroll-behavior: smooth;
  padding: 24px 0 8px;
}
.chat-inner, .msg-row { max-width: 820px; margin: 0 auto; padding: 0 20px; }

.msg-row { display: flex; gap: 14px; padding-top: 22px; padding-bottom: 22px; }
.msg-row + .msg-row { border-top: 1px solid color-mix(in srgb, var(--border) 60%, transparent); }

.avatar {
  width: 34px; height: 34px; flex-shrink: 0; border-radius: 10px;
  display: grid; place-items: center; font-size: 15px; font-weight: 700;
}
.avatar.user { background: var(--bg-elev-2); color: var(--text); }
.avatar.bot { background: var(--accent-grad); color: #fff; }
.avatar .ic { width: 19px; height: 19px; }

.msg-body { flex: 1; min-width: 0; padding-top: 4px; }
.msg-name { font-size: 13px; font-weight: 600; color: var(--text-dim); margin-bottom: 6px; }

/* Markdown content */
.content { font-size: 15px; line-height: 1.7; color: var(--text); word-wrap: break-word; }
.content > *:first-child { margin-top: 0; }
.content > *:last-child { margin-bottom: 0; }
.content p { margin: 0 0 12px; }
.content h1,.content h2,.content h3 { margin: 18px 0 10px; line-height: 1.3; }
.content h1 { font-size: 1.5em; } .content h2 { font-size: 1.3em; } .content h3 { font-size: 1.12em; }
.content ul,.content ol { margin: 0 0 12px; padding-left: 24px; }
.content li { margin: 4px 0; }
.content a { color: var(--accent-2); text-decoration: none; }
.content a:hover { text-decoration: underline; }
.content blockquote {
  border-left: 3px solid var(--accent); margin: 0 0 12px;
  padding: 4px 14px; color: var(--text-dim); background: var(--bg-elev-2);
  border-radius: 0 8px 8px 0;
}
.content table { border-collapse: collapse; width: 100%; margin: 0 0 14px; font-size: 14px; display: block; overflow-x: auto; }
.content th,.content td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.content th { background: var(--bg-elev-2); font-weight: 600; }
.content :not(pre) > code {
  font-family: var(--font-mono); font-size: .88em;
  background: var(--bg-elev-2); padding: 2px 6px; border-radius: 6px;
  color: var(--accent-2);
}
.content hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }

/* User bubble */
.msg-row.user .content {
  background: var(--bubble-user); color: var(--bubble-user-text);
  padding: 12px 16px; border-radius: 16px 16px 16px 4px;
  display: inline-block; max-width: 100%;
}
.msg-row.user .avatar.bot { display: none; }

/* Code blocks */
.code-wrap { position: relative; margin: 0 0 14px; border-radius: 12px; overflow: hidden; border: 1px solid var(--border); }
.code-head {
  display: flex; align-items: center; justify-content: space-between;
  background: #0f1117; padding: 7px 12px; font-size: 12px; color: #9aa0ad;
  font-family: var(--font-mono); border-bottom: 1px solid #1d2129;
}
.code-copy {
  display: flex; align-items: center; gap: 5px;
  background: transparent; border: none; color: #9aa0ad; cursor: pointer;
  font-family: inherit; font-size: 12px; padding: 3px 6px; border-radius: 6px;
  transition: background .14s, color .14s;
}
.code-copy:hover { background: #1d2129; color: #fff; }
.code-copy .ic { width: 14px; height: 14px; }
.code-wrap pre { margin: 0; padding: 14px 16px; overflow-x: auto; background: var(--code-bg); }
.code-wrap pre code { font-family: var(--font-mono); font-size: 13.5px; line-height: 1.6; background: none; padding: 0; }

/* ---------- Thinking block ---------- */
.think {
  margin: 0 0 14px;
  border: 1px solid var(--think-border);
  background: var(--think-bg);
  border-radius: 12px; overflow: hidden;
}
.think-head {
  display: flex; align-items: center; gap: 9px;
  padding: 11px 14px; cursor: pointer; user-select: none;
  font-size: 13.5px; font-weight: 600; color: var(--text-dim);
  transition: background .14s;
}
.think-head:hover { background: color-mix(in srgb, var(--accent) 6%, transparent); }
.think-head .brain { width: 17px; height: 17px; color: var(--accent); flex-shrink: 0; }
.think-head .chev { width: 16px; height: 16px; margin-left: auto; transition: transform .2s; color: var(--text-faint); }
.think.open .think-head .chev { transform: rotate(180deg); }
.think-head .spinner { width: 14px; height: 14px; border: 2px solid var(--accent); border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.think-body {
  display: grid; grid-template-rows: 0fr;
  transition: grid-template-rows .25s ease;
}
.think.open .think-body { grid-template-rows: 1fr; }
.think-body-inner { overflow: hidden; }
.think-text {
  padding: 0 16px 14px; font-size: 13.5px; line-height: 1.65;
  color: var(--text-dim); white-space: pre-wrap; word-wrap: break-word;
  border-top: 1px solid var(--think-border);
  padding-top: 12px;
}

/* Typing cursor */
.cursor { display: inline-block; width: 8px; height: 16px; background: var(--accent);
  vertical-align: text-bottom; border-radius: 2px; animation: blink 1s step-end infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- Welcome ---------- */
.welcome { max-width: 680px; margin: auto; padding: 40px 24px; text-align: center;
  display: flex; flex-direction: column; align-items: center; height: 100%;
  justify-content: center; }
.welcome .logo {
  width: 68px; height: 68px; border-radius: 20px; display: grid; place-items: center;
  background: var(--accent-grad); color: #fff; margin-bottom: 22px;
  box-shadow: 0 12px 36px -10px rgba(46,155,255,.55);
}
.welcome .logo .ic { width: 34px; height: 34px; }
.welcome h1 { font-size: 28px; font-weight: 700; margin-bottom: 8px; }
.welcome p { color: var(--text-dim); font-size: 15px; margin-bottom: 28px; }
.suggests { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; width: 100%; }
.chip {
  text-align: left; padding: 14px 16px; border-radius: 12px;
  background: var(--bg-elev); border: 1px solid var(--border); color: var(--text);
  font-family: inherit; font-size: 13.5px; cursor: pointer; line-height: 1.45;
  transition: border-color .14s, transform .12s, background .14s;
}
.chip:hover { border-color: var(--accent); transform: translateY(-2px); background: var(--bg-elev-2); }

/* ---------- Composer ---------- */
.composer { padding: 10px 0 16px; }
.composer-inner {
  max-width: 820px; margin: 0 auto; padding: 8px 8px 8px 18px;
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 22px; box-shadow: var(--shadow);
  margin-left: 20px; margin-right: 20px;
  transition: border-color .15s;
}
.composer-inner:focus-within { border-color: var(--accent); }
#input {
  flex: 1; resize: none; border: none; outline: none; background: transparent;
  font-family: inherit; font-size: 15px; line-height: 1.55; color: var(--text);
  max-height: 200px; padding: 9px 0;
}
#input::placeholder { color: var(--text-faint); }
.send {
  width: 42px; height: 42px; flex-shrink: 0; border: none; border-radius: 14px;
  background: var(--accent-grad); color: #fff; cursor: pointer;
  display: grid; place-items: center; transition: filter .14s, opacity .14s, transform .12s;
}
.send:hover { filter: brightness(1.1); }
.send:active { transform: scale(.94); }
.send:disabled { opacity: .4; cursor: not-allowed; }
.send.stop { background: #ff5d5d; }
.hint { text-align: center; font-size: 11.5px; color: var(--text-faint); margin-top: 10px; padding: 0 20px; }
.hide { display: none !important; }

/* ---------- Mobile / responsive ---------- */
.scrim { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 35; opacity: 0; transition: opacity .25s; }
.only-mobile { display: none; }

@media (max-width: 860px) {
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; top: 0; left: 0; height: 100dvh;
    transform: translateX(-100%); box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .scrim.show { display: block; opacity: 1; }
  #modelSelect { max-width: 150px; }
  .welcome h1 { font-size: 23px; }
  .suggests { grid-template-columns: 1fr; }
  .composer-inner, .msg-row, .chat-inner { margin-left: 12px; margin-right: 12px; }
  .msg-row { padding-left: 0; padding-right: 0; }
  .chat-inner, .msg-row { padding-left: 14px; padding-right: 14px; }
}

@media (max-width: 480px) {
  .topbar { padding: 10px 12px; gap: 8px; }
  .content { font-size: 14.5px; }
  .avatar { width: 30px; height: 30px; }
}

/* ---------- Scrollbar ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }
