/* ============================================================================
   THE RICH ONLINE — ระบบบริหารร้านฝากขาย
   สไตล์ทั้งระบบ ไม่พึ่งไลบรารีภายนอก โหลดไฟล์เดียวจบ

   โทนสี: หมึกเข้ม (ink) เป็นพื้นหลังเมนู · ทอง (gold) เป็นสีเน้น
   สีบอกสถานะ: good เขียว · warn ส้ม · bad แดง · info น้ำเงิน
   ============================================================================ */

:root {
  --ink-50:  #FBF9F6;
  --ink-100: #F3EEE6;
  --ink-200: #E7E0D6;
  --ink-300: #D6CCBE;
  --ink-400: #A99E90;
  --ink-500: #6E655D;
  --ink-600: #4A433C;
  --ink-700: #3F3934;
  --ink-800: #241D16;
  --ink-900: #14110F;

  --gold-50:  #FBF6EA;
  --gold-100: #F5EDDC;
  --gold-300: #E0C489;
  --gold-400: #C79A45;
  --gold-500: #A67C2E;
  --gold-600: #8A6524;
  --gold-700: #7A5A18;

  --good-50: #E4F3EC; --good-500: #157F5F; --good-700: #0E5C44;
  --warn-50: #FDF0DC; --warn-500: #B45309; --warn-700: #8A3F07;
  --bad-50:  #FBE7E5; --bad-500:  #B3261E; --bad-700:  #8C1D18;
  --info-50: #E6ECFD; --info-500: #1D4ED8; --info-700: #173FA6;

  --sidebar-w: 248px;
  --radius: 14px;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--ink-50);
  color: var(--ink-900);
  font-family: 'Noto Sans Thai', 'Sarabun', 'Leelawadee UI', ui-sans-serif, system-ui,
               -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--gold-100); }

a { color: inherit; }

/* ตัวเลขเงินให้เรียงตรงคอลัมน์เสมอ */
.tnum, td.num, th.num { font-variant-numeric: tabular-nums; }
td.num, th.num { text-align: end; }

input, select, textarea, button { font-family: inherit; font-size: inherit; }

h1, h2, h3 { margin: 0; line-height: 1.35; }

/* ─────────────────────────── โครงหน้า ─────────────────────────── */

.shell { min-height: 100dvh; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 16px;
  background: var(--ink-900); color: #fff;
}
.topbar-brand { font-weight: 700; font-size: 15px; }
.topbar-bell { margin-inline-start: auto; position: relative; text-decoration: none; font-size: 18px; }
.icon-btn { background: none; border: 0; color: #fff; font-size: 20px; cursor: pointer; padding: 4px; }
.dot-badge {
  position: absolute; top: -4px; inset-inline-end: -8px;
  background: var(--bad-500); color: #fff; border-radius: 999px;
  font-size: 10px; font-weight: 700; padding: 0 5px; line-height: 16px;
}

.sidebar {
  position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 50;
  width: 268px; max-width: 84vw;
  background: var(--ink-900);
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform .18s ease;
}
.sidebar.is-open { transform: none; }

.brand { padding: 20px 20px 16px; }
.brand-kicker {
  font-size: 10.5px; letter-spacing: .2em; text-transform: uppercase;
  color: rgba(224, 196, 137, .8); font-weight: 700;
}
.brand-title { color: #fff; font-weight: 800; font-size: 17px; margin-top: 2px; }

.nav { flex: 1; overflow-y: auto; padding: 0 12px; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  border-radius: 10px; padding: 10px 12px;
  font-size: 14px; color: var(--ink-300); text-decoration: none;
  background: none; border: 0; width: 100%; text-align: start; cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.nav-item.is-active { background: rgba(255, 255, 255, .12); color: var(--gold-300); font-weight: 600; }
.nav-item-sm { padding: 8px 12px; font-size: 13.5px; }
.nav-icon { width: 18px; text-align: center; flex: none; }
.nav-label { flex: 1; }
.nav-badge {
  background: var(--bad-500); color: #fff; border-radius: 999px;
  font-size: 11px; font-weight: 700; padding: 0 6px; min-width: 20px; text-align: center;
}

.sidebar-foot { padding: 12px; border-top: 1px solid rgba(255, 255, 255, .1); }
.who { padding: 0 8px 8px; }
.who-name { color: #fff; font-size: 13.5px; font-weight: 600; }
.who-role { color: var(--ink-400); font-size: 12px; }
.logout-form { margin: 0; }

.scrim {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(0, 0, 0, .5);
  display: none;
}
.scrim.is-open { display: block; }

/* min-width: 0 — main เป็น grid item ถ้าไม่ตั้ง ตารางกว้างจะดันทั้งหน้าจนล้นจอ แทนที่จะเลื่อนในกรอบ .table-wrap */
.main { padding: 16px; max-width: 1400px; min-width: 0; }

@media (min-width: 1024px) {
  .shell { display: grid; grid-template-columns: var(--sidebar-w) 1fr; }
  .topbar { display: none; }
  .sidebar { position: sticky; top: 0; height: 100dvh; width: var(--sidebar-w); max-width: none; transform: none; }
  .scrim { display: none !important; }
  .main { padding: 32px; }
}

.bare { background: var(--ink-50); }
.bare-main { max-width: 480px; margin: 0 auto; padding: 32px 16px; }
.bare-main.bare-wide { max-width: 1000px; }

/* ─────────────────────────── ชิ้นส่วนพื้นฐาน ─────────────────────────── */

.card {
  background: #fff;
  border: 1px solid var(--ink-200);
  border-radius: var(--radius);
}

.section { padding: 16px; margin-bottom: 16px; }
@media (min-width: 640px) { .section { padding: 20px; } }

.section-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 12px; flex-wrap: wrap;
}
.section-title { font-size: 15px; font-weight: 700; }

.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-start;
  justify-content: space-between; gap: 12px; margin-bottom: 20px;
}
.page-title { font-size: 22px; font-weight: 800; letter-spacing: -.01em; }
.page-sub { font-size: 13.5px; color: var(--ink-500); margin-top: 2px; }
.page-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.grid { display: grid; gap: 12px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.split { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 1024px) { .split { grid-template-columns: 2fr 1fr; } }

.stack > * + * { margin-top: 12px; }
.muted { color: var(--ink-500); }
.small { font-size: 13px; }
.xsmall { font-size: 12px; }
.bold { font-weight: 700; }
.right { text-align: end; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.mt-3 { margin-top: 12px; }
.mb-3 { margin-bottom: 12px; }

/* ─────────────────────────── ปุ่ม ─────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border-radius: 10px; padding: 8px 16px;
  font-weight: 600; font-size: 14px; white-space: nowrap;
  border: 1px solid transparent; cursor: pointer; text-decoration: none;
  transition: background .15s, opacity .15s;
}
.btn-primary { background: var(--ink-900); color: var(--gold-300); }
.btn-primary:hover { background: var(--ink-800); }
.btn-gold { background: var(--gold-500); color: #fff; }
.btn-gold:hover { background: var(--gold-600); }
.btn-ghost { background: #fff; color: var(--ink-700); border-color: var(--ink-200); }
.btn-ghost:hover { background: var(--ink-100); }
.btn-danger { background: var(--bad-500); color: #fff; }
.btn-danger:hover { background: var(--bad-700); }
.btn-sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ─────────────────────────── ฟอร์ม ─────────────────────────── */

.field {
  width: 100%;
  border: 1px solid var(--ink-200);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 14.5px;
  background: #fff;
  color: inherit;
}
.field:focus { outline: 2px solid var(--gold-300); outline-offset: -1px; border-color: transparent; }
textarea.field { min-height: 84px; resize: vertical; }

.label { display: block; font-size: 13px; font-weight: 600; color: var(--ink-600); margin-bottom: 4px; }
.form-row { margin-bottom: 12px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
.hint { font-size: 12px; color: var(--ink-500); margin-top: 4px; }

.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: 18px; height: 18px; }

/* ─────────────────────────── ตาราง ─────────────────────────── */

.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
@media (min-width: 640px) { .table-wrap { margin: 0; padding: 0; } }

table.tbl { width: 100%; min-width: 560px; border-collapse: collapse; }
table.tbl.narrow { min-width: 0; }
.tbl th {
  background: var(--ink-100); font-size: 12.5px; font-weight: 700;
  color: var(--ink-700); text-align: start; padding: 9px 12px; white-space: nowrap;
}
.tbl td { padding: 9px 12px; border-top: 1px solid var(--ink-100); font-size: 14px; vertical-align: top; }
.tbl tbody tr:hover { background: var(--ink-50); }
.tbl tfoot td { border-top: 2px solid var(--ink-200); font-weight: 700; }

.empty { text-align: center; color: var(--ink-400); font-size: 14px; padding: 36px 12px; }

/* ─────────────────────────── ชิปและแบนเนอร์ ─────────────────────────── */

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 9px; border-radius: 7px;
  font-size: 11.5px; font-weight: 700; white-space: nowrap;
}
.chip-gold { background: var(--gold-100); color: var(--gold-700); }
.chip-good { background: var(--good-50); color: var(--good-700); }
.chip-warn { background: var(--warn-50); color: var(--warn-700); }
.chip-bad  { background: var(--bad-50);  color: var(--bad-700); }
.chip-info { background: var(--info-50); color: var(--info-700); }
.chip-gray { background: var(--ink-100); color: var(--ink-600); }

.banner {
  border-radius: 12px; padding: 12px 16px; margin-bottom: 16px;
  font-size: 14px; font-weight: 500;
}
.banner-good { background: var(--good-50); color: var(--good-700); }
.banner-warn { background: var(--warn-50); color: var(--warn-700); }
.banner-bad  { background: var(--bad-50);  color: var(--bad-700); }
.banner-info { background: var(--info-50); color: var(--info-700); }

.note {
  border-inline-start: 3px solid var(--gold-400);
  background: var(--gold-50);
  padding: 10px 14px; border-radius: 0 10px 10px 0;
  font-size: 13.5px; color: var(--ink-700);
}

/* ─────────────────────────── การ์ดตัวเลข ─────────────────────────── */

.kpi { padding: 16px; height: 100%; }
.kpi-label { font-size: 12.5px; font-weight: 600; color: var(--ink-500); }
.kpi-value {
  font-size: 26px; font-weight: 800; letter-spacing: -.02em;
  line-height: 1.2; margin-top: 2px; font-variant-numeric: tabular-nums;
}
.kpi-value.kpi-bad  { color: var(--bad-500); }
.kpi-value.kpi-good { color: var(--good-500); }
.kpi-value.kpi-warn { color: var(--warn-500); }
.kpi-hint { font-size: 12.5px; color: var(--ink-500); margin-top: 2px; }
.kpi-link { display: block; text-decoration: none; }
.kpi-link:hover { opacity: .9; }

/* ─────────────────────────── กราฟ ─────────────────────────── */

.bar-chart { display: flex; align-items: flex-end; gap: 3px; }
.bar-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; position: relative; }
.bar {
  width: 100%; border-radius: 3px 3px 0 0;
  background: linear-gradient(to bottom, var(--gold-400), var(--gold-500));
}
.bar-tip {
  position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
  margin-bottom: 4px; display: none; z-index: 10;
  background: var(--ink-900); color: #fff; font-size: 11px;
  border-radius: 6px; padding: 3px 8px; white-space: nowrap;
}
.bar-col:hover .bar-tip { display: block; }

.share-bar { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--ink-100); }
.seg-0 { background: var(--gold-500); }
.seg-1 { background: var(--ink-700); }
.seg-2 { background: var(--good-500); }
.seg-3 { background: var(--info-500); }
.seg-4 { background: var(--warn-500); }
.legend { margin-top: 10px; display: flex; flex-direction: column; gap: 4px; }
.legend-row { display: flex; align-items: center; justify-content: space-between; font-size: 13px; gap: 12px; }
.legend-label { display: flex; align-items: center; gap: 8px; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }
.legend-value { font-weight: 600; font-variant-numeric: tabular-nums; }

.cycle { width: 100%; }
.cycle-track { height: 6px; border-radius: 999px; background: var(--ink-100); overflow: hidden; }
.cycle-fill { height: 100%; border-radius: 999px; }
.cycle-good { background: var(--good-500); }
.cycle-gold { background: var(--gold-400); }
.cycle-warn { background: var(--warn-500); }
.cycle-bad  { background: var(--bad-500); }
.cycle-text { font-size: 11.5px; color: var(--ink-500); margin-top: 4px; font-variant-numeric: tabular-nums; }

/* ─────────────────────────── กล่องพับได้ ─────────────────────────── */

details.box { border: 1px solid var(--ink-200); border-radius: 12px; background: #fff; margin-bottom: 12px; }
details.box > summary {
  padding: 12px 16px; cursor: pointer; font-weight: 600; font-size: 14.5px;
  list-style: none; display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
details.box > summary::-webkit-details-marker { display: none; }
details.box > summary::after { content: '▾'; color: var(--ink-400); }
details.box[open] > summary::after { content: '▴'; }
details.box > .box-body { padding: 0 16px 16px; }

/* ─────────────────────────── แผ่นลายเซ็น ─────────────────────────── */

.sigpad-wrap { border: 1px dashed var(--ink-300); border-radius: 12px; background: #fff; padding: 8px; }
canvas.sigpad { width: 100%; height: 180px; touch-action: none; display: block; border-radius: 8px; }

.thumb { width: 84px; height: 84px; object-fit: cover; border-radius: 10px; border: 1px solid var(--ink-200); }
.thumb-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* ─────────────────── เอกสารสัญญาแบบพิมพ์ได้ (A4) ─────────────────── */

.contract { width: 210mm; margin: 0 auto; }
.contract .page {
  width: 210mm; min-height: 297mm;
  padding: 12mm 12mm 14mm;
  background: #fff; position: relative; box-sizing: border-box;
  font-size: 12px; line-height: 1.6;
}
.contract table { width: 100%; border-collapse: collapse; }
.contract .cell { border: 1px solid #1F3864; padding: 3px 5px; vertical-align: top; }
.contract .cell.head { background: #EDF1F8; font-weight: 700; text-align: start; }
.contract .cell.blue { background: #1F3864; color: #fff; font-weight: 700; text-align: center; }
.contract .sec {
  font-size: 12px; font-weight: 800; color: #1F3864;
  border-inline-start: 4px solid #1F3864; padding-inline-start: 6px; margin: 10px 0 4px;
}
.contract .doc-title { text-align: center; font-size: 16px; font-weight: 800; color: #1F3864; }
.contract .footer-note {
  position: absolute; bottom: 6mm; inset-inline: 12mm;
  text-align: center; font-size: 9px; color: var(--ink-500);
}
.contract .terms p { text-align: justify; margin: 2px 0 8px; }
.contract .sign-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-top: 16px; }
.contract .sign-box { border: 1px solid var(--ink-300); border-radius: 6px; padding: 8px; text-align: center; }
.contract .sign-box img { max-height: 60px; max-width: 100%; }
.contract .sign-line { border-top: 1px solid var(--ink-400); margin-top: 40px; padding-top: 4px; font-size: 11px; }
.contract .hash { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 9px; word-break: break-all; }

@media print {
  @page { size: A4; margin: 0; }
  body { background: #fff !important; }
  .no-print, .topbar, .sidebar, .scrim, .banner { display: none !important; }
  .main, .bare-main { padding: 0 !important; max-width: none !important; }
  .shell { display: block !important; }
  .contract { width: auto; }
  .contract .page { page-break-after: always; box-shadow: none !important; margin: 0 !important; }
  .contract .page:last-child { page-break-after: auto; }
}

@media screen {
  .contract .page { box-shadow: 0 2px 18px rgba(20, 17, 15, .10); margin: 0 auto 18px; border-radius: 4px; }
}

@media screen and (max-width: 900px) {
  .contract, .contract .page { width: 100%; min-height: 0; padding: 14px; }
  /* บนมือถือหน้ากระดาษไม่สูงเต็ม A4 เลขหน้าจึงต้องไหลตามเนื้อหา ไม่งั้นจะทับข้อความ */
  .contract .footer-note { position: static; margin-top: 14px; }
  .contract .sign-grid { grid-template-columns: 1fr; }
}
