:root {
  --bg: #ffffff; --panel: #f6f7f9; --border: #e3e5e8; --text: #1f2328; --muted: #656d76;
  --accent: #2563eb; --accent-fg: #ffffff; --ok: #16a34a; --err: #dc2626; --code-bg: #f1f3f5;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #16181c; --panel: #1d2025; --border: #2d3139; --text: #e6e8eb; --muted: #9aa1ab;
          --accent: #60a5fa; --accent-fg: #0b1220; --ok: #4ade80; --err: #f87171; --code-bg: #23272e; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
             font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
body { display: flex; }
#sidebar { width: 420px; min-width: 320px; display: flex; flex-direction: column; border-right: 1px solid var(--border);
           background: var(--panel); }
main { flex: 1; position: relative; }
.map-frame { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; visibility: hidden; }
.map-frame.active { visibility: visible; }
header { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 10px 12px 10px 16px;
         border-bottom: 1px solid var(--border); }
/* thread rail */
#rail { width: 264px; flex: none; display: flex; flex-direction: column; background: var(--rail, var(--panel));
        border-right: 1px solid var(--border); min-height: 0; }
body.rail-collapsed #rail { display: none; }
#rail-open { display: none; }
body.rail-collapsed #rail-open { display: inline-flex; }
.rail-top { display: flex; align-items: center; justify-content: space-between; padding: 12px 10px 8px 16px; }
.brand { font-weight: 650; font-size: 13.5px; letter-spacing: -.01em; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; padding: 0;
            border: 0; background: none; color: var(--muted); border-radius: 6px; }
.icon-btn:hover { background: var(--bg); color: var(--text); }
.new-thread { margin: 4px 12px 8px; display: flex; align-items: center; gap: 8px; justify-content: center; padding: 7px 10px;
              background: var(--accent); color: var(--accent-fg); border: 0; font-weight: 600; }
.new-thread:hover { filter: brightness(1.05); }
.search { margin: 0 12px 6px; display: flex; align-items: center; gap: 6px; padding: 0 8px; border: 1px solid var(--border);
          border-radius: 7px; background: var(--bg); color: var(--muted); }
.search input { flex: 1; min-width: 0; border: 0; background: none; color: var(--text); font: inherit; font-size: 13px; padding: 6px 0; outline: none; }
.search:focus-within { border-color: var(--accent); }
.search kbd { font: 11px ui-monospace, monospace; color: var(--muted); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }
#thread-groups { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.group-label { font-size: 11.5px; font-weight: 600; color: var(--muted); padding: 12px 8px 4px; }
.thread { position: relative; display: flex; gap: 10px; align-items: flex-start; padding: 8px 8px; border-radius: 8px; cursor: pointer;
          outline: none; }
.thread:hover, .thread:focus-visible { background: var(--bg); }
.thread.current { background: var(--bg); box-shadow: inset 3px 0 0 var(--accent); }
.thread .status-icon { flex: none; width: 14px; height: 14px; margin-top: 2px; display: flex; align-items: center; justify-content: center; }
.thread .t-body { flex: 1; min-width: 0; }
.thread .t-title { font-size: 13px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.thread.unread .t-title { font-weight: 600; }
.thread .t-meta { font-size: 11.5px; color: var(--muted); margin-top: 2px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.badge { font-size: 10.5px; font-weight: 600; padding: 0 6px; border-radius: 9px; line-height: 16px; }
.badge.story { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
.badge.warn { background: color-mix(in srgb, var(--err) 14%, transparent); color: var(--err); }
.thread .t-actions { position: absolute; right: 6px; top: 6px; display: none; gap: 2px; background: var(--bg); border-radius: 6px; }
.thread:hover .t-actions, .thread:focus-within .t-actions { display: flex; }
.thread .t-actions .icon-btn { width: 26px; height: 26px; }
.thread .t-actions .icon-btn:disabled { opacity: .35; cursor: not-allowed; }
.thread input.rename { width: 100%; font: inherit; font-size: 13px; padding: 2px 4px; border: 1px solid var(--accent); border-radius: 4px;
                       background: var(--bg); color: var(--text); }
.spinner { width: 12px; height: 12px; border-radius: 50%; border: 2px solid color-mix(in srgb, var(--accent) 25%, transparent);
           border-top-color: var(--accent); animation: spin .8s linear infinite; }
.unread-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.warn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--err); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
.rail-empty { color: var(--muted); font-size: 13px; padding: 16px 10px; line-height: 1.5; }
.rail-foot { padding: 8px 16px 12px; font-size: 11.5px; color: var(--muted); border-top: 1px solid var(--border); }
/* chat header */
.thread-head { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; }
#thread-title { flex: 1; min-width: 0; font: inherit; font-weight: 650; color: var(--text); background: none; border: 1px solid transparent;
                border-radius: 6px; padding: 3px 6px; margin-left: -6px; text-overflow: ellipsis; }
#thread-title:hover { border-color: var(--border); }
#thread-title:focus { border-color: var(--accent); outline: none; background: var(--bg); }
#thread-status { font-size: 12px; color: var(--muted); white-space: nowrap; display: flex; align-items: center; gap: 6px; }
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50; background: var(--text); color: var(--bg);
         padding: 8px 10px 8px 14px; border-radius: 8px; font-size: 13px; display: flex; gap: 12px; align-items: center;
         box-shadow: 0 6px 20px rgba(0,0,0,.2); }
#toast button { background: none; border: 0; color: var(--bg); font-weight: 700; text-decoration: underline; padding: 2px 4px; }
#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 39; }
@media (max-width: 1100px) {
  #rail { position: fixed; left: 0; top: 0; bottom: 0; z-index: 40; box-shadow: 4px 0 24px rgba(0,0,0,.18); }
  body:not(.rail-open) #rail { display: none; }
  #rail-open { display: inline-flex !important; }
  #rail-collapse { display: none; }
}
button { font: inherit; cursor: pointer; border-radius: 6px; border: 1px solid var(--border); background: var(--bg);
         color: var(--text); padding: 4px 10px; }
button:disabled { opacity: .5; cursor: default; }
#logs { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.log { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 12px; }
.log[hidden] { display: none; }
.intro p { color: var(--muted); margin: 0 0 10px; }
.examples { display: flex; flex-direction: column; gap: 6px; }
.examples button { text-align: left; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-fg); padding: 8px 12px;
            border-radius: 12px 12px 2px 12px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant { display: flex; flex-direction: column; gap: 6px; }
.reply p { margin: 0 0 8px; } .reply p:last-child { margin: 0; }
.reply ul, .reply ol { margin: 0 0 8px; padding-left: 20px; }
.reply code { background: var(--code-bg); padding: 1px 4px; border-radius: 4px; font-size: 12.5px; }
details.step { border: 1px solid var(--border); border-radius: 6px; background: var(--bg); font-size: 13px; }
details.step > summary { list-style: none; cursor: pointer; padding: 5px 9px; display: flex; gap: 8px; align-items: baseline; }
details.step > summary::-webkit-details-marker { display: none; }
.step .name { font-weight: 600; white-space: nowrap; }
.step .meta { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.step .status { white-space: nowrap; font-size: 12px; }
.step .status.ok { color: var(--ok); } .step .status.err { color: var(--err); }
.step .status.run::after { content: "…"; }
.step pre { margin: 0; padding: 8px 10px; background: var(--code-bg); border-top: 1px solid var(--border);
            overflow-x: auto; font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap;
            word-break: break-word; max-height: 360px; }
details.thinking { color: var(--muted); font-size: 13px; }
details.thinking > summary { cursor: pointer; }
details.thinking .body { white-space: pre-wrap; padding: 4px 0 0 14px; border-left: 2px solid var(--border); margin-top: 4px; }
.error { color: var(--err); font-size: 13px; }
.notice, .usage { color: var(--muted); font-size: 12px; }
#composer { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
#input { flex: 1; resize: none; font: inherit; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
         background: var(--bg); color: var(--text); }
#send { background: var(--accent); color: var(--accent-fg); border: 0; padding: 0 16px; }
@media (max-width: 760px) {
  body { flex-direction: column-reverse; }
  #sidebar { width: 100%; min-width: 0; height: 50%; border-right: 0; border-top: 1px solid var(--border); }
  main { height: 50%; flex: none; }
}
.reply table { border-collapse: collapse; font-size: 12.5px; margin: 0 0 8px; display: block; overflow-x: auto; }
.reply th, .reply td { border: 1px solid var(--border); padding: 3px 7px; text-align: left; }
.reply th { background: var(--bg); }
.actions { display: flex; gap: 6px; }
#story:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.story-card { border: 1px solid var(--border); border-radius: 8px; background: var(--bg); padding: 10px 12px; font-size: 13px; }
.story-card .title { font-weight: 600; margin-bottom: 4px; }
.story-card .links { display: flex; gap: 10px; margin-top: 6px; }
.story-card a { color: var(--accent); font-weight: 600; text-decoration: none; }
.story-card .muted { color: var(--muted); }
#toast[hidden], #scrim[hidden] { display: none; }
.thread:hover .t-body, .thread:focus-within .t-body { padding-right: 52px; }
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: var(--panel); padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 10px; background: var(--bg);
              border: 1px solid var(--border); border-radius: 12px; padding: 24px; box-shadow: 0 8px 30px rgba(0,0,0,.06); }
.login-card .brand { font-size: 16px; }
.login-card .muted { color: var(--muted); margin: 0 0 4px; font-size: 13px; }
.login-card input[type=password] { font: inherit; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px;
                                   background: var(--bg); color: var(--text); }
.login-card input[type=password]:focus { outline: none; border-color: var(--accent); }
.login-card button { background: var(--accent); color: var(--accent-fg); border: 0; padding: 9px; font-weight: 600; }
.region-switch { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; padding: 2px; margin-bottom: 10px; background: var(--bg); }
.region-switch button { border: 0; background: none; padding: 4px 12px; border-radius: 6px; font-size: 13px; color: var(--muted); }
.region-switch button[aria-checked=true] { background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.examples[hidden] { display: none; }
.badge.region { background: color-mix(in srgb, var(--muted) 16%, transparent); color: var(--muted); }
