:root {
  --bg: #0e1116; --panel: #161b22; --panel-2: #1c2230; --line: #2a3140;
  --fg: #dce3ec; --muted: #8b97a8; --accent: #4c9aff; --accent-2: #3ddc97;
  --danger: #ff6b6b; --warn: #ffbf47; --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 14px/1.5 system-ui, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
  height: 100dvh; /* the mobile address bar shrinks the real viewport */
  padding-bottom: env(safe-area-inset-bottom);
}
.muted { color: var(--muted); }
.hidden { display: none !important; }
h2, h3 { margin: 18px 0 8px; font-weight: 600; }
button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--fg);
  padding: 6px 12px; border-radius: 7px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #06101d; font-weight: 600; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.ghost { background: transparent; }
button.block { display: block; width: 100%; }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea {
  background: #0b0f14; border: 1px solid var(--line); border-radius: 7px; padding: 7px 9px;
}
textarea { resize: vertical; font-family: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; letter-spacing: .2px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent-2); display: inline-block; }

.auth-body { display: grid; place-items: center; }
.auth-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 28px 30px; width: min(360px, 92vw); display: grid; gap: 12px;
}
.auth-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-error { color: var(--danger); font-size: 13px; margin: 0; }

.topbar {
  display: flex; align-items: center; gap: 18px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.tabs { display: flex; gap: 6px; }
.tab { border: none; background: transparent; color: var(--muted); padding: 7px 12px; }
.tab.active { color: var(--fg); background: var(--panel-2); border-radius: 7px; }
.who { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.inline { display: inline; }
.nav-toggle { display: none; font-size: 17px; line-height: 1; padding: 5px 10px; }
/* Hidden until the panel opens: an invisible-but-present overlay swallows every click. */
.scrim { display: none; position: fixed; inset: 0; background: rgba(4, 7, 12, .62); z-index: 40; }
.ban-cell { display: flex; align-items: center; gap: 6px; }
.ban-cell select { width: auto; }

.layout { flex: 1; display: grid; grid-template-columns: 258px 1fr; min-height: 0; }
.sidebar { border-right: 1px solid var(--line); padding: 12px; display: flex; flex-direction: column; gap: 10px; overflow: auto; }
.chat-list { display: flex; flex-direction: column; gap: 4px; overflow: auto; }
.chat-item {
  text-align: left; border: 1px solid transparent; background: transparent;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; display: flex; gap: 8px; align-items: center;
}
.chat-item:hover { background: var(--panel); }
.chat-item.active { background: var(--panel-2); border-color: var(--line); }
.chat-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.quota { font-size: 12px; border-top: 1px solid var(--line); padding-top: 8px; }

.content { padding: 14px 18px; overflow: auto; min-width: 0; }
.view { display: none; flex-direction: column; min-height: 0; }
.view.active { display: flex; height: 100%; }
#view-stats.active, #view-admin.active { display: block; }

.chat-head { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
.title-input { flex: 1; background: transparent; border-color: transparent; font-weight: 600; }
.title-input:hover { border-color: var(--line); }
.system-prompt { margin-bottom: 10px; background: #0b0f14; }

.messages { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 12px; padding: 4px 2px 12px; }
.msg { max-width: 92ch; border-radius: 12px; padding: 10px 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: #1b3a5c; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); }
.msg.assistant.partial { border-color: var(--warn); }
.msg.assistant.error { border-color: var(--danger); }
.msg pre {
  background: #080b10; border: 1px solid var(--line); border-radius: 8px; padding: 10px;
  overflow: auto; font-family: var(--mono); font-size: 12.5px; white-space: pre; margin: 8px 0;
}
.msg code { font-family: var(--mono); background: #080b10; padding: 1px 4px; border-radius: 4px; }
.msg pre code { padding: 0; background: none; }
.msg .tag { font-size: 11px; color: var(--muted); margin-bottom: 4px; text-transform: uppercase; letter-spacing: .4px; }
.msg details { margin: 6px 0; }
.msg summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.thinking { color: var(--muted); font-style: italic; white-space: pre-wrap; }

.queue-banner {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px;
  padding: 7px 11px; margin-bottom: 8px; font-size: 13px; color: var(--warn);
}
.composer { border-top: 1px solid var(--line); padding-top: 10px; display: grid; gap: 8px; }
.composer textarea { width: 100%; }
.composer-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.opt { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.opt input { width: 74px; }
.opt input[type=checkbox] { width: auto; }
.spacer { flex: 1; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 10px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.card b { display: block; font-size: 19px; margin-top: 2px; }
.card small { color: var(--muted); font-size: 11.5px; }
.card.warn { border-color: var(--warn); }
.card.bad { border-color: var(--danger); }

.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.table { border-collapse: collapse; width: 100%; font-size: 13px; }
.table th, .table td { padding: 7px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.table th { color: var(--muted); font-weight: 600; background: var(--panel); position: sticky; top: 0; }
.table tr:last-child td { border-bottom: none; }
.table td input, .table td select { padding: 3px 5px; width: 78px; background: #0b0f14; }
.table .num { text-align: right; font-family: var(--mono); }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 20px; border: 1px solid var(--line); }
.badge.on { color: var(--accent-2); border-color: var(--accent-2); }
.badge.off { color: var(--danger); border-color: var(--danger); }
.badge.q { color: var(--warn); } .badge.r { color: var(--accent); }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-top: 12px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 10px; padding-top: 10px; }
.grid-form label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.grid-form label.check { flex-direction: row; align-items: center; }
.grid-form button { align-self: end; }

.toast {
  position: fixed; right: 18px; bottom: 18px; background: var(--panel-2);
  border: 1px solid var(--accent); border-radius: 9px; padding: 10px 14px;
  opacity: 0; transform: translateY(8px); transition: .2s; pointer-events: none; max-width: 60ch;
}
.toast.show { opacity: 1; transform: none; }
.toast.err { border-color: var(--danger); }

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px 10px; padding: 8px 10px; }
  .nav-toggle { display: block; }
  .brand { font-size: 13px; }
  .tabs { order: 3; width: 100%; overflow-x: auto; }
  .who { gap: 6px; }
  .who button { padding: 5px 9px; font-size: 13px; }

  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 50; top: 0; bottom: 0; left: 0; width: min(320px, 86vw);
    background: var(--panel); border-right: 1px solid var(--line); overflow-y: auto;
    transform: translateX(-101%); transition: transform .18s ease-out;
    padding-top: calc(12px + env(safe-area-inset-top));
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; }

  .content { padding: 10px 12px; }
  .messages { gap: 9px; }
  .msg { max-width: 100%; }
  .msg pre { font-size: 12px; }

  /* iOS zooms the whole page when a focused control is smaller than 16px */
  input, select, textarea { font-size: 16px; }
  .opt input { width: 80px; }
  .composer-row { gap: 8px 10px; }
  .composer .spacer { display: none; }
  .composer #btn-send, .composer #btn-cancel { flex: 1; }

  .cards { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }

  /* A twelve-column table cannot be scrolled by thumb: turn rows into captioned cards. */
  .table-wrap { border: none; overflow: visible; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table thead { display: none; }
  .table tr {
    background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
    padding: 6px 10px; margin-bottom: 8px;
  }
  .table td {
    display: flex; justify-content: space-between; align-items: center; gap: 12px;
    border: none; padding: 4px 0; white-space: normal;
  }
  .table td::before { content: attr(data-label); color: var(--muted); font-size: 12px; }
  .table td:empty { display: none; }
  .table td[colspan] { justify-content: flex-start; }
  .table td input, .table td select { width: 46%; min-width: 92px; text-align: right; }
  .ban-cell { justify-content: flex-end; }

  .grid-form { grid-template-columns: 1fr; }
  .toast {
    left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom));
    max-width: none;
  }
}
