* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--font);
}

a { color: var(--accent); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

/* ---------- Controls ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.block { width: 100%; }
.btn.large { padding: 12px 22px; font-size: 1.05rem; }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.3rem;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.field { display: grid; gap: 6px; font-size: 0.9rem; color: var(--muted); }
.field > span { font-weight: 500; }
.field select, .field input, .field textarea { color: var(--text); width: 100%; }
.field.inline { display: flex; align-items: center; gap: 10px; }
.field.inline select, .field.inline input { width: auto; }
.check { display: flex; align-items: center; gap: 8px; }

.banner { margin: 0; padding: 10px 14px; border-radius: var(--radius-sm); background: var(--surface-2); }
.banner.error { color: var(--danger); border: 1px solid currentColor; background: transparent; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  border: 1px solid currentColor;
}
.badge.ok { color: var(--ok); }
.badge.warn { color: var(--warn); }
.badge.bad { color: var(--danger); }
.badge.muted { color: var(--muted); }

.status { min-height: 1.4em; margin: 0 0 6px; font-size: 0.85rem; color: var(--muted); }
.status.error { color: var(--danger); }

/* ---------- Landing and prose pages ---------- */

body.landing { display: grid; place-items: center; min-height: 100vh; padding: 24px; }
.landing-card {
  width: min(480px, 100%);
  padding: 40px 32px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: center;
  display: grid;
  gap: 18px;
}
.brand-mark { margin: 0; font-size: 2rem; letter-spacing: -0.02em; }
.lede { margin: 0; color: var(--muted); }
.fine-print { margin: 0; font-size: 0.85rem; color: var(--muted); }

.prose { max-width: 720px; margin: 0 auto; padding: 40px 20px 80px; }
.prose h1 { letter-spacing: -0.02em; }

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

body.app { height: 100vh; overflow: hidden; }
.layout { display: grid; grid-template-columns: 280px 1fr; height: 100vh; }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; }
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.sidebar-bottom { margin-top: auto; }

.usage { display: grid; gap: 6px; font-size: 0.85rem; color: var(--muted); }
.usage-label { display: flex; justify-content: space-between; }
.meter { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.meter-fill { height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
.usage.full .meter-fill { background: var(--danger); }

.user-menu summary {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  list-style: none;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary:hover { background: var(--surface-2); }
.user-menu .menu { display: grid; gap: 10px; padding: 10px 8px 0; }
.avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; }

.chat { display: flex; flex-direction: column; min-width: 0; height: 100vh; }
.chat-header { align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 24px max(16px, calc((100% - 820px) / 2));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-state { margin: auto; text-align: center; }
.empty-state h2 { margin: 0 0 6px; }

.msg { max-width: 100%; }
.msg .meta { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 4px; }
.msg.user { align-self: flex-end; max-width: 80%; }
.msg.user .body {
  background: var(--user-bubble);
  color: var(--user-text);
  padding: 10px 14px;
  border-radius: var(--radius) var(--radius) 4px var(--radius);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.assistant .body { overflow-wrap: anywhere; }
.msg.streaming .body::after {
  content: "▍";
  margin-left: 2px;
  animation: blink 1s steps(2) infinite;
  color: var(--muted);
}
@keyframes blink { 50% { opacity: 0; } }
.msg .note { display: block; margin-top: 6px; font-size: 0.8rem; color: var(--muted); }
.msg .error-note { display: block; margin-top: 6px; font-size: 0.85rem; color: var(--danger); }
.msg.failed.user .body { opacity: 0.7; }

/* Markdown */
.msg .body > :first-child { margin-top: 0; }
.msg .body > :last-child { margin-bottom: 0; }
.msg .body pre {
  position: relative;
  background: var(--code-bg);
  color: var(--code-text);
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font: 0.88rem/1.5 var(--mono);
}
.msg .body code { font-family: var(--mono); font-size: 0.9em; }
.msg .body :not(pre) > code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.msg .body table { border-collapse: collapse; }
.msg .body th, .msg .body td { border: 1px solid var(--border); padding: 4px 8px; }
.msg .body blockquote { margin: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 9px;
  font: 0.75rem var(--font);
  color: var(--code-text);
  background: rgb(255 255 255 / 0.08);
  border: 1px solid rgb(255 255 255 / 0.15);
  border-radius: 6px;
  cursor: pointer;
}
.copy-btn:hover { background: rgb(255 255 255 / 0.16); }

.composer-wrap { padding: 0 max(16px, calc((100% - 820px) / 2)) 16px; }
.composer { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 200px;
  padding: 12px 14px;
  border-radius: var(--radius);
  line-height: 1.45;
}

.scrim { display: none; }

/* ---------- Admin ---------- */

body.admin { padding: 0 0 60px; }
.admin-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px max(16px, calc((100% - 1100px) / 2));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.pill { font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.admin-main { padding: 0 max(16px, calc((100% - 1100px) / 2)); }
.tabs { display: flex; gap: 4px; margin: 20px 0 8px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tabs button {
  padding: 10px 14px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tab-panel { padding-top: 12px; }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 16px; }
.card {
  display: grid;
  gap: 12px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.card h3 { margin: 0; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card p { margin: 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.card .row input { flex: 1; min-width: 160px; }
.key-mask { font-family: var(--mono); }

.toolbar { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar input[type="search"] { flex: 1; min-width: 200px; }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { color: var(--muted); font-weight: 600; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; }
.table tr:last-child td { border-bottom: 0; }
.table tr.unavailable td { color: var(--muted); }
.table input[type="number"] { width: 90px; }
.table input[type="text"] { width: 100%; min-width: 120px; }
.user-cell { display: flex; align-items: center; gap: 10px; }
.user-cell small { display: block; color: var(--muted); }
.pager { display: flex; align-items: center; gap: 12px; justify-content: center; margin-top: 12px; }
.settings-form { display: grid; gap: 16px; max-width: 560px; }

/* ---------- Small screens ---------- */

.only-mobile { display: none; }

@media (max-width: 767px) {
  .only-mobile { display: flex; }
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(300px, 85vw);
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .scrim { display: block; position: fixed; inset: 0; background: rgb(0 0 0 / 0.4); z-index: 10; }
  .msg.user { max-width: 92%; }
}

@media (prefers-reduced-motion: reduce) {
  .msg.streaming .body::after { animation: none; }
  .sidebar, .meter-fill { transition: none; }
}

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

/* ---------- Attachments ---------- */

.composer { flex-wrap: wrap; }
.composer textarea { min-width: 0; }
.composer.dragging { outline: 2px dashed var(--accent); outline-offset: 4px; border-radius: var(--radius); }
.attachments, .chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments { flex-basis: 100%; }
.chips { margin-top: 8px; }
.msg.user .chips { justify-content: flex-end; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 4px 6px 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 0.82rem;
}
.chip .label { display: inline-flex; gap: 6px; min-width: 0; color: var(--text); text-decoration: none; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.chip .info { color: var(--muted); }
.chip.error { border-color: var(--danger); }
.chip.error .info { color: var(--danger); white-space: normal; }
.chip button { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 0.8rem; padding: 2px 4px; border-radius: 6px; }
.chip button:hover { color: var(--danger); }
.chip.has-audio { flex-wrap: wrap; border-radius: var(--radius-sm); }
.chip audio { height: 32px; max-width: 240px; }
.thumb img { display: block; max-width: 180px; max-height: 140px; border-radius: var(--radius-sm); border: 1px solid var(--border); object-fit: cover; }
.mic-btn.recording { color: var(--danger); font-variant-numeric: tabular-nums; font-size: 0.95rem; }

/* ---------- Saved chats ---------- */

.history { display: grid; gap: 6px; min-height: 0; }
.history-title { margin: 0; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.history-status { margin: 0; font-size: 0.85rem; }
.history-status.error { color: var(--danger); }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 40vh; overflow-y: auto; }
.history-list[inert] { opacity: 0.6; }
.history-list li { display: flex; align-items: stretch; gap: 2px; }
.history-item {
  flex: 1;
  min-width: 0;
  display: grid;
  text-align: left;
  padding: 6px 8px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.history-item:hover, .history-item[aria-current="true"] { background: var(--surface-2); }
.history-item .title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.9rem; }
.history-item .date { font-size: 0.72rem; color: var(--muted); }
.history-delete { border: 0; background: transparent; color: var(--muted); cursor: pointer; border-radius: var(--radius-sm); padding: 0 8px; }
.history-delete:hover { color: var(--danger); background: var(--surface-2); }

/* ---------- 3D landing (glass) ---------- */

body.landing {
  position: relative;
  overflow-x: hidden; /* the page may still scroll down when the card is taller than the window */
  color: #fff;
  background: radial-gradient(circle at 30% 20%, #4f46e5 0%, #1e1b4b 45%, #0b0a1f 100%);
  color-scheme: dark;
}
.scene { position: fixed; inset: 0; perspective: 900px; pointer-events: none; }
.cube {
  --size: 90px;
  position: absolute;
  width: var(--size);
  height: var(--size);
  transform-style: preserve-3d;
  animation: cube-spin 16s linear infinite, cube-bob 6s ease-in-out infinite;
}
.cube i {
  position: absolute;
  inset: 0;
  border: 1px solid rgb(255 255 255 / 0.5);
  background: linear-gradient(135deg, rgb(255 255 255 / 0.26), rgb(129 140 248 / 0.1));
  box-shadow: inset 0 0 22px rgb(255 255 255 / 0.16);
}
.cube i:nth-child(1) { transform: translateZ(calc(var(--size) / 2)); }
.cube i:nth-child(2) { transform: rotateY(180deg) translateZ(calc(var(--size) / 2)); }
.cube i:nth-child(3) { transform: rotateY(90deg) translateZ(calc(var(--size) / 2)); }
.cube i:nth-child(4) { transform: rotateY(-90deg) translateZ(calc(var(--size) / 2)); }
.cube i:nth-child(5) { transform: rotateX(90deg) translateZ(calc(var(--size) / 2)); }
.cube i:nth-child(6) { transform: rotateX(-90deg) translateZ(calc(var(--size) / 2)); }
.cube-1 { left: 10%; top: 16%; }
.cube-2 { --size: 60px; right: 12%; top: 12%; animation-duration: 12s, 7s; animation-direction: reverse, normal; }
.cube-3 { --size: 46px; right: 22%; bottom: 16%; animation-duration: 10s, 5s; }
.cube-4 { --size: 70px; left: 18%; bottom: 12%; animation-duration: 20s, 8s; animation-direction: reverse, normal; }
@keyframes cube-spin { from { transform: rotateX(20deg) rotateY(0deg); } to { transform: rotateX(380deg) rotateY(360deg); } }
@keyframes cube-bob { 0%, 100% { translate: 0 0; } 50% { translate: 0 -18px; } }

body.landing .landing-card {
  position: relative;
  z-index: 1;
  background: rgb(255 255 255 / 0.12);
  border: 1px solid rgb(255 255 255 / 0.3);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.4);
  color: #fff;
}
body.landing .lede, body.landing .fine-print { color: rgb(255 255 255 / 0.85); }
body.landing a:not(.btn) { color: #c7d2fe; }
body.landing .btn.primary { background: #fff; border-color: #fff; color: #3730a3; }
body.landing .btn.primary:hover { background: #e0e7ff; }
body.landing .banner.error { color: #fecaca; }
body.landing .btn:focus-visible, body.landing a:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }

/* 404: an extruded number that tilts slowly, plus a glass second button. */
.notfound-code {
  margin: 0;
  font-size: clamp(4.5rem, 20vw, 8rem);
  font-weight: 800;
  line-height: 1;
  color: #fff;
  text-shadow:
    0 1px 0 #c7d2fe, 0 2px 0 #a5b4fc, 0 3px 0 #818cf8, 0 4px 0 #6366f1,
    0 5px 0 #4f46e5, 0 6px 0 #4338ca, 0 16px 28px rgb(0 0 0 / 0.45);
  transform: perspective(600px) rotateX(14deg);
  animation: code-tilt 8s ease-in-out infinite;
}
@keyframes code-tilt {
  0%, 100% { transform: perspective(600px) rotateX(14deg) rotateY(-12deg); }
  50% { transform: perspective(600px) rotateX(14deg) rotateY(12deg); }
}
.landing-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
body.landing .btn.glass { background: rgb(255 255 255 / 0.1); border-color: rgb(255 255 255 / 0.4); color: #fff; }
body.landing .btn.glass:hover { background: rgb(255 255 255 / 0.2); }

/* ---------- Layered app (depth) ---------- */

body.app, body.admin { background: linear-gradient(160deg, var(--bg-2), var(--bg) 70%); }
body.admin { min-height: 100vh; } /* so the gradient reaches the bottom of a short page */
.layout { gap: 14px; padding: 14px; }
.sidebar { border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-depth); }
.chat { height: 100%; min-height: 0; gap: 12px; }
.chat-log { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-depth); }
.composer-wrap { padding: 0; }
.composer {
  padding: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-depth);
  transform: translateY(-2px);
}
.composer textarea { border-color: transparent; background: var(--surface-2); }
.msg.user .body { box-shadow: 0 8px 18px rgb(79 70 229 / 0.25); }
.msg.assistant .body {
  padding: 10px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) var(--radius) 4px;
}
.card, .table-wrap, .admin-header { box-shadow: var(--shadow-depth); }
.btn.danger { background: var(--danger-bg); border-color: var(--danger-bg); color: #fff; }

/* ---------- Focus (every control shows where keyboard focus is) ---------- */

.icon-btn:focus-visible,
.tabs button:focus-visible,
.copy-btn:focus-visible,
.user-menu summary:focus-visible,
.history-item:focus-visible,
.history-delete:focus-visible,
.chip button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
/* These sit inside a scrolling parent (.tabs, .history-list) that clips anything drawn outside
   them, so their ring goes inside the box. The tabs keep 2px clear for the selected underline. */
.tabs button:focus-visible { outline-offset: -4px; }
.history-item:focus-visible,
.history-delete:focus-visible { outline-offset: -2px; }

/* ---------- Motion and small screens ---------- */

@media (max-width: 767px) {
  .layout { gap: 8px; padding: 8px; }
  .sidebar { border-radius: 0 var(--radius) var(--radius) 0; }
  .sidebar:not(.open) { box-shadow: none; } /* a hidden drawer must not leak its shadow onto the screen */
}

@media (max-width: 600px) {
  .cube { animation: none; transform: rotateX(-24deg) rotateY(32deg); }
  .cube-3, .cube-4 { display: none; }
  .notfound-code { animation: none; }
}

@media (prefers-reduced-motion: reduce) {
  .cube { animation: none; transform: rotateX(-24deg) rotateY(32deg); }
  .composer { transform: none; }
  .notfound-code { animation: none; }
}
