:root {
  --navy: #0f2942;
  --navy-light: #1a3d5e;
  --teal: #0d9488;
  --teal-dark: #0f766e;
  --teal-soft: #ccfbf1;
  --bg: #f1f5f9;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --emerald: #059669;
  --amber: #d97706;
  --rose: #dc2626;
  --blue: #2563eb;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(15, 41, 66, 0.06);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font-family: inherit; }

/* Header */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: linear-gradient(135deg, var(--navy) 0%, #133856 100%);
  color: #fff;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  box-shadow: 0 2px 12px rgba(15, 41, 66, 0.22);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand img { width: 34px; height: 34px; border-radius: 9px; }
.brand-title { font-weight: 800; font-size: 16px; line-height: 1.15; }
.brand-sub { font-size: 11px; color: #99f6e4; opacity: 0.92; }

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid rgba(255,255,255,0.2);
  background: rgba(255,255,255,0.1);
  color: #fff;
  cursor: pointer;
  transition: 0.15s;
}
.pill:hover { background: rgba(255,255,255,0.2); }
.pill.online { border-color: #34d399; color: #a7f3d0; }
.pill.offline { border-color: #fbbf24; color: #fde68a; }
.pill.sync-btn {
  background: var(--teal);
  border-color: #2dd4bf;
  color: #fff;
}
.pill.sync-btn:hover { background: var(--teal-dark); }
.pill.sync-btn.dirty {
  background: #f59e0b;
  border-color: #fde68a;
  color: #1e293b;
}

/* Desktop Main Tabs */
.main-nav {
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  position: sticky;
  top: 56px;
  z-index: 30;
}
.nav-tab {
  border: none;
  background: transparent;
  padding: 9px 18px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 14px;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: 0.15s;
}
.nav-tab:hover { background: #f1f5f9; color: var(--navy); }
.nav-tab.active {
  background: var(--navy);
  color: #fff;
}
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 19px;
  height: 19px;
  padding: 0 5px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: var(--rose);
  color: #fff;
}

/* Container */
.container {
  max-width: 1240px;
  margin: 0 auto;
  padding: 16px 14px 90px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.kpi-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  border-left: 4px solid var(--teal);
}
.kpi-card.blue { border-left-color: var(--blue); }
.kpi-card.emerald { border-left-color: var(--emerald); }
.kpi-card.amber { border-left-color: var(--amber); }
.kpi-card.rose { border-left-color: var(--rose); }
.kpi-label { font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.kpi-value { font-size: 24px; font-weight: 800; color: var(--navy); line-height: 1.2; }
.kpi-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* Charts Grid */
.charts-grid {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  margin-bottom: 14px;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.card-title {
  font-size: 15px;
  font-weight: 800;
  color: var(--navy);
  margin: 0;
}

/* Sub-tabs inside BMC */
.subtabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.subtab {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--muted);
  padding: 7px 13px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.subtab.active {
  background: var(--teal);
  border-color: var(--teal);
  color: #fff;
}

/* Filters & Toolbar */
.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 12px;
}
.input, .select, .textarea {
  padding: 8px 11px;
  border: 1px solid #cbd5e1;
  border-radius: 9px;
  font-size: 13px;
  background: #fff;
  color: var(--text);
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(13, 148, 136, 0.14);
}
.toolbar .input { min-width: 190px; flex: 1; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 13px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 700;
  border: 1px solid transparent;
  cursor: pointer;
  transition: 0.15s;
}
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:hover { background: var(--teal-dark); }
.btn-navy { background: var(--navy); color: #fff; }
.btn-navy:hover { background: var(--navy-light); }
.btn-outline { background: #fff; border-color: #cbd5e1; color: var(--text); }
.btn-outline:hover { background: #f8fafc; }
.btn-danger { background: #fee2e2; color: var(--rose); border-color: #fecaca; }
.btn-danger:hover { background: #fecaca; }
.btn-sm { padding: 5px 9px; font-size: 12px; border-radius: 7px; }

/* Tables */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th {
  background: #f8fafc;
  color: var(--muted);
  font-weight: 700;
  text-align: left;
  padding: 10px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 10px 10px;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fafc; }

.num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.remain-ok { color: var(--emerald); font-weight: 800; }
.remain-zero { color: var(--muted); }
.cull-text { color: var(--rose); font-weight: 700; }

.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}
.tag-teal { background: var(--teal-soft); color: #115e59; }
.tag-blue { background: #dbeafe; color: #1e40af; }
.tag-amber { background: #fef3c7; color: #92400e; }
.tag-rose { background: #fee2e2; color: #991b1b; }
.tag-gray { background: #e2e8f0; color: #334155; }

.row-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

/* Traceability Flow */
.trace-flow {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-top: 12px;
}
.trace-col {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}
.trace-col h4 {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--navy);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.trace-node {
  background: #fff;
  border: 1px solid var(--border);
  border-left: 4px solid var(--teal);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 8px;
  font-size: 12.5px;
  cursor: pointer;
}
.trace-node.selected {
  border-color: var(--navy);
  box-shadow: 0 0 0 2px rgba(15, 41, 66, 0.2);
}
.trace-node.stage-g45 { border-left-color: var(--blue); }
.trace-node.stage-bm { border-left-color: var(--amber); }
.trace-node.stage-nau { border-left-color: var(--emerald); }
.trace-node .node-title { font-weight: 800; color: var(--navy); margin-bottom: 3px; }
.trace-node .node-meta { color: var(--muted); font-size: 11.5px; line-height: 1.45; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 100;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}
.modal-head {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-head h3 { margin: 0; font-size: 16px; color: var(--navy); }
.modal-body { padding: 16px 18px; }
.modal-foot {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12px; font-weight: 700; color: var(--navy); }
.field .hint { font-size: 11px; color: var(--muted); }

/* Toast */
.toast {
  position: fixed;
  bottom: 76px;
  right: 16px;
  z-index: 200;
  background: var(--navy);
  color: #fff;
  padding: 11px 16px;
  border-radius: 11px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 10px 25px rgba(0,0,0,0.2);
  display: none;
  max-width: 380px;
}
.toast.show { display: block; }
.toast.error { background: var(--rose); }
.toast.success { background: var(--emerald); }

.empty-state {
  text-align: center;
  padding: 28px 16px;
  color: var(--muted);
  font-size: 13px;
}

.dev-only, .dev-block, .admin-only, .admin-block, .editor-only, .editor-block { display: none; }

/* Developer: full features */
body.role-developer .dev-only,
body.role-developer .admin-only,
body.role-developer .editor-only { display: inline-flex; }
body.role-developer .dev-block,
body.role-developer .admin-block,
body.role-developer .editor-block { display: block; }

/* Admin: management + trace + approval */
body.role-admin .admin-only,
body.role-admin .editor-only { display: inline-flex; }
body.role-admin .admin-block,
body.role-admin .editor-block { display: block; }

/* User: BMC view + farm entry */
body.role-user .editor-only { display: inline-flex; }
body.role-user .editor-block { display: block; }

.pending-card {
  border: 1px solid #fde68a;
  background: #fffbeb;
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* Mandatory Login Gate */
.login-gate {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: linear-gradient(145deg, #0b1f33 0%, #0f2942 50%, #0d5c63 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-gate.hidden { display: none; }
.login-card {
  background: #ffffff;
  width: 100%;
  max-width: 400px;
  border-radius: 18px;
  padding: 28px 24px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}
.login-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}
.login-brand img { width: 46px; height: 46px; border-radius: 12px; }
.login-brand h2 { margin: 0; font-size: 19px; color: var(--navy); }
.login-brand p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.login-error {
  color: var(--rose);
  font-size: 12.5px;
  font-weight: 700;
  min-height: 18px;
  margin-bottom: 8px;
}
body.locked .topbar,
body.locked .main-nav,
body.locked .container { display: none; }

/* Under Development Card (Nauplius & PL) */
.wip-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 48px 24px;
  text-align: center;
  max-width: 560px;
  margin: 28px auto;
  box-shadow: var(--shadow);
}
.wip-icon { font-size: 46px; margin-bottom: 12px; }
.wip-card h2 { margin: 0 0 8px; color: var(--navy); font-size: 20px; }
.wip-card p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.6; }

/* Mobile Responsive (PWA bottom bar) */
@media (max-width: 860px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .charts-grid { grid-template-columns: 1fr; }
  .trace-flow { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .main-nav {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    border-top: 1px solid var(--border);
    border-bottom: none;
    padding: 6px 8px env(safe-area-inset-bottom, 6px);
    box-shadow: 0 -2px 10px rgba(0,0,0,0.08);
  }
  .nav-tab {
    flex: 1;
    justify-content: center;
    padding: 10px 6px;
    font-size: 12.5px;
  }
}
