:root {
  --bg: #0B1220;
  --surface: #111A2E;
  --surface-2: #17233D;
  --border: #243456;
  --text: #F1F5F9;
  --muted: #94A3B8;
  --accent: #38BDF8;
  --accent-strong: #0284C7;
  --green: #22C55E;
  --danger: #F87171;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

/* Network Offline Bar */
.net-banner {
  position: fixed;
  top: calc(var(--safe-top) + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  display: none;
  max-width: calc(100vw - 24px);
  text-align: center;
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.45);
}
.net-banner.offline { display: block; background: #991B1B; color: #FEF2F2; border: 1px solid #FCA5A5; }
.net-banner.online { display: block; background: #166534; color: #F0FDF4; border: 1px solid #86EFAC; }

/* Lock Screen */
.lock-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: radial-gradient(circle at top, #172A4D 0%, var(--bg) 62%);
}
.lock-card {
  width: 100%;
  max-width: 390px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 28px 22px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}
.lock-avatar {
  width: 84px;
  height: 84px;
  border-radius: 22px;
  border: 2px solid var(--accent);
  box-shadow: 0 10px 24px rgba(56, 189, 248, 0.25);
  margin-bottom: 12px;
}
.lock-card h1 {
  margin: 0;
  font-size: 23px;
  font-weight: 900;
  letter-spacing: 0.3px;
}
.lock-sub {
  margin: 6px 0 18px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}
.pin-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.pin-form input {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  text-align: center;
  letter-spacing: 3px;
  outline: none;
}
.pin-form input:focus { border-color: var(--accent); }
.lock-error {
  margin-top: 10px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--danger);
  min-height: 18px;
}
.lock-foot {
  margin: 14px 0 0;
  font-size: 11.5px;
  color: var(--muted);
}

/* App Shell */
.app-shell {
  height: 100dvh;
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-left: 1px solid rgba(36, 52, 86, 0.55);
  border-right: 1px solid rgba(36, 52, 86, 0.55);
}

.app-header {
  padding: calc(var(--safe-top) + 10px) 14px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}
.brand-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.header-avatar {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: 1.5px solid var(--accent);
  flex-shrink: 0;
}
.brand-text {
  min-width: 0;
}
.brand-text strong {
  display: block;
  font-size: 14.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brain-badge {
  display: block;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brain-badge.ok { color: #86EFAC; }
.brain-badge.warn { color: #FCD34D; }

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* Buttons */
.btn-primary {
  background: linear-gradient(135deg, var(--accent-strong), #2563EB);
  color: #FFFFFF;
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
}
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 13px;
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}
.btn-ghost {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 6px 10px;
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}
.btn-ghost.danger { color: #FCA5A5; border-color: rgba(248, 113, 113, 0.35); }

/* Tab Panes */
.tab-pane {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}
.tab-pane.active { display: flex; }

/* Chat Stream */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: 88%;
  padding: 11px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.55;
  word-wrap: break-word;
}
.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #0284C7, #1D4ED8);
  color: #FFFFFF;
  border-bottom-right-radius: 5px;
}
.msg.bot {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-bottom-left-radius: 5px;
}
.msg.bot strong { color: #BAE6FD; }
.msg.bot code {
  background: rgba(56, 189, 248, 0.14);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 12.5px;
}
.msg-img {
  display: block;
  max-width: 220px;
  max-height: 220px;
  border-radius: 10px;
  margin-bottom: 8px;
  border: 1px solid rgba(255, 255, 255, 0.25);
}
.msg-actions {
  margin-top: 8px;
  padding-top: 7px;
  border-top: 1px dashed rgba(148, 163, 184, 0.25);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.msg-act-btn {
  background: rgba(56, 189, 248, 0.12);
  color: #7DD3FC;
  border: 1px solid rgba(56, 189, 248, 0.35);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 11.5px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}
.msg-act-btn.saved {
  background: rgba(34, 197, 94, 0.16);
  color: #86EFAC;
  border-color: rgba(34, 197, 94, 0.4);
}
.msg-meta {
  font-size: 10.5px;
  color: var(--muted);
  margin-left: auto;
}

/* Quick Chips */
.quick-chips {
  display: flex;
  gap: 7px;
  padding: 6px 12px;
  overflow-x: auto;
  flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}
.quick-chips::-webkit-scrollbar { display: none; }
.q-chip {
  flex-shrink: 0;
  background: var(--surface);
  color: #BAE6FD;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 11px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}

/* Attachment Preview */
.attach-preview {
  margin: 0 12px 6px;
  padding: 8px 10px;
  background: var(--surface-2);
  border: 1px solid var(--accent);
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.attach-preview img {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 8px;
}
.attach-meta { flex: 1; min-width: 0; }
.attach-meta strong { display: block; font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-meta span { display: block; font-size: 11px; color: var(--muted); }

/* Composer */
.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 7px;
  padding: 9px 10px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-composer textarea {
  flex: 1;
  min-height: 40px;
  max-height: 115px;
  resize: none;
  padding: 9px 12px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  line-height: 1.4;
  outline: none;
}
.chat-composer textarea:focus { border-color: var(--accent); }
.icon-btn {
  height: 40px;
  padding: 0 11px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: #E2E8F0;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
  flex-shrink: 0;
}
.icon-btn.recording {
  background: #DC2626;
  border-color: #FCA5A5;
  color: #FFFFFF;
}
.send-btn {
  height: 40px;
  padding: 0 15px;
  border-radius: 11px;
  border: none;
  background: linear-gradient(135deg, var(--accent-strong), #2563EB);
  color: #FFFFFF;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
  flex-shrink: 0;
}
.send-btn:disabled { opacity: 0.5; }

/* Secondary Panes (Note & Brain) */
.pane-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px;
}
.card h2 { margin: 0 0 8px; font-size: 16px; }
.card-desc { margin: 0 0 14px; font-size: 13px; color: var(--muted); line-height: 1.5; }
.card-head-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.stack-form { display: flex; flex-direction: column; gap: 9px; }
.stack-form label { font-size: 12.5px; font-weight: 700; color: #CBD5E1; }
.stack-form input, .stack-form textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.stack-form input:focus, .stack-form textarea:focus { border-color: var(--accent); }
.form-row { display: flex; gap: 8px; margin-top: 4px; }
.form-row .btn-primary { flex: 1; }
.status-box {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 12.5px;
  font-weight: 700;
}
.status-box.ok { background: rgba(34, 197, 94, 0.16); color: #86EFAC; border: 1px solid rgba(34, 197, 94, 0.4); }
.status-box.err { background: rgba(248, 113, 113, 0.16); color: #FCA5A5; border: 1px solid rgba(248, 113, 113, 0.4); }

.meta-pill-box {
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 12.5px;
  color: #BAE6FD;
  margin-bottom: 14px;
  line-height: 1.5;
}
.sub-head { margin: 14px 0 6px; font-size: 13px; color: #7DD3FC; }
.code-preview {
  margin: 0;
  padding: 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: #E2E8F0;
  font-size: 12.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 260px;
  overflow-y: auto;
  font-family: 'Segoe UI', system-ui, sans-serif;
}

/* Bottom Navigation */
.bottom-nav {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 8px 10px calc(var(--safe-bottom) + 8px);
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.nav-btn {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 11px;
  padding: 9px 6px;
  font-size: 12.5px;
  font-weight: 800;
  cursor: pointer;
  font-family: inherit;
}
.nav-btn.active {
  background: rgba(56, 189, 248, 0.14);
  color: #7DD3FC;
  border-color: rgba(56, 189, 248, 0.4);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 78px;
  transform: translateX(-50%);
  z-index: 9999;
  background: #0F172A;
  color: #F8FAFC;
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 9px 16px;
  font-size: 12.5px;
  font-weight: 700;
  box-shadow: 0 14px 32px rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }
