* { box-sizing: border-box; }

:root {
  --bg: #1a1a1a;
  --bg-2: #232323;
  --bg-3: #2c2c2c;
  --bg-4: #383838;
  --fg: #e6e6e6;
  --fg-dim: #999;
  --fg-faint: #666;
  --accent: #c97f50;
  --accent-2: #b86e40;
  --border: #333;
  --user-bg: #2d3a4d;
  --tool-bg: #2a2520;
  --error: #d05c5c;
  --warning: #d0a85c;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
}
button { font: inherit; }
input, textarea { font: inherit; color: inherit; }

/* === LOGIN === */
.login-page { display: grid; place-items: center; height: 100vh; }
.login-card {
  background: var(--bg-2); padding: 2.5rem; border-radius: 12px;
  min-width: 320px; border: 1px solid var(--border);
}
.login-card h1 { margin: 0 0 1.5rem; text-align: center; font-weight: 500; }
.login-card form { display: flex; flex-direction: column; gap: .75rem; }
.login-card label { display: flex; flex-direction: column; gap: .25rem;
  font-size: .9rem; color: var(--fg-dim); }
.login-card input {
  background: var(--bg-3); border: 1px solid var(--border);
  color: var(--fg); padding: .5rem .75rem; border-radius: 6px;
  font-size: 1rem;
}
.login-card input:focus { outline: 1px solid var(--accent); }
.login-card button {
  background: var(--accent); color: white; border: 0;
  padding: .65rem; border-radius: 6px; cursor: pointer;
  font-size: 1rem; margin-top: .5rem;
}
.login-card button:hover { background: var(--accent-2); }
.error { color: var(--error); text-align: center; margin: 0 0 .5rem; }

/* === LAYOUT === */
body:not(.login-page) {
  display: grid; grid-template-columns: 280px 1fr; height: 100vh;
}

#sidebar {
  background: var(--bg-2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}

.sidebar-header {
  padding: .65rem; border-bottom: 1px solid var(--border);
  display: flex; gap: .5rem;
}
#new-chat {
  flex: 1; background: var(--accent); color: white; border: 0;
  padding: .55rem .75rem; border-radius: 6px; cursor: pointer;
  font-size: .9rem; display: inline-flex; align-items: center;
  justify-content: center; gap: .4rem;
}
#new-chat:hover { background: var(--accent-2); }
.logout-form { margin: 0; }
.logout-form button {
  background: transparent; border: 1px solid var(--border); color: var(--fg-dim);
  padding: .55rem .65rem; border-radius: 6px; cursor: pointer;
  display: flex; align-items: center;
}
.logout-form button:hover { color: var(--fg); border-color: var(--fg-dim); }

.sidebar-search { padding: .5rem .65rem; }
#search-input {
  width: 100%; background: var(--bg-3); border: 1px solid var(--border);
  color: var(--fg); padding: .45rem .65rem; border-radius: 6px;
  font-size: .85rem;
}
#search-input:focus { outline: 1px solid var(--accent); }

.sidebar-section { padding: .25rem .35rem; flex-shrink: 0; }
.sidebar-section h3 {
  font-size: .7rem; text-transform: uppercase; letter-spacing: .1em;
  color: var(--fg-faint); margin: .65rem .65rem .25rem;
  font-weight: 600;
  display: flex; align-items: center; justify-content: space-between;
}
.icon-btn {
  background: transparent; border: 0; color: var(--fg-dim);
  cursor: pointer; padding: .25rem .4rem; border-radius: 4px;
  display: inline-flex; align-items: center; font-size: 1rem;
}
.icon-btn:hover { background: var(--bg-3); color: var(--fg); }
.icon-btn.danger:hover { background: rgba(208, 92, 92, 0.15); color: var(--error); }

#projects-list, .conv-list, #conv-groups {
  list-style: none; margin: 0; padding: 0;
}
#conversations-section { flex: 1; overflow-y: auto; }
.conv-group {
  font-size: .65rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--fg-faint); padding: .5rem .85rem .15rem; margin-top: .25rem;
}

.conv-list li, #projects-list li {
  padding: .45rem .65rem; border-radius: 6px; cursor: pointer;
  font-size: .87rem; color: var(--fg-dim); margin: 1px 0;
  display: flex; align-items: center; gap: .4rem;
  position: relative;
}
.conv-list li .conv-text, #projects-list li .conv-text {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-list li:hover, #projects-list li:hover {
  background: var(--bg-3); color: var(--fg);
}
.conv-list li.active, #projects-list li.active {
  background: var(--bg-3); color: var(--fg);
}
.conv-list li.starred::before { content: "★"; color: var(--accent); margin-right: 2px; }
.conv-list li .stream-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); flex-shrink: 0;
  animation: stream-pulse 1.4s ease-in-out infinite;
  margin-right: 2px;
}
@keyframes stream-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: .35; transform: scale(.75); }
}
#projects-list li .project-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.conv-list li .conv-menu {
  background: transparent; border: 0; color: var(--fg-faint);
  cursor: pointer; padding: 2px 6px; border-radius: 3px;
  opacity: 0; font-size: 1rem; line-height: 1;
}
.conv-list li:hover .conv-menu { opacity: 1; }
.conv-list li .conv-menu:hover { background: var(--bg-4); color: var(--fg); }

.conv-list li .snippet {
  font-size: .75rem; color: var(--fg-faint); margin-top: 2px;
}
.conv-list li mark { background: var(--accent); color: white; padding: 0 2px; border-radius: 2px; }

/* === CHAT === */
#chat { display: flex; flex-direction: column; overflow: hidden; }
#chat-header {
  padding: .6rem 1.25rem; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  min-height: 50px;
}
.title-wrap { display: flex; align-items: center; gap: .5rem; flex: 1; min-width: 0; }
#chat-title {
  margin: 0; font-size: 1rem; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  cursor: pointer;
}
#chat-title:hover { color: var(--accent); }
.project-badge {
  font-size: .7rem; padding: 2px 8px; border-radius: 10px;
  background: var(--bg-3); color: var(--fg-dim);
  border-left: 3px solid var(--accent);
}
.header-actions { display: flex; align-items: center; gap: .25rem; }
#status { font-size: .8rem; color: var(--fg-dim); margin-left: .5rem; }
#star-btn.active { color: var(--accent); }

#messages {
  flex: 1; overflow-y: auto; padding: 2rem 1.5rem;
}
.messages-inner { max-width: 800px; margin: 0 auto; }
.msg { margin-bottom: 1.5rem; }
.msg .role {
  font-size: .7rem; color: var(--fg-faint);
  text-transform: uppercase; letter-spacing: .08em;
  margin-bottom: .35rem; font-weight: 600;
}
.msg.user {
  background: var(--user-bg); padding: .65rem 1rem;
  border-radius: 12px; max-width: 85%; margin-left: auto;
}
.msg.user .role { display: none; }
.msg .content {
  line-height: 1.65; word-wrap: break-word;
}
.msg .content > *:first-child { margin-top: 0; }
.msg .content > *:last-child { margin-bottom: 0; }

.msg .content p { margin: .5em 0; }
.msg .content h1, .msg .content h2, .msg .content h3,
.msg .content h4, .msg .content h5, .msg .content h6 {
  margin: 1em 0 .4em; line-height: 1.3;
}
.msg .content h1 { font-size: 1.4rem; }
.msg .content h2 { font-size: 1.2rem; }
.msg .content h3 { font-size: 1.05rem; }
.msg .content ul, .msg .content ol { margin: .5em 0; padding-left: 1.5em; }
.msg .content li { margin: .25em 0; }
.msg .content a { color: var(--accent); text-decoration: underline; }
.msg .content blockquote {
  margin: .5em 0; padding: .25em 1em; border-left: 3px solid var(--bg-4);
  color: var(--fg-dim);
}
.msg .content code {
  background: var(--bg-3); padding: 1px 5px; border-radius: 3px;
  font-family: var(--mono); font-size: .88em;
}
.msg .content pre {
  background: var(--bg-3); padding: .85rem 1rem; border-radius: 8px;
  overflow-x: auto; font-family: var(--mono); font-size: .85em;
  border: 1px solid var(--border); margin: .5em 0;
  position: relative;
}
.msg .content pre code { background: transparent; padding: 0; font-size: 1em; }
.msg .content pre .copy-btn {
  position: absolute; top: 4px; right: 4px;
  background: var(--bg-2); border: 1px solid var(--border); color: var(--fg-dim);
  font-size: .7rem; padding: 2px 8px; border-radius: 3px; cursor: pointer;
  opacity: 0; transition: opacity .15s;
}
.msg .content pre:hover .copy-btn { opacity: 1; }
.msg .content pre .copy-btn:hover { color: var(--fg); }
.msg .content table {
  border-collapse: collapse; margin: .5em 0; font-size: .9em;
}
.msg .content th, .msg .content td {
  border: 1px solid var(--border); padding: .35em .65em;
}
.msg .content th { background: var(--bg-3); }
.msg .content hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }

.tool-call {
  display: inline-flex; align-items: baseline; gap: .35em;
  background: var(--tool-bg); color: var(--accent);
  padding: 3px 10px; border-radius: 4px; font-family: var(--mono);
  font-size: .8em; margin: 3px 4px 3px 0; border-left: 2px solid var(--accent);
  vertical-align: baseline;
}
.tool-call .tool-name { font-weight: 600; }
.tool-call .tool-arg {
  color: var(--fg-dim); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; max-width: 380px; display: inline-block;
}

.msg.system {
  font-size: .85em; color: var(--fg-dim); padding: .4rem .75rem;
  background: var(--bg-2); border-radius: 6px;
  border-left: 3px solid var(--accent);
}

/* === COMPOSER === */
#composer {
  display: flex; flex-direction: column; gap: .5rem; padding: 1rem 1.5rem;
  border-top: 1px solid var(--border); background: var(--bg-2);
}
#composer.drag-over {
  background: var(--bg-3);
  outline: 2px dashed var(--accent); outline-offset: -8px;
}
.composer-row { display: flex; gap: .5rem; align-items: flex-end; }
.composer-inner { max-width: 800px; margin: 0 auto; width: 100%; }
#input {
  flex: 1; resize: none; background: var(--bg-3);
  border: 1px solid var(--border); border-radius: 12px;
  padding: .75rem 1rem; line-height: 1.4;
  max-height: 220px; font-family: inherit;
}
#input:focus { outline: 1px solid var(--accent); border-color: transparent; }
#composer button {
  background: var(--accent); color: white; border: 0;
  padding: 0; width: 42px; height: 42px; border-radius: 50%;
  cursor: pointer; display: inline-flex; align-items: center;
  justify-content: center; flex-shrink: 0;
}
#composer button:hover { background: var(--accent-2); }
#composer button:disabled { background: var(--bg-4); cursor: not-allowed; }
#composer #stop { background: var(--error); }
#composer .composer-icon {
  background: transparent; border: 1px solid var(--border);
  color: var(--fg-dim); width: 42px; height: 42px;
}
#composer .composer-icon:hover {
  background: var(--bg-3); color: var(--fg); border-color: var(--bg-4);
}
#composer .composer-icon.active {
  color: var(--accent); border-color: var(--accent); background: var(--bg-3);
}
#mic-btn.recording {
  color: var(--error); border-color: var(--error);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .55; }
}

/* === ATTACHMENTS === */
#attach-preview {
  display: flex; flex-wrap: wrap; gap: .5rem;
  padding: .35rem 0;
}
#attach-preview[hidden] { display: none; }
.attach-chip {
  display: inline-flex; align-items: center; gap: .45rem;
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 8px; padding: .25rem .35rem .25rem .5rem;
  font-size: .82rem; color: var(--fg-dim);
  max-width: 240px;
}
.attach-chip.uploading { opacity: .6; }
.attach-chip img.thumb {
  width: 36px; height: 36px; object-fit: cover; border-radius: 4px;
}
.attach-chip .icon-tile {
  width: 36px; height: 36px; border-radius: 4px; background: var(--bg-4);
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--fg-dim); font-size: 1rem;
}
.attach-chip .att-name {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 140px;
}
.attach-chip .att-remove {
  background: transparent; border: 0; color: var(--fg-faint);
  cursor: pointer; padding: 0 .35rem; font-size: 1.05rem; line-height: 1;
}
.attach-chip .att-remove:hover { color: var(--error); }

/* Attachments shown inside a sent message */
.msg .attachments {
  display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: .5rem;
}
.msg .attachments img.msg-img {
  max-width: 280px; max-height: 280px;
  border-radius: 8px; cursor: pointer; display: block;
  border: 1px solid var(--border);
}
.msg .attachments .att-file {
  display: inline-flex; align-items: center; gap: .5rem;
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 8px; padding: .45rem .65rem; font-size: .85rem;
  color: var(--fg); text-decoration: none;
}
.msg .attachments .att-file:hover { background: var(--bg-4); }
.msg.user .attachments img.msg-img { max-width: 220px; max-height: 220px; }
@media (max-width: 768px) {
  .msg.user .attachments img.msg-img { max-width: 60vw; max-height: 60vw; }
  .msg .attachments img.msg-img { max-width: 70vw; max-height: 70vw; }
}

.empty {
  text-align: center; color: var(--fg-dim); margin-top: 4rem;
}
.empty h2 { font-weight: 400; color: var(--fg); margin-bottom: 1rem; }
.empty .suggestions {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: .75rem; max-width: 640px; margin: 1.5rem auto 0;
}
.empty .suggestion {
  text-align: left; padding: .85rem 1rem; background: var(--bg-2);
  border: 1px solid var(--border); border-radius: 10px; cursor: pointer;
  font-size: .87rem; line-height: 1.4; color: var(--fg-dim);
}
.empty .suggestion:hover {
  background: var(--bg-3); border-color: var(--bg-4); color: var(--fg);
}
.empty .suggestion strong { color: var(--fg); display: block; margin-bottom: 2px; }

/* === MODAL === */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal[hidden] { display: none; }
.context-menu[hidden] { display: none; }
#search-results[hidden] { display: none; }
#star-btn[hidden], #rename-btn[hidden], #delete-btn[hidden],
#stop[hidden], #project-badge[hidden] { display: none; }
.modal-card {
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 1.5rem; width: 90%; max-width: 540px;
  max-height: 85vh; overflow-y: auto;
}
.modal-card h2 { margin: 0 0 1rem; font-weight: 500; font-size: 1.1rem; }
.modal-card label {
  display: flex; flex-direction: column; gap: .25rem;
  font-size: .85rem; color: var(--fg-dim); margin-bottom: .85rem;
}
.modal-card input, .modal-card textarea {
  background: var(--bg-3); border: 1px solid var(--border);
  border-radius: 6px; padding: .5rem .75rem;
  font-family: inherit;
}
.modal-card textarea { resize: vertical; min-height: 100px; font-family: inherit; }
.modal-card input:focus, .modal-card textarea:focus {
  outline: 1px solid var(--accent); border-color: transparent;
}
/* Project files inside the project modal */
#project-files-section {
  margin-top: .25rem; margin-bottom: 1rem;
  padding-top: .85rem; border-top: 1px solid var(--border);
}
.project-files-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: .5rem; margin-bottom: .5rem;
}
.project-files-label {
  font-size: .85rem; color: var(--fg-dim);
}
.ghost-btn {
  background: transparent; border: 1px solid var(--border); color: var(--fg);
  padding: .25rem .6rem; border-radius: 6px; cursor: pointer; font-size: .8rem;
}
.ghost-btn:hover { background: var(--bg-3); }
.project-files-list {
  list-style: none; margin: 0 0 .5rem 0; padding: 0;
  max-height: 220px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: 6px;
}
.project-file-row {
  display: flex; align-items: center; gap: .5rem;
  padding: .4rem .6rem; border-bottom: 1px solid var(--border);
  font-size: .85rem;
}
.project-file-row:last-child { border-bottom: 0; }
.project-file-icon { font-size: 1rem; flex-shrink: 0; }
.project-file-name {
  flex: 1; min-width: 0; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.project-file-meta { color: var(--fg-faint); font-size: .75rem; flex-shrink: 0; }
.project-file-del {
  background: transparent; border: 0; color: var(--fg-dim); cursor: pointer;
  font-size: 1.1rem; line-height: 1; padding: 0 .25rem;
}
.project-file-del:hover { color: var(--error); }
.project-file-empty {
  list-style: none; padding: .6rem; text-align: center;
  color: var(--fg-faint); font-size: .8rem;
}
.project-files-hint {
  margin: 0; font-size: .75rem; color: var(--fg-faint);
}

.modal-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; }
.modal-actions button {
  padding: .5rem 1rem; border-radius: 6px; border: 0; cursor: pointer;
  background: var(--accent); color: white;
}
.modal-actions button:hover { background: var(--accent-2); }
.modal-actions button.ghost {
  background: transparent; border: 1px solid var(--border); color: var(--fg);
}
.modal-actions button.ghost:hover { background: var(--bg-3); }
.modal-actions button.ghost.danger { color: var(--error); border-color: rgba(208, 92, 92, 0.3); }
.modal-actions button.ghost.danger:hover { background: rgba(208, 92, 92, 0.1); }

/* === Context menu === */
.context-menu {
  position: fixed; z-index: 200; min-width: 180px;
  background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 8px; padding: .25rem; list-style: none;
  margin: 0; box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
.context-menu li {
  padding: .45rem .85rem; border-radius: 4px; cursor: pointer;
  font-size: .87rem; display: flex; align-items: center; gap: .5rem;
}
.context-menu li:hover { background: var(--bg-3); }
.context-menu li.danger { color: var(--error); }
.context-menu li.divider {
  border-top: 1px solid var(--border); margin: .25rem 0; padding: 0;
  cursor: default; pointer-events: none;
}

/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg-faint); }

/* === MOBILE === */
.mobile-only { display: none; }
#sidebar-backdrop { display: none; }

@media (max-width: 768px) {
  body:not(.login-page) {
    grid-template-columns: 1fr;
  }

  /* Sidebar becomes off-canvas drawer */
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 86vw; max-width: 320px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,0.45);
  }
  body.sidebar-open #sidebar { transform: translateX(0); }

  #sidebar-backdrop {
    display: block;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 40;
    opacity: 0; pointer-events: none;
    transition: opacity .22s ease;
  }
  body.sidebar-open #sidebar-backdrop {
    opacity: 1; pointer-events: auto;
  }

  .mobile-only { display: inline-flex; }

  #chat-header {
    padding: .55rem .75rem;
    gap: .5rem;
  }
  #chat-title { font-size: .95rem; }
  .project-badge { display: none; }
  #status { display: none; }

  #messages {
    padding: 1rem .85rem;
  }
  .msg.user { max-width: 92%; padding: .55rem .85rem; }

  #composer {
    padding: .65rem .75rem;
    gap: .4rem;
  }
  #input {
    padding: .65rem .85rem;
    border-radius: 10px;
    max-height: 160px;
  }
  #composer button {
    width: 38px; height: 38px;
  }

  .empty { margin-top: 2rem; }
  .empty .suggestions {
    grid-template-columns: 1fr;
    gap: .5rem;
  }

  .tool-call .tool-arg { max-width: 180px; }

  .modal-card {
    padding: 1.1rem;
    width: 94%;
    max-height: 90vh;
  }

  /* Avoid iOS zoom on input focus */
  #input, #search-input,
  .modal-card input, .modal-card textarea,
  .login-card input {
    font-size: 16px;
  }
}

@media (max-width: 380px) {
  #chat-title { font-size: .9rem; }
  .header-actions { gap: 0; }
  #messages { padding: .85rem .6rem; }
}
