/* SalesCloud — หน้าจอหลัก */
@font-face { font-family: "DocThai"; src: url("../fonts/Sarabun-Regular.ttf") format("truetype"), url("../fonts/Loma.otf") format("opentype"); font-weight: 400; }
@font-face { font-family: "DocThai"; src: url("../fonts/Sarabun-Bold.ttf") format("truetype"), url("../fonts/Loma-Bold.otf") format("opentype"); font-weight: 700; }

:root {
  --brand: #1f6fb2;
  --brand-dark: #185a91;
  --side: #2c3138;
  --side-2: #23272d;
  --side-text: #b8c0c9;
  --bg: #eef1f4;
  --panel: #ffffff;
  --line: #dde2e7;
  --text: #2d3339;
  --muted: #7b8691;
  --green: #5fae4f;
  --green-head: #9ccc65;
  --red: #e0524a;
  --orange: #f0a832;
  --sky: #2aa4d9;
  --violet: #8e6bbf;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: "Sarabun", "DocThai", "Tahoma", sans-serif; font-size: 14px; color: var(--text); background: var(--bg); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* ---------------- โครงหน้า */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.app.collapsed { grid-template-columns: 56px 1fr; }
.side { background: var(--side); color: var(--side-text); position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.side .brand { height: 50px; display: flex; align-items: center; gap: 8px; padding: 0 16px; background: var(--brand); color: #fff; font-weight: 700; font-size: 18px; letter-spacing: .5px; }
.side .brand svg { flex: none; }
.side .who { display: flex; align-items: center; gap: 10px; padding: 14px 16px; background: var(--side-2); }
.side .avatar { width: 40px; height: 40px; border-radius: 50%; background: #fff; color: var(--brand); display: grid; place-items: center; font-weight: 700; flex: none; overflow: hidden; }
.side .avatar img { width: 100%; height: 100%; object-fit: contain; }
.side .who b { color: #fff; display: block; font-size: 13px; }
.side .who a { color: #9fc8ee; font-size: 12px; }
.nav { list-style: none; margin: 0; padding: 6px 0 30px; }
.nav > li > a, .nav > li > span { display: flex; align-items: center; gap: 10px; padding: 10px 16px; color: var(--side-text); cursor: pointer; user-select: none; font-size: 13.5px; }
.nav > li > a:hover, .nav > li > span:hover { background: #363c44; color: #fff; text-decoration: none; }
.nav .ico { width: 18px; height: 18px; flex: none; opacity: .85; }
.nav .caret { margin-left: auto; transition: transform .15s; font-size: 10px; }
.nav li.open > span .caret { transform: rotate(90deg); }
.nav li.active > a, .nav li.active > span { background: var(--brand); color: #fff; }
.nav ul { list-style: none; margin: 0; padding: 0; background: var(--side-2); display: none; }
.nav li.open > ul { display: block; }
.nav ul a { display: block; padding: 7px 16px 7px 44px; color: var(--side-text); font-size: 13px; position: relative; }
.nav ul a::before { content: ""; position: absolute; left: 25px; top: 50%; width: 6px; height: 6px; border-radius: 50%; border: 1px solid #6c757d; transform: translateY(-50%); }
.nav ul a:hover { color: #fff; text-decoration: none; }
.nav ul a.current { color: #fff; background: #15181c; font-weight: 600; }
.nav ul a.current::before { background: var(--brand); border-color: var(--brand); }
.app.collapsed .side .label, .app.collapsed .side .caret, .app.collapsed .side .who div, .app.collapsed .nav ul { display: none !important; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { height: 50px; background: #fff; border-bottom: 1px solid var(--line); display: flex; align-items: center; padding: 0 8px 0 0; position: sticky; top: 0; z-index: 20; }
.topbar .burger { width: 50px; height: 50px; border: 0; background: none; cursor: pointer; color: var(--brand); display: grid; place-items: center; border-right: 1px solid var(--line); }
.topbar .spacer { flex: 1; }
.topbar .tb-item { padding: 0 14px; height: 50px; display: flex; align-items: center; gap: 6px; color: var(--muted); border-left: 1px solid var(--line); font-size: 13px; }
.topbar .tb-item:hover { color: var(--brand); text-decoration: none; background: #f7f9fb; }
.pagehead { display: flex; align-items: center; gap: 10px; padding: 12px 18px; background: #f8f9fa; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.pagehead h1 { font-size: 19px; }
.pagehead .crumb { color: var(--muted); font-size: 12px; }
.pagehead .tools { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.content { padding: 16px 18px 40px; }

/* ---------------- กล่อง */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 3px; margin-bottom: 16px; }
.panel > .ph { padding: 10px 14px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; font-weight: 600; }
.panel > .pb { padding: 14px; }
.grid { display: grid; gap: 12px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 900px) { .g2, .g3, .g4 { grid-template-columns: 1fr; } .app { grid-template-columns: 1fr; } .side { display: none; } .app.collapsed .side, .app.mobile-open .side { display: block; position: fixed; z-index: 50; width: 240px; } }

/* ---------------- การ์ดสถิติ */
.stat { display: flex; align-items: center; gap: 16px; padding: 22px 20px; color: #fff; border-radius: 3px; }
.stat .circle { width: 64px; height: 64px; border-radius: 50%; background: rgba(255,255,255,.85); display: grid; place-items: center; flex: none; }
.stat b { font-size: 24px; display: block; line-height: 1.2; }
.stat span { font-size: 13px; opacity: .95; }
.bg-sky { background: var(--sky); } .bg-red { background: var(--red); } .bg-green { background: #8bc34a; } .bg-orange { background: var(--orange); } .bg-violet { background: var(--violet); }

/* ---------------- ปุ่ม */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid transparent; border-radius: 3px; padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer; background: #fff; color: var(--text); border-color: #cfd6dd; line-height: 1.4; white-space: nowrap; }
.btn:hover { text-decoration: none; filter: brightness(.96); }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-green { background: var(--green); border-color: var(--green); color: #fff; }
.btn-red { background: var(--red); border-color: var(--red); color: #fff; }
.btn-orange { background: var(--orange); border-color: var(--orange); color: #fff; }
.btn-violet { background: var(--violet); border-color: var(--violet); color: #fff; }
.btn-sky { background: var(--sky); border-color: var(--sky); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--brand); }
.btn-sm { padding: 3px 8px; font-size: 12px; }
.btn-lg { padding: 9px 18px; font-size: 15px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.dropdown { position: relative; display: inline-block; }
.dropdown .menu { display: none; position: absolute; right: 0; top: 100%; z-index: 30; background: #fff; border: 1px solid var(--line); border-radius: 3px; box-shadow: 0 6px 18px rgba(0,0,0,.12); min-width: 230px; padding: 4px 0; }
.dropdown.open .menu { display: block; }
.dropdown .menu a, .dropdown .menu button { display: flex; gap: 8px; align-items: center; width: 100%; padding: 7px 14px; color: var(--text); background: none; border: 0; font: inherit; font-size: 13px; text-align: left; cursor: pointer; }
.dropdown .menu a:hover, .dropdown .menu button:hover { background: #f1f5f9; text-decoration: none; }
.dropdown .menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.dropdown .menu .danger { color: var(--red); }

/* ---------------- ฟอร์ม */
label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 3px; }
input[type=text], input[type=number], input[type=password], input[type=email], input[type=date], select, textarea {
  width: 100%; font: inherit; font-size: 13.5px; padding: 6px 8px; border: 1px solid #ccd3da; border-radius: 2px; background: #fff; color: var(--text); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 2px rgba(31,111,178,.15); }
input[readonly] { background: #f3f5f7; }
textarea { resize: vertical; min-height: 34px; }
.field { margin-bottom: 10px; }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--text); font-size: 13px; cursor: pointer; }
.inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ฟอร์มเอกสาร: ตารางกรอบแบบใบเอกสาร */
.docbox { display: grid; grid-template-columns: 1.1fr 1.1fr .9fr .9fr; border: 1px solid #c9d0d6; }
.docbox > div { border-right: 1px solid #c9d0d6; border-bottom: 1px solid #c9d0d6; padding: 5px 8px 7px; min-width: 0; position: relative; }
.docbox label { font-size: 11px; margin-bottom: 2px; }
.docbox input, .docbox select, .docbox textarea { border-color: transparent; padding: 3px 4px; background: transparent; }
.docbox input:hover, .docbox textarea:hover, .docbox select:hover { border-color: #d6dde3; }
.docbox input:focus, .docbox textarea:focus, .docbox select:focus { background: #fff; }
.docbox .span2 { grid-column: span 2; }
.docbox .span3 { grid-column: span 3; }
.docbox .hl { background: #fbfcfd; }
.docbox .date-box input { border: 1px solid #c9d0d6; text-align: center; }
.docno { display: flex; gap: 4px; }
.docno input { font-weight: 700; }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 12px; flex-wrap: wrap; }
.tabs button { border: 1px solid transparent; border-bottom: 0; background: none; padding: 8px 14px; font: inherit; font-size: 13px; cursor: pointer; color: var(--muted); border-radius: 3px 3px 0 0; }
.tabs button.on { border-color: var(--line); background: #fff; color: var(--text); font-weight: 600; margin-bottom: -1px; }
.tabpane { display: none; } .tabpane.on { display: block; }

.assign { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.assign .chip { display: flex; align-items: stretch; border: 1px solid #ccd3da; border-radius: 2px; overflow: hidden; }
.assign .chip span { padding: 5px 9px; color: #fff; display: grid; place-items: center; }
.assign .chip input { border: 0; width: 150px; }

/* ตารางรายการสินค้า */
table { border-collapse: collapse; width: 100%; }
.items { border: 1px solid #c9d0d6; }
.items th { background: var(--green-head); color: #fff; font-weight: 600; padding: 7px 6px; font-size: 13px; border-right: 1px solid rgba(255,255,255,.4); }
.items td { border-top: 1px solid #e3e7eb; padding: 4px; vertical-align: top; }
.items td input, .items td textarea, .items td select { border-color: transparent; padding: 4px 5px; }
.items td input:hover, .items td textarea:hover { border-color: #d6dde3; }
.items tr:nth-child(even) td { background: #fafbfc; }
.items .drag { cursor: grab; color: #9aa4ad; text-align: center; padding-top: 9px; }
.items .del { background: var(--red); color: #fff; border: 0; width: 26px; height: 30px; cursor: pointer; border-radius: 2px; }
.items .num input { text-align: right; }
.items .amt { text-align: right; padding: 8px 8px; font-weight: 600; background: #f1f3f5 !important; white-space: nowrap; }
.items .codecol { width: 120px; }
.ac-wrap { position: relative; }
.ac-list { position: absolute; z-index: 40; left: 0; top: 100%; min-width: 320px; max-height: 280px; overflow: auto; background: #fff; border: 1px solid var(--line); box-shadow: 0 6px 16px rgba(0,0,0,.12); }
.ac-list div { padding: 6px 10px; cursor: pointer; font-size: 13px; border-bottom: 1px solid #f0f2f4; }
.ac-list div:hover, .ac-list div.on { background: #eaf3fb; }
.ac-list small { color: var(--muted); display: block; }

.totals { width: 100%; }
.totals td { padding: 6px 10px; border-top: 1px solid #e3e7eb; }
.totals td:first-child { text-align: right; color: var(--muted); white-space: nowrap; }
.totals td:first-child input, .totals td:first-child select { margin-left: 6px; display: inline-block; vertical-align: middle; }
.totals td.v { text-align: right; font-weight: 600; width: 160px; background: #f1f3f5; }
.totals tr.grand td { font-size: 16px; color: var(--text); }
.totals tr.grand td.v { background: #e2e8ee; }
.totals input { text-align: right; width: 110px; }
.bottom { display: grid; grid-template-columns: 1fr 380px; gap: 14px; margin-top: 10px; }
@media (max-width: 900px) { .bottom { grid-template-columns: 1fr; } .docbox { grid-template-columns: 1fr 1fr; } }
.savebar { display: flex; justify-content: center; gap: 10px; padding: 14px; border-top: 1px solid var(--line); background: #f8f9fa; position: sticky; bottom: 0; z-index: 10; }

/* ---------------- ตารางรายการ */
.list { background: #fff; }
.list th { background: var(--green-head); color: #fff; text-align: left; padding: 8px 8px; font-weight: 600; font-size: 13px; white-space: nowrap; }
.list td { padding: 8px; border-top: 1px solid #e8ecef; vertical-align: top; }
.list tr:hover td { background: #f7fbff; }
.list td.dno a { font-weight: 700; }
.list .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.list .sub a { margin-right: 10px; }
.list tfoot td { background: #f4f6f8; font-weight: 700; }
.filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.filters input[type=text] { max-width: 320px; }
.filters .date { width: 130px; }
.pager { display: flex; gap: 4px; padding: 10px 14px; justify-content: flex-end; }
.pager a, .pager span { padding: 3px 9px; border: 1px solid var(--line); border-radius: 2px; font-size: 12px; }
.pager span { background: var(--brand); color: #fff; border-color: var(--brand); }

/* ---------------- สถานะ */
.badge { display: inline-block; padding: 2px 8px; border-radius: 2px; font-size: 11.5px; color: #fff; white-space: nowrap; }
.st-new { background: #5b9bd5; } .st-sent { background: #8e6bbf; } .st-ok { background: #5fae4f; } .st-partial { background: #f0a832; }
.st-overdue { background: #e0524a; } .st-cancel { background: #9aa4ad; }
.tag { display: inline-block; padding: 1px 6px; font-size: 11px; border: 1px solid var(--line); border-radius: 2px; color: var(--muted); }
.tag.cash { border-color: #5fae4f; color: #3d8a2f; } .tag.credit { border-color: #2aa4d9; color: #1b7fae; }
.cancelled-banner { background: #fdecea; color: #b3261e; border: 1px solid #f5c2bd; padding: 8px 14px; margin-bottom: 12px; border-radius: 3px; }

/* ---------------- แจ้งเตือน */
.flash { position: fixed; bottom: 80px; right: 18px; z-index: 100; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.flash div { padding: 10px 16px; border-radius: 3px; color: #fff; box-shadow: 0 4px 12px rgba(0,0,0,.15); max-width: 420px; animation: fadeout 6s forwards; }
.flash .ok { background: #4c9a3f; } .flash .error { background: #d0443c; }
@keyframes fadeout { 0%, 85% { opacity: 1; } 100% { opacity: 0; visibility: hidden; } }

/* ---------------- กล่องโต้ตอบ */
.modal { position: fixed; inset: 0; background: rgba(20,24,28,.45); display: none; align-items: flex-start; justify-content: center; z-index: 200; padding-top: 8vh; }
.modal.open { display: flex; }
.modal .box { background: #fff; width: min(560px, 94vw); border-radius: 4px; box-shadow: 0 12px 40px rgba(0,0,0,.25); }
.modal .mh { padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 700; display: flex; }
.modal .mh button { margin-left: auto; border: 0; background: none; font-size: 18px; cursor: pointer; color: var(--muted); }
.modal .mb { padding: 16px; }
.modal .mf { padding: 12px 16px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.print-opt { display: grid; grid-template-columns: 1fr 90px 90px; gap: 8px 12px; align-items: center; }
.print-opt .h { font-size: 12px; color: var(--muted); }
.print-opt label.radio { color: var(--text); font-size: 13.5px; display: flex; align-items: center; gap: 6px; margin: 0; cursor: pointer; }

/* ---------------- หน้าเข้าสู่ระบบ */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(135deg, #1f6fb2 0%, #134a78 100%); padding: 20px; }
.login { background: #fff; width: min(400px, 100%); border-radius: 6px; box-shadow: 0 20px 50px rgba(0,0,0,.25); overflow: hidden; }
.login .lh { padding: 26px 28px 8px; text-align: center; }
.login .lh h1 { font-size: 22px; color: var(--brand); }
.login .lb { padding: 18px 28px 28px; }
.login .lf { text-align: center; font-size: 12px; color: var(--muted); padding: 10px; background: #f6f8fa; }

/* ---------------- แผงควบคุม */
.bars { display: flex; align-items: flex-end; gap: 8px; height: 190px; padding: 10px 4px 0; border-bottom: 1px solid var(--line); }
.bars .bar { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.bars .bar i { display: block; width: 70%; background: var(--sky); border-radius: 2px 2px 0 0; min-height: 2px; }
.bars .bar:last-child i { background: var(--brand); }
.bars .bar small { font-size: 10.5px; color: var(--muted); margin-top: 4px; white-space: nowrap; }
.bars .bar em { font-style: normal; font-size: 10px; color: var(--muted); margin-bottom: 2px; }
.kv { width: 100%; }
.kv td { padding: 8px 10px; border-top: 1px solid #edf0f2; }
.kv tr.sec td { background: #d9edf7; text-align: center; font-weight: 700; }
.kv td.v { text-align: right; font-weight: 600; }

/* ---------------- พิมพ์รายงานจากหน้าจอ */
@media print {
  .side, .topbar, .filters, .pagehead .tools, .savebar, .pager, .flash, .noprint { display: none !important; }
  .app { display: block; }
  body { background: #fff; }
  .content { padding: 0; }
  .panel { border: 0; }
  .list th { background: #ddd !important; color: #000 !important; }
  a { color: #000; text-decoration: none; }
}
/* เมนูย่อยระดับ 3 (เหมือน TRCloud) */
.nav ul li.sg > span { display: flex; align-items: center; padding: 7px 16px 7px 44px; color: var(--side-text); font-size: 13px; cursor: pointer; position: relative; user-select: none; }
.nav ul li.sg > span::before { content: ""; position: absolute; left: 25px; top: 50%; width: 6px; height: 6px; border-radius: 50%; border: 1px solid #6c757d; transform: translateY(-50%); }
.nav ul li.sg > span::after { content: "▶"; margin-left: auto; font-size: 9px; transition: transform .15s; }
.nav ul li.sg.open > span::after { transform: rotate(90deg); }
.nav ul li.sg > span:hover { color: #fff; }
.nav ul ul { background: #1f2327; }
.nav ul ul a { padding-left: 60px; font-size: 12.5px; }
.nav ul ul a::before { left: 44px; width: 4px; height: 4px; }
