/* ── Reset & base ────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0f0f12; --surface: #1a1a20; --border: #2a2a35;
  --text: #e0e0e8; --muted: #88889a; --accent: #b084cc; --accent-dim: #7a5f99;
  --danger: #c44a6a; --hover: #2a2a35; --radius: 8px; --shadow: 0 2px 12px rgba(0,0,0,.4);
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --sidebar-w: 280px;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font: 14px/1.5 var(--font); }

a { color: var(--accent); }
a:hover { color: #c9a4e0; }

/* ── Layout ──────────────────────────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
#sidebar {
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

#sidebar header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  position: relative;
}

#sidebar header h1 {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.3px;
}

.sidebar-header-actions {
  display: flex;
  gap: 4px;
}

.sidebar-header-actions button {
  font-size: 11px;
  padding: 4px 8px;
  border-radius: var(--radius);
  background: var(--hover);
  color: var(--text);
  border: 1px solid var(--border);
  cursor: pointer;
  min-height: 28px;
  white-space: nowrap;
}

.sidebar-header-actions button:hover {
  background: var(--accent-dim);
  border-color: var(--accent);
}

#sidebar footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}

#sidebar footer .row { display: flex; gap: 4px; }

#hamburger,
#hamburger-main { display: none; }

/* ── Folder tree ─────────────────────────────────────────────────────────── */
.sidebar-body { flex: 1; overflow-y: auto; }

.folder-tree { list-style: none; padding: 6px; }

.folder-row {
  display: flex; align-items: center; padding: 7px 10px;
  border-radius: var(--radius); cursor: pointer; user-select: none;
  transition: background .1s;
  min-height: 36px;
}
.folder-row:hover { background: var(--hover); }
.folder-row.active { background: var(--accent-dim); color: #fff; }

.folder-name { flex: 1; font-size: 13px; font-weight: 500; }
.folder-count { font-size: 11px; color: var(--muted); margin-left: 8px; }

.folder-block { margin-bottom: 2px; }

.folder-actions { display: flex; gap: 2px; margin-left: 6px; }
.folder-actions button {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 14px; padding: 2px 4px; border-radius: 4px; line-height: 1;
}
.folder-actions button:hover { background: rgba(255,255,255,.08); color: var(--text); }
.folder-row.readonly { opacity: .6; cursor: default; }

.chat-list { list-style: none; padding-left: 16px; }
.chat-item {
  display: flex; align-items: center; padding: 5px 10px; cursor: pointer;
  border-radius: var(--radius); transition: background .1s; min-height: 36px;
  font-size: 13px;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--accent-dim); color: #fff; }
.chat-item .chat-labels { font-size: 10px; color: var(--muted); margin-left: auto; display: flex; gap: 3px; flex-wrap: wrap; }

/* ── Main content ────────────────────────────────────────────────────────── */
#main {
  display: flex; flex-direction: column; overflow: hidden;
  position: relative;
}

#mobile-bar { display: none; }

.empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); text-align: center; padding: 32px;
}
.empty h2 { font-size: 18px; margin-bottom: 8px; color: var(--text); }
.empty p { font-size: 13px; max-width: 280px; }
.empty-cta {
  margin-top: 20px; padding: 10px 24px; font-size: 14px;
  background: var(--accent); color: #fff; border: none; border-radius: var(--radius);
  cursor: pointer; min-height: 44px;
}
.empty-cta:hover { background: #c9a4e0; }

/* ── Chat view ───────────────────────────────────────────────────────────── */
#chat { flex: 1; display: flex; flex-direction: column; }

#chat > header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
#chat-title { font-size: 15px; font-weight: 600; background: none; border: none; color: var(--text); flex: 1; min-width: 120px; outline: none; }
#folder-picker { font-size: 12px; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 4px; padding: 4px 8px; }
#chat > header button {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 16px; padding: 4px 6px; border-radius: 4px; min-height: 36px; min-width: 36px;
}
#chat > header button:hover { background: var(--hover); color: var(--text); }

.label-bar {
  display: flex; gap: 4px; flex-wrap: wrap; padding: 6px 14px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.label-chip {
  font-size: 11px; background: var(--hover); color: var(--accent);
  padding: 2px 8px; border-radius: 10px;
}

/* ── Messages ────────────────────────────────────────────────────────────── */
#messages {
  flex: 1; overflow-y: auto; padding: 14px; list-style: none;
  display: flex; flex-direction: column; gap: 8px;
}

.msg {
  max-width: 75%; padding: 10px 14px; border-radius: 12px;
  line-height: 1.5; word-wrap: break-word; white-space: pre-wrap;
  font-size: 14px;
}
.msg.user {
  align-self: flex-end; background: var(--accent-dim); color: #fff;
  border-bottom-right-radius: 4px;
}
.msg.assistant {
  align-self: flex-start; background: var(--surface);
  border: 1px solid var(--border); border-bottom-left-radius: 4px;
}
.msg p { margin-bottom: 4px; }
.msg p:last-child { margin-bottom: 0; }
.msg code { background: rgba(0,0,0,.3); padding: 1px 4px; border-radius: 3px; font-size: 13px; }
.msg pre { background: #0a0a0d; padding: 10px; border-radius: 6px; overflow-x: auto; margin: 4px 0; }
.msg a { color: #c9a4e0; }

/* ── Attachments ─────────────────────────────────────────────────────────── */
.msg-attachment {
  display: block; margin-top: 6px; max-width: 100%;
}
.msg-attachment img {
  max-width: 100%; max-height: 300px; border-radius: 8px; cursor: pointer;
  transition: opacity .15s;
}
.msg-attachment img:hover { opacity: .85; }
.msg-attachment .doc-link {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; background: var(--hover); border-radius: 6px;
  text-decoration: none; color: var(--text); font-size: 13px;
}
.msg-attachment .doc-link:hover { background: var(--accent-dim); color: #fff; }

/* ── Composer ────────────────────────────────────────────────────────────── */
#composer {
  padding: 10px 14px; border-top: 1px solid var(--border);
  background: var(--surface);
}
#input {
  width: 100%; padding: 10px 12px; font: inherit; font-size: 14px;
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: var(--radius); resize: none; outline: none;
}
#input:focus { border-color: var(--accent); }

#composer .row { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; }

.composer-actions { display: flex; gap: 6px; align-items: center; }

#upload-btn, #send {
  border: none; border-radius: var(--radius); cursor: pointer;
  font-size: 14px; padding: 8px 16px; min-height: 44px; transition: opacity .15s;
}
#upload-btn { background: none; color: var(--muted); font-size: 20px; padding: 8px 10px; }
#upload-btn:hover { color: var(--text); }
#upload-btn.inflight { opacity: .4; pointer-events: none; }
#send {
  background: var(--accent); color: #fff; font-weight: 500;
}
#send:hover:not(:disabled) { background: #c9a4e0; }
#send:disabled { opacity: .4; cursor: default; }

#status { font-size: 12px; color: var(--muted); flex: 1; }

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.6);
}
.modal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; width: 400px; max-width: 90vw; max-height: 80vh;
  overflow-y: auto; box-shadow: var(--shadow);
}
.modal-card header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.modal-card header h3 { font-size: 15px; font-weight: 600; }
.modal-card .x { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 20px; padding: 2px 6px; border-radius: 4px; }
.modal-card .x:hover { background: var(--hover); color: var(--text); }
.modal-body { padding: 18px; }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; font-weight: 500; margin-bottom: 4px; color: var(--muted); }
.field input, .field select {
  width: 100%; padding: 8px 10px; font: inherit; font-size: 14px;
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }

.modal-body .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.modal-body button {
  padding: 8px 18px; font-size: 13px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--hover); color: var(--text);
  cursor: pointer; min-height: 38px;
}
.modal-body button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.modal-body button.primary:hover { background: #c9a4e0; }
.modal-body button:hover:not(.primary) { background: var(--border); }

/* ── Lightbox ────────────────���───────���───────────────────────────────────── */
#lightbox {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.85); cursor: zoom-out;
}
#lightbox img {
  max-width: 90vw; max-height: 90vh; border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0,0,0,.6);
}

/* ── Chip row (labels) ───────────────────────────────────────────────────── */
.chip-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.chip-row .chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: var(--hover); padding: 4px 10px; border-radius: 20px;
  font-size: 12px;
}
.chip-row .chip .del { cursor: pointer; opacity: .6; font-size: 14px; }
.chip-row .chip .del:hover { opacity: 1; color: var(--danger); }

/* ── Upload progress ─────────────────────────────────────────────────────── */
#upload-progress { font-size: 12px; color: var(--muted); padding: 10px 14px; text-align: center; }

/* ── Responsive ──────────────────────────────────────────────────────────── */
#sidebar-overlay { display: none; }

@media (max-width: 768px) {
  #hamburger { display: inline-flex; }
  #hamburger-main { display: inline-flex; font-size: 22px; background: none; border: none; color: var(--text); cursor: pointer; padding: 8px; min-height: 44px; min-width: 44px; }
  .sidebar-header-actions button { font-size: 10px; padding: 3px 6px; min-height: 26px; }

  #app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 50; transform: translateX(-100%); transition: transform .25s ease; }
  #app.sidebar-open #sidebar { transform: translateX(0); }
  #sidebar-overlay { display: block; position: fixed; inset: 0; z-index: 49; background: rgba(0,0,0,.5); opacity: 0; pointer-events: none; transition: opacity .25s; }
  #app.sidebar-open #sidebar-overlay { opacity: 1; pointer-events: auto; }

  #mobile-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--surface); }
  #mobile-title { font-size: 15px; font-weight: 600; }

  .msg { max-width: 90%; }
  .msg.user { max-width: 90%; }

  #input { font-size: 16px; }
  #upload-btn, #send { min-height: 44px; }

  .login-card { width: 90vw; max-width: 320px; }
}

@media (max-width: 480px) {
  #sidebar { width: 100%; }
  .sidebar-header-actions button { font-size: 10px; padding: 3px 6px; }
  .msg { max-width: 95%; font-size: 15px; }
  .login-card { width: 100%; max-width: 100%; margin: 0 12px; }
  .login-card h1 { font-size: 22px; }
}
