/* Helix Template — UI shell (M3) + Meridian (M4). Internal app shell for logged-in staff/tenant
   users: top bar, flyout sidebar, user-banner dropdown (+ zoom), the views, and the right-hand
   Meridian agent column. Everything is sized in rem so the zoom control scales the whole interface —
   the Meridian column included. The shell only ever talks to /api (the shared Action Layer). */

/* ─── App frame ─── */
#app { min-height: 100%; }
.topbar {
  position: fixed; inset: 0 0 auto 0; height: 3.5rem; z-index: 30;
  display: flex; align-items: center; gap: .75rem; padding: 0 1rem;
  background: color-mix(in srgb, var(--bg-elev) 92%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
  transition: right .2s ease;
}
.topbar .iconbtn { display: inline-grid; place-items: center; width: 2.25rem; height: 2.25rem; border-radius: var(--radius-sm); }
.iconbtn { background: transparent; border: 1px solid transparent; color: var(--muted); cursor: pointer; }
.iconbtn:hover { background: var(--surface-2); color: var(--text); }
.iconbtn.active { background: var(--primary-soft); color: var(--text); border-color: color-mix(in srgb, var(--primary) 35%, transparent); }
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 680; letter-spacing: .01em; }
.brand .dot { width: .7rem; height: .7rem; border-radius: 3px; background: linear-gradient(135deg, var(--primary), #8ab4ff); box-shadow: 0 0 0 3px var(--primary-soft); }
.brand small { color: var(--faint); font-weight: 500; font-size: .7rem; text-transform: uppercase; letter-spacing: .12em; }
.spacer { flex: 1; }

/* user banner button + dropdown */
.userbtn { display: flex; align-items: center; gap: .55rem; padding: .3rem .55rem; border-radius: 999px; background: var(--surface); border: 1px solid var(--border); cursor: pointer; }
.userbtn:hover { border-color: var(--border-strong); }
.avatar { width: 1.8rem; height: 1.8rem; border-radius: 50%; display: grid; place-items: center; font-weight: 700; font-size: .8rem; color: var(--primary-ink); background: linear-gradient(135deg, var(--primary), #7aa0ff); }
.userbtn .who { display: flex; flex-direction: column; line-height: 1.05; text-align: left; }
.userbtn .who b { font-size: .82rem; }
.userbtn .who span { font-size: .68rem; color: var(--muted); }
.caret { color: var(--faint); font-size: .7rem; }

.menu {
  position: fixed; top: 3.2rem; right: 1rem; width: 19rem; z-index: 40;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; transform-origin: top right;
}
.menu .mhead { padding: .9rem 1rem; border-bottom: 1px solid var(--border); }
.menu .mhead b { display: block; }
.menu .mhead span { color: var(--muted); font-size: .82rem; }
.menu .msec { padding: .7rem 1rem; border-bottom: 1px solid var(--border); }
.menu .msec:last-child { border-bottom: 0; }
.menu .mlabel { font-size: .7rem; text-transform: uppercase; letter-spacing: .1em; color: var(--faint); margin-bottom: .5rem; }
.menu .mrow { display: flex; align-items: center; gap: .5rem; width: 100%; padding: .5rem .6rem; border-radius: var(--radius-sm); background: transparent; border: 1px solid transparent; cursor: pointer; color: var(--text); text-align: left; }
.menu .mrow:hover { background: var(--surface-2); }
.menu .mrow.active { border-color: var(--primary); background: var(--primary-soft); }
.menu .mrow small { color: var(--muted); }

/* segmented control (zoom) */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: .15rem; }
.seg button { flex: 1; padding: .3rem .7rem; border: 0; background: transparent; color: var(--muted); border-radius: 999px; cursor: pointer; font-size: .8rem; }
.seg button.on { background: var(--primary); color: var(--primary-ink); font-weight: 600; }

/* ─── Sidebar (flyout) ─── */
.scrim { position: fixed; inset: 3.5rem 0 0 0; background: rgba(4, 6, 11, .55); z-index: 18; opacity: 0; pointer-events: none; transition: opacity .18s ease; }
.scrim.show { opacity: 1; pointer-events: auto; }
.sidebar {
  position: fixed; top: 3.5rem; left: 0; bottom: 0; width: 15rem; z-index: 20;
  background: var(--bg-elev); border-right: 1px solid var(--border);
  transform: translateX(-100%); transition: transform .2s ease; padding: .75rem; overflow-y: auto;
}
.sidebar.open { transform: translateX(0); }
.navlink { display: flex; align-items: center; gap: .7rem; padding: .65rem .8rem; border-radius: var(--radius-sm); color: var(--muted); cursor: pointer; border: 1px solid transparent; margin-bottom: .15rem; }
.navlink:hover { background: var(--surface); color: var(--text); text-decoration: none; }
.navlink.active { background: var(--primary-soft); color: var(--text); border-color: color-mix(in srgb, var(--primary) 35%, transparent); }
.navlink .ic { width: 1.15rem; height: 1.15rem; flex: none; }
.navlink b { font-weight: 550; font-size: .92rem; }
.nav-group { margin: 1rem .3rem .4rem; font-size: .68rem; text-transform: uppercase; letter-spacing: .12em; color: var(--faint); }

/* ─── Main ─── */
.main { margin-top: 3.5rem; padding: 1.6rem clamp(1rem, 4vw, 2.5rem); max-width: 64rem; margin-left: auto; margin-right: auto; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
.page-head h1 { font-size: 1.6rem; }
.page-head p { color: var(--muted); margin: .35rem 0 0; }

.grid { display: grid; gap: 1rem; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 720px) { .grid.cols-2, .grid.cols-3 { grid-template-columns: 1fr; } }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem 1.2rem; }
.card h2 { font-size: 1.05rem; margin-bottom: .2rem; }
.card .sub { color: var(--muted); font-size: .85rem; margin-bottom: .9rem; }
.stat { font-size: 2rem; font-weight: 700; letter-spacing: -.01em; }
.stat-label { color: var(--muted); font-size: .82rem; }

/* tables */
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th { text-align: left; color: var(--faint); font-weight: 600; font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; padding: .5rem .6rem; border-bottom: 1px solid var(--border); }
.table td { padding: .6rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: var(--bg-elev); }
.table .mono { font-family: var(--mono); font-size: .8rem; color: var(--muted); }

/* badges */
.badge { display: inline-flex; align-items: center; gap: .35rem; font-size: .72rem; font-weight: 600; padding: .2rem .55rem; border-radius: 999px; border: 1px solid var(--border-strong); color: var(--muted); }
.badge.role-owner { color: #d9b3ff; border-color: #4b3a66; background: #221833; }
.badge.role-admin { color: #8ab4ff; border-color: #2f4068; background: var(--primary-soft); }
.badge.role-member { color: var(--ok); border-color: #2c5544; background: var(--ok-soft); }
.badge.role-viewer { color: var(--muted); }
.badge.st-open { color: var(--primary); border-color: #2f4068; }
.badge.st-done { color: var(--ok); border-color: #2c5544; background: var(--ok-soft); }
.badge.st-archived { color: var(--faint); }

/* buttons + forms */
.btn { display: inline-flex; align-items: center; gap: .45rem; padding: .55rem .9rem; border-radius: var(--radius-sm); border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text); cursor: pointer; font-size: .9rem; font-weight: 550; }
.btn:hover { border-color: var(--primary); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: #5a2a36; background: var(--danger-soft); }
.btn.sm { padding: .35rem .6rem; font-size: .8rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.field { display: flex; flex-direction: column; gap: .35rem; margin-bottom: .85rem; }
.field label { font-size: .8rem; color: var(--muted); }
.input, select.input { width: 100%; padding: .6rem .7rem; background: var(--bg-elev); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); }
.input:focus { border-color: var(--primary); outline: none; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.toolbar { display: flex; gap: .6rem; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; }

/* notices */
.notice { padding: .7rem .9rem; border-radius: var(--radius-sm); font-size: .88rem; margin-bottom: 1rem; border: 1px solid; }
.notice.err { color: var(--danger); border-color: #5a2a36; background: var(--danger-soft); }
.notice.ok { color: var(--ok); border-color: #2c5544; background: var(--ok-soft); }
.notice.info { color: var(--muted); border-color: var(--border); background: var(--bg-elev); }
.empty { color: var(--muted); text-align: center; padding: 2.5rem 1rem; }
.muted { color: var(--muted); }
.right { margin-left: auto; }

/* ─── Login ─── */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: 100%; max-width: 23rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 1.8rem; box-shadow: var(--shadow); }
.login-card .brand { font-size: 1.15rem; margin-bottom: .3rem; }
.login-card .lede { color: var(--muted); font-size: .88rem; margin: 0 0 1.4rem; }
.login-card .hint { margin-top: 1.2rem; padding-top: 1rem; border-top: 1px solid var(--border); color: var(--faint); font-size: .78rem; line-height: 1.6; }
.login-card .hint code { cursor: pointer; }

/* modal */
.modal-scrim { position: fixed; inset: 0; background: rgba(4, 6, 11, .6); z-index: 50; display: grid; place-items: center; padding: 1.5rem; }
.modal { width: 100%; max-width: 26rem; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-lg); padding: 1.4rem; box-shadow: var(--shadow); }
.modal h3 { margin-bottom: 1rem; }

/* ─── Meridian (M4): the in-app agent column ─── */
/* The panel width doubles as the gutter reserved on the rest of the shell. rem-based, so the zoom
   control scales the column with everything else; clamped against the viewport so it never dominates. */
:root { --mer-w: clamp(20rem, 30vw, 30rem); }
.mer-panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: var(--mer-w); z-index: 36;
  display: flex; flex-direction: column;
  background: var(--bg-elev); border-left: 1px solid var(--border-strong);
  box-shadow: -12px 0 30px rgba(0, 0, 0, .28);
  transform: translateX(100%); transition: transform .22s ease;
}
body.meridian-open .mer-panel { transform: translateX(0); }
/* reserve the gutter by shrinking #app (the static-flow container of .main) + offsetting the fixed
   topbar; .main stays auto-centred inside the narrower #app, so nothing overlaps the column. */
body.meridian-open #app { margin-right: var(--mer-w); transition: margin-right .22s ease; }
body.meridian-open .topbar { right: var(--mer-w); }

.mer-head { display: flex; align-items: center; gap: .6rem; padding: 0 .75rem 0 1rem; height: 3.5rem; flex: none; border-bottom: 1px solid var(--border); }
.mer-title { display: flex; align-items: center; gap: .55rem; flex: 1; min-width: 0; }
.mer-title .ic { color: var(--primary); flex: none; }
.mer-title b { display: block; font-size: .95rem; line-height: 1.1; }
.mer-title span { display: block; font-size: .7rem; color: var(--faint); }

.mer-log { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: .7rem; }
.mer-empty { color: var(--muted); font-size: .88rem; line-height: 1.6; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
.mer-empty b { color: var(--text); }

.mer-msg { max-width: 92%; display: flex; flex-direction: column; gap: .4rem; }
.mer-msg.u { align-self: flex-end; align-items: flex-end; }
.mer-msg.a { align-self: flex-start; align-items: flex-start; }
.mer-text { display: inline-block; padding: .55rem .8rem; border-radius: var(--radius); font-size: .9rem; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.mer-msg.u .mer-text { background: var(--primary); color: var(--primary-ink); border-bottom-right-radius: 4px; }
.mer-msg.a .mer-text { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.mer-msg.a .mer-text:empty { display: none; }

/* tool-call chips: one per action Meridian dispatches — the visible "user = agent" proof */
.mer-tool { display: inline-flex; align-items: center; gap: .45rem; align-self: flex-start; font-size: .78rem; color: var(--muted); background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: .25rem .65rem; }
.mer-tool .ic { width: .9rem; height: .9rem; color: var(--faint); flex: none; }
.mer-tool code { background: transparent; padding: 0; color: var(--text); font-size: .78rem; }
.mer-tool i { font-style: normal; color: var(--faint); }
.mer-tool.ok { border-color: #2c5544; } .mer-tool.ok i { color: var(--ok); }
.mer-tool.err { border-color: #5a2a36; } .mer-tool.err i { color: var(--danger); }

.mer-compose { flex: none; display: flex; gap: .5rem; align-items: flex-end; padding: .75rem; border-top: 1px solid var(--border); background: var(--bg-elev); }
.mer-input { flex: 1; resize: none; max-height: 10rem; min-height: 2.5rem; padding: .6rem .7rem; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); line-height: 1.4; font-size: .9rem; }
.mer-input:focus { border-color: var(--primary); outline: none; }
.mer-send { padding: .6rem .7rem; }
.mer-send .ic { margin: 0; }

@media (max-width: 860px) {
  /* on narrow screens the column overlays instead of reserving a gutter */
  :root { --mer-w: min(92vw, 24rem); }
  body.meridian-open #app { margin-right: 0; }
  body.meridian-open .topbar { right: 0; }
  .mer-panel { z-index: 45; }
}

@media (max-width: 480px) {
  .userbtn .who { display: none; }
  .menu { right: .5rem; left: .5rem; width: auto; }
}
