:root {
  --bg: #0d0d0d;
  --surface: #171717;
  --surface2: #1f1f1f;
  --surface3: #2a2a2a;
  --border: #2d2d2d;
  --accent: #8b5cf6;
  --accent-d: rgba(139, 92, 246, 0.14);
  --text: #e5e5e5;
  --muted: #9ca3af;
  --dim: #6b7280;
  --green: #22c55e;
  --red: #ef4444;
  --yellow: #f59e0b;
  --sidebar: 260px;
  --notes: 250px;
  --header: 52px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
}

button { cursor: pointer; border: none; background: none; color: inherit; font: inherit; }
input, textarea { font: inherit; color: inherit; outline: none; }

.hidden { display: none !important; }

/* Login */
#login-screen {
  position: fixed; inset: 0; background: var(--bg);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 2rem; width: 320px; text-align: center;
}
.login-title { font-size: 1.5rem; font-weight: 700; margin-bottom: 1.25rem; }
.login-input {
  width: 100%; background: var(--surface2); border: 1px solid var(--border);
  border-radius: 10px; padding: .7rem 1rem; margin-bottom: .8rem;
}
.login-input:focus { border-color: var(--accent); }
.login-btn {
  width: 100%; background: var(--accent); color: #fff; border-radius: 10px;
  padding: .7rem; font-weight: 600;
}
.login-err { color: var(--red); font-size: .8rem; margin-top: .6rem; min-height: 1.2em; }

/* App */
.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.header {
  height: var(--header); background: var(--surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: .5rem; padding: 0 .75rem; flex-shrink: 0;
}
.header-title { font-weight: 700; font-size: 1rem; display: flex; align-items: center; gap: .4rem; }
.header-spacer { flex: 1; }
.icon-btn {
  color: var(--muted); padding: 6px 10px; border-radius: 8px; font-size: 1.1rem;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--surface2); color: var(--text); }

.main { display: flex; flex: 1; overflow: hidden; position: relative; }

/* Sidebar */
.sidebar {
  width: var(--sidebar); background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0;
  transition: transform .2s; z-index: 20;
}
.sb-hdr {
  display: flex; align-items: center; justify-content: space-between;
  padding: .55rem .85rem; border-bottom: 1px solid var(--border);
  font-size: .72rem; font-weight: 700; color: var(--muted); text-transform: uppercase;
}
.sb-body { flex: 1; overflow-y: auto; padding: .3rem 0; }
.folder { user-select: none; }
.folder-hdr {
  display: flex; align-items: center; gap: .35rem; padding: .4rem .75rem;
  cursor: pointer; border-radius: 6px; margin: 1px .35rem; transition: background .12s;
}
.folder-hdr:hover { background: var(--surface2); }
.folder-chevron { font-size: .6rem; color: var(--dim); transition: transform .15s; }
.folder-chevron.open { transform: rotate(90deg); }
.folder-name { flex: 1; font-weight: 600; font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-acts { display: none; gap: 2px; }
.folder-hdr:hover .folder-acts { display: flex; }
.folder-children { padding-left: .6rem; }
.sess-item {
  display: flex; align-items: center; gap: .35rem; padding: .32rem .75rem;
  cursor: pointer; border-radius: 6px; margin: 1px .35rem; font-size: .82rem;
}
.sess-item:hover { background: var(--surface2); }
.sess-item.active { background: var(--accent-d); color: var(--accent); }
.sess-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sess-acts { display: none; gap: 2px; }
.sess-item:hover .sess-acts { display: flex; }
.sb-empty { padding: 2rem 1rem; text-align: center; color: var(--dim); font-size: .82rem; }

/* Chat */
.chat { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; background: var(--bg); }
.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .75rem; }
.empty-chat {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--dim); gap: .5rem; text-align: center; padding: 2rem;
}

.msg { max-width: 92%; display: flex; flex-direction: column; gap: .25rem; }
.msg.user { align-self: flex-end; }
.msg.assistant, .msg.tool { align-self: flex-start; }
.msg-bubble {
  padding: .7rem .9rem; border-radius: 14px; line-height: 1.55; word-break: break-word;
}
.msg.user .msg-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .msg-bubble { background: var(--surface2); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg-meta { font-size: .7rem; color: var(--dim); }

/* Markdown inside assistant messages */
.msg-bubble p { margin-bottom: .45rem; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble ul, .msg-bubble ol { margin: .4rem 0 .4rem 1.2rem; }
.msg-bubble li { margin: .12rem 0; }
.msg-bubble code {
  background: rgba(255,255,255,.08); padding: .08rem .32rem; border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: .92em;
}
.msg-bubble pre {
  background: rgba(0,0,0,.35); border: 1px solid var(--border); border-radius: 10px;
  padding: .7rem; overflow-x: auto; margin: .5rem 0;
}
.msg-bubble pre code { background: none; padding: 0; border-radius: 0; }
.msg-bubble blockquote { border-left: 3px solid var(--accent); padding-left: .7rem; color: var(--muted); margin: .5rem 0; }
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 { font-weight: 700; margin: .6rem 0 .3rem; }
.msg-bubble h1 { font-size: 1.25rem; }
.msg-bubble h2 { font-size: 1.1rem; }
.msg-bubble h3 { font-size: .95rem; }
.msg-bubble table { border-collapse: collapse; margin: .5rem 0; }
.msg-bubble th, .msg-bubble td { border: 1px solid var(--border); padding: .35rem .5rem; }
.msg-bubble th { background: var(--surface); }

.thoughts {
  background: var(--surface); border: 1px dashed var(--border); border-radius: 8px;
  padding: .5rem .7rem; font-size: .8rem; color: var(--muted); margin-top: .3rem;
}
.thoughts summary { cursor: pointer; }
.thoughts-body { margin-top: .4rem; white-space: pre-wrap; }

.msg.tool { align-self: flex-start; max-width: 92%; }
.msg.tool > summary {
  list-style: none; cursor: pointer; user-select: none;
  display: flex; align-items: center; gap: .4rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: .5rem .75rem; font-size: .82rem; color: var(--text);
}
.msg.tool > summary::-webkit-details-marker { display: none; }
.msg.tool > summary::before {
  content: '▶'; font-size: .65rem; color: var(--dim); transition: transform .15s;
}
.msg.tool[open] > summary::before { transform: rotate(90deg); }
.tool-card {
  background: var(--surface); border: 1px solid var(--border); border-top: none;
  border-radius: 0 0 10px 10px; padding: .6rem .75rem; font-size: .82rem;
}
.tool-summary { font-weight: 600; }
.tool-body { color: var(--text); white-space: pre-wrap; }
.tool-status { font-size: .75rem; margin-left: auto; }
.tool-status.done { color: var(--green); }
.tool-status.in_progress { color: var(--yellow); }

/* Input */
.input-area {
  border-top: 1px solid var(--border); background: var(--surface);
  padding: .6rem .75rem; display: flex; flex-direction: column; gap: .4rem; flex-shrink: 0;
}
.attach-bar { display: flex; gap: .4rem; flex-wrap: wrap; }
.attach-chip { background: var(--surface2); border: 1px solid var(--border); border-radius: 6px; padding: .2rem .5rem; font-size: .75rem; display: flex; align-items: center; gap: .3rem; }
.input-row { display: flex; align-items: flex-end; gap: .4rem; }
.input-area textarea {
  flex: 1; background: var(--surface2); border: 1px solid var(--border); border-radius: 12px;
  padding: .65rem .85rem; resize: none; min-height: 44px; max-height: 160px; line-height: 1.5;
}
.input-area textarea:focus { border-color: var(--accent); }
.send-btn { background: var(--accent); color: #fff; border-radius: 12px; padding: .55rem .9rem; font-weight: 600; }
.send-btn:disabled { opacity: .5; }
.mic-btn, .attach-btn { color: var(--muted); padding: .55rem; border-radius: 12px; }
.mic-btn:hover, .attach-btn:hover { background: var(--surface2); color: var(--text); }
.mic-btn.recording { color: var(--red); background: rgba(239,68,68,.12); }

/* Notes */
.notes {
  width: var(--notes); background: var(--surface); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0; transition: transform .2s; z-index: 20;
}
.notes-hdr { display: flex; align-items: center; justify-content: space-between; padding: .55rem .85rem; border-bottom: 1px solid var(--border); font-size: .72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; }
.notes-body { flex: 1; overflow-y: auto; padding: .4rem; }
.note-card { background: var(--surface2); border: 1px solid var(--border); border-radius: 8px; padding: .5rem; margin-bottom: .35rem; cursor: pointer; position: relative; }
.note-card:hover { border-color: var(--accent); }
.note-title { font-weight: 600; font-size: .8rem; margin-bottom: .2rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-text { font-size: .75rem; color: var(--muted); overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
.note-form { background: var(--surface2); border: 1px solid var(--accent); border-radius: 8px; padding: .5rem; margin-bottom: .35rem; }
.note-form input, .note-form textarea { width: 100%; background: transparent; border: none; border-bottom: 1px solid var(--border); margin-bottom: .3rem; font-size: .8rem; padding: .2rem 0; }
.note-form textarea { resize: none; min-height: 50px; }

/* Mobile overlays */
.mob-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 15; display: none; }
.mob-overlay.show { display: block; }

@media (max-width: 900px) {
  .sidebar { position: absolute; left: 0; top: 0; bottom: 0; transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .notes { position: absolute; right: 0; top: 0; bottom: 0; transform: translateX(100%); }
  .notes.open { transform: translateX(0); }
}

@media (min-width: 901px) {
  .mob-overlay { display: none !important; }
}

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.65); display: flex; align-items: center; justify-content: center; z-index: 200; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 1.25rem; width: 560px; max-width: 94vw; max-height: 90vh; overflow-y: auto; }
.modal-title { font-weight: 700; font-size: 1.05rem; margin-bottom: 1rem; }
.modal textarea { width: 100%; background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: .7rem; min-height: 180px; resize: vertical; margin-bottom: .75rem; }
.modal-btns { display: flex; gap: .5rem; justify-content: flex-end; }
.btn-primary { background: var(--accent); color: #fff; border-radius: 8px; padding: .5rem 1rem; font-weight: 600; }
.btn-secondary { background: transparent; border: 1px solid var(--border); border-radius: 8px; padding: .5rem 1rem; color: var(--muted); }

/* Permission dialog */
.permission-dialog { background: var(--surface3); border: 1px solid var(--accent); border-radius: 12px; padding: .9rem; margin: .5rem 0; }
.permission-title { font-weight: 600; margin-bottom: .4rem; }
.permission-body { font-size: .85rem; color: var(--muted); margin-bottom: .6rem; white-space: pre-wrap; }
.permission-btns { display: flex; gap: .4rem; flex-wrap: wrap; }
.permission-btns button { border: 1px solid var(--border); border-radius: 8px; padding: .4rem .7rem; font-size: .8rem; }
.permission-btns .allow { background: var(--accent); color: #fff; border-color: var(--accent); }
.permission-btns .reject { color: var(--red); }

/* Toast */
.toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: var(--accent); color: #fff; padding: .5rem 1rem; border-radius: 8px; z-index: 300; opacity: 0; transition: opacity .3s; pointer-events: none; }
.toast.show { opacity: 1; }
