/*
 * studio-chat admin — one stylesheet, no build step (served as-is from public/).
 * Quiet, dense chrome; the one loud thing is the transcript: her turns in plum,
 * his in plain white, so a conversation reads like the chat it is.
 */

:root {
    --bg: #f2f3f6;
    --panel: #ffffff;
    --panel-2: #f8f9fb;
    --ink: #1c2130;
    --ink-2: #454c5e;
    --muted: #767d8f;
    --faint: #a3a9b7;
    --line: #e1e4ea;
    --line-2: #eceef2;

    --accent: #6b3fa0;
    --accent-ink: #4e2a7a;
    --accent-soft: #f3edfa;
    --accent-line: #e0d2f0;

    --ok: #1d7347;
    --ok-soft: #e5f3eb;
    --warn: #8f5507;
    --warn-soft: #fcf2df;
    --warn-line: #f1dcae;
    --danger: #b42318;
    --danger-soft: #fdecea;
    --danger-line: #f6c9c4;

    --radius: 8px;
    --radius-sm: 6px;
    --sans: "Instrument Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
    --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
    font-family: var(--sans);
    font-size: 14px;
    line-height: 1.5;
    color: var(--ink);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}
[x-cloak] { display: none !important; }

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-ink); text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }

h1 { font-size: 21px; line-height: 1.25; font-weight: 600; letter-spacing: -0.01em; margin: 0; }
h2 { font-size: 15px; line-height: 1.3; font-weight: 600; margin: 0 0 10px; }
h3 { font-size: 13.5px; font-weight: 600; margin: 0 0 6px; }
p { margin: 0 0 10px; color: var(--ink-2); }
code, pre, .mono { font-family: var(--mono); font-size: 12px; }
code { background: var(--line-2); border-radius: 4px; padding: 1px 5px; }
pre {
    margin: 0;
    padding: 10px 12px;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.55;
    overflow: auto;
}
pre.tall { max-height: 420px; }

/* ---------- Shell ---------- */

.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }

.side {
    background: var(--panel);
    border-right: 1px solid var(--line);
    padding: 18px 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.side .brand {
    display: flex; align-items: center; gap: 9px;
    font-weight: 600; font-size: 15px; color: var(--ink);
    padding: 0 8px 16px;
}
.side .brand-mark {
    width: 22px; height: 22px; border-radius: 7px 7px 7px 2px;
    background: var(--accent);
    flex: none;
}
.side .group { font-size: 12px; color: var(--faint); padding: 14px 10px 4px; }
.side a {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    color: var(--ink-2); padding: 7px 10px; border-radius: var(--radius-sm);
    font-size: 14px; text-decoration: none;
}
.side a:hover { background: var(--panel-2); color: var(--ink); }
.side a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.side .count {
    font-size: 11.5px; font-weight: 600; line-height: 18px; min-width: 20px; text-align: center;
    padding: 0 6px; border-radius: 999px; background: var(--danger); color: #fff;
}
.side .foot { margin-top: auto; padding: 14px 10px 0; border-top: 1px solid var(--line-2); }
.side .who { font-size: 12.5px; color: var(--muted); margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

main { padding: 24px 32px 48px; min-width: 0; }

/* ---------- Page header ---------- */

.page-head {
    display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap;
    margin-bottom: 18px;
}
.page-head .sub { color: var(--muted); font-size: 13px; margin-top: 4px; }
.page-head .actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.crumb { font-size: 13px; margin-bottom: 6px; display: inline-block; }

.section { margin-top: 28px; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.section-head h2 { margin: 0; }
.section-head .hint { font-size: 12.5px; color: var(--muted); }

/* ---------- Panels ---------- */

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.panel-pad { padding: 16px 18px; }
.panel + .panel { margin-top: 14px; }
.panel-head {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 12px 16px; border-bottom: 1px solid var(--line-2);
}
.panel-head h2 { margin: 0; font-size: 14px; }

/* ---------- Buttons ---------- */

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    font: inherit; font-size: 13px; font-weight: 500; line-height: 20px;
    padding: 5px 12px; border-radius: var(--radius-sm);
    border: 1px solid var(--line); background: var(--panel); color: var(--ink);
    cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--panel-2); border-color: #cfd3db; color: var(--ink); text-decoration: none; }
.btn:disabled, .btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-ink); border-color: var(--accent-ink); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #8f1c13; border-color: #8f1c13; color: #fff; }
.btn-danger-ghost { color: var(--danger); border-color: var(--danger-line); background: var(--panel); }
.btn-danger-ghost:hover { background: var(--danger-soft); color: var(--danger); border-color: var(--danger-line); }
.btn-sm { font-size: 12.5px; padding: 2px 9px; line-height: 18px; }
.btn-link {
    font: inherit; font-size: 13px; background: none; border: 0; padding: 0;
    color: var(--accent); cursor: pointer;
}
.btn-link:hover { color: var(--accent-ink); text-decoration: underline; }
.btn-link.quiet { color: var(--muted); }
.btn-link.quiet:hover { color: var(--ink); }
.btn-link.danger { color: var(--danger); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; background: var(--panel); }
.seg button { font: inherit; font-size: 13px; border: 0; background: none; padding: 4px 12px; cursor: pointer; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }

/* ---------- Forms ---------- */

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span, .field > label { font-size: 12.5px; color: var(--muted); }
.input, .select, .textarea {
    font: inherit; font-size: 13.5px; color: var(--ink);
    padding: 6px 9px; border: 1px solid #d5d9e0; border-radius: var(--radius-sm);
    background: var(--panel); min-width: 0;
}
.input:focus, .select:focus, .textarea:focus { outline: 2px solid var(--accent-line); border-color: var(--accent); }
.input.mono { font-family: var(--mono); font-size: 12.5px; }
.input-sm { font-size: 12.5px; padding: 3px 7px; }
.textarea { resize: vertical; line-height: 1.45; }
.check { display: inline-flex; align-items: center; gap: 7px; font-size: 13.5px; color: var(--ink-2); cursor: pointer; }
.check input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.error-text { color: var(--danger); font-size: 12.5px; }

.filters {
    display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end;
    padding: 14px 16px; border-bottom: 1px solid var(--line-2);
}
.filters .grow { flex: 1 1 240px; }
.filters-more {
    display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: flex-end;
    padding: 12px 16px; background: var(--panel-2); border-bottom: 1px solid var(--line-2);
}

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th {
    text-align: left; font-weight: 500; font-size: 12.5px; color: var(--muted);
    padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--panel);
}
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: #fbfbfd; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .nowrap { white-space: nowrap; }
.table .top td { vertical-align: top; }
.table td.empty { padding: 28px 12px; text-align: center; color: var(--muted); }
.table th.sortable { cursor: pointer; user-select: none; }
.table th.sortable:hover, .table th.sorted { color: var(--accent-ink); }
.table .sub-row td { background: var(--panel-2); padding: 8px 12px 10px 44px; }
.row-link { color: var(--ink); font-weight: 600; }
.row-link:hover { color: var(--accent-ink); }

/* Key/value lists (detail pages, cost, ingest) */
.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 7px 22px; font-size: 13.5px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* ---------- Badges & status ---------- */

.badge {
    display: inline-flex; align-items: center; gap: 4px;
    font-size: 12px; font-weight: 500; line-height: 18px;
    padding: 0 7px; border-radius: 999px;
    background: var(--line-2); color: var(--ink-2); white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.solid-danger { background: var(--danger); color: #fff; font-weight: 600; }
.badge.accent { background: var(--accent-soft); color: var(--accent-ink); }
.badge.locale { font-family: var(--mono); font-size: 11.5px; background: none; border: 1px solid var(--line); color: var(--muted); }
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; background: var(--faint); flex: none; }
.dot.ok { background: var(--ok); }
.dot.warn { background: #d08a12; }
.dot.danger { background: var(--danger); }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12.5px; }
.ok-text { color: var(--ok); }
.warn-text { color: var(--warn); }
.danger-text { color: var(--danger); }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- Callouts ---------- */

.callout {
    display: flex; gap: 12px; align-items: flex-start; justify-content: space-between;
    padding: 10px 14px; border-radius: var(--radius); font-size: 13.5px;
    border: 1px solid var(--line); background: var(--panel); margin-bottom: 16px;
}
.callout.warn { background: var(--warn-soft); border-color: var(--warn-line); color: #5f3a06; }
.callout.danger { background: var(--danger-soft); border-color: var(--danger-line); color: #7a1a12; }
.callout.ok { background: var(--ok-soft); border-color: #c4e3d0; color: #154f32; }
.callout .quote { margin-top: 4px; white-space: pre-wrap; font-style: italic; }

/* ---------- Avatars ---------- */

.avatar {
    width: 32px; height: 32px; border-radius: 50%; object-fit: cover; flex: none;
    background: var(--accent-soft); color: var(--accent-ink);
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 13px; font-weight: 600; overflow: hidden;
}
.avatar.lg { width: 52px; height: 52px; font-size: 19px; }
.who-cell { display: flex; align-items: center; gap: 10px; min-width: 0; }
.who-cell .name { display: flex; align-items: center; gap: 6px; }
.who-cell .meta { font-size: 12.5px; color: var(--muted); }

/* Hover preview for the character list */
.peek { position: relative; display: inline-block; }
.peek .peek-img {
    display: none; position: absolute; left: 0; top: calc(100% + 6px); z-index: 30;
    width: 200px; height: 260px; object-fit: cover; border-radius: var(--radius);
    border: 1px solid var(--line); box-shadow: 0 12px 32px rgba(28, 33, 48, .2); background: var(--panel);
}
.peek:hover .peek-img { display: block; }

/* ---------- Stats (dashboard) ---------- */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 0; }
.stat { padding: 16px 18px; border-right: 1px solid var(--line-2); text-decoration: none; color: var(--ink); display: block; }
.stat:last-child { border-right: 0; }
a.stat:hover { background: var(--panel-2); text-decoration: none; color: var(--ink); }
.stat .label { font-size: 13px; color: var(--muted); }
.stat .value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat .note { font-size: 12.5px; color: var(--muted); }
.stat.alert .value { color: var(--danger); }

.cols-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 18px; }

/* ---------- Conversation: transcript ---------- */

.conv-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 24px; align-items: start; }
/* Columns sit side by side: the stacked-panel gap (.panel + .panel) must not push the second one down. */
.conv-layout > .panel, .conv-layout > aside { margin-top: 0; }
.conv-layout > aside { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; }

.chat { display: flex; flex-direction: column; height: calc(100vh - 190px); min-height: 420px; overflow: hidden; }
/* Beside a chat window, memory is as tall as its content: no cap, no inner scroll. */
.conv-layout:has(.chat) > aside { position: static; max-height: none; overflow: visible; }
.chat .transcript { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.transcript { display: flex; flex-direction: column; gap: 14px; padding: 18px 20px; }
.older { align-self: center; margin-bottom: 4px; }
.turn { display: flex; flex-direction: column; max-width: 78%; align-self: flex-start; }
.turn.him { align-self: flex-end; align-items: flex-end; }
.turn .bubble {
    padding: 9px 13px; border-radius: 14px 14px 14px 4px; white-space: pre-wrap; overflow-wrap: anywhere;
    background: var(--accent-soft); border: 1px solid var(--accent-line); color: var(--ink); font-size: 14px;
}
.turn.him .bubble { background: var(--panel); border-color: var(--line); border-radius: 14px 14px 4px 14px; }
.turn.anchored .bubble { box-shadow: 0 0 0 2px var(--accent); }
.turn .photo-drop { display: inline-flex; flex-direction: column; gap: 4px; margin-top: 6px; text-decoration: none; }
.turn .photo-drop img { max-width: 160px; max-height: 220px; border-radius: 10px; border: 1px solid var(--line); object-fit: cover; }
.turn .photo-drop span { font-size: 12px; }
.turn .stamp { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin-bottom: 3px; padding: 0 4px; }
.turn .stamp strong { color: var(--ink-2); font-weight: 600; }
.turn .tools { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; margin-top: 4px; padding: 0 4px; }
.turn .tools .btn-link, .turn .tools summary { font-size: 12.5px; }
.turn details summary { cursor: pointer; color: var(--muted); list-style: none; }
.turn details summary::-webkit-details-marker { display: none; }
.turn details[open] summary { color: var(--ink); }
.turn details pre { margin-top: 6px; font-size: 11.5px; max-width: 640px; }
.turn .inspect {
    margin-top: 8px; width: min(720px, 100%); background: var(--panel);
    border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px;
}
.inspect .lbl { font-size: 12.5px; color: var(--muted); margin: 0 0 4px; }
.inspect .lbl + pre, .inspect .lines { margin-bottom: 10px; }
.inspect .lines div { font-size: 13px; white-space: pre-wrap; margin-bottom: 3px; }
.system-turn { align-self: center; font-size: 12.5px; color: var(--muted); }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 14px 16px; border-top: 1px solid var(--line-2); flex-wrap: wrap; }
.composer .textarea { flex: 1 1 280px; }

/* ---------- Conversation: memory sidebar ---------- */

.memory { padding: 4px 16px 16px; font-size: 13.5px; }
.memory .block { padding: 14px 0 12px; border-bottom: 1px solid var(--line-2); }
.memory .block:last-child { border-bottom: 0; }
.memory .block-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.memory .block-head h3 { margin: 0; }
.memory .block-head .n { font-size: 12px; color: var(--faint); }
.memory ul { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.memory li { display: flex; gap: 8px; align-items: baseline; justify-content: space-between; }
.memory .none { color: var(--faint); font-size: 13px; }
.fact .scope { font-size: 11.5px; color: var(--muted); margin-right: 4px; }
.fact .conf { font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums; }
.fact-edit { display: flex; gap: 6px; align-items: center; margin-top: 5px; }
.inline-form { display: flex; gap: 6px; margin-top: 8px; }
.inline-form .input { flex: 1; }

/* ---------- Pagination ---------- */

.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--line-2); font-size: 13px; color: var(--muted); }
.pager .pages { display: flex; gap: 4px; flex-wrap: wrap; }
.pager .pg {
    min-width: 30px; padding: 3px 9px; border: 1px solid var(--line); background: var(--panel);
    border-radius: var(--radius-sm); font: inherit; font-size: 13px; line-height: 20px;
    text-align: center; color: var(--ink-2); cursor: pointer;
}
.pager .pg:hover { background: var(--panel-2); }
.pager .pg.cur { background: var(--accent); border-color: var(--accent); color: #fff; cursor: default; }
.pager .pg.off { color: var(--faint); cursor: default; background: var(--panel); }

/* ---------- Docs page ---------- */

.doc { max-width: 920px; font-size: 14px; line-height: 1.6; }
.doc h2 { font-size: 17px; margin: 34px 0 10px; padding-top: 18px; border-top: 1px solid var(--line); }
.doc h3 { font-size: 14px; margin: 20px 0 6px; font-family: var(--mono); font-weight: 500; }
.doc pre { margin: 6px 0 12px; }
.doc table { border-collapse: collapse; width: 100%; margin: 6px 0 12px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.doc th, .doc td { text-align: left; vertical-align: top; border-bottom: 1px solid var(--line-2); padding: 7px 10px; }
.doc th { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.doc .toc { display: flex; gap: 6px 16px; flex-wrap: wrap; margin: 12px 0 4px; }
.doc .note { color: var(--muted); }

/* ---------- Responsive ---------- */

@media (max-width: 1100px) {
    .conv-layout { grid-template-columns: minmax(0, 1fr); }
    .conv-layout > aside { position: static; max-height: none; }
    .chat { height: 75vh; }
    .cols-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
    .shell { grid-template-columns: minmax(0, 1fr); }
    .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; padding: 10px 12px; gap: 4px; border-right: 0; border-bottom: 1px solid var(--line); }
    .side .brand { padding: 4px 8px; width: 100%; }
    .side .group { display: none; }
    .side .foot { margin: 0; padding: 0; border: 0; display: flex; align-items: center; gap: 8px; }
    .side .who { display: none; }
    main { padding: 16px; }
    .turn { max-width: 92%; }
    .stat { border-right: 0; border-bottom: 1px solid var(--line-2); }
}
@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; }
}
