:root {
  --bg: #faf9f6;
  --panel: #ffffff;
  --side: #f3f1ec;
  --text: #1f1e1c;
  --muted: #6e6a62;
  --border: #e4e0d8;
  --accent: #d97757;
  --accent-text: #ffffff;
  --user-bubble: #efe9df;
  --code: #f0ede6;
  --ok: #2f8f5b;
  --warn: #b7791f;
  --err: #c2412d;
  --info: #3b6fd1;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --radius: 14px;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c1b19; --panel: #262522; --side: #22211e; --text: #ecebe7; --muted: #a29e95;
    --border: #3a3834; --user-bubble: #34312c; --code: #2f2d29; --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  --bg: #1c1b19; --panel: #262522; --side: #22211e; --text: #ecebe7; --muted: #a29e95;
  --border: #3a3834; --user-bubble: #34312c; --code: #2f2d29; --shadow: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; }
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.spacer { flex: 1; }
code, pre { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 13px; }

/* buttons */
.btn { border: 1px solid var(--border); background: var(--panel); border-radius: 10px; padding: 8px 14px; cursor: pointer; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 13px; white-space: nowrap; }
.btn.danger { color: var(--err); }
.icon-btn { border: none; background: transparent; width: 36px; height: 36px; border-radius: 10px; cursor: pointer; font-size: 18px; flex: none; }
.icon-btn:hover { background: var(--code); }
.mobile-only { display: none; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: grid; gap: 10px; }
.login-card input { padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); }
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.logo { font-weight: 700; font-size: 20px; display: flex; align-items: center; gap: 8px; }
.logo.small { font-size: 16px; }
.logo-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); color: #fff; display: inline-grid; place-items: center; font-size: 12px; }
.error-text { color: var(--err); min-height: 1.5em; margin: 0; font-size: 14px; }

/* layout */
.app { display: flex; height: 100%; height: 100dvh; }
.sidebar { width: 280px; flex: none; background: var(--side); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px; gap: 8px; }
.job-list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; flex: 1; }
.job { padding: 9px 10px; border-radius: 10px; cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 6px; align-items: center; }
.job:hover { background: var(--code); }
.job.active { background: var(--panel); box-shadow: var(--shadow); }
.job-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.job-meta { grid-column: 1; display: flex; gap: 6px; align-items: center; font-size: 12px; color: var(--muted); min-width: 0; }
.job-meta span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.job-actions { grid-row: 1 / span 2; grid-column: 2; display: flex; gap: 2px; opacity: .55; }
.job:hover .job-actions, .job.active .job-actions { opacity: 1; }
.job-actions button { border: none; background: transparent; cursor: pointer; padding: 4px 6px; border-radius: 6px; font-size: 13px; }
.job-actions button:hover { background: var(--border); }
.disk { padding: 12px 14px 16px; border-top: 1px solid var(--border); }
.disk-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-bottom: 6px; }
.disk-bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.disk-bar span.high { background: var(--err); }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--bg); }
.model-picker select { appearance: none; border: 1px solid var(--border); background: var(--panel) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23888'/%3E%3C/svg%3E") no-repeat right 12px center; padding: 7px 32px 7px 12px; border-radius: 10px; font-weight: 600; cursor: pointer; max-width: 60vw; }

/* badges */
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--code); color: var(--muted); white-space: nowrap; font-weight: 600; }
.badge.queued { color: var(--warn); }
.badge.running { color: var(--info); }
.badge.done { color: var(--ok); }
.badge.failed, .badge.cancelled { color: var(--err); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.queued { background: var(--warn); }
.dot.running { background: var(--info); animation: pulse 1.2s infinite; }
.dot.done { background: var(--ok); }
.dot.failed, .dot.cancelled { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }

/* chat */
.chat { flex: 1; overflow-y: auto; padding: 24px 16px; }
.messages, .results { max-width: 820px; margin: 0 auto; }
.empty { max-width: 560px; margin: 10vh auto 0; text-align: center; }
.empty h1 { font-size: 26px; margin: 8px 0; }
.empty-icon { font-size: 40px; }
.msg { margin: 0 0 18px; display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble { padding: 10px 14px; border-radius: var(--radius); max-width: 85%; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant .bubble, .msg.error .bubble, .msg.system .bubble { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.error .bubble { border-color: color-mix(in srgb, var(--err) 45%, var(--border)); }
.msg.error .bubble::before { content: "⚠ "; color: var(--err); }
.msg-meta { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 8px; align-items: center; }
.model-tag { background: var(--code); border-radius: 6px; padding: 1px 6px; }
.bubble code { background: var(--code); padding: 1px 5px; border-radius: 5px; }

.steps { margin: 0 0 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); max-width: 85%; }
.steps summary { padding: 8px 12px; cursor: pointer; font-size: 13px; color: var(--muted); list-style: none; display: flex; align-items: center; gap: 8px; }
.steps summary::-webkit-details-marker { display: none; }
.steps summary::after { content: "▸"; margin-left: auto; transition: transform .15s; }
.steps[open] summary::after { transform: rotate(90deg); }
.step-list { padding: 0 12px 10px; display: grid; gap: 6px; max-height: 420px; overflow-y: auto; }
.step { font-size: 13px; }
.step.text { color: var(--text); white-space: pre-wrap; }
.step.info { color: var(--muted); font-style: italic; }
.step.tool pre { margin: 0; background: var(--code); padding: 6px 8px; border-radius: 8px; white-space: pre-wrap; word-break: break-all; }
.step.tool .tool-name { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.step.result details summary { padding: 0; font-size: 12px; }
.step.result pre { margin: 4px 0 0; max-height: 200px; overflow: auto; background: var(--code); padding: 6px 8px; border-radius: 8px; white-space: pre-wrap; font-size: 12px; }
.step.result.bad summary { color: var(--err); }
.spinner { width: 14px; height: 14px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { padding: 0 12px 10px; font-size: 12px; color: var(--muted); }
.bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-bottom: 4px; }
.bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.bar.indeterminate span { width: 30%; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { margin-left: -30%; } to { margin-left: 100%; } }
.retry { margin-top: 6px; }

/* results */
.results-title { font-size: 13px; font-weight: 600; color: var(--muted); margin: 8px 0; text-transform: uppercase; letter-spacing: .04em; }
.result-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; margin-bottom: 14px; box-shadow: var(--shadow); }
.result-card video { display: block; width: 100%; max-height: 70vh; background: #000; }
.result-info { display: flex; align-items: center; gap: 10px; padding: 10px 14px; flex-wrap: wrap; }
.result-info .name { font-weight: 600; overflow-wrap: anywhere; flex: 1; min-width: 0; }

/* composer */
.composer-wrap { padding: 8px 16px 14px; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.composer, .uploads, .inputs, .hint { max-width: 820px; margin: 0 auto; }
.composer { display: flex; align-items: flex-end; gap: 6px; background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 6px; box-shadow: var(--shadow); }
.composer:focus-within { border-color: var(--muted); }
.composer textarea { flex: 1; border: none; background: transparent; resize: none; padding: 8px 4px; max-height: 200px; outline: none; }
.send-btn { width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--accent); color: #fff; font-size: 18px; cursor: pointer; flex: none; }
.send-btn:disabled { opacity: .4; cursor: default; }
.hint { min-height: 18px; padding: 4px 6px 0; }
.inputs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--code); border: 1px solid var(--border); border-radius: 99px; padding: 3px 4px 3px 10px; font-size: 13px; max-width: 100%; }
.chip a { color: inherit; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button { border: none; background: transparent; cursor: pointer; border-radius: 50%; width: 20px; height: 20px; line-height: 1; color: var(--muted); }
.chip button:hover { background: var(--border); }
.upload { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; margin-bottom: 8px; font-size: 13px; }
.upload-row { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 4px; }
.upload-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload.failed { border-color: var(--err); }

.drop-overlay { position: fixed; inset: 0; background: color-mix(in srgb, var(--accent) 18%, transparent); backdrop-filter: blur(2px); display: grid; place-items: center; z-index: 50; pointer-events: none; }
.drop-overlay div { border: 3px dashed var(--accent); padding: 40px 60px; border-radius: 24px; font-size: 20px; font-weight: 700; background: var(--panel); }
.toast { position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: calc(100% - 32px); font-size: 14px; }
.scrim { display: none; }

@media (max-width: 800px) {
  .mobile-only { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; width: min(300px, 85vw); }
  .app.menu-open .sidebar { transform: none; }
  .app.menu-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .topbar { padding: 8px 10px; }
  .bubble, .steps { max-width: 100%; }
  .chat { padding: 16px 12px; }
  .composer-wrap { padding-left: 10px; padding-right: 10px; }
}
