/* ==========================================================================
   Biz2026-web Stylesheet
   Full Viewport Theme Immersion (PKS & IQ), Responsive Modern UI, A4 Print Engine
   ========================================================================== */

/* --- Fonts & Base Reset --- */
@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;600&display=swap');

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

/* --- Theme Variables --- */
:root {
  --font-sans: 'Sarabun', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 10px rgba(0,0,0,0.08), 0 2px 4px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.12);
  --transition-fast: 0.18s ease;
}

/* Theme PKS (ร้านเพื่อนก่อสร้าง) */
:root[data-theme="pks"] {
  --bg-page: #fff7ed;          /* warm cream orange */
  --bg-card: #ffffff;
  --bg-sidebar: #ffedd5;       /* soft orange header/sidebar */
  --bg-sidebar-hover: #fed7aa;
  --border-color: #fed7aa;
  --border-subtle: #ffedd5;
  --text-main: #431407;        /* deep espresso brown */
  --text-muted: #7c2d12;       /* builder brick orange */
  --text-light: #9a3412;
  --primary: #eb672d;          /* builder orange */
  --primary-hover: #c2410c;
  --primary-text: #ffffff;
  --accent: #f6b73c;           /* warm gold */
  --accent-blue: #59b8cb;      /* sky blue */
  --success: #16a34a;
  --danger: #dc2626;
  --badge-bg: #ea580c;
  --card-border: #fed7aa;
}

/* Theme IQ (ร้านไอคิว) */
:root[data-theme="iq"] {
  --bg-page: #edf2f7;          /* ice slate blue */
  --bg-card: #ffffff;
  --bg-sidebar: #ffffff;
  --bg-sidebar-hover: #f1f5f9;
  --border-color: #cbd5e1;     /* slate border */
  --border-subtle: #e2e8f0;
  --text-main: #0f172a;        /* dark navy */
  --text-muted: #334155;       /* slate */
  --text-light: #64748b;
  --primary: #9e1b28;          /* crimson red */
  --primary-hover: #7f121d;
  --primary-text: #ffffff;
  --accent: #477a94;           /* petroleum blue */
  --accent-blue: #2563eb;
  --success: #16a34a;
  --danger: #dc2626;
  --badge-bg: #9e1b28;
  --card-border: #cbd5e1;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-page);
  color: var(--text-main);
  min-height: 100vh;
  line-height: 1.5;
  transition: background-color 0.25s ease, color 0.25s ease;
  display: flex;
  flex-direction: column;
}

/* --- Layout Structure --- */
.app-container {
  display: flex;
  min-height: 100vh;
  width: 100%;
}

/* Sidebar */
.sidebar {
  width: 280px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: background-color 0.25s ease, border-color 0.25s ease;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 100;
}

.sidebar-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand-logo {
  width: 44px;
  height: 44px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  background: #ffffff;
  padding: 2px;
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-color);
}

.brand-info {
  display: flex;
  flex-direction: column;
}

.brand-name {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
}

.brand-badge {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
}

/* Shop Switcher */
.shop-switcher-container {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border-color);
}

.shop-switcher-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.shop-switcher {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  background: rgba(0,0,0,0.04);
  padding: 4px;
  border-radius: var(--radius-md);
}

.btn-switch-shop {
  border: none;
  background: transparent;
  padding: 0.5rem;
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  transition: all var(--transition-fast);
  text-align: center;
}

.btn-switch-shop.active {
  background: var(--primary);
  color: var(--primary-text);
  box-shadow: var(--shadow-sm);
}

/* Nav Menu */
.sidebar-nav {
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex: 1;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  color: var(--text-main);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.92rem;
  cursor: pointer;
  transition: all var(--transition-fast);
  position: relative;
}

.nav-link:hover {
  background: var(--bg-sidebar-hover);
  color: var(--text-main);
}

.nav-link.active {
  background: var(--primary);
  color: var(--primary-text);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}

.nav-icon {
  font-size: 1.15rem;
  width: 24px;
  text-align: center;
}

.nav-badge {
  margin-left: auto;
  background-color: var(--danger);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  animation: pulse-glow 2s infinite;
}

@keyframes pulse-glow {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.08); opacity: 0.85; }
}

.sidebar-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border-color);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

/* Main Content Area */
.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
  overflow-y: auto;
}

/* Top App Header */
.top-header {
  background-color: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  padding: 1rem 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
}

.header-tagline-box {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-tagline {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.2px;
}

.header-status-pill {
  font-size: 0.75rem;
  background: rgba(22, 163, 74, 0.12);
  color: var(--success);
  border: 1px solid rgba(22, 163, 74, 0.3);
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.header-status-pill::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

/* Content Body */
.content-body {
  padding: 2rem;
  flex: 1;
}

.view-section {
  display: none;
}

.view-section.active {
  display: block;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- Cards & UI Elements --- */
.card {
  background-color: var(--bg-card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  transition: all var(--transition-fast);
}

.card:hover {
  box-shadow: var(--shadow-md);
}

.card-title {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* KPI Cards Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--card-border);
  border-radius: var(--radius-lg);
  padding: 1.25rem 1.5rem;
  box-shadow: var(--shadow-sm);
  position: relative;
  overflow: hidden;
}

.kpi-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--primary);
}

.kpi-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
}

.kpi-value {
  font-size: 1.75rem;
  font-weight: 700;
  font-family: var(--font-mono);
  color: var(--text-main);
  line-height: 1.1;
}

.kpi-subtext {
  font-size: 0.78rem;
  color: var(--text-light);
  margin-top: 0.4rem;
}

/* Cash Flow Matrix Cards */
.channels-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.channel-card {
  border-radius: var(--radius-md);
  padding: 1.25rem;
  box-shadow: var(--shadow-sm);
  border: 1px solid rgba(0,0,0,0.06);
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  position: relative;
}

/* Channel specific palettes */
.channel-card.role-dong {
  background: #e0f2fe; /* blue */
  border-color: #7dd3fc;
  color: #0369a1;
}
.channel-card.role-lin_pks,
.channel-card.role-lin_iq {
  background: #f0fdf4; /* light green */
  border-color: #86efac;
  color: #15803d;
}
.channel-card.role-iq {
  background: #dcfce7; /* vibrant green */
  border-color: #4ade80;
  color: #166534;
}
.channel-card.role-cash_pks,
.channel-card.role-cash_iq {
  background: #fef9c3; /* gold */
  border-color: #fde047;
  color: #854d0e;
}

.channel-card-line1 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 0.95rem;
}

.channel-role-badge {
  font-size: 0.72rem;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  background: rgba(255,255,255,0.7);
  font-weight: 700;
}

.channel-card-line2 {
  background: rgba(255,255,255,0.85);
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: #334155;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.channel-card-line3 {
  font-family: var(--font-mono);
  font-size: 1.5rem;
  font-weight: 700;
  text-align: right;
  margin-top: 0.25rem;
}

/* Form Styles */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  position: relative;
}

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

.form-control {
  font-family: var(--font-sans);
  font-size: 0.92rem;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-main);
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.form-control:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(235, 103, 45, 0.15);
}

:root[data-theme="iq"] .form-control:focus {
  box-shadow: 0 0 0 3px rgba(158, 27, 40, 0.15);
}

/* Buttons */
.btn {
  font-family: var(--font-sans);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.65rem 1.2rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: all var(--transition-fast);
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
}
.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: var(--shadow-sm);
}

.btn-secondary {
  background: var(--bg-page);
  color: var(--text-main);
  border-color: var(--border-color);
}
.btn-secondary:hover {
  background: var(--border-color);
}

.btn-success {
  background: var(--success);
  color: #fff;
}
.btn-success:hover {
  background: #15803d;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover {
  background: #b91c1c;
}

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

/* Autocomplete Dropdown */
.autocomplete-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: #ffffff;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  max-height: 200px;
  overflow-y: auto;
  z-index: 200;
  display: none;
}

.autocomplete-item {
  padding: 0.6rem 0.85rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 0.85rem;
  color: var(--text-main);
}

.autocomplete-item:hover {
  background: var(--bg-sidebar-hover);
}

.autocomplete-item strong {
  color: var(--primary);
}

/* Tables */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  margin-bottom: 1rem;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  text-align: left;
}

.table th {
  background: var(--bg-sidebar);
  color: var(--text-main);
  font-weight: 600;
  padding: 0.75rem 0.85rem;
  border-bottom: 2px solid var(--border-color);
}

.table td {
  padding: 0.75rem 0.85rem;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
}

.table tbody tr:hover {
  background: rgba(0,0,0,0.02);
}

.table-input {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 0.85rem;
}

/* Status Badges */
.badge {
  display: inline-block;
  padding: 0.25rem 0.55rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 999px;
  text-align: center;
}

.badge-pending {
  background: #fef3c7;
  color: #92400e;
}
.badge-paid, .badge-fully-paid {
  background: #dcfce7;
  color: #166534;
}
.badge-partially-paid {
  background: #e0f2fe;
  color: #0369a1;
}
.badge-draft {
  background: #f1f5f9;
  color: #475569;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

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

.modal-content {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 750px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  transform: scale(0.95);
  transition: transform 0.2s ease;
  overflow: hidden;
}

.modal-backdrop.open .modal-content {
  transform: scale(1);
}

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

.modal-header h3 {
  font-size: 1.15rem;
  color: var(--text-main);
  font-weight: 700;
}

.btn-close-modal {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;
  background: var(--bg-page);
}

/* ==========================================================================
   A4 GOVERNMENT PRINT ENGINE
   Precise 15mm (1.5cm) margin, 1-page fit, 17 rows, diagonal pen slash
   ========================================================================== */
.a4-print-sheet {
  width: 210mm;
  min-height: 297mm;
  padding: 15mm;
  margin: 0 auto 20px auto;
  background: #ffffff;
  color: #000000;
  box-shadow: var(--shadow-lg);
  position: relative;
  font-family: 'Sarabun', sans-serif;
  font-size: 13px;
  line-height: 1.35;
  box-sizing: border-box;
}

.a4-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  border-bottom: 1.5px solid #000;
  padding-bottom: 8px;
}

.a4-header-pks {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.a4-header-iq {
  text-align: center;
}

.a4-logo {
  max-height: 65px;
  max-width: 140px;
  object-fit: contain;
}

.a4-title {
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  margin: 6px 0;
  letter-spacing: 0.5px;
}

.a4-doc-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12.5px;
}

.a4-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
}

.a4-table th, .a4-table td {
  border: 1px solid #000;
  padding: 3.5px 6px;
  font-size: 12px;
  height: 22px;
}

.a4-table th {
  background: #f8fafc;
  text-align: center;
  font-weight: 700;
}

/* Diagonal Pen Slash on empty rows */
.slash-cell {
  position: relative;
  overflow: hidden;
}

.slash-cell::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(to top right, transparent calc(50% - 0.75px), #000 calc(50% - 0.75px), #000 calc(50% + 0.75px), transparent calc(50% + 0.75px));
  pointer-events: none;
}

.a4-sign-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-top: 15px;
  text-align: center;
  font-size: 12px;
}

.a4-sign-box {
  padding: 10px;
}

.a4-sign-line {
  margin-top: 35px;
  border-top: 1px dotted #000;
  width: 75%;
  margin-left: auto;
  margin-right: auto;
  padding-top: 4px;
}

/* Media Query for Print */
@media print {
  @page {
    size: A4 portrait;
    margin: 15mm;
  }

  body {
    background: #ffffff !important;
    color: #000000 !important;
  }

  .sidebar, .top-header, .modal-backdrop:not(#modalPrintPreview), .btn, .no-print {
    display: none !important;
  }

  .app-container, .main-wrapper, .content-body {
    display: block !important;
    padding: 0 !important;
    margin: 0 !important;
    height: auto !important;
    overflow: visible !important;
  }

  .a4-print-sheet {
    box-shadow: none !important;
    margin: 0 !important;
    padding: 0 !important;
    width: 100% !important;
    page-break-after: always;
  }

  .modal-backdrop.open {
    position: static !important;
    background: transparent !important;
    padding: 0 !important;
  }

  .modal-content {
    box-shadow: none !important;
    border: none !important;
    max-width: 100% !important;
    transform: none !important;
  }

  .modal-header, .modal-footer {
    display: none !important;
  }
}

/* Responsive adjustments */
@media (max-width: 900px) {
  .app-container {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    height: auto;
    position: relative;
  }
  .top-header {
    flex-direction: column;
    align-items: flex-start;
  }
  .header-actions {
    width: 100%;
    justify-content: space-between;
  }
  .content-body {
    padding: 1rem;
  }
}

/* ==========================================================================
   RBAC & Customer Portal Styles
   ========================================================================== */
body.role-customer .sidebar {
  display: none !important;
}

body.role-customer .main-wrapper {
  margin: 0 auto;
  max-width: 1100px;
  width: 100%;
}

body.role-customer .header-tagline-box,
body.role-customer #topOwnerName,
body.role-customer #btnCopyCustomerLink {
  display: none !important;
}

body.role-customer .customer-portal-title {
  display: flex !important;
  flex-direction: column;
}

body.role-customer .top-header {
  padding: 1rem 1.5rem;
  background-color: var(--bg-card);
  border-bottom: 2px solid var(--primary);
}

body.role-customer .view-section:not(#view-draft-po) {
  display: none !important;
}

/* Staff Role Restrictions */
body.role-staff .sidebar-nav a[data-view="tax-pit"],
body.role-staff .sidebar-nav a[data-view="double-entry"] {
  display: none !important;
}

/* Pagination & Filters Components */
.card-filters .form-control-sm {
  height: 32px;
  font-size: 0.8rem;
  padding: 0.2rem 0.5rem;
}

.pagination-btn {
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
  font-size: 0.82rem;
  font-weight: 500;
  background: var(--bg-card);
  color: var(--text-main);
  cursor: pointer;
  transition: all 0.15s ease;
  min-width: 32px;
  text-align: center;
}

.pagination-btn:hover:not(:disabled) {
  background: var(--bg-page);
  border-color: var(--primary);
  color: var(--primary);
}

.pagination-btn.active {
  background: var(--primary) !important;
  color: #ffffff !important;
  border-color: var(--primary) !important;
  font-weight: 700;
}

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

.role-selector-box:hover {
  border-color: var(--primary);
}

