:root {
  color-scheme: light dark;
  --bg: #faf9f5; --panel: #ffffff; --line: #e3dfd3; --text: #2b2a27; --muted: #77746b;
  --bubble: #efece3; --accent: #c6613f; --on-accent: #ffffff; --selected: #2b2a27; --on-selected: #faf9f5;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624; --panel: #30302e; --line: #454541; --text: #f0eee6; --muted: #a8a59b;
    --bubble: #3a3a37; --accent: #d97757; --on-accent: #1f1e1d; --selected: #f0eee6; --on-selected: #262624;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column; height: 100dvh;
  background: var(--bg); color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
button:focus-visible, textarea:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

header {
  display: flex; align-items: center; gap: 4px; height: calc(48px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 6px 0;
}
header .icon { flex: none; width: 44px; height: 44px; border-radius: 10px; font-size: 20px; color: var(--muted); }
header button.icon:hover { background: var(--bubble); color: var(--text); }
header .spacer { display: block; }
#title { flex: 1; min-width: 0; text-align: center; font-size: 15px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.layout { position: relative; flex: 1; min-height: 0; display: flex; }
.column { flex: 1; min-width: 0; display: flex; flex-direction: column; }
aside {
  flex: none; width: 260px; padding: 4px 8px 10px; overflow-y: auto; overscroll-behavior: contain;
  background: var(--bg); border-right: 1px solid var(--line);
}
#new {
  display: block; width: 100%; min-height: 44px; margin-bottom: 6px; padding: 9px 10px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--panel); text-align: left; font-size: 15px; font-weight: 500;
}
#new:hover { background: var(--bubble); }
#chats button {
  display: block; width: 100%; min-height: 40px; padding: 9px 10px; border-radius: 8px;
  text-align: left; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#chats button:hover { background: var(--bubble); }
#chats button[aria-current] { background: var(--bubble); font-weight: 500; }
#chats button.running::before { content: "● "; color: var(--accent); }
#backdrop { position: absolute; inset: 0; z-index: 1; background: rgb(0 0 0 / .4); }
@media (max-width: 760px) {
  aside { position: absolute; inset: 0 auto 0 0; z-index: 2; width: min(280px, 82vw); transform: translateX(-100%); visibility: hidden; transition: transform .18s ease, visibility .18s; }
  aside.open { transform: none; visibility: visible; }
}
@media (min-width: 761px) {
  #backdrop { display: none; }
  .layout.collapsed aside { display: none; }
}
@media (prefers-reduced-motion: reduce) { aside { transition: none; } }

main { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
#messages { max-width: 720px; margin: 0 auto; padding: 8px 16px 16px; display: flex; flex-direction: column; gap: 18px; }
article { overflow-wrap: anywhere; white-space: pre-wrap; }
article.user { align-self: flex-end; max-width: 85%; padding: 9px 14px; border-radius: 16px; background: var(--bubble); }
article.assistant { align-self: stretch; }
article a { color: var(--accent); }
article code { padding: 1px 4px; border-radius: 4px; background: var(--bubble); font-size: .9em; }
article pre { margin: 8px 0; padding: 10px; overflow-x: auto; white-space: pre; border-radius: 8px; background: var(--bubble); font-size: 14px; }
.attached { display: block; margin-bottom: 4px; font-size: 14px; color: var(--muted); }
.step { margin: 4px 0 0; color: var(--muted); font-size: 14px; }

footer { position: relative; max-width: 720px; width: 100%; margin: 0 auto; padding: 4px 12px max(6px, env(safe-area-inset-bottom)); }
#notice { margin: 0 0 6px; color: var(--accent); font-size: 14px; text-align: center; }
#form { display: flex; flex-direction: column; padding: 10px 8px 8px 14px; border: 1px solid var(--line); border-radius: 20px; background: var(--panel); }
#files { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 6px 8px 0; }
#files span { display: inline-flex; align-items: center; max-width: 100%; height: 30px; padding: 0 4px 0 10px; border-radius: 15px; background: var(--bubble); font-size: 13px; }
#files span.loading { color: var(--muted); }
#files .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#files button { flex: none; width: 26px; height: 26px; border-radius: 50%; color: var(--muted); }
#input {
  width: 100%; min-height: 28px; max-height: 160px; padding: 0 6px 0 0; resize: none;
  border: 0; outline: 0; background: none; color: inherit; font: inherit;
}
.bar { display: flex; align-items: center; margin-top: 6px; }
#attach { flex: none; width: 36px; height: 36px; border-radius: 50%; font-size: 22px; line-height: 1; color: var(--muted); }
#attach:hover { background: var(--bubble); color: var(--text); }
#send {
  flex: none; width: 36px; height: 36px; margin-left: auto; border-radius: 50%;
  background: var(--accent); color: var(--on-accent); font-size: 18px; font-weight: 600; line-height: 1;
}
#settings { display: block; margin: 2px 0 0 8px; padding: 6px 8px; border-radius: 8px; font-size: 13px; color: var(--muted); }
#settings:hover { background: var(--bubble); color: var(--text); }
#settings::after { content: " ▴"; }

#menu {
  position: absolute; left: 12px; bottom: 100%; z-index: 3; width: min(300px, calc(100% - 24px));
  margin-bottom: 4px; padding: 10px 12px 12px; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); box-shadow: 0 6px 24px rgb(0 0 0 / .18);
}
#menu .label { margin: 0 0 4px; font-size: 13px; color: var(--muted); }
#menu .label:not(:first-child) { margin-top: 10px; }
.segment { display: flex; height: 40px; padding: 3px; border-radius: 10px; background: var(--bubble); }
.segment button { flex: 1; padding: 0 8px; border-radius: 7px; font-size: 15px; color: var(--muted); }
.segment button[aria-pressed="true"] { background: var(--selected); color: var(--on-selected); font-weight: 500; }

body.login { align-items: center; justify-content: center; padding: 24px; }
#login { display: flex; flex-direction: column; align-items: center; gap: 12px; width: 100%; max-width: 320px; }
#login input, #login button { width: 100%; height: 48px; border-radius: 12px; font: inherit; }
#login input { padding: 0 14px; border: 1px solid var(--line); background: var(--panel); color: inherit; }
#login button { background: var(--accent); color: var(--on-accent); font-weight: 600; }
#error { margin: 0; color: var(--accent); font-size: 14px; text-align: center; }
