:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --line: #dde1e6;
  --accent: #1a5fb4;
  --accent-text: #ffffff;
  --ok: #1e8e3e;
  --ok-bg: #e6f4ea;
  --warn: #b06000;
  --warn-bg: #fef7e0;
  --bad: #c5221f;
  --bad-bg: #fce8e6;
  --me-bubble: #e8f0fe;
  --shadow: 0 8px 24px rgba(20, 30, 50, .12);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101317; --surface: #171b21; --surface-2: #1f242c; --text: #e6e9ee; --muted: #9aa5b4;
    --line: #2c333d; --accent: #7cacf8; --accent-text: #0b1220; --ok: #81c995; --ok-bg: #16301f;
    --warn: #fdd663; --warn-bg: #3a2f10; --bad: #f28b82; --bad-bg: #3b1a18; --me-bubble: #1d2a40;
    --shadow: 0 8px 24px rgba(0, 0, 0, .5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101317; --surface: #171b21; --surface-2: #1f242c; --text: #e6e9ee; --muted: #9aa5b4;
  --line: #2c333d; --accent: #7cacf8; --accent-text: #0b1220; --ok: #81c995; --ok-bg: #16301f;
  --warn: #fdd663; --warn-bg: #3a2f10; --bad: #f28b82; --bad-bg: #3b1a18; --me-bubble: #1d2a40;
  --shadow: 0 8px 24px rgba(0, 0, 0, .5); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  display: flex; flex-direction: column;
}
h1, h2, h3, h4 { margin: 0 0 .4em; font-weight: 600; line-height: 1.25; }
h1 { font-size: 1.5rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1.05rem; } h4 { font-size: .9rem; margin-top: 1.2em; }
.muted { color: var(--muted); } .small { font-size: .85rem; }
[hidden] { display: none !important; }

button, .button, select, input, textarea { font: inherit; color: inherit; }
button, .button {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: .45em .9em; cursor: pointer; text-decoration: none; white-space: nowrap;
}
button:hover, .button:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.ghost, .button.ghost { background: transparent; }
button.danger { color: var(--bad); }
input, select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: .5em .6em; background: var(--surface);
}
textarea { resize: vertical; }
label { display: block; font-size: .85rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: .25em; color: var(--text); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.top {
  display: flex; align-items: center; gap: .6rem; padding: .6rem 16px;
  background: var(--surface); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.brand { font-weight: 600; color: var(--text); text-decoration: none; margin-right: .4rem; display: flex; align-items: center; gap: 8px; }
.brand img { width: 28px; height: 28px; flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text small { font-size: .68rem; font-weight: 500; color: var(--muted); letter-spacing: .02em; }
#picker { width: auto; max-width: 280px; }
.spacer { flex: 1; }
.me { font-size: .8rem; color: var(--muted); }
.me a { color: inherit; }
.pill { font-size: .78rem; padding: .2em .65em; border-radius: 999px; background: var(--surface-2); }
.pill.draft { background: var(--surface-2); }
/* Shimmer placeholders while the top bar's data loads (aria-busy until boot finishes). */
.top[aria-busy="true"] #picker, .top[aria-busy="true"] .me:empty, .pill.skel {
  background: linear-gradient(90deg, var(--surface-2) 30%, var(--line) 50%, var(--surface-2) 70%) 0 0 / 200% 100%;
  animation: shimmer 1.3s linear infinite; color: transparent; border-color: transparent;
}
.top[aria-busy="true"] #picker { min-width: 200px; pointer-events: none; }
.top[aria-busy="true"] .me:empty { display: inline-block; width: 140px; height: 1.1em; border-radius: 6px; }
.pill.skel { display: inline-block; width: 130px; height: 1.6em; }
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  .top[aria-busy="true"] #picker, .top[aria-busy="true"] .me:empty, .pill.skel { animation: none; }
}
.pill.in_review { background: var(--warn-bg); color: var(--warn); }
.pill.approved { background: var(--ok-bg); color: var(--ok); }
.approval { padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.approval-head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.approval-img { max-width: 100%; max-height: 260px; object-fit: contain; border: 1px solid var(--line); border-radius: 8px; background: #fff; justify-self: start; }
.approval-text { margin: 0; white-space: pre-wrap; font: inherit; font-size: .88rem; background: var(--surface-2); border-radius: 8px; padding: .6em .8em; max-height: 220px; overflow: auto; }
.msg .author { font-size: .75rem; color: var(--muted); margin-bottom: 2px; }
.msg.other { background: var(--surface-2); }
h2.all { margin-top: 1.8rem; }
.home .area h3 { margin-top: 1rem; }
.banner { padding: .6rem 16px; background: var(--warn-bg); color: var(--warn); font-size: .9rem; }

.home { padding: 24px 16px; max-width: 980px; width: 100%; margin: 0 auto; }
.home .area { margin-top: 1.6rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.card {
  display: block; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; color: var(--text); text-decoration: none;
}
.card:hover { border-color: var(--accent); }
.card .muted { font-size: .82rem; }

.work { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(320px, 38%) 1fr; }
.chat { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--surface); }
.log { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: .6em .85em; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.agent { background: var(--surface-2); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.me { background: var(--me-bubble); align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.note { align-self: center; background: none; color: var(--muted); font-size: .82rem; text-align: center; }
.activity { padding: 0 16px 6px; font-size: .8rem; color: var(--muted); }
.activity::before { content: ""; display: inline-block; width: .6em; height: .6em; border-radius: 50%; background: var(--accent); margin-right: .5em; animation: pulse 1s infinite alternate; }
@keyframes pulse { from { opacity: .25; } to { opacity: 1; } }
.chips { display: flex; gap: 6px; padding: 0 16px; flex-wrap: wrap; }
.chip { font-size: .8rem; padding: .25em .7em; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.chip:hover { color: var(--text); }
.count.info:not(:empty) { background: var(--surface-2); color: var(--muted); }
ol.decisions { margin: 0 0 1.2em; padding-left: 1.4em; display: grid; gap: 6px; }
ol.decisions li { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: .5em .7em; }
ol.decisions li .muted { color: var(--muted); font-size: .9em; }
#term-form, #decision-form, #step-form { max-width: 560px; margin-top: 1em; }
.composer { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.composer textarea { flex: 1; }
.start { align-self: center; margin: auto; text-align: center; max-width: 340px; }

.board { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.tabs { display: flex; gap: 2px; padding: 8px 16px 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button { border: none; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: .5em .8em; color: var(--muted); }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.count:not(:empty) { background: var(--warn-bg); color: var(--warn); border-radius: 999px; padding: 0 .5em; font-size: .75rem; margin-left: .2em; }
.panel { flex: 1; min-height: 0; overflow: auto; padding: 16px; display: flex; flex-direction: column; }
.maphead { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.zoom { display: flex; gap: 4px; }
.zoom button, .zoom .button { padding: .3em .7em; }
.zoom > span { display: flex; gap: 4px; }
.zoom [hidden] { display: none; }
.seg { margin-left: 6px; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.seg button:last-child { border-radius: 0 var(--radius) var(--radius) 0; margin-left: -1px; }
.seg button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.diagram.live { padding: 0; min-height: 520px; width: 100%; background: #fff; }
.diagram { flex: 1; min-height: 300px; overflow: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.diagram svg { display: block; margin: 0 auto; }
.diagram .node { cursor: pointer; }
.diagram .cluster rect { fill: var(--surface-2) !important; stroke: var(--line) !important; rx: 8px; }
.diagram .cluster .nodeLabel, .diagram .cluster-label { color: var(--muted) !important; font-weight: 600; }
.diagram .node.selected > * { filter: drop-shadow(0 0 4px var(--accent)); }
.legend { margin: .6em 0 0; display: flex; gap: .4em; align-items: center; flex-wrap: wrap; }
.key { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--line); margin-left: .5em; }
.key.approved { background: #e6f4ea; border-color: #1e8e3e; }
.key.comment { background: #fef7e0; border-color: #f9ab00; }
.key.risk { border: 2px solid #d93025; }
.key.sub { border: 1px dashed var(--muted); }

table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th, td { text-align: left; padding: .5em .6em; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
ul.issues { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
ul.issues li { background: var(--surface); border: 1px solid var(--line); border-left: 3px solid var(--warn); border-radius: 8px; padding: .6em .8em; }
ul.issues li.error { border-left-color: var(--bad); }
ul.issues .ask { color: var(--muted); font-size: .85rem; }
dl.gloss dt { font-weight: 600; margin-top: .8em; }
dl.gloss dd { margin: .1em 0 0; }

.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 100%); background: var(--surface);
  border-left: 1px solid var(--line); box-shadow: var(--shadow); display: flex; flex-direction: column; z-index: 20;
}
.drawer-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.drawer-head h3 { margin: 0; }
.drawer-body { flex: 1; overflow-y: auto; padding: 14px 16px 24px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.stack { display: grid; gap: 10px; }
details summary { cursor: pointer; margin: 1.2em 0 .6em; font-weight: 500; }
.comment { background: var(--warn-bg); border-radius: 8px; padding: .5em .7em; margin-bottom: 8px; font-size: .88rem; }
.comment.resolved { background: var(--surface-2); color: var(--muted); }
.comment .outcome { margin-top: .3em; font-size: .85rem; }
.comment .by { font-size: .78rem; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.comment .by button { padding: 0 .5em; font-size: .78rem; }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); width: min(440px, calc(100% - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog.wide { width: min(760px, calc(100% - 32px)); max-height: calc(100vh - 48px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dialog-head h3 { margin: 0; }
button.icon { padding: .1em .55em; font-size: 1.3rem; line-height: 1; }
/* Head and buttons stay put; only the message scrolls, so the actions are always on screen. */
#approval-dialog { overflow: hidden; }
#approval-dialog > .stack { display: flex; flex-direction: column; max-height: calc(100vh - 48px - 2.5em); }
#approval-body { overflow: auto; min-height: 0; flex: 1; }
#approval-dialog .approval-img { max-height: 320px; }
#approval-dialog .approval-text { max-height: none; overflow-wrap: anywhere; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: .6em 1em; border-radius: 8px; font-size: .9rem; z-index: 30; max-width: calc(100% - 32px); }

@media (max-width: 860px) {
  .work { grid-template-columns: 1fr; grid-template-rows: minmax(320px, 45vh) auto; overflow-y: auto; }
  .chat { border-right: none; border-bottom: 1px solid var(--line); }
  .board { min-height: 70vh; }
  #picker { max-width: 100%; flex: 1 1 160px; }
}
