:root {
  --bg: #0f0f10;
  --bg-soft: #17181a;
  --panel: #1e1f22;
  --border: #2a2b2f;
  --text: #e8e8e9;
  --text-dim: #9a9ba1;
  --accent: #4d6bfe;
  --user-bubble: #2e3f66;
  --error: #e5534b;
  --sidebar-w: 250px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  background:
    radial-gradient(1100px 700px at 15% -10%, rgba(77, 107, 254, .09), transparent 60%),
    radial-gradient(900px 600px at 100% 0%, rgba(122, 77, 254, .06), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo {
  width: 30px; height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, #4d6bfe, #7a4dfe);
  color: #fff;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(77, 107, 254, .35);
}
.title { font-size: 15px; font-weight: 600; }
.brand .title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 38vw;
}
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 5px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: all .15s;
}
.ghost-btn:hover { color: var(--text); border-color: #3a3b41; }
.actions { display: flex; align-items: center; gap: 8px; }
.model-select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  padding: 6px 26px 6px 12px;
  cursor: pointer;
  outline: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%239a9ba1' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  transition: border-color .15s;
}
.model-select:hover { border-color: #3d3f47; }
.model-select:focus { border-color: rgba(77, 107, 254, .55); }
.model-select:disabled { opacity: .5; cursor: not-allowed; }
.model-select option { background: var(--panel); color: var(--text); }
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  transition: all .15s;
}
.icon-btn:hover { color: var(--text); background: var(--panel); }
#menu-btn { display: none; }

/* ---- layout ---- */
.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ---- sidebar ---- */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: rgba(23, 24, 26, .72);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.sidebar-head { padding: 14px 14px 12px; border-bottom: 1px solid var(--border); }
.new-chat-btn {
  width: 100%;
  background: linear-gradient(135deg, #4d6bfe, #6a4dfe);
  color: #fff;
  border: none;
  border-radius: 12px;
  padding: 11px 0;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s, transform .1s, box-shadow .15s;
  box-shadow: 0 4px 14px rgba(77, 107, 254, .28);
}
.new-chat-btn:hover { opacity: .94; transform: translateY(-1px); box-shadow: 0 6px 18px rgba(77, 107, 254, .38); }
.new-chat-btn:active { transform: translateY(0); }
.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px;
  min-height: 0;
}
.session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 13.5px;
  margin-bottom: 3px;
  transition: background .15s, color .15s;
  user-select: none;
}
.session-item:hover { background: rgba(255, 255, 255, .05); color: var(--text); }
.session-item.active {
  background: linear-gradient(135deg, rgba(77, 107, 254, .16), rgba(122, 77, 254, .12));
  color: var(--text);
  border: 1px solid rgba(77, 107, 254, .22);
  padding: 9px 11px;
}
.session-item .title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13.5px;
}
.session-item .del-btn {
  visibility: hidden;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
  border-radius: 6px;
  flex-shrink: 0;
}
.session-item:hover .del-btn { visibility: visible; }
.session-item .del-btn:hover { color: var(--error); }
.sidebar-foot {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
}
/* mobile drawer scrim */
#scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .5);
  z-index: 20;
}

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

/* ---- chat area ---- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 28px 28px 18px;
}
.chat-inner { max-width: 1000px; margin: 0 auto; }

.msg { margin-bottom: 24px; max-width: 100%; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  max-width: 76%;
  background: linear-gradient(135deg, #2e3f66, #39477e);
  border-radius: 14px 14px 4px 14px;
  padding: 10px 16px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-size: 15px;
}
.msg.assistant { line-height: 1.75; font-size: 15px; }
.msg.assistant .md-body { word-break: break-word; overflow-wrap: anywhere; }
.msg .meta {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

/* markdown — everything must never overflow the viewport horizontally */
.md-body { max-width: 100%; }
.md-body p { margin: 0 0 10px; max-width: 100%; }
.md-body p:last-child { margin-bottom: 0; }
.md-body h1, .md-body h2, .md-body h3 { margin: 14px 0 8px; line-height: 1.4; }
.md-body h1 { font-size: 20px; } .md-body h2 { font-size: 17px; } .md-body h3 { font-size: 15px; }
.md-body ul, .md-body ol { margin: 6px 0 10px; padding-left: 22px; max-width: 100%; }
.md-body li { margin: 3px 0; overflow-wrap: anywhere; }
.md-body code {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
  font-family: "Cascadia Code", Consolas, "Courier New", monospace;
  font-size: 13px;
  overflow-wrap: anywhere;
}
.md-body pre {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin: 10px 0;
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}
.md-body pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.6;
  overflow-wrap: normal;
  white-space: pre;
}
.md-body blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-dim);
  margin: 8px 0;
  overflow-wrap: anywhere;
}
.md-body table {
  display: block;
  overflow-x: auto;
  max-width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  -webkit-overflow-scrolling: touch;
}
.md-body th, .md-body td { border: 1px solid var(--border); padding: 6px 10px; font-size: 13.5px; white-space: nowrap; }
.md-body a { color: var(--accent); overflow-wrap: anywhere; }
.md-body hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.md-body img { max-width: 100%; height: auto; border-radius: 8px; }

/* cursor while streaming */
.streaming .md-body > :last-child::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* thinking animation (doubao-style bouncing dots) */
.thinking {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 2px;
}
.thinking span {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  opacity: .4;
  animation: think 1.2s ease-in-out infinite;
}
.thinking span:nth-child(2) { animation-delay: .18s; }
.thinking span:nth-child(3) { animation-delay: .36s; }
@keyframes think {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-6px); opacity: 1; }
}
.streaming .thinking::after { content: none; }

/* error bubble */
.msg.error .bubble {
  background: rgba(229, 83, 75, .12);
  border: 1px solid rgba(229, 83, 75, .4);
  color: var(--error);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13.5px;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

/* ---- composer ---- */
.composer { flex-shrink: 0; padding: 12px 28px 16px; }
.composer-box {
  max-width: 1000px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: rgba(30, 31, 34, .85);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 12px 14px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, .25);
  transition: border-color .2s, box-shadow .2s;
}
.composer-box:focus-within {
  border-color: rgba(77, 107, 254, .55);
  box-shadow: 0 4px 28px rgba(0, 0, 0, .3), 0 0 0 3px rgba(77, 107, 254, .12);
}
#input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.6;
  max-height: 180px;
}
#input::placeholder { color: #5c5d63; }
.send-btn {
  width: 38px; height: 38px;
  flex-shrink: 0;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, #4d6bfe, #6a4dfe);
  color: #fff;
  font-size: 15px;
  cursor: pointer;
  transition: opacity .15s, transform .1s, box-shadow .15s;
  box-shadow: 0 3px 12px rgba(77, 107, 254, .3);
}
.send-btn:hover { opacity: .92; transform: scale(1.04); }
.send-btn:active { transform: scale(.95); }
.send-btn:disabled { opacity: .35; cursor: not-allowed; transform: none; box-shadow: none; }
.send-btn.stop { background: var(--error); font-size: 12px; box-shadow: 0 3px 12px rgba(229, 83, 75, .3); }
.hint { text-align: center; color: var(--text-dim); font-size: 11.5px; margin-top: 8px; }

/* empty state — doubao-style onboarding */
.empty-state { text-align: center; color: var(--text-dim); padding-top: 14vh; }
.empty-state .hero-logo {
  width: 76px; height: 76px;
  margin: 0 auto 22px;
  border-radius: 24px;
  background: linear-gradient(135deg, #4d6bfe, #7a4dfe);
  color: #fff;
  font-weight: 800;
  font-size: 34px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 10px 36px rgba(77, 107, 254, .4);
}
.empty-state h2 { font-size: 24px; font-weight: 700; color: var(--text); margin-bottom: 10px; }
.empty-state .sub { font-size: 14px; color: var(--text-dim); margin-bottom: 34px; }
.presets {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  max-width: 720px;
  margin: 0 auto;
}
.preset {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(30, 31, 34, .7);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 13px 18px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: border-color .18s, transform .12s, box-shadow .18s, background .18s;
  text-align: left;
}
.preset:hover {
  border-color: rgba(77, 107, 254, .55);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .35), 0 0 0 3px rgba(77, 107, 254, .1);
  background: rgba(38, 40, 46, .8);
}
.preset .ico { font-size: 18px; flex-shrink: 0; }
.preset .txt { line-height: 1.4; }

/* assistant meta badge + copy */
.msg.assistant { position: relative; }
.msg.assistant .meta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  color: var(--text-dim);
  background: rgba(255, 255, 255, .05);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 2px 10px;
  margin-bottom: 10px;
}
.msg.assistant .meta .dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4d6bfe, #7a4dfe);
}
.copy-btn {
  position: absolute;
  top: 0;
  right: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(30, 31, 34, .9);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-dim);
  font-size: 12px;
  font-family: inherit;
  padding: 4px 10px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s, color .15s, border-color .15s;
}
.msg.assistant:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text); border-color: #3d3f47; }
.copy-btn.copied { color: #4ade80; border-color: rgba(74, 222, 128, .4); }
pre { position: relative; }
.copy-code-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(15, 15, 16, .75);
  border: 1px solid var(--border);
  border-radius: 7px;
  color: var(--text-dim);
  font-size: 11.5px;
  font-family: inherit;
  padding: 3px 9px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .15s, color .15s;
}
pre:hover .copy-code-btn { opacity: 1; }
.copy-code-btn:hover { color: var(--text); }
.copy-code-btn.copied { color: #4ade80; }

/* ---- login view ---- */
.login-view {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 10;
}
.login-view.hidden, .app-view.hidden { display: none; }
.app-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.login-card {
  width: min(360px, 92vw);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 34px 30px 30px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.login-logo {
  width: 44px; height: 44px;
  margin: 0 auto;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 20px;
  display: flex; align-items: center; justify-content: center;
}
.login-title { font-size: 17px; font-weight: 600; margin: 2px 0 6px; }
.login-input {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  padding: 10px 12px;
  outline: none;
  transition: border-color .15s;
}
.login-input:focus { border-color: #3d3f47; }
.login-error {
  min-height: 18px;
  color: var(--error);
  font-size: 12.5px;
  line-height: 1.5;
}
.login-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 0;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s, transform .1s;
}
.login-btn:hover { opacity: .92; }
.login-btn:active { transform: scale(.98); }
.login-btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---- wide screens (>=1600px): use the space better ---- */
@media (min-width: 1600px) {
  :root { --sidebar-w: 280px; }
  .chat { padding: 32px 40px 20px; }
  .chat-inner, .composer-box { max-width: 1080px; }
  .composer { padding: 14px 40px 18px; }
  .msg.assistant { font-size: 15.5px; line-height: 1.8; }
  .msg.user .bubble { font-size: 15.5px; }
  .empty-state { padding-top: 18vh; }
}

/* ---- responsive: phones ---- */
@media (max-width: 720px) {  .chat { padding: 16px 12px 10px; }
  .msg { margin-bottom: 16px; }
  .msg.assistant { font-size: 14px; }
  .msg.user .bubble { max-width: 90%; font-size: 14px; padding: 9px 12px; }
  .composer { padding: 8px 10px 10px; }
  .composer-box { border-radius: 14px; padding: 8px 10px; }
  #input { font-size: 16px; } /* avoid iOS auto-zoom */
  .send-btn { width: 38px; height: 38px; }
  .hint { font-size: 10.5px; }
  .topbar { padding: 10px 12px; }

  /* sidebar becomes a drawer */
  .layout { position: relative; }
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 30;
    width: min(78vw, 280px);
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 0 0 24px rgba(0,0,0,.5);
  }
  .sidebar.open { transform: translateX(0); }
  #scrim.show { display: block; }
  #menu-btn { display: inline-flex; }
  .del-btn { visibility: visible !important; } /* always deletable on touch */
  .session-item { padding: 12px 10px; font-size: 14px; }
}

/* scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #33353b; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #40424a; }
@media (max-width: 720px) {
  ::-webkit-scrollbar { width: 4px; height: 4px; }
}
