:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efeee9;
  --text: #1d1d1b;
  --muted: #6b6a65;
  --line: #dddbd3;
  --accent: #1d1d1b;
  --accent-text: #ffffff;
  --ok: #1f7a4d;
  --ok-bg: #e2f2e9;
  --warn: #9a5b00;
  --warn-bg: #fbefd9;
  --bad: #b3261e;
  --bad-bg: #fbe4e2;
  --confirm: #1f7a4d;
  --toast-ok: #1f7a4d;
  --toast-bad: #b3261e;
  --radius: 14px;
  --tap: 56px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  /* Top safe area only matters when opened from the home screen (status bar on top).
     In Safari it changes while the toolbar shrinks, which added a gap above the top bar. */
  --safe-t: 0px;
  color-scheme: light;
}

/* Appearance (Menu > Appearance): <html data-theme="dark | light | system">.
   Dark is the default. "system" follows the device setting. */
:root[data-theme="dark"] {
  --bg: #121211;
  --surface: #1d1d1b;
  --surface-2: #2a2a27;
  --text: #f2f1ec;
  --muted: #a3a29b;
  --line: #34332f;
  --accent: #f2f1ec;
  --accent-text: #121211;
  --ok: #6fd3a0;
  --ok-bg: #173527;
  --warn: #f0b85a;
  --warn-bg: #3a2c12;
  --bad: #ff8a80;
  --bad-bg: #3d1a17;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] {
    --bg: #121211;
    --surface: #1d1d1b;
    --surface-2: #2a2a27;
    --text: #f2f1ec;
    --muted: #a3a29b;
    --line: #34332f;
    --accent: #f2f1ec;
    --accent-text: #121211;
    --ok: #6fd3a0;
    --ok-bg: #173527;
    --warn: #f0b85a;
    --warn-bg: #3a2c12;
    --bad: #ff8a80;
    --bad-bg: #3d1a17;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.muted { color: var(--muted); }
.error { color: var(--bad); margin: 0; }
.num { font-variant-numeric: tabular-nums; }

/* Buttons */
.btn {
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 600;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-danger { color: var(--bad); }
.btn-lg { min-height: var(--tap); font-size: 18px; }
.btn[disabled] { opacity: .4; pointer-events: none; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.icon-btn {
  width: 44px; height: 44px; border: 0; background: none; border-radius: 10px;
  display: grid; place-items: center; fill: currentColor;
}

.fs-btn .fs-exit { display: none; }
.fs-btn.on .fs-enter { display: none; }
.fs-btn.on .fs-exit { display: block; }
.icon-btn[hidden] { display: none; }

/* Form fields */
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field span { font-size: 14px; font-weight: 600; color: var(--muted); }
.field input, .field textarea, .field select {
  min-height: 50px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-size: 18px; width: 100%;
}
.field textarea { min-height: 160px; font-size: 15px; font-family: ui-monospace, Menlo, monospace; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; color: var(--muted); }
.check input { width: 22px; height: 22px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 28px;
}
.login-card h1 { font-size: 28px; margin-bottom: 8px; }
.login-card p { margin: 0 0 20px; }
.login-card .btn { width: 100%; margin-top: 8px; }
.login-card .error { margin: 10px 0 0; }
.login-card { max-width: 360px; padding: 20px; }
.pw-wrap { position: relative; }
.pw-wrap input {
  width: 100%; min-height: 56px; padding: 10px 56px 10px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg); font-size: 18px;
}
.pw-eye {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 48px; height: 48px; border: 0; background: none; color: var(--muted); border-radius: 10px;
}
.pw-eye[aria-pressed="true"] { color: var(--text); }
.captcha-step { display: grid; justify-items: center; gap: 12px; }
.captcha-box { min-height: 78px; }
.captcha-status { margin: 0; text-align: center; min-height: 1.4em; }
.captcha-step .btn { width: 100%; }

/* App shell: the page itself never scrolls (that made iPad browsers shift the top bar
   while their toolbar shrank and grew). Only the content area below the top bar scrolls. */
html, body { overflow: hidden; }
.login { height: 100%; overflow-y: auto; }
.app { position: fixed; inset: 0; display: flex; flex-direction: column; }
.view { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.topbar {
  position: relative; flex-shrink: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-t)) 12px 8px 16px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.brand { font-size: 18px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.sync {
  display: flex; align-items: center; gap: 8px; min-height: 36px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--surface);
  font-size: 14px; font-weight: 600;
}
.sync .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); }
.sync.saving .dot { background: var(--warn); animation: pulse 1s infinite; }
.sync.error { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }
.sync.error .dot { background: var(--bad); }
@keyframes pulse { 50% { opacity: .3; } }

.view { padding: 12px 16px calc(var(--nav-h) + var(--safe-b) + 24px); }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar button { border: 0; background: none; font-size: 15px; font-weight: 600; color: var(--muted); }
.tabbar button.active { color: var(--text); box-shadow: inset 0 3px 0 var(--text); }

.banner {
  padding: 12px 14px; border-radius: 12px; margin-bottom: 12px; font-weight: 600;
  display: flex; align-items: center; gap: 12px; justify-content: space-between;
}
.banner.bad { background: var(--bad-bg); color: var(--bad); }
.banner.warn { background: var(--warn-bg); color: var(--warn); }

/* Chips */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 12px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 40px; padding: 0 16px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600; font-size: 15px;
}
.chip.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.tag {
  display: inline-block; padding: 2px 8px; border-radius: 6px; font-size: 12px; font-weight: 700;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.tag.ok { background: var(--ok-bg); color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.bad { background: var(--bad-bg); color: var(--bad); }

/* Sell */
.sell { display: block; }
.grid {
  display: grid; gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  padding-bottom: 300px;
}
.product {
  position: relative; min-height: 96px; padding: 12px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--surface);
  display: flex; flex-direction: column; justify-content: space-between; align-items: flex-start;
  text-align: left; gap: 6px;
}
.product:active { transform: scale(.97); background: var(--surface-2); }
.product .p-name { font-weight: 650; font-size: 16px; line-height: 1.2;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.product .p-price { color: var(--muted); font-weight: 600; }
.product .qty {
  position: absolute; top: -6px; right: -6px; min-width: 28px; height: 28px; padding: 0 6px;
  border-radius: 14px; background: var(--accent); color: var(--accent-text);
  font-weight: 700; font-size: 15px; display: grid; place-items: center;
}
.product.in-cart { border-color: var(--text); }
.product .p-foot { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; width: 100%; }
.p-stock { font-size: 13px; font-weight: 700; color: var(--muted); white-space: nowrap; }
.p-stock.low { color: var(--warn); }
.p-stock.out, .stock-out { color: var(--bad); font-weight: 700; }
.product.sold-out { opacity: .6; }
.product.custom { border-style: dashed; color: var(--muted); }
.empty { padding: 32px 16px; text-align: center; color: var(--muted); }

.cart {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-b)); z-index: 25;
  background: var(--surface); border-top: 1px solid var(--line);
  box-shadow: 0 -8px 24px rgba(0,0,0,.08);
  display: flex; flex-direction: column; max-height: calc(100% - var(--nav-h) - 80px);
}
.cart-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; border: 0; background: none;
  width: 100%; text-align: left; min-height: 52px;
}
.cart-head .c-count { flex: 1; font-weight: 600; }
.cart-head .c-total { font-size: 22px; font-weight: 750; }
.cart-head .chev { transition: transform .2s; color: var(--muted); }
.cart.open .cart-head .chev { transform: rotate(180deg); }
.cart-body { display: none; overflow-y: auto; padding: 0 16px; }
.cart.open .cart-body { display: block; }
.line {
  display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.line-name { font-weight: 600; }
.line-price { border: 0; background: none; padding: 2px 0; color: var(--muted); text-decoration: underline dotted; text-align: left; }
.line-price.overridden { color: var(--warn); }
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper button {
  width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-size: 22px; font-weight: 600;
}
.stepper .q { min-width: 28px; text-align: center; font-weight: 700; font-size: 18px; }
.line-total { min-width: 72px; text-align: right; font-weight: 700; }
.cart-tools { display: flex; gap: 8px; padding: 12px 0; flex-wrap: wrap; }
.cart-tools .btn { flex: 1; min-height: 44px; }
.sums { padding: 4px 0 8px; }
.sums div { display: flex; justify-content: space-between; color: var(--muted); }
.cart-note { font-size: 14px; color: var(--muted); padding-bottom: 8px; }
.pay { padding: 8px 12px 12px; }
.pay-label { font-size: 13px; color: var(--muted); padding: 0 4px 6px; }
.methods { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.method {
  min-height: 54px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2);
  font-weight: 700; font-size: 14px; padding: 0 4px; line-height: 1.1;
}
.method:not([disabled]) { background: var(--surface); border: 2px solid var(--text); }
.method.selected:not([disabled]) { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.method[disabled] { opacity: .45; }
.method:active { transform: scale(.96); }
.confirm {
  width: 100%; min-height: 58px; margin-top: 8px; border-radius: 12px; border: 0;
  background: var(--confirm); color: #fff; font-size: 18px; font-weight: 750;
}
.confirm[disabled] { background: var(--surface-2); color: var(--muted); }
.confirm:not([disabled]):active { transform: scale(.98); }

/* iPad and wider: cart becomes a right column */
@media (min-width: 768px) {
  .sell { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 16px; align-items: start; }
  .grid { padding-bottom: 0; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .product { min-height: 110px; }
  /* The cart always fits the visible screen (with or without the Safari toolbar).
     Only the list of items scrolls; the payment buttons and Confirm never hide.
     --app-h and --top-h are measured by app.js. */
  .cart {
    position: sticky; top: 0; bottom: auto; left: auto; right: auto;
    border: 1px solid var(--line); border-radius: 18px; box-shadow: none;
    max-height: calc(var(--app-h, 100dvh) - var(--top-h, 61px) - var(--nav-h) - var(--safe-b) - 36px);
    overflow: hidden;
  }
  .cart-head { pointer-events: none; flex-shrink: 0; }
  .cart-head .chev { display: none; }
  .cart-body { display: block; flex: 1 1 auto; min-height: 0; }
  .pay { flex-shrink: 0; border-top: 1px solid var(--line); padding-top: 10px; }
  .methods { grid-template-columns: repeat(2, 1fr); }
  .method { min-height: 64px; font-size: 17px; }
  .method:last-child { grid-column: span 2; }
}
/* Short screens (iPad sideways with the Safari toolbar): tighter buttons so items keep room. */
@media (min-width: 768px) and (max-height: 820px) {
  .method { min-height: 52px; }
  .confirm { min-height: 54px; }
  .cart-head { min-height: 44px; padding-top: 6px; padding-bottom: 6px; }
}

/* Lists (sales, products) */
.list { display: grid; gap: 8px; }
.row {
  display: grid; grid-template-columns: 64px 1fr auto; gap: 4px 12px; align-items: center;
  padding: 12px 14px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--surface); text-align: left; width: 100%;
}
.row .r-time { font-weight: 700; color: var(--muted); font-size: 14px; }
.row .r-items { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .r-total { font-weight: 750; font-size: 18px; text-align: right; }
.row .r-tags { grid-column: 2 / 4; display: flex; gap: 6px; flex-wrap: wrap; }
.row.voided { opacity: .55; }
.row.voided .r-items, .row.voided .r-total { text-decoration: line-through; }
.list-head { display: flex; justify-content: space-between; align-items: baseline; margin: 4px 0 10px; gap: 12px; flex-wrap: wrap; }
.list-head h2 { font-size: 20px; }

.prow {
  display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center;
  padding: 10px 12px 10px 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface);
}
.prow.hidden-p { opacity: .55; }
.prow .p-meta { font-size: 14px; color: var(--muted); }
.group-title { font-size: 14px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 0 8px; }

/* Totals */
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 18px; }
@media (min-width: 768px) { .cards { grid-template-columns: repeat(4, 1fr); } }
.card { padding: 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }
.card .k { font-size: 14px; color: var(--muted); font-weight: 600; }
.card .v { font-size: 26px; font-weight: 750; margin-top: 4px; }
.card.strong { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.card.strong .k { color: inherit; opacity: .75; }
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; border: 1px solid var(--line); margin-bottom: 18px; }
.table th, .table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--line); }
.table th { font-size: 13px; color: var(--muted); font-weight: 700; }
.table td.r, .table th.r { text-align: right; }
.table tr:last-child td { border-bottom: 0; }
.section-title { font-size: 18px; margin: 6px 0 10px; }

/* Modal */
.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 560px; max-height: 90%; overflow-y: auto;
  background: var(--bg); border-radius: 20px 20px 0 0; padding: 20px 20px calc(20px + var(--safe-b));
}
@media (min-width: 768px) {
  .modal { align-items: center; }
  .sheet { border-radius: 20px; }
}
.sheet h2 { font-size: 22px; margin-bottom: 14px; }
.sheet .actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet .actions .btn { flex: 1; min-height: 52px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 14px; }
.seg button { min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; }
.seg button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.seg.three { grid-template-columns: repeat(3, 1fr); margin-bottom: 20px; }
.menu-label { font-size: 14px; font-weight: 700; color: var(--muted); margin-bottom: 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin-bottom: 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-weight: 600; word-break: break-all; }
.items-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.items-table td { padding: 6px 0; border-bottom: 1px solid var(--line); }
.items-table td.r { text-align: right; }
.preview { font-size: 14px; max-height: 180px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; background: var(--surface); margin-bottom: 14px; }
.menu-list { display: grid; gap: 8px; }
.menu-list .btn { min-height: 52px; text-align: left; }

/* Toast */
/* Snackbar: bottom of the screen, above the cart and tab bar. Green for success, red for errors. */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 60;
  bottom: var(--toast-bottom, calc(var(--nav-h) + var(--safe-b) + 12px));
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: max-content; min-width: min(340px, calc(100% - 32px)); max-width: calc(100% - 32px);
  min-height: 52px; padding: 8px 8px 8px 16px; border-radius: 14px;
  background: var(--toast-ok); color: #fff; font-weight: 650; box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast button { min-height: 40px; padding: 0 16px; border-radius: 10px; border: 0; background: #fff; color: var(--toast-ok); font-weight: 750; }
.toast.bad { background: var(--toast-bad); }
.toast.bad button { color: var(--toast-bad); }

/* Printable report: hidden on screen, the only thing visible when printing */
.report { display: none; }
@media print {
  html, body { overflow: visible !important; height: auto !important; }
  body { background: #fff; color: #000; }
  body > *:not(.report) { display: none !important; }
  .report { display: block !important; font: 11pt/1.35 -apple-system, "Helvetica Neue", Arial, sans-serif; color: #000; }
  .report h1 { font-size: 18pt; margin-bottom: 4pt; }
  .report h2 { font-size: 13pt; margin: 16pt 0 6pt; }
  .report p { margin: 2pt 0; }
  .report table { width: 100%; border-collapse: collapse; }
  .report th, .report td { border-bottom: 0.5pt solid #999; padding: 4pt 4pt; text-align: left; vertical-align: top; }
  .report th { font-size: 9pt; text-transform: uppercase; }
  .report td.r, .report th.r { text-align: right; }
  .report tr { page-break-inside: avoid; }
  .report .void td { color: #777; text-decoration: line-through; }
  .report a { color: #000; word-break: break-all; }
  .report .summary td { font-weight: 700; }
  @page { margin: 14mm; }
}

/* iPad upright: slightly narrower cart so both columns fit. */
@media (min-width: 768px) and (max-width: 900px) {
  .sell { grid-template-columns: minmax(0, 1fr) 340px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

/* iPad and wider: the tabs move into the middle of the top bar as a compact switch,
   so the whole bottom of the screen is free for products and the cart. */
@media (min-width: 768px) {
  :root { --nav-h: 0px; }
  .tabbar {
    top: calc(var(--safe-t) + 10px); bottom: auto; left: 50%; right: auto; transform: translateX(-50%);
    height: 40px; padding: 3px; gap: 2px;
    grid-template-columns: repeat(4, auto);
    border: 1px solid var(--line); border-radius: 999px; background: var(--surface);
  }
  .tabbar button { padding: 0 18px; border-radius: 999px; font-size: 15px; }
  .tabbar button.active { box-shadow: none; background: var(--text); color: var(--bg); }
  .brand { max-width: calc(50% - 210px); }
  .topbar .sync { margin-left: auto; }
  .view { padding-bottom: calc(var(--safe-b) + 16px); }
}

@media (display-mode: standalone), (display-mode: fullscreen) {
  :root { --safe-t: env(safe-area-inset-top, 0px); }
}
