/* ==========================================================================
   APAI UI - shared stylesheet
   Plain CSS, no build step.

   Design rules (deliberately restrained):
     - flat surfaces, no gradients, no glows, no backdrop blur
     - 1px borders do the structural work; shadows only on modals
     - neutral greys as the base, one muted accent used sparingly
     - tight corner radii, compact type, colour reserved for status
   ========================================================================== */

:root {
  /* surfaces - neutral, barely tinted */
  --bg: #131416;
  --bg-elev: #191a1d;
  --bg-elev-2: #1f2124;
  --bg-input: #101113;
  --border: #2b2e33;
  --border-soft: #232629;

  /* text */
  --text: #e4e6e9;
  --text-dim: #9ba1a9;
  --text-faint: #6e747c;

  /* one accent, two shades: fill for buttons, lighter for text on dark */
  --accent: #3f6ea8;
  --accent-hover: #4a7cb8;
  --accent-fg: #8ab0dd;
  --accent-soft: rgba(63, 110, 168, 0.14);

  /* status - muted, never neon */
  --green: #57936f;
  --green-fg: #86c3a0;
  --red: #a85660;
  --red-fg: #e0919a;
  --amber: #96763c;
  --amber-fg: #d8b475;

  --radius: 6px;
  --radius-sm: 4px;

  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

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

h1, h2, h3 { margin: 0 0 .5rem; font-weight: 600; letter-spacing: -0.005em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .94rem; }

p { margin: 0 0 1rem; color: var(--text-dim); }

code, pre, .mono { font-family: var(--mono); font-size: .875em; }

/* ---------------------------------------------------------------- layout */

.wrap { max-width: 1120px; margin: 0 auto; padding: 0 20px; }
.wrap-narrow { max-width: 420px; margin: 0 auto; padding: 0 20px; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1120px; margin: 0 auto; padding: 0 20px;
  height: 52px; display: flex; align-items: center; gap: 20px;
}
.brand {
  font-weight: 650; font-size: .95rem; letter-spacing: .02em;
  color: var(--text); display: flex; align-items: center; gap: 8px;
  text-decoration: none;
}
.brand:hover { color: var(--text); text-decoration: none; }
.brand-dot {
  width: 8px; height: 8px; border-radius: 2px;
  background: var(--accent); flex-shrink: 0;
}
.nav { display: flex; gap: 2px; }
.nav a {
  color: var(--text-dim); padding: 6px 10px; border-radius: var(--radius-sm);
  font-size: .875rem; text-decoration: none;
}
.nav a:hover { background: var(--bg-elev-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent-fg); }
.spacer { flex: 1; }

.page { padding: 26px 0 60px; }
.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 18px; margin-bottom: 20px; flex-wrap: wrap;
}
.page-head p { margin: 0; }

.grid { display: grid; gap: 14px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 860px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------------------ cards */

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.card-tight { padding: 12px; }
.card h2 { margin-bottom: .3rem; }
.card-sub { color: var(--text-faint); font-size: .84rem; margin: 0; }

.stat-label {
  color: var(--text-faint); font-size: .75rem; font-weight: 500;
  letter-spacing: .02em; margin-bottom: 4px;
}
.stat-value { font-size: 1.3rem; font-weight: 600; letter-spacing: -0.01em; }
.stat-value.money { font-family: var(--mono); font-size: 1.15rem; }
.stat-hint { color: var(--text-faint); font-size: .8rem; margin-top: 3px; }

/* --------------------------------------------------------------- controls */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--bg-elev-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 7px 13px; font-size: .875rem; font-weight: 500;
  cursor: pointer; font-family: inherit; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover:not(:disabled) { background: #26282c; border-color: #3a3e44; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary {
  background: var(--accent); border-color: var(--accent); color: #f2f6fb;
  font-weight: 550;
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { color: var(--red-fg); border-color: #3d2c2f; }
.btn-danger:hover:not(:disabled) { background: #24191b; border-color: #55393d; }
.btn-sm { padding: 4px 9px; font-size: .8rem; }
.btn-block { width: 100%; }

label.field { display: block; margin-bottom: 13px; }
.field-label {
  display: block; font-size: .82rem; font-weight: 500;
  color: var(--text-dim); margin-bottom: 5px;
}
.field-hint { font-size: .78rem; color: var(--text-faint); margin-top: 4px; }

input[type=text], input[type=password], input[type=number],
input[type=email], input[type=datetime-local], select, textarea {
  width: 100%; background: var(--bg-input); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 10px; font-size: .9rem; font-family: inherit;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
input::placeholder, textarea::placeholder { color: var(--text-faint); }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row-end { justify-content: flex-end; }
.checkbox { display: flex; align-items: center; gap: 7px; font-size: .875rem; color: var(--text-dim); }
.checkbox input { width: auto; }

/* ---------------------------------------------------------------- tables */

table { width: 100%; border-collapse: collapse; font-size: .875rem; }
th {
  text-align: left; color: var(--text-faint); font-weight: 500;
  font-size: .78rem; padding: 8px 10px;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
td { padding: 10px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(255, 255, 255, 0.02); }
.table-scroll { overflow-x: auto; }

/* ---------------------------------------------------------------- badges */

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 1px 6px; border-radius: 3px;
  font-size: .75rem; font-weight: 500;
  background: var(--bg-elev-2); color: var(--text-dim);
  border: 1px solid var(--border);
}
.badge-green { background: rgba(87, 147, 111, .12); color: var(--green-fg); border-color: rgba(87, 147, 111, .3); }
.badge-red { background: rgba(168, 86, 96, .12); color: var(--red-fg); border-color: rgba(168, 86, 96, .3); }
.badge-amber { background: rgba(150, 118, 60, .12); color: var(--amber-fg); border-color: rgba(150, 118, 60, .3); }
.badge-blue { background: var(--accent-soft); color: var(--accent-fg); border-color: rgba(63, 110, 168, .3); }

/* --------------------------------------------------------------- alerts */

.alert {
  padding: 10px 12px; border-radius: var(--radius-sm); font-size: .875rem;
  margin-bottom: 13px; border: 1px solid; border-left-width: 3px;
}
.alert-error { background: rgba(168, 86, 96, .08); border-color: rgba(168, 86, 96, .35); color: var(--red-fg); }
.alert-ok { background: rgba(87, 147, 111, .08); border-color: rgba(87, 147, 111, .35); color: var(--green-fg); }
.alert-warn { background: rgba(150, 118, 60, .08); border-color: rgba(150, 118, 60, .35); color: var(--amber-fg); }
.alert-info { background: var(--accent-soft); border-color: rgba(63, 110, 168, .35); color: var(--accent-fg); }

.empty {
  text-align: center; color: var(--text-faint);
  padding: 30px 16px; font-size: .875rem;
}

.secret-box {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 9px 11px;
  font-family: var(--mono); font-size: .85rem; word-break: break-all;
}
.secret-box code { flex: 1; color: var(--text); }

/* ----------------------------------------------------------------- modal */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 56px 18px; overflow-y: auto;
}
.modal {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  width: 100%; max-width: 520px; padding: 20px;
}
.modal-wide { max-width: 760px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px; padding-bottom: 12px;
  border-bottom: 1px solid var(--border-soft);
}
.modal-head h2 { margin: 0; }
.modal-close {
  background: none; border: none; color: var(--text-faint);
  font-size: 1.35rem; cursor: pointer; line-height: 1; padding: 0 4px;
}
.modal-close:hover { color: var(--text); }

/* ------------------------------------------------------------ login page */

.auth-shell {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 30px 18px;
}
.auth-card {
  width: 100%; max-width: 400px;
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 8px; padding: 28px;
}
.auth-card .brand { justify-content: center; margin-bottom: 4px; font-size: 1.1rem; }
.auth-sub { text-align: center; color: var(--text-faint); font-size: .875rem; margin-bottom: 22px; }
.code-input {
  font-family: var(--mono); text-align: center; letter-spacing: .1em;
  font-size: .95rem; text-transform: uppercase;
}

/* -------------------------------------------------------------- chat ui */

.chat-shell {
  display: grid; grid-template-columns: 230px 1fr;
  gap: 14px; height: calc(100vh - 52px); padding: 14px 20px;
  max-width: 1320px; margin: 0 auto;
}
@media (max-width: 900px) { .chat-shell { grid-template-columns: 1fr; } }

.chat-side {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); display: flex; flex-direction: column;
  overflow: hidden;
}
.chat-side-head { padding: 10px; border-bottom: 1px solid var(--border-soft); }
.chat-sessions { flex: 1; overflow-y: auto; padding: 5px; }
.session-item {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 9px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: .85rem; color: var(--text-dim);
}
.session-item:hover { background: var(--bg-elev-2); color: var(--text); }
.session-item.active { background: var(--accent-soft); color: var(--accent-fg); }
.session-item span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-del {
  opacity: 0; background: none; border: none; color: var(--text-faint);
  cursor: pointer; font-size: .95rem; padding: 0 3px;
}
.session-item:hover .session-del { opacity: 1; }
.session-del:hover { color: var(--red-fg); }

.chat-main {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); display: flex; flex-direction: column; overflow: hidden;
}
.chat-head {
  padding: 9px 13px; border-bottom: 1px solid var(--border-soft);
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.chat-head select { width: auto; min-width: 180px; padding: 6px 9px; font-size: .85rem; }
.chat-balance { margin-left: auto; font-family: var(--mono); font-size: .82rem; color: var(--text-dim); }

.chat-log { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; gap: 10px; max-width: 86%; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar {
  width: 26px; height: 26px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: .68rem; font-weight: 600; background: var(--bg-elev-2);
  border: 1px solid var(--border); color: var(--text-faint);
}
.msg-user .msg-avatar { background: var(--accent-soft); color: var(--accent-fg); border-color: rgba(63, 110, 168, .3); }
.msg-body {
  background: var(--bg-elev-2); border: 1px solid var(--border-soft);
  border-radius: 8px; padding: 9px 12px; white-space: pre-wrap;
  word-wrap: break-word; font-size: .9rem;
}
.msg-user .msg-body { background: var(--bg-elev-2); border-color: var(--border); }
.msg-meta { font-size: .72rem; color: var(--text-faint); margin-top: 5px; font-family: var(--mono); }

.chat-compose { border-top: 1px solid var(--border-soft); padding: 11px 13px; }
.chat-compose textarea { min-height: 52px; max-height: 190px; }
.chat-compose-row { display: flex; gap: 8px; align-items: flex-end; margin-top: 8px; }
.compose-hint { font-size: .75rem; color: var(--text-faint); }

.typing span {
  display: inline-block; width: 4px; height: 4px; margin-right: 3px;
  border-radius: 50%; background: var(--text-faint); animation: blink 1.3s infinite;
}
.typing span:nth-child(2) { animation-delay: .18s; }
.typing span:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

/* -------------------------------------------------------------- docs page */

.docs-layout { display: grid; grid-template-columns: 190px 1fr; gap: 28px; }
@media (max-width: 900px) { .docs-layout { grid-template-columns: 1fr; } }
.docs-nav { position: sticky; top: 72px; align-self: start; }
.docs-nav a {
  display: block; padding: 5px 9px; border-radius: var(--radius-sm);
  color: var(--text-dim); font-size: .85rem; text-decoration: none;
}
.docs-nav a:hover { background: var(--bg-elev-2); color: var(--text); text-decoration: none; }

pre.code {
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px 14px; overflow-x: auto;
  font-size: .82rem; line-height: 1.55; color: #c6cdd6; margin: 0 0 14px;
}
.endpoint {
  display: flex; align-items: center; gap: 9px; flex-wrap: wrap;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 9px 11px; margin-bottom: 11px;
}
.method {
  font-family: var(--mono); font-size: .72rem; font-weight: 600;
  padding: 2px 6px; border-radius: 3px; letter-spacing: .03em;
  border: 1px solid;
}
.method-get { background: var(--accent-soft); color: var(--accent-fg); border-color: rgba(63, 110, 168, .35); }
.method-post { background: rgba(87, 147, 111, .12); color: var(--green-fg); border-color: rgba(87, 147, 111, .35); }
.endpoint code { color: var(--text); font-size: .875rem; }

.section { margin-bottom: 38px; scroll-margin-top: 70px; }

table.params td:first-child { font-family: var(--mono); color: var(--text); white-space: nowrap; }
table.params td:nth-child(2) { color: var(--text-faint); font-size: .82rem; white-space: nowrap; }

/* ---------------------------------------------------------------- misc */

.toast-host {
  position: fixed; bottom: 18px; right: 18px; z-index: 90;
  display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: var(--bg-elev-2); border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm); padding: 10px 13px;
  font-size: .875rem; max-width: 360px;
}
.toast-error { border-left-color: var(--red); }
.toast-ok { border-left-color: var(--green); }

.spinner {
  width: 14px; height: 14px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }
.muted { color: var(--text-faint); }
.dim { color: var(--text-dim); }
.nowrap { white-space: nowrap; }
.text-right { text-align: right; }
.mt-0 { margin-top: 0; }
.mt-1 { margin-top: 10px; }
.mt-2 { margin-top: 20px; }
.mb-0 { margin-bottom: 0; }
.mb-1 { margin-bottom: 10px; }
.mb-2 { margin-bottom: 20px; }
.mono-sm { font-family: var(--mono); font-size: .82rem; }
