/* ========================
   Color Palette — เสริมสุข Light Theme
   Primary Blue  : #29ABE2
   Accent Green  : #A8D52B
======================== */

:root {
  --bg:          #f0f7fb;
  --panel:       #ffffff;
  --panel2:      #e8f5fc;
  --line:        #cce8f5;
  --text:        #1a2e3b;
  --muted:       #5a7a8a;
  --brand:       #29ABE2;
  --brand-dark:  #1a8bbf;
  --brand2:      #A8D52B;
  --brand2-dark: #88af1e;
  --success:     #A8D52B;
  --warn:        #f59e0b;
  --danger:      #ef4444;
  --radius:      14px;
  --shadow:      0 2px 12px rgba(41,171,226,.10);
  --shadow-md:   0 4px 20px rgba(41,171,226,.14);
}

/* ======================== Reset ======================== */
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
}

img { max-width: 100%; height: auto; }

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; color: var(--brand-dark); }

h1, h2, h3 { margin: 0 0 14px; color: var(--text); }

/* ======================== Layout ======================== */
.layout { display: flex; min-height: 100vh; }

/* ======================== Sidebar ======================== */
.sidebar {
  width: 220px;
  flex: 0 0 220px;
  background: var(--panel);
  padding: 0;
  border-right: 2px solid var(--line);
  box-shadow: 2px 0 10px rgba(41,171,226,.08);
  display: flex;
  flex-direction: column;
  z-index: 200;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 2px solid var(--line);
}

.brand {
  font-size: 18px;
  font-weight: 700;
  color: var(--brand);
  letter-spacing: .3px;
}

/* ปุ่มปิด sidebar — ซ่อนใน desktop แสดงเฉพาะ mobile */
.sidebar-close {
  display: none;
  background: none;
  border: none;
  font-size: 22px;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 8px;
  border-radius: 8px;
  min-height: unset;
  line-height: 1;
}
.sidebar-close:hover { background: var(--panel2); color: var(--brand); }

/* user tag */
.sidebar-user {
  font-size: 13px;
  color: var(--brand-dark);
  margin: 12px 12px 4px;
  padding: 7px 10px;
  background: var(--panel2);
  border-radius: 8px;
  border-left: 3px solid var(--brand);
  font-weight: 600;
}

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

.nav a {
  display: block;
  padding: 9px 12px;
  margin: 2px 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  transition: background .15s, color .15s;
}

.nav a:hover {
  background: var(--panel2);
  color: var(--brand);
  text-decoration: none;
}

.nav a.active {
  background: var(--brand);
  color: #fff;
  font-weight: 700;
  text-decoration: none;
}

/* Logout */
.nav a[href*="logout"] {
  color: var(--danger);
  margin-top: 6px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}
.nav a[href*="logout"]:hover { background: #fef2f2; }

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

/* ======================== Topbar ======================== */
.topbar {
  padding: 11px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
  gap: 12px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar-left strong {
  font-size: 16px;
  color: var(--brand);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar .muted {
  font-size: 13px;
  white-space: nowrap;
}

/* Hamburger — แสดงตลอด ทั้ง mobile และ desktop */
.menu-toggle {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--brand);
  min-height: unset;
  padding: 5px 8px;
  border-radius: 8px;
  line-height: 1;
  flex-shrink: 0;
}
.menu-toggle:hover { background: var(--panel2); }

/* ======================== Container ======================== */
.container {
  width: 100%;
  max-width: 1400px;
  margin: auto;
  padding: 20px;
}

/* ======================== Card ======================== */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.card h2 {
  font-size: 15px;
  color: var(--brand);
  border-left: 4px solid var(--brand2);
  padding-left: 10px;
  margin-bottom: 16px;
}

/* ======================== KPI ======================== */
.dashboard-kpi-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.kpi {
  padding: 16px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

.kpi .num {
  font-size: 28px;
  font-weight: 700;
  color: var(--brand);
}

.dashboard-kpi-small {
  padding: 14px 16px;
  min-height: auto;
  border-radius: 12px;
  border-top: 3px solid var(--brand);
  position: relative;
}

.dashboard-kpi-small:nth-child(2) { border-top-color: var(--brand2); }
.dashboard-kpi-small:nth-child(2) .num { color: var(--brand2-dark); }
.dashboard-kpi-small:nth-child(3) { border-top-color: var(--warn); }
.dashboard-kpi-small:nth-child(3) .num { color: #b45309; }

.dashboard-kpi-small .muted {
  font-size: 13px;
  margin-bottom: 4px;
  color: var(--muted);
}

.dashboard-kpi-small .num {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--brand);
}

/* ======================== Table ======================== */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 10px;
  border: 1px solid var(--line);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 600px;
}

th, td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

th {
  background: var(--brand);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

/* stripe rows */
tbody tr:nth-child(even) { background: #f7fbfe; }
tbody tr:hover { background: var(--panel2); }
tr:last-child td { border-bottom: none; }

/* empty state */
td[colspan] {
  text-align: center;
  color: var(--muted);
  padding: 32px 12px;
  font-size: 14px;
}

/* ======================== Form elements ======================== */
input, select, textarea {
  width: 100%;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--line);
  background: #fff;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  min-height: 44px;
  transition: border-color .15s, box-shadow .15s;
  -webkit-appearance: none;
  appearance: none;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(41,171,226,.15);
}

input[readonly] {
  background: #f0f7fb;
  color: var(--muted);
  cursor: default;
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 5px;
}

small.help {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%235a7a8a' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

/* ======================== Buttons ======================== */
button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border: none;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  min-height: 44px;
  transition: background .15s, box-shadow .15s;
  text-decoration: none;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

button:hover, .btn:hover {
  background: var(--brand-dark);
  box-shadow: 0 4px 12px rgba(41,171,226,.30);
  text-decoration: none;
  color: #fff;
}

button:active, .btn:active { transform: scale(.97); }

.btn-secondary {
  background: #e8f0f5;
  color: var(--text);
}
.btn-secondary:hover {
  background: #d4e8f5;
  color: var(--brand-dark);
  box-shadow: none;
}

.btn-success {
  background: var(--brand2);
  color: #fff;
}
.btn-success:hover {
  background: var(--brand2-dark);
  box-shadow: 0 4px 12px rgba(168,213,43,.35);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { background: #dc2626; box-shadow: 0 4px 12px rgba(239,68,68,.30); }

.btn-warn { background: var(--warn); color: #fff; }
.btn-warn:hover { background: #d97706; }

.btn-sm {
  padding: 5px 12px;
  font-size: 13px;
  min-height: 34px;
}

/* ======================== Badge ======================== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.badge-ok     { background: #ecfccb; color: #3f6212; border: 1px solid #bef264; }
.badge-warn   { background: #fef3c7; color: #92400e; border: 1px solid #fcd34d; }
.badge-danger { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; }
.badge-muted  { background: #f1f5f9; color: #475569; border: 1px solid #cbd5e1; }
.badge-in     { background: #dbeafe; color: #1e40af; border: 1px solid #93c5fd; }
.badge-out    { background: #fef3c7; color: #92400e; border: 1px solid #fcd34d; }

/* ======================== Flash messages ======================== */
.flash {
  padding: 12px 16px;
  border-radius: 10px;
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 500;
}
.flash-success { background: #ecfccb; border: 1px solid #bef264; color: #365314; }
.flash-danger  { background: #fee2e2; border: 1px solid #fca5a5; color: #991b1b; }
.flash-warning { background: #fef3c7; border: 1px solid #fcd34d; color: #78350f; }

/* ======================== Login ======================== */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: linear-gradient(135deg, #e8f5fc 0%, #f0fbe8 100%);
}

.login-card {
  width: min(440px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px;
  box-shadow: var(--shadow-md);
}

.login-card .brand {
  text-align: center;
  border-bottom: 2px solid var(--line);
  padding-bottom: 16px;
  margin-bottom: 24px;
}

.login-logo {
  max-width: 180px;
  height: auto;
  display: block;
  margin: 0 auto;
}

.login-card .login-subtitle {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 20px;
  margin-top: -12px;
}

/* ======================== Misc ======================== */
.muted       { color: var(--muted); }
.text-right  { text-align: right; }
.text-center { text-align: center; }
.actions     { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* tx-row (form rows for stock_in/out) */
.tx-row {
  margin-bottom: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fafdff;
}

/* ======================== Grid / Row / Col ======================== */
.grid   { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }

.row { display: flex; flex-wrap: wrap; gap: 12px; }

.col-1   { flex: 0 0 calc(8.333%  - 12px); min-width: 0; }
.col-2   { flex: 0 0 calc(16.666% - 12px); min-width: 0; }
.col-3   { flex: 0 0 calc(25%     - 12px); min-width: 0; }
.col-4   { flex: 0 0 calc(33.333% - 12px); min-width: 0; }
.col-6   { flex: 0 0 calc(50%     - 12px); min-width: 0; }
.col-8   { flex: 0 0 calc(66.666% - 12px); min-width: 0; }
/* auto-width column — ไม่ขยายบน mobile ใช้กับปุ่มลบใน tx-row */
.col-auto { flex: 0 0 auto; display: flex; align-items: flex-end; }
.col-12 { flex: 0 0 100%; }

/* ======================== Stock search bar ======================== */
.stock-search-bar {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  flex-wrap: wrap;
}
.search-input   { flex: 1; min-width: 200px; }
.search-buttons { display: flex; gap: 8px; align-items: flex-end; }

/* ======================== Overlay (mobile sidebar) ======================== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(41,171,226,.18);
  z-index: 199;
  backdrop-filter: blur(2px);
}
.sidebar-overlay.show { display: block; }

/* ========================
   Responsive — Tablet (≤992px)
======================== */
@media (max-width: 992px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }

  /* sidebar กลายเป็น drawer */
  .sidebar {
    position: fixed;
    left: -240px;
    top: 0;
    bottom: 0;
    width: 240px;
    flex: none;
    overflow-y: auto;
    transition: left .25s ease;
    box-shadow: none;
  }

  .sidebar.open {
    left: 0;
    box-shadow: 4px 0 20px rgba(41,171,226,.20);
  }

  /* แสดงปุ่มปิดเฉพาะ mobile */
  .sidebar-close { display: flex; }
}

/* ========================
   Responsive — Mobile (≤768px)
======================== */
@media (max-width: 768px) {
  .container { padding: 12px; }

  .grid-4, .grid-2 { grid-template-columns: 1fr; }

  /* col ทั้งหมดเต็มแถว */
  .col-1, .col-2, .col-3, .col-4,
  .col-6, .col-8, .col-12 { flex: 0 0 100%; }

  .hide-mobile { display: none !important; }

  .dashboard-kpi-row { grid-template-columns: 1fr; }

  /* topbar compact */
  .topbar { padding: 10px 14px; }
  .topbar-left strong { font-size: 14px; }
  .topbar .muted { display: none; }

  /* table min-width เล็กลงบน mobile */
  table { min-width: 480px; }

  /* actions stack */
  .actions { flex-direction: row; flex-wrap: wrap; }

  /* tx-row mobile */
  .tx-row { padding: 10px; }

  /* card */
  .card { padding: 14px; }

  /* login */
  .login-card { padding: 24px 18px; }
}

/* ========================
   Responsive — small mobile (≤480px)
======================== */
@media (max-width: 480px) {
  .dashboard-kpi-row { grid-template-columns: 1fr; }
  .topbar-left strong { max-width: 160px; }
}

/* ======================== Dashboard extras ======================== */
@media (max-width: 900px) {
  .dashboard-kpi-row { grid-template-columns: 1fr; }
}
