:root {
  --ink: #1f2a37;
  --muted: #6b7684;
  --line: #e3e7ec;
  --bg: #f5f7fa;
  --card: #ffffff;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --wa: #1fa855;
  --danger: #c0392b;
  --radius: 12px;
  --tap: 44px;
  color-scheme: light;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
h1 { font-size: 1.4rem; margin: 8px 0 14px; }
h2 { font-size: 1.2rem; margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.wrap { max-width: 1400px; margin: 0 auto; padding: 12px 16px; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  background: var(--ink); color: #fff; padding: 0 12px; padding-top: env(safe-area-inset-top);
  min-height: 52px;
}
.brand { color: #fff; font-weight: 700; text-decoration: none; white-space: nowrap; }
.nav { display: flex; gap: 2px; margin-left: auto; }
.nav a {
  color: #cfd6df; text-decoration: none; padding: 0 10px; min-height: var(--tap);
  display: flex; align-items: center; gap: 6px; border-radius: 8px; font-size: .95rem;
}
.nav a.on { color: #fff; background: rgba(255,255,255,.12); }
.badge { background: #f59e0b; color: #1f2a37; border-radius: 10px; padding: 0 7px; font-size: .75rem; font-weight: 700; }
.logout button {
  background: none; border: 0; color: #cfd6df; font: inherit; font-size: .85rem;
  min-height: var(--tap); padding: 0 6px; cursor: pointer;
}
@media (max-width: 480px) {
  .brand { display: none; }
  .nav { margin-left: 0; flex: 1; }
  .nav a { flex: 1; justify-content: center; padding: 0 4px; }
}

/* buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.whatsapp { background: var(--wa); border-color: var(--wa); color: #fff; }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; min-height: 52px; font-size: 1.05rem; }
.btn:disabled { opacity: .6; cursor: default; }
input, select, textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS zooming on focus */
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
  min-height: var(--tap); padding: 0 12px; width: 100%;
}
textarea { padding: 10px 12px; resize: vertical; }
.stack { display: grid; gap: 12px; }
label.stack, .stack label { display: grid; gap: 6px; font-weight: 600; font-size: .95rem; }
.alert { background: #fdecea; color: var(--danger); padding: 10px 12px; border-radius: 10px; }
.empty { text-align: center; color: var(--muted); padding: 48px 16px; }

/* login */
.login { max-width: 380px; margin: 10vh auto 0; background: var(--card); padding: 24px; border-radius: var(--radius); box-shadow: 0 2px 10px rgba(0,0,0,.06); }
.login h1 { margin-top: 0; }

/* catalog toolbar */
.toolbar { display: flex; gap: 8px; position: sticky; top: 52px; z-index: 10; background: var(--bg); padding: 8px 0; }
.toolbar input { flex: 1; }
.toolbar select { width: auto; flex: 0 0 auto; }
/* category tree: folders under the client's running items, subfolders are branches */
.tree-toggle {
  display: flex; align-items: center; gap: 6px; width: 100%; min-height: var(--tap); margin: 2px 0 8px;
  padding: 0 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink);
  font: inherit; font-size: .95rem; text-align: left; cursor: pointer;
}
.tt-label { color: var(--muted); }
.tt-path { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-toggle[aria-expanded="true"] .tt-caret { transform: rotate(180deg); }
.tree-panel {
  display: none; background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 6px; margin-bottom: 10px; max-height: 60vh; overflow: auto; overscroll-behavior: contain;
}
.tree-panel.open { display: block; }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { margin-left: 14px; padding-left: 8px; border-left: 1px solid var(--line); }
.tree .row { display: flex; align-items: stretch; border-radius: 8px; }
.tree .row.on { background: var(--ink); }
.tree .row.on .name, .tree .row.on .caret { color: #fff; }
.tree .row.on .n { color: #cfd6df; }
.tree .caret, .tree .spacer { flex: 0 0 34px; }
.tree .caret {
  border: 0; background: none; color: var(--muted); font-size: .8rem; cursor: pointer; border-radius: 8px;
}
.tree .caret[aria-expanded="true"] { transform: rotate(90deg); }
.tree .name {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 8px 0 0;
  border: 0; background: none; color: var(--ink); font: inherit; font-size: .92rem; text-align: left; cursor: pointer;
  overflow-wrap: anywhere;
}
.tree .name .n { margin-left: auto; color: var(--muted); font-size: .8rem; padding-left: 6px; }
.tree .row:not(.on):hover { background: var(--bg); }
.tree .top-level > .row .name { font-weight: 600; }
@media (min-width: 900px) {
  .tree-toggle { display: none; }
  .cat-layout.has-tree { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 16px; align-items: start; }
  .tree-panel {
    display: block; position: sticky; top: 120px; max-height: calc(100vh - 136px); margin: 0;
  }
  /* the sidebar takes width, so fit columns to the space left instead of the viewport */
  .has-tree .grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
}

/* grid: 2 cols phone, 3-4 tablet, 5-6 desktop */
.grid { display: grid; gap: 10px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 640px)  { .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (min-width: 900px)  { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; } }
@media (min-width: 1150px) { .grid { grid-template-columns: repeat(5, minmax(0, 1fr)); } }
@media (min-width: 1350px) { .grid { grid-template-columns: repeat(6, minmax(0, 1fr)); } }
.card {
  background: var(--card); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column;
  border: 2px solid transparent; box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.card.in { border-color: var(--accent); }
.card .thumb { display: block; width: 100%; aspect-ratio: 800 / 976; background: #fff; border: 0; padding: 0; cursor: zoom-in; }
.card .thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.card .body { padding: 8px; display: grid; gap: 6px; flex: 1; grid-template-rows: 1fr auto auto; } /* controls line up across a row */
.card .top { align-self: start; display: grid; gap: 4px; min-width: 0; }
.card .cat { font-size: .78rem; line-height: 1.3; color: var(--muted); overflow-wrap: anywhere; }
.card .cat .sep { color: #b4bcc6; }
/* the client's own note on a part */
.pnote-show {
  display: block; width: 100%; text-align: left; font: inherit; font-size: .82rem; line-height: 1.35; color: #6b4e00;
  background: #fff7e0; border: 1px solid #f3e1a6; border-radius: 8px; padding: 6px 8px; cursor: pointer;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.pnote-add {
  border: 0; background: none; padding: 4px 0; min-height: 32px; text-align: left; cursor: pointer;
  color: var(--accent); font: inherit; font-size: .82rem; font-weight: 600;
}
.pnote-edit { display: grid; gap: 6px; }
.pnote-edit textarea { font-size: 16px; padding: 6px 8px; min-height: 72px; }
.pnote-actions { display: flex; gap: 6px; }
.pnote-actions .btn { flex: 1; min-height: 38px; padding: 0 8px; }
.pnote-err { color: var(--danger); font-size: .8rem; }
.card select { min-height: 40px; font-size: 16px; padding: 0 8px; }

/* quantity stepper */
.stepper { display: flex; align-items: stretch; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #fff; }
.stepper button {
  flex: 0 0 var(--tap); min-height: var(--tap); border: 0; background: #eef2f7; color: var(--ink);
  font-size: 1.35rem; font-weight: 600; cursor: pointer; touch-action: manipulation;
}
.stepper button:active { background: #dde4ee; }
.stepper input { border: 0; border-radius: 0; text-align: center; min-width: 0; padding: 0 2px; font-weight: 700; }

/* fixed basket bar */
.basketbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; align-items: center; gap: 12px;
  justify-content: space-between; background: var(--card); border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); box-shadow: 0 -4px 14px rgba(0,0,0,.06);
}
.basketbar span { font-weight: 600; }

/* lightbox */
.lightbox { position: fixed; inset: 0; z-index: 50; background: rgba(15,20,28,.92); display: flex; flex-direction: column; }
.lb-body { flex: 1; overflow: auto; display: flex; align-items: center; justify-content: center; touch-action: pinch-zoom pan-x pan-y; }
.lb-body img { max-width: 100%; max-height: 100%; background: #fff; }
.lb-close {
  position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 10px; width: var(--tap); height: var(--tap);
  border-radius: 50%; border: 0; background: rgba(255,255,255,.18); color: #fff; font-size: 1.2rem; cursor: pointer;
}

/* basket lines */
.lines { display: grid; gap: 10px; margin-bottom: 16px; }
.line {
  display: grid; grid-template-columns: 72px 1fr auto; gap: 10px; align-items: center;
  background: var(--card); border-radius: var(--radius); padding: 8px;
}
.line img { width: 72px; height: 88px; object-fit: contain; background: #fff; border-radius: 8px; }
.line .info { display: grid; gap: 6px; min-width: 0; }
.line .code { font-weight: 700; }
.line .ctrl { display: grid; gap: 6px; grid-template-columns: 1fr; }
.line .remove {
  align-self: start; width: 36px; height: 36px; border: 0; background: none; color: var(--muted); font-size: 1.1rem; cursor: pointer;
}
@media (min-width: 640px) {
  .line { grid-template-columns: 88px 1fr auto; }
  .line .ctrl { grid-template-columns: 1fr 170px; }
}
.totals { font-weight: 700; margin: 12px 0; }
.notice { background: #fff7e6; color: #8a5a00; padding: 10px 12px; border-radius: 10px; margin-bottom: 10px; }

/* done */
.done { background: var(--card); border-radius: var(--radius); padding: 24px 18px; text-align: center; display: grid; gap: 12px; max-width: 480px; margin: 0 auto; }
.done .tick { width: 56px; height: 56px; border-radius: 50%; background: #e7f7ee; color: var(--wa); font-size: 1.8rem; display: grid; place-items: center; margin: 0 auto; }

/* orders */
.order { background: var(--card); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; }
.order header { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.order header strong { margin-right: 8px; }
.order ul { list-style: none; padding: 0; margin: 10px 0; display: grid; gap: 4px; }
.order li { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; font-size: .95rem; border-bottom: 1px dashed var(--line); padding-bottom: 4px; }
.order .code { font-weight: 600; }
.order .qty { font-weight: 700; }
.order .note { background: var(--bg); padding: 8px 10px; border-radius: 8px; white-space: pre-wrap; font-size: .9rem; }
.order .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.order .actions .btn { flex: 1 1 160px; }
.btn.danger { color: var(--danger); }
.ostatus { font-size: .78rem; font-weight: 700; padding: 2px 10px; border-radius: 10px; white-space: nowrap; }
.st-new { background: #e8f0fe; color: #1d4ed8; }
.st-processing { background: #fff4d6; color: #8a5a00; }
.st-done { background: #e7f7ee; color: #13643a; }
.st-cancelled { background: #eef0f3; color: var(--muted); }
.order p.muted { margin: 4px 0 0; }
#editBar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
#editBar .small-btn { min-height: 36px; margin-left: auto; }
