:root {
  --bg: #fafafa;
  --bg-elevated: #ffffff;
  --border: #e4e4e7;
  --text: #18181b;
  --text-muted: #71717a;
  --accent: #ea580c;
  --accent-hover: #c2410c;
  --accent-soft: #fff7ed;
  --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;
  }
}

* {
  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);
}

.error {
  color: #b91c1c;
  font-size: 0.85rem;
}

/* --- 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 a
       slide-in drawer so the main content can use the full viewport. The
       drawer is toggled by a hamburger button injected by the SPA (#nav-toggle
       in widgets.rs) — when checked, the sidebar slides in from the left.
       This avoids JS state by piggybacking the :checked pseudo-class on a
       hidden checkbox.  */
@media (max-width: 768px) {
  .app { flex-direction: column; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    width: 80%; max-width: 320px;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform 180ms ease-out;
    box-shadow: 0 0 24px rgba(0,0,0,0.25);
  }
  .sidebar.is-open { transform: translateX(0); }
  .chat-page { flex-direction: column; }
  .chat-sidebar {
    position: fixed; inset: 56px auto 0 0;
    width: 80%; 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);
  }
  .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.85rem; padding: 0.25rem 0.4rem; }
  .chat-input { padding: 0.5rem 0.6rem; flex-wrap: wrap; }
  .chat-input input[type=text] { width: 100%; order: 10; }
  .body { padding: 0.8rem; }
  /* Backdrop dimmer when either drawer is open. */
  .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; }
}

/* --- 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;
}
