/* ===== Color system (light default, dark via prefers-color-scheme) ===== */
:root {
  color-scheme: light dark;
  --bg: #F7F7F5;
  --surface: #FFFFFF;
  --surface-2: #F1F2EF;
  --ink: #161D1E;
  --ink-soft: #5A6566;          /* 5.9:1 on --bg (AA) */
  --line: rgba(22, 29, 30, .10);
  --accent: #0F6E6E;            /* 6.3:1 on white (AA) */
  --accent-ink: #FFFFFF;
  --accent-soft: #E3F0EF;
  --warn: #8A4F0C;
  --warn-soft: #FBF0E2;
  --danger: #A23535;
  --danger-soft: #F9E8E8;
  --ok: #1E7A45;
  --bar: rgba(247, 247, 245, .82);
  --shadow: 0 1px 2px rgba(16, 24, 24, .04), 0 6px 20px rgba(16, 24, 24, .06);

  --font-ui: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-content: -apple-system, BlinkMacSystemFont, "SF Pro Text", "IBM Plex Sans Arabic", "Segoe UI", Roboto, sans-serif;

  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px;
  --r-sm: 10px; --r-md: 14px; --r-lg: 18px;
  --tabbar-h: 56px;
  --composer-h: 72px;
  --kb: 0px;                     /* keyboard height, set by app.js */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --ease: cubic-bezier(.2, .8, .2, 1);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E1314;
    --surface: #182022;
    --surface-2: #1F2729;
    --ink: #E9EFEF;
    --ink-soft: #A3B0B1;        /* 7.6:1 on --bg */
    --line: rgba(233, 239, 239, .12);
    --accent: #45BDB4;          /* 7.9:1 on --bg */
    --accent-ink: #062022;
    --accent-soft: rgba(69, 189, 180, .14);
    --warn: #F0B062;
    --warn-soft: rgba(240, 176, 98, .14);
    --danger: #F08A8A;
    --danger-soft: rgba(240, 138, 138, .14);
    --ok: #6FD39A;
    --bar: rgba(14, 19, 20, .82);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  }
}

/* ===== Base ===== */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 17px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { -webkit-appearance: none; appearance: none; touch-action: manipulation; cursor: pointer; }
a, label { touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.muted { color: var(--ink-soft); }
.error { color: var(--danger); font-size: 15px; margin: var(--s3) 0 0; }
.noscript { padding: var(--s5); text-align: center; }
.ver { text-align: center; font-size: 12px; color: var(--ink-soft); margin: var(--s5) 0 0; }

.screen { min-height: 100vh; min-height: 100dvh; }

/* ===== Lock ===== */
.lock { display: grid; place-items: center; padding: calc(var(--s5) + var(--safe-t)) calc(var(--s5) + var(--safe-r)) calc(var(--s5) + var(--safe-b)) calc(var(--s5) + var(--safe-l)); }
.lock-card { width: 100%; max-width: 360px; text-align: center; }
.lock-icon { border-radius: 18px; box-shadow: var(--shadow); }
.lock-card h1 { font-size: 28px; font-weight: 700; margin: var(--s4) 0 var(--s1); letter-spacing: -.02em; }
.lock-card form { margin-top: var(--s5); display: grid; gap: var(--s3); }
.lock-card input {
  width: 100%; min-height: 52px; padding: 14px 16px; font-size: 17px; border-radius: var(--r-md);
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  -webkit-appearance: none; appearance: none; outline: none;
}
.lock-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }

/* ===== Buttons ===== */
.btn {
  min-height: 48px; padding: 12px 18px; border-radius: var(--r-md); border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; font-size: 17px;
  transition: transform .15s var(--ease), opacity .15s var(--ease), background-color .2s var(--ease);
}
.btn:active { transform: scale(.97); opacity: .85; }
.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-block { width: 100%; }
.btn-quiet { background: var(--surface-2); border-color: transparent; }
.btn-danger { color: var(--danger); background: transparent; border-color: transparent; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.icon-btn {
  min-width: 44px; min-height: 44px; display: inline-grid; place-items: center; border-radius: 12px;
  border: 0; background: transparent; color: var(--accent);
  transition: background-color .15s var(--ease);
}
.icon-btn:active { background: var(--accent-soft); }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; min-height: 44px; padding: 0 var(--s2); }

/* ===== Navigation bars ===== */
.navbar {
  position: sticky; top: 0; z-index: 10;
  padding: var(--safe-t) calc(var(--s2) + var(--safe-r)) var(--s2) calc(var(--s2) + var(--safe-l));
  background: var(--bar);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: .5px solid var(--line);
}
.navbar-row { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: 44px; }
.navbar-row > :last-child { justify-self: end; }
.navbar-large { border-bottom: 0; }
.large-title { font-size: 34px; font-weight: 700; letter-spacing: -.02em; margin: var(--s1) var(--s2) var(--s2); }
.nav-title { font-size: 17px; font-weight: 600; margin: 0; text-align: center; max-width: 52vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-sub { margin: 0; text-align: center; font-size: 13px; color: var(--ink-soft); min-height: 18px; }
.back-btn {
  justify-self: start; display: inline-flex; align-items: center; gap: 2px; min-height: 44px; padding: 0 var(--s2) 0 0;
  border: 0; background: none; color: var(--accent); font-size: 17px;
}
.back-btn:active { opacity: .6; }

.content {
  width: 100%; max-width: 720px; margin: 0 auto;
  padding: var(--s4) calc(var(--s4) + var(--safe-r)) calc(var(--s5) + var(--safe-b)) calc(var(--s4) + var(--safe-l));
}
#agent .content { padding-bottom: calc(var(--tabbar-h) + var(--composer-h) + var(--s5) + var(--safe-b)); }
#agent.no-composer .content { padding-bottom: calc(var(--tabbar-h) + var(--s5) + var(--safe-b)); }

/* ===== Home: agent cards ===== */
.agent-list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--s3); }
.agent-card {
  width: 100%; display: grid; grid-template-columns: 52px 1fr auto; align-items: center; gap: var(--s3);
  padding: var(--s4); text-align: start; border: 0; border-radius: var(--r-lg);
  background: var(--surface); box-shadow: var(--shadow);
  transition: transform .15s var(--ease), background-color .2s var(--ease);
}
.agent-card:active { transform: scale(.98); background: var(--surface-2); }
.agent-icon { width: 52px; height: 52px; border-radius: 14px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); }
.agent-name { display: block; font-size: 17px; font-weight: 600; }
.agent-desc { display: block; font-size: 15px; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent-meta { display: flex; align-items: center; gap: var(--s2); color: var(--ink-soft); }
.agent-text { min-width: 0; }
.pill { font-size: 13px; font-weight: 600; padding: 3px 9px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); }

/* ===== Workspace thread ===== */
.thread { display: flex; flex-direction: column; gap: var(--s3); }
.msg { max-width: 86%; padding: 10px 14px; border-radius: 20px; animation: rise .2s var(--ease); }
.msg-owner { align-self: flex-end; background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 6px; }
.msg-agent { align-self: flex-start; background: var(--surface); box-shadow: var(--shadow); border-bottom-left-radius: 6px; }
.msg-text { font-family: var(--font-content); white-space: pre-wrap; overflow-wrap: anywhere; unicode-bidi: plaintext; }
.msg-meta { font-size: 12px; margin-top: var(--s1); opacity: .75; }
.msg-file { font-size: 13px; font-weight: 600; margin-bottom: var(--s1); opacity: .9; }
.agent-status {
  display: flex; align-items: center; gap: var(--s3); margin-top: var(--s4); padding: var(--s3) var(--s4);
  border-radius: var(--r-md); background: var(--accent-soft); color: var(--accent); font-size: 15px;
}
.pulse { width: 8px; height: 8px; border-radius: 50%; background: currentColor; flex: none; animation: pulse 1.4s infinite; }

/* ===== Composer (sits above tab bar, rises with the keyboard) ===== */
.composer {
  position: fixed; left: 0; right: 0; z-index: 9;
  bottom: calc(var(--tabbar-h) + var(--safe-b));
  padding: var(--s2) calc(var(--s3) + var(--safe-r)) var(--s2) calc(var(--s3) + var(--safe-l));
  background: var(--bar);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: .5px solid var(--line);
}
/* With the keyboard open, anchor the composer to the bottom of the VISIBLE area. */
body.kb-open .composer { bottom: auto; top: calc(var(--vvt, 0px) + var(--vvh, 100dvh)); transform: translateY(-100%); padding-bottom: var(--s2); }
body.kb-open .tabbar { display: none; }
.composer-row { max-width: 720px; margin: 0 auto; display: flex; align-items: flex-end; gap: var(--s2); }
.composer textarea {
  flex: 1; min-width: 0; resize: none; max-height: 140px; min-height: 44px; padding: 10px 14px;
  border-radius: 22px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font-family: var(--font-content); font-size: 17px; line-height: 1.4;
  -webkit-appearance: none; appearance: none; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.send-btn {
  width: 44px; height: 44px; flex: none; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink); display: grid; place-items: center;
  transition: transform .15s var(--ease), opacity .15s var(--ease);
}
.send-btn:active { transform: scale(.92); }
.send-btn[disabled] { opacity: .4; }
.attach { cursor: pointer; }
.attachment {
  max-width: 720px; margin: 0 auto var(--s2); display: flex; justify-content: space-between; align-items: center;
  font-size: 15px; padding: var(--s1) var(--s1) var(--s1) var(--s3); border-radius: var(--r-sm);
  background: var(--accent-soft); color: var(--accent);
}

/* ===== Cards (approvals, history) ===== */
.card { background: var(--surface); border-radius: var(--r-lg); box-shadow: var(--shadow); padding: var(--s4); margin-bottom: var(--s3); animation: rise .2s var(--ease); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); margin-bottom: var(--s3); flex-wrap: wrap; }
.card-title { font-size: 18px; font-weight: 600; margin: 0 0 var(--s1); font-family: var(--font-content); unicode-bidi: plaintext; overflow-wrap: anywhere; }
.chip { display: inline-flex; align-items: center; font-size: 13px; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.chip-warn { background: var(--warn-soft); color: var(--warn); }
.chip-danger { background: var(--danger-soft); color: var(--danger); }
.chip-muted { background: var(--surface-2); color: var(--ink-soft); }
.section-label { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-soft); margin: var(--s4) 0 var(--s2); }
.content-block {
  font-family: var(--font-content); white-space: pre-wrap; overflow-wrap: anywhere; unicode-bidi: plaintext;
  background: var(--surface-2); border-radius: var(--r-md); padding: var(--s3) var(--s4); line-height: 1.75;
}
.stats { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s2); }
.stat { font-size: 13px; color: var(--ink-soft); background: var(--surface-2); padding: 3px 10px; border-radius: 8px; }
.small { font-size: 14px; color: var(--ink-soft); overflow-wrap: anywhere; }
.feedback { font-size: 15px; color: var(--warn); background: var(--warn-soft); padding: var(--s2) var(--s3); border-radius: var(--r-sm); margin-top: var(--s3); unicode-bidi: plaintext; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s2); margin-top: var(--s4); }
.actions .btn-primary { grid-column: 1 / -1; }
.actions .btn { font-size: 16px; padding: 12px 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* ===== Canvas editor =====
   A full-screen sheet that always matches the VISIBLE area (above the keyboard):
   top = --vvt, height = --vvh. The text area fills the middle and scrolls; the header and the
   Approve button stay in view, so nothing hides behind the keyboard. */
.editor {
  position: fixed; left: 0; right: 0; z-index: 40;
  top: var(--vvt, 0px); height: var(--vvh, 100dvh);
  display: flex; flex-direction: column; background: var(--bg);
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  animation: fade .18s var(--ease);
}
body.kb-open .editor { padding-bottom: 0; }
.editor-bar { flex: none; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; min-height: 48px; padding: 0 var(--s3); border-bottom: .5px solid var(--line); }
.editor-bar h2 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; }
.editor-bar .link-btn:first-child { justify-self: start; }
.editor-bar .link-btn:last-child { justify-self: end; }
.editor-note { flex: none; margin: var(--s2) var(--s4) 0; font-size: 14px; color: var(--accent); }
.editor-text {
  flex: 1 1 auto; min-height: 0; margin: var(--s3) var(--s3) 0; padding: var(--s4);
  border: 1px solid var(--line); border-radius: var(--r-lg); background: var(--surface); color: var(--ink);
  font-family: var(--font-content); font-size: 18px; line-height: 1.8; resize: none; outline: none;
  overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  -webkit-appearance: none; appearance: none;
}
.editor-text:focus { border-color: var(--accent); }
.editor-lint { flex: none; margin: var(--s2) var(--s4) 0; min-height: 20px; font-size: 13px; color: var(--ink-soft); }
.editor-lint.warn { color: var(--warn); font-weight: 600; }
.editor-foot { flex: none; padding: var(--s2) var(--s3) var(--s3); }
body.editing { overflow: hidden; }
html.editing { overflow: hidden; }
.done-wrap { margin-top: var(--s2); }
.done-wrap summary { cursor: pointer; color: var(--ink-soft); font-weight: 600; min-height: 44px; display: flex; align-items: center; padding: 0 var(--s1); }
.row { display: flex; justify-content: space-between; align-items: baseline; gap: var(--s3); }

/* ===== Empty & skeleton ===== */
.empty { text-align: center; color: var(--ink-soft); padding: var(--s6) var(--s3); }
.empty strong { display: block; color: var(--ink); font-weight: 600; font-size: 18px; margin-bottom: var(--s1); }
.skeleton { border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow); padding: var(--s4); margin-bottom: var(--s3); }
.sk-line { height: 12px; border-radius: 6px; margin: 8px 0; background: linear-gradient(90deg, var(--surface-2) 0%, var(--line) 50%, var(--surface-2) 100%); background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
.sk-line.w60 { width: 60%; } .sk-line.w80 { width: 80%; } .sk-line.w40 { width: 40%; }

/* ===== Tab bar ===== */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding: 0 var(--safe-r) var(--safe-b) var(--safe-l);
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--bar);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: .5px solid var(--line);
}
.tab {
  position: relative; border: 0; background: none; min-height: 44px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; font-weight: 600; color: var(--ink-soft);
}
.tab[aria-current="page"] { color: var(--accent); }
.badge {
  position: absolute; top: 4px; left: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center;
}

/* ===== Banners, toast, install hint ===== */
.net-banner {
  position: fixed; top: calc(var(--safe-t) + var(--s2)); left: 50%; transform: translateX(-50%); z-index: 30;
  max-width: calc(100% - 32px); padding: var(--s2) var(--s4); border-radius: 999px; font-size: 14px; font-weight: 600;
  background: var(--ink); color: var(--bg); box-shadow: var(--shadow);
}
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 30;
  bottom: calc(var(--tabbar-h) + var(--composer-h) + var(--s4) + var(--safe-b));
  max-width: calc(100% - 32px); padding: var(--s3) var(--s4); border-radius: var(--r-md); font-size: 15px;
  background: var(--ink); color: var(--bg); box-shadow: var(--shadow); animation: rise .2s var(--ease);
}
.toast.ok { background: var(--ok); color: #fff; }
.toast.err { background: var(--danger); color: #fff; }
.install-hint {
  position: fixed; left: var(--s3); right: var(--s3); z-index: 25;
  bottom: calc(var(--s3) + var(--safe-b));
  display: flex; align-items: center; gap: var(--s2); padding: var(--s2) var(--s2) var(--s2) var(--s4);
  border-radius: var(--r-lg); background: var(--surface); box-shadow: var(--shadow); font-size: 15px;
}
.install-hint p { margin: 0; flex: 1; }
.install-hint svg { vertical-align: -3px; color: var(--accent); }

/* ===== Motion ===== */
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pulse { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}

@media (min-width: 760px) {
  .large-title { margin-left: var(--s4); }
}
