:root {
  --bg: #f6f2ec;
  --surface: #fffdf9;
  --surface-2: #efe8dd;
  --line: #e2d8c9;
  --text: #2a2622;
  --muted: #7a7065;
  --accent: #243b33;
  --accent-ink: #f4ecdd;
  --accent-soft: #dfe8e2;
  --gold: #b0894f;
  --danger: #b4483c;
  --shadow: 0 1px 2px rgba(40, 30, 20, 0.06), 0 6px 20px rgba(40, 30, 20, 0.06);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #171614;
    --surface: #201f1c;
    --surface-2: #2a2824;
    --line: #37342e;
    --text: #ece6dc;
    --muted: #a39a8d;
    --accent: #8fb5a3;
    --accent-ink: #13201b;
    --accent-soft: #24332d;
    --gold: #d0a868;
    --danger: #e07d70;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 20px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 Inter, system-ui, sans-serif;
}
[hidden] { display: none !important; }
h1, h2, h3 { font-family: 'Playfair Display', Georgia, serif; font-weight: 600; margin: 0; }
.muted { color: var(--muted); }
.error { color: var(--danger); margin: 0; font-size: 14px; }
.spacer { flex: 1; }
.link { color: var(--accent); text-decoration: none; font-size: 14px; }

input, select, textarea {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn {
  font: inherit; font-weight: 500;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }

.brand-mark {
  display: inline-grid; place-items: center;
  width: 48px; height: 48px; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink);
  font: 600 26px 'Playfair Display', Georgia, serif;
}
.brand-mark.sm { width: 28px; height: 28px; font-size: 16px; border-radius: 7px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: 32px; box-shadow: var(--shadow);
  display: grid; gap: 14px; text-align: center; justify-items: center;
}
.login-card input, .login-card button { width: 100%; }

/* Layout */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.tabs { display: flex; gap: 4px; margin-left: 12px; }
.tab {
  font: inherit; font-weight: 500; background: none; border: 0; color: var(--muted);
  padding: 7px 12px; border-radius: 8px; cursor: pointer;
}
.tab.active { background: var(--accent-soft); color: var(--accent); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.agent-status { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.agent-status i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.agent-status.ok i { background: #3f9a6d; }
.agent-status.down i { background: var(--danger); }
.view { flex: 1; min-height: 0; }

/* Chat */
.chat-view { display: flex; }
.sidebar {
  width: 290px; flex-shrink: 0; border-right: 1px solid var(--line);
  padding: 14px; display: flex; flex-direction: column; gap: 10px; overflow: hidden;
  background: var(--surface);
}
.search { width: 100%; }
.topic-list, .search-results { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.topic {
  text-align: left; font: inherit; background: none; border: 0; border-radius: 8px;
  padding: 8px 10px; cursor: pointer; color: var(--text);
  display: flex; flex-direction: column; gap: 1px;
}
.topic:hover { background: var(--surface-2); }
.topic.active { background: var(--accent-soft); }
.topic .t { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topic .m { font-size: 12px; color: var(--muted); }
.topic .live { color: var(--gold); }
.search-hit .s { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.search-hit mark { background: color-mix(in srgb, var(--gold) 35%, transparent); color: inherit; }
.archived-toggle { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }

.chat-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--line); }
.topic-title { font-size: 19px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head-actions { display: flex; gap: 6px; }
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 860px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 6px; }
.msg .who { font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; }
.msg .badge { background: var(--surface-2); border-radius: 99px; padding: 1px 8px; font-size: 11px; }
.msg.user .bubble { background: var(--accent-soft); align-self: flex-end; max-width: 85%; white-space: pre-wrap; }
.msg.user .who { justify-content: flex-end; }
.bubble { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; overflow-wrap: anywhere; }
.msg.error .bubble { border-color: var(--danger); }
.msg-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.typing::after { content: '▍'; animation: blink 1s steps(1) infinite; color: var(--gold); }
@keyframes blink { 50% { opacity: 0; } }

.empty-state { margin: auto; max-width: 620px; text-align: center; display: grid; gap: 10px; }
.starters { display: grid; gap: 8px; margin-top: 8px; }
.starter {
  font: inherit; text-align: left; background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 14px; cursor: pointer; color: var(--text);
}
.starter:hover { border-color: var(--accent); }

.composer { display: flex; gap: 10px; padding: 12px 20px 16px; border-top: 1px solid var(--line); background: var(--surface); }
.composer textarea { flex: 1; resize: none; max-height: 220px; }

/* Markdown */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1em 0 0.4em; line-height: 1.3; }
.md h1 { font-size: 22px; } .md h2 { font-size: 19px; } .md h3 { font-size: 17px; } .md h4 { font-size: 15px; font-family: inherit; }
.md p { margin: 0.5em 0; }
.md ul, .md ol { padding-left: 1.4em; margin: 0.5em 0; }
.md table { border-collapse: collapse; width: 100%; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 6px 9px; text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); }
.md code { background: var(--surface-2); border-radius: 4px; padding: 1px 5px; font-size: 13px; }
.md pre { background: var(--surface-2); padding: 12px; border-radius: 8px; overflow-x: auto; }
.md pre code { background: none; padding: 0; }
.md blockquote { border-left: 3px solid var(--gold); margin: 0.5em 0; padding-left: 12px; color: var(--muted); }
.md a { color: var(--accent); }
.md .kartu-head { border-top: 1px dashed var(--line); padding-top: 12px; color: var(--gold); }

/* Board */
.board-view, .schedules-view { display: flex; flex-direction: column; padding: 16px 20px; gap: 12px; overflow: auto; }
.board-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.board-toolbar h2 { font-size: 22px; margin-right: auto; }
.board { display: grid; grid-template-columns: repeat(5, minmax(230px, 1fr)); gap: 12px; flex: 1; min-height: 0; overflow-x: auto; }
.column { background: var(--surface-2); border-radius: var(--radius); padding: 10px; display: flex; flex-direction: column; gap: 8px; min-height: 200px; }
.column.drop { outline: 2px dashed var(--accent); }
.column-head { display: flex; justify-content: space-between; font-weight: 600; font-size: 14px; padding: 2px 4px; }
.column-head .n { color: var(--muted); font-weight: 400; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; cursor: grab; display: grid; gap: 6px; box-shadow: var(--shadow);
}
.card:active { cursor: grabbing; }
.card .ct { font-weight: 500; line-height: 1.35; }
.card .cx { font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .meta { display: flex; gap: 6px; flex-wrap: wrap; font-size: 12px; }
.chip { background: var(--accent-soft); color: var(--accent); border-radius: 99px; padding: 1px 8px; }
.chip.date { background: var(--surface-2); color: var(--muted); }
.chip.late { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }

/* Schedules */
.presets { display: flex; gap: 8px; flex-wrap: wrap; }
.schedule-list { display: grid; gap: 10px; }
.schedule {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; display: grid; grid-template-columns: 1fr auto; gap: 6px 16px; align-items: start;
}
.schedule h3 { font-size: 17px; }
.schedule .when { font-size: 14px; color: var(--accent); font-weight: 500; }
.schedule .prompt { font-size: 14px; color: var(--muted); grid-column: 1 / -1; white-space: pre-wrap; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.schedule .info { font-size: 13px; color: var(--muted); grid-column: 1 / -1; display: flex; gap: 14px; flex-wrap: wrap; }
.schedule .actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.schedule.off { opacity: 0.6; }

/* Tool activity under a streaming reply */
.tool-status { font-size: 13px; color: var(--gold); display: flex; gap: 6px; align-items: center; }
.tool-status::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: blink 1s steps(1) infinite; }

/* Memory */
.memory-view { display: flex; flex-direction: column; padding: 16px 20px; gap: 12px; overflow: auto; }
.memory-view > p { max-width: 900px; margin: 0; }
.memory-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
.memory-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 10px; align-content: start; }
.memory-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.memory-head h3 { font-size: 17px; }
.memory-head span { font-size: 12px; }
.meter { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.meter i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: inherit; }
.meter i.high { background: var(--gold); }
.memory-list { margin: 0; padding-left: 1.3em; display: grid; gap: 8px; font-size: 14px; }
.memory-list li { white-space: pre-wrap; overflow-wrap: anywhere; }
.memory-list .empty { list-style: none; margin-left: -1.3em; color: var(--muted); }
.memory-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Dialogs */
.dialog {
  width: min(760px, calc(100% - 32px)); max-height: calc(100% - 32px);
  border: 1px solid var(--line); border-radius: 16px; padding: 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(20, 15, 10, 0.45); }
.dialog form { padding: 18px 20px; display: grid; gap: 12px; }
.dialog-head { display: flex; gap: 10px; align-items: center; }
.dialog-head h3 { flex: 1; }
.title-input { flex: 1; font: 600 18px 'Playfair Display', Georgia, serif; border-color: transparent; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; }
.field-row label, label.stack { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.field-row label { flex: 1; min-width: 140px; }
label.stack textarea, label.stack input { color: var(--text); font-size: 15px; }
label.check { display: flex; gap: 8px; align-items: center; font-size: 14px; }
.body-tabs { display: flex; gap: 6px; align-items: center; border-bottom: 1px solid var(--line); padding-bottom: 8px; }
.body-tab { font: inherit; font-size: 13px; background: none; border: 0; padding: 4px 10px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.body-tab.active { background: var(--accent-soft); color: var(--accent); }
.body-tabs .link { margin-left: auto; }
.card-preview { max-height: 50vh; overflow-y: auto; }
.card-body { min-height: 45vh; font: 14px/1.5 ui-monospace, Consolas, monospace; resize: vertical; }
.dialog-actions { display: flex; gap: 8px; align-items: center; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px;
  box-shadow: var(--shadow); font-size: 14px; z-index: 50;
}

.mobile-only { display: none; }
@media (max-width: 820px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  .tabs { order: 3; width: 100%; margin: 0; overflow-x: auto; }
  .brand span:last-child { display: none; }
  .mobile-only { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(300px, 86vw); transform: translateX(-100%); transition: transform 0.2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .chat-head, .messages, .composer, .board-view, .schedules-view, .memory-view { padding-left: 16px; padding-right: 16px; }
  .chat-head { flex-wrap: wrap; row-gap: 6px; }
  .topic-title { flex-basis: calc(100% - 52px); font-size: 17px; }
  .chat-head-actions { width: 100%; }
  .chat-head-actions .btn { padding: 4px 9px; }
  .board { grid-template-columns: repeat(5, 82vw); scroll-snap-type: x mandatory; }
  .column { scroll-snap-align: start; }
  .schedule { grid-template-columns: 1fr; }
  .schedule .actions { justify-content: flex-start; }
}
