/* ── CSS Variables (light mode) ─────────────────────────────────────────────── */
:root {
  --bg:           #f7f7f8;
  --surface:      #ffffff;
  --border:       #e5e5e5;
  --text:         #1a1a1a;
  --text-muted:   #8e8e93;
  --text-body:    #3a3a3a;
  --input-bg:     #fafafa;
  --input-focus:  #ffffff;
  --hover-bg:     #f0f0f5;
  --badge-bg:     #eef2ff;
  --badge-color:  #4f46e5;
  --accent:       #4f46e5;
  --accent-hover: #4338ca;
  --disabled:     #c7c7cc;
  --highlight:    #fef08a;
  --collapse-btn: #8e8e93;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:           #1c1c1e;
    --surface:      #2c2c2e;
    --border:       #3a3a3c;
    --text:         #f2f2f7;
    --text-muted:   #8e8e93;
    --text-body:    #d1d1d6;
    --input-bg:     #3a3a3c;
    --input-focus:  #48484a;
    --hover-bg:     #3a3a3c;
    --badge-bg:     #2d2b52;
    --badge-color:  #a5b4fc;
    --accent:       #6366f1;
    --accent-hover: #818cf8;
    --disabled:     #48484a;
    --highlight:    #713f12;
    --collapse-btn: #636366;
  }
}

/* Missive explicitly sets dark — override OS preference */
[data-theme="dark"] {
  --bg:           #1c1c1e;
  --surface:      #2c2c2e;
  --border:       #3a3a3c;
  --text:         #f2f2f7;
  --text-muted:   #8e8e93;
  --text-body:    #d1d1d6;
  --input-bg:     #3a3a3c;
  --input-focus:  #48484a;
  --hover-bg:     #3a3a3c;
  --badge-bg:     #2d2b52;
  --badge-color:  #a5b4fc;
  --accent:       #6366f1;
  --accent-hover: #818cf8;
  --disabled:     #48484a;
  --highlight:    #713f12;
  --collapse-btn: #636366;
}

[data-theme="light"] {
  --bg:           #f7f7f8;
  --surface:      #ffffff;
  --border:       #e5e5e5;
  --text:         #1a1a1a;
  --text-muted:   #8e8e93;
  --text-body:    #3a3a3a;
  --input-bg:     #fafafa;
  --input-focus:  #ffffff;
  --hover-bg:     #f0f0f5;
  --badge-bg:     #eef2ff;
  --badge-color:  #4f46e5;
  --accent:       #4f46e5;
  --accent-hover: #4338ca;
  --disabled:     #c7c7cc;
  --highlight:    #fef08a;
  --collapse-btn: #8e8e93;
}

/* ── Base ───────────────────────────────────────────────────────────────────── */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ── Header ─────────────────────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#domain-info {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
}

#domain-label {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
}

.domain-badge {
  background: var(--badge-bg);
  color: var(--badge-color);
  border-radius: 4px;
  padding: 2px 7px;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
}

/* ── Domain override ────────────────────────────────────────────────────────── */
.btn-override {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 11px;
  padding: 1px 3px;
  color: var(--text-muted);
  opacity: 0.6;
  border-radius: 3px;
  flex-shrink: 0;
}

.btn-override:hover { opacity: 1; background: var(--hover-bg); }

#domain-override-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#domain-override-input {
  flex: 1;
  padding: 5px 8px;
  font-size: 12px;
}

.btn-icon-sm {
  background: none;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
  flex-shrink: 0;
  line-height: 1;
}

.btn-icon-sm:hover { background: var(--hover-bg); color: var(--text); }

/* ── Buttons ────────────────────────────────────────────────────────────────── */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-primary:disabled { background: var(--disabled); cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text-muted);
}

.btn-ghost:hover { background: var(--hover-bg); }
.btn-ghost.small { padding: 3px 8px; font-size: 11px; }

.full-width { width: 100%; margin-top: 4px; }

/* ── Agent setup ────────────────────────────────────────────────────────────── */
#agent-setup {
  padding: 14px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

#agent-setup p {
  margin-bottom: 8px;
  color: var(--text-muted);
}

.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; }

/* ── Note form ──────────────────────────────────────────────────────────────── */
#note-form {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2px;
}

.form-header span { font-weight: 600; font-size: 13px; }

input[type="text"],
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 13px;
  font-family: inherit;
  background: var(--input-bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}

input[type="text"]:focus,
textarea:focus {
  border-color: var(--accent);
  background: var(--input-focus);
}

textarea { resize: vertical; min-height: 80px; }

/* ── Search bar ─────────────────────────────────────────────────────────────── */
#search-bar {
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#search-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  background: var(--input-bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

#search-input:focus {
  border-color: var(--accent);
  background: var(--input-focus);
}

.search-highlight {
  background: var(--highlight);
  border-radius: 2px;
  padding: 0 1px;
  color: var(--text);
}

.no-results {
  text-align: center;
  color: var(--text-muted);
  padding: 30px 20px;
  font-size: 12px;
}

/* ── Notes container ────────────────────────────────────────────────────────── */
#notes-container {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px;
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 40px 20px;
  line-height: 1.6;
}

.hint { margin-top: 6px; font-size: 12px; }

/* ── Note card ──────────────────────────────────────────────────────────────── */
.note-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 13px;
  margin-bottom: 8px;
  position: relative;
}

.note-card:hover .note-actions { opacity: 1; }

.note-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.note-title-row {
  display: flex;
  align-items: flex-start;
  gap: 5px;
  flex: 1;
  min-width: 0;
}

.collapse-icon {
  color: var(--collapse-btn);
  font-size: 9px;
  margin-top: 3px;
  flex-shrink: 0;
  transition: transform 0.2s;
  display: inline-block;
}

.note-card.expanded .collapse-icon { transform: rotate(90deg); }

.note-title {
  font-weight: 600;
  font-size: 13px;
  line-height: 1.4;
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

.note-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
  flex-shrink: 0;
}

.note-actions button {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 13px;
  padding: 2px 4px;
  border-radius: 4px;
  color: var(--text-muted);
}

.note-actions button:hover { background: var(--hover-bg); color: var(--text); }
.note-actions .delete-btn:hover { color: #d93025; }

.note-meta {
  display: flex;
  gap: 10px;
  margin-top: 5px;
  color: var(--text-body);
  font-size: 11px;
  flex-wrap: wrap;
}

.note-meta span { display: flex; align-items: center; gap: 3px; }
.note-meta strong { font-weight: 600; color: var(--text); }

/* Collapsible body */
.note-body-wrap {
  overflow: hidden;
  max-height: 0;
  transition: max-height 0.25s ease, opacity 0.2s ease;
  opacity: 0;
}

.note-card.expanded .note-body-wrap {
  max-height: 600px;
  opacity: 1;
}

.note-body {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  color: var(--text-body);
  line-height: 1.5;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* When searching, auto-expand matched notes */
.note-card.search-match .note-body-wrap {
  max-height: 600px;
  opacity: 1;
}

/* ── File attachments ───────────────────────────────────────────────────────── */
#file-drop-zone {
  border: 1.5px dashed var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  text-align: center;
  cursor: pointer;
  font-size: 12px;
  color: var(--text-muted);
  position: relative;
  transition: border-color 0.15s, background 0.15s;
}

#file-drop-zone:hover,
#file-drop-zone.drag-over {
  border-color: var(--accent);
  background: var(--badge-bg);
  color: var(--accent);
}

#file-drop-zone input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
  border: none;
  padding: 0;
  background: none;
}

#file-preview-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.file-preview-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--hover-bg);
  border-radius: 5px;
  padding: 5px 8px;
  font-size: 11px;
}

.file-preview-thumb {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
  border: 1px solid var(--border);
}

.file-preview-icon {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

.file-preview-info {
  flex: 1;
  min-width: 0;
}

.file-preview-name {
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-preview-size { color: var(--text-muted); font-size: 10px; }

.file-preview-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 14px;
  padding: 0 2px;
  line-height: 1;
  flex-shrink: 0;
}

.file-preview-remove:hover { color: #d93025; }

/* Attachments inside expanded note */
.note-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.attachment-thumb-wrap {
  position: relative;
  display: inline-flex;
}

.attachment-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 5px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: opacity 0.15s;
}

.attachment-thumb:hover { opacity: 0.85; }

.attachment-file {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--hover-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 8px;
  font-size: 11px;
  color: var(--text);
  text-decoration: none;
  max-width: 140px;
}

.attachment-file:hover { border-color: var(--accent); color: var(--accent); }

.attachment-file-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.attachment-delete {
  position: absolute;
  top: -5px;
  right: -5px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #d93025;
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 10px;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.attachment-thumb-wrap:hover .attachment-delete,
.attachment-file-wrap:hover .attachment-delete { display: flex; }

.attachment-file-wrap {
  position: relative;
  display: inline-flex;
}

/* Lightbox */
#lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  cursor: zoom-out;
}

#lightbox img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 6px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}

/* ── Footer ─────────────────────────────────────────────────────────────────── */
footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 11px;
}

footer strong { color: var(--text); }

/* ── Utility ────────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
