* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
  --ink: #10241c; --bg: #eef0f4; --card: #fff; --line: #dde1e8; --mut: #78808f;
  --blue: #2f5aea; --green: #167a3d; --amber: #b45f06; --red: #a33;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
}
html, body { height: 100%; }
body {
  font-family: -apple-system, system-ui, "Segoe UI", sans-serif; margin: 0;
  background: var(--bg); color: #1c1e21; display: flex; flex-direction: column;
  height: 100dvh; overscroll-behavior: none;
}
header.top {
  background: var(--ink); color: #fff; padding: calc(10px + var(--sat)) 16px 10px;
  display: flex; align-items: center; gap: 10px; flex: none;
}
header.top .dot { width: 9px; height: 9px; background: #4ade80; border-radius: 50%; flex: none; }
header.top .dot.busy { background: #fbbf24; animation: pulse 1.1s infinite; }
@keyframes pulse { 50% { opacity: .35; } }
header.top h1 { font-size: 15px; margin: 0; font-weight: 600; }
header.top .sub { opacity: .6; font-size: 11.5px; margin-left: auto; text-align: right; max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
main { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.wrap { max-width: 760px; margin: 0 auto; padding: 0 14px; }
nav.tabs {
  flex: none; display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: var(--sab);
}
nav.tabs a {
  flex: 1; text-align: center; padding: 8px 0 7px; text-decoration: none; color: var(--mut);
  font-size: 10.5px; display: flex; flex-direction: column; gap: 2px; align-items: center;
}
nav.tabs a .ico { font-size: 19px; line-height: 1; position: relative; }
nav.tabs a.active { color: var(--ink); font-weight: 600; }
nav.tabs .badge {
  position: absolute; top: -4px; right: -10px; background: #e02d2d; color: #fff; font-size: 9px;
  min-width: 15px; height: 15px; border-radius: 8px; line-height: 15px; font-weight: 700;
}

/* Chat: #thread must be the one scroller, so the flex chain needs min-height:0
   all the way down or children grow to content and autoscroll dies. */
#chat-page main { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
#thread { flex: 1; min-height: 0; overflow-y: auto; padding: 14px 0 8px; }
.msg { margin: 8px 0; display: flex; }
.msg .bubble {
  max-width: 82%; padding: 9px 13px; border-radius: 14px; font-size: 14px; line-height: 1.45;
  white-space: pre-wrap; box-shadow: 0 1px 2px rgba(0,0,0,.07); word-break: break-word;
}
.gm .bubble { background: var(--card); border-bottom-left-radius: 4px; }
.owner { justify-content: flex-end; } .owner .bubble { background: var(--blue); color: #fff; border-bottom-right-radius: 4px; }
.system { justify-content: center; } .system .bubble { background: transparent; box-shadow: none; color: var(--mut); font-size: 11.5px; text-align: center; }
.briefing .bubble { background: #fdf9ee; border: 1px solid #f0e6c8; font-family: ui-monospace, monospace; font-size: 12px; max-width: 94%; }
.goal-done .bubble { background: #e8f7ee; border: 1px solid #cdeeda; }
.goal-esc .bubble { background: #fdeeee; border: 1px solid #f5d4d4; }
.goal-link { display: inline-block; margin-top: 6px; font-size: 12px; color: var(--green); font-weight: 600; text-decoration: none; }
.approval .bubble { background: #fff7ef; border: 1px solid #f3ddc4; }
.approval.draft .bubble { background: #f4f0ff; border: 1px dashed #c9bcf0; }
.drafttag { display: inline-block; font-size: 10px; font-weight: 700; color: #6b46c1; background: #e9e2fb; border-radius: 6px; padding: 1px 7px; margin-bottom: 5px; letter-spacing: .04em; }
.actions { margin-top: 8px; display: flex; gap: 8px; flex-wrap: wrap; }
button, .btn {
  cursor: pointer; border: none; border-radius: 9px; padding: 8px 14px; font-size: 13px;
  font-family: inherit; background: #e8eaee; color: #222;
}
button.pri { background: var(--green); color: #fff; }
button.sec { background: #e6e6e6; }
button.ghost { background: transparent; color: var(--mut); text-decoration: underline; padding: 8px 4px; }
.editbox { width: 100%; margin-top: 8px; border: 1px solid #cfd5df; border-radius: 9px; padding: 9px 11px; font-size: 13.5px; font-family: inherit; min-height: 64px; }
.reasonbox { width: 100%; margin-top: 8px; border: 1px solid #cfd5df; border-radius: 9px; padding: 8px 11px; font-size: 13px; font-family: inherit; }
.meta { font-size: 10px; color: #9aa1ad; margin-top: 4px; }
.file .bubble a { color: inherit; font-weight: 600; }
.file .bubble img { max-width: 100%; border-radius: 9px; margin-top: 6px; display: block; border: 1px solid var(--line); min-height: 40px; background: #fafbfc; }
#working { display: none; padding: 4px 14px 8px; }
#working.on { display: block; }
#working .pill {
  display: inline-flex; gap: 8px; align-items: center; background: var(--card); border-radius: 999px;
  padding: 6px 14px; font-size: 12px; color: var(--mut); box-shadow: 0 1px 2px rgba(0,0,0,.07);
}
#working .spin { width: 12px; height: 12px; border: 2px solid #cbd2dc; border-top-color: var(--ink); border-radius: 50%; animation: rot .8s linear infinite; }
@keyframes rot { to { transform: rotate(360deg); } }
footer.compose { flex: none; background: var(--card); border-top: 1px solid var(--line); padding: 8px 0 calc(8px + var(--sab)); }
footer.compose .wrap { display: flex; gap: 8px; align-items: flex-end; }
#box {
  flex: 1; border: 1px solid #cfd5df; border-radius: 12px; padding: 10px 13px; font-size: 15px;
  outline: none; font-family: inherit; resize: none; max-height: 110px; min-height: 42px;
}
#send { background: var(--ink); color: #fff; border-radius: 12px; padding: 0 18px; height: 42px; font-size: 14px; flex: none; }
#attach { font-size: 20px; padding: 8px 2px; cursor: pointer; flex: none; }
.dropzone { outline: 3px dashed var(--blue); outline-offset: -12px; }
#upl { display: none; font-size: 11px; color: var(--mut); padding: 2px 14px; }
#upl.on { display: block; }

/* Panels (non-chat pages) */
.panelpage { padding: 14px 0 30px; }
section.card { background: var(--card); border-radius: 12px; padding: 14px 16px; margin: 0 0 12px; box-shadow: 0 1px 2px rgba(0,0,0,.06); }
section.card h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: #556; margin: 0 0 10px; }
.row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid #f0f2f5; font-size: 13.5px; }
.row:first-of-type, h2 + .row { border-top: none; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; }
.row .sub { color: var(--mut); font-size: 11.5px; margin-top: 1px; overflow: hidden; text-overflow: ellipsis; }
.chip { font-size: 10.5px; padding: 2px 9px; border-radius: 999px; background: #e8eaee; white-space: nowrap; }
.chip.done, .chip.executed, .chip.sent_sim, .chip.auto, .chip.ready, .chip.ok { background: #d8f2e0; color: #135c2e; }
.chip.open, .chip.learning { background: #fff1cc; color: #7a5b00; }
.chip.waiting_approval, .chip.pending, .chip.in_progress, .chip.review { background: #ffe1cc; color: #8a3c00; }
.chip.escalated, .chip.rejected, .chip.error, .chip.missing { background: #ffd9d9; color: #8f1d1d; }
.chip.suggest { background: #e9e2fb; color: #5b3fa8; }
.seg { display: inline-flex; background: #e8eaee; border-radius: 9px; padding: 2px; }
.seg button { padding: 5px 10px; font-size: 11.5px; border-radius: 7px; background: transparent; }
.seg button.on { background: var(--ink); color: #fff; }
.bar { height: 5px; background: #e8eaee; border-radius: 3px; overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; background: var(--green); border-radius: 3px; }
.tabbar { display: flex; gap: 6px; margin-bottom: 12px; }
.tabbar button { border-radius: 999px; padding: 7px 15px; font-size: 12.5px; }
.tabbar button.on { background: var(--ink); color: #fff; }
input.txt, select.txt {
  width: 100%; border: 1px solid #cfd5df; border-radius: 9px; padding: 10px 12px; font-size: 14px;
  font-family: inherit; margin: 4px 0 10px; background: #fff;
}
label.lbl { font-size: 11.5px; color: var(--mut); font-weight: 600; }
.note { font-size: 11.5px; color: var(--mut); line-height: 1.5; }
.evimg { max-width: 100%; border-radius: 9px; border: 1px solid var(--line); margin-top: 8px; }
.mono { font-family: ui-monospace, monospace; font-size: 11px; color: #445; word-break: break-all; }
.detail { display: none; padding: 8px 0 2px; }
.detail.on { display: block; }
.empty { color: var(--mut); font-size: 13px; padding: 18px 0; text-align: center; }
.convo { display: flex; flex-direction: column; gap: 6px; }
.convo .m { max-width: 84%; padding: 8px 12px; border-radius: 12px; font-size: 13px; line-height: 1.4; white-space: pre-wrap; }
.convo .out { align-self: flex-end; background: #dcebdd; }
.convo .in { align-self: flex-start; background: #fff; border: 1px solid var(--line); }
.toast {
  position: fixed; bottom: calc(70px + var(--sab)); left: 50%; transform: translateX(-50%);
  background: #222; color: #fff; font-size: 12.5px; border-radius: 9px; padding: 9px 16px;
  opacity: 0; transition: opacity .25s; pointer-events: none; z-index: 40; max-width: 86vw;
}
.toast.on { opacity: .94; }
@media (min-width: 700px) {
  nav.tabs a { flex-direction: row; gap: 7px; font-size: 12.5px; padding: 11px 0; }
  nav.tabs a .ico { font-size: 16px; }
}
