:root{
  /* M@B platform defaults (fallbacks) */
  --tenant-primary:#525B5E;
  --tenant-bg:#FCF9F4;
  --tenant-text:#111827;
  --tenant-muted:#6b7280;
  --tenant-border:#e5e7eb;
  --tenant-danger-bg:#fff2f2;
  --tenant-danger-border:#f2bcbc;

  /* existing vp tokens mapped to tenant tokens */
  --vp-primary: var(--tenant-primary, #525B5E);
  --vp-bg: var(--tenant-bg, #FCF9F4);
  --vp-text: var(--tenant-text, #111827);
  --vp-muted: var(--tenant-muted, #6b7280);
  --vp-border: var(--tenant-border, #e5e7eb);
  --vp-danger-bg: var(--tenant-danger-bg, #fff2f2);
  --vp-danger-border: var(--tenant-danger-border, #f2bcbc);
}

/* ===== Base layout ===== */

body{
  font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial,sans-serif;
  margin:0;
  color:var(--vp-text);
  background:#f0f0f0;
}

a{color:var(--vp-primary);text-decoration:none}
a:hover{text-decoration:underline}

table{font-size:14px}
select.input{background:#fff}

/* Tables shouldn't force horizontal scrolling of the whole page */
table { width: 100%; border-collapse: collapse; }
.table-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* Prevent long strings (emails/URLs/code) from blowing out layout */
pre, code { white-space: pre-wrap; word-break: break-word; }

/* ===== Common UI bits ===== */

.card{background:#fff;border:none;border-radius:10px;padding:24px 28px;box-shadow:0 4px 16px rgba(0,0,0,.08)}
.hstack{display:flex;gap:10px;align-items:center;flex-wrap:wrap}
.muted{color:var(--vp-muted)}
.btn{display:inline-block;border:1px solid #d1d5db;border-radius:8px;padding:8px 14px;background:#fff;cursor:pointer;font-size:.9rem;color:#374151;font-weight:500}
.btn:hover{background:#f3f4f6;text-decoration:none}
.btn-primary{background:var(--vp-primary);color:#fff;border-color:var(--vp-primary)}
.btn-primary:hover{filter:brightness(.92);background:var(--vp-primary);color:#fff}
.input{width:100%;padding:10px;border:1px solid #ccc;border-radius:6px;font-size:1rem;font-family:inherit}
.input:focus{border-color:var(--vp-primary);outline:none;box-shadow:0 0 0 3px rgba(82,91,94,.18)}
select.input{background:#fff}
.alert{background:var(--vp-danger-bg);border:1px solid var(--vp-danger-border);padding:10px 14px;border-radius:8px}

/* Used on some pages for headings/labels */
.brand{font-weight:700;font-size:22px}

/* ===== Layout safety / responsive defaults ===== */
*, *::before, *::after { box-sizing: border-box; }

html, body { width: 100%; max-width: 100%; overflow-x: clip; }

img, svg, video { max-width: 100%; height: auto; }

/* =========================
   App Shell + Header + Menu
   ========================= */

.app-shell{min-height:100vh;display:flex;flex-direction:column}

/* Content area (readable width + responsive) */
.page-container{
  flex:1;
  width:100%;
  max-width:1480px;
  margin:0 auto;
  padding:20px;

  /* prevents accidental horizontal scroll from long content */
  overflow-x:auto;
  -webkit-overflow-scrolling:touch;
}

.app-footer{margin-top:18px;color:var(--vp-muted);font-size:12px}

/* ===== Full-width sticky header ===== */
.app-header{
  width:100%;
  margin:0;
  padding:16px 20px;
  background:#fff;
  border-bottom:none;
  box-shadow:0 2px 8px rgba(0,0,0,.08);
  position:sticky;
  top:0;
  z-index:50;
}

/*
  Header layout: left-aligned brand + right auth actions
  [hamburger] [org name / tagline]    [auth info]
*/
.app-header-top{
  display:flex;
  align-items:center;
  gap:16px;
  height:100%;
  /* sign-out button is absolutely positioned; brand expands to fill */
}

/* Left column: top row (logo + text) stacked above hamburger */
.app-brand--layout-v2{
  display:flex;
  flex-direction:column;
  align-items:flex-start;
  gap:10px;
  min-width:0;
  flex:1;
}

/* Top row inside the left column: logo + org name/tagline side by side */
.app-brand-top{
  display:flex;
  align-items:center;
  gap:14px;
  min-width:0;
}

/* Org name + tagline stacked vertically */
.app-brand-text{
  display:flex;
  flex-direction:column;
  gap:4px;
  min-width:0;
}

/* Hamburger label sits to the right of the three lines */
.hamburger-label{
  margin-left:8px;
}

.app-title-main{
  font-weight:800;
  font-size:22px;
  line-height:1.1;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  max-width:clamp(160px, 45vw, 600px);
}

.app-title-sub{
  font-size:13px;
  line-height:1.25;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
  max-width:clamp(160px, 45vw, 600px);
}

/* Right: auth actions column (kept for views/header.php compatibility) */
.app-actions{
  display:flex;
  flex-direction:column;
  align-items:flex-end;
  justify-content:center;
  gap:6px;
  flex-shrink:0;
}

.app-actions-meta{
  display:flex;
  flex-direction:column;
  gap:2px;
  align-items:flex-end;
}

.app-actions-meta .muted{ margin:0; line-height:1.2; }

/* ── Sign-out block (absolutely positioned top-right of .app-header) ── */
.app-header-logout{
  position:absolute;
  top:50%; right:16px;
  transform:translateY(-50%);
  display:flex;
  flex-direction:column;
  align-items:flex-end;
  gap:4px;
  z-index:10;
}

.btn-signout{
  display:inline-block;
  background:linear-gradient(180deg,#e74c3c,#b03025);
  color:#fff;
  text-decoration:none;
  font-size:0.85rem;
  font-weight:700;
  padding:7px 16px;
  border-radius:8px;
  box-shadow:0 3px 0 rgba(0,0,0,0.25),0 4px 10px rgba(0,0,0,0.18);
  white-space:nowrap;
  cursor:pointer;
  border:none;
  transition:filter 0.1s ease,transform 0.08s ease,box-shadow 0.08s ease;
}
.btn-signout:hover{ filter:brightness(1.1); text-decoration:none; color:#fff; }
.btn-signout:active{
  transform:translateY(2px);
  box-shadow:0 1px 0 rgba(0,0,0,0.25),0 2px 4px rgba(0,0,0,0.14);
}

.app-header-user{
  font-size:0.7rem;
  color:rgba(255,255,255,0.85);
  text-align:right;
  line-height:1.35;
  text-shadow:1px 1px 3px rgba(0,0,0,0.4);
}
.app-header-user strong{ color:#fff; }
.app-header-user span{ color:rgba(255,255,255,0.7); }

@media (max-width:760px){
  .btn-signout{ font-size:0.78rem; padding:5px 12px; border-radius:6px; }
  .app-header-logout{ gap:2px; right:10px; }
  .app-header-user{ font-size:0.62rem; }
}

/* Logo image displayed inline in the header, left of org name */
.app-logo-img{
  height:60px;
  max-width:280px;
  object-fit:contain;
  flex-shrink:0;
}

/* ===== Hamburger (bare: no box) ===== */

.hamburger{
  border:0;
  background:transparent;
  padding:4px 0;
  cursor:pointer;
  display:inline-flex;
  align-items:center;
  gap:0;
}

.hamburger:focus{
  outline:2px solid var(--vp-primary);
  outline-offset:4px;
}

.hamburger-lines{
  width:22px;
  height:14px;
  position:relative;
  flex-shrink:0;
}

.hamburger-lines span{
  position:absolute;
  left:0;right:0;
  height:2px;
  background:var(--vp-text);
  border-radius:2px;
}

.hamburger-lines span:nth-child(1){top:0}
.hamburger-lines span:nth-child(2){top:6px}
.hamburger-lines span:nth-child(3){bottom:0}

/* ===== 3-zone brand row ===== */

/* Brand top: 3-column grid — left / center / right zones on one horizontal line */
.app-brand-top{
  display:grid;
  grid-template-columns:1fr auto 1fr;
  align-items:center;
  width:100%;
  gap:0;
}

/* Each zone is a flex row so logo + text sit side-by-side when in the same zone */
.brand-zone{
  display:flex;
  align-items:center;
  gap:14px;
  min-width:0;
}
.brand-zone-left  { justify-content:flex-start; }
.brand-zone-center{ justify-content:center; }
.brand-zone-right { justify-content:flex-end; }

/* Brand text block (title + subtitle stacked) */
.app-brand-text{
  display:flex;
  flex-direction:column;
  gap:4px;
  min-width:0;
}

/* ===== Mobile ===== */
@media (max-width: 760px){
  .app-header{ min-height:auto; padding:14px 16px; }
  .page-container{ padding:16px; }

  .app-header-top{ gap:10px; }

  .app-title-main{ font-size:18px; max-width:55vw; }
  .app-title-sub{ font-size:12px; max-width:55vw; }

  .app-logo-img{ height:44px; max-width:160px; }
}


/* ===== Popover Menu (anchored-style dropdown) ===== */

.menu-drawer{
  position:fixed;
  inset:0;
  display:none;
  z-index:9999;
  background:transparent;
}
.menu-drawer.open{ display:block; }

.menu-backdrop{
  position:absolute;
  inset:0;
  background:transparent;
}

.menu-panel{
  position:fixed;
  left:16px;
  top:120px;
  width:320px;
  max-width:calc(100vw - 32px);
  max-height:calc(100vh - 140px);
  overflow:auto;
  -webkit-overflow-scrolling:touch;
  background:var(--vp-bg);
  border:1px solid var(--vp-border);
  border-radius:16px;
  padding:12px;
  box-shadow:0 18px 60px rgba(0,0,0,.22);
  opacity:0;
  transform:translateY(-6px);
  transition:opacity .12s ease, transform .12s ease;
}
.menu-drawer.open .menu-panel{
  opacity:1;
  transform:translateY(0);
}

.menu-items{ display:flex; flex-direction:column; gap:10px; margin-top:12px; }
.menu-items .btn{ width:100%; text-align:left; }

@media (max-width:760px){
  .menu-panel{ left:12px; right:12px; width:auto; }
}


/* ===================================================
   Split-panel layout (votesystem-style card pages)
   =================================================== */

.card-title    { font-size:18px; font-weight:700; margin:0 0 4px; }
.card-subtitle { font-size:13px; color:var(--vp-muted); margin:0 0 20px; }
.section-heading { font-size:15px; font-weight:700; margin:0 0 12px; color:var(--vp-text); }

/* Two-column split inside a card */
.split-layout {
  display:grid;
  grid-template-columns:1fr 1fr;
  gap:0;
  align-items:start;
}
.split-col { padding-right:28px; }
.split-col + .split-col {
  padding-right:0;
  padding-left:28px;
  border-left:1px solid var(--vp-border);
}
@media (max-width:900px) {
  .split-layout { grid-template-columns:1fr; }
  .split-col { padding-right:0; }
  .split-col + .split-col {
    padding-left:0;
    border-left:none;
    border-top:1px solid var(--vp-border);
    padding-top:24px;
    margin-top:24px;
  }
}

/* Form rows */
.form-row { display:flex; flex-direction:column; gap:4px; margin-bottom:16px; }
.form-row > label { font-size:13px; font-weight:600; color:var(--vp-text); }
.form-row small  { font-size:12px; color:var(--vp-muted); }
.form-row-inline { display:flex; align-items:center; gap:8px; margin-bottom:12px; font-size:13px; }
.form-actions    { display:flex; gap:8px; align-items:center; margin-top:4px; }

/* Badges */
.badge         { display:inline-block; padding:2px 9px; border-radius:20px; font-size:11px; font-weight:600; }
.badge-success { background:#d1fae5; color:#065f46; }
.badge-muted   { background:#f3f4f6; color:#6b7280; }
.badge-danger  { background:#fee2e2; color:#991b1b; }

/* Button variants */
.btn-secondary { background:#f3f4f6; border-color:#d1d5db; color:#374151; }
.btn-secondary:hover { background:#e5e7eb; }
.btn-danger    { background:#fee2e2; border-color:#fca5a5; color:#991b1b; }
.btn-danger:hover { background:#fecaca; }
.btn-xs        { padding:4px 10px; font-size:12px; border-radius:6px; }

/* Data table */
.vp-table { width:100%; border-collapse:collapse; font-size:13px; }
.vp-table th {
  text-align:left; padding:7px 10px;
  border-bottom:2px solid var(--vp-border);
  font-size:11px; color:var(--vp-muted);
  text-transform:uppercase; letter-spacing:.05em;
  white-space:nowrap;
}
.vp-table td { padding:8px 10px; border-bottom:1px solid var(--vp-border); vertical-align:middle; }
.vp-table tbody tr:hover { background:rgba(0,0,0,.025); }
.vp-table .col-actions { width:1%; white-space:nowrap; }

/* Search/filter input */
.filter-bar { margin-bottom:12px; }
.filter-bar input {
  width:100%; padding:8px 12px;
  border:1px solid var(--vp-border);
  border-radius:10px; font-size:13px;
  background:#fff;
}


/* ─── Admin rail (desktop primary nav) ───────────────────────────── */

/* Flex wrap around the rail + page content. Header (above) and footer
   (below) sit OUTSIDE this wrap so they span the full viewport width.
   Opened by page_header() and closed by page_footer() when an admin
   page is detected. */
.app-body-wrap {
  display: flex;
  flex: 1 1 auto;
  align-items: stretch;
  width: 100%;
  min-height: 0;
}

.app-body-wrap > .page-container {
  flex: 1 1 auto;
  min-width: 0;       /* let the flex item shrink instead of overflowing */
}

.admin-rail {
  flex: 0 0 64px;
  width: 64px;
  background: var(--vp-rail-bg, #fafafa);
  border-right: 1px solid var(--vp-border, #e5e5e5);
  z-index: 50;
  /* Sticky so the rail stays visible as the page content scrolls.
     Sticks to top:0 of the nearest scrolling ancestor (the body). */
  position: sticky;
  top: 0;
  align-self: flex-start;
  max-height: 100vh;
  /* No overflow scrolling — mixing overflow-x:visible with
     overflow-y:auto causes browsers to silently force both to auto,
     which clips the hover labels. */
  overflow: visible;
}

/* Spacer reserved for layouts that want padding-top inside the rail.
   Unused in the current layout (rail starts at body-wrap top which is
   already below the header), but the markup still emits one with
   height:0 by default. Inline height set per-render in admin_rail.php. */
.admin-rail__spacer {
  flex-shrink: 0;
}

.admin-rail__nav {
  display: flex;
  flex-direction: column;
  padding: 8px 0;
}

/* Rail item — a wrapper around the icon link plus its hover surface
   (tooltip label OR submenu flyout, depending on whether 'children'
   are present). */
.admin-rail__item {
  position: relative;
  display: block;
  border-left: 3px solid transparent;
  transition: background-color 0.12s ease;
}

.admin-rail__item:hover,
.admin-rail__item:focus-within {
  background: var(--vp-rail-hover-bg, #eef);
}

.admin-rail__item.is-active {
  background: var(--vp-rail-active-bg, #e0e7ff);
  border-left-color: var(--vp-rail-active-accent, #1d4ed8);
}

.admin-rail__item.is-active .admin-rail__icon {
  color: var(--vp-rail-active-fg, #1d4ed8);
}

/* The clickable icon area — same height as before. */
.admin-rail__link {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 48px;
  width: 100%;
  text-decoration: none;
  color: var(--vp-rail-fg, #333);
}

.admin-rail__link:focus { outline: none; }

.admin-rail__icon {
  font-size: 22px;
  line-height: 1;
}

/* Flyout panel — slides out to the right of the rail on hover.
   Every rail item gets one (single-page items get a single-row
   panel; grouped items get a row per child), so the hover UX is
   identical across the rail. */
.admin-rail__flyout {
  position: absolute;
  left: 64px;
  top: 0;
  background: #fff;
  border: 1px solid var(--vp-border, #e5e5e5);
  border-radius: 6px;
  padding: 4px 0;
  /* Fixed width so every flyout looks identical regardless of the
     longest child label. 220px comfortably fits "Vote Screen
     Templates" (the longest current label). */
  width: 220px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.18);
  opacity: 0;
  visibility: hidden;
  transform: translateX(-4px);
  transition: opacity 0.12s ease, visibility 0.12s ease, transform 0.12s ease;
  z-index: 51;
}

.admin-rail__item:hover .admin-rail__flyout,
.admin-rail__item:focus-within .admin-rail__flyout,
.admin-rail__item.is-open .admin-rail__flyout {
  opacity: 1;
  visibility: visible;
  transform: translateX(0);
}

.admin-rail__flyout-item {
  display: block;
  padding: 8px 14px;
  font-size: 13px;
  color: #1f2937;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 0.10s ease;
}

.admin-rail__flyout-item:hover,
.admin-rail__flyout-item:focus {
  background: #f3f4f6;
  outline: none;
}

.admin-rail__flyout-item.is-active {
  background: var(--vp-rail-active-bg, #e0e7ff);
  color: var(--vp-rail-active-fg, #1d4ed8);
  font-weight: 600;
}

/* Mobile: the rail flips from a left column to a horizontal scrolling
   strip pinned just under the header. Hamburger is suppressed on admin
   pages so we don't double-stack two navs on small screens. */
@media (max-width: 768px) {
  body.has-admin-rail .hamburger,
  body.has-admin-rail .menu-drawer { display: none !important; }

  .app-body-wrap { flex-direction: column; }
  .admin-rail {
    flex: 0 0 auto;
    width: 100%;
    max-height: none;
    border-right: none;
    border-bottom: 1px solid var(--vp-border, #e5e5e5);
    /* Stay below the sticky header by keeping its own sticky top:0 — the
       header is already in normal flow above .app-body-wrap. */
    position: sticky;
    top: 0;
  }
  .admin-rail__spacer { display: none; }
  .admin-rail__nav {
    flex-direction: row;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 4px 8px;
    gap: 2px;
    -webkit-overflow-scrolling: touch;
  }
  .admin-rail__item {
    flex: 0 0 auto;
    border-left: none;
    border-bottom: 3px solid transparent;  /* underline-style active marker */
  }
  .admin-rail__item.is-active {
    border-left-color: transparent;
    border-bottom-color: var(--vp-rail-active-accent, #1d4ed8);
  }
  .admin-rail__link { width: 56px; height: 44px; }
  /* Flyouts: positioned fixed so they escape the rail's overflow-y:hidden
     clip. JS in admin_rail.php sets top/left at open time anchored to the
     tapped icon's bounding rect. */
  .admin-rail__flyout {
    position: fixed;
    top: auto;
    left: auto;
    right: auto;
    transform: none;
    margin-top: 0;
    width: auto;
    min-width: 200px;
    max-width: 80vw;
  }
}

/* Desktop: rail replaces the hamburger entirely on admin pages.
 * Scope to body.has-admin-rail so the hamburger still works on
 * non-admin surfaces (the voter portal opens it for the operator menu). */
@media (min-width: 769px) {
  body.has-admin-rail .hamburger,
  body.has-admin-rail .menu-drawer { display: none !important; }
}
