/* ChatCPM — ChatGPT-style shell around a fully local MiniCPM model. */
:root {
  --bg: #ffffff;
  --side: #f9f9f9;
  --text: #0d0d0d;
  --muted: #8f8f8f;
  --line: #e5e5e5;
  --hover: #ececec;
  --user-bubble: #f4f4f4;
  --composer: #ffffff;
  --composer-line: #d9d9d9;
  --btn: #0d0d0d;
  --btn-text: #ffffff;
  --accent: #10a37f;
  --danger: #e02f2f;
  --col: 768px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #212121;
    --side: #181818;
    --text: #ececec;
    --muted: #9b9b9b;
    --line: #303030;
    --hover: #2f2f2f;
    --user-bubble: #303030;
    --composer: #303030;
    --composer-line: #303030;
    --btn: #ffffff;
    --btn-text: #0d0d0d;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 ui-sans-serif, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}
button, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
button:disabled { cursor: default; }
[hidden] { display: none !important; }

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

/* ---------- sidebar ---------- */
.sidebar {
  width: 260px; flex: none; background: var(--side);
  display: flex; flex-direction: column; gap: 2px;
  padding: 8px 8px max(8px, env(safe-area-inset-bottom));
  border-right: 1px solid transparent;
}
.side-top { display: flex; align-items: center; gap: 6px; padding: 4px 4px 6px; }
.wordmark { font-weight: 650; letter-spacing: -0.02em; font-size: 16px; }
.icon-btn {
  width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center;
  color: var(--text); flex: none;
}
.icon-btn:hover { background: var(--hover); }
.new-chat {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 9px 10px; border-radius: 10px; font-size: 14px; font-weight: 500; text-align: left;
}
.new-chat:hover { background: var(--hover); }
.side-label { font-size: 12px; color: var(--muted); padding: 14px 10px 6px; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; }
.chat-item {
  display: flex; align-items: center; gap: 6px; width: 100%;
  padding: 8px 8px 8px 10px; border-radius: 10px; font-size: 14px;
  color: var(--text); text-align: left;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--hover); }
.chat-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .x { opacity: 0; color: var(--muted); width: 22px; height: 22px; border-radius: 6px; display: grid; place-items: center; flex: none; }
.chat-item:hover .x, .chat-item.active .x { opacity: 1; }
.chat-item .x:hover { color: var(--text); background: var(--bg); }
.side-foot { padding: 10px; border-top: 1px solid var(--line); margin-top: 4px; }
.engine { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.engine-sub { font-size: 11.5px; color: var(--muted); padding-left: 16px; margin-top: 2px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ready { background: var(--accent); }
.dot.busy { background: #d8a13a; animation: pulse 1.1s ease-in-out infinite; }
.dot.error { background: var(--danger); }
@keyframes pulse { 50% { opacity: .3; } }

.scrim { display: none; }

/* ---------- main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 6px; flex: none;
  padding: max(6px, env(safe-area-inset-top)) 10px 6px; min-height: 52px;
}
.model-picker { position: relative; flex: 1; min-width: 0; display: flex; }
.model-picker select {
  appearance: none; -webkit-appearance: none; background: transparent; border: 0;
  padding: 7px 28px 7px 10px; border-radius: 10px; font-size: 15px; font-weight: 500;
  max-width: 100%; min-width: 0; text-overflow: ellipsis; overflow: hidden; white-space: nowrap;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 16px, calc(100% - 10px) 16px;
  background-size: 5px 5px, 5px 5px; background-repeat: no-repeat;
}
.model-picker select:hover { background-color: var(--hover); }
.only-mobile { display: none; }

.status { flex: none; padding: 6px 16px 10px; max-width: var(--col); width: 100%; margin: 0 auto; }
.status-row { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); }
.pct { font-variant-numeric: tabular-nums; }
.track { height: 3px; background: var(--line); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.fill { height: 100%; width: 0; background: var(--text); border-radius: 3px; transition: width .2s ease; }
.status.err .status-row { color: var(--danger); }
.status.err .fill { background: var(--danger); }

.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.thread { max-width: var(--col); margin: 0 auto; padding: 8px 16px 24px; }

.turn { margin: 0 0 24px; }
.turn.user { display: flex; justify-content: flex-end; }
.turn.user .body {
  background: var(--user-bubble); padding: 10px 16px; border-radius: 20px;
  max-width: 75%; white-space: pre-wrap; overflow-wrap: anywhere;
}
.turn.ai .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.turn.ai .body.err { color: var(--danger); }
.turn.ai .foot { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.turn.ai .stat { font-size: 12px; color: var(--muted); }
.copy { font-size: 12px; color: var(--muted); padding: 3px 7px; border-radius: 6px; }
.copy:hover { background: var(--hover); color: var(--text); }
.caret { display: inline-block; width: 8px; height: 16px; background: currentColor; vertical-align: -3px; border-radius: 1px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.body code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .88em; background: var(--hover); padding: 2px 5px; border-radius: 5px; }
.body pre { background: var(--hover); padding: 12px 14px; border-radius: 12px; overflow-x: auto; margin: 10px 0; white-space: pre; }
.body pre code { background: none; padding: 0; }
.body strong { font-weight: 650; }

/* ---------- dock (greeting + composer) ---------- */
.dock { flex: none; padding: 0 16px max(10px, env(safe-area-inset-bottom)); }
.dock-inner { max-width: var(--col); margin: 0 auto; }
.greeting { display: none; font-size: 28px; line-height: 1.25; font-weight: 600; letter-spacing: -0.02em; text-align: center; margin: 0 0 26px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--composer); border: 1px solid var(--composer-line);
  border-radius: 26px; padding: 8px 8px 8px 18px;
  box-shadow: 0 2px 12px rgba(0,0,0,.05);
}
@media (prefers-color-scheme: dark) { .composer { box-shadow: none; } }
.composer textarea {
  flex: 1; border: 0; outline: none; background: none; resize: none;
  padding: 9px 0; max-height: 42vh; line-height: 1.5;
}
.composer textarea::placeholder { color: var(--muted); }
.composer-actions { display: flex; align-items: center; }
.round {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  background: var(--btn); color: var(--btn-text); flex: none;
}
.round:disabled { opacity: .25; }

.suggestions { display: none; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }
.chip {
  border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px;
  font-size: 13.5px; color: var(--text); background: var(--bg);
}
.chip:hover { background: var(--hover); }
.load-pill {
  display: none; margin: 18px auto 0; padding: 11px 20px; border-radius: 999px;
  background: var(--btn); color: var(--btn-text); font-size: 14.5px; font-weight: 550;
}
.load-pill:disabled { opacity: .5; }
.disclaimer { text-align: center; font-size: 11.5px; color: var(--muted); margin: 10px 0 4px; }

/* empty state: centre the greeting + composer, ChatGPT style */
.main.empty .scroll { display: none; }
.main.empty .dock { margin-block: auto; }
.main.empty .greeting,
.main.empty .suggestions { display: flex; }
.main.empty .greeting { display: block; }
.main.needs-model .load-pill { display: block; }

/* ---------- mobile ---------- */
@media (max-width: 860px) {
  .only-mobile { display: grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%);
    transition: transform .22s ease; box-shadow: 0 0 40px rgba(0,0,0,.2);
  }
  .layout.side-open .sidebar { transform: none; }
  .layout.side-open .scrim {
    display: block; position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.45);
  }
  .greeting { font-size: 23px; margin-bottom: 20px; }
  .turn.user .body { max-width: 88%; }
  .thread { padding-inline: 12px; }
  .dock { padding-inline: 12px; }
  .model-picker select { font-size: 14px; }
}

/* image attachment (vision models) */
.attachment { position: relative; width: 84px; margin: 0 0 8px 6px; }
.attachment img { width: 84px; height: 84px; object-fit: cover; border-radius: 12px; display: block; border: 1px solid var(--line); }
.drop-img {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%;
  background: var(--btn); color: var(--btn-text); display: grid; place-items: center;
}
.composer:has(.attach-btn:not([hidden])) { padding-left: 8px; }
.attach-btn {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  color: var(--text); flex: none; margin-bottom: 1px; border: 1px solid var(--composer-line);
}
.attach-btn:hover { background: var(--hover); }
.turn .shot { max-width: min(280px, 70%); border-radius: 14px; display: block; margin-bottom: 8px; }
.turn.user .shot { margin-left: auto; }
