:root {
  --esps-blue: #1a56db;
  --esps-dark: #0f172a;
  --esps-gray: #64748b;
  --esps-bg: #f1f5f9;
  --esps-white: #ffffff;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--esps-bg);
  color: var(--esps-dark);
  line-height: 1.6;
}

.app-header {
  background: linear-gradient(135deg, var(--esps-blue), #0f2a6b);
  color: var(--esps-white);
  padding: 3rem 2rem;
  text-align: center;
}

.app-header h1 {
  font-size: 2rem;
  letter-spacing: 0.05em;
}

.subtitle {
  font-size: 1.1rem;
  opacity: 0.9;
  margin-top: 0.25rem;
}

.tagline {
  font-size: 0.95rem;
  opacity: 0.75;
  margin-top: 0.25rem;
}

/* Legacy landing-page container (not used by the SPA). */
.landing-main {
  max-width: 720px;
  margin: 2rem auto;
  padding: 0 1rem;
}

#api-status {
  background: var(--esps-dark);
  color: #4ade80;
  padding: 1rem;
  border-radius: 8px;
  overflow-x: auto;
  margin-top: 0.5rem;
}

/* ---- Header / session ---- */
.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.auth-area {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.user-chip {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.2;
}

.user-name {
  font-weight: 600;
}

.user-role {
  font-size: 0.8rem;
  opacity: 0.8;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.section-title {
  margin-top: 1.5rem;
  font-size: 1.15rem;
}

/* ---- Buttons ---- */
.btn {
  display: inline-block;
  padding: 0.5rem 1.1rem;
  border-radius: 6px;
  border: 1px solid transparent;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s, opacity 0.15s;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--esps-blue);
  color: var(--esps-white);
}

.btn-primary:hover:not(:disabled) {
  background: #1244b3;
}

.btn-outline {
  background: transparent;
  color: var(--esps-white);
  border-color: rgba(255, 255, 255, 0.6);
}

.btn-outline:hover {
  background: rgba(255, 255, 255, 0.12);
}

.btn-block {
  width: 100%;
}

/* ---- Panels ---- */
.panel {
  padding: 1rem 1.25rem;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
  background: var(--esps-white);
}

.panel-muted {
  border-left: 4px solid var(--esps-gray);
}

.panel-success {
  border-left: 4px solid #16a34a;
}

.panel h3 {
  margin-bottom: 0.5rem;
}

.panel p {
  margin-bottom: 0.25rem;
}

/* ---- Auth page ---- */
.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--esps-dark), #1e3a8a);
  padding: 1rem;
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--esps-white);
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.35);
}

.auth-brand {
  text-align: center;
  margin-bottom: 1.5rem;
}

.auth-brand h1 {
  font-size: 2.2rem;
  letter-spacing: 0.12em;
  color: var(--esps-blue);
}

.auth-brand .subtitle {
  color: var(--esps-dark);
  margin-top: 0;
}

.auth-brand .tagline {
  color: var(--esps-gray);
  font-size: 0.85rem;
}

.auth-form .form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
  color: var(--esps-dark);
}

.form-group input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
}

.form-group input:focus {
  outline: 2px solid var(--esps-blue);
  outline-offset: 1px;
  border-color: transparent;
}

.auth-error {
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecaca;
  padding: 0.6rem 0.8rem;
  border-radius: 6px;
  font-size: 0.875rem;
  margin-bottom: 1rem;
}

.auth-hint {
  margin-top: 1rem;
  text-align: center;
  font-size: 0.8rem;
  color: var(--esps-gray);
}

/* ---- App shell layout ---- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 250px;
  background: var(--esps-dark);
  color: #cbd5e1;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 50;
  overflow-y: auto;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 1.1rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--esps-blue);
  color: #fff;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
}

.brand-text {
  line-height: 1.1;
}
.brand-text strong {
  color: #fff;
  letter-spacing: 0.06em;
}
.brand-text small {
  color: #94a3b8;
  font-size: 0.72rem;
}

.sidebar-nav {
  flex: 1;
  padding: 0.6rem 0.5rem 1rem;
}

.nav-group-label {
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #64748b;
  padding: 0.9rem 0.75rem 0.3rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  background: none;
  border: none;
  color: #cbd5e1;
  text-align: left;
  font-size: 0.9rem;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.nav-item.active {
  background: var(--esps-blue);
  color: #fff;
}

.nav-icon {
  width: 18px;
  text-align: center;
  opacity: 0.85;
}

.sidebar-footer {
  padding: 1rem;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.sidebar-user {
  align-items: flex-start !important;
}

/* ---- Main area ---- */
.app-main {
  flex: 1;
  margin-left: 250px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--esps-white);
  border-bottom: 1px solid #e2e8f0;
  padding: 0.75rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

.topbar-title {
  font-weight: 600;
  font-size: 1.05rem;
}

.topbar-spacer {
  flex: 1;
}

.topbar-user {
  font-size: 0.85rem;
  color: var(--esps-gray);
}

.icon-btn {
  background: none;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  width: 36px;
  height: 36px;
  font-size: 1.1rem;
  cursor: pointer;
  display: none;
}

.content {
  padding: 1.5rem;
  flex: 1;
}

.btn-sm {
  padding: 0.35rem 0.8rem;
  font-size: 0.85rem;
}

/* ---- Dashboard ---- */
.dashboard-head h1 {
  font-size: 1.5rem;
}

.muted {
  color: var(--esps-gray);
}

.muted.small,
.small {
  font-size: 0.82rem;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin: 1.25rem 0;
}

.kpi-card {
  background: var(--esps-white);
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  padding: 1.1rem 1.25rem;
}

.kpi-value {
  font-size: 1.9rem;
  font-weight: 700;
  color: var(--esps-blue);
}

.kpi-label {
  font-weight: 600;
  margin-top: 0.15rem;
}

.kpi-hint {
  font-size: 0.8rem;
  color: var(--esps-gray);
}

.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.panel-title {
  font-size: 1.05rem;
  margin-bottom: 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid #f1f5f9;
}

.quick-links {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.quick-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--esps-bg);
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 0.6rem 0.9rem;
  font-size: 0.9rem;
  cursor: pointer;
  color: var(--esps-dark);
  transition: border-color 0.15s, background 0.15s;
}

.quick-link:hover {
  border-color: var(--esps-blue);
  background: #fff;
}

.quick-link-arrow {
  color: var(--esps-blue);
}

.status-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

.dot-ok {
  background: #16a34a;
}

.dot-bad {
  background: #dc2626;
}

/* ---- Placeholder ---- */
.panel-placeholder {
  text-align: center;
  padding: 3rem 1.5rem;
}

.panel-placeholder h2 {
  font-size: 1.4rem;
}

.placeholder-desc {
  max-width: 520px;
  margin: 0.75rem auto;
  color: var(--esps-gray);
}

.placeholder-route {
  font-size: 0.8rem;
  color: var(--esps-gray);
}

.placeholder-route code {
  background: var(--esps-bg);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.placeholder-icon {
  font-size: 2rem;
  margin-top: 0.75rem;
}

/* ---- Responsive ---- */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    width: 240px;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .app-main {
    margin-left: 0;
  }
  .icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .dashboard-grid {
    grid-template-columns: 1fr;
  }
}

/* ---- Page head / toolbar ---- */
.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.page-head h1 {
  font-size: 1.4rem;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.toolbar .input {
  flex: 1;
  min-width: 220px;
}

.toolbar .input-select {
  flex: 0 0 auto;
  min-width: 0;
  width: 180px;
}

.input {
  padding: 0.5rem 0.75rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 0.95rem;
  background: var(--esps-white);
}

.input:focus {
  outline: 2px solid var(--esps-blue);
  outline-offset: 1px;
  border-color: transparent;
}

textarea.input {
  font-family: inherit;
  resize: vertical;
}

.btn-outline-dark {
  background: transparent;
  color: var(--esps-dark);
  border-color: #cbd5e1;
}

.btn-outline-dark:hover {
  background: var(--esps-bg);
}

.btn-danger {
  background: #dc2626;
  color: #fff;
}

.btn-danger:hover {
  background: #b91c1c;
}

/* ---- Table ---- */
.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.table th {
  text-align: left;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--esps-gray);
  padding: 0.6rem 0.75rem;
  border-bottom: 2px solid #e2e8f0;
}

.table td {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}

.table tbody tr:hover {
  background: #f8fafc;
}

.ta-right {
  text-align: right;
}

.mono {
  font-family: Consolas, Monaco, monospace;
  font-size: 0.85rem;
}

.table-error {
  color: #b91c1c;
}

.text-danger {
  color: #b91c1c;
  font-weight: 600;
}

.text-success {
  color: #15803d;
  font-weight: 600;
}

.mrp-preview {
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
  padding: 0.75rem;
  background: #f8fafc;
  min-height: 3rem;
}

.mrp-preview .table {
  margin-bottom: 0;
}

.table-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 0.75rem;
  gap: 1rem;
  flex-wrap: wrap;
}

.pagination {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.pagination-info {
  font-size: 0.85rem;
  color: var(--esps-gray);
}

/* ---- Tabs ---- */
.tabs {
  display: flex;
  gap: 0.25rem;
  border-bottom: 2px solid #e2e8f0;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}

.tab-btn {
  padding: 0.55rem 1.1rem;
  border: none;
  background: transparent;
  color: var(--esps-gray);
  font-size: 0.95rem;
  font-weight: 600;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
}

.tab-btn:hover {
  color: var(--esps-dark);
}

.tab-btn.active {
  color: var(--esps-blue);
  border-bottom-color: var(--esps-blue);
}

/* ---- Badges ---- */
.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
}

.badge-ok {
  background: #dcfce7;
  color: #15803d;
}

.badge-muted {
  background: #e2e8f0;
  color: #475569;
}

.badge-warn {
  background: #fef9c3;
  color: #a16207;
}

.badge-danger {
  background: #fee2e2;
  color: #b91c1c;
}

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 2rem 1rem;
  z-index: 100;
  overflow-y: auto;
}

.modal {
  background: var(--esps-white);
  border-radius: 10px;
  width: 100%;
  max-width: 620px;
  box-shadow: 0 20px 50px rgba(15, 23, 42, 0.3);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid #e2e8f0;
}

.modal-head h3 {
  font-size: 1.05rem;
}

.modal-form {
  padding: 1.25rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.modal-form .form-group {
  margin-bottom: 0.75rem;
}

.form-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.form-check input {
  width: auto;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 1rem;
}

.modal-close {
  border: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--esps-gray);
}

/* ---- Repeating input rows (formula / QC items) ---- */
.item-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.5rem;
}

.item-row > .input {
  flex: 1;
}

.item-row .item-remove {
  flex: 0 0 auto;
}

.item-row-stack > .item-name {
  flex: 2;
}

/* ---- Responsive ---- */
@media (max-width: 640px) {
  .form-row {
    grid-template-columns: 1fr;
  }
  .table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}

/* ---- Analytics charts (Phase 14) ---- */
.chart-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr;
  gap: 1rem;
  margin: 1.25rem 0;
}

.chart-panel {
  background: var(--esps-white);
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  padding: 1rem 1.25rem;
  min-width: 0;
}

.chart-panel h3 {
  margin: 0 0 0.75rem;
  font-size: 1rem;
}

.chart-canvas-wrap {
  position: relative;
  height: 260px;
}

.chart-toggle {
  display: inline-flex;
  gap: 0.25rem;
  margin-bottom: 0.75rem;
}

.chart-toggle .btn.active {
  background: var(--esps-blue);
  border-color: var(--esps-blue);
  color: var(--esps-white);
}

@media (max-width: 900px) {
  .chart-grid {
    grid-template-columns: 1fr;
  }
}

/* ---- Executive dashboard (Phase 14b) ---- */
.widget-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
  margin: 1.25rem 0;
}

.widget-card {
  background: var(--esps-white);
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  padding: 1rem 1.25rem;
  min-width: 0;
}

.widget-card h3 {
  font-size: 0.95rem;
  margin: 0 0 0.6rem;
  padding-bottom: 0.45rem;
  border-bottom: 1px solid #f1f5f9;
}

.widget-body {
  font-size: 0.88rem;
}

.mini-list {
  list-style: none;
}

.mini-list li {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid #f8fafc;
  font-size: 0.85rem;
}

.mini-list li:last-child {
  border-bottom: none;
}

.bottom-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1rem;
  margin: 1.25rem 0;
}

.ai-assistant {
  display: flex;
  flex-direction: column;
}

.ai-log {
  flex: 1;
  min-height: 90px;
  max-height: 220px;
  overflow-y: auto;
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
}

.ai-msg {
  margin: 0.3rem 0;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
}

.ai-msg.user {
  background: var(--esps-blue);
  color: #fff;
  margin-left: 1.5rem;
}

.ai-msg.bot {
  background: var(--esps-bg);
  color: var(--esps-dark);
  margin-right: 1.5rem;
}

.ai-form {
  display: flex;
  gap: 0.5rem;
}

.ai-form .input {
  flex: 1;
  min-width: 0;
}

.notification-list {
  list-style: none;
  font-size: 0.85rem;
}

.notification-list li {
  padding: 0.4rem 0;
  border-bottom: 1px solid #f1f5f9;
}

.notification-list li:last-child {
  border-bottom: none;
}

@media (max-width: 900px) {
  .widget-grid,
  .bottom-grid,
  .kpi-grid {
    grid-template-columns: 1fr;
  }
}


