:root {
  --bg: #fafafa;
  --bg-elevated: #ffffff;
  --border: #e4e4e7;
  --text: #18181b;
  --text-muted: #71717a;
  --accent: #ea580c;
  --accent-hover: #c2410c;
  --accent-soft: #fff7ed;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: #fef2f2;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --info: #2563eb;
  --info-soft: #eff6ff;
  --font-sans:
    "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --bg-elevated: #18181b;
    --border: #27272a;
    --text: #fafafa;
    --text-muted: #a1a1aa;
    --accent: #f97316;
    --accent-hover: #fb923c;
    --accent-soft: #1c1410;
    --danger: #f87171;
    --danger-hover: #fca5a5;
    --danger-soft: #2a1414;
    --success: #4ade80;
    --success-soft: #112218;
    --info: #60a5fa;
    --info-soft: #102036;
  }
}

* {
  box-sizing: border-box;
}

html,
body,
#app {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

.app {
  display: flex;
  height: 100%;
}

.sidebar {
  width: 240px;
  flex: 0 0 240px;
  border-right: 1px solid var(--border);
  background: var(--bg-elevated);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.sidebar h1 {
  font-size: 0.95rem;
  margin: 0 0 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sidebar h1::before {
  content: "";
  width: 1.25rem;
  height: 1.25rem;
  background: var(--accent);
  border-radius: 0.25rem;
}

.sidebar-link {
  padding: 0.5rem 0.75rem;
  border-radius: 0.375rem;
  color: var(--text);
  font-size: 0.9rem;
}
.sidebar-link:hover {
  background: var(--bg);
  text-decoration: none;
}
.sidebar-link[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.toolbar {
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.body {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
}

.center {
  display: grid;
  place-items: center;
  height: 100%;
  padding: 1rem;
}

.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.5rem;
  width: 100%;
  max-width: 24rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

label {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: block;
  margin-bottom: 0.25rem;
}

input,
select,
textarea {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 0.375rem;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}
input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5rem 0.875rem;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: 0;
  transition: background 120ms;
}
.btn-primary {
  background: var(--accent);
  color: white;
}
.btn-primary:hover {
  background: var(--accent-hover);
}
.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-danger {
  background: var(--danger);
  color: white;
}
.btn-danger:hover {
  background: var(--danger-hover);
}
.btn[disabled],
.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.btn[disabled]:hover,
.btn:disabled:hover {
  background: var(--accent);
}
.btn-danger[disabled]:hover,
.btn-danger:disabled:hover {
  background: var(--danger);
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
}

/* ---------- Toast notifications (top-right stack) ---------- */
.toaster {
  position: fixed;
  top: 0.75rem; right: 0.75rem;
  z-index: 90;
  display: flex; flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
  max-width: min(360px, 90vw);
}
.toast {
  pointer-events: auto;
  padding: 0.6rem 0.85rem;
  border-radius: 0.5rem;
  font-size: 0.88rem;
  line-height: 1.35;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  animation: toast-slide-in 180ms ease-out;
  word-break: break-word;
}
.toast-success { border-left: 4px solid var(--success); background: var(--success-soft); }
.toast-error   { border-left: 4px solid var(--danger);  background: var(--danger-soft); }
.toast-info    { border-left: 4px solid var(--info);    background: var(--info-soft); }
@keyframes toast-slide-in {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ---------- Confirm modal styling ---------- */
.modal-confirm {
  max-width: 420px;
  width: calc(100vw - 2rem);
  padding: 1.2rem 1.25rem 1.1rem;
  border-radius: 0.6rem;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.18);
}

/* ---------- Spinner (inline in buttons) ---------- */
.spinner {
  width: 0.9em; height: 0.9em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: spin 700ms linear infinite;
  vertical-align: -2px;
  margin-right: 0.35rem;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- Empty state ---------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
  gap: 0.4rem;
}
.empty-state-icon  { font-size: 2.2rem; opacity: 0.7; }
.empty-state-title { font-size: 1.05rem; color: var(--text); font-weight: 500; }
.empty-state-hint  { font-size: 0.88rem; max-width: 28rem; line-height: 1.5; }

/* ---------- Drag-drop on chat input ---------- */
.chat-input-dragover {
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
  background: var(--accent-soft);
}

/* ---------- Command palette (⌘K) ---------- */
.cmdp {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 0.6rem;
  width: min(560px, 92vw);
  max-height: 70vh;
  display: flex; flex-direction: column;
  box-shadow: 0 24px 64px rgba(0,0,0,0.25);
  overflow: hidden;
}
.cmdp-input {
  border: 0 !important;
  border-bottom: 1px solid var(--border) !important;
  border-radius: 0;
  padding: 0.9rem 1rem;
  font-size: 1rem;
  outline: none;
}
.cmdp-list {
  overflow-y: auto;
  flex: 1;
  padding: 0.3rem 0.4rem;
}
.cmdp-item {
  display: flex;
  align-items: baseline;
  gap: 0.55rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 0;
  background: transparent;
  text-align: left;
  border-radius: 0.4rem;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text);
}
.cmdp-item:hover { background: var(--bg); }
.cmdp-group {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  flex: 0 0 5.5rem;
}
.cmdp-label { font-weight: 500; }
.cmdp-hint {
  color: var(--text-muted);
  font-size: 0.78rem;
  margin-left: auto;
  text-align: right;
}
.cmdp-empty {
  padding: 1.2rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.88rem;
}
.cmdp-foot {
  border-top: 1px solid var(--border);
  padding: 0.45rem 0.85rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  justify-content: space-between;
}

/* ---------- Lightbox (image zoom modal) ---------- */
.lightbox-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
  animation: lb-fade 140ms ease-out;
  cursor: zoom-out;
}
@keyframes lb-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
.lightbox-img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 0.4rem;
  box-shadow: 0 12px 48px rgba(0,0,0,0.6);
  cursor: default;
}
.lightbox-close {
  position: absolute;
  top: 0.6rem; right: 0.8rem;
  width: 40px; height: 40px;
  border-radius: 20px;
  border: 0;
  background: rgba(255, 255, 255, 0.15);
  color: white;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.25); }

/* ---------- Skeleton loading rows ---------- */
.skeleton-list { display: flex; flex-direction: column; gap: 0.55rem; padding: 0.5rem 0; }
.skeleton-row {
  height: 2.6rem;
  border-radius: 0.4rem;
  background: linear-gradient(90deg, var(--bg) 0%, var(--bg-elevated) 50%, var(--bg) 100%);
  background-size: 200% 100%;
  animation: skel-shimmer 1.4s linear infinite;
}
@keyframes skel-shimmer {
  to { background-position: -200% 0; }
}

/* ---------- Form helpers ---------- */
.field-error {
  color: var(--danger);
  font-size: 0.78rem;
  margin-top: 0.25rem;
}
.field-hint {
  color: var(--text-muted);
  font-size: 0.78rem;
  margin-top: 0.25rem;
}
.input-error,
input.invalid,
textarea.invalid,
select.invalid {
  border-color: var(--danger) !important;
}
/* Password strength bar */
.pw-strength {
  height: 4px; border-radius: 2px; margin-top: 0.35rem;
  background: var(--border); overflow: hidden;
}
.pw-strength-fill {
  height: 100%;
  transition: width 160ms ease-out, background 160ms ease-out;
}

/* --- chat page additions --- */
.chat-body { display: flex; flex-direction: column; height: calc(100% - 56px); padding: 0; }
.messages { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem; }
.msg { padding: 0.6rem 0.9rem; border-radius: 0.5rem; max-width: 70%; background: var(--bg-elevated); border: 1px solid var(--border); }
.msg-user { align-self: flex-end; background: var(--accent-soft); border-color: var(--accent); }
.msg-role { font-size: 0.7rem; color: var(--text-muted); margin-bottom: 0.2rem; text-transform: uppercase; }
.msg-content { white-space: pre-wrap; word-wrap: break-word; }

/* Markdown-rendered assistant messages — neutral defaults, code-block focus. */
.msg-md { white-space: normal; }
.msg-md p { margin: 0.4rem 0; }
.msg-md p:first-child { margin-top: 0; }
.msg-md p:last-child { margin-bottom: 0; }
.msg-md h1, .msg-md h2, .msg-md h3, .msg-md h4 { margin: 0.6rem 0 0.3rem; font-weight: 600; }
.msg-md h1 { font-size: 1.15rem; }
.msg-md h2 { font-size: 1.05rem; }
.msg-md h3 { font-size: 0.95rem; }
.msg-md ul, .msg-md ol { margin: 0.4rem 0; padding-left: 1.4rem; }
.msg-md li { margin: 0.15rem 0; }
.msg-md a { color: var(--accent); text-decoration: underline; }
.msg-md code {
  background: var(--bg-deep, rgba(0,0,0,0.08));
  padding: 0.05rem 0.3rem;
  border-radius: 0.25rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
}
.msg-md pre {
  background: var(--bg-deep, rgba(0,0,0,0.08));
  padding: 0.6rem 0.8rem;
  border-radius: 0.5rem;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  line-height: 1.45;
  margin: 0.4rem 0;
}
.msg-md pre code { background: none; padding: 0; border-radius: 0; font-size: inherit; }

/* Syntect-highlighted fence — inline-styled spans inside, dark background. */
.msg-md pre.hl {
  background: #2d2d2d;
  color: #d3d0c8;
  padding: 0.75rem 0.85rem;
}
.msg-md pre.hl code { color: inherit; }
/* Hover-revealed action row on each message. */
.msg { position: relative; }
.msg-actions {
  display: flex;
  gap: 0.25rem;
  margin-top: 0.25rem;
  opacity: 0;
  transition: opacity 120ms;
}
.msg:hover .msg-actions { opacity: 1; }
.msg-action {
  font-size: 0.85rem;
  padding: 0.1rem 0.4rem;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 0.25rem;
  cursor: pointer;
  line-height: 1;
}
.msg-action:hover { background: var(--bg); }

/* Tool-call chips rendered above an assistant message. */
.tool-chips { display: flex; flex-direction: column; gap: 0.3rem; margin-bottom: 0.4rem; }
.tool-chip {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  font-size: 0.78rem;
}
.tool-chip summary {
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  color: var(--text-muted);
  list-style: none;
}
.tool-chip summary::-webkit-details-marker { display: none; }
.tool-chip[open] summary { border-bottom: 1px solid var(--border); }
.tool-chip-section { padding: 0.35rem 0.5rem; }
.tool-chip-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  margin-bottom: 0.15rem;
}
.tool-chip pre {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 0.78rem;
  max-height: 200px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-md pre.hl[data-lang]::before {
  content: attr(data-lang);
  display: block;
  font-size: 0.7rem;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.4rem;
}
.msg-md blockquote {
  margin: 0.4rem 0;
  padding: 0.2rem 0.8rem;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
.msg-md table { border-collapse: collapse; margin: 0.4rem 0; font-size: 0.88em; }
.msg-md th, .msg-md td { border: 1px solid var(--border); padding: 0.25rem 0.5rem; }
.msg-md hr { border: 0; border-top: 1px solid var(--border); margin: 0.6rem 0; }
.chat-input { display: flex; gap: 0.5rem; padding: 0.75rem 1rem; border-top: 1px solid var(--border); }
.chat-input input[type=text] { flex: 1; }
.toolbar select { font: inherit; padding: 0.3rem 0.5rem; border-radius: 0.3rem; border: 1px solid var(--border); background: var(--bg-elevated); color: var(--text); }
.toolbar select + select { margin-left: 0.5rem; }

/* --- chat page layout (sidebar + main) --- */
.chat-page { display: flex; height: 100%; min-height: 0; }
.chat-sidebar {
  flex: 0 0 240px;
  width: 240px;
  border-right: 1px solid var(--border);
  background: var(--bg-elevated);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.chat-sidebar-header { padding: 0.75rem; border-bottom: 1px solid var(--border); }
.chat-list { flex: 1; overflow-y: auto; padding: 0.5rem; }
.chat-list-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.5rem 0.6rem;
  border-radius: 0.375rem;
  cursor: pointer;
  font-size: 0.875rem;
}
.chat-list-item:hover { background: var(--bg); }
.chat-list-item.active { background: var(--accent-soft); color: var(--accent); }
.chat-list-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list-delete {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0.1rem 0.35rem;
  border-radius: 0.25rem;
  visibility: hidden;
}
.chat-list-item:hover .chat-list-delete { visibility: visible; }
.chat-list-delete:hover { background: var(--bg); color: var(--text); }
.chat-list-item mark { background: var(--accent-soft); color: var(--accent); padding: 0 0.15em; border-radius: 0.15em; }
.chat-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

/* --- Mobile (≤ 768px): collapse the outer app sidebar + chat sidebar into
       slide-in drawers so the main content uses the full viewport. Two
       drawers are addressable:
         1. .sidebar (app shell — Settings/Providers/MCP/Admin nav). Opened
            via the .mobile-topbar hamburger in Shell (app.rs).
         2. .chat-sidebar (chat list + search). Opened via the toolbar
            hamburger in chat.rs.
       Each drawer has its own backdrop. Both ignore .is-open on desktop. */
@media (max-width: 768px) {
  .app { flex-direction: column; }
  /* Mobile top bar: 48px sticky banner at viewport top, contains the
     app-shell hamburger + instance name. Pushes page content down by the
     same amount (handled per-layout via padding-top below). */
  .mobile-topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    height: 48px;
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0 0.7rem;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
  }
  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    width: 84%; max-width: 320px;
    z-index: 70;
    transform: translateX(-100%);
    transition: transform 180ms ease-out;
    box-shadow: 0 0 24px rgba(0,0,0,0.25);
    overflow-y: auto;
  }
  .sidebar.is-open { transform: translateX(0); }
  /* Backdrop for the app-shell drawer. Above chat-sidebar (z 50) so the
     shell drawer covers it cleanly when both could otherwise overlap. */
  .shell-backdrop {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 65; opacity: 0; pointer-events: none;
    transition: opacity 180ms ease-out;
  }
  .shell-backdrop.is-open { opacity: 1; pointer-events: auto; }

  .chat-page { flex-direction: column; }
  .chat-sidebar {
    position: fixed;
    /* Sit BELOW the mobile-topbar (48px) so the shell hamburger stays
       accessible even when the chat drawer is open. */
    inset: 48px auto 0 0;
    width: 84%; max-width: 320px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 180ms ease-out;
    box-shadow: 0 0 24px rgba(0,0,0,0.25);
    overflow-y: auto;
  }
  .chat-sidebar.is-open { transform: translateX(0); }
  .messages { padding: 0.6rem 0.5rem; }
  .msg { max-width: 92%; }
  .toolbar { padding: 0.5rem 0.7rem; flex-wrap: wrap; gap: 0.4rem; }
  .toolbar select { font-size: 0.9rem; padding: 0.5rem 0.55rem; min-height: 40px; }
  .chat-input { padding: 0.5rem 0.6rem; flex-wrap: wrap; gap: 0.45rem; align-items: center; }
  /* Layout: tool toggles + 📎 sit on the top row (default order). The
     text input + Send button form their own row below via order: 10 and
     a wrapping flex setup. */
  .chat-input input[type=text] {
    flex: 1 1 100%; order: 10;
    /* iOS Safari zoom guard — anything <16px triggers an auto-zoom on focus. */
    font-size: 16px;
    min-height: 44px;
  }
  .chat-input > button[type=submit],
  .chat-input > button[type=button] { order: 11; min-height: 44px; min-width: 44px; }
  .chat-input > button[type=submit] { min-width: 4.5rem; font-weight: 600; }
  /* Generic touch-target floor — inputs/buttons/selects 44px on mobile. */
  input, select, textarea {
    font-size: 16px; /* iOS zoom guard */
    min-height: 44px;
    padding: 0.65rem 0.8rem;
  }
  textarea { min-height: 6rem; }
  .btn {
    min-height: 44px;
    padding: 0.65rem 1rem;
    font-size: 0.95rem;
  }
  .body { padding: 0.9rem; }
  /* Tighten generic chrome on mobile — most settings pages render a card
     list with horizontal padding; trim it so cards fit phone widths. */
  .card { padding: 0.9rem; }
  table { font-size: 0.85rem; }
  /* Sidebar nav links need bigger taps too. */
  .sidebar-link { padding: 0.7rem 0.9rem; font-size: 0.95rem; }
  /* Backdrop dimmer for the chat-list drawer. */
  .sidebar-backdrop {
    position: fixed; inset: 0; background: rgba(0,0,0,0.4);
    z-index: 40; opacity: 0; pointer-events: none;
    transition: opacity 180ms ease-out;
  }
  .sidebar-backdrop.is-open { opacity: 1; pointer-events: auto; }
  /* Hamburger button — shown only on mobile. */
  .hamburger {
    display: inline-flex; align-items: center; justify-content: center;
    width: 36px; height: 36px;
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    border-radius: 0.4rem;
    font-size: 1.1rem;
    cursor: pointer;
  }
}
@media (min-width: 769px) {
  .hamburger { display: none; }
  .sidebar-backdrop { display: none; }
  .shell-backdrop { display: none; }
  .mobile-topbar { display: none; }
}

/* --- modal --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: grid;
  place-items: center;
  z-index: 50;
}
.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1.25rem;
  min-width: 320px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
