:root {
  --bg: #0f172a;
  --bg-soft: #1e293b;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-soft: #64748b;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #16a34a;
  --amber: #d97706;
  --red: #dc2626;
  --slate: #64748b;
  --purple: #7c3aed;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Sarabun', system-ui, -apple-system, sans-serif;
  background: var(--surface-2);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: 256px; flex-shrink: 0;
  background: var(--bg); color: #cbd5e1;
  display: flex; flex-direction: column;
  padding: 20px 14px;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 6px 8px 20px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 10px; overflow: hidden;
  background: linear-gradient(135deg, var(--primary), var(--purple));
  display: grid; place-items: center; font-weight: 700; color: #fff; font-size: 16px;
}
.brand-mark img { width: 100%; height: 100%; display: block; }
.brand-title { color: #fff; font-weight: 700; font-size: 16px; }
.brand-sub { font-size: 11px; color: #64748b; }

/* Mode switch (Asset vs Warehouse) */
#mode-switch {
  display: flex; gap: 4px; padding: 4px;
  background: var(--bg-soft); border-radius: 10px; margin: 0 4px 12px;
}
.mode-btn {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 6px; border: none; border-radius: 7px; cursor: pointer;
  background: transparent; color: #94a3b8; font-family: inherit;
  font-size: 13px; font-weight: 600; transition: .15s;
}
.mode-btn:hover { color: #fff; }
.mode-btn.active { background: var(--primary); color: #fff; }
.mode-btn .ico { font-size: 15px; }

#nav { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
#nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
  color: #cbd5e1; font-size: 14px; font-weight: 500; transition: .15s;
}
#nav a:hover { background: var(--bg-soft); color: #fff; }
#nav a.active { background: var(--primary); color: #fff; }
#nav a { position: relative; }
.nav-badge { position:absolute; top:6px; right:8px; background:#ef4444; color:#fff; border-radius:999px; font-size:11px; font-weight:700; min-width:18px; height:18px; display:flex; align-items:center; justify-content:center; padding:0 4px; line-height:1; }
.nav-divider { height: 1px; background: #334155; margin: 8px 4px; opacity: .6; }
.ico { font-size: 16px; }
.sidebar-foot { margin-top: auto; font-size: 11px; color: #475569; padding: 8px; }
.lang-toggle {
  margin: 8px 0 4px; width: 100%; padding: 7px 10px; border-radius: 8px; cursor: pointer;
  background: #1e293b; color: #cbd5e1; border: 1px solid #334155; font-size: 12px; font-weight: 600;
  font-family: inherit; transition: background .15s, color .15s;
}
.lang-toggle:hover { background: #334155; color: #fff; }

/* Content */
.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 28px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 20px; font-weight: 700; }
#view { padding: 24px 28px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-family: inherit;
  font-size: 14px; font-weight: 500; cursor: pointer; transition: .15s;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { color: var(--red); border-color: #fecaca; }
.btn-danger:hover { background: #fef2f2; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* Cards / stats */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 18px; box-shadow: var(--shadow);
}
.stat-card .stat-label { font-size: 13px; color: var(--text-soft); font-weight: 500; }
.stat-card .stat-value { font-size: 30px; font-weight: 700; margin-top: 6px; }
.stat-card .stat-meta { font-size: 12px; color: var(--text-soft); margin-top: 4px; }
.stat-accent { width: 36px; height: 4px; border-radius: 2px; margin-bottom: 12px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.grid-2 > * { min-width: 0; }
.section-title { font-size: 15px; font-weight: 600; margin-bottom: 14px; }

/* Bar chart rows */
.bar-row { display: grid; grid-template-columns: 130px 1fr 44px; align-items: center; gap: 10px; margin-bottom: 10px; }
.bar-label { font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: var(--surface-2); border-radius: 6px; height: 22px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; background: linear-gradient(90deg, var(--primary), var(--purple)); transition: width .4s; }
.bar-num { font-size: 13px; font-weight: 600; text-align: right; color: var(--text-soft); }

/* Table */
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.toolbar input[type=search], .toolbar select {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-family: inherit; font-size: 14px; background: var(--surface);
}
.toolbar input[type=search] { flex: 1; min-width: 200px; }
.spacer { flex: 1; }

/* Bulk action bar (shown when assets are selected) */
.bulk-bar { display: none; align-items: center; gap: 10px; margin-bottom: 14px; padding: 10px 14px;
  background: #eff6ff; border: 1px solid #bfdbfe; border-radius: 10px; flex-wrap: wrap; }
.bulk-bar.show { display: flex; }
.bulk-count { font-weight: 600; color: var(--primary-dark); margin-inline-end: 4px; }
.bulk-bar .bulk-clear { margin-inline-start: auto; }
.bulk-bar button[disabled] { opacity: .45; cursor: not-allowed; }
.chk-col { width: 38px; text-align: center; }
.chk-col input { width: 16px; height: 16px; cursor: pointer; accent-color: var(--primary); }

.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: right; padding: 12px 14px; font-size: 14px; }
th { background: var(--surface-2); color: var(--text-soft); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; border-bottom: 1px solid var(--border); }
td { border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.muted { color: var(--text-soft); }
.t-actions { display: flex; gap: 6px; justify-content: flex-start; }

/* Status badge */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.available { color: var(--green); background: #dcfce7; }
.badge.in_use { color: var(--primary); background: #dbeafe; }
.badge.maintenance { color: var(--amber); background: #fef3c7; }
.badge.retired { color: var(--slate); background: #f1f5f9; }
.badge.lost { color: var(--red); background: #fee2e2; }

.pill { display:inline-block; padding: 2px 8px; border-radius: 6px; font-size: 12px; background: var(--surface-2); color: var(--text-soft); }
.warn-text { color: var(--red); font-weight: 600; }
.amber-text { color: var(--amber); font-weight: 600; }

/* Empty */
.empty { text-align: center; padding: 48px 20px; color: var(--text-soft); }

/* WMS movement badges */
.move-badge {
  display: inline-block; padding: 2px 8px; border-radius: 6px;
  font-size: 11px; font-weight: 600; vertical-align: middle;
}
.move-in { color: var(--green); background: #dcfce7; }
.move-out { color: var(--red); background: #fee2e2; }
.move-adjust { color: var(--amber); background: #fef3c7; }
.move-transfer { color: var(--primary); background: #dbeafe; }

/* Machine multi-select checkbox group (stock item form) */
.check-group {
  display: flex; flex-wrap: wrap; gap: 8px;
  max-height: 160px; overflow-y: auto;
  padding: 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2);
}
.check-item {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 6px; background: var(--surface);
  border: 1px solid var(--border); font-size: 13px; cursor: pointer;
}
.check-item input { margin: 0; }

/* Requisition line editor rows */
.line-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.line-row select { flex: 1; min-width: 0; }

/* Stock-count variance cells */
.count-diff { font-weight: 700; text-align: center; }
.count-diff.diff-up { color: var(--green); }
.count-diff.diff-down { color: var(--red); }
.count-inp { text-align: right; }

/* Checkbox filter label in toolbars */
.chk-label {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 14px; color: var(--text); cursor: pointer; user-select: none;
}
.chk-label input { width: 16px; height: 16px; cursor: pointer; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,.5);
  display: grid; place-items: center; z-index: 100; padding: 20px;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 560px;
  max-height: 90vh; overflow: auto; box-shadow: 0 20px 50px rgba(0,0,0,.3);
}
.modal-head { padding: 18px 22px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { font-size: 17px; }
.modal-body { padding: 22px; }
.modal-foot { padding: 16px 22px; border-top: 1px solid var(--border); display: flex; gap: 10px; justify-content: flex-start; }
.close-x { cursor: pointer; font-size: 22px; color: var(--text-soft); background: none; border: none; line-height: 1; }

/* Form */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 13px; font-weight: 500; color: var(--text-soft); }
.field input, .field select, .field textarea {
  padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px;
  font-family: inherit; font-size: 14px; background: var(--surface);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.field textarea { resize: vertical; min-height: 64px; }

/* History list */
.hist { list-style: none; }
.hist li { display: flex; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.hist .dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex-shrink: 0; }
.hist .dot.out { background: var(--primary); }
.hist .dot.in { background: var(--green); }

/* Toast */
#toast-root { position: fixed; bottom: 22px; right: 22px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast { background: var(--bg); color: #fff; padding: 12px 18px; border-radius: 10px; font-size: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.25); animation: slidein .2s ease; }
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }
@keyframes slidein { from { transform: translateY(10px); opacity: 0; } }

.loading { padding: 40px; text-align: center; color: var(--text-soft); }

/* Login */
#login-screen {
  position: fixed; inset: 0; z-index: 300;
  background: linear-gradient(135deg, #1e293b, #0f172a);
  display: grid; place-items: center; padding: 20px;
}
.login-card {
  background: var(--surface); border-radius: 16px; padding: 32px;
  width: 100%; max-width: 380px; box-shadow: 0 20px 60px rgba(0,0,0,.4);
  display: flex; flex-direction: column; gap: 14px;
}
.login-brand { display: flex; align-items: center; gap: 12px; justify-content: center; }
.login-brand h2 { font-size: 22px; }
.login-err { color: var(--red); font-size: 13px; min-height: 18px; text-align: center; }
.login-hint { font-size: 12px; color: var(--text-soft); text-align: center; margin-top: 6px; }

/* User box (sidebar) */
#user-box .user-name { color: #e2e8f0; font-size: 13px; font-weight: 600; }
#user-box .user-role { color: #64748b; font-size: 11px; }

/* Barcode label */
.bc-preview { display: grid; place-items: center; padding: 8px; }
.bc-label { border: 1px solid var(--border); border-radius: 8px; padding: 16px 20px; text-align: center; background: #fff; }
.bc-name { font-weight: 600; font-size: 15px; margin-bottom: 8px; }
.bc-meta { color: var(--text-soft); font-size: 12px; margin-top: 6px; }
.bc-svg svg { max-width: 100%; height: auto; }

@media (max-width: 860px) {
  .sidebar { width: 64px; padding: 16px 8px; }
  .brand-title, .brand-sub, #nav a span:not(.ico), .mode-btn span:not(.ico), .sidebar-foot { display: none; }
  #nav a { justify-content: center; }
  #mode-switch { flex-direction: column; }
  .grid-2 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  th:nth-child(n+5), td:nth-child(n+5) { display: none; }
}
