:root {
  /* สีแบรนด์จากโลโก้ร้าน — ส้มอิฐ ตัวอักษรขาว */
  --brand: #EE4D26;          /* ดูดมาจากพื้นหลังไฟล์โลโก้ร้านตรง ๆ */
  --brand-ink: #ffffff;
  --brand-deep: #BE3915;

  --bg: #FBF6F2;
  --panel: #ffffff;
  --panel-2: #F4EBE5;
  --line: #E7D9D0;
  --ink: #241A15;
  --ink-2: #6E5D54;
  --accent: var(--brand);

  /* สีสถานะ — ตั้งใจให้ "ต้องซื้อ" เป็นแดงเข้มอมชมพู จะได้ไม่จมไปกับส้มของแบรนด์ */
  --red: #B3123A;
  --red-bg: #FCE7EC;
  --amber: #96650B;
  --amber-bg: #FBEFD8;
  --green: #1B7A4B;
  --green-bg: #E1F2E8;
  --grey-bg: #EFE7E1;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(36,26,21,.06), 0 8px 24px rgba(36,26,21,.07);
}
@media (prefers-color-scheme: dark) {
  :root {
    /* หัวเว็บใช้ส้มเดียวกับโหมดสว่าง — โลโก้เป็นตัวอักษรขาว ต้องอยู่บนส้มเข้มเสมอ */
    --bg: #17110E; --panel: #211814; --panel-2: #2C211B; --line: #3B2C24;
    --ink: #F5EDE7; --ink-2: #AC9A8E; --accent: var(--brand);

    --red: #FF7D9B; --red-bg: #3D1622; --amber: #F3C275; --amber-bg: #38290F;
    --green: #6FD5A2; --green-bg: #143024; --grey-bg: #2C211B;
    --shadow: 0 1px 2px rgba(0,0,0,.45), 0 8px 24px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 -apple-system, "SF Pro Text", "Helvetica Neue", "Noto Sans Thai", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select { font: inherit; color: inherit; }

/* ---------- header ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  background: var(--brand); color: var(--brand-ink);
  padding: max(10px, env(safe-area-inset-top)) 14px 0;
}
.top-inner { max-width: 820px; margin: 0 auto; }
.top-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.logo { display: flex; align-items: center; gap: 9px; min-width: 0; }
.logo img { height: 36px; width: auto; display: block; flex: none; }
.logo.has-img .brand-sub { display: none; }   /* โลโก้บอกชื่อร้านอยู่แล้ว ไม่ต้องเขียนซ้ำ */
.logo.has-img { gap: 11px; }
.logo .mark { height: 30px; width: 30px; flex: none; }
.brand { font-size: 17px; font-weight: 800; letter-spacing: -.01em; line-height: 1.2; white-space: nowrap; }
.brand-sub { font-size: 11.5px; opacity: .84; white-space: nowrap; }
.datebox {
  position: relative; flex: none; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  background: rgba(255,255,255,.18); border: 1px solid rgba(255,255,255,.32); border-radius: 999px;
  padding: 7px 13px; font-size: 13px; font-weight: 650; color: var(--brand-ink);
}
.datebox input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; }

.tabs { display: flex; gap: 2px; margin-top: 10px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  flex: 1 0 auto; background: none; border: 0; border-bottom: 2.5px solid transparent;
  padding: 10px 12px; font-size: 14.5px; font-weight: 600; color: rgba(255,255,255,.72);
  cursor: pointer; white-space: nowrap;
}
.tabs button.on { color: var(--brand-ink); border-bottom-color: var(--brand-ink); }
.tabs em {
  font-style: normal; background: var(--brand-ink); color: var(--brand); border-radius: 999px;
  padding: 1px 7px; font-size: 12px; margin-left: 4px; vertical-align: 1px; font-weight: 800;
}

main { padding: 16px 14px 80px; max-width: 820px; margin: 0 auto; }

/* ---------- แถบสรุปบนสุด ---------- */
.alert {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px;
  border-radius: var(--radius); margin-bottom: 14px; box-shadow: var(--shadow);
  border: 1px solid transparent; cursor: pointer; width: 100%; text-align: left;
}
.alert.warn { background: var(--red-bg); border-color: color-mix(in srgb, var(--red) 30%, transparent); }
.alert.good { background: var(--green-bg); border-color: color-mix(in srgb, var(--green) 25%, transparent); }
.alert.idle { background: var(--panel); border-color: var(--line); }
.alert .big { font-size: 30px; font-weight: 800; line-height: 1; }
.alert.warn .big { color: var(--red); }
.alert.good .big { color: var(--green); }
.alert .txt strong { display: block; font-size: 15.5px; }
.alert .txt span { font-size: 13px; color: var(--ink-2); }
.alert .go { margin-left: auto; font-size: 20px; color: var(--ink-2); }

.stats { display: flex; gap: 8px; margin-bottom: 16px; }
.stat {
  flex: 1; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 9px 10px; text-align: center;
}
.stat b { display: block; font-size: 18px; }
.stat span { font-size: 11.5px; color: var(--ink-2); }

/* ---------- กลุ่ม / การ์ดรายการ ---------- */
.group { margin-bottom: 18px; }
.group > h3 {
  position: sticky; top: 108px; z-index: 5; margin: 0 0 8px;
  font-size: 13px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--ink-2); background: var(--bg); padding: 6px 2px;
}
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 8px; box-shadow: var(--shadow);
  border-left: 4px solid var(--line);
}
.card.low { border-left-color: var(--red); }
.card.edge { border-left-color: var(--amber); }
.card.ok { border-left-color: var(--green); }
.card.unknown { border-left-color: var(--line); }

.card-head { display: flex; align-items: flex-start; gap: 10px; }
.card-name { font-weight: 650; font-size: 15.5px; flex: 1; }
.card-meta { font-size: 12.5px; color: var(--ink-2); margin-top: 2px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip {
  background: var(--panel-2); border-radius: 999px; padding: 2px 9px; font-size: 11.5px; font-weight: 600;
}
.pill { border-radius: 999px; padding: 3px 11px; font-size: 12.5px; font-weight: 700; white-space: nowrap; }
.pill.low { background: var(--red-bg); color: var(--red); }
.pill.edge { background: var(--amber-bg); color: var(--amber); }
.pill.ok { background: var(--green-bg); color: var(--green); }
.pill.unknown, .pill.nomin { background: var(--grey-bg); color: var(--ink-2); }

.entry { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.entry .lbl { font-size: 12.5px; color: var(--ink-2); }
.qty {
  width: 108px; padding: 10px 12px; font-size: 17px; font-weight: 650; text-align: center;
  background: var(--panel-2); border: 1.5px solid var(--line); border-radius: 11px;
}
.qty:focus { outline: none; border-color: var(--accent); background: var(--panel); }
.frac { display: flex; gap: 5px; flex-wrap: wrap; }
.frac button {
  min-width: 46px; padding: 9px 8px; border-radius: 11px; font-size: 14px; font-weight: 650;
  background: var(--panel-2); border: 1.5px solid var(--line); cursor: pointer;
}
.frac button.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }

/* ---------- หน้าต้องซื้อ ---------- */
.shop-group { margin-bottom: 20px; }
.shop-head { display: flex; align-items: baseline; gap: 8px; margin: 0 0 8px; font-size: 16px; font-weight: 700; }
.shop-head small { font-weight: 500; color: var(--ink-2); font-size: 12.5px; }
.buy-row { display: flex; align-items: flex-start; gap: 12px; padding: 12px 14px; }
.buy-row.done { opacity: .45; }
.buy-row.done .card-name { text-decoration: line-through; }
.check {
  flex: none; width: 26px; height: 26px; border-radius: 8px; border: 2px solid var(--line);
  background: var(--panel-2); cursor: pointer; font-size: 15px; line-height: 1; padding: 0;
}
.check[aria-pressed="true"] { background: var(--green); border-color: var(--green); color: #fff; }

.actions { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.btn {
  border-radius: 11px; padding: 10px 16px; font-size: 14.5px; font-weight: 650;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 40%, transparent); }
.btn.sm { padding: 6px 11px; font-size: 13px; border-radius: 9px; }

.empty { text-align: center; padding: 48px 20px; color: var(--ink-2); }
.empty .ico { font-size: 44px; }
.empty h3 { margin: 12px 0 4px; color: var(--ink); font-size: 17px; }

table.hist { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
table.hist th, table.hist td { padding: 11px 13px; text-align: left; border-bottom: 1px solid var(--line); font-size: 14px; }
table.hist th { font-size: 12px; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; }
table.hist tr:last-child td { border-bottom: 0; }
table.hist td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- dialog ---------- */
dialog {
  border: 0; border-radius: 18px; padding: 20px; width: min(460px, calc(100vw - 28px));
  background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
dialog label { display: block; margin-bottom: 11px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
dialog label small { font-weight: 500; opacity: .8; }
dialog input {
  display: block; width: 100%; margin-top: 5px; padding: 10px 12px; font-size: 16px; font-weight: 500;
  background: var(--panel-2); border: 1.5px solid var(--line); border-radius: 10px; color: var(--ink);
}
dialog input:focus { outline: none; border-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 16px 0 0; }
.dialog-err { color: var(--red); font-size: 13px; margin: 0 0 8px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(20px + env(safe-area-inset-bottom));
  background: var(--ink); color: var(--bg); padding: 11px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 600; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,.25);
}

@media print {
  .top, .actions, .check, .toast { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; break-inside: avoid; }
}

#copyText {
  width: 100%; padding: 12px; font-size: 13.5px; line-height: 1.6;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  background: var(--panel-2); border: 1.5px solid var(--line); border-radius: 10px;
  color: var(--ink); resize: vertical; -webkit-user-select: all; user-select: all;
}

/* ปุ่มปิดมุมบนขวาของกล่อง — iPad/มือถือไม่มีปุ่ม Esc ต้องมีทางออกที่เห็นชัดเสมอ */
.dlg-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.dlg-head h2 { margin: 0; flex: 1; }
.dlg-close {
  flex: none; width: 38px; height: 38px; border-radius: 50%; font-size: 17px; line-height: 1;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--ink-2); cursor: pointer;
}
.dlg-close:hover, .dlg-close:active { background: var(--line); color: var(--ink); }
dialog { max-height: calc(100dvh - 40px); overflow-y: auto; }

/* แถบเลือกใบ (บาร์ / เบเกอรี่) */
.sections { display: flex; gap: 7px; margin-bottom: 14px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.sections::-webkit-scrollbar { display: none; }
.sec-chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 15px; border-radius: 999px; font-size: 14px; font-weight: 650; white-space: nowrap;
  background: var(--panel); border: 1.5px solid var(--line); color: var(--ink-2); cursor: pointer;
}
.sec-chip.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.sec-chip em {
  font-style: normal; background: var(--red); color: #fff; border-radius: 999px;
  padding: 1px 7px; font-size: 11.5px; font-weight: 700;
}
.sec-chip.on em { background: rgba(0, 0, 0, .3); color: #fff; }

.frac-clear {
  min-width: auto !important; padding: 9px 12px !important;
  background: transparent !important; color: var(--ink-2) !important;
  border-style: dashed !important; font-weight: 500 !important;
}

/* โลโก้: ใช้ไฟล์ public/logo.png ถ้ามี ไม่งั้นใช้ดาว 8 แฉกที่วาดด้วย SVG */
.logo .mark { color: var(--brand-ink); }
.logo.has-img .mark { display: none; }

/* ---------- หน้าเข้าสู่ระบบ ---------- */
.login-page { background: var(--brand); }
.login-wrap { display: grid; place-items: center; min-height: 100dvh; padding: 24px 16px; max-width: none; }
.login-card {
  width: min(380px, 100%); background: var(--panel); color: var(--ink);
  border-radius: 20px; padding: 30px 26px 26px; box-shadow: 0 18px 50px rgba(0,0,0,.22); text-align: center;
}
.login-mark { color: var(--brand); width: 46px; height: 46px; margin: 0 auto 16px; }
.login-mark svg { width: 100%; height: 100%; display: block; }
/* โลโก้เป็นตัวอักษรขาว วางบนการ์ดขาวจะหายไป — ใช้ไฟล์เป็นแม่พิมพ์ (mask) แล้วเติมสีแบรนด์แทน */
.login-mark.has-img {
  width: min(200px, 66%); height: auto; aspect-ratio: 488 / 256;
  background: var(--brand);
  -webkit-mask: url(/logo.png) center / contain no-repeat;
          mask: url(/logo.png) center / contain no-repeat;
}
.login-card h1 { margin: 0; font-size: 21px; font-weight: 800; }
.login-sub { margin: 3px 0 22px; font-size: 13px; color: var(--ink-2); }
.login-card label { display: block; text-align: left; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.login-card input {
  display: block; width: 100%; margin-top: 6px; padding: 12px 14px; font-size: 16px;
  background: var(--panel-2); border: 1.5px solid var(--line); border-radius: 11px; color: var(--ink);
}
.login-card input:focus { outline: none; border-color: var(--brand); }
.login-card .btn { width: 100%; margin-top: 16px; padding: 13px; font-size: 15.5px; }
.login-card .dialog-err { margin: 12px 0 0; text-align: left; }

/* ปุ่มออกจากระบบท้ายหน้าประวัติ */
.signout { margin-top: 26px; text-align: center; }

/* จอแคบมาก (iPhone SE / มินิ) — โลโก้กับป้ายวันที่ชนกัน ตัดชื่อหน้าออก โลโก้กับแท็บบอกอยู่แล้วว่าคืออะไร */
@media (max-width: 369px) {
  .logo.has-img .brand { display: none; }
  .logo img { height: 32px; }
}
/* โลโก้บอกชื่อร้านแล้ว ไม่ต้องเขียนซ้ำใต้หัวข้อ */
.login-card:has(.login-mark.has-img) .login-sub { display: none; }
.login-card:has(.login-mark.has-img) h1 { margin-bottom: 20px; }
