/* ARCADIA CRM — หน้าตาตามต้นแบบที่พี่เล็กอนุมัติ (design/crm-prototype.html)
   Layout: เต็มความกว้างจอทุกขนาด · จำนวนคอลัมน์ปรับเองด้วย grid auto-fill / flex-wrap (ไม่พึ่ง media query — Center-Safe Mobile)
   media query ใช้แค่สลับเมนู: จอ >= 900px เมนูอยู่แถบบน / จอเล็กเมนูอยู่แถบล่าง (ถ้า media query ไม่ทำงาน ก็ยังใช้แถบล่างได้ปกติ) */
:root {
  --bg: #f3f5f2; --surface: #ffffff; --surface-2: #e9eee9; --ink: #18221e; --muted: #5d6b64; --line: #d5ddd7;
  --accent: #13795b; --accent-ink: #ffffff; --accent-soft: #dcefe6;
  --ok: #1d7a3e; --ok-soft: #ddf1e3; --warn: #9a5b00; --warn-soft: #fbeccf; --bad: #b3261e; --bad-soft: #fbe1de; --info: #1f5fa8; --info-soft: #dfe9f7;
  --f-display: "Bai Jamjuree", "IBM Plex Sans Thai", Tahoma, sans-serif;
  --f-body: "IBM Plex Sans Thai", Tahoma, "Leelawadee UI", sans-serif;
  --f-mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;
}
@media (prefers-color-scheme: dark) { :root {
  --bg: #111715; --surface: #18201d; --surface-2: #222c28; --ink: #e6ede9; --muted: #9aaba3; --line: #2f3b36;
  --accent: #4cc39a; --accent-ink: #0b1a14; --accent-soft: #1d3a2f;
  --ok: #6fd08f; --ok-soft: #1c3424; --warn: #f0b95a; --warn-soft: #3a2d14; --bad: #ff8a80; --bad-soft: #3d1c1a; --info: #8ab8f0; --info-soft: #1a2a3d;
  color-scheme: dark } }

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-family: var(--f-body); font-size: 15px; line-height: 1.55; min-height: 100vh; }
h1, h2, h3 { font-family: var(--f-display); margin: 0; line-height: 1.25; text-wrap: balance; }
a { color: var(--accent); }
[hidden] { display: none !important; }

/* ---------- โครงหน้า ---------- */
.topbar { position: sticky; top: 0; z-index: 5; background: var(--surface); border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top, 0px); }
.topbar-in { display: flex; align-items: center; gap: 10px; min-height: 56px;
  padding: 8px max(16px, 2vw, env(safe-area-inset-right, 0px)) 8px max(16px, 2vw, env(safe-area-inset-left, 0px)); }
.topbar h1 { font-size: 17px; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.topbar .brand { font-family: var(--f-display); font-weight: 700; font-size: 17px; white-space: nowrap; color: var(--ink); text-decoration: none; display: none; }
.topnav { display: none; gap: 4px; flex: none; }
.topnav a { padding: 7px 12px; border-radius: 9px; color: var(--muted); text-decoration: none; font: 500 14px var(--f-body); white-space: nowrap; }
.topnav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.topbar .back { border: 0; background: var(--surface-2); color: var(--ink); width: 36px; height: 36px; border-radius: 10px; font-size: 20px; line-height: 1; cursor: pointer; text-decoration: none; display: grid; place-items: center; flex: none; }
.menu-btn { border: 1px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 10px; padding: 6px 10px; font: 500 13px var(--f-body); cursor: pointer; flex: none; }
.menu { position: absolute; right: max(16px, env(safe-area-inset-right, 0px)); top: calc(56px + env(safe-area-inset-top, 0px)); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,.12); min-width: 200px; display: flex; flex-direction: column; padding: 6px; z-index: 10; }
.menu a, .menu span { padding: 9px 12px; border-radius: 8px; text-decoration: none; color: var(--ink); font-size: 14px; }
.menu a:hover { background: var(--surface-2); }
.menu span { color: var(--muted); font-size: 12.5px; }
main { display: flex; flex-direction: column; gap: 12px;
  padding: 16px max(16px, 2vw, env(safe-area-inset-right, 0px)) calc(96px + env(safe-area-inset-bottom, 0px)) max(16px, 2vw, env(safe-area-inset-left, 0px)); }
.tabs { position: fixed; left: 0; right: 0; bottom: 0; z-index: 5; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px); }
.tabs-in { max-width: 720px; margin: 0 auto; display: flex; padding: 0 env(safe-area-inset-right, 0px) 0 env(safe-area-inset-left, 0px); }
.tabs a { flex: 1; min-width: 0; color: var(--muted); font: 500 11.5px var(--f-body); padding: 8px 2px 10px; text-decoration: none; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.tabs a svg { width: 22px; height: 22px; }
.tabs a.on { color: var(--accent); }

/* ---------- ชิ้นส่วน ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; min-width: 0; }
.hello { font-family: var(--f-display); font-size: 19px; font-weight: 600; }
.sub { color: var(--muted); font-size: 13px; }
.sec { font-size: 12px; font-weight: 600; letter-spacing: .04em; color: var(--muted); margin: 6px 2px -4px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr)); gap: 10px; }
.grid2 > * { min-width: 0; }
.action { border: 1px solid var(--line); background: var(--surface); border-radius: 14px; padding: 14px 12px; text-align: left; color: var(--ink); font: 600 14px var(--f-body); display: flex; flex-direction: column; gap: 6px; text-decoration: none; }
.action svg { width: 26px; height: 26px; color: var(--accent); }
.action.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.action.primary svg { color: var(--accent-ink); }
.action small { font-weight: 400; font-size: 12px; opacity: .8; }
.num { font-family: var(--f-mono); font-variant-numeric: tabular-nums; }
.big { font-family: var(--f-display); font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.row { display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.row > div { min-width: 0; }
.list { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.li { padding: 11px 14px; border-top: 1px solid var(--line); display: flex; gap: 10px; align-items: center; justify-content: space-between; color: var(--ink); text-decoration: none; background: none; border-left: 0; border-right: 0; border-bottom: 0; text-align: left; font: inherit; width: 100%; cursor: pointer; }
.li:first-child { border-top: 0; }
.li > div { min-width: 0; }
.li .t { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.code { font-family: var(--f-mono); font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.chip { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.info { background: var(--info-soft); color: var(--info); }
.chip.mute { background: var(--surface-2); color: var(--muted); }
.alert { border-radius: 12px; padding: 10px 12px; font-size: 13.5px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert button { margin-top: 6px; }
.search { display: flex; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; align-items: center; }
.search input { border: 0; background: none; color: var(--ink); font: 16px var(--f-body); padding: 11px 0; flex: 1; min-width: 0; outline: none; }
.search:focus-within { border-color: var(--accent); }
.filters { display: flex; flex-wrap: wrap; gap: 8px; }
.filters select { flex: 1 1 160px; min-width: 0; max-width: 100%; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; color: var(--muted); font: 600 13.5px var(--f-body); padding: 7px 8px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.stock { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 8px; text-align: center; }
.stock > div { background: var(--surface-2); border-radius: 8px; padding: 6px 2px; min-width: 0; }
.stock b { display: block; font-family: var(--f-mono); font-size: 14px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stock span { font-size: 11px; color: var(--muted); }
.stock .neg { background: var(--bad-soft); }
.stock .neg b { color: var(--bad); }
.stock .full { background: var(--warn-soft); }
.stock .full b { color: var(--warn); }
.stock.empty-stock b { color: var(--muted); font-weight: 400; }
.whs-tag { display: inline-block; margin-top: 6px; font-size: 11.5px; font-weight: 600; color: var(--info); background: var(--info-soft); border-radius: 6px; padding: 1px 8px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { font-size: 12.5px; color: var(--muted); font-weight: 500; }
input, select, textarea { font: 16px var(--f-body); color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; max-width: 100%; }
.field input, .field select, .field textarea { width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.btn { font: 600 15px var(--f-body); border-radius: 12px; padding: 12px 16px; border: 1px solid transparent; cursor: pointer; text-align: center; text-decoration: none; display: inline-block; }
.btn.main { background: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn.danger { background: var(--surface); color: var(--bad); border-color: var(--bad); }
.btn.sm { padding: 6px 12px; font-size: 13px; border-radius: 9px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: progress; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-row .btn { flex: 1 1 140px; }
.linkbtn { font: 600 13px var(--f-body); color: var(--accent); background: none; border: 0; padding: 0; cursor: pointer; text-decoration: none; }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(160px, 100%), 1fr)); gap: 8px 16px; }
.kv > div { min-width: 0; overflow-wrap: anywhere; }
.kv span { display: block; font-size: 12px; color: var(--muted); }
.tbl-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); max-width: 100%; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { text-align: left; padding: 9px 12px; border-top: 1px solid var(--line); vertical-align: top; }
th { border-top: 0; font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
td.r, th.r { text-align: right; }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom, 0px)); width: min(560px, calc(100% - 32px)); background: var(--ink); color: var(--bg); border-radius: 12px; padding: 11px 14px; font-size: 14px; box-shadow: 0 6px 20px rgba(0,0,0,.2); z-index: 20; }
.banner { background: var(--warn-soft); color: var(--warn); font-size: 13px; text-align: center; padding: 6px max(16px, env(safe-area-inset-right, 0px)) 6px max(16px, env(safe-area-inset-left, 0px)); }
.empty { text-align: center; color: var(--muted); padding: 22px 10px; font-size: 14px; }
.skel { height: 64px; border-radius: 14px; background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2)); background-size: 200% 100%; }
.datebar { display: flex; align-items: center; gap: 8px; }
.datebar input { flex: 1; min-width: 0; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* ---------- จัดหลายคอลัมน์ตามความกว้างจอ ---------- */
.cardgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(340px, 100%), 1fr)); gap: 12px; align-items: start; }
.cardgrid > * { min-width: 0; max-width: 100%; }
.dash { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(380px, 100%), 1fr)); gap: 16px; align-items: start; }
.dash > *, .col { min-width: 0; max-width: 100%; display: flex; flex-direction: column; gap: 12px; }
.split { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; }
.split > .side { flex: 1 1 360px; min-width: 0; max-width: 100%; display: flex; flex-direction: column; gap: 12px; }
.split > .mainc { flex: 2 1 560px; min-width: 0; max-width: 100%; display: flex; flex-direction: column; gap: 12px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.toolbar > .search { flex: 2 1 320px; min-width: 0; }
.toolbar > .filters { flex: 1 1 320px; min-width: 0; }
.toolbar > .seg { flex: 0 1 320px; min-width: 0; }
.toolbar > .datebar { flex: 1 1 320px; min-width: 0; }
.toolbar > select, .toolbar > .btn { flex: 0 1 220px; min-width: 0; }
.lcard { display: flex; gap: 10px; align-items: center; justify-content: space-between; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; color: var(--ink); text-decoration: none; }
.lcard > div { min-width: 0; }
.lcard:hover { border-color: var(--accent); }
.lcard .t { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.narrow { max-width: 640px; width: 100%; }

@media (min-width: 900px) {
  .topnav { display: flex; }
  .topbar .brand { display: inline; }
  .tabs { display: none; }
  main { padding-bottom: 40px; }
  .toast { bottom: 24px; }
}
@media (prefers-reduced-motion: no-preference) { .skel { animation: sh 1.2s linear infinite; } @keyframes sh { to { background-position: -200% 0; } } }
