/* ============ THEME: BLUE FORMAL ============ */
:root {
  --primary: #1e3a8a;
  --primary-dark: #1e2f6e;
  --primary-light: #3b5bb8;
  --primary-pale: #eff3fb;
  --accent: #2563eb;
  --success: #059669;
  --success-pale: #d1fae5;
  --danger: #dc2626;
  --danger-pale: #fee2e2;
  --warning: #d97706;
  --warning-pale: #fef3c7;
  --text: #1f2937;
  --text-muted: #6b7280;
  --border: #d1d5db;
  --border-light: #e5e7eb;
  --bg: #f9fafb;
  --white: #ffffff;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Sarabun', 'Tahoma', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
}

/* ============ LOGIN VIEW ============ */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
}
.login-card {
  background: var(--white);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
  padding: 40px;
  width: 100%;
  max-width: 420px;
}
.login-header {
  text-align: center;
  margin-bottom: 28px;
}
.login-header h1 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: var(--primary);
}
.login-header p {
  margin: 0;
  color: var(--text-muted);
  font-size: 15px;
}
.login-form .form-group { margin-bottom: 16px; }
.login-error {
  background: var(--danger-pale);
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  margin-bottom: 16px;
  display: none;
}
.login-error.show { display: block; }
.btn-block { width: 100%; }

/* ============ HEADER ============ */
.app-header {
  background: var(--primary);
  color: var(--white);
  padding: 16px 0;
  box-shadow: var(--shadow-lg);
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.app-header h1 { margin: 0; font-size: 22px; font-weight: 600; }
.user-info {
  font-size: 14px;
  opacity: 0.95;
  display: flex;
  align-items: center;
  gap: 12px;
}
.btn-link {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.4);
  color: var(--white);
  padding: 6px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.btn-link:hover { background: rgba(255,255,255,0.15); }

/* ============ TAB NAV ============ */
.tab-nav {
  background: var(--white);
  border-bottom: 2px solid var(--border-light);
  display: flex;
  overflow-x: auto;
  position: sticky;
  top: 64px;
  z-index: 90;
}
.tab-btn {
  flex: 1;
  min-width: 120px;
  padding: 16px 20px;
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  font-size: 16px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}
.tab-btn:hover { background: var(--primary-pale); color: var(--primary); }
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: var(--primary-pale);
  font-weight: 600;
}

/* ============ CONTAINER ============ */
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ============ CARD ============ */
.card {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 20px;
  border: 1px solid var(--border-light);
}
.card h2 {
  margin: 0 0 20px 0;
  font-size: 20px;
  color: var(--primary);
  padding-bottom: 12px;
  border-bottom: 2px solid var(--primary-pale);
}

/* ============ FORM ============ */
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 16px;
}
.form-group { display: flex; flex-direction: column; margin-bottom: 16px; }
.form-group label { font-weight: 500; margin-bottom: 6px; color: var(--text); font-size: 15px; }
.required { color: var(--danger); }

input[type="text"], input[type="password"], input[type="date"], input[type="number"], select, textarea {
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  font-family: inherit;
  background: var(--white);
  transition: border 0.15s, box-shadow 0.15s;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(30, 58, 138, 0.1);
}
textarea { resize: vertical; }

.checkbox-group { flex-direction: row; align-items: center; }
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: normal !important;
}
.checkbox-label input[type="checkbox"] { width: 20px; height: 20px; cursor: pointer; }

/* ============ BUTTONS ============ */
.btn {
  padding: 12px 24px;
  border: none;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn-large { padding: 14px 32px; font-size: 17px; font-weight: 600; }
.btn-primary { background: var(--primary); color: var(--white); }
.btn-primary:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}
.btn-secondary { background: var(--white); color: var(--primary); border: 1.5px solid var(--primary); }
.btn-secondary:hover:not(:disabled) { background: var(--primary-pale); }
.btn-danger { background: var(--white); color: var(--danger); border: 1.5px solid var(--danger); }
.btn-danger:hover:not(:disabled) { background: var(--danger-pale); }
.btn-sm { padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.form-actions { margin-top: 20px; display: flex; justify-content: flex-end; gap: 12px; }

/* ============ PRODUCT LIST ============ */
.product-list {
  margin: 16px 0;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  overflow: hidden;
}
.product-row {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light);
  gap: 12px;
}
.product-row:last-child { border-bottom: none; }
.product-row:nth-child(even) { background: #fafbfc; }
.product-row .product-name { font-weight: 500; }
.product-row .stock-info { font-size: 14px; color: var(--text-muted); }
.product-row .unit-label { color: var(--text-muted); font-size: 14px; }
.product-row input[type="number"] { text-align: right; }
.product-row.warning input[type="number"] {
  border-color: var(--danger);
  background: var(--danger-pale);
}
.product-list-header {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr;
  padding: 12px 16px;
  background: var(--primary-pale);
  font-weight: 600;
  color: var(--primary);
  font-size: 14px;
  gap: 12px;
}

/* ============ TABLE ============ */
.table-wrapper {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border-light);
}
.data-table { width: 100%; border-collapse: collapse; font-size: 15px; }
.data-table th {
  background: var(--primary);
  color: var(--white);
  padding: 12px;
  text-align: left;
  font-weight: 600;
  font-size: 14px;
}
.data-table td { padding: 12px; border-bottom: 1px solid var(--border-light); }
.data-table tr:hover td { background: var(--primary-pale); }
.data-table tr.voided td {
  color: var(--text-muted);
  text-decoration: line-through;
  background: #f3f4f6;
}
.text-right { text-align: right; }
.text-center { text-align: center; }

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 500;
}
.badge-active { background: var(--success-pale); color: var(--success); }
.badge-voided { background: var(--danger-pale); color: var(--danger); }
.badge-warning { background: var(--warning-pale); color: var(--warning); }

/* ============ SUMMARY ============ */
.summary-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 12px;
}
.summary-header h2 { margin: 0; padding: 0; border: none; }
.filter-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  padding: 16px;
  background: var(--primary-pale);
  border-radius: var(--radius);
  margin-bottom: 20px;
}
.filter-row .form-group { margin-bottom: 0; }

.supplier-group { margin-bottom: 24px; }
.supplier-group h3 {
  background: var(--primary);
  color: var(--white);
  padding: 10px 16px;
  margin: 0;
  border-radius: var(--radius) var(--radius) 0 0;
  font-size: 16px;
}
.supplier-group .table-wrapper {
  border-radius: 0 0 var(--radius) var(--radius);
  border-top: none;
}
.low-stock-row { background: var(--warning-pale) !important; }
.low-stock-row td:first-child::before { content: "⚠️ "; }

.empty-state { text-align: center; color: var(--text-muted); padding: 24px; margin: 0; }

/* ============ TOAST ============ */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--text);
  color: var(--white);
  padding: 14px 24px;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: all 0.3s;
  z-index: 1000;
  max-width: 90%;
  text-align: center;
  white-space: pre-line;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* ============ LOADING ============ */
.loading-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(255,255,255,0.85);
  z-index: 2000;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.loading-overlay.show { display: flex; }
.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border-light);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--primary); font-weight: 500; }

/* ============ RESPONSIVE ============ */
@media (max-width: 640px) {
  .login-card { padding: 24px; }
  .app-header h1 { font-size: 18px; }
  .container { padding: 12px; }
  .card { padding: 16px; }
  .product-row, .product-list-header {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .product-row .product-name { grid-column: 1 / -1; }
  .data-table { font-size: 14px; }
  .data-table th, .data-table td { padding: 8px; }
  .btn-large { width: 100%; }
}

/* ============ V2 ADDITIONS ============ */

/* Alert levels */
.alert-critical {
  background: #fee2e2 !important;
  border-left: 4px solid var(--danger);
}
.alert-warning {
  background: #fef3c7 !important;
  border-left: 4px solid var(--warning);
}
.badge-critical { background: var(--danger); color: white; }
.badge-info { background: var(--primary-pale); color: var(--primary); }

/* Stock value summary */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}
.kpi-card {
  background: var(--white);
  border: 1px solid var(--border-light);
  border-left: 4px solid var(--primary);
  padding: 16px;
  border-radius: var(--radius);
}
.kpi-card.danger { border-left-color: var(--danger); }
.kpi-card.warning { border-left-color: var(--warning); }
.kpi-card.success { border-left-color: var(--success); }
.kpi-label {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.kpi-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
}
.kpi-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Permission denied */
.no-permission {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}
.no-permission .icon { font-size: 48px; margin-bottom: 12px; }

/* Admin panel sub-tabs */
.subtab-nav {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border-light);
  margin-bottom: 16px;
  overflow-x: auto;
}
.subtab-btn {
  padding: 10px 18px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}
.subtab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

/* Edit modal */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 1500;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-overlay.show { display: flex; }
.modal {
  background: var(--white);
  border-radius: 12px;
  padding: 24px;
  max-width: 600px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--primary);
  margin: 0 0 16px 0;
}

/* Reorder list */
.reorder-row {
  display: grid;
  grid-template-columns: 2.5fr 1fr 1fr 1fr 1fr;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light);
  align-items: center;
}
.reorder-row:nth-child(even) { background: #fafbfc; }
.reorder-header {
  display: grid;
  grid-template-columns: 2.5fr 1fr 1fr 1fr 1fr;
  gap: 12px;
  padding: 12px 16px;
  background: var(--primary-pale);
  font-weight: 600;
  color: var(--primary);
  font-size: 13px;
}

/* Search input */
.search-input {
  margin-bottom: 12px;
}

/* Price column */
.text-right.col-price { color: var(--text-muted); font-size: 14px; }

@media (max-width: 640px) {
  .reorder-row, .reorder-header { grid-template-columns: 1fr 1fr; gap: 8px; }
  .reorder-row > div:first-child, .reorder-header > div:first-child { grid-column: 1 / -1; }
}

/* alerts settings inputs */
.alert-input {
  padding: 6px 10px;
  font-size: 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
}
.alert-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(30,58,138,0.1);
}
