:root {
  color-scheme: light dark;
  --bg: #f7f7f5;
  --panel: #ffffff;
  --border: #e2e1dc;
  --text: #1d1d1b;
  --muted: #6b6a64;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --danger: #b3382c;
  --danger-text: #ffffff;
  --user: #e8efe9;
  --code: #efeeea;
  --warn-bg: #fff6e5;
  --warn-border: #e6c378;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171716;
    --panel: #201f1e;
    --border: #383733;
    --text: #ecebe6;
    --muted: #a09f97;
    --accent: #6fb59f;
    --accent-text: #10201b;
    --danger: #e0695c;
    --danger-text: #240806;
    --user: #2a3631;
    --code: #2b2a28;
    --warn-bg: #33291a;
    --warn-border: #7c6330;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, input, textarea { font: inherit; color: inherit; }
button { border: 1px solid transparent; border-radius: 8px; padding: 9px 16px; cursor: pointer; }
button:disabled { opacity: .55; cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.danger { background: var(--danger); color: var(--danger-text); font-weight: 600; }
.ghost { background: transparent; border-color: var(--border); }
.small { padding: 5px 10px; font-size: 13px; }
.muted { color: var(--muted); }

/* Login */
.login-body { display: flex; align-items: center; justify-content: center; padding: 16px; min-height: 100%; }
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
}
.login-card h1 { margin: 0; font-size: 24px; }
.login-card p.muted { margin: 4px 0 20px; }
.login-card label { display: block; margin: 14px 0 5px; font-weight: 600; font-size: 14px; }
.login-card input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.login-card .primary { width: 100%; margin-top: 20px; }
.form-error { color: var(--danger); margin: 12px 0 0; font-size: 14px; }

/* Estrutura */
.layout { display: flex; height: 100dvh; }
.sidebar {
  width: 280px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
.sidebar-head, .sidebar-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
}
.sidebar-foot { border-top: 1px solid var(--border); }
.sidebar-foot span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conversation {
  display: block;
  width: 100%;
  padding: 8px 10px;
  margin-bottom: 2px;
  background: transparent;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation:hover { background: var(--bg); }
.conversation[aria-current] { background: var(--user); font-weight: 600; }
.scrim { display: none; }

/* Chat */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}
.chat-head h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.menu { display: none; }
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px max(16px, calc((100% - 800px) / 2));
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.empty { margin: auto; color: var(--muted); }
.bubble { max-width: 100%; overflow-wrap: anywhere; }
.bubble.user {
  align-self: flex-end;
  max-width: 85%;
  padding: 9px 14px;
  background: var(--user);
  border-radius: 14px;
  white-space: pre-wrap;
}
.bubble.assistant p { margin: 0 0 10px; }
.bubble.assistant > :last-child { margin-bottom: 0; }
.bubble h2, .bubble h3, .bubble h4, .bubble h5 { margin: 14px 0 6px; font-size: 1.05em; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble a { color: var(--accent); }
code { background: var(--code); padding: 1px 5px; border-radius: 5px; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
pre {
  margin: 0 0 10px;
  padding: 10px 12px;
  background: var(--code);
  border-radius: 8px;
  overflow-x: auto;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
pre code { padding: 0; background: none; }
.note { margin: 0; font-size: 13px; color: var(--muted); }
.note-warn { color: var(--danger); }

/* Aprovacao */
.approvals { padding: 0 max(16px, calc((100% - 800px) / 2)); }
.approval {
  margin-bottom: 12px;
  padding: 12px 14px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: 10px;
}
.approval h2 { margin: 0 0 4px; font-size: 15px; }
.approval p { margin: 0 0 8px; font-size: 14px; }
.approval pre { max-height: 200px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--panel); }
.actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }

/* Composer */
.chat-error { padding: 0 max(16px, calc((100% - 800px) / 2)); margin: 0 0 8px; }
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px max(16px, calc((100% - 800px) / 2)) max(14px, env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1;
  min-width: 0;
  max-height: 200px;
  padding: 10px 12px;
  resize: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}

@media (max-width: 720px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(85vw, 300px);
    transform: translateX(-100%);
    transition: transform .15s ease;
  }
  .sidebar.open { transform: none; }
  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 10;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: rgba(0, 0, 0, .4);
  }
  .menu { display: inline-block; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
