:root {
  --bg: #f6f7f5;
  --panel: #ffffff;
  --ink: #17201a;
  --muted: #5d6b62;
  --line: #dfe5df;
  --accent: #15803d;
  --accent-ink: #ffffff;
  --accent-soft: #e7f5ec;
  --warn: #b45309;
  --danger: #b91c1c;
  --user-bubble: #eef4f0;
  --code-bg: #f0f2ef;
  --head-1: #16a34a;
  --head-2: #14532d;
  --radius: 10px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1411;
    --panel: #161d18;
    --ink: #e5ece7;
    --muted: #9aa89f;
    --line: #26302a;
    --accent: #22c55e;
    --accent-ink: #06240f;
    --accent-soft: #163222;
    --warn: #f59e0b;
    --danger: #f87171;
    --user-bubble: #1c2620;
    --code-bg: #1d2520;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.55 var(--font); }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 7px 12px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 8px 10px; width: 100%;
}
textarea { resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }

header.top {
  background: linear-gradient(90deg, var(--head-1), var(--head-2)); color: #fff;
  padding: 10px 18px; display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
}
header.top .brand { font-weight: 700; letter-spacing: .2px; }
header.top .brand small { font-weight: 400; opacity: .85; margin-left: 6px; }
header.top .spacer { flex: 1; }
header.top .who { font-size: 13px; opacity: .9; }
header.top a { color: #fff; }

.credit { min-width: 220px; }
.credit .label { font-size: 12px; display: flex; justify-content: space-between; gap: 10px; }
.bar { height: 8px; border-radius: 99px; background: rgba(255,255,255,.25); overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; background: #fff; border-radius: 99px; transition: width .4s; }
.bar.low > i { background: #fde68a; }
.bar.out > i { background: #fecaca; }
.bar.dark { background: var(--line); }
.bar.dark > i { background: var(--accent); }
.bar.dark.low > i { background: var(--warn); }
.bar.dark.out > i { background: var(--danger); }

.notice {
  background: var(--accent-soft); border-bottom: 1px solid var(--line); color: var(--ink);
  font-size: 13px; padding: 7px 18px;
}
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.pill { display: inline-block; font-size: 11.5px; border: 1px solid var(--line); border-radius: 99px; padding: 1px 8px; color: var(--muted); }

/* chat layout */
.app { display: grid; grid-template-columns: 290px 1fr; height: calc(100% - 92px); min-height: 0; }
aside.side { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
aside.side .new { padding: 12px; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
aside.side .list { overflow: auto; flex: 1; padding: 6px; }
.conv { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.conv:hover { background: var(--user-bubble); }
.conv.on { background: var(--accent-soft); }
.conv .t { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .m { font-size: 11.5px; color: var(--muted); }
aside.side .foot { border-top: 1px solid var(--line); padding: 10px 12px; display: flex; gap: 8px; }

main.chat { display: flex; flex-direction: column; min-height: 0; }
.thread { flex: 1; overflow: auto; padding: 18px 0; }
.thread .inner { max-width: 820px; margin: 0 auto; padding: 0 20px; }
.msg { margin: 14px 0; }
.msg .who { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.msg.user .body { background: var(--user-bubble); border-radius: var(--radius); padding: 10px 14px; white-space: pre-wrap; }
.msg.assistant .body { padding: 2px 2px; }
.msg .meta { font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.msg.err .body { color: var(--danger); }
.body pre { background: var(--code-bg); padding: 10px 12px; border-radius: 8px; overflow: auto; font: 13px/1.5 var(--mono); }
.body code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; font: 13px var(--mono); }
.body pre code { background: none; padding: 0; }
.body table { border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.body th, .body td { border: 1px solid var(--line); padding: 5px 8px; text-align: left; vertical-align: top; }
.body h1, .body h2, .body h3 { font-size: 16px; margin: 14px 0 6px; }
.body p { margin: 6px 0; }
.body ul, .body ol { margin: 6px 0; padding-left: 22px; }
.typing { color: var(--muted); font-size: 13px; }
.typing::after { content: "…"; animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .2; } }

.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 12px 0; }
.composer .inner { max-width: 820px; margin: 0 auto; padding: 0 20px; display: flex; gap: 10px; align-items: flex-end; }
.composer textarea { min-height: 52px; max-height: 260px; }
.composer .hint { max-width: 820px; margin: 6px auto 0; padding: 0 20px; font-size: 11.5px; color: var(--muted); }

.empty { max-width: 640px; margin: 60px auto; padding: 0 20px; text-align: center; color: var(--muted); }
.empty h2 { color: var(--ink); font-size: 20px; }
.policy { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 18px 0; text-align: left; }
.policy div { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; background: var(--panel); font-size: 13px; }
.policy b { display: block; color: var(--accent); letter-spacing: .5px; }

/* panels, tables (admin and skills) */
.page { max-width: 1180px; margin: 0 auto; padding: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; }
.card h2 { font-size: 16px; margin: 0 0 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.stat .n { font-size: 22px; font-weight: 700; }
.stat .l { font-size: 12px; color: var(--muted); }
table.t { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.t th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; border-bottom: 1px solid var(--line); padding: 6px 8px; }
table.t td { border-bottom: 1px solid var(--line); padding: 7px 8px; vertical-align: middle; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: var(--user-bubble); }
table.t .num, .num { text-align: right; font-variant-numeric: tabular-nums; }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.tabs button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--ink); width: min(760px, 94vw); padding: 18px; }
dialog::backdrop { background: rgba(0,0,0,.35); }
.skill { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin: 8px 0; background: var(--panel); }
.skill pre { white-space: pre-wrap; font: 12.5px/1.5 var(--mono); background: var(--code-bg); padding: 8px 10px; border-radius: 8px; max-height: 260px; overflow: auto; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; height: auto; }
  aside.side { border-right: 0; border-bottom: 1px solid var(--line); max-height: 40vh; }
  main.chat { height: 70vh; }
  .policy, .stats, .grid2 { grid-template-columns: 1fr; }
  .credit { min-width: 0; width: 100%; }
}
