@import url('https://fonts.googleapis.com/css2?family=Sarabun:wght@400;600;700&display=swap');

:root {
  --primary: #1565c0;
  --primary-light: #e3f2fd;
  --danger: #c62828;
  --danger-light: #ffebee;
  --success: #2e7d32;
  --success-light: #e8f5e9;
  --warning: #e65100;
  --warning-light: #fff3e0;
  --muted: #757575;
  --border: #e0e0e0;
  --bg: #f5f6fa;
  --card: #fff;
  --sidebar-w: 200px;
  --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Sarabun', sans-serif; background: var(--bg); font-size: 15px; color: #212121; }

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

.sidebar {
  width: var(--sidebar-w);
  background: #1a237e;
  color: #fff;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 50;
  padding: 0 0 16px;
}
.sidebar-logo {
  padding: 16px 14px 12px;
  font-size: 1rem;
  font-weight: 700;
  border-bottom: 1px solid rgba(255,255,255,.12);
  margin-bottom: 8px;
}
.sidebar-section {
  font-size: .7rem;
  color: rgba(255,255,255,.5);
  padding: 12px 14px 4px;
  text-transform: uppercase;
  letter-spacing: .6px;
}
.nav-link {
  display: block;
  padding: 9px 14px;
  color: rgba(255,255,255,.82);
  text-decoration: none;
  font-size: .88rem;
  border-radius: 6px;
  margin: 1px 8px;
  transition: background .15s;
}
.nav-link:hover { background: rgba(255,255,255,.12); color: #fff; }
.nav-link.active { background: rgba(255,255,255,.2); color: #fff; font-weight: 600; }

.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  padding: 24px;
  min-height: 100vh;
}
.page-title {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ── Card ────────────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,.06);
  padding: 16px;
  margin-bottom: 12px;
}

/* ── Metrics bar ─────────────────────────────────────────────────────────── */
.metrics { display: flex; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.metric {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 18px;
  min-width: 120px;
  flex: 1;
}
.metric-val { font-size: 1.6rem; font-weight: 700; line-height: 1; }
.metric-lbl { font-size: .78rem; color: var(--muted); margin-top: 4px; }

/* ── Chip filters ────────────────────────────────────────────────────────── */
.chips, .chip-group { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.chip {
  padding: 5px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--card);
  cursor: pointer;
  font-size: .82rem;
  transition: all .15s;
  white-space: nowrap;
}
.chip:hover { border-color: var(--primary); color: var(--primary); }
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  font-size: .85rem;
  font-family: inherit;
  font-weight: 600;
  transition: all .15s;
  text-decoration: none;
  white-space: nowrap;
}
.btn-primary   { background: var(--primary);   color: #fff; }
.btn-primary:hover   { background: #0d47a1; }
.btn-danger    { background: var(--danger);    color: #fff; }
.btn-danger:hover    { background: #b71c1c; }
.btn-outline   { background: transparent; border-color: var(--border); color: #444; }
.btn-outline:hover   { border-color: #aaa; background: #f5f5f5; }
.btn-ghost     { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover     { background: #f0f0f0; color: #333; }
.btn-sm        { padding: 4px 10px; font-size: .78rem; }
.btn-icon      { padding: 5px 8px; border-radius: 5px; }

/* ── Badge ───────────────────────────────────────────────────────────────── */
.badge { display: inline-block; padding: 2px 10px; border-radius: 12px; font-size: .72rem; font-weight: 700; background: #eee; color: #555; }
.badge-draft    { background: #e8eaf6; color: #3949ab; }
.badge-waiting  { background: #fff8e1; color: #f57f17; }
.badge-partial  { background: #fff3e0; color: #e65100; }
.badge-complete { background: #e8f5e9; color: #2e7d32; }
.badge-cancel   { background: #f5f5f5; color: #757575; }
.badge-open     { background: var(--primary-light); color: var(--primary); }
.badge-warn     { background: #fff8e1; color: #f57f17; }
.badge-ok       { background: #e8f5e9; color: #2e7d32; }

/* ── Table ───────────────────────────────────────────────────────────────── */
.tbl-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
.tbl {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
}
.tbl thead th {
  background: #f0f2f6;
  padding: 9px 12px;
  text-align: left;
  font-weight: 600;
  font-size: .78rem;
  color: #444;
  border-bottom: 2px solid #d0d4e0;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 2;
}
.tbl thead th.sortable { cursor: pointer; user-select: none; }
.tbl thead th.sortable:hover { background: #e4e8f0; }
.tbl thead th.sort-active { color: var(--primary); }
.tbl tbody tr { border-bottom: 1px solid #f0f0f0; transition: background .1s; }
.tbl tbody tr:hover { background: #f0f4ff; }
.tbl tbody tr.row-selected { background: #e8f0fe; }
.tbl tbody tr:last-child { border-bottom: none; }
.tbl td { padding: 8px 12px; vertical-align: middle; }
.tbl td.mono { font-family: monospace; font-size: .8rem; color: #555; }
.tbl td.num  { text-align: right; font-weight: 600; }
.tbl td.muted { color: var(--muted); font-size: .78rem; }

.qty-ok     { color: #2e7d32; }
.qty-low    { color: #e65100; }
.qty-empty  { color: #9e9e9e; }
.qty-urgent { color: var(--danger); font-weight: 700; }

/* ── Inline panel (ขยายใต้แถว) ──────────────────────────────────────────── */
.inline-panel {
  display: none;
  background: #f8f9fb;
  border-top: 2px solid var(--primary-light);
  padding: 16px;
}
.inline-panel.open { display: block; }

/* ── Popover ────────────────────────────────────────────────────────────── */
.pop-wrap { position: relative; display: inline-block; }
.pop {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 200;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  min-width: 300px;
  box-shadow: 0 6px 24px rgba(0,0,0,.13);
}
.pop.open { display: block; }
.pop-title { font-weight: 700; font-size: .9rem; margin-bottom: 12px; }

/* ── Form controls ───────────────────────────────────────────────────────── */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: .78rem; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: .88rem;
  font-family: inherit;
  background: #fff;
  transition: border-color .15s;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.radio-group { display: flex; gap: 12px; }
.radio-group label { display: flex; align-items: center; gap: 6px; font-size: .85rem; cursor: pointer; color: #333; font-weight: 400; }
.radio-group input[type=radio] { width: auto; }

/* ── Search bar ─────────────────────────────────────────────────────────── */
.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  align-items: center;
}
.search-bar input {
  flex: 1;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .88rem;
  font-family: inherit;
}
.search-bar input:focus { outline: none; border-color: var(--primary); }

/* ── Traffic light ───────────────────────────────────────────────────────── */
.tl-green  { color: #2e7d32; font-weight: 700; }
.tl-orange { color: #e65100; font-weight: 700; }
.tl-red    { color: var(--danger); font-weight: 700; }
.tl-grey   { color: #bbb; }

/* ── Progress bar ────────────────────────────────────────────────────────── */
.progress { background: #e0e0e0; border-radius: 4px; height: 5px; overflow: hidden; margin-top: 4px; }
.progress-bar { height: 100%; border-radius: 4px; background: var(--primary); transition: width .3s; }

/* ── Toast ───────────────────────────────────────────────────────────────── */
#toast-container { position: fixed; bottom: 24px; right: 24px; z-index: 9999; display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 12px 18px;
  border-radius: 8px;
  font-size: .88rem;
  font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,.15);
  animation: slideIn .2s ease;
  max-width: 320px;
}
.toast-success { background: #e8f5e9; color: #1b5e20; border-left: 4px solid #43a047; }
.toast-error   { background: #ffebee; color: #b71c1c; border-left: 4px solid #e53935; }
.toast-warning { background: #fff8e1; color: #e65100; border-left: 4px solid #ffa000; }
@keyframes slideIn { from { transform: translateX(40px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ── Spinner overlay ─────────────────────────────────────────────────────── */
#spinner {
  display: none;
  position: fixed; inset: 0; z-index: 9998;
  background: rgba(255,255,255,.65);
  align-items: center;
  justify-content: center;
}
#spinner.on { display: flex; }
.spinner-ring {
  width: 40px; height: 40px;
  border: 4px solid #e0e0e0;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Divider ─────────────────────────────────────────────────────────────── */
.divider { border: none; border-top: 1px solid var(--border); margin: 16px 0; }

/* ── Tabs ────────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--border); margin-bottom: 16px; }
.tab-btn, .tab {
  padding: 8px 18px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: .88rem;
  font-family: inherit;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all .15s;
}
.tab-btn:hover, .tab:hover { color: var(--primary); }
.tab-btn.active, .tab.active { color: var(--primary); font-weight: 700; border-bottom-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ── Misc ────────────────────────────────────────────────────────────────── */
.text-muted { color: var(--muted); }
.text-sm    { font-size: .8rem; }
.text-right { text-align: right; }
.flex       { display: flex; }
.flex-gap   { display: flex; gap: 8px; align-items: center; }
.actions-cell { display: flex; gap: 4px; justify-content: flex-end; }
.mt-8  { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8  { margin-bottom: 8px; }

/* ── Drag-and-drop rows ──────────────────────────────────────────────────── */
#create-items-tbody tr { transition: opacity .15s; }
#create-items-tbody tr.drag-dragging { opacity: .4; }
#create-items-tbody tr.drag-over td { background: #e3f2fd; }
.drag-handle { cursor: grab; color: #bdbdbd; font-size: 1rem; user-select: none; }
.drag-handle:active { cursor: grabbing; }
