:root {
  --bg: #f4f5f7;
  --card: #fff;
  --text: #1d2330;
  --muted: #6b7280;
  --line: #e3e6eb;
  --accent: #2f6fed;
  --accent-soft: #e8f0fe;
  --ok: #1f9d55;
  --ok-soft: #e6f6ec;
  --warn: #b7791f;
  --warn-soft: #fff6e0;
  --err: #d64545;
  --err-soft: #fdecec;
  --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; color: var(--text); background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; margin: 4px 0; }
h3 { margin: 0 0 12px; font-size: 16px; }
code { background: #f0f2f5; padding: 1px 5px; border-radius: 4px; font-size: 13px; word-break: break-all; }
pre { background: #1e2430; color: #e6e9ef; padding: 10px 12px; border-radius: 8px; overflow-x: auto; font-size: 13px; white-space: pre-wrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.warn { color: var(--warn); }
.inline { display: inline; }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 24px; background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.topbar nav { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 17px; color: var(--text); display: inline-flex; align-items: center; gap: 8px; }
.brand small { color: var(--muted); font-weight: 400; }
.brand.big { font-size: 22px; }
.brand-mark { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 800; }
.container { max-width: 1320px; margin: 0 auto; padding: 20px 24px 40px; }
.guest { min-height: 100vh; display: grid; place-items: center; padding: 16px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.narrow { max-width: 520px; margin-bottom: 20px; }
.auth-card { width: 100%; max-width: 380px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }

label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 6px; margin-top: -6px; }
input:not([type=checkbox]), textarea, select { display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; color: var(--text); background: #fff; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px 4px; margin: 0; min-width: 0; }
legend { font-weight: 600; padding: 0 6px; }

.btn { display: inline-block; padding: 8px 14px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--text); font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: #c7ccd4; text-decoration: none; }
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #255fd1; }
.btn-danger { color: var(--err); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-xs { padding: 2px 8px; font-size: 12px; border-radius: 6px; }
.btn-ghost { border-color: transparent; color: var(--muted); }
button.link { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; padding: 0; }

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; background: var(--ok-soft); color: #14532d; }
.flash-error { background: var(--err-soft); color: #7f1d1d; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; background: #eef0f3; color: var(--muted); }
.badge-admin { background: var(--accent-soft); color: var(--accent); }
.badge-queued { background: #eef0f3; color: #4b5563; }
.badge-running { background: var(--accent-soft); color: var(--accent); }
.badge-needs_input { background: var(--warn-soft); color: var(--warn); }
.badge-done { background: var(--ok-soft); color: var(--ok); }
.badge-failed { background: var(--err-soft); color: var(--err); }
.badge-cancelled { background: #f3f4f6; color: #9ca3af; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.project-card { color: var(--text); display: flex; flex-direction: column; gap: 4px; }
.project-card:hover { border-color: var(--accent); text-decoration: none; }
.project-card-head { display: flex; justify-content: space-between; gap: 8px; align-items: center; }

.table { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; font-size: 14px; vertical-align: middle; }
.table th { background: #fafbfc; font-weight: 600; color: var(--muted); }
.table tr.inactive td { opacity: .5; }
.table .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.pw-form input { display: inline-block; width: 140px; margin: 0 4px 0 0; padding: 4px 8px; font-size: 13px; }

.settings { margin-bottom: 10px; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.settings-actions { margin-top: 14px; display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.settings-extra { display: flex; gap: 8px; margin-bottom: 16px; justify-content: flex-end; }

.workspace { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; height: calc(100vh - 210px); min-height: 480px; }
.chat { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.chat-log { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.chat-empty { margin: auto; max-width: 480px; text-align: center; }
.msg { max-width: 82%; }
.msg-meta { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.msg-body { padding: 10px 14px; border-radius: 12px; background: #f1f3f6; overflow-wrap: anywhere; }
.msg-user { align-self: flex-end; }
.msg-user .msg-meta { text-align: right; }
.msg-user .msg-body { background: var(--accent); color: #fff; }
.msg-user .msg-body code { background: rgba(255,255,255,.2); color: #fff; }
.msg-user .msg-body a { color: #fff; text-decoration: underline; }
.msg-system { align-self: center; max-width: 90%; }
.msg-system .msg-meta { text-align: center; }
.msg-system .msg-body { background: transparent; color: var(--muted); font-size: 13px; padding: 2px 8px; text-align: center; }
.msg-system.msg-kind-error .msg-body { color: var(--err); }
.msg-kind-question .msg-body { background: var(--warn-soft); border: 1px solid #f3dca0; }
.msg-kind-report .msg-body { background: var(--ok-soft); border: 1px solid #bfe5cc; }
.msg-assistant.msg-kind-error .msg-body { background: var(--err-soft); border: 1px solid #f5c2c2; }
.msg-title { font-weight: 700; margin-bottom: 4px; font-size: 13px; text-transform: uppercase; letter-spacing: .02em; }
.msg-kind-question .msg-title { color: var(--warn); }
.msg-kind-report .msg-title { color: var(--ok); }
.msg-kind-error .msg-title { color: var(--err); }
.task-ref { color: inherit; opacity: .8; }
.rep-sec { margin-top: 10px; }
.rep-h { font-weight: 600; font-size: 13px; }
.rep-sec ul { margin: 4px 0 0; padding-left: 18px; }
.rep-sec li { margin: 2px 0; }
.rep-meta { margin-top: 10px; font-size: 12px; color: var(--muted); }
.tag { font-size: 11px; padding: 0 6px; border-radius: 4px; background: rgba(0,0,0,.06); }
.tag-created { background: #d9f2e3; }
.tag-deleted { background: #fbdcdc; }

.chat-status { border-top: 1px solid var(--line); padding: 10px 18px; background: #fafbfc; font-size: 14px; max-height: 190px; overflow-y: auto; }
.status-row { display: flex; align-items: center; gap: 4px 10px; flex-wrap: wrap; }
.status-progress { color: var(--accent); overflow-wrap: anywhere; }
.timer, .cost { font-variant-numeric: tabular-nums; white-space: nowrap; }
.status-row .timer, .status-row .cost { color: var(--muted); font-size: 13px; }
.task-meta { display: inline-flex; gap: 8px; margin-left: auto; }
.status-wait { color: var(--warn); font-weight: 600; }
.status-events { margin: 4px 0 6px 24px; font-size: 13px; line-height: 1.45; color: var(--muted); }
.status-events .ev { padding: 0; border: 0; }
.status-events .ev > div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev-cmd { font: 11.5px/1.4 ui-monospace, Menlo, Consolas, monospace; color: #8a919c; }
.status-events .ev-cmd { padding-left: 1.6em; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.chat-form { display: flex; gap: 10px; padding: 12px; border-top: 1px solid var(--line); align-items: flex-end; }
.chat-form textarea { margin: 0; }
.chat-side { display: flex; flex-direction: column; gap: 6px; width: 190px; flex-shrink: 0; }
.chat-side select { margin: 0; padding: 5px 8px; font-size: 13px; }
.task-model { color: var(--muted); }
.project-total { margin: -4px 0 10px; padding: 8px 10px; border-radius: 8px; background: #f6f7f9; color: var(--muted); }
.project-total b { color: var(--text); }
.tokens { cursor: help; }

.tasks { overflow-y: auto; }
.task-list { display: flex; flex-direction: column; gap: 8px; }
.task { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.task-running { border-color: #b9cdf8; }
.task-needs_input { border-color: #f3dca0; }
.task-top { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.task-title { margin: 4px 0; font-size: 14px; overflow-wrap: anywhere; }
.task-progress { color: var(--accent); margin-bottom: 4px; overflow-wrap: anywhere; }
.task-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

.events-dialog { width: min(900px, 94vw); max-height: 80vh; border: 1px solid var(--line); border-radius: var(--radius); padding: 0; }
.events-dialog::backdrop { background: rgba(0,0,0,.3); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: #fff; }
.dlg-body { padding: 12px 16px; font-size: 13.5px; line-height: 1.5; }
.ev { padding: 3px 0; border-bottom: 1px dashed #f0f1f3; white-space: pre-wrap; overflow-wrap: anywhere; }
.dlg-body .ev { display: flex; gap: 10px; }
.dlg-body .ev > div { min-width: 0; flex: 1; }
.ev-time { flex: none; font-variant-numeric: tabular-nums; }
.dlg-body .ev-cmd { margin-top: 2px; padding: 4px 8px; background: #f6f7f9; border-radius: 6px; max-height: 12em; overflow: auto; }

@media (max-width: 900px) {
  .container { padding: 14px 16px 30px; }
  .workspace { grid-template-columns: 1fr; height: auto; }
  .chat { height: 70vh; }
  .msg { max-width: 95%; }
  .topbar { padding: 10px 16px; }
}
@media (max-width: 560px) {
  .chat-form { flex-direction: column; align-items: stretch; }
  .chat-side { width: auto; flex-direction: row; flex-wrap: wrap; }
  .chat-side select { flex: 1 1 140px; width: auto; }
}
