/* chat_llm web UI: WhatsApp-style, mobile first. No external fonts or libraries (it must work on the LAN with no internet). */
:root {
  --green: #008069;
  --green-dark: #075e54;
  --accent: #25d366;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --header: #008069;
  --header-text: #ffffff;
  --chat-bg: #efeae2;
  --bubble-me: #d9fdd3;
  --bubble-them: #ffffff;
  --text: #111b21;
  --muted: #667781;
  --line: #e9edef;
  --quote-bg: rgba(11, 20, 26, 0.06);
  --link: #027eb5;
  --system: #fff5c4;
  --shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13);
  --input-bg: #ffffff;
  --composer-bg: #f0f2f5;
  --hover: #f5f6f6;
  --active: #f0f2f5;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --header: #202c33;
    --header-text: #e9edef;
    --chat-bg: #0b141a;
    --bubble-me: #005c4b;
    --bubble-them: #202c33;
    --text: #e9edef;
    --muted: #8696a0;
    --line: #222d34;
    --quote-bg: rgba(255, 255, 255, 0.07);
    --link: #53bdeb;
    --system: #182229;
    --shadow: 0 1px 0.5px rgba(11, 20, 26, 0.5);
    --input-bg: #2a3942;
    --composer-bg: #202c33;
    --hover: #202c33;
    --active: #2a3942;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px; line-height: 1.35; color: var(--text); background: var(--bg);
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
input, textarea { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: currentColor; display: block; }

/* ---------------------------------------------------------------- login */
#login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(var(--green) 0 220px, var(--bg) 220px); }
.login-card { width: 100%; max-width: 420px; background: var(--panel); border-radius: 12px; padding: 28px 24px; box-shadow: 0 6px 24px rgba(11, 20, 26, 0.18); }
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 18px; color: var(--muted); }
.login-card label { display: block; font-size: 13px; color: var(--green); margin-bottom: 6px; }
.login-card input { width: 100%; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--input-bg); }
.login-card input:focus, #search:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.login-card .error { color: #d93025; min-height: 1.4em; margin: 8px 0; font-size: 14px; }
.btn-primary { width: 100%; padding: 12px; border-radius: 24px; background: var(--green); color: #fff; font-weight: 600; }
.btn-primary:disabled { opacity: 0.6; }

/* ---------------------------------------------------------------- layout */
#app { height: 100dvh; display: flex; max-width: 1600px; margin: 0 auto; background: var(--panel); position: relative; overflow: hidden; }
#sidebar { width: 380px; flex: none; display: flex; flex-direction: column; border-right: 1px solid var(--line); min-height: 0; background: var(--panel); }
#conv { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); position: relative; }
.bar { flex: none; height: 59px; padding: 0 12px; padding-top: env(safe-area-inset-top); box-sizing: content-box; display: flex; align-items: center; gap: 12px; background: var(--header); color: var(--header-text); }
.bar-actions { margin-left: auto; display: flex; gap: 4px; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 50%; }
.icon-btn:active, .icon-btn:hover { background: rgba(255, 255, 255, 0.15); }
.whoami { display: flex; align-items: center; gap: 10px; min-width: 0; font-weight: 600; }
.whoami span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.avatar { flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 15px; text-transform: uppercase; user-select: none; }
.search { padding: 8px 12px; border-bottom: 1px solid var(--line); }
#search { width: 100%; padding: 9px 14px; border: 0; border-radius: 8px; background: var(--composer-bg); }
#chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.chat-row { display: flex; gap: 12px; padding: 10px 14px; align-items: center; cursor: pointer; border-bottom: 1px solid var(--line); }
.chat-row:hover { background: var(--hover); }
.chat-row.active { background: var(--active); }
.chat-row .avatar { width: 49px; height: 49px; }
.chat-main { flex: 1; min-width: 0; }
.chat-top, .chat-bottom { display: flex; align-items: center; gap: 8px; }
.chat-title { flex: 1; font-size: 16px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-time { flex: none; font-size: 12px; color: var(--muted); }
.chat-row.unread .chat-time { color: var(--accent); }
.chat-preview { flex: 1; font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: #0b141a; font-size: 12px; font-weight: 700; display: grid; place-items: center; }
#chat-empty, #conv-empty { color: var(--muted); text-align: center; padding: 32px 24px; }
#conv-empty { margin: auto; max-width: 420px; }
#conv-empty h2 { color: var(--text); font-weight: 400; }

/* ---------------------------------------------------------------- conversation */
#conv-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.conv-head { cursor: default; }
.conv-title { min-width: 0; flex: 1; }
.conv-title b { display: block; font-size: 16px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-title small { display: block; font-size: 13px; opacity: 0.75; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }
#messages {
  flex: 1; min-height: 0; overflow-y: auto; padding: 12px 8% 8px; -webkit-overflow-scrolling: touch;
  background-image: radial-gradient(rgba(127, 127, 127, 0.07) 1px, transparent 1px); background-size: 22px 22px;
}
.day { text-align: center; margin: 12px 0; }
.day span, .unread-divider span { display: inline-block; background: var(--panel); color: var(--muted); font-size: 12.5px; padding: 5px 12px; border-radius: 8px; box-shadow: var(--shadow); }
.unread-divider { text-align: center; margin: 10px 0; }
.unread-divider span { background: var(--system); }
#load-older { display: block; margin: 4px auto 10px; padding: 6px 14px; border-radius: 16px; background: var(--panel); color: var(--link); font-size: 13px; box-shadow: var(--shadow); }
.msg { display: flex; margin-bottom: 2px; }
.msg.me { justify-content: flex-end; }
.msg.first { margin-top: 8px; }
.bubble { position: relative; max-width: min(78%, 620px); padding: 6px 8px 6px 9px; border-radius: 8px; box-shadow: var(--shadow); background: var(--bubble-them); word-wrap: break-word; overflow-wrap: anywhere; min-width: 64px; }
.me .bubble { background: var(--bubble-me); }
.msg.first .bubble { border-top-left-radius: 0; }
.msg.me.first .bubble { border-top-left-radius: 8px; border-top-right-radius: 0; }
.author { font-size: 13px; font-weight: 600; margin-bottom: 1px; }
.text { white-space: normal; }
.text code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em; background: var(--quote-bg); padding: 1px 4px; border-radius: 4px; }
.text pre { margin: 4px 0; padding: 8px; background: var(--quote-bg); border-radius: 6px; overflow-x: auto; white-space: pre; }
.text pre code { background: none; padding: 0; }
.text a { color: var(--link); }
.mention { color: var(--link); font-weight: 500; }
.mention.self { background: rgba(37, 211, 102, 0.22); border-radius: 4px; padding: 0 2px; }
.meta { float: right; margin: 6px 0 -3px 10px; font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 6px; line-height: 1; }
.reply-btn { opacity: 0; width: 18px; height: 18px; color: var(--muted); }
.reply-btn svg { width: 16px; height: 16px; }
.bubble:hover .reply-btn { opacity: 1; }
@media (hover: none) { .reply-btn { opacity: 0.55; } }
.quote { border-left: 4px solid var(--accent); background: var(--quote-bg); border-radius: 6px; padding: 4px 8px; margin-bottom: 4px; cursor: pointer; font-size: 13px; }
.quote b { display: block; font-size: 12.5px; }
.quote span { display: block; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 46ch; }
.flash .bubble { animation: flash 1.4s ease-out; }
@keyframes flash { 0% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: var(--shadow); } }

.atts { display: flex; flex-direction: column; gap: 4px; margin-bottom: 4px; }
.att-img { display: block; max-width: 100%; max-height: 340px; min-height: 90px; min-width: 140px; border-radius: 6px; background: var(--quote-bg); cursor: zoom-in; object-fit: cover; }
.att-card { display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--quote-bg); border-radius: 6px; cursor: pointer; text-align: left; width: 100%; }
.att-card svg { width: 30px; height: 30px; color: var(--muted); flex: none; }
.att-card .nm { min-width: 0; flex: 1; }
.att-card .nm b { display: block; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.att-card .nm small { color: var(--muted); font-size: 12px; }
.att-video { width: 100%; max-height: 360px; border-radius: 6px; background: #000; }

#to-bottom { position: absolute; right: 16px; bottom: 80px; z-index: 5; width: 42px; height: 42px; border-radius: 50%; background: var(--panel); box-shadow: 0 2px 6px rgba(11, 20, 26, 0.3); display: grid; place-items: center; color: var(--muted); }
#to-bottom .badge { position: absolute; top: -8px; }

/* ---------------------------------------------------------------- composer */
#reply-preview, #files-preview { flex: none; background: var(--composer-bg); padding: 8px 12px 0; }
.reply-box { display: flex; align-items: center; gap: 8px; background: var(--quote-bg); border-left: 4px solid var(--accent); border-radius: 8px; padding: 6px 8px; }
.reply-box .nm { flex: 1; min-width: 0; font-size: 13px; }
.reply-box .nm b { display: block; }
.reply-box .nm span { display: block; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#files-preview { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; }
.chip { position: relative; flex: none; width: 74px; height: 74px; border-radius: 8px; background: var(--quote-bg); display: grid; place-items: center; overflow: hidden; font-size: 11px; text-align: center; padding: 4px; word-break: break-all; }
.chip img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.chip button { position: absolute; top: 2px; right: 2px; width: 20px; height: 20px; border-radius: 50%; background: rgba(0, 0, 0, 0.6); color: #fff; display: grid; place-items: center; z-index: 2; }
.chip button svg { width: 14px; height: 14px; }
#progress { flex: none; height: 3px; background: var(--line); }
#progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.15s; }
#composer { flex: none; display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px; padding-bottom: calc(8px + env(safe-area-inset-bottom)); background: var(--composer-bg); }
#composer .icon-btn { color: var(--muted); flex: none; }
#text { flex: 1; min-width: 0; resize: none; max-height: 140px; padding: 10px 14px; border: 0; border-radius: 22px; background: var(--input-bg); line-height: 1.3; overflow-y: auto; }
#text:focus { outline: none; }
#send { background: var(--green); color: #fff; }
#send:disabled { opacity: 0.5; }

/* ---------------------------------------------------------------- overlays */
#modal-root:empty { display: none; }
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(11, 20, 26, 0.6); display: grid; place-items: center; padding: 16px; }
.modal { width: 100%; max-width: 440px; max-height: 90dvh; overflow-y: auto; background: var(--panel); border-radius: 12px; padding: 20px; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4); }
.modal h3 { margin: 0 0 12px; font-weight: 500; font-size: 18px; }
.modal input[type="text"] { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--input-bg); margin-bottom: 12px; }
.modal .row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.modal .row:last-of-type { border-bottom: 0; }
.modal .row label { flex: 1; display: flex; gap: 10px; align-items: center; cursor: pointer; }
.modal .row small { color: var(--muted); }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.btn { padding: 9px 18px; border-radius: 20px; font-weight: 500; }
.btn.primary { background: var(--green); color: #fff; }
.btn.ghost { color: var(--green); }
.menu { position: fixed; z-index: 40; min-width: 200px; background: var(--panel); color: var(--text); border-radius: 8px; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35); padding: 6px 0; }
.menu button { display: block; width: 100%; text-align: left; padding: 11px 20px; }
.menu button:hover { background: var(--hover); }
#lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0, 0, 0, 0.92); display: grid; place-items: center; }
#lightbox img { max-width: 96vw; max-height: 88dvh; object-fit: contain; }
#lightbox .top { position: absolute; top: 0; left: 0; right: 0; padding: 8px; padding-top: calc(8px + env(safe-area-inset-top)); display: flex; justify-content: flex-end; gap: 8px; color: #fff; }
#toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 70; max-width: 90vw; background: #323232; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4); }
#toast.error { background: #b3261e; }
#offline { position: fixed; top: 0; left: 0; right: 0; z-index: 80; text-align: center; padding: 6px; padding-top: calc(6px + env(safe-area-inset-top)); background: #b3261e; color: #fff; font-size: 13px; }

/* ---------------------------------------------------------------- phones: one column at a time */
@media (max-width: 760px) {
  #sidebar { width: 100%; border-right: 0; }
  #conv { position: absolute; inset: 0; transform: translateX(100%); transition: transform 0.22s ease; z-index: 10; }
  body.in-chat #conv { transform: none; }
  .back { display: grid; margin-left: -6px; }
  #messages { padding: 10px 8px 8px; }
  .bubble { max-width: 86%; }
  .quote span { max-width: 30ch; }
}
@media (min-width: 761px) { .conv-head .back { display: none; } }
