:root {
  --bg: #04070c;
  --bg2: #0a111b;
  --panel: rgba(12, 22, 33, 0.82);
  --line: rgba(79, 227, 193, 0.22);
  --line-strong: rgba(79, 227, 193, 0.5);
  --accent: #4fe3c1;
  --accent-dim: #2a8f7a;
  --amber: #ffb547;
  --text: #dbe9f0;
  --muted: #7d93a3;
  --danger: #ff6b6b;
  --good: #4fe3c1;
  --radius: 10px;
  --mono: 'Cascadia Mono', 'Consolas', 'SFMono-Regular', Menlo, monospace;
  --sans: 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body.hud {
  background: radial-gradient(ellipse at 50% -20%, #0f2a33 0%, var(--bg) 60%) fixed, var(--bg);
  color: var(--text);
  font: 15px/1.55 var(--sans);
  overflow: hidden;
}
body.admin { overflow: auto; }

.grid-bg {
  position: fixed; inset: 0; pointer-events: none; z-index: 0;
  background-image:
    linear-gradient(rgba(79, 227, 193, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(79, 227, 193, 0.045) 1px, transparent 1px);
  background-size: 42px 42px;
  mask-image: radial-gradient(ellipse at center, #000 30%, transparent 85%);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.85em; }
.pad { padding: 10px 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; }
.warn-text { color: var(--amber); }
.nowrap { white-space: nowrap; }
a { color: var(--accent); }

/* ---------- buttons & inputs ---------- */
button, .button-like {
  font: inherit; cursor: pointer; border-radius: 8px; padding: 8px 14px;
  border: 1px solid var(--accent); background: linear-gradient(180deg, rgba(79, 227, 193, 0.25), rgba(79, 227, 193, 0.08));
  color: var(--text); letter-spacing: 0.02em; text-decoration: none; display: inline-flex; align-items: center; gap: 6px;
  transition: background 0.15s, box-shadow 0.15s, border-color 0.15s;
}
button:hover, .button-like:hover { box-shadow: 0 0 14px rgba(79, 227, 193, 0.35); }
button:disabled { opacity: 0.5; cursor: progress; }
button.ghost, .ghost.button-like { background: transparent; border-color: var(--line); }
button.ghost:hover, .ghost.button-like:hover { border-color: var(--accent); }
button.danger, .icon-btn.danger { color: var(--danger); }
button.wide { width: 100%; justify-content: center; }
button.icon-only { padding: 6px 10px; }
.small-btn { padding: 4px 9px; font-size: 0.85em; }

input, textarea {
  font: inherit; color: var(--text); background: rgba(3, 8, 14, 0.8);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%;
}
input:focus, textarea:focus, button:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(79, 227, 193, 0.18); }
label { display: block; color: var(--muted); font-size: 0.9em; }
label input, label textarea { margin-top: 4px; }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
label.check input { width: auto; margin: 0; }

/* ---------- the animated core ---------- */
.core { position: relative; width: 110px; height: 110px; margin: 0 auto 10px; }
.core span { position: absolute; inset: 0; border-radius: 50%; border: 2px solid transparent; }
.core span:nth-child(1) { border-top-color: var(--accent); border-bottom-color: var(--accent); animation: spin 7s linear infinite; }
.core span:nth-child(2) { inset: 15%; border-left-color: var(--amber); border-right-color: var(--amber); animation: spin 4.5s linear infinite reverse; }
.core span:nth-child(3) { inset: 29%; border: 1px dashed rgba(79, 227, 193, 0.6); animation: spin 12s linear infinite; }
.core i {
  position: absolute; inset: 40%; border-radius: 50%;
  background: radial-gradient(circle, #eafff9 0%, var(--accent) 45%, rgba(79, 227, 193, 0) 72%);
  box-shadow: 0 0 22px var(--accent); animation: pulse 3.2s ease-in-out infinite;
}
.core.big { width: 150px; height: 150px; }
.core.tiny { width: 28px; height: 28px; margin: 0; }
.core.tiny span { border-width: 1.5px; }
.core.busy span { animation-duration: 1.1s; }
.core.busy span:nth-child(2) { animation-duration: 0.8s; }
.core.speaking i { animation-duration: 0.55s; }
.core.dim { filter: grayscale(1) brightness(0.6); }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 0%, 100% { transform: scale(0.85); opacity: 0.75; } 50% { transform: scale(1.1); opacity: 1; } }

/* ---------- layout ---------- */
.view { position: relative; z-index: 1; height: 100%; }
.view.center { display: flex; align-items: center; justify-content: center; padding: 20px; }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.5), inset 0 0 30px rgba(79, 227, 193, 0.04); backdrop-filter: blur(6px);
}
.panel.narrow { width: min(420px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; }
.panel.narrow label { text-align: left; }
.brand { font-family: var(--mono); letter-spacing: 0.22em; text-transform: uppercase; color: var(--accent); text-shadow: 0 0 12px rgba(79, 227, 193, 0.45); margin: 0; }
h1.brand { font-size: 1.5rem; }
#code { text-align: center; font-family: var(--mono); letter-spacing: 0.12em; font-size: 1.1em; }

#app-view { display: flex; flex-direction: column; }
#app-view.hidden { display: none !important; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 18px; border-bottom: 1px solid var(--line); background: rgba(4, 9, 15, 0.85); position: relative; z-index: 2;
}
.brand-wrap { display: flex; align-items: center; gap: 12px; }
.brand-wrap .brand { font-size: 1.05rem; }
.status { font-family: var(--mono); font-size: 0.72rem; letter-spacing: 0.18em; color: var(--amber); border: 1px solid rgba(255, 181, 71, 0.4); padding: 2px 8px; border-radius: 20px; }
.top-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.quota { font-family: var(--mono); font-size: 0.8rem; color: var(--muted); }
.quota.warn { color: var(--danger); }
#toggle-sidebar { display: none; }

.layout { flex: 1; display: grid; grid-template-columns: 250px 1fr auto; min-height: 0; position: relative; z-index: 1; }
.sidebar { border-right: 1px solid var(--line); padding: 14px; overflow-y: auto; background: rgba(4, 9, 15, 0.55); }
.conv-list { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.conv { display: flex; align-items: stretch; border-radius: 8px; border: 1px solid transparent; }
.conv.active { border-color: var(--line-strong); background: rgba(79, 227, 193, 0.07); }
.conv-open { flex: 1; min-width: 0; flex-direction: column; align-items: flex-start; gap: 0; background: none; border: none; padding: 7px 9px; text-align: left; }
.conv-open:hover { box-shadow: none; background: rgba(79, 227, 193, 0.05); }
.conv-title { display: block; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 0.75em; color: var(--muted); }
.conv-del { background: none; border: none; color: var(--muted); padding: 0 9px; opacity: 0; }
.conv:hover .conv-del, .conv-del:focus-visible { opacity: 1; }
.conv-del:hover { color: var(--danger); box-shadow: none; }

/* ---------- chat ---------- */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 24px clamp(14px, 4vw, 48px); display: flex; flex-direction: column; gap: 16px; }
.msg { max-width: min(820px, 100%); }
.msg.user { align-self: flex-end; }
.msg .who { font-family: var(--mono); font-size: 0.7rem; letter-spacing: 0.18em; color: var(--muted); margin-bottom: 4px; }
.msg.user .who { text-align: right; }
.msg.bot .who { color: var(--accent-dim); }
.bubble { padding: 11px 15px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--panel); overflow-wrap: anywhere; }
.msg.user .bubble { white-space: pre-wrap; background: rgba(255, 181, 71, 0.08); border-color: rgba(255, 181, 71, 0.3); }
.msg.bot .bubble { border-left: 3px solid var(--accent); }
.msg.sys .bubble { border-color: rgba(255, 107, 107, 0.45); color: #ffc9c9; background: rgba(255, 107, 107, 0.07); }
.bubble p { margin: 0 0 0.7em; }
.bubble p:last-child, .bubble ul:last-child, .bubble ol:last-child { margin-bottom: 0; }
.bubble h3, .bubble h4, .bubble h5, .bubble h6 { margin: 0.6em 0 0.3em; color: var(--accent); }
.bubble ul, .bubble ol { margin: 0 0 0.7em; padding-left: 1.3em; }
.bubble code { font-family: var(--mono); font-size: 0.9em; background: rgba(79, 227, 193, 0.1); padding: 1px 5px; border-radius: 4px; }
.code { margin: 0.6em 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #02060a; }
.code-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 10px; font-family: var(--mono); font-size: 0.75rem; color: var(--muted); border-bottom: 1px solid var(--line); }
.code-head .copy { padding: 2px 8px; font-size: 0.75rem; background: transparent; border-color: var(--line); }
.code pre { margin: 0; padding: 12px; overflow-x: auto; }
.code pre code { background: none; padding: 0; font-size: 0.86rem; line-height: 1.5; }
.file-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.file-chip { font-family: var(--mono); font-size: 0.8rem; padding: 3px 9px; border: 1px solid var(--line-strong); border-radius: 20px; text-decoration: none; }
.file-chip:hover { background: rgba(79, 227, 193, 0.1); }

.dots { display: inline-flex; gap: 5px; padding: 4px 0; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: blink 1.2s infinite ease-in-out; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }

.welcome { margin: auto; text-align: center; max-width: 640px; padding: 20px 0; }
.welcome h2 { font-weight: 400; margin: 18px 0 4px; font-size: 1.6rem; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 20px; }
.chip { background: transparent; border-color: var(--line); font-size: 0.88rem; text-align: left; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 12px clamp(14px, 4vw, 48px) 18px; border-top: 1px solid var(--line); background: rgba(4, 9, 15, 0.7); }
.composer textarea { resize: none; max-height: 200px; min-height: 42px; }
.composer .icon { padding: 9px 11px; background: transparent; border-color: var(--line); }
.composer .icon.live { border-color: var(--danger); color: var(--danger); box-shadow: 0 0 14px rgba(255, 107, 107, 0.45); animation: pulse-border 1s infinite; }
@keyframes pulse-border { 50% { box-shadow: 0 0 2px rgba(255, 107, 107, 0.2); } }

/* ---------- workspace panel ---------- */
.files { width: 340px; border-left: 1px solid var(--line); padding: 14px; overflow-y: auto; background: rgba(4, 9, 15, 0.6); display: flex; flex-direction: column; gap: 10px; }
.files-head { display: flex; justify-content: space-between; align-items: baseline; }
.files h2 { margin: 0; font-family: var(--mono); font-size: 0.9rem; letter-spacing: 0.18em; text-transform: uppercase; color: var(--accent); }
.files p { margin: 0; }
.files-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.file-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
.file { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 6px; }
.file:hover { background: rgba(79, 227, 193, 0.05); }
.file-name { flex: 1; min-width: 0; background: none; border: none; padding: 2px; font-family: var(--mono); font-size: 0.82rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: block; text-align: left; }
.file-name:hover { box-shadow: none; color: var(--accent); }
.file-size { font-size: 0.75rem; color: var(--muted); }
.icon-btn { background: none; border: 1px solid transparent; padding: 1px 7px; border-radius: 6px; text-decoration: none; color: var(--accent); font-size: 0.95rem; }
.icon-btn:hover { border-color: var(--line); box-shadow: none; }
.editor { display: flex; flex-direction: column; gap: 6px; }
.editor input { font-family: var(--mono); font-size: 0.85rem; }
.editor textarea { font-family: var(--mono); font-size: 0.82rem; min-height: 260px; resize: vertical; white-space: pre; }
.row { display: flex; gap: 6px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: #0d1b26; border: 1px solid var(--line-strong); padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 30px rgba(0, 0, 0, 0.6);
  max-width: 90vw;
}

/* ---------- admin ---------- */
#dash-view { height: auto; min-height: 100%; }
.dash { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; padding: 18px; position: relative; z-index: 1; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 18px; min-width: 0; }
.card h2 { margin: 0 0 10px; font-family: var(--mono); font-size: 0.9rem; letter-spacing: 0.18em; text-transform: uppercase; color: var(--accent); }
.wide-card { grid-column: 1 / -1; }
.power-card { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.power-card p { margin: 0; }
.power { font-family: var(--mono); letter-spacing: 0.12em; font-size: 1rem; padding: 12px 20px; white-space: nowrap; }
.power.off { border-color: var(--danger); background: rgba(255, 107, 107, 0.12); color: #ffc9c9; }
.stack { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.stack button { align-self: flex-start; }
.row-form { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.row-form input { flex: 1; min-width: 160px; width: auto; }
.invite { border: 1px dashed var(--amber); border-radius: 10px; padding: 12px; margin-bottom: 12px; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.invite p { flex-basis: 100%; margin: 0; }
.invite-code { font-family: var(--mono); font-size: 1.25rem; letter-spacing: 0.14em; color: var(--amber); }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: 0.78rem; letter-spacing: 0.06em; text-transform: uppercase; border-bottom: 1px solid var(--line); padding: 6px 8px; }
.table td { padding: 8px; border-bottom: 1px solid rgba(79, 227, 193, 0.08); vertical-align: top; }
.table tr.revoked td { opacity: 0.6; }
.table td.detail { max-width: 420px; overflow-wrap: anywhere; }
.table td .small-btn { margin: 0 4px 4px 0; }
.limit-input { width: 80px; padding: 4px 6px; margin-right: 4px; }
.badge { display: inline-block; font-size: 0.72rem; font-family: var(--mono); letter-spacing: 0.08em; padding: 1px 8px; border-radius: 20px; border: 1px solid; }
.badge.good { color: var(--good); border-color: rgba(79, 227, 193, 0.5); }
.badge.bad { color: var(--danger); border-color: rgba(255, 107, 107, 0.5); }
.badge.warn { color: var(--amber); border-color: rgba(255, 181, 71, 0.5); }
.brain-list { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.brain-list li.disabled { opacity: 0.65; }

/* ---------- API keys, remote access ---------- */
select {
  font: inherit; color: var(--text); background: rgba(3, 8, 14, 0.8);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; min-width: 160px;
}
select:focus { outline: none; border-color: var(--accent); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.card-head h2 { margin: 0; }
.inline-label { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.inline-label input { margin: 0; width: auto; }
.row-form .inline-label input { min-width: 0; }
.key-code { font-family: var(--mono); font-size: 1rem; color: var(--amber); overflow-wrap: anywhere; user-select: all; }
.message-box { width: 100%; min-height: 220px; font-family: var(--mono); font-size: 0.82rem; resize: vertical; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 8px; }
.facts dt { color: var(--muted); font-size: 0.85em; }
.facts dd { margin: 0; font-family: var(--mono); font-size: 0.85em; overflow-wrap: anywhere; }
h3.sub { margin: 16px 0 6px; font-size: 0.85rem; font-family: var(--mono); letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); }
.fcc-note { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }
.fcc-note p { margin: 2px 0; }
.api-log { margin-top: 12px; }
.api-log summary { cursor: pointer; color: var(--muted); }
.table tr.expired td { opacity: 0.6; }
.limit-input.wide-limit { width: 120px; }
.tier-select { width: auto; padding: 4px 6px; }
.tier-select.owner { color: var(--amber); border-color: rgba(255, 181, 71, 0.6); }
.try-out { white-space: pre-wrap; overflow-wrap: anywhere; font-family: var(--mono); font-size: 0.85rem; max-height: 320px; overflow: auto; border: 1px solid rgba(255, 255, 255, 0.12); border-radius: 8px; padding: 10px; margin: 0; }

/* ---------- small screens ---------- */
@media (max-width: 860px) {
  #toggle-sidebar { display: inline-flex; }
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: absolute; inset: 0 auto 0 0; width: 260px; z-index: 5; transform: translateX(-105%); transition: transform 0.2s; background: #050b12; }
  .sidebar.open { transform: none; }
  .files { position: absolute; inset: 0 0 0 auto; width: min(360px, 100%); z-index: 5; background: #050b12; }
  .dash { grid-template-columns: 1fr; }
  .top-actions .quota { order: -1; width: 100%; }
}
