:root {
  --green-900: #14532d;
  --green-700: #1e7d46;
  --green-600: #239a57;
  --green-100: #e6f6e9;
  --green-50: #f2faf4;
  --ink: #1c2b21;
  --muted: #5f7267;
  --border: #d7e8dc;
  --card: #ffffff;
  --danger: #c0392b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--green-100) 0%, var(--green-50) 40%, #ffffff 100%);
}

#app { display: flex; height: 100vh; height: 100dvh; }

/* ---------- sidebar ---------- */
#sidebar {
  width: 264px;
  flex: 0 0 264px;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.85);
  border-right: 1px solid var(--border);
}
.side-head { padding: 16px; border-bottom: 1px solid var(--border); }
.logo { font-weight: 700; font-size: 17px; color: var(--green-900); margin-bottom: 12px; }
#conv-list { flex: 1; overflow-y: auto; padding: 8px; }
.conv-item {
  display: flex; align-items: center; gap: 6px;
  padding: 9px 10px; border-radius: 8px; cursor: pointer;
  font-size: 14px; color: var(--ink);
}
.conv-item:hover { background: var(--green-100); }
.conv-item.active { background: var(--green-100); font-weight: 600; }
.conv-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .del { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 13px; padding: 2px 4px; }
.conv-item .del:hover { color: var(--danger); }
.side-foot { padding: 12px 16px; border-top: 1px solid var(--border); }

/* ---------- main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.85);
  border-bottom: 1px solid var(--border);
}
.selectors { display: flex; gap: 8px; flex-wrap: wrap; }
.selectors select {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: #fff; font-size: 14px; color: var(--ink); max-width: 46vw;
}
.key-status { font-size: 12px; color: var(--muted); margin-left: auto; }
.key-status.ok { color: var(--green-700); }
.key-status.missing { color: #b7791f; }

#messages { flex: 1; overflow-y: auto; padding: 20px 16px; }
#empty-state {
  max-width: 560px; margin: 12vh auto 0; text-align: center; color: var(--muted);
}
#empty-state h2 { color: var(--green-900); margin-bottom: 8px; }
#empty-state .hint { font-size: 13px; }

.msg { max-width: 820px; margin: 0 auto 16px; display: flex; }
.msg .bubble {
  padding: 12px 16px; border-radius: 14px; line-height: 1.65;
  font-size: 15px; word-break: break-word; max-width: 92%;
}
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--green-700); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--card); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.error .bubble { background: #fdf0ee; border: 1px solid #f2c6c0; color: var(--danger); }
.msg .meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.bubble pre {
  background: #0f2a1a; color: #d9f2e2; padding: 12px; border-radius: 8px;
  overflow-x: auto; font-size: 13px; line-height: 1.5;
}
.bubble code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 22px; }
.typing::after { content: "▍"; animation: blink 1s steps(1) infinite; color: var(--green-600); }
@keyframes blink { 50% { opacity: 0; } }

/* ---------- composer ---------- */
#composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 12px 16px;
  background: rgba(255, 255, 255, 0.9);
  border-top: 1px solid var(--border);
}
#input {
  flex: 1; resize: none; max-height: 160px;
  padding: 11px 14px; border: 1px solid var(--border); border-radius: 12px;
  font-size: 15px; font-family: inherit; line-height: 1.5;
}
#input:focus { outline: 2px solid var(--green-600); outline-offset: 0; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid transparent; border-radius: 9px; cursor: pointer;
  padding: 9px 16px; font-size: 14px; font-family: inherit;
}
.btn.primary { background: var(--green-700); color: #fff; }
.btn.primary:hover { background: var(--green-600); }
.btn.ghost { background: none; border-color: var(--border); color: var(--green-900); width: 100%; }
.btn.danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn {
  border: none; background: none; cursor: pointer; font-size: 17px;
  color: var(--green-900); padding: 6px 8px; border-radius: 8px;
}
.icon-btn:hover { background: var(--green-100); }
.hidden { display: none !important; }

/* ---------- modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(20, 50, 35, 0.45);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal-card {
  background: #fff; border-radius: 14px; width: min(560px, 92vw);
  max-height: 88vh; display: flex; flex-direction: column;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; color: var(--green-900); }
.modal-body { padding: 16px 18px; overflow-y: auto; }
.modal-body .note { font-size: 13px; color: var(--muted); line-height: 1.6; margin-top: 0; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; color: var(--green-900); }
.field .hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.field input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; font-family: inherit;
}
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border); text-align: right; }

/* ---------- responsive ---------- */
@media (max-width: 760px) {
  #sidebar { position: fixed; z-index: 40; height: 100%; transform: translateX(-100%); transition: transform 0.2s; }
  #sidebar.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,0.2); }
}
