:root {
  --bg: #f4f5f7; --panel: #ffffff; --side: #1f2430; --side-text: #d6d9e0; --side-active: #2f3646;
  --text: #1c1e24; --muted: #6b7280; --border: #e3e5ea; --accent: #2da9e8; --accent-dark: #1f8fc9;
  --bubble-user: #eef6fb; --bubble-bot: #ffffff; --danger: #d9534f; --radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-size: 15px; }
button, input, textarea { font: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin-top: 8px; font-size: 14px; }

/* login */
.login { height: 100%; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { background: var(--panel); padding: 32px; border-radius: 16px; width: 100%; max-width: 380px;
  box-shadow: 0 10px 30px rgba(0,0,0,.08); }
.login-card h1 { font-size: 22px; margin: 0 0 4px; }
.login-card p { margin: 0 0 20px; }
.login-card label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.login-card input { display: block; width: 100%; margin-top: 6px; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 15px; }
.login-card button { width: 100%; }

/* layout */
.app { display: flex; height: 100%; }
.sidebar { width: 290px; background: var(--side); color: var(--side-text); display: flex; flex-direction: column; flex-shrink: 0; }
.sidebar-top { padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.sidebar-top input { padding: 8px 10px; border-radius: 8px; border: 1px solid #3a4152; background: #2a303d; color: var(--side-text); }
.threads { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.thread { padding: 10px 10px; border-radius: 8px; cursor: pointer; display: flex; align-items: center; gap: 8px; margin-bottom: 2px; }
.thread:hover { background: #283040; }
.thread.active { background: var(--side-active); }
.thread .t-title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.thread .t-date { font-size: 11px; color: #8a91a0; }
.thread .t-owner { font-size: 11px; color: var(--accent); }
.sidebar-bottom { padding: 12px 14px; border-top: 1px solid #2f3646; display: flex; gap: 12px; align-items: center; font-size: 13px; }
.sidebar-bottom .muted { flex: 1; color: #8a91a0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.title-input { flex: 1; border: 1px solid transparent; border-radius: 8px; padding: 6px 8px; font-size: 16px; font-weight: 600; background: transparent; }
.title-input:hover:not(:disabled), .title-input:focus { border-color: var(--border); background: var(--bg); outline: none; }
.icon { display: none; background: none; border: 0; font-size: 20px; cursor: pointer; }

.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 14px; }
.empty { max-width: 560px; margin: 60px auto; color: var(--muted); }
.empty h2 { color: var(--text); font-size: 20px; }
.msg { max-width: 760px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble { overflow-wrap: anywhere; padding: 12px 14px; border-radius: var(--radius); white-space: pre-wrap; line-height: 1.45; max-width: 92%; }
.user .bubble { background: var(--bubble-user); }
.assistant .bubble { background: var(--bubble-bot); border: 1px solid var(--border); }
.assistant .bubble.question { border-color: #f0c36d; background: #fff8e6; }
.assistant .bubble.raw { color: var(--muted); font-size: 13px; }
.why { font-size: 13px; color: var(--muted); margin: 6px 4px 0; font-style: italic; max-width: 92%; }
.msg-images { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; justify-content: flex-end; }
.msg-images img { max-height: 140px; border-radius: 8px; border: 1px solid var(--border); }
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.actions button { border: 1px solid var(--border); background: var(--panel); border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; }
.actions button:hover { border-color: var(--accent); color: var(--accent-dark); }
.actions button.done { background: #e8f7ee; border-color: #b7e2c6; color: #2b7a4b; }
.edit-box { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.edit-box textarea { width: 100%; padding: 8px; border: 1px solid var(--border); border-radius: 8px; }
.typing { color: var(--muted); font-size: 13px; padding: 4px 0; }

.composer { border-top: 1px solid var(--border); background: var(--panel); padding: 10px 16px 12px; }
.composer textarea { width: 100%; max-width: 760px; display: block; margin: 0 auto; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 10px; resize: vertical; min-height: 72px; max-height: 320px; }
.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer-row { max-width: 760px; margin: 8px auto 0; display: flex; align-items: center; gap: 12px; }
.composer-row .muted { flex: 1; }
.attachments { max-width: 760px; margin: 0 auto 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.attachments .att { position: relative; }
.attachments img { height: 64px; border-radius: 6px; border: 1px solid var(--border); }
.attachments .rm { position: absolute; top: -6px; right: -6px; background: var(--danger); color: #fff; border: 0; border-radius: 50%;
  width: 20px; height: 20px; font-size: 12px; cursor: pointer; }
.file-label { cursor: pointer; }

button.primary { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 10px 16px; cursor: pointer; font-weight: 600; }
button.primary:hover { background: var(--accent-dark); }
button.primary:disabled { opacity: .6; cursor: default; }
button.wide { width: 100%; }
button.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; }
.sidebar button.link { color: #9fc9e6; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #1f2430; color: #fff; padding: 10px 16px;
  border-radius: 8px; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.2); z-index: 50; }

@media (max-width: 800px) {
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform .2s; width: 82%; max-width: 320px; }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.3); }
  .icon { display: block; }
  .bubble { max-width: 100%; }
  .composer-row .small { display: none; }
}

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 20px; }
.modal-card { background: var(--panel); border-radius: 14px; padding: 22px; width: 100%; max-width: 520px; max-height: 85vh; overflow: auto; }
.modal-card h3 { margin: 0 0 12px; font-size: 17px; }
.modal-card label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
.modal-card input { display: block; width: 100%; margin-top: 4px; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 15px; }
.modal-actions { margin-top: 12px; text-align: right; }
.stats { width: 100%; border-collapse: collapse; font-size: 14px; }
.stats th, .stats td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.stats th { color: var(--muted); font-weight: 500; font-size: 12px; }
.msg-images img { max-height: 160px; max-width: 240px; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; object-fit: cover; }
.msg-images img.zoom { max-height: none; max-width: 100%; cursor: zoom-out; }
.topbar .link.small { font-size: 13px; white-space: nowrap; }

.chips { max-width: 760px; margin: 0 auto 6px; display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px dashed var(--border); background: transparent; color: var(--muted); border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent-dark); }
.card-form { display: flex; gap: 6px; align-items: center; }
.card-form input { width: 210px; padding: 5px 8px; border: 1px solid var(--border); border-radius: 8px; font-size: 13px; }
.bubble.card { background: #f6f7f9; border: 1px dashed var(--border); font-size: 13px; color: #374151; }
.bubble.streaming::after { content: "▍"; color: var(--accent); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0; } }
@media (max-width: 800px) { .card-form input { width: 130px; } }
