:root {
  --bg: #0b0d17;
  --panel: #121526;
  --panel-2: #181c31;
  --line: #262b47;
  --txt: #e8eaf6;
  --muted: #9aa1c4;
  --accent: #6d7cff;
  --accent-2: #a06dff;
  --user: #2a2f52;
  --bot: #161a2e;
  --danger: #ff6b81;
  --ok: #4ade80;
  --radius: 16px;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--txt);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}

button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; color: inherit; }

a { color: var(--accent); }

/* ---------- header ---------- */
.app-header {
  padding: 12px 16px;
  background: linear-gradient(90deg, #141833, #1a1440);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 5;
}

.app-header h1 { font-size: 17px; margin: 0; font-weight: 700; }

.app-header .sub { font-size: 12px; color: var(--muted); font-weight: 400; }

.app-header .spacer { flex: 1; }

.icon-btn {
  background: var(--panel-2);
  border: 1px solid var(--line);
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  font-size: 17px;
}

.icon-btn:active { transform: scale(.95); }
.icon-btn[disabled] { opacity: .4; }

/* ---------- chat ---------- */
.chat-main {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 8px;
  scroll-behavior: smooth;
  position: relative;
}

.stream {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 100%;
}

.empty {
  text-align: center;
  color: var(--muted);
  margin: auto;
  max-width: 520px;
  padding: 24px 8px;
}

.empty h2 { color: var(--txt); font-size: 22px; margin: 0 0 8px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 16px; }

.chip {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--txt);
  padding: 8px 12px;
  border-radius: 999px;
  font-size: 13px;
}

.chip:active { border-color: var(--accent); }

.msg {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-wrap: break-word;
  animation: pop .18s ease;
  overflow-wrap: anywhere;
}

@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.msg.user {
  align-self: flex-end;
  background: var(--user);
  border: 1px solid #384070;
  border-bottom-right-radius: 5px;
}

.msg.bot {
  align-self: flex-start;
  background: var(--bot);
  border: 1px solid var(--line);
  border-bottom-left-radius: 5px;
}

.msg p { margin: 0 0 8px; }
.msg p:last-child { margin-bottom: 0; }
.msg img { max-width: 100%; border-radius: 10px; display: block; margin-bottom: 6px; }
.msg pre { background: #0a0c16; border: 1px solid var(--line); padding: 10px; border-radius: 10px; overflow-x: auto; margin: 8px 0; white-space: pre; }
.msg code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9em; background: #0a0c16; padding: 1px 5px; border-radius: 5px; }
.msg pre code { background: none; padding: 0; }
.msg.err { border-color: #5b2333; color: #ffb3c0; }
.msg .who { font-size: 11px; color: var(--muted); margin-bottom: 4px; letter-spacing: .03em; text-transform: uppercase; }

.cursor {
  display: inline-block;
  width: 8px;
  height: 17px;
  background: var(--accent);
  vertical-align: -3px;
  animation: blink .8s steps(2) infinite;
}

@keyframes blink { 50% { opacity: 0; } }

/* ---------- jump-to-bottom ---------- */
.jump {
  position: absolute;
  right: 16px;
  bottom: 16px;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: none;
  place-items: center;
  z-index: 6;
}

.jump.on { display: grid; }

/* ---------- composer ---------- */
.composer {
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.bar { max-width: 860px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }

.composer textarea {
  flex: 1;
  resize: none;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--txt);
  border-radius: 14px;
  padding: 11px 13px;
  max-height: 132px;
  min-height: 46px;
  font: inherit;
  outline: none;
}

.composer textarea:focus { border-color: var(--accent); }

.send-btn, .attach-btn {
  width: 46px;
  height: 46px;
  border-radius: 14px;
  font-size: 18px;
  flex: 0 0 auto;
}

.send-btn { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; }
.send-btn[disabled] { opacity: .45; }
.send-btn.stop { background: linear-gradient(135deg, #ff5f6d, #ff9966); }
.attach-btn { background: var(--panel-2); border: 1px solid var(--line); }

.preview { max-width: 860px; margin: 0 auto 8px; display: none; gap: 8px; align-items: center; }
.preview.on { display: flex; }
.preview img { height: 52px; border-radius: 8px; border: 1px solid var(--line); }

.status { max-width: 860px; margin: 6px auto 0; font-size: 12px; color: var(--muted); min-height: 16px; text-align: center; }

/* ---------- settings sheet ---------- */
.sheet { position: fixed; inset: 0; background: rgba(0, 0, 0, .6); display: none; z-index: 20; }
.sheet.on { display: block; }

.panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: min(420px, 94vw);
  background: var(--panel);
  border-left: 1px solid var(--line);
  padding: 18px;
  overflow-y: auto;
  animation: slide .2s ease;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

@keyframes slide { from { transform: translateX(30px); opacity: .5; } to { transform: none; opacity: 1; } }

.panel h3 { margin: 0; font-size: 16px; }
.panel .head { display: flex; align-items: center; justify-content: space-between; }

label { font-size: 13px; color: var(--muted); display: block; margin-bottom: 5px; }

.panel select,
.panel input[type="password"],
.panel input[type="text"],
.panel textarea {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--txt);
  border-radius: 10px;
  padding: 10px;
  font: inherit;
  outline: none;
}

.panel textarea.sys { resize: vertical; min-height: 76px; font-size: 14px; }
.panel input[type="range"] { width: 100%; accent-color: var(--accent); }

.hint { font-size: 12px; color: var(--muted); line-height: 1.45; }
.row { display: flex; gap: 8px; }
.row .grow { flex: 1; }

.save-btn { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; padding: 11px; border-radius: 10px; width: 100%; }
.ghost-btn { background: var(--panel-2); border: 1px solid var(--line); color: var(--txt); padding: 11px; border-radius: 10px; width: 100%; }
.danger-btn { background: var(--panel-2); border: 1px solid var(--line); color: var(--danger); padding: 11px; border-radius: 10px; width: 100%; }

details.adv > summary { cursor: pointer; font-size: 13px; color: var(--muted); }

/* ---------- toast ---------- */
.toasts {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 40;
  pointer-events: none;
  width: min(440px, 92vw);
}

.toast {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: pop .2s ease;
  text-align: center;
}

.toast.ok { border-color: #1d4032; color: var(--ok); }
.toast.err { border-color: #5b2333; color: #ffb3c0; }

@media (max-width: 600px) {
  .msg { max-width: 94%; }
  .app-header h1 { font-size: 16px; }
}
