:root {
  --red: #b71c1c;
  --red-dark: #8e1414;
  --yellow: #ffd600;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --line: #e2e2e6;
  --bg: #f4f4f6;
  --card: #fff;
  --wa: #1fa855;
  --radius: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  padding-bottom: calc(84px + var(--safe-b));
}

/* ---- Top bar ---- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  background: var(--red); color: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.2);
}
.topbar-logo { width: 34px; height: 34px; }
.topbar-title { display: flex; flex-direction: column; line-height: 1.1; margin-right: auto; }
.topbar-title strong { font-size: 17px; }
.topbar-title span { font-size: 13px; opacity: .85; }
.conn {
  font-size: 12px; padding: 3px 8px; border-radius: 99px;
  background: rgba(0,0,0,.25); white-space: nowrap;
}
.conn.ok { background: rgba(255,255,255,.95); color: var(--wa); font-weight: 600; }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: transparent; color: inherit; cursor: pointer; font-size: 18px;
}
.topbar .icon-btn:hover { background: rgba(255,255,255,.15); }
.icon-btn.sm { width: 34px; height: 34px; font-size: 16px; color: var(--muted); }
.icon-btn.sm:hover { background: var(--bg); }
.icon-btn.danger:hover { color: var(--red); }

/* ---- Layout ---- */
.wrap { max-width: 880px; margin: 0 auto; padding: 12px 16px; display: grid; gap: 12px; }
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 14px 16px 16px; border: 1px solid var(--line);
}
.card h2 { margin: 0 0 10px; font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--red); }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head h2 { margin: 0; }
h4 { margin: 20px 0 6px; font-size: 14px; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; min-width: 0; }
.field > span { font-size: 12.5px; color: var(--muted); font-weight: 600; }
input, textarea, select {
  font: inherit; color: inherit; width: 100%;
  padding: 10px 12px; border: 1px solid #cfcfd4; border-radius: 9px;
  background: #fff; min-height: 44px;
}
textarea { resize: vertical; text-transform: uppercase; overflow: hidden; }
#cName, #cAttn, .i-note { text-transform: uppercase; }
input:focus, textarea:focus { outline: 2px solid var(--red); outline-offset: -1px; border-color: transparent; }
.row2, .row3 { display: grid; gap: 0 12px; grid-template-columns: 1fr; }
@media (min-width: 560px) {
  .row2 { grid-template-columns: 1fr 1fr; }
  .row3 { grid-template-columns: 1fr 1fr 1fr; }
}
.hint { font-size: 13px; color: var(--muted); margin: 8px 0 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chips-label { font-size: 12.5px; color: var(--muted); font-weight: 600; margin-right: 2px; }
.chips button {
  font: inherit; font-size: 13px; padding: 6px 12px; border-radius: 99px;
  border: 1px solid var(--line); background: #fff; cursor: pointer;
}
.chips button.on { background: var(--red); color: #fff; border-color: var(--red); }

/* ---- Buttons ---- */
.btn {
  font: inherit; font-weight: 600; font-size: 15px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 16px; border-radius: 10px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  cursor: pointer; text-decoration: none; text-align: center;
}
.btn:hover { border-color: #bdbdc4; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; }
.btn.primary:hover { background: var(--red-dark); }
.btn.ghost { background: var(--bg); }
.btn.wa { background: var(--wa); border-color: var(--wa); color: #fff; }
.btn.wa-outline { border-color: var(--wa); color: var(--wa); }
.btn:disabled { opacity: .55; cursor: default; }
.btn[hidden] { display: none; }

/* ---- Items ---- */
.items { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.item { border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: #fcfcfd; }
.item-top { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.item-no {
  flex: none; width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
  background: var(--red); color: #fff;
}
.item-no:empty { background: var(--line); }
.item-tools { margin-left: auto; display: flex; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button {
  font: inherit; font-size: 12.5px; padding: 7px 8px; border: 0; background: #fff; cursor: pointer;
  border-right: 1px solid var(--line); white-space: nowrap;
}
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--ink); color: #fff; }
.item .i-desc { margin-bottom: 8px; }
.item .i-pick {
  margin-bottom: 8px; font-weight: 600; cursor: pointer;
  appearance: none; -webkit-appearance: none; padding-right: 36px;
  background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23b71c1c' stroke-width='2' fill='none'/%3E%3C/svg%3E") no-repeat right 14px center;
  border-color: #e3b9b9; text-overflow: ellipsis;
}
.item .i-pick.empty { color: var(--red); }
.item .i-price, .item .i-note { background: #fffbe6; border-color: #e8d27a; font-weight: 600; }
.item.just-picked .i-price, .item.just-picked .i-note { box-shadow: 0 0 0 3px rgba(255, 214, 0, .55); }
.item-nums { display: grid; grid-template-columns: 1fr 1fr 1.1fr; gap: 0 8px; align-items: end; }
.item-nums .field { margin-bottom: 0; }
.line-total { display: flex; flex-direction: column; align-items: flex-end; justify-content: center; min-height: 44px; }
.line-total span { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.line-total strong { font-size: 17px; }
.item[data-type="item"] .f-note { display: none; }
.item[data-type="note"] .f-qty, .item[data-type="note"] .f-price, .item[data-type="note"] .f-total { display: none; }
.item[data-type="note"] .item-nums { grid-template-columns: 1fr; }
.item[data-type="text"] .item-nums { display: none; }
.item-flags { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 8px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer; min-height: 32px; }
.check input { width: 18px; height: 18px; min-height: 0; accent-color: var(--red); }
.add-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.add-row .btn { padding: 10px 6px; font-size: 14px; }

/* ---- Bottom bar ---- */
.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px calc(10px + var(--safe-b));
  background: #fff; border-top: 1px solid var(--line);
  box-shadow: 0 -2px 10px rgba(0,0,0,.06);
}
.bottombar .total { display: flex; flex-direction: column; margin-right: auto; line-height: 1.15; }
.bottombar .total span { font-size: 12px; color: var(--muted); font-weight: 600; }
.bottombar .total strong { font-size: 22px; }
.bottombar .btn { min-width: 150px; min-height: 48px; font-size: 16px; }
@media (min-width: 880px) {
  .bottombar { padding-left: calc((100vw - 848px) / 2); padding-right: calc((100vw - 848px) / 2); }
}

/* ---- Sheets (dialogs) ---- */
dialog.sheet {
  border: 0; padding: 0; border-radius: 16px; width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 40px); max-height: calc(100dvh - 40px);
  box-shadow: 0 10px 40px rgba(0,0,0,.3); color: var(--ink);
}
dialog.sheet[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(0,0,0,.45); }
@media (max-width: 559px) {
  dialog.sheet {
    margin: auto 0 0; width: 100vw; max-width: 100vw;
    border-radius: 16px 16px 0 0; max-height: 92dvh;
  }
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 8px 10px 16px; border-bottom: 1px solid var(--line);
}
.sheet-head h3 { margin: 0; font-size: 17px; }
.sheet-body { padding: 14px 16px calc(16px + var(--safe-b)); overflow: auto; }
.preview { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-bottom: 10px; background: #eee; }
.preview iframe { display: block; width: 100%; height: 56vh; border: 0; }
.preview[hidden] { display: none; }
.actions { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; }
.actions .btn { width: 100%; }
@media (max-width: 400px) { .actions { grid-template-columns: 1fr; } }
.status { font-size: 14px; margin: 4px 0 12px; color: var(--muted); }
.status.ok { color: var(--wa); }
.status.err { color: var(--red); }

.search { margin-bottom: 0; }
.pick-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.pick-tools .search { flex: 1; }
.pick-tools .btn { flex: none; min-height: 44px; }
.pick-list li.pick-edit {
  display: block; border: 2px solid var(--red); background: #fffdf5; padding: 12px;
}
.pick-edit .field[hidden] { display: none; }
.pick-edit .check { margin-bottom: 10px; }
.edit-bar { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pick-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.pick-list li {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.pick-list .pick-main { flex: 1; min-width: 0; cursor: pointer; text-align: left; border: 0; background: none; font: inherit; padding: 0; color: inherit; }
.pick-list .pick-desc { display: block; font-weight: 600; font-size: 14px; white-space: pre-line; }
.pick-list .pick-meta { display: block; font-size: 13px; color: var(--muted); }
.pick-list.compact li { padding: 8px 10px; font-size: 14px; }
.pick-empty { color: var(--muted); font-size: 14px; justify-content: center; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
