:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1b2330; --muted: #5d6878; --line: #dde1e7;
  --accent: #0b5cad; --accent-text: #ffffff; --user: #e6eff9; --error: #b3261e; --warn-bg: #fff4d6;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c; --panel: #1b212a; --text: #e7ebf0; --muted: #9aa5b4; --line: #2c3540;
    --accent: #5aa5f0; --accent-text: #0c1117; --user: #223247; --error: #ff8a80; --warn-bg: #3a3013;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 16px/1.5 system-ui, "Segoe UI", sans-serif; }
h1, h2 { margin: 0; }
button { font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); color: var(--text); padding: 8px 14px; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.link { border: 0; background: none; color: var(--accent); padding: 4px; }
.muted { color: var(--muted); }
.error { color: var(--error); min-height: 1.5em; margin: 0; }
.warning { background: var(--warn-bg); border-radius: 8px; padding: 10px 14px; text-align: left; }

/* sign-in */
#login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 28px; width: 100%; max-width: 380px; display: grid; gap: 14px; }
.login-card h1 { font-size: 1.3rem; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 4px; font-size: .9rem; color: var(--muted); }
input, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px; width: 100%; }

/* layout */
#app { display: grid; grid-template-columns: 270px 1fr; height: 100%; }
#sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 12px; padding: 14px; min-height: 0; }
#conversations { flex: 1; overflow-x: hidden; overflow-y: auto; display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; align-content: start; }
.conv { display: flex; align-items: center; border-radius: 8px; min-width: 0; }
.conv .open { min-width: 0; }
.conv.active, .conv:hover { background: var(--bg); }
.conv button { border: 0; background: none; padding: 8px; }
.conv .open { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .delete { color: var(--muted); visibility: hidden; }
.conv:hover .delete, .conv .delete:focus { visibility: visible; }
.account { display: flex; justify-content: space-between; align-items: center; font-size: .9rem; color: var(--muted); }

#chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#chat header { display: flex; align-items: center; gap: 8px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
#chat header h1 { font-size: 1.05rem; }
#menu { display: none; font-size: 1.3rem; }
#messages { flex: 1; overflow-y: auto; padding: 20px; }
#messages > * { max-width: 860px; margin: 0 auto 16px; }

#welcome { text-align: center; padding-top: 8vh; }
.starters { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin: 24px 0; }
.starters button { padding: 14px; text-align: left; }

.msg.user { background: var(--user); border-radius: 12px; padding: 10px 14px; width: fit-content; max-width: 80%; margin-right: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.assistant { overflow-wrap: anywhere; }
.msg.assistant h3 { font-size: 1rem; letter-spacing: .03em; margin: 1.4em 0 .4em; border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.msg.assistant a { color: var(--accent); }
.msg.assistant pre { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 12px; overflow-x: auto; font-size: .85rem; }
.table-wrap { overflow-x: auto; }
.msg.assistant table { border-collapse: collapse; font-size: .92rem; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; vertical-align: top; }
.msg.assistant th { background: var(--panel); }
.status { color: var(--muted); font-style: italic; min-height: 1.5em; }
.msg-actions { display: flex; gap: 8px; }
.msg-actions button { font-size: .85rem; padding: 4px 10px; }

#composer { border-top: 1px solid var(--line); padding: 12px 20px 16px; display: grid; gap: 8px; max-width: 900px; width: 100%; margin: 0 auto; }
#input { resize: none; max-height: 200px; }
.composer-actions { display: flex; justify-content: space-between; }

@media (max-width: 760px) {
  #app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 25% 0 0; z-index: 2; transform: translateX(-100%); transition: transform .2s; }
  #app.menu-open #sidebar { transform: none; box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .4); }
  #menu { display: block; }
  #chat header, #messages, #composer { padding-left: 16px; padding-right: 16px; }
  .starters { grid-template-columns: 1fr; }
}
