:root {
  --bg: #fff; --side: #f4f4f5; --text: #1f2328; --muted: #6b7280; --border: #e5e7eb;
  --user: #eef2ff; --accent: #10a37f; --accent-h: #0d8a6b;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #212121; --side: #171717; --text: #ececec; --muted: #9ca3af; --border: #333; --user: #2f2f2f; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 16px/1.55 system-ui, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; color: inherit; }

.login { height: 100%; display: grid; place-items: center; }
.login form { display: flex; flex-direction: column; gap: 12px; width: min(320px, 90vw); }
.login h1 { text-align: center; margin: 0 0 8px; }
.login input { padding: 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); font: inherit; }
.login button { padding: 12px; border: 0; border-radius: 8px; background: var(--accent); color: #fff; }
.login button:hover { background: var(--accent-h); }
.err { color: #dc2626; min-height: 1.2em; margin: 0; text-align: center; }

.app { display: flex; height: 100%; }
aside { width: 270px; background: var(--side); display: flex; flex-direction: column; padding: 12px; gap: 6px; flex-shrink: 0; }
aside h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 8px 4px; }
.primary, .nav { text-align: left; padding: 10px 12px; border-radius: 8px; border: 0; background: transparent; }
.primary { border: 1px solid var(--border); }
.primary:hover, .nav:hover { background: var(--border); }
.bottom { margin-top: auto; color: var(--muted); }
#chatList { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#chatList li { display: flex; align-items: center; border-radius: 8px; }
#chatList li:hover, #chatList li.active { background: var(--border); }
#chatList .name { flex: 1; padding: 8px 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; font-size: 14px; }
#chatList .del { border: 0; background: none; color: var(--muted); padding: 4px 8px; visibility: hidden; }
#chatList li:hover .del { visibility: visible; }

main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
header { padding: 10px 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; font-weight: 600; }
.icon { display: none; border: 0; background: none; font-size: 20px; }
#chatView { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.msg { max-width: 760px; margin: 0 auto 20px; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { padding: 10px 14px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; max-width: 100%; }
.msg.user .bubble { background: var(--user); }
.msg.bot .bubble { padding-left: 0; }
.bubble img { max-width: 100%; border-radius: 8px; display: block; margin: 8px 0; }
.bubble code { background: var(--side); padding: 1px 5px; border-radius: 4px; }
.bubble pre { background: var(--side); padding: 12px; border-radius: 8px; overflow-x: auto; margin: 8px 0; }
.bubble pre code { padding: 0; background: none; }
.cursor::after { content: '▍'; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.empty { text-align: center; color: var(--muted); margin-top: 20vh; font-size: 22px; }

#composer, #imgForm { max-width: 760px; width: calc(100% - 32px); margin: 0 auto 20px; display: flex; align-items: flex-end; gap: 8px; border: 1px solid var(--border); border-radius: 24px; padding: 8px 8px 8px 16px; background: var(--side); }
#input, #imgPrompt { flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text); font: inherit; max-height: 200px; padding: 6px 0; }
#send, #imgBtn { width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; flex-shrink: 0; }
#send:disabled, #imgBtn:disabled { opacity: .4; cursor: default; }

#imagesView { flex: 1; display: flex; flex-direction: column; min-height: 0; }
#imagesView.hidden { display: none; }
#gallery { flex: 1; overflow-y: auto; padding: 20px; align-content: start; }
#imgStatus { max-width: 760px; width: calc(100% - 32px); margin: 0 auto 8px; text-align: left; }
#gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
#gallery a { display: block; aspect-ratio: 1; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
#gallery img { width: 100%; height: 100%; object-fit: cover; }
#imagesView .empty { grid-column: 1 / -1; }

@media (max-width: 760px) {
  aside { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; }
  aside.open { transform: none; box-shadow: 0 0 40px #0006; }
  .icon { display: block; }
}
