/* =====================================================================
   BooksIQ — brand stylesheet. Maroon / Blue / Gold, DM Sans + Playfair.
   Clean, modern, fast, fully responsive (owners use phones). No frameworks.
   ===================================================================== */

:root {
  --maroon: #8E1B1B;
  --maroon-dark: #6f1414;
  --blue: #1E5FA8;
  --blue-dark: #184c86;
  --gold-1: #F9B941;
  --gold-2: #E07A18;
  --ink: #1c2430;
  --muted: #667085;
  --line: #e6e9ef;
  --bg: #f5f6f9;
  --card: #ffffff;
  --green: #178a4c;
  --green-bg: #e7f6ee;
  --amber: #a9711a;
  --amber-bg: #fdf4e3;
  --red: #b42318;
  --red-bg: #fdecea;
  --blue-bg: #e8f0fb;
  --gray-bg: #eef1f5;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --shadow-lg: 0 12px 32px rgba(16,24,40,.14);
  --sidebar-w: 248px;
  --font: 'DM Sans', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 .4em; font-weight: 600; line-height: 1.25; }
.brand-word { font-family: 'Playfair Display', Georgia, serif; font-weight: 700; letter-spacing: .2px; }

/* ---------- App shell ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w);
  background: linear-gradient(180deg, var(--maroon), var(--maroon-dark));
  color: #fff; flex-shrink: 0;
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sidebar .logo {
  padding: 20px 20px 16px;
  display: flex; align-items: center; gap: 10px;
  font-size: 22px; color: #fff;
}
.sidebar .logo .iq { color: var(--gold-1); }
.sidebar .logo:hover { text-decoration: none; }
/* Phase 41 — BETA badge on the logo (config BETA_MODE). Pure CSS pill; shows in
   both the expanded sidebar, the collapsed rail, and the login/signup logo. */
.beta-badge {
  display: inline-block; font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 9px; font-weight: 700; line-height: 1; letter-spacing: .4px;
  background: var(--gold-1, #F9B941); color: #7c2d12;
  padding: 2px 5px; border-radius: 7px; vertical-align: super; margin-left: 5px;
}
.app.sidebar-collapsed .sidebar-top .logo { position: relative; }
.app.sidebar-collapsed .logo .beta-badge {
  font-size: 7px; padding: 1px 3px; margin: 0;
  position: absolute; top: 4px; right: 4px;
}
.auth-card .head .logo .beta-badge { font-size: 11px; padding: 2px 6px; }
.nav { padding: 8px 12px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 11px;
  color: #f3e7e7; padding: 10px 12px; border-radius: var(--radius-sm);
  margin-bottom: 2px; font-weight: 500; font-size: 14.5px;
}
.nav a:hover { background: rgba(255,255,255,.10); text-decoration: none; color: #fff; }
/* P30.3 — active-module highlight: brand-gold left accent + stronger bg, so "where am I" is unmistakable (inset shadow = no layout shift). */
.nav a.active { background: rgba(255,255,255,.18); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 var(--gold-1); }
.nav a .ico { width: 18px; text-align: center; opacity: .95; }
.nav .section { padding: 14px 12px 6px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .08em; color: #e7bcbc; }
.sidebar .foot { padding: 12px; border-top: 1px solid rgba(255,255,255,.14); font-size: 13px; }
/* E1 — sidebar header holds the logo + collapse toggle; long user name/email truncate. */
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding-right: 12px; }
.sidebar-top .logo { flex: 1; min-width: 0; }
.collapse-form { margin: 0; }
.collapse-btn { background: rgba(255,255,255,.12); border: 0; color: #fff; cursor: pointer;
  width: 26px; height: 26px; border-radius: 6px; font-size: 13px; line-height: 1; display: flex;
  align-items: center; justify-content: center; transition: transform .15s ease, background .15s ease; }
.collapse-btn:hover { background: rgba(255,255,255,.24); }
.foot-name, .foot-email { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* §Phase 51 — Zoho-parity sidebar. Smooth collapse; inline-SVG icons; grouped collapsible
   nav; filled brand-gold active pill; collapsed rail = icon + 2-line label with group flyouts. */
.sidebar { transition: width .2s ease; }
.nav a .ico { flex-shrink: 0; }
.nav a { margin-bottom: 3px; }
/* Active = a FILLED brand-gold pill (gold on maroon), matching in the rail + expanded. */
.nav a.active { background: var(--gold-1); color: #5b1717; font-weight: 600; box-shadow: none; }
.nav a.active:hover { background: var(--gold-1); color: #5b1717; }
.nav a.active .ico { opacity: 1; }
.nav-item.nav-top { margin-bottom: 8px; }
.flyout-title { display: none; }
/* Expanded group header: small-caps text + right-aligned chevron (icon hidden — it's for the rail). */
.nav-group { margin-bottom: 2px; }
.nav-group-head {
  display: flex; align-items: center; width: 100%; background: none; border: 0; cursor: pointer;
  font-family: inherit; padding: 12px 12px 6px; gap: 11px;
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: #e7bcbc;
}
.nav-group-head .ico { display: none; }
.nav-group-head .lbl { flex: 1; text-align: left; }
.nav-group-head .chev { transition: transform .2s ease; opacity: .75; margin-left: auto; }
.nav-group-head:hover { color: #fff; }
.nav-group.collapsed .nav-group-head .chev { transform: rotate(-90deg); }
.nav-group-body { overflow: hidden; transition: max-height .22s ease; max-height: 460px; }
.nav-group.collapsed .nav-group-body { max-height: 0; }

/* ---- Collapsed RAIL: icon + tiny 2-line label beneath (Zoho), groups open a hover flyout ---- */
.app.sidebar-collapsed { --sidebar-w: 78px; }
/* Keep the rail vertically scrollable (many groups on a short viewport must stay reachable);
   the flyout escapes via position:fixed placed by app.js — not by un-clipping the sidebar. */
.app.sidebar-collapsed .sidebar { width: 78px; overflow-y: auto; overflow-x: hidden; }
.app.sidebar-collapsed .sidebar-top { flex-direction: column; gap: 8px; padding: 14px 0 8px; }
.app.sidebar-collapsed .logo { font-size: 0; padding: 0; justify-content: center; }
.app.sidebar-collapsed .logo::before { content: 'B'; font-size: 22px; color: #fff; font-family: 'Playfair Display', Georgia, serif; }
.app.sidebar-collapsed .collapse-btn { transform: rotate(180deg); }
.app.sidebar-collapsed .foot { text-align: center; }
.app.sidebar-collapsed .foot-name, .app.sidebar-collapsed .foot-email { display: none; }
/* Rail-level items (Dashboard + each group head) stack icon over a small centred label. */
.app.sidebar-collapsed .nav > .nav-item,
.app.sidebar-collapsed .nav-group-head {
  flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  padding: 8px 2px; text-transform: none; letter-spacing: 0; font-size: 0; color: #f3e7e7;
}
.app.sidebar-collapsed .nav-group-head { border-radius: var(--radius-sm); }
.app.sidebar-collapsed .nav-group-head .ico { display: block; }
.app.sidebar-collapsed .nav-group-head .chev { display: none; }
.app.sidebar-collapsed .nav > .nav-item .lbl,
.app.sidebar-collapsed .nav-group-head .lbl {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  flex: none; text-align: center; font-size: 9.5px; line-height: 1.12; max-width: 68px; color: #e7bcbc;
}
.app.sidebar-collapsed .nav-group-head:hover { background: rgba(255,255,255,.10); }
.app.sidebar-collapsed .nav-group-head:hover .lbl { color: #fff; }
.app.sidebar-collapsed .nav a.active .lbl { color: #5b1717; }
.app.sidebar-collapsed .nav a .ico { width: 20px; height: 20px; }
/* The group's children live in a hover/focus FLYOUT panel to the right of the rail.
   position:fixed (viewport-relative, app.js sets top/left) so the sidebar can keep its own
   vertical scroll without clipping the flyout. Touches the rail edge → no hover-gap. */
.app.sidebar-collapsed .nav-group { position: relative; }
/* `.nav-group > .nav-group-body` (3 classes, later in source) beats `.nav-group.collapsed
   .nav-group-body { max-height:0 }` so a persisted-collapsed group still flies out in the rail. */
.app.sidebar-collapsed .nav-group > .nav-group-body {
  position: fixed; left: 78px; top: 0; width: 210px; max-height: 80vh; overflow-y: auto;
  background: var(--maroon-dark); border: 1px solid rgba(255,255,255,.12); border-radius: 10px;
  box-shadow: var(--shadow-lg); padding: 6px; display: none; z-index: 120; transition: none;
}
.app.sidebar-collapsed .nav-group:hover > .nav-group-body,
.app.sidebar-collapsed .nav-group:focus-within > .nav-group-body { display: block; }
.app.sidebar-collapsed .nav-group-body .flyout-title {
  display: block; font-size: 10px; text-transform: uppercase; letter-spacing: .07em;
  color: #e7bcbc; padding: 4px 10px 6px;
}
.app.sidebar-collapsed .nav-group-body .nav-item {   /* flyout children go back to a horizontal row */
  flex-direction: row; align-items: center; gap: 10px; padding: 9px 10px; font-size: 0;
}
.app.sidebar-collapsed .nav-group-body .nav-item .lbl {
  display: inline; -webkit-line-clamp: none; max-width: none; text-align: left; font-size: 13.5px; color: inherit;
}

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; background: var(--card); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 14px; padding: 0 22px;
  position: sticky; top: 0; z-index: 20;
}
.topbar .page-title { font-size: 17px; font-weight: 600; }
.topbar .spacer { flex: 1; }
.topbar .org-pill {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--gray-bg); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 999px; font-size: 13.5px; font-weight: 500; color: var(--ink);
}
.topbar .org-pill .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, var(--gold-1), var(--gold-2)); }
/* E1 — long org names truncate with an ellipsis inside a fixed-width pill (full name in tooltip). */
.topbar .org-pill.org-name { max-width: 220px; }
.topbar .org-name .org-name-txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
@media (max-width: 640px) { .topbar .org-pill.org-name { max-width: 120px; } }
/* Always-visible Logout pill (topbar) + sidebar-footer logout (both POST forms). */
.topbar .topbar-logout { margin: 0; display: inline-flex; }
.topbar .topbar-logout .org-pill { cursor: pointer; font: inherit; line-height: 1; }
.topbar .topbar-logout .org-pill:hover { border-color: var(--maroon); color: var(--maroon); }
.sidebar .foot .logout-form { margin: 0; }
.sidebar .foot .logout-link { color: #fff; background: none; border: 0; padding: 0; cursor: pointer;
  font: inherit; text-decoration: underline; }
.sidebar .foot .logout-link:hover { text-decoration: none; }
/* Item 3 — hamburger is a ≥44×44 touch target (centered glyph). */
.menu-btn { display: none; background: none; border: 0; font-size: 22px; cursor: pointer; color: var(--ink);
  min-width: 44px; min-height: 44px; align-items: center; justify-content: center; padding: 0; }
/* Item 3 — mobile drawer close control (shown only when the sidebar is a drawer). */
.drawer-close { display: none; background: rgba(255,255,255,.14); border: 0; color: #fff; cursor: pointer;
  min-width: 44px; min-height: 44px; border-radius: 8px; font-size: 20px; line-height: 1;
  align-items: center; justify-content: center; flex-shrink: 0; }
.drawer-close:hover { background: rgba(255,255,255,.26); }
.content { padding: 22px; max-width: 1180px; width: 100%; margin: 0 auto; }

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-head .sub { color: var(--muted); font-size: 14px; }
.page-head .spacer { flex: 1; }

/* ---------- Cards ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); }
.card .card-body { padding: 20px; }
.card .card-head { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex;
  align-items: center; gap: 10px; }
.card .card-head h2 { font-size: 16px; margin: 0; }
.grid { display: grid; gap: 18px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }

/* ---------- Stat tiles ---------- */
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 13px; font-weight: 500; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 6px; letter-spacing: -.01em; }
.stat .value.pos { color: var(--green); }
.stat .value.neg { color: var(--red); }
.stat .hint { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.stat.accent-gold { border-top: 3px solid var(--gold-2); }
.stat.accent-blue { border-top: 3px solid var(--blue); }
.stat.accent-maroon { border-top: 3px solid var(--maroon); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius); max-width: 100%; }
table.tbl { width: 100%; border-collapse: collapse; background: var(--card); font-size: 14px; }
table.tbl th, table.tbl td { padding: 11px 14px; text-align: left; border-bottom: 1px solid var(--line);
  white-space: nowrap; }
table.tbl th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
  font-weight: 600; background: #fbfbfd; }
table.tbl td.num, table.tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.tbl tbody tr:hover { background: #fafbfe; }
table.tbl .muted { color: var(--muted); }
.table-empty { padding: 40px 20px; text-align: center; color: var(--muted); }

/* ---------- Buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 8px; justify-content: center;
  padding: 9px 16px; border-radius: var(--radius-sm); border: 1px solid transparent;
  font: inherit; font-weight: 600; font-size: 14px; cursor: pointer; text-decoration: none;
  transition: filter .12s ease, background .12s ease; }
.btn:hover { text-decoration: none; }
.btn-primary { background: linear-gradient(135deg, var(--gold-1), var(--gold-2)); color: #3a2400; }
.btn-primary:hover { filter: brightness(1.04); }
.btn-blue { background: var(--blue); color: #fff; }
.btn-blue:hover { background: var(--blue-dark); }
.btn-maroon { background: var(--maroon); color: #fff; }
.btn-maroon:hover { background: var(--maroon-dark); }
.btn-ghost { background: #fff; border-color: var(--line); color: var(--ink); }
.btn-ghost:hover { background: var(--gray-bg); }
.btn-danger { background: #fff; border-color: var(--red); color: var(--red); }
.btn-danger:hover { background: var(--red-bg); }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn[disabled] { opacity: .55; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 600; color: #3a4453; }
.field .req { color: var(--red); }
.field .help { font-size: 12px; color: var(--muted); }
.input, select, textarea {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  font: inherit; font-size: 14.5px; background: #fff; color: var(--ink); }
.input:focus, select:focus, textarea:focus { outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(30,95,168,.14); }
textarea { min-height: 84px; resize: vertical; }
.input.err { border-color: var(--red); }
.field .err-msg { color: var(--red); font-size: 12.5px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 0 0 16px; }
fieldset legend { font-weight: 600; font-size: 13px; padding: 0 6px; color: var(--maroon); }
.checkbox { display: flex; align-items: center; gap: 9px; font-size: 14px; }
.checkbox input { width: auto; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-green { background: var(--green-bg); color: var(--green); }
.badge-blue { background: var(--blue-bg); color: var(--blue); }
.badge-amber { background: var(--amber-bg); color: var(--amber); }
.badge-red { background: var(--red-bg); color: var(--red); }
.badge-gray { background: var(--gray-bg); color: var(--muted); }
.pill-type { font-size: 11.5px; padding: 2px 8px; border-radius: 6px; background: var(--gray-bg); color: var(--muted); }

/* ---------- Flash / alerts ---------- */
.flashes { margin-bottom: 16px; display: flex; flex-direction: column; gap: 10px; }
.alert { padding: 11px 15px; border-radius: var(--radius-sm); font-size: 14px; border: 1px solid transparent; }
.alert-success { background: var(--green-bg); border-color: #bfe6cf; color: #10603a; }
.alert-error { background: var(--red-bg); border-color: #f3c6c1; color: #8c1c14; }
.alert-info { background: var(--blue-bg); border-color: #c7dcf5; color: #174a80; }
.alert-warning { background: var(--amber-bg); border-color: #f0dcb4; color: #7d5310; }

/* ---------- Auth screens ---------- */
.auth-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 24px; background: radial-gradient(1200px 500px at 50% -10%, #fbe9c9 0%, var(--bg) 55%); }
.auth-card { width: 100%; max-width: 420px; background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; box-shadow: var(--shadow-lg); overflow: hidden; }
.auth-card .head { padding: 26px 28px 8px; text-align: center; }
.auth-card .head .logo { font-size: 30px; color: var(--maroon); }
.auth-card .head .logo .iq {
  background: linear-gradient(135deg, var(--gold-1), var(--gold-2));
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.auth-card .head p { color: var(--muted); font-size: 14px; margin: 4px 0 0; }
.auth-card .body { padding: 18px 28px 28px; }
.auth-card .body .field { margin-bottom: 14px; }
.auth-card .body .btn { width: 100%; }
.auth-alt { text-align: center; margin-top: 16px; font-size: 14px; color: var(--muted); }

/* ---------- Onboarding steps ---------- */
.steps { display: flex; gap: 8px; margin-bottom: 20px; flex-wrap: wrap; }
.steps .step { flex: 1; min-width: 120px; padding: 10px 12px; border-radius: var(--radius-sm);
  background: var(--card); border: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.steps .step.on { border-color: var(--gold-2); color: var(--ink); font-weight: 600; }

/* ---------- Utilities ---------- */
.muted { color: var(--muted); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.num { font-variant-numeric: tabular-nums; }
.mt-0 { margin-top: 0; } .mt-1 { margin-top: 8px; } .mt-2 { margin-top: 16px; } .mt-3 { margin-top: 24px; }
.mb-0 { margin-bottom: 0; } .mb-2 { margin-bottom: 16px; }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }
.tag-note { font-size: 12px; color: var(--muted); }
.inline-form { display: inline; }

/* ---------- IQ Assistant widget ---------- */
.iq-fab { position: fixed; right: 22px; bottom: 22px; width: 56px; height: 56px; border-radius: 50%;
  border: 0; cursor: pointer; z-index: 80; color: #fff; font-family: 'Playfair Display', serif;
  font-weight: 700; font-size: 18px; box-shadow: var(--shadow-lg);
  background: linear-gradient(135deg, var(--maroon), var(--maroon-dark)); }
.iq-fab:hover { filter: brightness(1.08); }
.iq-panel { position: fixed; right: 22px; bottom: 88px; width: 360px; max-width: calc(100vw - 44px);
  height: 480px; max-height: calc(100vh - 130px); background: var(--card); border: 1px solid var(--line);
  border-radius: 16px; box-shadow: var(--shadow-lg); z-index: 80; display: flex; flex-direction: column; overflow: hidden; }
/* H1 fix: author `display:flex` above overrides the UA `[hidden]` rule, so the
   panel never hid on close/toggle. This higher-specificity rule restores it. */
.iq-panel[hidden] { display: none; }
.iq-head { background: linear-gradient(135deg, var(--maroon), var(--maroon-dark)); color: #fff;
  padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; font-size: 16px; }
.iq-head button { background: none; border: 0; color: #fff; cursor: pointer; font-size: 15px; }
.iq-body { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; background: var(--bg); }
.iq-msg { padding: 9px 12px; border-radius: 12px; font-size: 14px; max-width: 85%; line-height: 1.45; }
.iq-bot { background: #fff; border: 1px solid var(--line); align-self: flex-start; }
.iq-user { background: var(--blue); color: #fff; align-self: flex-end; }
.iq-actions { display: flex; gap: 8px; margin-top: 8px; }
.iq-input { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--line); background: #fff; }
.iq-input input { flex: 1; padding: 9px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm); font: inherit; }
/* §Phase 51 — voice input controls (mic + language toggle) on the IQ assistant. */
.iq-mic, .iq-lang { border: 1px solid var(--line); background: #fff; border-radius: var(--radius-sm); cursor: pointer; font: inherit; line-height: 1; }
.iq-mic { width: 38px; font-size: 16px; }
.iq-lang { min-width: 34px; padding: 0 7px; font-size: 12px; font-weight: 600; color: var(--muted); }
.iq-mic:hover, .iq-lang:hover { background: var(--bg); }
.iq-mic.listening { background: var(--maroon); border-color: var(--maroon); animation: iqpulse 1.1s ease-in-out infinite; }
@keyframes iqpulse { 0%, 100% { box-shadow: 0 0 0 0 rgba(142,27,27,.45); } 50% { box-shadow: 0 0 0 5px rgba(142,27,27,0); } }
@media (prefers-reduced-motion: reduce) { .iq-mic.listening { animation: none; } }
/* Item 1 — on phones lift the chat FAB above the bottom action area (the kbd footer
   is hidden < 768px, but sticky action bars / browser chrome sit at the bottom). */
@media (max-width: 767px) {
  .iq-fab { bottom: 72px; }
  .iq-panel { bottom: 138px; max-height: calc(100vh - 170px); }
}

/* ---------- Command palette + shortcut help (§23) ---------- */
#cmdk, #kbdhelp { position: fixed; inset: 0; z-index: 200; }
#cmdk[hidden], #kbdhelp[hidden] { display: none; }
.cmdk-back { position: absolute; inset: 0; background: rgba(20,22,28,.45); }
.cmdk-box { position: relative; max-width: 560px; margin: 12vh auto 0; background: var(--card);
  border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow-lg); overflow: hidden; }
.cmdk-input { width: 100%; border: 0; border-bottom: 1px solid var(--line); padding: 16px 18px; font: inherit; font-size: 16px; outline: none; }
.cmdk-list { list-style: none; margin: 0; padding: 6px; max-height: 44vh; overflow-y: auto; }
.cmdk-list li { padding: 10px 12px; border-radius: 8px; cursor: pointer; font-size: 14px; }
.cmdk-list li.on, .cmdk-list li:hover { background: var(--bg); }
.cmdk-list li.empty { color: var(--muted); cursor: default; }
.cmdk-list li.cmdk-grp { padding: 8px 12px 3px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); cursor: default; background: none; font-weight: 600; }
.cmdk-list li.cmdk-grp:hover { background: none; }
.cmdk-hint { padding: 8px 14px; font-size: 11px; color: var(--muted); border-top: 1px solid var(--line); }
#kbdhelp .cmdk-box { padding: 18px; }
#kbdhelp kbd { background: var(--bg); border: 1px solid var(--line); border-radius: 5px; padding: 1px 6px; font: inherit; font-size: 12px; }
/* Drill-down links inside report tables read as plain figures until hovered. */
a.drill { color: inherit; text-decoration: none; border-bottom: 1px dotted var(--line); }
a.drill:hover { color: var(--blue); border-bottom-color: var(--blue); }

/* ---------- Action bar + print (§42) ---------- */
.action-bar { display: inline-flex; gap: 6px; flex-wrap: wrap; align-items: center; }
@media print {
  .sidebar, .topbar, .staging-banner, .no-print, .action-bar, .btn-row, .iq-fab, .iq-panel, form[data-confirm] { display: none !important; }
  .app, .main, .content { display: block !important; margin: 0 !important; padding: 0 !important; box-shadow: none !important; }
  body { background: #fff !important; }
  .card { box-shadow: none !important; border: 1px solid #ddd !important; break-inside: avoid; }
}

/* ---------- Staging banner ---------- */
.staging-banner { position: sticky; top: 0; z-index: 300; background: linear-gradient(90deg, #F9B941, #E07A18);
  color: #3a2400; font-weight: 700; font-size: 13px; text-align: center; padding: 8px 14px;
  letter-spacing: .01em; box-shadow: 0 1px 6px rgba(224,122,24,.35); }

/* ---------- Universal search-select (§31) ---------- */
.ss { position: relative; }
/* §43 C — the results panel is portaled to <body> and positioned as position:fixed
   (searchselect.js) so table/card overflow never clips it. left/top/width are set
   inline per open; z-index sits below the app-confirm modal (9999). */
.ss-list { position: fixed; z-index: 9000; margin: 2px 0 0; padding: 4px;
  list-style: none; background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow-lg); max-height: 260px; overflow-y: auto; }
.ss-list[hidden] { display: none; }
.ss-list li { padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 14px; }
.ss-list li.on, .ss-list li:hover { background: var(--bg); }
.ss-list li.ss-empty { color: var(--muted); cursor: default; }
.ss-list li.ss-create { color: var(--blue); font-weight: 600; }
.ss-hint { color: var(--muted); font-size: 12px; margin-left: 6px; }

/* ---------- Row action menu (§43 E2) + inline edit affordance ---------- */
.edit-mini { margin-left: 6px; color: var(--muted); text-decoration: none; font-size: 13px; }
.edit-mini:hover { color: var(--blue); }
.rowmenu { display: inline-block; position: relative; }
.rowmenu > summary { list-style: none; cursor: pointer; }
.rowmenu > summary::-webkit-details-marker { display: none; }
.rowmenu-pop { position: fixed; z-index: 9000; min-width: 172px; padding: 6px; margin: 0;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; gap: 2px; text-align: left; }
.rowmenu-pop a, .rowmenu-pop button { display: block; width: 100%; text-align: left; padding: 8px 10px;
  border-radius: 6px; background: none; border: 0; font: inherit; color: inherit; cursor: pointer; text-decoration: none; }
.rowmenu-pop a:hover, .rowmenu-pop button:hover { background: var(--bg); }
.rowmenu-pop form { margin: 0; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .sidebar { position: fixed; z-index: 60; transform: translateX(-100%);
    transition: transform .2s ease; box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: inline-flex; }
  /* Drawer mode: show the close (✕) control, hide the desktop rail-collapse toggle. */
  .drawer-close { display: inline-flex; }
  .collapse-form { display: none; }
  .backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 55; }
  .grid.cols-4 { grid-template-columns: repeat(2, 1fr); }
  .grid.cols-3 { grid-template-columns: 1fr; }
  .grid.cols-2 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .content { padding: 16px; }
  .grid.cols-4 { grid-template-columns: 1fr; }
  .stat .value { font-size: 22px; }
  /* Item 7 — keep the header to at most TWO rows at 390px: row 1 = menu + title,
     row 2 = the pills (notifications, FY, org, logout). Hide the flex spacer and
     tighten gaps/padding so all four pills fit on one line; logout stays one tap. */
  .topbar { flex-wrap: wrap; height: auto; row-gap: 6px; gap: 8px; padding: 8px 12px; }
  .topbar .spacer { display: none; }
  .topbar .page-title { flex: 1 1 auto; min-width: 0; font-size: 15px;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar .org-pill { padding: 6px 10px; font-size: 12.5px; }
  .topbar .org-pill.org-name { max-width: 100px; }
  /* §37 calendar: stack the month grid above the upcoming list. */
  .cal-layout { grid-template-columns: 1fr !important; }
  /* §36 360px pass: any wide table scrolls rather than breaking the layout. */
  .table, .tbl { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; white-space: nowrap; }
  /* Item 1/2 — nothing else may force the body sideways: wide code/media scroll or clamp. */
  pre, code { overflow-x: auto; max-width: 100%; }
  img { max-width: 100%; height: auto; }
  .action-bar { flex-wrap: wrap; }
  .adm-wrap, .adm-top { padding-left: 12px; padding-right: 12px; }
}
/* Very small phones (≤360px) — tighten spacing so nothing clips. */
@media (max-width: 360px) {
  .content { padding: 12px; }
  .btn, .btn-sm { padding: 7px 10px; }
  .cal-grid td { height: 56px; }
  .auth-card .body { padding: 18px; }
}

/* ===== §Phase 49 — Organization switcher (one shared component: page + dropdown) ===== */
.orgsw-wrap { position: relative; display: inline-flex; }
.topbar .org-pill.org-name .orgsw-caret { font-size: 10px; opacity: .65; flex-shrink: 0; margin-left: 2px; }
.topbar #orgswitchBtn { cursor: pointer; text-decoration: none; }
.topbar #orgswitchBtn:hover,
.topbar #orgswitchBtn[aria-expanded="true"] { border-color: var(--maroon); color: var(--maroon); }

/* Dropdown panel — portaled to <body>, mirrors the .rowmenu-pop popover recipe. */
.orgsw-pop {
  position: fixed; z-index: 9000; width: min(340px, calc(100vw - 16px));
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow-lg); padding: 8px;
  /* Cap the whole panel and scroll it as one — so the footer + "Add organization"
     stay reachable on short viewports (the inner list must not also scroll). */
  max-height: calc(100vh - 80px); overflow-y: auto;
}
.orgsw-pop .orgsw-list { max-height: none; overflow: visible; }

/* Shared inner layout (the /onboarding page and the dropdown both render these). */
.orgsw-search { padding: 2px 2px 8px; }
.orgsw-filter { width: 100%; }
.orgsw-list { display: flex; flex-direction: column; gap: 2px; max-height: 62vh; overflow-y: auto; }
.orgsw-form { margin: 0; }
.orgsw-row {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 44px;
  padding: 8px 10px; border-radius: 8px; background: none; border: 1px solid transparent;
  text-align: left; font: inherit; color: var(--ink); cursor: pointer;
}
button.orgsw-row:hover { background: var(--bg); }
button.orgsw-row:focus-visible { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(30,95,168,.14); }
.orgsw-row.is-current { background: var(--blue-bg); cursor: default; }
.orgsw-av {
  flex-shrink: 0; width: 34px; height: 34px; border-radius: 9px; color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12.5px; letter-spacing: .3px;
}
.orgsw-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.orgsw-name { font-weight: 600; font-size: 14px; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.orgsw-meta { display: flex; align-items: center; gap: 6px; min-width: 0; font-size: 12px; color: var(--muted); }
.orgsw-gstin { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.orgsw-chip { flex-shrink: 0; font-size: 10.5px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--gray-bg); color: var(--muted); white-space: nowrap; }
.orgsw-chip.orgsw-role-owner { background: var(--amber-bg); color: var(--amber); }
.orgsw-chip.orgsw-role-accountant { background: var(--blue-bg); color: var(--blue); }
.orgsw-chip.orgsw-role-ca_readonly { background: var(--gray-bg); color: var(--muted); }
.orgsw-badge { flex-shrink: 0; font-size: 11px; font-weight: 600; color: var(--blue); background: var(--blue-bg); padding: 2px 9px; border-radius: 999px; }
.orgsw-chev { flex-shrink: 0; color: var(--muted); font-size: 20px; line-height: 1; }
.orgsw-empty { padding: 12px 10px; color: var(--muted); font-size: 13px; text-align: center; }
.orgsw-foot { padding: 8px 10px 2px; margin-top: 4px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.orgsw-add {
  display: block; width: 100%; text-align: left; margin-top: 6px; padding: 9px 10px;
  border-radius: 8px; background: none; border: 1px dashed var(--line); color: var(--blue);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.orgsw-add:hover { background: var(--blue-bg); border-color: var(--blue); }
@media (max-width: 560px) { .orgsw-pop { width: calc(100vw - 16px); } }
