/* ============ TOKENS ============ */
:root {
  --bg: #fbfaf7;
  --surface: #ffffff;
  --surface-2: #f5f4ef;
  --border: #e7e5dd;
  --border-strong: #d6d3c8;
  --fg: #0a0a0a;
  --fg-2: #404040;
  --fg-3: #737373;
  --fg-4: #a3a3a3;

  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --accent-soft: #eff6ff;
  --accent-ring: rgba(37, 99, 235, 0.18);

  --success: #16a34a;
  --success-soft: #ecfdf5;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn: #d97706;
  --warn-soft: #fffbeb;

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;

  --shadow-sm: 0 1px 2px rgba(10, 10, 10, 0.04), 0 1px 1px rgba(10,10,10,0.03);
  --shadow: 0 4px 16px rgba(10, 10, 10, 0.06), 0 1px 2px rgba(10,10,10,0.04);
  --shadow-lg: 0 20px 50px rgba(10,10,10,0.12), 0 4px 12px rgba(10,10,10,0.06);

  --font-sans: "IBM Plex Sans Thai", "IBM Plex Sans", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", monospace;

  --sidebar-w: 244px;
}

* { box-sizing: border-box; }
html, body, #root { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--fg);
  font-size: 14.5px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button { font-family: inherit; }
input, textarea, select { font-family: inherit; font-size: inherit; color: inherit; }
a { color: inherit; }

.mono { font-family: var(--font-mono); font-feature-settings: "tnum"; }
.tnum { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

/* ============ LAYOUT ============ */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  gap: 4px;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 8px 18px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 10px;
}
.sidebar-brand-mark {
  width: 30px; height: 30px;
  background: var(--fg);
  color: #fbfaf7;
  border-radius: 7px;
  display: grid; place-items: center;
  font-weight: 600;
  font-size: 14px;
  letter-spacing: -0.02em;
}
.sidebar-brand-name { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.sidebar-brand-sub { font-size: 11.5px; color: var(--fg-3); margin-top: 1px; }

.nav-section-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-4);
  padding: 14px 10px 6px;
  font-weight: 500;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--fg-2);
  font-size: 13.5px;
  cursor: pointer;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  transition: background 120ms ease, color 120ms ease;
}
.nav-item:hover { background: var(--surface-2); color: var(--fg); }
.nav-item.active {
  background: var(--fg);
  color: var(--surface);
}
.nav-item.active .nav-ico { opacity: 1; }
.nav-ico { width: 16px; height: 16px; opacity: 0.7; flex-shrink: 0; }
.nav-badge {
  margin-left: auto;
  font-size: 10.5px;
  background: var(--danger);
  color: white;
  padding: 1px 6px;
  border-radius: 999px;
  font-weight: 500;
}
.nav-item.active .nav-badge { background: var(--surface); color: var(--fg); }

.sidebar-user {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding: 12px 8px 0;
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center;
  font-weight: 600; font-size: 12.5px;
  flex-shrink: 0;
}
.sidebar-user-name { font-size: 13px; font-weight: 500; }
.sidebar-user-role { font-size: 11px; color: var(--fg-3); }

/* ============ MAIN ============ */
.main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.topbar {
  height: 60px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 0 24px;
  display: flex; align-items: center; gap: 16px;
  flex-shrink: 0;
}
.topbar-title { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; }
.topbar-sub { font-size: 12.5px; color: var(--fg-3); white-space: nowrap; }
.topbar-spacer { flex: 1; }
.topbar-search {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  min-width: 260px;
  transition: all 120ms ease;
}
.topbar-search:focus-within { background: white; border-color: var(--border-strong); box-shadow: 0 0 0 4px var(--accent-ring); }
.topbar-search input { background: transparent; border: none; outline: none; width: 100%; font-size: 13px; }

.scroll-area {
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px 64px;
}
.page-header {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}
.page-title { font-size: 24px; font-weight: 600; letter-spacing: -0.018em; }
.page-sub { color: var(--fg-3); font-size: 13.5px; margin-top: 4px; }

/* ============ BUTTONS ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 7px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 120ms ease;
  white-space: nowrap;
  user-select: none;
}
.btn:focus-visible { outline: none; box-shadow: 0 0 0 4px var(--accent-ring); }
.btn-primary { background: var(--fg); color: var(--surface); }
.btn-primary:hover { background: #262626; }
.btn-accent { background: var(--accent); color: white; }
.btn-accent:hover { background: var(--accent-2); }
.btn-success { background: var(--success); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-secondary {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--fg);
}
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--fg-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--fg); }
.btn-sm { padding: 5px 9px; font-size: 12px; }
.btn-lg { padding: 11px 18px; font-size: 14px; }
.btn-icon { padding: 7px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ============ INPUTS ============ */
.field { display: flex; flex-direction: column; gap: 5px; }
.label { font-size: 12px; font-weight: 500; color: var(--fg-2); }
.hint { font-size: 11.5px; color: var(--fg-3); }
.input, .select, .textarea {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 11px;
  font-size: 13.5px;
  outline: none;
  transition: all 120ms ease;
  width: 100%;
}
.input:focus, .select:focus, .textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-ring);
}
.input-with-prefix { position: relative; }
.input-prefix {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--fg-4); pointer-events: none;
}
.input-with-prefix .input { padding-left: 32px; }

/* ============ CARDS ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}
.card-pad-0 { padding: 0; }
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.card-header h3 { margin: 0; font-size: 14px; font-weight: 600; }
.card-header-sub { font-size: 12px; color: var(--fg-3); }

/* ============ STATS GRID ============ */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 22px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  position: relative;
  overflow: hidden;
  transition: transform 200ms ease, box-shadow 200ms ease;
}
.stat:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.stat-label {
  font-size: 12px;
  color: var(--fg-3);
  display: flex; align-items: center; gap: 6px;
}
.stat-value {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.025em;
  margin-top: 6px;
  font-variant-numeric: tabular-nums;
}
.stat-delta {
  font-size: 12px;
  margin-top: 4px;
  display: inline-flex; align-items: center; gap: 4px;
}
.stat-delta.up { color: var(--success); }
.stat-delta.down { color: var(--danger); }
.stat-bar {
  position: absolute;
  left: 0; right: 0; bottom: 0; height: 3px;
  background: linear-gradient(90deg, var(--accent), transparent);
  opacity: 0.6;
}

/* ============ TABLE ============ */
.table-wrap { overflow: auto; border-radius: var(--radius); }
table.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.table thead th {
  background: var(--surface-2);
  text-align: left;
  font-weight: 500;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-3);
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table tbody td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr { transition: background 100ms ease; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: none; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .clickable { cursor: pointer; }

/* ============ BADGES ============ */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--fg-2);
  border: 1px solid var(--border);
}
.badge-success { background: var(--success-soft); color: var(--success); border-color: rgba(22,163,74,0.18); }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: rgba(220,38,38,0.18); }
.badge-warn { background: var(--warn-soft); color: var(--warn); border-color: rgba(217,119,6,0.18); }
.badge-accent { background: var(--accent-soft); color: var(--accent); border-color: rgba(37,99,235,0.18); }
.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(10,10,10,0.42);
  display: grid; place-items: center;
  z-index: 100;
  animation: fadeIn 160ms ease;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: min(560px, 92vw);
  max-height: 88vh;
  display: flex; flex-direction: column;
  animation: slideUp 220ms cubic-bezier(0.16, 1, 0.3, 1);
}
.modal-lg { width: min(820px, 94vw); }
.modal-header {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-title { font-size: 16px; font-weight: 600; }
.modal-body { padding: 18px 22px; overflow-y: auto; }
.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(12px) scale(0.98); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes pop { 0% { transform: scale(0.92); opacity: 0; } 60% { transform: scale(1.02); } 100% { transform: scale(1); opacity: 1; } }
@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

/* ============ TOAST ============ */
.toast-stack {
  position: fixed; right: 22px; bottom: 22px;
  display: flex; flex-direction: column-reverse; gap: 8px;
  z-index: 200;
  pointer-events: none;
}
.toast {
  background: var(--fg);
  color: var(--surface);
  padding: 11px 14px 11px 12px;
  border-radius: var(--radius);
  font-size: 13px;
  display: flex; align-items: center; gap: 10px;
  min-width: 280px;
  box-shadow: var(--shadow-lg);
  animation: slideUp 200ms ease;
  pointer-events: auto;
}
.toast .toast-ico { width: 18px; height: 18px; flex-shrink: 0; }
.toast.success .toast-ico { color: #4ade80; }
.toast.danger .toast-ico { color: #f87171; }
.toast.info .toast-ico { color: #60a5fa; }

/* ============ CHARTS ============ */
.chart-grid line { stroke: var(--border); stroke-dasharray: 2 3; }
.chart-axis text { fill: var(--fg-3); font-size: 10.5px; font-family: var(--font-mono); }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chart-area { fill: url(#chart-gradient); opacity: 0.6; }
.chart-point { fill: white; stroke: var(--accent); stroke-width: 2; }
.bar-receive { fill: var(--success); }
.bar-issue { fill: var(--danger); }
.bar-receive-soft { fill: var(--success); opacity: 0.18; }
.bar-issue-soft { fill: var(--danger); opacity: 0.18; }

@keyframes draw-line {
  from { stroke-dashoffset: 1000; }
  to { stroke-dashoffset: 0; }
}
.chart-line.animate {
  stroke-dasharray: 1000;
  animation: draw-line 1.4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
@keyframes grow-bar {
  from { transform: scaleY(0); }
  to { transform: scaleY(1); }
}
.bar-animate { transform-origin: bottom; animation: grow-bar 700ms cubic-bezier(0.16, 1, 0.3, 1) forwards; }

/* ============ WAREHOUSE MAP ============ */
.warehouse-map {
  display: grid;
  gap: 10px;
}
.location-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  cursor: pointer;
  position: relative;
  transition: all 160ms ease;
  min-height: 70px;
}
.location-cell:hover { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-ring); transform: translateY(-1px); }
.location-cell .row > .btn-ghost.btn-icon { opacity: 0; transition: opacity 120ms ease; }
.location-cell:hover .row > .btn-ghost.btn-icon { opacity: 0.7; }
.location-cell:hover .row > .btn-ghost.btn-icon:hover { opacity: 1; }
.location-cell.empty { background: repeating-linear-gradient(45deg, var(--surface), var(--surface) 8px, var(--surface-2) 8px, var(--surface-2) 12px); }
.location-cell.full { border-color: var(--success); }
.location-cell.high { border-color: var(--warn); }
.location-code { font-family: var(--font-mono); font-size: 11px; color: var(--fg-3); }
.location-qty { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.location-fill {
  position: absolute; left: 0; right: 0; bottom: 0;
  height: 3px;
  background: var(--accent);
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  transition: width 400ms ease;
}

/* ============ LOGIN ============ */
.login-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr 1fr;
}
.login-side {
  background: var(--fg);
  color: var(--surface);
  padding: 48px;
  display: flex; flex-direction: column; justify-content: space-between;
  position: relative;
  overflow: hidden;
}
.login-side .pattern {
  position: absolute; inset: 0;
  background-image:
    radial-gradient(circle at 20% 20%, rgba(37,99,235,0.18), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(255,255,255,0.06), transparent 40%);
  pointer-events: none;
}
.login-side .pattern svg { width: 100%; height: 100%; opacity: 0.07; }
.login-form-side {
  display: grid; place-items: center;
  padding: 48px;
}
.login-card { width: 100%; max-width: 360px; }

/* ============ MISC ============ */
.divider { height: 1px; background: var(--border); border: none; margin: 14px 0; }
.row { display: flex; align-items: center; gap: 10px; }
.col { display: flex; flex-direction: column; gap: 10px; }
.spacer { flex: 1; }
.muted { color: var(--fg-3); }
.text-mono { font-family: var(--font-mono); }
.text-sm { font-size: 12.5px; }
.text-xs { font-size: 11.5px; }
.uppercase { text-transform: uppercase; letter-spacing: 0.06em; }

.kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  color: var(--fg-3);
}

.pulse-dot {
  width: 7px; height: 7px;
  background: var(--success);
  border-radius: 50%;
  position: relative;
  display: inline-block;
}
.pulse-dot::after {
  content: '';
  position: absolute; inset: -3px;
  border-radius: 50%;
  background: var(--success);
  opacity: 0.35;
  animation: pulse-ring 1.6s ease-out infinite;
}
@keyframes pulse-ring {
  0% { transform: scale(0.7); opacity: 0.5; }
  100% { transform: scale(2.2); opacity: 0; }
}

.skeleton {
  background: linear-gradient(90deg, var(--surface-2), #ebe9e1, var(--surface-2));
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: 4px;
}

.tabs {
  display: flex; gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 18px;
}
.tab {
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 500;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--fg-3);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: all 120ms ease;
}
.tab:hover { color: var(--fg); }
.tab.active { color: var(--fg); border-bottom-color: var(--fg); }

.empty-state {
  text-align: center;
  padding: 48px 18px;
  color: var(--fg-3);
}
.empty-state-ico {
  width: 40px; height: 40px;
  margin: 0 auto 12px;
  opacity: 0.4;
}

/* ============ BARCODE SCAN ============ */
.scan-frame {
  position: relative;
  background: #0a0a0a;
  border-radius: var(--radius);
  aspect-ratio: 16 / 9;
  overflow: hidden;
}
.scan-frame .scan-line {
  position: absolute; left: 6%; right: 6%;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  box-shadow: 0 0 12px var(--accent);
  animation: scan-move 1.6s ease-in-out infinite alternate;
  top: 50%;
}
@keyframes scan-move {
  from { top: 18%; }
  to { top: 82%; }
}
.scan-corners {
  position: absolute; inset: 12%;
  border-radius: 8px;
}
.scan-corners::before, .scan-corners::after,
.scan-corners > i::before, .scan-corners > i::after {
  content: ''; position: absolute;
  width: 22px; height: 22px;
  border: 2px solid var(--accent);
}
.scan-corners::before { top: 0; left: 0; border-right: none; border-bottom: none; }
.scan-corners::after { top: 0; right: 0; border-left: none; border-bottom: none; }
.scan-corners > i::before { bottom: 0; left: 0; border-right: none; border-top: none; position: absolute; }
.scan-corners > i::after { bottom: 0; right: 0; border-left: none; border-top: none; position: absolute; }
.scan-fake-product {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: rgba(255,255,255,0.18);
  font-family: var(--font-mono);
  letter-spacing: 0.2em;
  font-size: 11px;
}

/* ============ Print styles (Export PDF) ============ */
@media print {
  @page { size: A4; margin: 14mm; }
  body { background: #fff !important; }
  .sidebar, .topbar, .tweaks-fab, .tweaks-panel, .toast-stack, .no-print { display: none !important; }
  .app-shell { display: block !important; }
  .main { padding: 0 !important; margin: 0 !important; max-width: none !important; }
  .card { box-shadow: none !important; border: 1px solid #ddd !important; break-inside: avoid; }
  .page-header { margin-bottom: 12px; }
  .table-wrap { max-height: none !important; overflow: visible !important; }
  table { break-inside: auto; }
  tr { break-inside: avoid; break-after: auto; }
  thead { display: table-header-group; }
  .bar-animate, .chart-grid line { animation: none !important; }
}
