:root {
  --brand-navy: #0f548c;
  --brand-navy-dark: #093961;
  --brand-red: #e60000;
  --brand-red-hover: #c20000;
  --bg-page: #f3f6f9;
  --bg-card: #ffffff;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --border-color: #dbe2ea;
  --radius-md: 8px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 3px rgba(15, 84, 140, 0.08);
  --shadow-md: 0 4px 12px rgba(15, 84, 140, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, "Noto Sans Thai", sans-serif;
  background-color: var(--bg-page);
  color: var(--text-main);
  font-size: 14px;
  line-height: 1.45;
}

/* Header (ตรงตาม Component Header ใน Power Apps) */
.app-header {
  background: var(--brand-navy);
  color: #fff;
  border-bottom: 4px solid var(--brand-red);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-md);
}

.header-inner {
  max-width: 1560px;
  margin: 0 auto;
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  gap: 16px;
}

.brand-box {
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
}

.logo-wrap {
  background: #fff;
  padding: 4px 12px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  height: 42px;
}

.logo-wrap img {
  height: 32px;
  width: auto;
  object-fit: contain;
}

.header-title {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: 0.4px;
}

.header-title .sys-red {
  color: #ff4d4d;
  font-weight: 700;
}

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

/* ปุ่ม Sync ข้อมูล MS List <-> D1 */
.btn-sync {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #ffffff;
  color: var(--brand-navy);
  border: 1px solid rgba(255, 255, 255, 0.4);
  padding: 7px 14px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-sync:hover {
  background: #eff6ff;
}

.sync-badge {
  background: var(--brand-red);
  color: #fff;
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  font-weight: 700;
}

.sync-badge.synced {
  background: #16a34a;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.2);
  padding: 5px 12px;
  border-radius: 8px;
}

.user-meta {
  text-align: right;
  line-height: 1.2;
}

.user-meta .login-label {
  font-size: 10px;
  color: #ff8080;
  font-weight: 700;
  text-transform: uppercase;
}

.user-meta .user-name {
  font-size: 13px;
  font-weight: 600;
  color: #fff;
}

.role-switcher {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 5px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
}

.role-switcher option {
  color: #1e293b;
  background: #fff;
}

/* Main Container */
.main-container {
  max-width: 1560px;
  margin: 0 auto;
  padding: 18px 16px 48px;
}

/* KPI Summary Cards & Modern Filter Panel */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 16px;
}

@media (max-width: 1024px) {
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

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

.kpi-card {
  background: #ffffff;
  border: 1.5px solid #e2e8f0;
  border-radius: 12px;
  padding: 14px 18px;
  cursor: pointer;
  transition: all 0.15s ease;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
  user-select: none;
}

.kpi-card:hover {
  border-color: #93c5fd;
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.07);
}

.kpi-card.active {
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.14);
}

.kpi-value {
  font-size: 26px;
  font-weight: 800;
  color: #0f172a;
  line-height: 1.15;
  margin-bottom: 6px;
}

.kpi-label-row {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 13px;
  font-weight: 500;
  color: #64748b;
  margin-bottom: 10px;
}

.kpi-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.kpi-bar-track {
  width: 100%;
  height: 4px;
  background: #e2e8f0;
  border-radius: 999px;
  overflow: hidden;
}

.kpi-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.25s ease;
}

.modern-filter-card {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 16px 18px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
}

.modern-filter-row1 {
  display: grid;
  grid-template-columns: 1.15fr 2.6fr 1.15fr 1.15fr 0.85fr;
  gap: 14px;
  align-items: end;
}

@media (max-width: 1100px) {
  .modern-filter-row1 {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 640px) {
  .modern-filter-row1 {
    grid-template-columns: 1fr;
  }
}

.modern-filter-row2 {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
  margin-top: 14px;
}

.modern-filter-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.modern-filter-group label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: #64748b;
}

.modern-select,
.modern-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  background: #f8fafc;
  color: #0f172a;
  font-size: 13.5px;
  font-weight: 500;
  font-family: inherit;
  transition: border-color 0.15s, background-color 0.15s;
}

.modern-select:focus,
.modern-input:focus {
  outline: none;
  border-color: #3b82f6;
  background: #ffffff;
}

.btn-reset-modern {
  background: #ffffff;
  color: #2563eb;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-reset-modern:hover {
  background: #eff6ff;
  border-color: #bfdbfe;
}

/* Filter Panel */
.filter-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px 16px;
  align-items: end;
}

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

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

.form-group label .req {
  color: var(--brand-red);
  margin-left: 2px;
}

.input,
.select,
.textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #cbd5e1;
  border-radius: var(--radius-sm);
  background: #f8fafc;
  color: var(--text-main);
  font-size: 13.5px;
  font-family: inherit;
  transition: border-color 0.15s, background-color 0.15s;
}

.input:focus,
.select:focus,
.textarea:focus {
  outline: none;
  border-color: var(--brand-navy);
  background: #fff;
}

.input:disabled,
.select:disabled,
.textarea:disabled {
  background: #e2e8f0;
  color: #475569;
  cursor: not-allowed;
}

/* Action Bar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--brand-red);
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  color: var(--brand-navy);
}

.toolbar-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.15s ease;
}

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

.btn-navy {
  background: var(--brand-navy);
  color: #fff;
}

.btn-navy:hover:not(:disabled) {
  background: var(--brand-navy-dark);
}

.btn-red {
  background: var(--brand-red);
  color: #fff;
}

.btn-red:hover:not(:disabled) {
  background: var(--brand-red-hover);
}

.btn-gray {
  background: #e2e8f0;
  color: #1e293b;
}

.btn-gray:hover:not(:disabled) {
  background: #cbd5e1;
}

.btn-outline {
  background: #fff;
  color: var(--brand-navy);
  border-color: var(--brand-navy);
}

.btn-outline:hover:not(:disabled) {
  background: #f0f7ff;
}

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

/* Data Table (แทนที่ Gallery ของ Power Apps) */
.table-wrap {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow-x: auto;
  box-shadow: var(--shadow-sm);
}

.crm-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 980px;
}

.crm-table thead th {
  background: var(--brand-navy);
  color: #fff;
  font-weight: 600;
  font-size: 13px;
  padding: 11px 12px;
  text-align: left;
  border-bottom: 2px solid var(--brand-red);
  white-space: nowrap;
  user-select: none;
}

.crm-table thead th.sortable {
  cursor: pointer;
}

.crm-table thead th.sortable:hover {
  background: var(--brand-navy-dark);
}

.crm-table tbody tr {
  border-bottom: 1px solid #edf2f7;
  transition: background 0.1s;
}

.crm-table tbody tr:hover {
  background: #f8fbff;
}

.crm-table td {
  padding: 10px 12px;
  vertical-align: middle;
  font-size: 13.5px;
}

/* Status Pill Colors (ตรงตาม Switch(ThisItem.Status) ใน Screen_Presale) */
.status-pill {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 12px;
  text-align: center;
  min-width: 120px;
  color: #1e293b;
}

.status-approved { background: rgba(201, 242, 155, 1); }
.status-action-plan { background: rgba(255, 252, 127, 1); }
.status-wait-approve { background: rgba(255, 200, 150, 1); }
.status-wait-answer { background: rgba(216, 191, 216, 1); }
.status-info-only { background: rgba(134, 226, 213, 1); }
.status-po-received { background: rgba(174, 214, 241, 1); }
.status-quoted { background: rgba(245, 183, 177, 1); }
.status-loss { background: rgba(200, 200, 200, 1); }
.status-default { background: #e2e8f0; }

/* Sync status dot on row */
.sync-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 99px;
  font-weight: 600;
}

.sync-tag.synced {
  background: #dcfce7;
  color: #15803d;
}

.sync-tag.pending {
  background: #fef3c7;
  color: #b45309;
}

/* Section Banner inside Forms */
.section-banner {
  background: var(--brand-navy);
  color: #fff;
  padding: 9px 16px;
  font-weight: 600;
  font-size: 14px;
  border-radius: var(--radius-sm);
  margin: 18px 0 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.form-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.form-grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 14px 24px;
}

.split-layout {
  display: grid;
  grid-template-columns: 1.15fr 0.85fr;
  gap: 18px;
}

@media (max-width: 1024px) {
  .split-layout {
    grid-template-columns: 1fr;
  }
}

.comment-box {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 12px;
  background: #f8fafc;
  margin-bottom: 10px;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 16px;
}

.modal-card {
  background: #fff;
  border-radius: 10px;
  max-width: 680px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: 0 20px 35px rgba(0, 0, 0, 0.25);
}

.modal-header {
  background: var(--brand-navy);
  color: #fff;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 3px solid var(--brand-red);
}

.modal-body {
  padding: 18px;
}

/* Toast Notification */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: #1e293b;
  color: #fff;
  padding: 12px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow-md);
  font-size: 13.5px;
  border-left: 4px solid #38bdf8;
}

.toast.success { border-left-color: #22c55e; }
.toast.error { border-left-color: #ef4444; }

