/* ============ Tokens ============ */
:root {
  --bg: #f2f5f3;
  --card: #ffffff;
  --ink: #17231d;
  --muted: #64716a;
  --line: #e2e8e4;
  --brand: #0e9f6e;
  --brand-ink: #0a7a54;
  --brand-soft: #dff4ea;
  --danger: #d92d20;
  --danger-soft: #fee4e2;
  --warn-ink: #b54708;
  --warn-soft: #fef0c7;
  --cash-ink: #93540a;
  --cash-soft: #fdefc8;
  --paynow-ink: #4a44c6;
  --paynow-soft: #e4e2ff;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(23 35 29 / 0.05), 0 4px 14px rgb(23 35 29 / 0.06);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { block-size: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.45;
}

button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }

/* The browser's own [hidden] rule is only a default, so any author `display` beats it and the
   element stays on screen. Every hidden={...} toggle in the JS depends on this winning. */
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.ok { color: var(--brand-ink); }
.bad { color: var(--danger); }
.loading { color: var(--muted); text-align: center; padding-block: 32px; }
.center { text-align: center; }
.ta-right { text-align: right; }

/* ============ App shell ============ */
#app {
  block-size: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

#appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--brand);
  color: #fff;
  padding: 10px 14px;
  padding-block-start: calc(10px + env(safe-area-inset-top));
}
.appbar-title { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16.5px; }
.appbar-title svg { inline-size: 22px; block-size: 22px; }
.appbar-side { display: flex; align-items: center; gap: 8px; }
.role-chip {
  background: rgb(255 255 255 / 0.2);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12.5px;
  font-weight: 600;
}

#view {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding: 14px 14px 20px;
  inline-size: 100%;
  max-inline-size: 560px;
  margin-inline: auto;
}

#tabbar {
  display: flex;
  background: var(--card);
  border-block-start: 1px solid var(--line);
  padding: 5px 4px calc(5px + env(safe-area-inset-bottom));
}
#tabbar button {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 500;
  padding: 4px 0;
  min-block-size: 50px;
  border-radius: 10px;
  min-inline-size: 0;
}
#tabbar button svg { inline-size: 23px; block-size: 23px; }
#tabbar button.active { color: var(--brand-ink); font-weight: 700; }
#tabbar button[hidden] { display: none; }

/* ============ Buttons ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-block-size: 50px;
  padding: 0 18px;
  border: none;
  border-radius: 14px;
  font-size: 16px;
  font-weight: 650;
  transition: filter 0.12s ease, transform 0.05s ease;
}
.btn:active:not(:disabled) { transform: scale(0.985); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:active:not(:disabled) { filter: brightness(0.94); }
.btn-ghost { background: var(--card); border: 1px solid var(--line); color: var(--ink); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-ghost { background: none; border: 1px solid var(--danger-soft); color: var(--danger); }
.btn-block { inline-size: 100%; }
.btn-small { min-block-size: 40px; font-size: 14px; padding: 0 14px; }
.btn-row { display: flex; gap: 10px; margin-block-start: 16px; }
.btn-row .btn { flex: 1; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 42px;
  block-size: 42px;
  border: none;
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  flex-shrink: 0;
}
.icon-btn svg { inline-size: 21px; block-size: 21px; }
.icon-btn.light { background: rgb(255 255 255 / 0.18); color: #fff; }
.icon-btn.danger { background: var(--danger-soft); color: var(--danger); }

/* ============ Cards / shared layout ============ */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card.pad { padding: 16px; }
.card + .card { margin-block-start: 12px; }
.card-title { font-size: 17px; font-weight: 700; margin-block-end: 4px; }

.stack { display: grid; gap: 10px; }
.stack.tight { gap: 8px; margin-block: 10px 14px; }

.section-title { font-size: 16px; font-weight: 700; margin-block: 18px 10px; }
.section-hint { color: var(--muted); font-size: 13.5px; margin-block: 4px 12px; }

/* ============ Chips ============ */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 650;
  white-space: nowrap;
}
.chip-ok { background: var(--brand-soft); color: var(--brand-ink); }
.chip-disc { background: var(--danger-soft); color: var(--danger); }
.chip-warn { background: var(--warn-soft); color: var(--warn-ink); }
.chip-cash { background: var(--cash-soft); color: var(--cash-ink); }
.chip-paynow { background: var(--paynow-soft); color: var(--paynow-ink); }
.chip-muted { background: #edf1ee; color: var(--muted); }

/* ============ Sale date picker (sale screen) ============ */
.day-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-block-end: 10px;
}
.day-row .day-label { font-weight: 650; font-size: 14.5px; }
.day-row input {
  margin-inline-start: auto;
  block-size: 40px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  padding: 0 10px;
  font-size: 15px;
  font-weight: 650;
}

/* ============ Product row + stepper ============ */
.prow {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 8px 12px 14px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.prow .info { flex: 1; min-inline-size: 0; }
.prow .name { font-weight: 700; font-size: 16.5px; }
.prow .meta {
  color: var(--muted);
  font-size: 13px;
  margin-block-start: 3px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.stepper {
  display: flex;
  align-items: center;
  gap: 3px;
  background: var(--bg);
  border-radius: 14px;
  padding: 4px;
  flex-shrink: 0;
}
.stepper button {
  inline-size: 44px;
  block-size: 44px;
  border: none;
  border-radius: 11px;
  background: var(--card);
  font-size: 24px;
  line-height: 1;
  color: var(--brand-ink);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.1);
}
.stepper button:disabled { color: #b8c4be; background: transparent; box-shadow: none; cursor: default; }
.stepper input {
  inline-size: 42px;
  block-size: 44px;
  border: none;
  background: transparent;
  text-align: center;
  font-size: 18px;
  font-weight: 750;
  padding: 0;
  min-inline-size: 0;
}

/* ============ Floating action bar (sale screen) ============ */
.action-bar {
  position: sticky;
  bottom: 0;
  inset-block-end: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  margin-block-start: 14px;
  padding: 10px 12px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 -1px 2px rgb(23 35 29 / 0.04), 0 6px 20px rgb(23 35 29 / 0.14);
}
.bar-text { flex: 1; font-weight: 700; min-inline-size: 0; }
.bar-btn { min-block-size: 48px; }

/* ============ Form ============ */
.field { display: grid; gap: 6px; margin-block-end: 12px; }
.field > span { font-size: 13.5px; font-weight: 600; color: var(--muted); }
.text-input {
  inline-size: 100%;
  block-size: 50px;
  border: 1.5px solid var(--line);
  border-radius: 13px;
  padding: 0 14px;
  font-size: 16px; /* >=16px để iOS không tự zoom */
  background: var(--card);
}
.text-input:focus { outline: none; border-color: var(--brand); }
.text-input.num { inline-size: 110px; text-align: right; font-weight: 700; }
.money-input {
  inline-size: 100%;
  block-size: 54px;
  border: 1.5px solid var(--line);
  border-radius: 13px;
  padding: 0 14px;
  font-size: 20px;
  font-weight: 750;
  text-align: right;
}
.money-input:focus { outline: none; border-color: var(--brand); }
.money-input.big { block-size: 58px; font-size: 24px; }
.pin-input {
  inline-size: 100%;
  block-size: 58px;
  border: 1.5px solid var(--line);
  border-radius: 13px;
  padding: 0 16px;
  font-size: 24px;
  letter-spacing: 8px;
  text-align: center;
  font-weight: 700;
}
.pin-input:focus { outline: none; border-color: var(--brand); }
.pin-input::placeholder { letter-spacing: 1px; font-size: 15px; font-weight: 500; }
.form-error { color: var(--danger); font-size: 14px; font-weight: 600; margin-block: 8px; }

.inline-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.inline-field .name { font-weight: 650; }

/* ============ Day navigation ============ */
.day-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-block-end: 4px;
}
.day-nav input {
  block-size: 44px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  padding: 0 12px;
  font-size: 15px;
  font-weight: 650;
}
.day-caption { text-align: center; color: var(--muted); font-size: 13.5px; font-weight: 600; margin-block-end: 12px; }

/* ============ Segmented control (orders / restocks) ============ */
.seg-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: #e6ebe8;
  border-radius: 13px;
  padding: 4px;
  margin-block-end: 12px;
}
.seg {
  border: none;
  background: transparent;
  border-radius: 10px;
  block-size: 42px;
  font-weight: 650;
  color: var(--muted);
}
.seg.active { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }

/* ============ Stock card ============ */
.total-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  margin-block-end: 4px;
}
.total-big { font-size: 24px; font-weight: 800; }
.stock-card { padding: 14px 16px; }
.stock-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.stock-head .name { font-weight: 700; }
.stock-qty { font-size: 26px; font-weight: 800; color: var(--brand-ink); }
.stock-qty.zero { color: var(--danger); font-size: 18px; }
.stock-meta { margin-block-start: 2px; font-size: 13.5px; display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.stock-value { font-size: 13px; margin-block-start: 6px; }

/* ============ Order card / log ============ */
.order-card { position: relative; padding: 12px 14px; }
.order-card:has(.ctx-menu) { z-index: 30; }
.order-head { display: flex; align-items: center; gap: 8px; font-size: 14.5px; }
.order-items { color: var(--muted); font-size: 14px; margin-block-start: 4px; }
.order-amounts { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-block-start: 8px; }
.order-total { font-size: 19px; margin-inline-end: auto; }
.order-note { font-style: italic; color: var(--muted); font-size: 13.5px; margin-block-start: 6px; }
.del-btn { position: absolute; inset-block-start: 10px; inset-inline-end: 10px; inline-size: 36px; block-size: 36px; }
.order-card .order-head { padding-inline-end: 40px; }

.order-kebab, .del-btn {
  position: absolute;
  inset-block-start: 8px;
  inset-inline-end: 8px;
  inline-size: 36px;
  block-size: 36px;
}

/* Dropdown menu on an order card's "..." button */
.ctx-menu {
  position: absolute;
  inset-block-start: 42px;
  inset-inline-end: 8px;
  z-index: 30;
  inline-size: max-content;
  min-inline-size: 160px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 13px;
  box-shadow: 0 10px 30px rgb(23 35 29 / 0.25);
  padding: 6px;
  display: grid;
  gap: 2px;
}
.ctx-menu.up {
  inset-block-start: auto;
  inset-block-end: 42px;
}
.ctx-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border: none;
  background: none;
  border-radius: 9px;
  font-size: 15px;
  font-weight: 650;
  color: var(--ink);
  text-align: start;
  white-space: nowrap;
}
.ctx-item svg { inline-size: 18px; block-size: 18px; flex-shrink: 0; }
.ctx-item:active { background: var(--bg); }
.ctx-item.danger { color: var(--danger); }
.ctx-item.disabled { color: var(--muted); cursor: default; }
@media (prefers-reduced-motion: no-preference) {
  .ctx-menu { animation: menu-in 0.12s ease-out; }
}
@keyframes menu-in {
  from { opacity: 0; transform: translateY(-4px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

/* Order edit row inside the sheet */
.edit-row { display: flex; align-items: center; gap: 8px; }
.edit-row .name { font-weight: 650; flex: 1; min-inline-size: 0; }
.edit-row .muted { font-size: 13px; margin-inline-end: 4px; }

/* ============ End of day ============ */
.stat-row { display: flex; justify-content: space-between; align-items: center; padding-block: 7px; font-size: 15.5px; }
.stat-row.total { border-block-start: 1px dashed var(--line); margin-block-start: 4px; padding-block-start: 12px; font-size: 17px; }
.stat-row.sub { justify-content: space-around; color: var(--muted); font-size: 14px; padding-block-start: 10px; }

.breakdown { border-block-start: 1px dashed var(--line); margin-block-start: 8px; padding-block-start: 4px; }
.breakdown-title {
  font-size: 12.5px;
  font-weight: 750;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding-block: 6px 2px;
}
.breakdown .stat-row { padding-block: 4px; font-size: 15px; }

.transfer { margin-block-start: 12px; border: 1.5px solid var(--warn-soft); }
.transfer-amount { display: grid; gap: 2px; text-align: center; margin-block-end: 6px; }
.transfer-hint { color: var(--muted); font-size: 13.5px; text-align: center; margin-block: 4px 14px; }
.transfer.done { border-color: var(--brand-soft); }
.done-head { color: var(--brand-ink); font-size: 16.5px; margin-block-end: 4px; }

.hist-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 13px 14px;
  border: none;
  text-align: start;
  min-inline-size: 0;
}
.hist-left { min-inline-size: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-block: 10px 4px; }
.stat-cell { background: var(--bg); border-radius: 12px; padding: 10px; display: grid; gap: 2px; font-size: 13px; }
.stat-cell b { font-size: 16px; }

/* ============ Sheet (<dialog>) ============ */
dialog#sheet {
  border: none;
  border-radius: 20px 20px 0 0;
  padding: 6px 18px calc(18px + env(safe-area-inset-bottom));
  inline-size: min(100%, 520px);
  margin: auto auto 0;
  max-block-size: 88dvh;
  overflow-y: auto;
}
dialog#sheet::backdrop { background: rgb(10 20 15 / 0.5); }
@media (min-width: 560px) {
  dialog#sheet { border-radius: 20px; margin: auto; padding-block-end: 18px; }
}
dialog#sheet[open] { animation: sheet-in 0.22s cubic-bezier(0.2, 0.9, 0.3, 1); }
@keyframes sheet-in {
  from { transform: translateY(40px); opacity: 0.4; }
  to { transform: translateY(0); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  dialog#sheet[open] { animation: none; }
  .btn:active:not(:disabled) { transform: none; }
}
.sheet-body { display: grid; gap: 10px; }
.sheet-title { font-size: 19px; font-weight: 800; }
.sheet-msg { color: var(--muted); font-size: 14.5px; }

.item-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.total-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  background: var(--bg);
  border-radius: 13px;
  padding: 12px 14px;
  font-size: 15px;
}
.total-line b { font-size: 22px; }
.quick-row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.quick-row .btn { min-block-size: 42px; font-size: 13.5px; padding: 0 8px; }
.pay-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pay-grid .field { margin-block-end: 0; }
.pay-cash .money-input { border-color: var(--cash-soft); background: #fffdf5; }
.pay-now .money-input { border-color: var(--paynow-soft); background: #fbfbff; }
.calc-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
  padding-inline: 4px;
}
.calc-row b { font-size: 17px; }
.calc-row.big b { font-size: 21px; }

/* ============ Login page ============ */
.login-wrap {
  min-block-size: calc(100dvh - 40px);
  display: grid;
  place-content: center;
  gap: 18px;
  padding: 20px 6px;
}
.login-card {
  inline-size: min(92vw, 360px);
  background: var(--card);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 30px 24px 24px;
  text-align: center;
  display: grid;
  gap: 14px;
}
.login-logo {
  inline-size: 68px;
  block-size: 68px;
  margin-inline: auto;
  border-radius: 20px;
  background: var(--brand);
  color: #fff;
  display: grid;
  place-items: center;
}
.login-logo svg { inline-size: 36px; block-size: 36px; }
.login-title { font-size: 22px; font-weight: 800; }
.login-sub { color: var(--muted); font-size: 14px; }
.login-card .field { text-align: start; }
.setup-note, .warn-note {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  text-align: start;
  font-size: 13.5px;
  color: var(--warn-ink);
  background: var(--warn-soft);
  border-radius: 12px;
  padding: 10px 12px;
}
.setup-note svg, .warn-note svg { inline-size: 18px; block-size: 18px; flex-shrink: 0; margin-block-start: 1px; }
.login-foot { color: var(--muted); font-size: 12.5px; text-align: center; }

/* ============ Order photo (Paynow proof) ============ */
.photo-field {
  display: grid;
  gap: 8px;
  border: 1.5px dashed var(--line);
  border-radius: 13px;
  padding: 10px 12px;
}
.photo-row { display: flex; align-items: center; gap: 8px; }
.photo-label { font-weight: 650; font-size: 14.5px; }
/* the status also works as the spacer, so both buttons stay together on the right */
.photo-status { color: var(--muted); font-size: 13px; margin-inline-end: auto; }
.photo-row .photo-pick { min-block-size: 40px; font-size: 14px; padding: 0 14px; }
.photo-row .photo-pick svg { inline-size: 18px; block-size: 18px; }
.photo-row .icon-btn { inline-size: 40px; block-size: 40px; }
.photo-input { display: none; }

/* Thumbnail; tapping it expands the image in place (in a sheet) or opens the viewer (log card) */
.photo-preview {
  display: block;
  text-align: start; /* a <button> centres its content by default */
  padding: 0;
  border: none;
  background: none;
  line-height: 0;
  border-radius: 12px;
}
.photo-thumb {
  inline-size: 72px;
  block-size: 72px;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
}
.photo-preview.expanded .photo-thumb {
  inline-size: 100%;
  block-size: auto;
  max-block-size: 50dvh;
  object-fit: contain;
}
.order-card .photo-preview { margin-block-start: 8px; }
.photo-full {
  inline-size: 100%;
  max-block-size: 66dvh;
  object-fit: contain;
  border-radius: 13px;
  background: var(--bg);
}

/* ============ Empty / error states ============ */
.empty { text-align: center; color: var(--muted); padding-block: 40px 30px; }
.empty-emoji { font-size: 40px; margin-block-end: 8px; }
.boot-error {
  min-block-size: 70dvh;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 14px;
  text-align: center;
  color: var(--muted);
  padding: 20px;
}

/* ============ Toast ============ */
#toast {
  position: fixed;
  inset-block-start: calc(14px + env(safe-area-inset-top));
  inset-inline: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.toast {
  background: #1c2a23;
  color: #fff;
  padding: 11px 18px;
  border-radius: 14px;
  font-size: 14.5px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
  max-inline-size: min(88vw, 480px);
  text-align: center;
  animation: toast-in 0.2s ease-out;
}
.toast.err { background: #93261c; }
.toast.out { opacity: 0; transition: opacity 0.28s ease; }
@keyframes toast-in {
  from { transform: translateY(-8px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
