@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Thai:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap');

:root {
  --bg: #f3f4f1;
  --panel: #ffffff;
  --ink: #1d2327;
  --ink-2: #4a5359;
  --ink-3: #7b858b;
  --line: #dfe2dd;
  --line-2: #eceee9;
  --steel: #2d4a5a;
  --steel-2: #3f6377;
  --steel-soft: #e6edf0;
  --ok: #2f7d4f; --ok-bg: #e5f2ea;
  --warn: #9a6400; --warn-bg: #fbf0d9;
  --bad: #b23a2e; --bad-bg: #f8e3e0;
  --info: #2e5f9e; --info-bg: #e3ecf7;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 35, .06), 0 4px 16px rgba(20, 30, 35, .05);
  --mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
.mono { font-family: var(--mono); font-size: .92em; }
.muted { color: var(--ink-3); }
.hidden { display: none !important; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---- top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--steel); color: #fff;
  display: flex; align-items: center; gap: 14px; padding: 10px 20px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .02em; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 7px; background: #fff; color: var(--steel);
  display: grid; place-items: center; font-weight: 700; font-size: 13px; font-family: var(--mono);
}
.brand small { display: block; font-weight: 400; opacity: .7; font-size: 12px; letter-spacing: 0; }
.topbar .spacer { flex: 1; }
.topbar .meta { font-size: 12.5px; opacity: .8; text-align: right; }
.btn-ghost {
  background: transparent; border: 1px solid rgba(255,255,255,.35); color: #fff;
  border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px;
}
.btn-ghost:hover { background: rgba(255,255,255,.1); }

/* ---- buttons / inputs ---- */
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 8px 14px; cursor: pointer; font-weight: 500; font-size: 14px;
}
.btn:hover { border-color: var(--ink-3); }
.btn-primary { background: var(--steel); border-color: var(--steel); color: #fff; }
.btn-primary:hover { background: var(--steel-2); border-color: var(--steel-2); }
.btn-danger { color: var(--bad); }
.btn-danger:hover { border-color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }
.input {
  width: 100%; border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 9px 12px; outline: none;
}
.input:focus { border-color: var(--steel-2); box-shadow: 0 0 0 3px var(--steel-soft); }

/* ---- login ---- */
.login-wrap { min-height: calc(100vh - 52px); display: grid; place-items: center; padding: 20px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow);
  padding: 28px; border: 1px solid var(--line);
}
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card p { margin: 0 0 18px; color: var(--ink-3); font-size: 14px; }
.login-card .btn { width: 100%; margin-top: 12px; padding: 10px; }
.form-err { color: var(--bad); font-size: 13.5px; margin-top: 10px; min-height: 1em; }

/* ---- layout ---- */
.wrap { max-width: 1320px; margin: 0 auto; padding: 18px 20px 60px; }
.controls { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
.search { position: relative; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--ink-3); }
.search .input { padding-left: 38px; font-size: 15.5px; height: 44px; }
.toggles { display: flex; gap: 8px; flex-wrap: wrap; }
.toggle {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel);
  border-radius: 999px; padding: 7px 13px; cursor: pointer; font-size: 13.5px; user-select: none; white-space: nowrap;
}
.toggle[aria-pressed="true"] { background: var(--steel); border-color: var(--steel); color: #fff; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0 4px; }
.chip {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 11px;
  font-size: 13.5px; cursor: pointer;
}
.chip .c { color: var(--ink-3); margin-left: 4px; font-size: 12px; }
.chip[aria-pressed="true"] { border-color: var(--steel); background: var(--steel-soft); color: var(--steel); font-weight: 600; }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin: 14px 0; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.kpi .l { font-size: 12.5px; color: var(--ink-3); }
.kpi .v { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .v small { font-size: 13px; font-weight: 400; color: var(--ink-3); margin-left: 3px; }

/* ---- table ---- */
.table-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.table-scroll { overflow-x: auto; }
table.grid { width: 100%; border-collapse: collapse; font-size: 14px; }
table.grid th {
  text-align: left; font-weight: 600; font-size: 12.5px; color: var(--ink-2); background: #f7f8f6;
  padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; position: sticky; top: 0;
}
table.grid th.sortable { cursor: pointer; }
table.grid th.sortable:hover { color: var(--ink); }
table.grid td { padding: 9px 12px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: #f8faf9; }
.cell-code { font-family: var(--mono); font-size: 12.5px; color: var(--ink-2); white-space: nowrap; }
.cell-size { font-weight: 600; white-space: nowrap; }
.cell-sub { font-size: 12.5px; color: var(--ink-3); }
.cell-remark { font-size: 13px; color: var(--ink-2); max-width: 260px; }
.qty-main { font-weight: 600; }
.bar { height: 4px; background: var(--line-2); border-radius: 2px; margin-top: 5px; width: 90px; margin-left: auto; position: relative; overflow: hidden; }
.bar i { position: absolute; left: 0; top: 0; bottom: 0; background: var(--steel-2); border-radius: 2px; }
.bar b { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--warn); }

.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.b-ok { background: var(--ok-bg); color: var(--ok); }
.b-warn { background: var(--warn-bg); color: var(--warn); }
.b-bad { background: var(--bad-bg); color: var(--bad); }
.b-info { background: var(--info-bg); color: var(--info); }
.empty { padding: 40px; text-align: center; color: var(--ink-3); }

/* ---- drawer ---- */
.backdrop { position: fixed; inset: 0; background: rgba(15, 25, 30, .35); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(760px, 100%); background: var(--bg); z-index: 41;
  overflow-y: auto; box-shadow: -8px 0 30px rgba(0,0,0,.12);
}
.drawer-head { position: sticky; top: 0; background: var(--panel); border-bottom: 1px solid var(--line); padding: 14px 20px; display: flex; gap: 12px; align-items: flex-start; z-index: 2; }
.drawer-head h2 { margin: 0; font-size: 19px; }
.drawer-head .x { margin-left: auto; border: 0; background: transparent; font-size: 26px; line-height: 1; cursor: pointer; color: var(--ink-3); padding: 0 4px; }
.drawer-body { padding: 16px 20px 40px; display: grid; gap: 14px; grid-template-columns: minmax(0, 1fr); }
.drawer-body > * { min-width: 0; }
.section { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.section h3 { margin: 0; font-size: 14.5px; padding: 11px 14px; border-bottom: 1px solid var(--line-2); display: flex; align-items: center; gap: 8px; }
.section h3 .r { margin-left: auto; font-weight: 400; font-size: 12.5px; }
.section .pad { padding: 12px 14px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; background: var(--line-2); }
.stats > div { background: var(--panel); padding: 10px 14px; }
.stats .l { font-size: 12px; color: var(--ink-3); }
.stats .v { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; font-size: 14px; }
.kv dt { color: var(--ink-3); }
.kv dd { margin: 0; }
table.mini { width: 100%; border-collapse: collapse; font-size: 13px; }
table.mini th { text-align: left; font-weight: 600; color: var(--ink-3); font-size: 12px; padding: 7px 10px; border-bottom: 1px solid var(--line-2); white-space: nowrap; }
table.mini td { padding: 7px 10px; border-bottom: 1px solid var(--line-2); }
table.mini td.mono { white-space: nowrap; }
table.grid th.num, table.mini th.num { text-align: right; }
table.mini tr.zero td { color: var(--ink-3); }
.linkbtn { border: 0; background: none; color: var(--steel-2); cursor: pointer; padding: 0; font-size: 13px; text-decoration: underline; }
.dir-in { color: var(--ok); font-weight: 600; }
.dir-out { color: var(--bad); font-weight: 600; }

/* ---- admin ---- */
.drop {
  display: block;
  border: 2px dashed #b9c2c6; border-radius: 12px; background: var(--panel); padding: 30px; text-align: center; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.drop.over { border-color: var(--steel-2); background: var(--steel-soft); }
.drop strong { display: block; font-size: 16px; margin-bottom: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field label { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 4px; }
.queue-item { display: grid; grid-template-columns: 1fr auto; gap: 8px 14px; padding: 12px 14px; border-bottom: 1px solid var(--line-2); align-items: center; }
.queue-item:last-child { border-bottom: 0; }
.queue-item .fn { font-size: 12.5px; color: var(--ink-3); word-break: break-all; }
.queue-item .stats-line { font-size: 13px; color: var(--ink-2); }
.queue-item .warn { font-size: 12.5px; color: var(--warn); }
.progress { height: 6px; background: var(--line-2); border-radius: 3px; overflow: hidden; grid-column: 1 / -1; }
.progress i { display: block; height: 100%; background: var(--steel-2); width: 0; transition: width .2s; }
h2.page { font-size: 18px; margin: 22px 0 10px; }

@media (max-width: 860px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .controls { grid-template-columns: 1fr; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid2 { grid-template-columns: 1fr; }
  .topbar .meta { display: none; }
  /* cards on mobile */
  table.grid thead { display: none; }
  table.grid, table.grid tbody, table.grid tr, table.grid td { display: block; width: 100%; }
  table.grid tr { border-bottom: 1px solid var(--line); padding: 10px 14px; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; }
  table.grid td { border: 0; padding: 0; }
  table.grid td.m-hide { display: none; }
  table.grid td.m-right { grid-column: 2; grid-row: 1 / span 3; text-align: right; }
  .bar { margin-left: auto; }
  .wrap { padding: 12px 12px 60px; }
  .kv { grid-template-columns: 110px 1fr; }
}
