:root {
  --primary: #1e3a8a;
  --primary-light: #3b82f6;
  --primary-dark: #0f172a;
  --primary-subtle: #eff6ff;
  --gold: #d97706;
  --gold-light: #fbbf24;
  --gold-subtle: #fef3c7;
  --success: #10b981;
  --success-subtle: #ecfdf5;
  --warning: #f59e0b;
  --warning-subtle: #fffbeb;
  --danger: #ef4444;
  --danger-subtle: #fef2f2;
  --neutral-bg: #f8fafc;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --border-focus: #3b82f6;
  --text-main: #0f172a;
  --text-muted: #64748b;
  --text-light: #94a3b8;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-full: 9999px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --font-sans: 'Prompt', 'Kanit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--neutral-bg);
  color: var(--text-main);
  line-height: 1.5;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

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

/* Sidebar */
.app-sidebar {
  width: 260px;
  background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
  color: #fff;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 50;
  transition: var(--transition);
  box-shadow: 2px 0 10px rgba(0,0,0,0.1);
}

.brand-section {
  padding: 24px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo-icon {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #f59e0b, #d97706);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  color: white;
  box-shadow: 0 4px 10px rgba(217, 119, 6, 0.4);
}

.brand-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.3px;
  line-height: 1.2;
}

.brand-subtitle {
  font-size: 11px;
  color: #94a3b8;
  font-weight: 400;
  margin-top: 2px;
}

.nav-section {
  flex: 1;
  padding: 20px 12px;
  overflow-y: auto;
}

.nav-label {
  font-size: 11px;
  text-transform: uppercase;
  color: #64748b;
  letter-spacing: 0.5px;
  padding: 0 12px 8px;
  font-weight: 600;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  color: #94a3b8;
  text-decoration: none;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
  margin-bottom: 4px;
  border: 1px solid transparent;
}

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

.nav-item.active {
  background: rgba(59, 130, 246, 0.15);
  color: #60a5fa;
  border-color: rgba(59, 130, 246, 0.3);
  font-weight: 600;
}

.nav-item i {
  font-size: 17px;
  width: 22px;
  text-align: center;
}

.nav-badge {
  margin-left: auto;
  font-size: 11px;
  padding: 2px 7px;
  border-radius: var(--radius-full);
  font-weight: 600;
}

.nav-badge.danger {
  background: #ef4444;
  color: #fff;
}

.nav-badge.warning {
  background: #f59e0b;
  color: #000;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.15);
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: #3b82f6;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  color: white;
}

.user-info {
  flex: 1;
  overflow: hidden;
}

.user-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role-badge {
  font-size: 10px;
  display: inline-block;
  padding: 1px 6px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.12);
  color: #94a3b8;
}

.btn-logout {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: var(--transition);
}

.btn-logout:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

/* Main Content Area */
.app-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-x: hidden;
}

.app-header {
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 16px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 40;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.page-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-main);
}

.page-tagline {
  font-size: 12px;
  color: var(--text-muted);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.content-body {
  padding: 24px 28px;
  flex: 1;
}

/* KPI Banner & Cards */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow-sm);
  transition: var(--transition);
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--primary-light);
}

.kpi-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 4px;
  height: 100%;
}

.kpi-card.total::after { background: #3b82f6; }
.kpi-card.critical::after { background: #ef4444; }
.kpi-card.warning::after { background: #f59e0b; }
.kpi-card.completed::after { background: #10b981; }
.kpi-card.normal::after { background: #64748b; }

.kpi-icon {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
}

.kpi-card.total .kpi-icon { background: #eff6ff; color: #2563eb; }
.kpi-card.critical .kpi-icon { background: #fef2f2; color: #dc2626; }
.kpi-card.warning .kpi-icon { background: #fffbeb; color: #d97706; }
.kpi-card.completed .kpi-icon { background: #ecfdf5; color: #059669; }
.kpi-card.normal .kpi-icon { background: #f1f5f9; color: #475569; }

.kpi-data {
  flex: 1;
}

.kpi-value {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.1;
  color: var(--text-main);
}

.kpi-title {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
  margin-top: 4px;
}

.kpi-subtext {
  font-size: 11px;
  color: var(--text-light);
  margin-top: 2px;
}

/* Filter Bar */
.filter-bar {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-sm);
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
}

.search-box {
  position: relative;
  flex: 2;
  min-width: 240px;
}

.search-box i {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-light);
  font-size: 14px;
}

.search-box input {
  width: 100%;
  padding: 9px 12px 9px 38px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-family: inherit;
  background: #f8fafc;
  transition: var(--transition);
}

.search-box input:focus {
  outline: none;
  background: #fff;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.filter-select {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-family: inherit;
  background-color: #f8fafc;
  color: var(--text-main);
  min-width: 140px;
  flex: 1;
  cursor: pointer;
  transition: var(--transition);
}

.filter-select:focus {
  outline: none;
  background-color: #fff;
  border-color: var(--primary-light);
}

.filter-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 16px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: var(--transition);
  border: 1px solid transparent;
  white-space: nowrap;
}

.btn i {
  pointer-events: none;
}

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

.btn-primary:hover {
  background: #1d4ed8;
  box-shadow: 0 4px 10px rgba(30, 58, 138, 0.25);
}

.btn-success {
  background: var(--success);
  color: white;
}

.btn-success:hover {
  background: #059669;
}

.btn-danger {
  background: var(--danger);
  color: white;
}

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

.btn-secondary {
  background: #f1f5f9;
  color: var(--text-main);
  border-color: var(--border);
}

.btn-secondary:hover {
  background: #e2e8f0;
}

.btn-outline {
  background: transparent;
  border-color: var(--border);
  color: var(--text-main);
}

.btn-outline:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
}

.btn-sm {
  padding: 6px 10px;
  font-size: 12px;
}

.btn-icon {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: var(--radius-md);
}

/* Card Container */
.content-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  margin-bottom: 24px;
}

.card-header {
  padding: 16px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-size: 15px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
}

.card-subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

.card-body {
  padding: 20px 22px;
}

/* Table */
.table-responsive {
  width: 100%;
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 13px;
}

.data-table th {
  background: #f8fafc;
  padding: 12px 14px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  user-select: none;
}

.data-table th.sortable {
  cursor: pointer;
}

.data-table th.sortable:hover {
  color: var(--primary-light);
  background: #f1f5f9;
}

.data-table td {
  padding: 12px 14px;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}

.data-table tr:hover td {
  background-color: #f8fafc;
}

.data-table tr.row-cancelled td {
  opacity: 0.55;
  background-color: #fcfcfc;
}

.data-table tr.row-critical td {
  border-left: 3px solid #ef4444;
}

.data-table tr.row-warning td {
  border-left: 3px solid #f59e0b;
}

/* Badges & Status Pills */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-critical { background: #fee2e2; color: #991b1b; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-success { background: #d1fae5; color: #065f46; }
.badge-neutral { background: #f1f5f9; color: #475569; }
.badge-info { background: #dbeafe; color: #1e40af; }

.risk-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.risk-pill.critical { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.risk-pill.warning { background: #fffbeb; color: #b45309; border: 1px solid #fde68a; }
.risk-pill.completed { background: #ecfdf5; color: #047857; border: 1px solid #a7f3d0; }
.risk-pill.normal { background: #f1f5f9; color: #475569; border: 1px solid #e2e8f0; }
.risk-pill.cancelled { background: #f3f4f6; color: #6b7280; text-decoration: line-through; }

/* Timeline Pipeline Badge */
.pipeline-steps {
  display: flex;
  align-items: center;
  gap: 4px;
}

.pipeline-step {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: #f1f5f9;
  color: #94a3b8;
  font-weight: 500;
}

.pipeline-step.done {
  background: #dbeafe;
  color: #1d4ed8;
  font-weight: 600;
}

.pipeline-step.delivered {
  background: #d1fae5;
  color: #047857;
  font-weight: 600;
}

/* Pagination */
.pagination-bar {
  padding: 14px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid var(--border);
  background: #fcfcfd;
}

.pagination-info {
  font-size: 12px;
  color: var(--text-muted);
}

.pagination-controls {
  display: flex;
  gap: 4px;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.modal-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.modal-box {
  background: #fff;
  border-radius: var(--radius-lg);
  max-width: 650px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-xl);
  transform: translateY(20px);
  transition: transform 0.2s ease;
  overflow: hidden;
}

/* Keep long material forms scrollable inside the modal viewport. */
#modal-material .modal-box {
  height: min(90vh, 760px);
  max-height: 90vh;
}

#modal-material .modal-box > form {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

#modal-material .modal-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

#modal-material .modal-footer {
  flex: 0 0 auto;
}

.modal-backdrop.open .modal-box {
  transform: translateY(0);
}

.modal-header {
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: 16px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
  transition: var(--transition);
}

.modal-close:hover {
  color: var(--danger);
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  background: #f8fafc;
}

/* Forms */
.form-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group.full {
  grid-column: span 2;
}

.form-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-main);
}

.form-input, .form-textarea, .form-select {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 13px;
  font-family: inherit;
  transition: var(--transition);
  background: #fff;
}

.form-input:focus, .form-textarea:focus, .form-select:focus {
  outline: none;
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.form-textarea {
  min-height: 80px;
  resize: vertical;
}

.calc-preview-card {
  background: #f0fdf4;
  border: 1px dashed #86efac;
  border-radius: var(--radius-md);
  padding: 14px;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Toast Notifications */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toast {
  padding: 12px 18px;
  border-radius: var(--radius-md);
  color: white;
  font-weight: 500;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: var(--shadow-lg);
  animation: slideIn 0.3s ease;
  min-width: 280px;
}

.toast.success { background: #059669; }
.toast.error { background: #dc2626; }
.toast.info { background: #2563eb; }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Quick Role Switcher */
.role-chip-group {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.role-chip {
  padding: 3px 8px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.05);
  color: #cbd5e1;
  cursor: pointer;
  transition: var(--transition);
}

.role-chip:hover, .role-chip.active {
  background: #3b82f6;
  border-color: #3b82f6;
  color: #fff;
}

/* Charts / Progress bars */
.progress-bar-bg {
  width: 100%;
  height: 8px;
  background: #e2e8f0;
  border-radius: var(--radius-full);
  overflow: hidden;
  margin: 6px 0;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #3b82f6, #10b981);
  border-radius: var(--radius-full);
  transition: width 0.5s ease;
}

.zone-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.zone-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}

.zone-card-title {
  font-size: 14px;
  font-weight: 700;
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.zone-stats {
  display: flex;
  gap: 12px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Responsive */
@media (max-width: 900px) {
  .app-sidebar {
    width: 70px;
  }
  .brand-title, .brand-subtitle, .nav-label, .nav-item span, .nav-badge, .user-info, .role-chip-group {
    display: none;
  }
  .brand-section {
    padding: 16px 14px;
  }
  .nav-item {
    justify-content: center;
    padding: 12px;
  }
  .nav-item i {
    font-size: 20px;
    margin: 0;
  }
  .content-body {
    padding: 16px;
  }
  .form-grid {
    grid-template-columns: 1fr;
  }
  .form-group.full {
    grid-column: span 1;
  }
}
