:root {
  --bg: #090e1f;
  --sidebar-bg: #0c1228;
  --card-bg: #111827;
  --surface: #161f38;
  --surface2: #1d2844;
  --border: #263052;
  --border-subtle: #1a2238;
  --primary: #7060f0;
  --primary-hover: #8070f5;
  --primary-glow: rgba(112,96,240,0.25);
  --teal: #00c9b1;
  --text: #e2e8f8;
  --text-muted: #7a85a0;
  --text-dim: #3a4560;
  --danger: #ff5c6b;
  --success: #4caf7d;
  --warning: #f0a500;
  --radius: 12px;
  --shadow: 0 8px 32px rgba(0,0,0,0.5);
}

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

/* App is a fixed-height shell — the outer page never scrolls.
   All scrolling happens inside individual panels. */
html, body { overflow: hidden; height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

a { color: var(--primary); text-decoration: none; }

/* ── Login page ──────────────────────────────────────────────────────── */
.page-center {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
  background: radial-gradient(ellipse at 50% 0%, #1a2050 0%, var(--bg) 70%);
}

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px; width: 100%; max-width: 400px;
  box-shadow: var(--shadow);
}

.card h1 { font-size: 22px; margin-bottom: 24px; font-weight: 700; }

/* ── Forms ───────────────────────────────────────────────────────────── */
.field { margin-bottom: 16px; }
.field label {
  display: block; margin-bottom: 6px;
  color: var(--text-muted); font-size: 12px;
  text-transform: uppercase; letter-spacing: 0.05em;
}

input, select, textarea {
  width: 100%; background: var(--surface2);
  border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); padding: 10px 14px; font-size: 14px;
  outline: none; transition: border-color 0.2s; font-family: inherit;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); }
textarea { resize: vertical; min-height: 100px; }

/* ── Buttons ─────────────────────────────────────────────────────────── */
button, .btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; padding: 10px 20px; border-radius: 8px; border: none;
  cursor: pointer; font-size: 14px; font-weight: 500;
  transition: all 0.2s; font-family: inherit; color: var(--text);
}

.btn-primary { background: var(--primary); color: #fff; width: 100%; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface2); border: 1px solid var(--border); color: var(--text); }
.btn-secondary:hover { border-color: var(--primary); }
.btn-danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
.btn-sm { padding: 5px 12px; font-size: 12px; border-radius: 6px; }
.btn-icon { background: transparent; border: 1px solid var(--border); color: var(--text-muted); padding: 6px 10px; border-radius: 8px; }
.btn-icon:hover { border-color: var(--primary); color: var(--text); }

/* ── App layout ──────────────────────────────────────────────────────── */
.app-layout { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* ── Icon rail ───────────────────────────────────────────────────────── */
.icon-rail {
  width: 56px; background: var(--sidebar-bg);
  border-right: 1px solid var(--border-subtle);
  display: flex; flex-direction: column; align-items: center;
  padding: 12px 0; flex-shrink: 0; gap: 0;
}

.rail-top { display: flex; flex-direction: column; align-items: center; gap: 4px; flex: 1; }
.rail-bottom { display: flex; flex-direction: column; align-items: center; gap: 4px; padding-bottom: 8px; }

.rail-btn {
  position: relative; width: 40px; height: 40px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: 10px;
  color: var(--text-muted); cursor: pointer;
  transition: background 0.15s, color 0.15s;
  padding: 0; min-width: auto;
}
.rail-btn:hover { background: var(--surface); color: var(--text); }
.rail-btn.active { background: rgba(112,96,240,0.18); color: var(--primary); }
.rail-btn.rail-logout:hover { background: rgba(255,92,107,0.1); color: var(--danger); }
.rail-hamburger { margin-bottom: 2px; }

.rail-divider {
  width: 28px; height: 1px;
  background: var(--border-subtle); margin: 4px 0 8px; flex-shrink: 0;
}

/* Tooltip on hover */
.rail-btn::after {
  content: attr(title); position: absolute;
  left: calc(100% + 10px); top: 50%; transform: translateY(-50%);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 10px;
  font-size: 12px; color: var(--text); white-space: nowrap;
  pointer-events: none; opacity: 0; transition: opacity 0.15s; z-index: 200;
}
.rail-btn:hover::after { opacity: 1; }

/* ── Sidebar ─────────────────────────────────────────────────────────── */
.sidebar {
  width: 270px; background: var(--sidebar-bg);
  display: flex; flex-direction: column; flex-shrink: 0;
  overflow: hidden; border-right: 1px solid var(--border-subtle);
  transition: width 0.2s ease;
}
.sidebar.collapsed { width: 0; border-right: none; }

.sidebar-top-label {
  padding: 18px 16px 10px;
  font-size: 11px; font-weight: 700;
  color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.08em;
  flex-shrink: 0;
}

/* ── New item button ─────────────────────────────────────────────────── */
.new-item-btn {
  margin: 0 12px 14px; padding: 10px; background: transparent;
  border: 1px solid var(--primary); border-radius: 10px;
  color: var(--primary); cursor: pointer; text-align: center;
  font-size: 13px; font-weight: 500; transition: all 0.2s; user-select: none;
}
.new-item-btn:hover { background: var(--primary-glow); color: #fff; }

/* ── Sidebar sections ────────────────────────────────────────────────── */
.sidebar-section { display: flex; flex-direction: column; flex: 1; min-height: 0; }

.sidebar-section-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 16px 8px; cursor: pointer; user-select: none;
  font-size: 11px; font-weight: 600; letter-spacing: 0.05em;
  text-transform: uppercase; color: var(--text-muted);
}
.sidebar-section-header:hover { color: var(--text); }
.sidebar-section-arrow { font-size: 10px; }

.sidebar-section-body { flex: 1; overflow-y: auto; padding: 0 8px 8px; }
.sidebar-list { overflow-y: auto; }

/* ── Sidebar items ───────────────────────────────────────────────────── */
.sidebar-item {
  padding: 9px 10px; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; gap: 8px; transition: background 0.15s;
}
.sidebar-item:hover { background: var(--surface2); }
.sidebar-item.active { background: var(--surface2); }

.sidebar-item-icon {
  width: 20px; text-align: center; color: var(--text-muted);
  font-size: 13px; flex-shrink: 0;
}

.sidebar-item-title {
  font-size: 13px; font-weight: 400;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1;
}
.sidebar-item-meta { font-size: 11px; color: var(--text-muted); white-space: nowrap; }

.chat-delete-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 11px; padding: 2px 4px; border-radius: 4px; opacity: 0; flex-shrink: 0;
}
.sidebar-item:hover .chat-delete-btn { opacity: 1; }
.chat-delete-btn:hover { color: var(--danger); }

/* ── Scrollbar ───────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Main content ────────────────────────────────────────────────────── */
.main-content {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; background: var(--bg); padding: 16px; min-width: 0;
}

.view-panel { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-height: 0; }

/* ── Main card ───────────────────────────────────────────────────────── */
.main-card {
  flex: 1; background: var(--card-bg);
  border: 1px solid var(--border); border-radius: 16px;
  display: flex; flex-direction: column; overflow: hidden; min-height: 0;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
}

.main-card-header {
  padding: 16px 20px; border-bottom: 1px solid var(--border-subtle);
  display: flex; align-items: center; justify-content: space-between; flex-shrink: 0;
}
.main-card-title { font-size: 17px; font-weight: 600; }

.dots-menu-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 20px; padding: 2px 8px; border-radius: 6px; letter-spacing: 2px; line-height: 1;
}
.dots-menu-btn:hover { background: var(--surface2); color: var(--text); }

/* ── Dots dropdown ───────────────────────────────────────────────────── */
.dots-dropdown {
  position: absolute; right: 0; top: 100%; margin-top: 4px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 4px; min-width: 150px;
  box-shadow: var(--shadow); z-index: 100;
}
.dots-dropdown-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-radius: 6px; cursor: pointer;
  font-size: 13px; color: var(--text); transition: background 0.15s;
}
.dots-dropdown-item:hover { background: var(--surface2); }
.dots-dropdown-item.danger { color: var(--danger); }

/* ── Note card header title input ───────────────────────────────────── */
.note-card-title-input {
  font-size: 17px; font-weight: 600;
  background: transparent; border: none; outline: none;
  color: var(--text); padding: 0; width: 100%;
}
.note-card-title-input:focus { outline: none; border: none; }

/* ── Note tags row (between card header and format bar) ─────────────── */
.note-tags-row {
  padding: 8px 20px; border-bottom: 1px solid var(--border-subtle);
  display: flex; align-items: center; gap: 8px; flex-shrink: 0; flex-wrap: wrap;
}

/* ── Format toolbar ──────────────────────────────────────────────────── */
.note-format-bar {
  display: flex; align-items: center; gap: 2px;
  padding: 8px 16px; border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0; flex-wrap: wrap;
}

.fmt-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 28px; height: 26px; padding: 0 5px;
  background: none; border: none; border-radius: 5px;
  color: var(--text-muted); font-size: 13px; cursor: pointer;
  transition: all 0.15s; font-family: inherit;
}
.fmt-btn:hover { background: var(--surface2); color: var(--text); }
.fmt-btn.fmt-active { background: var(--surface2); color: var(--text); box-shadow: inset 0 0 0 1px var(--primary); }

.fmt-divider { width: 1px; height: 16px; background: var(--border); margin: 0 4px; flex-shrink: 0; }

/* ── Note layout ─────────────────────────────────────────────────────── */
.note-layout { display: flex; flex-direction: column; height: 100%; overflow: hidden; }

.note-header { padding: 14px 20px 12px; border-bottom: 1px solid var(--border-subtle); flex-shrink: 0; }

.note-title-input {
  font-size: 20px; font-weight: 600;
  background: transparent; border: none; border-radius: 0;
  padding: 0; width: 100%; color: var(--text);
}
.note-title-input:focus { outline: none; }

.note-meta-row { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px; }

.note-body-fill {
  flex: 1; min-height: 0; overflow-y: auto; resize: none; border: none; border-radius: 0;
  background: transparent; padding: 18px 20px; font-size: 15px;
  line-height: 1.7; color: var(--text);
}
.note-body-fill:focus { outline: none; }
.note-body-fill::placeholder { color: var(--text-dim); }
.note-body-fill[contenteditable]:empty::before {
  content: attr(data-placeholder); color: var(--text-dim); pointer-events: none;
}
.note-body-fill ol, .note-body-fill ul { padding-left: 24px; margin: 6px 0; }
.note-body-fill li { margin-bottom: 4px; }
/* Force dark text on yellow highlights so they're readable on the dark background */
.note-body-fill [style*="background-color:rgb(255, 255, 0)"],
.note-body-fill [style*="background-color: rgb(255, 255, 0)"],
.note-body-fill [style*="background-color:rgb(255,255,0)"],
.note-body-fill mark { color: #111827 !important; }



.note-toolbar {
  padding: 10px 14px; border-top: 1px solid var(--border-subtle);
  display: flex; align-items: center; gap: 8px; flex-shrink: 0;
}

.note-toolbar-btn {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text-muted); cursor: pointer;
  font-size: 16px; width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: all 0.2s;
}
.note-toolbar-btn:hover { border-color: var(--primary); color: var(--text); }

/* ── Send / Save pill button ─────────────────────────────────────────── */
.send-pill-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 18px; background: var(--primary);
  color: #fff; border: none; border-radius: 20px;
  font-size: 13px; font-weight: 600; cursor: pointer;
  transition: all 0.2s; white-space: nowrap; flex-shrink: 0;
}
.send-pill-btn:hover { background: var(--primary-hover); transform: translateY(-1px); }
.send-pill-btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

/* ── Tags ────────────────────────────────────────────────────────────── */
.tag-input-area {
  display: flex; flex-wrap: wrap; gap: 6px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; min-height: 36px; flex: 1;
}

.tag {
  background: var(--primary); color: #fff; padding: 2px 10px;
  border-radius: 20px; font-size: 11px;
  display: flex; align-items: center; gap: 4px;
}

.tag-remove {
  cursor: pointer; opacity: 0.7; font-size: 13px; line-height: 1;
  background: none; border: none; color: #fff; padding: 0;
  min-width: auto; height: auto;
}
.tag-remove:hover { opacity: 1; }
.tag-ai { background: rgba(112,96,240,0.6); }

/* ── AI badge ────────────────────────────────────────────────────────── */
.ai-badge {
  font-size: 10px; color: var(--primary);
  background: rgba(112,96,240,0.15); border-radius: 4px; padding: 1px 5px;
}

/* ── Files section ───────────────────────────────────────────────────── */
.files-section {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 14px; margin: 0 20px;
}

.file-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 0; border-bottom: 1px solid var(--border-subtle);
}
.file-item:last-child { border-bottom: none; }

.file-status { font-size: 11px; padding: 2px 8px; border-radius: 20px; }
.file-status.processed { background: rgba(76,175,125,0.15); color: var(--success); }
.file-status.rejected  { background: rgba(255,92,107,0.15); color: var(--danger); }
.file-status.pending   { background: rgba(240,165,0,0.15);  color: var(--warning); }

/* ── Chat messages ───────────────────────────────────────────────────── */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 20px;
  display: flex; flex-direction: column; gap: 14px; min-height: 0;
}

.message { display: flex; gap: 10px; max-width: 80%; }
.message.user { align-self: flex-end; flex-direction: row-reverse; }

.message-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; flex-shrink: 0; background: var(--surface2);
}
.message.user .message-avatar { background: var(--primary); }

.message-bubble {
  background: var(--surface); border: 1px solid var(--border-subtle);
  border-radius: 12px; padding: 10px 14px; line-height: 1.5; font-size: 14px;
}
.message.user .message-bubble { background: var(--primary); border-color: var(--primary); color: #fff; }

/* Markdown inside assistant bubbles */
.message-bubble p { margin-bottom: 6px; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble ol, .message-bubble ul { padding-left: 20px; margin: 6px 0; }
.message-bubble li { margin-bottom: 3px; }
.message-bubble strong { font-weight: 600; }
.message-bubble em { font-style: italic; }

.message-citations { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.message-ts {
  font-size: 10px; color: var(--text-muted); align-self: flex-end;
  white-space: nowrap; padding-bottom: 2px; flex-shrink: 0;
}
.message.user .message-ts { order: -1; text-align: right; }
.share-tooltip {
  position: fixed; z-index: 9999; pointer-events: none;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 11px; font-size: 12px; color: var(--text);
  box-shadow: 0 4px 16px rgba(0,0,0,0.35); max-width: 240px; line-height: 1.5;
  opacity: 0; transition: opacity 0.1s;
}
.share-tooltip.visible { opacity: 1; }
.shared-inbox-card {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; margin-bottom: 10px; cursor: pointer; transition: border-color 0.15s;
}
.shared-inbox-card:hover { border-color: var(--primary); }
.shared-inbox-tag {
  display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text-muted);
  margin-right: 4px;
}
.citation {
  font-size: 11px; padding: 2px 10px;
  background: rgba(112,96,240,0.2); color: var(--primary);
  border-radius: 20px; cursor: pointer;
}
.citation:hover { background: rgba(112,96,240,0.35); }

/* ── Chat input pill ─────────────────────────────────────────────────── */
.chat-input-pill {
  margin: 0 16px 16px; padding: 8px 8px 8px 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 28px; display: flex; align-items: center;
  gap: 8px; flex-shrink: 0; transition: border-color 0.2s;
}
.chat-input-pill:focus-within { border-color: var(--primary); }

.chat-input-pill textarea {
  flex: 1; border: none; background: transparent; padding: 2px 0;
  font-size: 14px; min-height: 24px; max-height: 100px;
  resize: none; color: var(--text);
}
.chat-input-pill textarea:focus { outline: none; border: none; }
.chat-input-pill textarea::placeholder { color: var(--text-muted); }

.icon-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  font-size: 17px; padding: 4px; border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; min-width: auto; height: auto;
}
.icon-btn:hover { color: var(--text); }

/* ── Alerts ──────────────────────────────────────────────────────────── */
.alert { padding: 10px 14px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
.alert-error   { background: rgba(255,92,107,0.1); border: 1px solid var(--danger); color: var(--danger); }
.alert-success { background: rgba(76,175,125,0.1); border: 1px solid var(--success); color: var(--success); }
.alert-warn    { background: rgba(240,165,0,0.1);  border: 1px solid var(--warning); color: var(--warning); }

/* ── Empty state ─────────────────────────────────────────────────────── */
.empty-state {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; height: 100%; gap: 12px;
  color: var(--text-muted); text-align: center;
}
.empty-state .icon { font-size: 48px; }
.empty-chat-icon { font-size: 72px; }

/* ── Admin table ─────────────────────────────────────────────────────── */
.main-body { flex: 1; overflow-y: auto; padding: 24px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); font-size: 13px; }
th { color: var(--text-muted); font-weight: 500; }
tr:hover td { background: var(--surface2); }

/* ── Loading dots ────────────────────────────────────────────────────── */
.dots::after { content: ''; animation: dots 1.2s steps(4,end) infinite; }
@keyframes dots {
  0%,20% { content: ''; } 40% { content: '.'; } 60% { content: '..'; } 80%,100% { content: '...'; }
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* ── Custom modal (replaces alert/confirm/prompt) ────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.65);
  display: flex; align-items: center; justify-content: center;
  z-index: 9000; animation: fadeIn 0.15s ease;
}
@keyframes fadeIn { from { opacity:0 } to { opacity:1 } }

.modal-box {
  background: var(--card-bg); border: 1px solid var(--border);
  border-radius: 14px; padding: 24px 28px; width: 100%; max-width: 400px;
  box-shadow: var(--shadow); animation: slideUp 0.15s ease;
}
.modal-title { font-size: 16px; font-weight: 600; margin-bottom: 14px; }
.modal-msg { font-size: 14px; line-height: 1.5; margin-bottom: 20px; color: var(--text); }
.modal-content {
  font-size: 13px; max-height: 260px; overflow-y: auto;
  margin-bottom: 18px; color: var(--text-muted); line-height: 1.6;
}
.modal-input { margin-bottom: 18px; background: var(--surface2); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ── Settings view ───────────────────────────────────────────────────── */
.settings-body {
  flex: 1; overflow-y: auto; padding: 28px 32px;
  display: flex; flex-direction: column; gap: 20px;
}
.account-columns {
  display: grid; grid-template-columns: 1fr 1fr; gap: 16px;
}
.account-section-header {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.08em; color: var(--text-muted);
  padding-bottom: 6px; border-bottom: 1px solid var(--border-subtle);
  margin-top: 4px;
}
.settings-section-title {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.07em; color: var(--text-muted); padding-bottom: 4px;
  border-bottom: 1px solid var(--border-subtle); margin-bottom: 4px;
}
.settings-msg { font-size: 12px; min-height: 16px; flex: 1; }

/* Account identity header */
.account-identity {
  display: flex; align-items: center; gap: 16px;
  padding: 20px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.account-avatar {
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700; flex-shrink: 0; letter-spacing: 0.5px;
}
.account-name { font-size: 16px; font-weight: 600; margin-bottom: 3px; }
.account-email { font-size: 13px; color: var(--text-muted); }

/* Settings cards */
.settings-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
  display: flex; flex-direction: column; gap: 12px;
}
.settings-card-title {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.07em; color: var(--text-muted);
  padding-bottom: 10px; border-bottom: 1px solid var(--border-subtle);
}
.settings-card .field { margin-bottom: 0; }
.settings-card-footer {
  display: flex; align-items: center; justify-content: flex-end;
  gap: 12px; margin-top: 4px;
}

/* Usage card header */
.usage-card-header {
  display: flex; align-items: baseline; justify-content: space-between;
}
.usage-renew-label {
  font-size: 12px; color: var(--text-muted);
}
.usage-numbers {
  display: flex; flex-direction: column; justify-content: center;
}

/* Budget bar */
.budget-bar-wrap { display: flex; flex-direction: column; gap: 6px; }
.budget-bar-track {
  height: 6px; background: var(--surface2); border-radius: 3px; overflow: hidden;
}
.budget-bar-fill {
  height: 100%; border-radius: 3px; background: var(--primary);
  transition: width 0.4s ease;
}
.budget-bar-labels {
  display: flex; justify-content: space-between;
  font-size: 12px; color: var(--text-muted);
}

/* ── Admin view ──────────────────────────────────────────────────────── */
.admin-body {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 20px;
  padding: 20px 24px;
}
.admin-stats { display: flex; gap: 12px; flex-shrink: 0; }
.admin-stat-card {
  flex: 1;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px;
}
.admin-stat-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 6px; }
.admin-stat-value { font-size: 22px; font-weight: 700; }
.admin-users-panel { display: flex; flex-direction: column; gap: 8px; }
.admin-table-wrap { overflow-x: auto; }
.admin-agent-panel { display: flex; flex-direction: column; gap: 12px; flex-shrink: 0; }

/* ── Toggle switch ───────────────────────────────────────────────────── */
.toggle { position: relative; display: inline-block; width: 36px; height: 20px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--surface2); border-radius: 20px; transition: 0.2s;
}
.toggle-slider::before {
  position: absolute; content: ''; height: 14px; width: 14px;
  left: 3px; bottom: 3px; background: var(--text-muted);
  border-radius: 50%; transition: 0.2s;
}
.toggle input:checked + .toggle-slider { background: rgba(112,96,240,0.35); }
.toggle input:checked + .toggle-slider::before { transform: translateX(16px); background: var(--primary); }

/* ── Tags view ───────────────────────────────────────────────────────── */
#tags-view .main-card { height: 100%; padding: 0; position: relative; }

/* Tag dialog */
.tag-dialog {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
}
.tag-dialog-box {
  background: var(--card-bg); border: 1px solid var(--border);
  border-radius: var(--radius); width: 480px; max-width: 92vw;
  max-height: 70vh; display: flex; flex-direction: column;
  box-shadow: 0 24px 64px rgba(0,0,0,0.5);
}
.tag-dialog-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 16px;
}
.tag-dialog-close {
  background: none; border: none; color: var(--text-muted);
  font-size: 16px; cursor: pointer; padding: 4px 8px; border-radius: 4px;
}
.tag-dialog-close:hover { background: var(--surface2); color: var(--text); }
.tag-dialog-list { overflow-y: auto; padding: 8px 0; }
.tag-dialog-note {
  padding: 12px 20px; cursor: pointer; border-bottom: 1px solid var(--border-subtle);
  transition: background 0.15s;
}
.tag-dialog-note:hover { background: var(--surface2); }
.tag-dialog-note:last-child { border-bottom: none; }

/* ── Stock Monitor ───────────────────────────────────────────────────── */
#stocks-report-body h3 {
  font-size: 15px; font-weight: 700; color: var(--text);
  margin: 20px 0 8px; padding-bottom: 6px;
  border-bottom: 1px solid var(--border-subtle);
}
#stocks-report-body h3:first-child { margin-top: 0; }
#stocks-report-body p { margin: 0 0 10px; color: var(--text); line-height: 1.75; }
#stocks-report-body ul, #stocks-report-body ol { padding-left: 22px; margin: 6px 0 12px; }
#stocks-report-body li { margin-bottom: 5px; color: var(--text); line-height: 1.6; }
#stocks-report-body b { color: var(--text); font-weight: 600; }
#stocks-report-body .stock-price-line {
  display: inline-flex; align-items: center; gap: 10px;
  font-size: 22px; font-weight: 700; color: var(--text); margin-bottom: 4px;
}
#stocks-report-body .stock-change-pos { color: var(--success); font-size: 14px; font-weight: 600; }
#stocks-report-body .stock-change-neg { color: var(--danger); font-size: 14px; font-weight: 600; }

/* ── Utilities ───────────────────────────────────────────────────────── */
.flex { display: flex; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.ml-auto { margin-left: auto; }
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 12px; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.hidden { display: none !important; }

/* ═══════════════════════════════════════════════════════════════════════
   MOBILE — max-width 767px
   All overrides are additive; desktop layout is unchanged.
   ═══════════════════════════════════════════════════════════════════════ */

/* ── Mobile tab bar (hidden on desktop) ───────────────────────────────── */
.mobile-tab-bar {
  display: none;
}
.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 499;
}
.sidebar-backdrop.visible { display: block; }

@media (max-width: 767px) {

  /* ── Root layout ──────────────────────────────────────────────────── */
  body { font-size: 15px; }

  /* Prevent iOS zoom on input focus (font-size < 16px triggers zoom) */
  input, select, textarea { font-size: 16px !important; }

  /* App layout lives entirely ABOVE the tab bar.
     Only subtract tab bar content height (49px). The body::after handles
     filling the safe area below, so no need to subtract it here. */
  .app-layout {
    height: calc(100dvh - 49px);
  }

  /* Fill the bottom safe area with the tab-bar color at the body level.
     This is a belt-and-suspenders approach — works even if env() doesn't
     propagate correctly into the tab bar's ::after pseudo-element. */
  body::after {
    content: '';
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: env(safe-area-inset-bottom);
    background: var(--bg);
    z-index: 599;
  }

  /* ── Hide desktop icon rail ───────────────────────────────────────── */
  .icon-rail { display: none; }

  /* ── Sidebar becomes a slide-in drawer ───────────────────────────── */
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 280px;
    z-index: 500;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    /* override width-collapse from .collapsed — use transform only */
    border-right: 1px solid var(--border-subtle) !important;
    padding-bottom: calc(49px + env(safe-area-inset-bottom));
  }
  .sidebar.collapsed { width: 280px; }
  .sidebar.mobile-open { transform: translateX(0); }

  /* Taller sidebar items for touch */
  .sidebar-item { padding: 13px 10px; }
  .new-item-btn { padding: 14px 10px; font-size: 14px; }
  .sidebar-top-label { padding: 20px 16px 12px; }

  /* Always-visible delete button (no hover on mobile) */
  .chat-delete-btn { opacity: 1; padding: 6px 8px; }

  /* ── Main content: edge-to-edge on mobile */
  .main-content {
    width: 100%;
    padding: 0;
  }

  /* No rounded card on mobile — full bleed, same background as page */
  .main-card { border-radius: 0; border: none; box-shadow: none; background: var(--bg); }

  .main-card-header { padding: 12px 14px; }
  .main-card-title { font-size: 16px; }
  .dots-menu-btn { padding: 6px 10px; min-height: 44px; min-width: 44px; }

  /* ── Mobile bottom tab bar ────────────────────────────────────────── */
  /* Outer nav: same background as the page so the bottom area is one
     continuous color from content → tab icons → safe area. */
  .mobile-tab-bar {
    display: block;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    background: var(--bg);
    border-top: 1px solid var(--border-subtle);
    z-index: 600;
  }

  /* Inner row: 49px (Apple HIG standard tab bar height) */
  .mobile-tab-inner {
    display: flex;
    height: 49px;
    justify-content: space-around;
    align-items: center;
  }

  .mobile-tab-btn {
    flex: 1;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 3px;
    background: none; border: none;
    color: var(--text-muted);
    font-size: 10px;
    min-width: 44px;
    cursor: pointer;
    padding: 0;
    transition: color 0.15s;
  }
  .mobile-tab-btn.active { color: var(--primary); }
  .mobile-tab-btn svg { width: 26px; height: 26px; }

  /* ── Notes: format bar scrolls horizontally ───────────────────────── */
  .note-format-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 6px 10px;
    scrollbar-width: none;
    gap: 3px;
  }
  .note-format-bar::-webkit-scrollbar { display: none; }

  .fmt-btn {
    min-width: 38px;
    height: 38px;
    flex-shrink: 0;
    font-size: 15px;
  }
  .fmt-divider { flex-shrink: 0; height: 20px; }

  /* Tags row: scroll horizontally */
  .note-tags-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 8px 12px;
    scrollbar-width: none;
  }
  .note-tags-row::-webkit-scrollbar { display: none; }

  /* Note body */
  .note-body-fill { padding: 14px 14px; font-size: 15px; }

  /* Note toolbar: natural flex child at bottom of card — no sticky needed */
  .note-toolbar {
    position: static;
    background: var(--bg);
    padding: 10px 12px;
    flex-shrink: 0;
  }
  .note-toolbar-btn { width: 44px; height: 44px; }

  /* ── Chat: keyboard-aware layout ─────────────────────────────────── */
  .chat-messages { padding: 12px; gap: 10px; }
  .message { max-width: 94%; }

  .chat-input-pill {
    margin: 0;
    border-radius: 0;
    border-left: none; border-right: none; border-bottom: none;
    padding: 8px 8px 8px 12px;
    flex-shrink: 0;
  }

  /* Send button meets 44px touch target */
  .send-pill-btn {
    min-height: 44px;
    padding: 10px 16px;
    border-radius: 14px;
    font-size: 14px;
  }

  /* Attach + mic buttons */
  .icon-btn {
    min-width: 44px;
    min-height: 44px;
    padding: 10px;
    font-size: 20px;
  }

  /* ── Buttons ──────────────────────────────────────────────────────── */
  .btn-primary { min-height: 48px; font-size: 15px; }
  .btn-sm { padding: 10px 16px; font-size: 13px; min-height: 40px; }
  .btn-secondary { min-height: 44px; }

  /* ── Settings ─────────────────────────────────────────────────────── */
  .settings-body { padding: 16px 14px; gap: 16px; }
  .account-columns { grid-template-columns: 1fr; }
  .settings-card { padding: 16px; }
  .settings-card-footer {
    flex-direction: column;
    align-items: stretch;
  }
  .settings-card-footer button { width: 100%; min-height: 44px; }

  /* ── Admin stats: 2×2 grid ────────────────────────────────────────── */
  .admin-stats { flex-wrap: wrap; }
  .admin-stat-card { flex: 1 1 calc(50% - 6px); min-width: 0; }
  .admin-body { padding: 14px 12px; }

  /* ── Modals: full-width on mobile ─────────────────────────────────── */
  .modal-box {
    margin: 12px;
    padding: 20px 18px;
    max-height: 90dvh;
    overflow-y: auto;
    border-radius: 16px;
  }
  .modal-actions {
    flex-direction: column-reverse;
    gap: 10px;
  }
  .modal-actions button { width: 100%; min-height: 48px; }

  /* ── Stocks header ────────────────────────────────────────────────── */
  #stocks-view .main-card-header {
    flex-wrap: wrap;
    gap: 8px;
  }

  /* ── Tags canvas: full width ──────────────────────────────────────── */
  #tags-view .main-card { border-radius: 12px; }

  /* ── Scrollbars wider on touch ────────────────────────────────────── */
  ::-webkit-scrollbar { width: 3px; }
}

/* ── Tablet: 768–1023px ───────────────────────────────────────────────── */
@media (min-width: 768px) and (max-width: 1023px) {
  .sidebar { width: 220px; }
  .account-columns { grid-template-columns: 1fr; }
  .main-content { padding: 12px; }
}

/* ── PWA standalone mode (installed to home screen) ──────────────────── */
/* Overrides the mobile media query above for proper safe-area handling.  */
/* black-translucent status bar means content starts at y=0 (behind the  */
/* status bar) — env(safe-area-inset-top) pushes it below.               */
@media (display-mode: standalone) and (max-width: 767px) {
  /* Push app content below the transparent status bar */
  .app-layout {
    padding-top: env(safe-area-inset-top);
  }

  /* Sidebar drawer is position:fixed top:0 — needs its own safe-area padding */
  .sidebar {
    padding-top: env(safe-area-inset-top);
  }

  /* Login page: center the form (no browser chrome fighting for space) */
  html, body { height: 100%; overflow: hidden; }
  .page-center {
    align-items: center;
    min-height: 100dvh;
    padding: max(env(safe-area-inset-top), 24px) 16px max(env(safe-area-inset-bottom), 24px);
  }
}
