:root {
  --bg: #f6f4ef;
  --panel: #fffefb;
  --ink: #1d2321;
  --muted: #6b726c;
  --line: #e2ded4;
  --accent: #0f6b5c;
  --accent-soft: #e3f0ec;
  --warn: #9a5b16;
  --warn-soft: #f8ecd9;
  --danger: #a33b32;
  --danger-soft: #f8e4e0;
  --shadow: 0 8px 28px rgba(29, 35, 33, .08);
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

.login-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 80% -10%, #e7efe9 0%, transparent 60%),
    radial-gradient(900px 500px at -10% 110%, #f0e7d8 0%, transparent 55%),
    var(--bg);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 32px 26px 24px;
  box-shadow: var(--shadow);
}
.login-mark, .brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 13px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 20px;
}
.login-card h1 { margin: 18px 0 4px; font-size: 26px; letter-spacing: .5px; }
.login-sub { margin: 0 0 24px; color: var(--muted); font-size: 14px; }
.login-form { display: grid; gap: 10px; }
.login-form label { font-size: 13px; color: var(--muted); }
.login-form input {
  width: 100%;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #faf9f5;
  outline: none;
}
.login-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.login-form button {
  margin-top: 6px;
  padding: 13px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  min-height: 48px;
}
.login-form button:disabled { opacity: .55; }
.error { color: var(--danger); font-size: 13px; min-height: 18px; }
.login-url { margin-top: 18px; font-size: 12px; color: var(--muted); word-break: break-all; text-align: center; }

.app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: rgba(246, 244, 239, .92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand-mark { width: 32px; height: 32px; border-radius: 10px; font-size: 16px; }
.brand-text { font-weight: 700; font-size: 15px; white-space: nowrap; }
.tabs {
  margin-left: auto;
  display: flex;
  background: #eae6dc;
  border-radius: 999px;
  padding: 3px;
}
.tab {
  border: 0;
  background: transparent;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 14px;
  color: var(--muted);
  min-height: 40px;
}
.tab.active { background: var(--panel); color: var(--ink); box-shadow: 0 1px 4px rgba(0,0,0,.08); }
.ghost-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  padding: 8px;
  min-height: 40px;
}

.view { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 18px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.msg { max-width: 86%; display: flex; flex-direction: column; gap: 4px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.bubble {
  padding: 11px 14px;
  border-radius: 16px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.msg-meta { font-size: 11px; color: var(--muted); padding: 0 4px; }
.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  max-width: 280px;
}
.empty strong { display: block; color: var(--ink); font-size: 17px; margin-bottom: 6px; }
.typing { display: inline-flex; gap: 4px; align-items: center; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--panel);
  outline: none;
  line-height: 1.4;
}
.composer textarea:focus { border-color: var(--accent); }
.round-btn {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--accent);
  font-size: 24px;
  line-height: 1;
}
.send-btn {
  flex: 0 0 auto;
  height: 44px;
  padding: 0 18px;
  border: 0;
  border-radius: 22px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}
.send-btn:disabled { opacity: .5; }

.board {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 14px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 22px;
  -webkit-overflow-scrolling: touch;
}
.ledger-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 8px;
}
.ledger-head h2 { margin: 0; font-size: 17px; }
.ledger-head .count { color: var(--muted); font-size: 13px; }
.ledger-head .desc { margin-left: auto; color: var(--muted); font-size: 12px; }
.cards { display: flex; flex-direction: column; gap: 8px; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px 14px;
}
.card-top { display: flex; align-items: flex-start; gap: 8px; }
.card-title { font-weight: 650; font-size: 15px; flex: 1; min-width: 0; word-break: break-word; }
.badge {
  flex: 0 0 auto;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #eeeae1;
  color: var(--muted);
  white-space: nowrap;
}
.badge.ok { background: var(--accent-soft); color: var(--accent); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.why {
  margin: 8px 0 0;
  font-size: 14px;
  color: var(--ink);
}
.why.unset { color: var(--warn); }
.meta { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 8px;
  background: #f1eee6;
  color: var(--muted);
  max-width: 100%;
  overflow-wrap: anywhere;
}
.chip.cog { background: var(--accent-soft); color: #0b5548; }
.chip.unsure { background: var(--warn-soft); color: var(--warn); }
.card details { margin-top: 10px; border-top: 1px dashed var(--line); padding-top: 8px; }
.card summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.detail-body { margin-top: 8px; font-size: 13px; color: #3f4743; white-space: pre-wrap; overflow-wrap: anywhere; }
.kv { display: grid; grid-template-columns: 76px 1fr; gap: 4px 8px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.subtask {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: #f7f5ef;
  font-size: 13px;
}
.subtask .st { color: var(--muted); font-size: 12px; }
.empty-line { color: var(--muted); font-size: 13px; padding: 4px 2px; }

.queue-panel {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
}
.queue-head { display: flex; align-items: center; gap: 10px; }
.queue-head h2 { margin: 0; font-size: 19px; }
.queue-hint { color: var(--muted); font-size: 13px; margin: 8px 0 18px; }
.queue-hint code { background: #eeeae1; padding: 1px 5px; border-radius: 5px; font-size: 12px; }
.progress {
  height: 10px;
  background: #e9e5db;
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 12px;
}
.progress > div {
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 999px;
  transition: width .3s ease;
}
.queue-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 10px;
}
.queue-stats strong { color: var(--ink); font-size: 15px; }
.queue-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 8px; overflow-wrap: anywhere; }
.queue-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 14px; }
.queue-actions button {
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--panel);
  color: var(--ink);
}
.queue-actions button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.queue-actions button:disabled { opacity: .45; }
.queue-actions .ghost-btn { border: 0; background: transparent; color: var(--muted); }
.queue-last {
  font-size: 13px;
  color: var(--muted);
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 12px;
  min-height: 44px;
  overflow-wrap: anywhere;
}
.stream-think {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}
.stream-think .dots { display: inline-flex; gap: 3px; }
.stream-think .dots i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--accent);
  animation: blink 1.2s infinite;
}
.stream-think .dots i:nth-child(2) { animation-delay: .2s; }
.stream-think .dots i:nth-child(3) { animation-delay: .4s; }

.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(20, 24, 22, .38); }
.sheet-panel {
  position: relative;
  width: 100%;
  max-height: 88dvh;
  overflow-y: auto;
  background: var(--panel);
  border-radius: 22px 22px 0 0;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  box-shadow: 0 -10px 40px rgba(0,0,0,.18);
  animation: rise .18s ease-out;
}
@keyframes rise { from { transform: translateY(16px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; gap: 12px; }
.sheet-head h2 { margin: 0; font-size: 18px; }
.sheet-head .ghost-btn { margin-left: auto; }
.sheet-hint { margin: 6px 0 16px; color: var(--muted); font-size: 13px; }
.ingest-form { display: grid; gap: 12px; }
.ingest-form label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.ingest-form input, .ingest-form select, .ingest-form textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #faf9f5;
  outline: none;
}
.ingest-form textarea { resize: vertical; }
.ingest-form input:focus, .ingest-form select:focus, .ingest-form textarea:focus { border-color: var(--accent); }
.ingest-form .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ingest-form button[type="submit"] {
  padding: 13px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  min-height: 48px;
}
.ingest-form button[type="submit"]:disabled { opacity: .55; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(78px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #26302c;
  color: #fff;
  padding: 9px 14px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 40;
  max-width: 90vw;
  text-align: center;
}

@media (min-width: 720px) {
  .messages, .board { padding-left: max(18px, calc((100vw - 680px) / 2)); padding-right: max(18px, calc((100vw - 680px) / 2)); }
  .composer { padding-left: max(18px, calc((100vw - 680px) / 2)); padding-right: max(18px, calc((100vw - 680px) / 2)); }
  .sheet-panel { max-width: 560px; margin: 0 auto; border-radius: 22px; margin-bottom: 24px; }
  .queue-panel { max-width: 680px; margin: 0 auto; width: 100%; }
}
@media (max-width: 380px) {
  .brand-text { display: none; }
  .tab { padding: 8px 10px; }
}