:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-muted: #f1f3f6;
  --text: #1c2430;
  --text-muted: #5f6b7a;
  --border: #e1e5ea;
  --border-strong: #c9d0d8;
  --accent: #2563eb;
  --accent-hover: #1d4fd8;
  --accent-soft: #e8efff;
  --success: #15803d;
  --success-soft: #e7f6ec;
  --warning: #a15c07;
  --warning-soft: #fdf3e1;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --info: #0e7490;
  --info-soft: #e3f4f8;
  --radius: 8px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
  --sidebar-w: 232px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu,
    Cantarell, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
}

h1,
h2,
h3 {
  margin: 0 0 0.5rem;
  line-height: 1.25;
  font-weight: 600;
}
h1 {
  font-size: 1.5rem;
}
h2 {
  font-size: 1.1rem;
}
h3 {
  font-size: 0.95rem;
  margin-top: 1rem;
}
p {
  margin: 0 0 0.75rem;
}

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

code,
pre {
  font-family: var(--font-mono);
  font-size: 0.85em;
}
code {
  background: var(--surface-muted);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  word-break: break-all;
}
/* Short identifiers in table cells (model ids, cron) must not wrap per character. */
td code {
  white-space: nowrap;
  word-break: normal;
}
pre {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem;
  margin: 0.4rem 0 0.75rem;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.pre-scroll {
  max-height: 240px;
}
.pre-wrap {
  white-space: pre-wrap;
  word-break: break-word;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.muted {
  color: var(--text-muted);
}
.small {
  font-size: 0.82rem;
}
.text-danger {
  color: var(--danger);
}
.nowrap {
  white-space: nowrap;
}
.mono {
  font-family: var(--font-mono);
}

/* Layout */
.app {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 0.75rem;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  display: block;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--text);
  padding: 0 0.6rem 1rem;
}
.brand:hover {
  text-decoration: none;
}

.nav {
  flex: 1;
}
.nav-group {
  margin-bottom: 1rem;
}
.nav-group-title {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 0 0.6rem 0.3rem;
}
.nav-link {
  display: block;
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius-sm);
  color: var(--text);
}
.nav-link:hover {
  background: var(--surface-muted);
  text-decoration: none;
}
.nav-link.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 0.9rem 0.6rem 0;
}
.admin-name {
  font-weight: 600;
  margin-bottom: 0.5rem;
  word-break: break-word;
}

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

.topbar {
  display: none;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar-title {
  font-weight: 600;
}

.content {
  width: 100%;
  max-width: 1280px;
  padding: 1.75rem 2rem 3rem;
}

.nav-toggle {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.nav-backdrop {
  display: none;
}

@media (max-width: 860px) {
  .topbar {
    display: flex;
  }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: 0 0 24px rgba(0, 0, 0, 0.12);
  }
  .nav-toggle:checked ~ .app .sidebar {
    transform: none;
  }
  .nav-toggle:checked ~ .app .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.35);
    z-index: 30;
  }
  .nav-toggle:focus-visible ~ .app .nav-button {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }
  .content {
    padding: 1.25rem 1rem 2.5rem;
  }
}

/* Page header & cards */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}
.page-header p {
  margin: 0;
  max-width: 72ch;
}
.page-actions {
  display: flex;
  gap: 0.5rem;
}
.section-title {
  margin: 1.5rem 0 0.75rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.25rem;
  margin-bottom: 1.25rem;
  min-width: 0;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
  flex-wrap: wrap;
}
.card-title {
  margin: 0;
  font-size: 1rem;
}
.card-actions {
  display: flex;
  gap: 0.5rem;
}
.card-narrow {
  max-width: 760px;
}
.card-preview {
  border-color: #b7e2c4;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}
.grid .card {
  margin-bottom: 0;
}
.stat-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 0.35rem;
}
.stat-value {
  font-size: 1.15rem;
  font-weight: 600;
  word-break: break-word;
}
.stat-meta {
  margin-top: 0.5rem;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.empty {
  color: var(--text-muted);
  padding: 0.75rem 0;
  margin: 0;
}

/* Tables */
.table-wrap {
  overflow-x: auto;
  margin: 0 -1.25rem;
  padding: 0 1.25rem;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th,
td {
  text-align: left;
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
th {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  background: var(--surface-muted);
  white-space: nowrap;
}
tbody tr:hover {
  background: #fafbfc;
}
tbody tr:last-child td {
  border-bottom: none;
}

/* Badges */
.badge {
  display: inline-block;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
  background: var(--surface-muted);
  color: var(--text-muted);
}
.badge-success {
  background: var(--success-soft);
  color: var(--success);
}
.badge-warning {
  background: var(--warning-soft);
  color: var(--warning);
}
.badge-danger {
  background: var(--danger-soft);
  color: var(--danger);
}
.badge-info {
  background: var(--info-soft);
  color: var(--info);
}
.badge-accent {
  background: var(--accent-soft);
  color: var(--accent);
}
.badge code {
  background: none;
  padding: 0;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.45rem 0.9rem;
  font: inherit;
  font-weight: 500;
  line-height: 1.3;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover {
  background: var(--surface-muted);
  text-decoration: none;
}
.btn-sm {
  padding: 0.25rem 0.6rem;
  font-size: 0.82rem;
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover {
  background: var(--accent-hover);
}
.btn-danger {
  color: var(--danger);
  border-color: #f1b8b2;
}
.btn-danger:hover {
  background: var(--danger-soft);
}
.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover {
  background: var(--surface-muted);
  color: var(--text);
}
.btn-block {
  width: 100%;
  padding: 0.65rem;
}
.btn:disabled,
.btn.is-loading,
.btn.htmx-request {
  opacity: 0.6;
  cursor: progress;
}
.btn.is-loading::after,
.btn.htmx-request::after {
  content: "";
  width: 0.8em;
  height: 0.8em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}
.inline-form {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0;
}

/* Forms */
input[type="text"],
input[type="url"],
input[type="number"],
input[type="password"],
input[type="date"],
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.6rem;
  width: 100%;
  min-width: 0;
}
textarea {
  min-height: 140px;
  resize: vertical;
  font-family: var(--font-mono);
  font-size: 0.88rem;
}
.textarea-tall {
  min-height: 320px;
}
input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent);
}
.inline-form input,
.inline-form select {
  width: auto;
}
.input-narrow {
  width: 4.5rem !important;
}
.select-sm {
  padding: 0.2rem 0.4rem;
  font-size: 0.82rem;
  max-width: 14rem;
}

.form {
  margin: 0;
}
.field {
  margin-bottom: 1rem;
  min-width: 0;
}
.field label {
  display: block;
  font-weight: 500;
  margin-bottom: 0.3rem;
}
.field-inline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.field-inline label {
  margin: 0;
}
.help {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}
.field-error {
  color: var(--danger);
  font-size: 0.82rem;
  margin-top: 0.25rem;
}
.has-error input,
.has-error select,
.has-error textarea {
  border-color: var(--danger);
}
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0 1rem;
}
.form-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
  align-items: end;
}
.filters label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}
.filter-actions {
  display: flex;
  gap: 0.4rem;
}

/* Flash */
.flash {
  padding: 0.7rem 1rem;
  border-radius: var(--radius-sm);
  margin-bottom: 1rem;
  border: 1px solid transparent;
}
.flash-success {
  background: var(--success-soft);
  color: var(--success);
  border-color: #b7e2c4;
}
.flash-error {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #f1b8b2;
}
.flash-info {
  background: var(--info-soft);
  color: var(--info);
  border-color: #b6dfe8;
}

/* Misc components */
.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-top: 1rem;
}

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  list-style: none;
  padding: 0;
  margin: 0.25rem 0 0.75rem;
}

.chain {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  list-style: none;
  padding: 0;
  margin: 0;
}
.chain li {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
}
.chain li + li::before {
  content: "→";
  color: var(--text-muted);
  margin-right: 0.35rem;
}
.chain-role {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
}

.target-list {
  list-style: none;
  padding: 0;
  margin: 0 0 0.35rem;
}
.target-list li {
  display: flex;
  align-items: center;
  gap: 0.2rem;
}

details > summary {
  cursor: pointer;
  font-weight: 500;
}
.help-box summary {
  margin: 0;
}
.help-box ul {
  margin: 0.6rem 0 0;
  padding-left: 1.2rem;
}
.disclosure {
  margin-top: 0.75rem;
}
.disclosure > summary {
  list-style: none;
}
.disclosure > summary::-webkit-details-marker {
  display: none;
}
.disclosure[open] > summary {
  margin-bottom: 0.75rem;
}

.message {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.9rem;
  margin-bottom: 0.75rem;
}
.message-bot {
  background: var(--accent-soft);
  border-color: #c9d7fb;
}
.message-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.tool-calls {
  list-style: none;
  padding: 0;
  margin: 0;
}
.tool-call {
  border: 1px solid var(--border);
  border-left: 4px solid var(--success);
  border-radius: var(--radius-sm);
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
}
.tool-call-error {
  border-left-color: var(--danger);
}
.tool-call-head {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.4rem;
}

.settings-list {
  display: flex;
  flex-direction: column;
}
.setting {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr);
  gap: 1rem;
  padding: 0.9rem 0;
  border-bottom: 1px solid var(--border);
}
.setting:last-child {
  border-bottom: none;
}
.setting-label {
  font-weight: 600;
}
.setting-control {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 0.4rem;
}
.setting-control .inline-form {
  flex: 1;
  min-width: 220px;
}
.setting-control .inline-form input,
.setting-control .inline-form select {
  flex: 1;
}
.setting-control .field-error {
  flex-basis: 100%;
}
.unit {
  color: var(--text-muted);
  font-size: 0.82rem;
}
@media (max-width: 760px) {
  .setting {
    grid-template-columns: 1fr;
  }
}

/* Auth & error pages */
.auth {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2rem;
}
.auth-card .brand {
  padding: 0 0 0.75rem;
  color: var(--accent);
}
.auth-card h1 {
  font-size: 1.25rem;
}

.error-page {
  text-align: center;
  padding: 3rem 1rem;
}
.error-code {
  font-size: 3rem;
  font-weight: 700;
  color: var(--border-strong);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.meta-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.25rem 1rem;
  margin: 0;
}
.meta-list dt {
  color: var(--muted, #6b7280);
}
.meta-list dd {
  margin: 0;
}
.qr svg {
  display: block;
  width: 264px;
  max-width: 100%;
  height: auto;
  background: #fff;
}
