/* TinyLLaMA chat UI. Colors are variables, redefined for the dark theme. */
:root {
  --bg: #ffffff;
  --bg-soft: #f6f6f8;
  --sidebar: #f3f3f6;
  --panel: #ffffff;
  --border: #e4e4ea;
  --text: #1c1c22;
  --muted: #6b6b78;
  --accent: #6d5dfc;
  --accent-soft: #eeebff;
  --accent-text: #ffffff;
  --user-bubble: #eeebff;
  --ok: #1f9d55;
  --bad: #c2413b;
  --code-bg: #f4f4f7;
  --shadow: 0 6px 24px rgba(20, 20, 40, .08);
  --radius: 14px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #16161b;
  --bg-soft: #1d1d24;
  --sidebar: #111116;
  --panel: #1f1f27;
  --border: #2d2d38;
  --text: #ececf1;
  --muted: #9a9aa8;
  --accent: #8b7dff;
  --accent-soft: #2a2645;
  --accent-text: #ffffff;
  --user-bubble: #2a2645;
  --ok: #4ccf7d;
  --bad: #ff7a72;
  --code-bg: #15151b;
  --shadow: 0 6px 24px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, input, textarea { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
a { color: var(--accent); }

.app { display: grid; grid-template-columns: 272px 1fr; height: 100dvh; }

/* ---------- sidebar ---------- */
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-top { padding: 16px 14px 8px; display: grid; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo, .empty-logo, .avatar {
  display: grid; place-items: center; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #b05dfc); color: #fff; font-weight: 700;
}
.logo { width: 34px; height: 34px; font-size: 14px; }
.brand-name { font-weight: 700; }
.brand-sub { font-size: 12px; color: var(--muted); line-height: 1.2; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--panel);
  padding: 8px 14px; cursor: pointer; transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn-new { width: 100%; font-weight: 600; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; }

.search {
  display: flex; align-items: center; gap: 8px; padding: 7px 10px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--panel); color: var(--muted);
}
.search input { border: 0; outline: 0; background: transparent; width: 100%; color: var(--text); }
.search:focus-within { border-color: var(--accent); }

.chat-list { flex: 1; overflow-y: auto; padding: 4px 8px; min-height: 0; }
.list-label { font-size: 12px; color: var(--muted); padding: 10px 8px 4px; }
.chat-item {
  display: flex; align-items: center; gap: 6px; width: 100%;
  padding: 8px 10px; border-radius: 9px; border: 0; background: transparent; cursor: pointer; text-align: left;
}
.chat-item:hover { background: var(--bg-soft); }
.chat-item.active { background: var(--accent-soft); }
.chat-title { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 14px; }
.chat-del { opacity: 0; border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 2px; border-radius: 6px; }
.chat-item:hover .chat-del, .chat-del:focus { opacity: 1; }
.chat-del:hover { color: var(--bad); }
.list-empty { color: var(--muted); font-size: 13px; padding: 10px; }

.sidebar-bottom { border-top: 1px solid var(--border); padding: 8px; display: grid; gap: 2px; }
.side-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 9px;
  border: 0; background: transparent; cursor: pointer; text-decoration: none; color: var(--text); font-size: 14px;
}
.side-link:hover { background: var(--bg-soft); }

/* ---------- main ---------- */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
}
.model-pill {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 999px;
  background: var(--bg-soft); border: 1px solid var(--border); font-weight: 600; font-size: 14px; min-width: 0;
}
.model-pill #modelName { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.pill-meta { color: var(--muted); font-weight: 400; white-space: nowrap; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status-dot.ok { background: var(--ok); }
.status-dot.down { background: var(--bad); }
.icon-btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid transparent; background: transparent;
  padding: 7px 9px; border-radius: 9px; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: var(--bg-soft); border-color: var(--border); }
.only-mobile { display: none; }

.conversation { flex: 1; overflow-y: auto; min-height: 0; }
.messages, .empty { max-width: 780px; margin: 0 auto; padding: 24px 20px; }

.empty { text-align: center; padding-top: 10vh; }
.empty-logo { width: 56px; height: 56px; margin: 0 auto 16px; font-size: 20px; border-radius: 16px; }
.empty h1 { margin: 0 0 8px; font-size: 28px; letter-spacing: -.02em; }
.empty p { margin: 0 auto 28px; color: var(--muted); max-width: 560px; }
.examples { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }
.example {
  text-align: left;
  border: 1px solid var(--border); background: var(--panel); border-radius: 12px; padding: 12px 14px;
  cursor: pointer; transition: border-color .15s, transform .15s;
}
.example:hover { border-color: var(--accent); transform: translateY(-1px); }
.example strong { display: block; font-size: 14px; }
.example span { color: var(--muted); font-size: 13px; }

.msg { display: flex; gap: 12px; margin: 0 0 22px; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 14px; border-radius: 16px 16px 4px 16px; max-width: 80%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.avatar { width: 30px; height: 30px; font-size: 12px; margin-top: 2px; border-radius: 9px; }
.msg.assistant .body { flex: 1; min-width: 0; }
.msg.assistant .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant.streaming .text::after {
  content: ""; display: inline-block; width: 8px; height: 16px; margin-left: 2px; vertical-align: -2px;
  background: var(--accent); border-radius: 2px; animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.msg.assistant .text.error { color: var(--bad); }

.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; font-size: 12.5px; color: var(--muted); }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); }
.chip.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.chip.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }
.chip.words { font-weight: 600; color: var(--text); }
.meta .stat { margin-left: 2px; }
.meta .actions { margin-left: auto; display: flex; gap: 2px; }
.meta .actions button { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 4px; border-radius: 7px; }
.meta .actions button:hover { color: var(--text); background: var(--bg-soft); }
.meta .actions svg { width: 16px; height: 16px; }

/* ---------- composer ---------- */
.composer-wrap { padding: 8px 20px 14px; }
.composer {
  max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  border: 1px solid var(--border); border-radius: 18px; background: var(--panel); padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; padding: 7px 0;
  max-height: 200px; line-height: 1.5;
}
.send {
  width: 38px; height: 38px; border-radius: 12px; border: 0; cursor: pointer; flex: none;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send:disabled { opacity: .4; cursor: default; }
.send .i-stop { display: none; fill: currentColor; stroke: none; }
.busy .send .i-stop { display: block; }
.busy .send .i-send { display: none; }
.composer-note { text-align: center; color: var(--muted); font-size: 12px; margin-top: 8px; }

/* ---------- dialogs ---------- */
.modal {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; width: min(460px, calc(100vw - 32px));
  background: var(--panel); color: var(--text); box-shadow: var(--shadow);
}
.modal-wide { width: min(720px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(10, 10, 20, .45); backdrop-filter: blur(2px); }
/* minmax(0, 1fr): a long code line scrolls inside its block instead of widening the dialog */
.modal-body { padding: 20px 22px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px;
              max-height: calc(100dvh - 64px); overflow-y: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal h3 { margin: 6px 0 -4px; font-size: 14px; }
.modal-foot { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; }
.field { display: grid; gap: 6px; }
.field > span { font-weight: 600; font-size: 14px; display: flex; justify-content: space-between; }
.field output { color: var(--accent); font-variant-numeric: tabular-nums; }
.field small { color: var(--muted); font-size: 12.5px; }
.field input[type="range"] { accent-color: var(--accent); }
.field input[type="number"], .field input[type="password"] {
  border: 1px solid var(--border); border-radius: 9px; padding: 8px 10px; background: var(--bg);
}
.muted { color: var(--muted); margin: 0; }
.small { font-size: 13px; }
.kv { display: grid; gap: 8px; }
.kv > div { display: flex; align-items: center; gap: 10px; }
.kv span { width: 70px; color: var(--muted); font-size: 13px; flex: none; }
.kv code { flex: 1; min-width: 0; overflow-x: auto; white-space: nowrap; }
code { font-family: ui-monospace, "Cascadia Code", Menlo, monospace; font-size: 13px; background: var(--code-bg); padding: 3px 6px; border-radius: 6px; }
.snippet { position: relative; }
.snippet pre { margin: 0; background: var(--code-bg); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; overflow-x: auto; }
.snippet pre code { background: transparent; padding: 0; white-space: pre; }
.snippet .copy { position: absolute; top: 8px; right: 8px; }
.copy { border: 1px solid var(--border); background: var(--panel); border-radius: 7px; padding: 3px 9px; font-size: 12px; cursor: pointer; }
.copy:hover { border-color: var(--accent); }

/* elements for one mode only (window.TL_CONFIG.mode, set on <html> as data-mode) */
[data-mode="browser"] .server-only, [data-mode="server"] .browser-only { display: none !important; }

.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-size: 14px;
  box-shadow: var(--shadow); z-index: 20; max-width: calc(100vw - 32px);
}

/* ---------- phones ---------- */
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 280px; z-index: 15;
    transform: translateX(-100%); transition: transform .2s ease;
  }
  .app.sidebar-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .scrim { position: fixed; inset: 0; background: rgba(10, 10, 20, .4); z-index: 14; }
  .only-mobile { display: inline-flex; }
  .hide-mobile { display: none; }
  .examples { grid-template-columns: 1fr; }
  .messages, .empty { padding: 18px 16px; }
  .empty { padding-top: 6vh; }
  .composer-wrap { padding: 6px 12px 10px; }
  .pill-meta { display: none; }
}
