/* ================================================================
   Construction Portal — Global stylesheet
   Mobile-first · CSS variables for theme · No framework
   ================================================================ */

/* --- Theme tokens ------------------------------------------------- */
:root {
  --primary:        #1e40af;
  --primary-dark:   #1e3a8a;
  --primary-light:  #3b82f6;
  --accent:         #f59e0b;
  --success:        #10b981;
  --warning:        #f59e0b;
  --danger:         #ef4444;

  --bg:             #f7f8fa;
  --bg-card:        #ffffff;
  --bg-elevated:    #ffffff;
  --bg-input:       #ffffff;

  --text:           #0f172a;
  --text-soft:      #475569;
  --text-muted:     #94a3b8;
  --border:         #e2e8f0;
  --border-strong:  #cbd5e1;
  --shadow-sm:      0 1px 2px rgba(15,23,42,.06);
  --shadow:         0 4px 16px rgba(15,23,42,.08);
  --shadow-lg:      0 12px 32px rgba(15,23,42,.12);

  --radius-sm:      6px;
  --radius:         10px;
  --radius-lg:      14px;
  --radius-pill:    999px;

  --topbar-h:       56px;
  --bottomnav-h:    60px;
  --content-max:    1200px;
  --touch-min:      44px;        /* Apple HIG minimum tap target */

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "IBM Plex Sans Thai",
               "Sarabun", "Prompt", "Noto Sans Thai", Tahoma, sans-serif;
}

[data-theme="dark"] {
  --primary:        #3b82f6;
  --primary-dark:   #1d4ed8;
  --primary-light:  #60a5fa;

  --bg:             #0b1220;
  --bg-card:        #111c2e;
  --bg-elevated:    #1a2640;
  --bg-input:       #0f1a2e;

  --text:           #e2e8f0;
  --text-soft:      #94a3b8;
  --text-muted:     #64748b;
  --border:         #1f2a44;
  --border-strong:  #334155;
  --shadow-sm:      0 1px 2px rgba(0,0,0,.4);
  --shadow:         0 4px 16px rgba(0,0,0,.5);
  --shadow-lg:      0 12px 32px rgba(0,0,0,.6);
}

/* --- Reset -------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { font-size: 15px; -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
img, svg { max-width: 100%; display: block; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: inherit; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Topbar ------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 50;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  height: var(--topbar-h);
}
.topbar-inner {
  max-width: var(--content-max);
  margin: 0 auto;
  height: 100%;
  padding: 0 12px;
  display: flex; align-items: center; gap: 10px;
}
.brand {
  display: inline-flex; align-items: center; gap: 8px;
  color: var(--text); font-weight: 600;
  text-decoration: none;
}
.brand:hover { text-decoration: none; }
.brand-mark { font-size: 22px; }
.brand-text { font-size: 16px; }
.brand-logo-img { height: 28px; width: auto; max-width: 150px; object-fit: contain; display: block; border-radius: 6px; }
.sidenav-head .brand-logo-img { height: 30px; max-width: 170px; }
@media (max-width: 600px) { .brand-text { display: none; } }

.topbar-spacer { flex: 1 1 auto; }

.icon-btn {
  background: transparent; border: 0;
  color: var(--text);
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px;
}
.icon-btn:hover { background: var(--bg); }

.project-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  color: var(--text); font-weight: 500;
  max-width: 240px;
}
.project-pill:hover { border-color: var(--primary); }
.project-pill-label {
  font-size: 11px; padding: 2px 6px;
  background: var(--primary); color: #fff;
  border-radius: var(--radius-sm);
  font-weight: 600;
}
.project-pill-name {
  font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
@media (max-width: 600px) {
  .project-pill { max-width: 130px; padding: 4px 8px; }
  .project-pill-name { font-size: 12px; }
}

.user-menu { position: relative; }
.user-chip {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 4px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  color: var(--text);
}
.user-chip:hover { border-color: var(--primary); }
.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600;
}
.user-name {
  font-size: 13px;
  max-width: 140px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
@media (max-width: 600px) { .user-name { display: none; } }

.user-menu-pop {
  position: absolute; top: calc(100% + 6px); right: 0;
  min-width: 180px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 60;
}
.user-menu-pop a {
  display: block; padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--text);
}
.user-menu-pop a:hover {
  background: var(--bg); text-decoration: none;
}

/* --- Side nav (slide-in drawer) ---------------------------------- */
.sidenav-backdrop {
  position: fixed; inset: 0;
  background: rgba(15,23,42,.45);
  z-index: 95;
  opacity: 0; visibility: hidden;
  transition: opacity .2s ease, visibility .2s ease;
}
.sidenav-backdrop.open { opacity: 1; visibility: visible; }

.sidenav {
  position: fixed; top: 0; left: 0;
  width: 280px; max-width: 84vw; height: 100%;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: 12px;
  padding-top: calc(12px + env(safe-area-inset-top));
  transform: translateX(-100%);
  transition: transform .25s ease;
  z-index: 96;
  box-shadow: 2px 0 16px rgba(15,23,42,.12);
  overflow-y: auto;
}
.sidenav[hidden] { display: block; }   /* override default; toggled via class */
.sidenav.open { transform: translateX(0); }
.sidenav-head {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 8px 14px;
  margin-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.sidenav-head .brand-mark { font-size: 26px; }
.sidenav-head-text { font-weight: 700; font-size: 16px; }
.sidenav-section-label {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .5px; color: var(--text-muted);
  padding: 12px 12px 4px;
}
.sidenav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px;
  border-radius: var(--radius);
  color: var(--text); font-weight: 500;
  margin-bottom: 2px;
  min-height: var(--touch-min);
}
.sidenav-item:hover, .sidenav-item.is-active {
  background: var(--bg); text-decoration: none;
}
.sidenav-item.is-active { color: var(--primary); font-weight: 600; }
.sidenav-item-icon { font-size: 20px; width: 26px; text-align: center; flex-shrink: 0; }

/* --- Main --------------------------------------------------------- */
.app-main {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 16px 12px 90px;       /* bottom padding for bottomnav + sticky bar */
}
.bare-main {
  max-width: 480px;
  padding: 32px 16px;
}
/* When sticky bottom action bar is present, give extra padding */
.has-sticky-bottom .app-main {
  padding-bottom: calc(var(--bottomnav-h) + 90px + env(safe-area-inset-bottom));
}
@media (max-width: 720px) {
  .app-main {
    padding-bottom: calc(var(--bottomnav-h) + 16px + env(safe-area-inset-bottom));
  }
  /* When a fixed sticky-bottom bar is on the page, reserve room so the
     last fields aren't hidden behind it (modern :has() selector). */
  .app-main:has(.sticky-bottom) {
    padding-bottom: calc(var(--bottomnav-h) + 84px + env(safe-area-inset-bottom));
  }
}

/* --- Page header -------------------------------------------------- */
.page-header {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 12px; flex-wrap: wrap;
  margin: 8px 0 18px;
}
.page-header h1 {
  margin: 0; font-size: 22px; font-weight: 700;
  line-height: 1.25;
}
.page-subtitle {
  margin: 4px 0 0; color: var(--text-soft); font-size: 14px;
}
.page-header-text { min-width: 0; flex: 1 1 auto; }
.page-header-actions { display: flex; gap: 8px; flex-shrink: 0; }
@media (max-width: 600px) {
  .page-header {
    margin: 4px 0 14px;
    align-items: stretch;
  }
  .page-header h1 { font-size: 19px; }
  /* Primary action becomes a full-width button below the title */
  .page-header-actions {
    width: 100%;
  }
  .page-header-actions .btn { flex: 1 1 auto; }
}

/* --- Cards -------------------------------------------------------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: 12px; }

/* --- Buttons ------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text); font-weight: 500;
  font-size: 14px;
  transition: all .15s;
  min-height: 40px;
  white-space: nowrap;
}
.btn:hover { border-color: var(--primary); }
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ghost  { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--bg); }
.btn-sm { padding: 6px 12px; min-height: 32px; font-size: 13px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* --- Forms -------------------------------------------------------- */
.form-row { margin-bottom: 14px; }
.form-row label {
  display: block;
  font-size: 13px; font-weight: 500;
  color: var(--text-soft);
  margin-bottom: 6px;
}
.input, .select, .textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 15px;
  min-height: 42px;
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}
.textarea { min-height: 80px; resize: vertical; }
.form-grid {
  display: grid; gap: 12px;
  grid-template-columns: 1fr;
}
@media (min-width: 600px) {
  .form-grid.cols-2 { grid-template-columns: 1fr 1fr; }
  .form-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
}
.help-text { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

/* --- Tables ------------------------------------------------------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table {
  width: 100%; border-collapse: collapse;
  font-size: 14px;
}
.table th, .table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table th {
  font-weight: 600;
  color: var(--text-soft);
  background: var(--bg);
  font-size: 13px;
  position: sticky; top: 0;
}
.table tr:hover td { background: var(--bg); }

/* --- Responsive table → stacked cards on mobile -----------------
   Add class .table-stack and give each <td> a data-label="..." attr.
   On narrow screens the header row hides and each row becomes a card
   with label:value pairs. ------------------------------------------ */
@media (max-width: 720px) {
  .table-stack thead { display: none; }
  .table-stack,
  .table-stack tbody,
  .table-stack tr,
  .table-stack td { display: block; width: 100%; }
  .table-stack tr {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 6px 12px;
    margin-bottom: 10px;
    box-shadow: var(--shadow-sm);
  }
  .table-stack tr:hover td { background: transparent; }
  .table-stack td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 7px 0;
    border-bottom: 1px solid var(--border);
    text-align: right;
    min-height: 36px;
  }
  .table-stack td:last-child { border-bottom: 0; }
  .table-stack td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--text-soft);
    font-size: 12px;
    text-align: left;
    flex-shrink: 0;
  }
  /* Hide cells flagged for desktop-only or empty-label rows */
  .table-stack td[data-label=""] { justify-content: flex-end; }
  .table-stack td.td-actions { padding-top: 10px; }
  .table-stack td.td-actions .btn { flex: 1; }
}

/* --- Badges / pills ----------------------------------------------- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: 11px;
  font-weight: 600;
  background: var(--bg);
  color: var(--text-soft);
}
.badge-success { background: rgba(16,185,129,.15); color: var(--success); }
.badge-warning { background: rgba(245,158,11,.15); color: var(--warning); }
.badge-danger  { background: rgba(239,68,68,.15);  color: var(--danger);  }
.badge-info    { background: rgba(59,130,246,.15); color: var(--primary); }

/* --- Toast -------------------------------------------------------- */
.toast-container {
  position: fixed; bottom: 24px; right: 24px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 9999;
  pointer-events: none;
}
.toast {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  min-width: 240px; max-width: 360px;
  font-size: 14px;
  opacity: 0; transform: translateY(8px);
  transition: all .2s ease;
  pointer-events: auto;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { border-left-color: var(--success); }
.toast-error   { border-left-color: var(--danger);  }
.toast-warning { border-left-color: var(--warning); }
@media (max-width: 600px) {
  .toast-container { left: 12px; right: 12px; bottom: 12px; }
  .toast { min-width: 0; max-width: none; }
}

/* --- Modal -------------------------------------------------------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15,23,42,.45);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  z-index: 1000;
}
.modal {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 560px;
  max-height: 90vh; overflow: auto;
  box-shadow: var(--shadow-lg);
}
.modal-head { padding: 16px 18px; border-bottom: 1px solid var(--border);
              display: flex; align-items: center; justify-content: space-between; }
.modal-body { padding: 18px; }
.modal-foot { padding: 14px 18px; border-top: 1px solid var(--border);
              display: flex; justify-content: flex-end; gap: 8px; }

/* --- Empty / loading / state -------------------------------------- */
.empty-state {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-muted);
}
.empty-state-icon { font-size: 44px; margin-bottom: 12px; opacity: .7; }
.empty-state h3 { margin: 4px 0 8px; color: var(--text-soft); }

.is-hidden { display: none !important; }
.is-loading { opacity: .55; pointer-events: none; }

/* --- Utilities ---------------------------------------------------- */
.text-center { text-align: center; }
.text-right  { text-align: right; }
.text-muted  { color: var(--text-muted); }
.text-soft   { color: var(--text-soft); }
.flex { display: flex; }
.flex-row { display: flex; flex-direction: row; gap: 8px; }
.flex-col { display: flex; flex-direction: column; gap: 8px; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1 1 auto; }
.gap-4 { gap: 4px; } .gap-8 { gap: 8px; } .gap-12 { gap: 12px; } .gap-16 { gap: 16px; }
.mb-0 { margin-bottom: 0; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.mt-16 { margin-top: 16px; }

/* --- Module grid (dashboard) -------------------------------------- */
.module-grid {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}
.module-card {
  position: relative;
  display: block;
  padding: 18px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  text-decoration: none;
  color: var(--text);
  transition: all .15s;
}
.module-card:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
  text-decoration: none;
}
.module-card-icon {
  width: 44px; height: 44px;
  border-radius: var(--radius);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 24px;
  margin-bottom: 10px;
  background: rgba(59,130,246,.12);
}
.module-card h3 {
  margin: 0 0 4px; font-size: 15px; font-weight: 600;
}
.module-card-desc { font-size: 12px; color: var(--text-muted); }
.module-card-count {
  position: absolute; top: 14px; right: 14px;
  background: var(--primary);
  color: #fff;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: 11px;
  font-weight: 600;
}

/* --- Login page --------------------------------------------------- */
.login-card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 32px 24px;
}
.login-card h1 {
  margin: 0 0 24px; text-align: center;
  font-size: 24px;
}
.login-brand {
  text-align: center; margin-bottom: 12px;
  font-size: 44px;
}

/* --- Print friendly ---------------------------------------------- */
@media print {
  .topbar, .sidenav, .bottomnav, .fab, .sticky-bottom,
  .toast-container, .page-header-actions, .no-print { display: none !important; }
  body { background: #fff; }
  .app-main { max-width: none; padding: 0; }
  .card { box-shadow: none; border: 1px solid #ccc; }
}

/* ================================================================
   MOBILE-FIRST COMPONENTS — added in UI/UX refactor
   ================================================================ */

/* --- Bottom Navigation (mobile primary nav) ---------------------- */
.bottomnav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--bottomnav-h);
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 12px rgba(15,23,42,.05);
  display: flex;
  z-index: 90;
}
.bottomnav-item {
  flex: 1 1 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 2px;
  padding: 6px 4px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 11px;
  min-height: var(--touch-min);
  transition: color .15s, background .15s;
  position: relative;
}
.bottomnav-item:hover, .bottomnav-item:focus {
  text-decoration: none;
  background: var(--bg);
}
.bottomnav-item.is-active {
  color: var(--primary);
}
.bottomnav-item.is-active::before {
  content: "";
  position: absolute;
  top: 0; left: 50%;
  transform: translateX(-50%);
  width: 32px; height: 3px;
  background: var(--primary);
  border-radius: 0 0 3px 3px;
}
.bottomnav-icon {
  font-size: 22px;
  line-height: 1;
}
.bottomnav-label {
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}
/* Center "+" button styled as a circle accent */
.bottomnav-item-cta {
  position: relative;
}
.bottomnav-item-cta .bottomnav-icon {
  width: 46px; height: 46px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 24px; font-weight: 600;
  box-shadow: 0 3px 10px rgba(30,64,175,.35);
  margin-top: -16px;
}
.bottomnav-item-cta .bottomnav-label {
  margin-top: -4px;
}
.bottomnav-item-cta.is-active::before { display: none; }

/* Show bottomnav only on mobile */
@media (min-width: 721px) {
  .bottomnav { display: none; }
}

/* --- Sticky bottom action bar (forms) --------------------------- */
.sticky-bottom {
  position: sticky;
  bottom: 0;
  margin: 16px -12px -16px;          /* negate app-main horizontal padding */
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 12px rgba(15,23,42,.06);
  display: flex; gap: 10px; justify-content: flex-end;
  flex-wrap: wrap;
  z-index: 50;
}
.sticky-bottom .btn { flex: 1 1 auto; max-width: 260px; }
@media (max-width: 720px) {
  .sticky-bottom {
    /* Float above bottom navigation */
    position: fixed;
    bottom: var(--bottomnav-h);
    left: 0; right: 0;
    margin: 0;
    padding-bottom: 12px;
  }
}

/* --- Floating Action Button (FAB) ------------------------------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(var(--bottomnav-h) + 18px + env(safe-area-inset-bottom));
  width: 56px; height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: 0;
  font-size: 26px;
  display: inline-flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 18px rgba(30,64,175,.4);
  z-index: 80;
  text-decoration: none;
  transition: transform .15s, box-shadow .15s;
}
.fab:hover, .fab:focus {
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 10px 22px rgba(30,64,175,.5);
  color: #fff; text-decoration: none;
}
.fab:active { transform: translateY(0) scale(.97); }
@media (min-width: 721px) {
  .fab { bottom: 24px; right: 24px; }    /* no bottomnav on desktop */
}

/* --- Filter chips (horizontal-scroll on mobile) ---------------- */
.chip-row {
  display: flex; gap: 8px;
  overflow-x: auto;
  padding: 4px 2px 8px;
  margin: 0 -12px;
  padding-left: 12px; padding-right: 12px;
  scroll-snap-type: x proximity;
  scrollbar-width: thin;
}
.chip-row::-webkit-scrollbar { height: 4px; }
.chip-row::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 2px; }
.chip {
  flex: 0 0 auto;
  display: inline-flex; align-items: center; gap: 4px;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  background: var(--bg-card);
  border-radius: var(--radius-pill);
  color: var(--text-soft);
  font-size: 13px;
  text-decoration: none;
  white-space: nowrap;
  min-height: 34px;
}
.chip:hover { text-decoration: none; }
.chip.is-active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* --- Collapsible section (form sections that fold up) ---------- */
.section {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
  overflow: hidden;
}
.section-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  min-height: var(--touch-min);
}
.section-head:hover { background: var(--bg); }
.section-head-icon {
  font-size: 20px;
  flex-shrink: 0;
}
.section-head-title {
  flex: 1;
  font-size: 15px;
}
.section-head-meta {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-muted);
  margin-right: 4px;
}
.section-toggle {
  font-size: 14px;
  color: var(--text-muted);
  transition: transform .2s;
}
.section[open] .section-toggle {
  transform: rotate(90deg);
}
.section-body {
  padding: 0 16px 16px;
  border-top: 1px solid var(--border);
  padding-top: 14px;
}
/* When using <details> element */
details.section > summary {
  list-style: none;
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  cursor: pointer;
  user-select: none;
  font-weight: 600;
  min-height: var(--touch-min);
}
details.section > summary::-webkit-details-marker { display: none; }
details.section[open] > summary { border-bottom: 1px solid var(--border); }
details.section > summary:hover { background: var(--bg); }
details.section > .section-body {
  padding: 14px 16px 16px;
  border-top: 0;
}

/* --- Improved buttons (touch-friendly) ------------------------- */
.btn {
  min-height: var(--touch-min);    /* override earlier 40px */
}
.btn-sm { min-height: 36px; }
.btn-block { display: flex; }
.btn-icon-only {
  width: var(--touch-min);
  padding: 0;
  justify-content: center;
}

/* Tap highlight removal (iOS) */
button, a, [data-action] {
  -webkit-tap-highlight-color: rgba(0,0,0,.05);
}

/* --- Improved form inputs -------------------------------------- */
.input, .select, .textarea {
  min-height: var(--touch-min);
  font-size: 16px;                 /* 16px prevents iOS zoom-on-focus */
}
@media (min-width: 721px) {
  .input, .select, .textarea { font-size: 15px; }   /* slightly smaller on desktop */
}

/* --- Compact topbar on mobile ---------------------------------- */
@media (max-width: 600px) {
  .topbar-inner { padding: 0 8px; gap: 6px; }
  .brand-mark { font-size: 24px; }
}

/* --- Card touch feedback --------------------------------------- */
.module-card:active,
.picker-card:active {
  transform: translateY(0) scale(.98);
}

/* --- Pull-to-refresh hint area (placeholder) ------------------- */
.pull-refresh-hint {
  text-align: center;
  padding: 8px;
  color: var(--text-muted);
  font-size: 12px;
}

/* --- Pending sync badge (on bottom nav icon) ------------------- */
.bottomnav-badge {
  position: absolute;
  top: -4px; right: -10px;
  min-width: 18px; height: 18px;
  padding: 0 5px;
  background: var(--danger);
  color: #fff;
  border-radius: 9px;
  font-size: 10px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  line-height: 1;
  border: 2px solid var(--bg-card);
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}

/* --- Pending photo state (queued for upload) ------------------- */
.dr-photo-thumb-wrap.is-pending .dr-photo-thumb {
  opacity: .8;
  filter: grayscale(.25);
}
.dr-photo-pending {
  position: absolute;
  bottom: 4px; right: 4px;
  background: rgba(245, 158, 11, .95);
  color: #fff;
  font-size: 13px;
  width: 22px; height: 22px;
  border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  animation: pulse-bg 1.5s ease-in-out infinite;
}
@keyframes pulse-bg {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.1); }
}
.dr-photo-thumb-wrap.is-synced {
  outline: 2px solid var(--success);
  outline-offset: 1px;
}

/* --- Offline banner -------------------------------------------- */
.offline-banner {
  position: fixed;
  top: 0; left: 0; right: 0;
  background: #f59e0b;
  color: #fff;
  text-align: center;
  padding: 6px 12px;
  font-size: 13px;
  z-index: 9990;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  animation: slide-down .25s ease;
}
@keyframes slide-down { from { transform: translateY(-100%); } to { transform: translateY(0); } }
.offline-banner.is-leaving { animation: slide-up .25s ease forwards; }
@keyframes slide-up { from { transform: translateY(0); } to { transform: translateY(-100%); } }
.offline-banner span { font-weight: 600; }
html.is-offline .topbar { top: 28px; }    /* push topbar down */
html.is-offline .app-main { padding-top: 32px; }

/* --- Manual-sync upload reminder ------------------------------- */
.upload-reminder {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--bottomnav-h) + 12px + env(safe-area-inset-bottom));
  background: linear-gradient(135deg, var(--success), #059669);
  color: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 12px 14px;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  z-index: 9994;
  max-width: 460px; margin: 0 auto;
  transform: translateY(140%);
  transition: transform .3s cubic-bezier(.34,1.4,.64,1);
}
.upload-reminder.show { transform: translateY(0); }
.upload-reminder-text { flex: 1; min-width: 140px; font-size: 14px; }
.upload-reminder-go {
  background: #fff; color: #047857;
  padding: 8px 14px; border-radius: var(--radius-pill);
  font-size: 13px; font-weight: 700; text-decoration: none;
  white-space: nowrap; border: 0; cursor: pointer; font-family: inherit;
}
.upload-reminder-go:hover { text-decoration: none; opacity: .92; }
.upload-reminder-details {
  color: rgba(255,255,255,.92); font-size: 12px; text-decoration: underline;
  white-space: nowrap; opacity: .9;
}
.upload-reminder-details:hover { opacity: 1; }
.upload-reminder-x {
  background: transparent; border: 0; color: rgba(255,255,255,.85);
  font-size: 20px; cursor: pointer; padding: 0 4px; line-height: 1;
}
@media (min-width: 721px) {
  .upload-reminder { left: auto; right: 24px; bottom: 24px; max-width: 380px; }
}

/* --- PWA install hint card ------------------------------------- */
.pwa-install-hint {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--bottomnav-h) + 12px + env(safe-area-inset-bottom));
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 14px;
  display: flex; align-items: center; gap: 12px;
  z-index: 100;
  max-width: 420px;
  margin: 0 auto;
  animation: slide-up-in .3s ease;
}
@keyframes slide-up-in { from { transform: translateY(40px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.pwa-install-hint-icon { font-size: 32px; }
.pwa-install-hint-text { flex: 1; line-height: 1.3; }
.pwa-install-hint-text strong { display: block; font-size: 14px; }
.pwa-install-hint-text small { color: var(--text-muted); font-size: 12px; }
.pwa-install-hint [data-pwa-install] {
  padding: 8px 16px;
  background: var(--primary);
  color: #fff;
  border: 0; border-radius: var(--radius-pill);
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.pwa-install-hint [data-pwa-dismiss] {
  background: transparent; border: 0; color: var(--text-muted);
  font-size: 22px; cursor: pointer; padding: 4px 8px;
}
@media (min-width: 721px) {
  .pwa-install-hint { bottom: 24px; right: 24px; left: auto; max-width: 360px; }
}

/* --- Permission chips (role × action toggles) ------------------ */
.perm-role {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
  background: var(--bg);
}
.perm-role-name {
  font-weight: 600; font-size: 14px;
  margin-bottom: 10px;
  display: flex; align-items: center; gap: 8px;
}
.perm-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.perm-chip { position: relative; }
.perm-chip input {
  position: absolute; opacity: 0; width: 0; height: 0;
}
.perm-chip label {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-pill);
  cursor: pointer;
  font-size: 13px; font-weight: 500;
  min-height: 38px;
  user-select: none;
  background: var(--bg-card);
  color: var(--text-soft);
  transition: all .12s;
}
.perm-chip label::before { content: "○"; font-size: 12px; opacity: .5; }
.perm-chip input:checked + label {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.perm-chip input:checked + label::before { content: "✓"; opacity: 1; }
.perm-chip input:focus-visible + label {
  box-shadow: 0 0 0 3px rgba(59,130,246,.3);
}

/* --- Data card (mobile-first list item) ------------------------ */
.data-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  margin-bottom: 10px;
  display: block;
  color: var(--text);
  text-decoration: none;
  transition: transform .15s, box-shadow .15s;
}
.data-card:hover {
  text-decoration: none;
  box-shadow: var(--shadow);
}
.data-card:active { transform: scale(.99); }
.data-card-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px; margin-bottom: 8px;
}
.data-card-title {
  font-size: 16px; font-weight: 600;
  margin: 0;
}
.data-card-meta {
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  font-size: 13px; color: var(--text-soft);
}
.data-card-meta-item {
  display: inline-flex; align-items: center; gap: 4px;
}

/* ================================================================
   ANIMATIONS + shared inline components
   ================================================================ */

/* --- Reusable inline alert (used by form/view/drafts) ---------- */
.alert-inline {
  padding: 12px 14px;
  border-radius: var(--radius);
  margin-bottom: 14px;
  font-size: 14px;
  border: 1px solid var(--border);
}
.alert-inline-success { background: rgba(16,185,129,.08); border-color: rgba(16,185,129,.3); color: #047857; }
.alert-inline-error   { background: rgba(239,68,68,.08);  border-color: rgba(239,68,68,.3);  color: #b91c1c; }
.alert-inline-warning { background: rgba(245,158,11,.08); border-color: rgba(245,158,11,.3); color: #b45309; }
[data-theme="dark"] .alert-inline-success { color: #6ee7b7; }
[data-theme="dark"] .alert-inline-error   { color: #fca5a5; }
[data-theme="dark"] .alert-inline-warning { color: #fcd34d; }

/* --- Entrance: fade + rise ------------------------------------- */
@keyframes cp-fade-up {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
.animate-in { animation: cp-fade-up .35s cubic-bezier(.22,.61,.36,1) both; }

/* Stagger children of a grid/list (cards appear in sequence) */
.module-grid > *,
.dr-list-mobile > *,
.picker-grid > * {
  animation: cp-fade-up .4s cubic-bezier(.22,.61,.36,1) both;
}
.module-grid > *:nth-child(1),.dr-list-mobile > *:nth-child(1),.picker-grid > *:nth-child(1){animation-delay:.02s}
.module-grid > *:nth-child(2),.dr-list-mobile > *:nth-child(2),.picker-grid > *:nth-child(2){animation-delay:.06s}
.module-grid > *:nth-child(3),.dr-list-mobile > *:nth-child(3),.picker-grid > *:nth-child(3){animation-delay:.10s}
.module-grid > *:nth-child(4),.dr-list-mobile > *:nth-child(4),.picker-grid > *:nth-child(4){animation-delay:.14s}
.module-grid > *:nth-child(5),.dr-list-mobile > *:nth-child(5),.picker-grid > *:nth-child(5){animation-delay:.18s}
.module-grid > *:nth-child(6),.dr-list-mobile > *:nth-child(6),.picker-grid > *:nth-child(6){animation-delay:.22s}
.module-grid > *:nth-child(n+7),.dr-list-mobile > *:nth-child(n+7),.picker-grid > *:nth-child(n+7){animation-delay:.26s}

/* --- Pop in: newly added row (collaborative add) --------------- */
@keyframes cp-pop {
  0%   { opacity: 0; transform: scale(.92) translateY(6px); }
  60%  { opacity: 1; transform: scale(1.02); }
  100% { transform: scale(1); }
}
.animate-pop {
  animation: cp-pop .32s cubic-bezier(.34,1.56,.64,1) both;
}

/* --- Collapse out: row removal --------------------------------- */
@keyframes cp-collapse {
  from { opacity: 1; transform: translateX(0); max-height: 200px; }
  to   { opacity: 0; transform: translateX(-12px); max-height: 0; margin: 0; padding-top: 0; padding-bottom: 0; }
}
.animate-out { animation: cp-collapse .22s ease forwards; overflow: hidden; }

/* --- Page main: gentle fade on load ---------------------------- */
@keyframes cp-page-in { from { opacity: 0; } to { opacity: 1; } }
.app-main { animation: cp-page-in .25s ease both; }

/* --- Button press ripple-ish feedback -------------------------- */
.btn { position: relative; overflow: hidden; }
.btn:active { transform: scale(.97); }
.btn-primary:active { transform: scale(.97); }

/* --- Module card hover lift (enhance existing) ----------------- */
.module-card { transition: transform .18s cubic-bezier(.22,.61,.36,1), box-shadow .18s, border-color .18s; }

/* --- FAB attention pulse on first paint ------------------------ */
@keyframes cp-fab-in { from { opacity: 0; transform: scale(.4) translateY(20px); } to { opacity: 1; transform: scale(1) translateY(0); } }
.fab { animation: cp-fab-in .4s cubic-bezier(.34,1.56,.64,1) both .2s; }

/* --- Global sync pill (queue drain progress) ------------------- */
.sync-pill {
  position: fixed;
  left: 50%; transform: translateX(-50%) translateY(-120%);
  top: calc(8px + env(safe-area-inset-top));
  background: var(--primary); color: #fff;
  padding: 8px 16px; border-radius: 999px;
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; gap: 8px;
  box-shadow: var(--shadow-lg);
  z-index: 9995;
  opacity: 0;
  transition: transform .3s cubic-bezier(.34,1.4,.64,1), opacity .3s;
  pointer-events: none;
}
.sync-pill.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.sync-pill.is-done { background: var(--success); }
.sync-pill.is-fail { background: var(--danger); }
.sync-pill.is-busy .sync-pill-spin { display: inline-block; animation: cp-spin-pill .8s linear infinite; }
@keyframes cp-spin-pill { to { transform: rotate(360deg); } }

/* --- Skeleton loader (optional use) ---------------------------- */
.skeleton {
  background: linear-gradient(90deg, var(--bg) 25%, var(--border) 37%, var(--bg) 63%);
  background-size: 400% 100%;
  animation: cp-shimmer 1.4s ease infinite;
  border-radius: var(--radius-sm);
}
@keyframes cp-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* --- Toast: add a subtle slide+scale ---------------------------- */
.toast { will-change: transform, opacity; }

/* --- Respect reduced-motion preference ------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
    scroll-behavior: auto !important;
  }
}

/* ===== In-app image lightbox (works in PWA standalone — has its own ✕ / back) ===== */
html.lb-open { overflow: hidden; }
.lb-ov {
  position: fixed; inset: 0; z-index: 9998; display: none;
  align-items: center; justify-content: center;
  background: rgba(7, 11, 20, .94); -webkit-tap-highlight-color: transparent;
}
.lb-ov.show { display: flex; animation: lbIn .15s ease; }
@keyframes lbIn { from { opacity: 0; } to { opacity: 1; } }
.lb-img {
  max-width: 100vw; max-height: 100vh; max-height: 100dvh; object-fit: contain;
  touch-action: none; will-change: transform; transform-origin: center center;
  user-select: none; -webkit-user-select: none; -webkit-user-drag: none;
}
.lb-close {
  position: fixed; top: calc(env(safe-area-inset-top, 0px) + 10px); right: 14px; z-index: 2;
  width: 46px; height: 46px; border-radius: 50%; border: 0; cursor: pointer;
  background: rgba(255, 255, 255, .18); color: #fff; font-size: 21px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px); box-shadow: 0 2px 10px rgba(0, 0, 0, .4);
}
.lb-close:hover { background: rgba(255, 255, 255, .3); }
.lb-hint {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom, 0px) + 12px); transform: translateX(-50%);
  color: #cbd5e1; font-size: 12px; background: rgba(0, 0, 0, .45); padding: 6px 13px; border-radius: 999px;
  pointer-events: none; max-width: 92vw; text-align: center; white-space: nowrap;
}
@media (max-width: 480px) { .lb-hint { white-space: normal; } }
@media print { .lb-ov { display: none !important; } }

