/* ────────────────────────────────────────────────────────────────
   Typhoon Chat — "night monsoon" theme
   ink navy · storm slate · monsoon teal accent
──────────────────────────────────────────────────────────────── */
:root {
  --bg: #0b1017;
  --bg-2: #0e141d;
  --surface: #121a26;
  --raised: #182130;
  --raised-2: #1e293b;
  --border: #223047;
  --border-soft: #1b2536;
  --text: #e8eef6;
  --muted: #93a1b5;
  --faint: #5d6b80;
  --accent: #3bd1c5;
  --accent-strong: #56e3d8;
  --accent-dim: #178f86;
  --accent-ink: #062a27;
  --user-bubble: #1f3448;
  --danger: #f27d72;
  --radius: 14px;
  --font: "IBM Plex Sans Thai", system-ui, -apple-system, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sidebar-w: 288px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15.5px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
.hidden { display: none !important; }

.app { display: flex; height: 100dvh; overflow: hidden; }

/* ───────────── sidebar ───────────── */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-2);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  transition: transform .25s ease;
  z-index: 30;
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 14px 10px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.brand-mark { width: 26px; height: 26px; color: var(--accent); }
#btn-close-sidebar { display: none; }

.new-chat {
  margin: 6px 12px 12px;
  display: flex; align-items: center; gap: 8px; justify-content: center;
  padding: 10px 14px;
  background: linear-gradient(135deg, var(--accent-dim), var(--accent));
  color: var(--accent-ink);
  border: none; border-radius: 10px;
  font-weight: 600; font-size: 14.5px;
  transition: filter .15s ease, transform .1s ease;
}
.new-chat:hover { filter: brightness(1.08); }
.new-chat:active { transform: scale(.985); }
.new-chat .plus { font-size: 17px; font-weight: 700; line-height: 1; }

.chat-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.chat-item {
  display: flex; align-items: center; gap: 6px;
  padding: 9px 10px; margin-bottom: 2px;
  border-radius: 9px;
  color: var(--muted); font-size: 14px;
  border: 1px solid transparent;
  transition: background .12s ease;
}
.chat-item:hover { background: var(--surface); color: var(--text); }
.chat-item.active { background: var(--raised); color: var(--text); border-color: var(--border-soft); }
.chat-item .title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  cursor: pointer; min-width: 0;
}
.chat-item .del {
  background: none; border: none; color: var(--faint);
  font-size: 15px; padding: 2px 5px; border-radius: 6px;
  opacity: 0; transition: opacity .12s ease;
}
.chat-item:hover .del { opacity: 1; }
.chat-item .del:hover { color: var(--danger); background: rgba(242,125,114,.1); }

.sidebar-foot { padding: 12px 14px 16px; border-top: 1px solid var(--border-soft); }
.model-label { display: block; font-size: 12px; color: var(--faint); margin-bottom: 6px; letter-spacing: .4px; }
.model-select {
  width: 100%;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 9px;
  padding: 8px 10px; font-family: inherit; font-size: 13.5px;
}

.scrim {
  display: none; position: fixed; inset: 0;
  background: rgba(4, 8, 14, .6); z-index: 25;
}

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

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-soft);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.topbar-title {
  font-weight: 600; font-size: 14.5px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-spacer { flex: 1; }
#btn-open-sidebar { display: none; }

.icon-btn {
  background: none; border: none; color: var(--muted);
  padding: 7px; border-radius: 8px; line-height: 0;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }

.scroll { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.scroll-pad { height: 24px; }

/* ───────────── welcome ───────────── */
.welcome {
  max-width: 680px; margin: 0 auto;
  padding: 9vh 24px 24px;
  text-align: center; position: relative;
}
.eye {
  position: absolute; top: 2vh; left: 50%; transform: translateX(-50%);
  width: 420px; height: 420px; border-radius: 50%;
  background: radial-gradient(circle, rgba(59,209,197,.14), transparent 62%);
  pointer-events: none;
  animation: breathe 7s ease-in-out infinite;
}
@keyframes breathe { 50% { opacity: .55; transform: translateX(-50%) scale(1.06); } }
.welcome-mark {
  width: 58px; height: 58px; color: var(--accent);
  animation: spin 14s linear infinite;
  position: relative;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .welcome-mark, .eye { animation: none; }
}
.welcome h1 { font-size: 26px; font-weight: 700; margin: 18px 0 8px; position: relative; }
.welcome-sub { color: var(--muted); margin: 0 0 28px; position: relative; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; position: relative; }
.chip {
  background: var(--surface); color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 9px 16px; font-size: 13.5px;
  transition: border-color .15s ease, color .15s ease, background .15s ease;
}
.chip:hover { border-color: var(--accent-dim); color: var(--text); background: var(--raised); }

/* ───────────── messages ───────────── */
.thread { max-width: 780px; margin: 0 auto; padding: 20px 20px 0; }
.msg { display: flex; gap: 12px; margin-bottom: 26px; }
.msg .avatar {
  width: 30px; height: 30px; flex-shrink: 0; border-radius: 9px;
  display: grid; place-items: center; margin-top: 2px;
  font-size: 13px; font-weight: 700;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .avatar { background: var(--user-bubble); color: #9fc3de; }
.msg.assistant .avatar { background: var(--raised); color: var(--accent); border: 1px solid var(--border-soft); }
.msg .avatar svg { width: 18px; height: 18px; }

.bubble { min-width: 0; max-width: 100%; }
.msg.user .bubble {
  background: var(--user-bubble);
  border-radius: var(--radius) 4px var(--radius) var(--radius);
  padding: 10px 15px;
  white-space: pre-wrap; word-wrap: break-word;
  max-width: 78%;
}
.msg.assistant .bubble { flex: 1; padding-top: 3px; }

.msg-actions { display: flex; gap: 4px; margin-top: 6px; opacity: 0; transition: opacity .15s ease; }
.msg:hover .msg-actions { opacity: 1; }
.msg-actions button {
  background: none; border: none; color: var(--faint);
  font-size: 12px; padding: 3px 8px; border-radius: 6px;
}
.msg-actions button:hover { background: var(--surface); color: var(--text); }

.cursor {
  display: inline-block; width: 8px; height: 17px;
  background: var(--accent); border-radius: 2px;
  vertical-align: text-bottom; margin-left: 2px;
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: .15; } }

.error-note {
  color: var(--danger); background: rgba(242,125,114,.08);
  border: 1px solid rgba(242,125,114,.25);
  border-radius: 10px; padding: 9px 13px; font-size: 13.5px;
  margin-top: 8px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.error-note .retry {
  background: none; border: 1px solid rgba(242,125,114,.4);
  color: var(--danger); border-radius: 7px; padding: 3px 12px; font-size: 12.5px;
}
.error-note .retry:hover { background: rgba(242,125,114,.12); }

/* ── markdown rendering ── */
.md p { margin: 0 0 .8em; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: .4em 0 .9em; padding-left: 1.5em; }
.md li { margin: .25em 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.1em 0 .45em; line-height: 1.35; }
.md h1 { font-size: 1.3em; } .md h2 { font-size: 1.18em; } .md h3 { font-size: 1.07em; }
.md a { color: var(--accent); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md blockquote {
  margin: .7em 0; padding: .3em 1em;
  border-left: 3px solid var(--accent-dim); color: var(--muted);
}
.md hr { border: none; border-top: 1px solid var(--border); margin: 1.2em 0; }
.md table { border-collapse: collapse; margin: .8em 0; width: 100%; font-size: .93em; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 11px; text-align: left; }
.md th { background: var(--surface); }
.md code {
  font-family: var(--mono); font-size: .87em;
  background: var(--raised); border: 1px solid var(--border-soft);
  padding: .12em .4em; border-radius: 5px;
}
.md pre {
  background: #0d131c; border: 1px solid var(--border-soft);
  border-radius: 11px; margin: .8em 0; overflow: hidden;
}
.md pre code {
  display: block; background: none; border: none;
  padding: 14px 16px; overflow-x: auto; font-size: 13px; line-height: 1.6;
}
.code-head {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--surface); border-bottom: 1px solid var(--border-soft);
  padding: 5px 12px; font-size: 11.5px; color: var(--faint);
  font-family: var(--mono); letter-spacing: .3px;
}
.code-head button {
  background: none; border: none; color: var(--faint);
  font-size: 11.5px; font-family: var(--font); padding: 2px 8px; border-radius: 5px;
}
.code-head button:hover { color: var(--text); background: var(--raised); }

/* ───────────── composer ───────────── */
.composer-wrap {
  padding: 8px 16px calc(14px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 40%);
}
.composer {
  max-width: 780px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 10px 10px 10px 18px;
  transition: border-color .15s ease, box-shadow .15s ease;
}
.composer:focus-within {
  border-color: var(--accent-dim);
  box-shadow: 0 0 0 3px rgba(59, 209, 197, .1);
}
#input {
  flex: 1; background: none; border: none; outline: none; resize: none;
  color: var(--text); font-family: inherit; font-size: 15.5px; line-height: 1.55;
  max-height: 200px; padding: 4px 0;
}
#input::placeholder { color: var(--faint); }
.send-btn {
  flex-shrink: 0;
  width: 38px; height: 38px; border-radius: 12px;
  border: none; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
  transition: filter .15s ease, opacity .15s ease, transform .1s ease;
}
.send-btn:hover { filter: brightness(1.1); }
.send-btn:active { transform: scale(.94); }
.send-btn:disabled { opacity: .35; cursor: default; filter: none; }
.send-btn.stop { background: var(--raised-2); color: var(--danger); border: 1px solid var(--border); }
.composer-hint {
  max-width: 780px; margin: 8px auto 0;
  text-align: center; font-size: 11.5px; color: var(--faint);
}

/* ───────────── scrollbars ───────────── */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--raised-2); border-radius: 5px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--border); }

/* ───────────── focus visibility ───────────── */
button:focus-visible, select:focus-visible, textarea:focus-visible, .chip:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

/* ───────────── mobile ───────────── */
@media (max-width: 860px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; height: 100dvh;
    transform: translateX(-100%);
    box-shadow: 12px 0 40px rgba(0,0,0,.45);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar.open ~ .scrim, .scrim.show { display: block; }
  #btn-open-sidebar, #btn-close-sidebar { display: grid; place-items: center; }
  .msg.user .bubble { max-width: 88%; }
  .welcome { padding-top: 6vh; }
  .welcome h1 { font-size: 21px; }
  .eye { width: 300px; height: 300px; }
}
