/*
 * Calm white/grey interface. All colours come from custom properties, with a
 * dark variant via prefers-color-scheme. Fonts come from the system — no
 * external requests, works offline.
 */

:root {
  --bg: #ffffff;
  --bg-subtle: #f7f7f8;
  --bg-raised: #ffffff;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --text: #18181b;
  --text-muted: #71717a;
  --accent: #18181b;
  --accent-text: #ffffff;
  --bubble-user: #f1f1f3;
  --danger: #b42318;

  --radius: 10px;
  --radius-lg: 14px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;
  --sidebar-width: 260px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131316;
    --bg-subtle: #1a1a1e;
    --bg-raised: #1f1f24;
    --border: #2c2c32;
    --border-strong: #3a3a42;
    --text: #ececf1;
    --text-muted: #9b9ba4;
    --accent: #ececf1;
    --accent-text: #131316;
    --bubble-user: #26262c;
    --danger: #f87171;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

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

body.centered {
  display: grid;
  place-items: center;
  padding: var(--space-4);
  background: var(--bg-subtle);
}

h1 {
  font-size: 1.25rem;
  font-weight: 600;
  margin: 0 0 var(--space-3);
  letter-spacing: -0.01em;
}

.muted {
  color: var(--text-muted);
}

.small {
  font-size: 0.85rem;
}

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

/* ---------- forms ---------- */

input[type='email'],
input[type='text'],
textarea {
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--bg-raised);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 10px 12px;
}

input:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 10px 16px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 500;
}

button:hover:not(:disabled) {
  opacity: 0.88;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button.link,
a.link {
  background: none;
  border: none;
  padding: 0;
  color: var(--text-muted);
  text-decoration: underline;
  font-weight: 400;
}

button.link:hover,
a.link:hover {
  color: var(--text);
}

button.danger {
  color: var(--danger);
}

.icon-button {
  background: none;
  color: var(--text-muted);
  border: 1px solid var(--border);
  padding: 4px 10px;
  line-height: 1.4;
}

.icon-button:hover {
  color: var(--text);
  background: var(--bg-subtle);
}

.stack {
  display: grid;
  gap: var(--space-2);
}

.row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
}

.row--form {
  margin-bottom: var(--space-5);
}

.row--form input[type='email'] {
  flex: 1 1 220px;
  width: auto;
}

.checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* ---------- cards & notices ---------- */

.card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-6);
}

.notice {
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3);
  margin: 0 0 var(--space-4);
  font-size: 0.9rem;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
}

.notice--error {
  border-color: var(--danger);
  color: var(--danger);
}

/* ---------- admin page ---------- */

.page {
  max-width: 780px;
  margin: 0 auto;
  padding: var(--space-6) var(--space-4);
}

.page__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.table th,
.table td {
  text-align: left;
  padding: var(--space-3) var(--space-2);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.table th {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.table td.actions {
  text-align: right;
}

.table code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  color: var(--text-muted);
  background: var(--bg-subtle);
  padding: 1px 5px;
  border-radius: 4px;
}

/* ---------- knowledge base ---------- */

.panel {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4) var(--space-5);
  margin-bottom: var(--space-5);
  background: var(--bg-raised);
}

.panel h2 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0 0 var(--space-1);
}

.panel .table {
  margin-top: var(--space-3);
}

.row--between {
  justify-content: space-between;
  align-items: center;
}

.row--form input[type='text'] {
  flex: 1 1 220px;
  width: auto;
}

input[type='file'] {
  font: inherit;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.editor {
  width: 100%;
  min-height: 60vh;
  font-family: var(--font-mono);
  font-size: 0.88rem;
  line-height: 1.6;
  resize: vertical;
  tab-size: 2;
}

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

.app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--bg-subtle);
  border-right: 1px solid var(--border);
  min-height: 0;
  padding-bottom: env(safe-area-inset-bottom);
}

.sidebar__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 600;
  letter-spacing: -0.01em;
}

.conversations {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-2);
  display: grid;
  gap: 2px;
  align-content: start;
}

.conversation {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  border-radius: var(--radius);
  padding: 0 var(--space-1) 0 var(--space-3);
}

.conversation:hover {
  background: var(--bg-raised);
}

.conversation[aria-current='true'] {
  background: var(--bg-raised);
  border: 1px solid var(--border);
}

.conversation__open {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  color: var(--text);
  text-align: left;
  padding: var(--space-2) 0;
  font-weight: 400;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation__delete {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 2px 6px;
  opacity: 0;
}

.conversation:hover .conversation__delete,
.conversation__delete:focus-visible {
  opacity: 1;
}

.sidebar__foot {
  border-top: 1px solid var(--border);
  padding: var(--space-3) var(--space-4);
  display: grid;
  gap: var(--space-1);
}

.sidebar__foot .muted {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat__head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
}

.chat__head h1 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5) var(--space-4);
  display: grid;
  gap: var(--space-4);
  align-content: start;
}

.message {
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  display: grid;
  gap: var(--space-1);
}

.message__role {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.message__body {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message--user .message__body {
  background: var(--bubble-user);
  border-radius: var(--radius);
  padding: var(--space-3);
}

.message--error .message__body {
  color: var(--danger);
}

.message code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--bg-subtle);
  padding: 1px 4px;
  border-radius: 4px;
}

.empty-state {
  max-width: 720px;
  margin: auto;
  text-align: center;
  color: var(--text-muted);
}

.typing::after {
  content: '▍';
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink {
  to {
    visibility: hidden;
  }
}

@media (prefers-reduced-motion: reduce) {
  .typing::after {
    animation: none;
  }
}

.composer {
  display: flex;
  gap: var(--space-2);
  align-items: flex-end;
  padding: var(--space-3) var(--space-4) calc(var(--space-4) + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  max-width: 752px;
  width: 100%;
  margin: 0 auto;
}

.composer textarea {
  resize: none;
  max-height: 200px;
}

.only-mobile {
  display: none;
}

@media (max-width: 720px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(80vw, var(--sidebar-width));
    z-index: 10;
    transform: translateX(-100%);
    transition: transform 0.18s ease;
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .only-mobile {
    display: inline-block;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar {
    transition: none;
  }
}

/* ---------- admin settings form ---------- */

.settings {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 46rem;
}

.settings hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 0.5rem 0;
  width: 100%;
}

.settings .field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.settings .field > label {
  font-weight: 500;
}

.settings select,
.settings textarea,
.settings input[type='text'],
.settings input[type='number'] {
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border-strong);
  border-radius: 0.5rem;
  background: var(--bg-raised);
  color: var(--text);
  font: inherit;
}

.settings textarea {
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
}

.settings .muted {
  margin: 0;
}

.settings > button {
  align-self: flex-start;
  margin-top: 0.5rem;
}

/* ---------- thinking and sources ---------- */

.thinking {
  margin-bottom: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--bg-subtle);
  font-size: 0.9rem;
}

.thinking summary {
  cursor: pointer;
  padding: 0.4rem 0.6rem;
  color: var(--text-muted);
  user-select: none;
}

.thinking__body {
  padding: 0 0.6rem 0.6rem;
  color: var(--text-muted);
  white-space: pre-wrap;
}

.sources {
  margin: 0.75rem 0 0;
  padding-left: 1.1rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.sources li {
  margin-top: 0.2rem;
}

.sources a {
  color: inherit;
}

/* ---------- OpenRouter balance ---------- */

.balance {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
  padding: 0.85rem 1rem;
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 0.5rem;
  background: var(--bg-subtle);
}

.balance--ok { border-left-color: #16a34a; }
.balance--low { border-left-color: #d97706; }
.balance--empty { border-left-color: #dc2626; }

.balance__figure {
  display: flex;
  flex-direction: column;
}

.balance__amount {
  font-size: 1.6rem;
  font-weight: 600;
  line-height: 1.2;
}

.balance--empty .balance__amount { color: #dc2626; }
.balance--low .balance__amount { color: #d97706; }

.balance__detail {
  display: grid;
  grid-template-columns: auto auto;
  gap: 0.15rem 0.75rem;
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.balance__detail dt { grid-column: 1; }
.balance__detail dd { grid-column: 2; margin: 0; }

.balance__warning {
  margin: 0.6rem 0 0;
  font-size: 0.9rem;
  color: #dc2626;
}

.balance--low .balance__warning { color: #d97706; }
