/* ============================================================
   RTG Contractor Work Permit System — Modern SSHE Stylesheet
   ============================================================ */
:root {
  --primary: #166534;
  --primary-mid: #16a34a;
  --primary-light: #34d399;
  --primary-soft: #dcfce7;
  --primary-tint: #f0fdf4;
  --bg: #f6faf7;
  --surface: #ffffff;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #1e293b;
  --text-muted: #64748b;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --warning: #f59e0b;
  --warning-soft: #fef3c7;
  --info: #0ea5e9;
  --info-soft: #e0f2fe;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.04);
  --shadow: 0 4px 14px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 12px 28px rgba(15, 23, 42, 0.10);
}

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

body {
  font-family: 'IBM Plex Sans Thai', 'Plus Jakarta Sans', 'Segoe UI', Tahoma, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  letter-spacing: -0.01em;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

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

.sidebar {
  width: 260px;
  background: linear-gradient(180deg, #15803d 0%, #198754 55%, #0f766e 100%);
  color: #fff;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 30;
  transition: transform 0.25s ease;
}

.sidebar-brand {
  padding: 20px 16px 18px;
  background: linear-gradient(180deg, rgba(255,255,255,0.97) 0%, rgba(240,253,244,0.90) 56%, rgba(21,128,61,0.22) 86%, rgba(21,128,61,0) 100%);
  border-bottom: 1px solid rgba(255,255,255,0.16);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
}

.brand-logo-img {
  width: 224px;
  max-width: 100%;
  height: auto;
  object-fit: contain;
  display: block;
  filter: drop-shadow(0 2px 6px rgba(15, 23, 42, 0.10));
}

.brand-text {
  margin-top: 4px;
  text-shadow: 0 1px 4px rgba(15, 81, 50, 0.55);
}
.brand-text h1 {
  font-size: 16px;
  font-weight: 700;
  line-height: 1.25;
  color: #ffffff;
}
.brand-text span {
  font-size: 11.5px;
  opacity: 0.92;
  display: block;
  color: #dcfce7;
}

.nav-section-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: rgba(255,255,255,0.55);
  padding: 16px 18px 6px;
  font-weight: 600;
}

.nav-menu {
  flex: 1;
  overflow-y: auto;
  padding: 6px 10px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 3px;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.85);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  transition: all 0.15s;
}

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

.nav-item.active {
  background: rgba(255,255,255,0.18);
  color: #fff;
  font-weight: 600;
  box-shadow: inset 3px 0 0 #4ade80;
}

.nav-badge {
  margin-left: auto;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 99px;
}

.sidebar-footer {
  padding: 14px;
  border-top: 1px solid rgba(255,255,255,0.12);
  font-size: 11.5px;
  color: rgba(255,255,255,0.75);
}

.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ---- Topbar ---- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mobile-menu-btn {
  display: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 18px;
  cursor: pointer;
}

.page-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
}

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

.role-switcher {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--primary-tint);
  border: 1px solid #bbf7d0;
  padding: 5px 12px;
  border-radius: 99px;
  font-size: 12.5px;
}

.role-switcher label {
  font-weight: 600;
  color: var(--primary);
  white-space: nowrap;
}

.role-switcher select {
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  outline: none;
}

.notif-btn {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.notif-btn:hover { background: var(--bg); }
.notif-dot {
  position: absolute;
  top: -3px;
  right: -3px;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 99px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

.content {
  padding: 24px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
}

/* ---- Cards & Grids ---- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 20px;
  margin-bottom: 18px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.card-title {
  font-size: 15.5px;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(195px, 1fr));
  gap: 14px;
  margin-bottom: 20px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-left: 4px solid var(--primary-mid);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}
.kpi-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.kpi-card.amber { border-left-color: #f59e0b; }
.kpi-card.sky   { border-left-color: #0284c7; }
.kpi-card.green { border-left-color: #16a34a; }
.kpi-card.red   { border-left-color: #dc2626; }
.kpi-card.gray  { border-left-color: #64748b; }

.kpi-label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }
.kpi-value { font-size: 28px; font-weight: 800; color: var(--text); line-height: 1.2; margin-top: 2px; }
.kpi-sub   { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.kpi-icon  { font-size: 28px; opacity: 0.85; }

/* ---- Workflow Diagram Banner ---- */
.flow-diagram {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 4px;
}

.flow-node {
  flex: 1;
  min-width: 135px;
  background: var(--primary-tint);
  border: 1.5px solid #bbf7d0;
  border-radius: 10px;
  padding: 10px 12px;
  text-align: center;
  position: relative;
}
.flow-node.current {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  box-shadow: 0 4px 10px rgba(20, 83, 45, 0.25);
}
.flow-node.done {
  background: #dcfce7;
  border-color: #22c55e;
}
.flow-node.rejected {
  background: #fef3c7;
  border-color: #f59e0b;
}
.flow-node .fn-icon { font-size: 20px; }
.flow-node .fn-role { font-size: 12.5px; font-weight: 700; margin-top: 2px; }
.flow-node .fn-act  { font-size: 11px; opacity: 0.8; }
.flow-arrow { font-size: 18px; color: var(--primary-mid); font-weight: 700; flex-shrink: 0; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 15px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary-mid); color: #fff; }
.btn-primary:hover { background: var(--primary); }
.btn-outline { background: #fff; border-color: var(--border-strong); color: var(--text); }
.btn-outline:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-warning { background: #f59e0b; color: #fff; }
.btn-warning:hover { background: #d97706; }
.btn-info { background: var(--info); color: #fff; }
.btn-info:hover { background: #0369a1; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-lg { padding: 11px 20px; font-size: 14.5px; }

/* ---- Badges ---- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 99px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-sky     { background: #e0f2fe; color: #0369a1; }
.badge-purple  { background: #f3e8ff; color: #6d28d9; }
.badge-amber   { background: #fef3c7; color: #b45309; }
.badge-teal    { background: #ccfbf1; color: #0f766e; }
.badge-emerald { background: #d1fae5; color: #047857; }
.badge-blue    { background: #dbeafe; color: #1d4ed8; }
.badge-green   { background: #dcfce7; color: #15803d; }
.badge-orange  { background: #ffedd5; color: #c2410c; }
.badge-red     { background: #fee2e2; color: #b91c1c; }
.badge-gray    { background: #f1f5f9; color: #475569; }

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

/* ---- Forms & Wizard ---- */
.wizard-steps {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.wizard-step {
  flex: 1;
  min-width: 150px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: #fff;
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}
.wizard-step.active {
  border-color: var(--primary-mid);
  background: var(--primary-tint);
}
.wizard-step.done {
  border-color: #86efac;
  background: #f0fdf4;
}
.ws-num {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--border);
  color: var(--text-muted);
  font-weight: 700;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.wizard-step.active .ws-num { background: var(--primary-mid); color: #fff; }
.wizard-step.done .ws-num   { background: #16a34a; color: #fff; }
.ws-title { font-size: 12.5px; font-weight: 600; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 14px;
}
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group.full { grid-column: 1 / -1; }
.form-label { font-size: 12.5px; font-weight: 600; color: #334155; }
.form-label .req { color: var(--danger); }

.form-control {
  padding: 9px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13.5px;
  background: #fff;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.form-control:focus { border-color: var(--primary-mid); box-shadow: 0 0 0 3px rgba(21, 128, 61, 0.12); }

.check-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
}

.check-card {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  background: #fff;
  transition: all 0.15s;
  font-size: 13px;
}
.check-card:hover { border-color: #86efac; background: #fafdfb; }
.check-card.selected { border-color: var(--primary-mid); background: var(--primary-tint); }
.check-card.high-risk.selected { border-color: #dc2626; background: #fef2f2; }
.check-card input[type="checkbox"] { margin-top: 3px; accent-color: var(--primary-mid); width: 16px; height: 16px; }

/* Worker picker */
.worker-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.worker-row.disabled { background: #fef2f2; opacity: 0.85; }
.worker-row.disabled .w-name { color: #b91c1c; }

/* Checklist pills (ผ่าน / ไม่เกี่ยวข้อง / ไม่ผ่าน) */
.eval-item {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  background: #fff;
}
.eval-item-q { font-size: 13px; font-weight: 500; margin-bottom: 8px; }
.pill-group { display: flex; gap: 6px; flex-wrap: wrap; }
.pill-btn {
  padding: 5px 14px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: #f8fafc;
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-muted);
}
.pill-btn.active-pass { background: #16a34a; color: #fff; border-color: #16a34a; }
.pill-btn.active-na   { background: #64748b; color: #fff; border-color: #64748b; }
.pill-btn.active-fail { background: #dc2626; color: #fff; border-color: #dc2626; }

/* Signature Canvas */
.sig-wrap {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: #fafafa;
  padding: 8px;
  text-align: center;
}
.sig-canvas {
  width: 100%;
  max-width: 420px;
  height: 130px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: crosshair;
  touch-action: none;
}

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }
.modal-box {
  background: #fff;
  border-radius: 14px;
  width: 100%;
  max-width: 920px;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--primary);
  color: #fff;
}
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-close {
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 18px;
}
.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}
.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  background: #f8fafc;
}

/* ---- Notification Drawer ---- */
.notif-dropdown {
  position: fixed;
  top: 64px;
  right: 20px;
  width: 360px;
  max-width: calc(100vw - 32px);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  z-index: 90;
  display: none;
  overflow: hidden;
}
.notif-dropdown.open { display: block; }
.notif-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 12.5px;
  cursor: pointer;
}
.notif-item:hover { background: var(--primary-tint); }
.notif-item.unread { background: #f0f9ff; border-left: 3px solid var(--info); }

/* ---- Charts ---- */
.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 18px;
}
.bar-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  font-size: 12.5px;
}
.bar-label { width: 170px; flex-shrink: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { flex: 1; height: 22px; background: #f1f5f9; border-radius: 6px; overflow: hidden; }
.bar-fill  { height: 100%; border-radius: 6px; display: flex; align-items: center; justify-content: flex-end; padding-right: 8px; color: #fff; font-weight: 700; font-size: 11.5px; min-width: 24px; }

/* ---- Official Printable RTG Form ---- */
.rtg-print-sheet {
  border: 1.5px solid #000;
  padding: 16px;
  background: #fff;
  color: #000;
  font-family: 'Sarabun', 'TH Sarabun New', sans-serif;
  font-size: 13px;
  margin-bottom: 20px;
}
.rtg-header-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 10px;
}
.rtg-header-table td, .rtg-header-table th {
  border: 1px solid #000;
  padding: 5px 8px;
  text-align: center;
}
.rtg-form-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}
.rtg-form-table th, .rtg-form-table td {
  border: 1px solid #000;
  padding: 4px 6px;
}
.rtg-form-table th { background: #f1f5f9; }
.rtg-sig-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  border: 1px solid #000;
  margin-top: 12px;
}
.rtg-sig-cell {
  border-right: 1px solid #000;
  padding: 6px;
  text-align: center;
  font-size: 11.5px;
  min-height: 82px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.rtg-sig-cell:last-child { border-right: none; }

/* ---- Toast ---- */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: #0f172a;
  color: #fff;
  padding: 12px 18px;
  border-radius: 10px;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 8px;
  animation: slideUp 0.2s ease;
}
@keyframes slideUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }

/* ---- Responsive ---- */
@media (max-width: 900px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .mobile-menu-btn { display: inline-flex; }
  .content { padding: 14px; }
  .topbar { padding: 10px 14px; }
  .rtg-sig-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---- Print Mode ---- */
@media print {
  .sidebar, .topbar, .modal-header, .modal-footer, .no-print, .toast-container, .notif-dropdown {
    display: none !important;
  }
  body { background: #fff; }
  .modal-backdrop { position: static; background: none; padding: 0; display: block !important; }
  .modal-box { box-shadow: none; max-height: none; max-width: 100%; border-radius: 0; }
  .modal-body { padding: 0; overflow: visible; }
  .rtg-print-sheet { page-break-after: always; border: 1.5px solid #000; }
}
