:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --surface-2: #eef2f1;
  --text: #13201d;
  --muted: #5b6b67;
  --border: #d8e0de;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #d5f0ec;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1513;
    --surface: #15201d;
    --surface-2: #1d2a27;
    --text: #e6efed;
    --muted: #93a6a1;
    --border: #2a3a36;
    --primary: #2dd4bf;
    --primary-ink: #04201c;
    --primary-soft: #11332e;
    --ok: #4ade80;
    --ok-soft: #12301f;
    --warn: #fbbf24;
    --warn-soft: #33270c;
    --danger: #f87171;
    --danger-soft: #3a1515;
    --shadow: none;
    color-scheme: dark;
  }
}

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

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 19px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .who { color: var(--muted); font-size: 14px; white-space: nowrap; }
.brand { display: flex; align-items: center; gap: 8px; }
.brand img { width: 30px; height: 30px; }

main {
  max-width: 1100px; margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 110px) max(16px, env(safe-area-inset-left));
}

.tabs {
  display: flex; gap: 6px; padding: 4px; margin-bottom: 16px;
  background: var(--surface-2); border-radius: 12px; overflow-x: auto;
}
.tabs button {
  flex: 1; min-width: max-content; border: 0; background: transparent;
  padding: 11px 14px; border-radius: 9px; font-weight: 600; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.tabs .count { display: inline-block; min-width: 22px; margin-left: 6px; padding: 0 6px; border-radius: 99px; background: var(--primary-soft); color: var(--primary); font-size: 13px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 600;
}
.btn:active { transform: scale(.98); }
.btn[disabled] { opacity: .45; pointer-events: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-lg { min-height: 60px; font-size: 18px; border-radius: 14px; }
.btn-block { width: 100%; }
.icon-btn { min-width: 44px; min-height: 44px; padding: 0 10px; border: 0; background: transparent; color: var(--primary); font-weight: 600; font-size: 16px; }

/* ---------- Cards / lists ---------- */
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(300px, 100%), 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.order-card { display: flex; flex-direction: column; gap: 10px; text-align: left; width: 100%; }
.order-card:active { transform: scale(.99); }
.order-card .row { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.order-card .customer { font-weight: 700; font-size: 18px; }
.order-card .ref { color: var(--muted); font-size: 14px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 14px; }

.badge { display: inline-flex; align-items: center; padding: 3px 10px; border-radius: 99px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.badge-pending { background: var(--surface-2); color: var(--muted); }
.badge-progress { background: var(--warn-soft); color: var(--warn); }
.badge-done { background: var(--ok-soft); color: var(--ok); }
.badge-missing { background: var(--danger-soft); color: var(--danger); }

.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--primary); border-radius: 99px; transition: width .25s; }

.empty { text-align: center; color: var(--muted); padding: 60px 16px; }
.empty b { display: block; color: var(--text); font-size: 18px; margin-bottom: 4px; }

/* ---------- Order detail ---------- */
.order-head { display: grid; gap: 8px; margin-bottom: 16px; }
.order-head .customer { font-size: 22px; font-weight: 700; }
.note { background: var(--warn-soft); color: var(--warn); padding: 10px 14px; border-radius: 10px; font-size: 15px; }

.lines { display: flex; flex-direction: column; gap: 8px; }
.line {
  display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 12px;
  width: 100%; text-align: left; padding: 14px 16px; min-height: 72px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  border-left: 6px solid var(--border);
}
.line.done { border-left-color: var(--ok); }
.line.missing { border-left-color: var(--danger); }
.line .name { font-weight: 650; font-size: 17px; }
.line .code { color: var(--muted); font-size: 13px; }
.line .qty { text-align: right; }
.line .qty .ordered { font-weight: 700; font-size: 18px; }
.line .qty .prepared { font-size: 14px; }
.line.done .prepared { color: var(--ok); font-weight: 600; }
.line.missing .prepared { color: var(--danger); font-weight: 600; }

.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; gap: 10px;
  padding: 12px max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 12px) max(16px, env(safe-area-inset-left));
  background: var(--surface); border-top: 1px solid var(--border);
}
.bottom-bar > * { flex: 1; }
.bottom-bar .btn-ghost { flex: 0 0 auto; }

/* ---------- Weighing sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; z-index: 50; background: rgb(0 0 0 / .45); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet {
  width: 100%; max-width: 720px; max-height: 100%; overflow-y: auto;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding: 16px max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 16px) max(16px, env(safe-area-inset-left));
  animation: up .2s ease-out;
}
@media (min-width: 760px) and (min-height: 700px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 20px; max-height: 94vh; }
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }

.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.sheet-head h2 { margin: 0; font-size: 21px; }
.sheet-head .code { color: var(--muted); font-size: 14px; }
.target { display: flex; justify-content: space-between; align-items: center; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; margin-bottom: 12px; }
.target b { font-size: 20px; }

.fields { display: grid; gap: 8px; grid-template-columns: 1fr 1fr; margin-bottom: 10px; }
.field {
  border: 2px solid var(--border); background: var(--surface); border-radius: 14px;
  padding: 8px 14px; text-align: left; min-height: 78px;
}
.field.big { grid-column: 1 / -1; }
.field[aria-pressed="true"] { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.field label { display: block; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.field .value { font-size: 34px; font-weight: 700; line-height: 1.2; }
.field.big .value { font-size: 52px; }
.field .value small { font-size: .45em; color: var(--muted); font-weight: 600; margin-left: 4px; }
.field .value .ph { color: var(--border); }

.delta { text-align: center; font-weight: 600; padding: 8px; border-radius: 10px; margin-bottom: 10px; font-size: 15px; }
.delta.ok { background: var(--ok-soft); color: var(--ok); }
.delta.warn { background: var(--warn-soft); color: var(--warn); }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.keypad button {
  min-height: 62px; font-size: 26px; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface); border-radius: 14px;
}
.keypad button:active { background: var(--primary-soft); }
@media (max-height: 900px) { .keypad button { min-height: 54px; } .field.big .value { font-size: 44px; } .field { min-height: 70px; } .sheet-head { margin-bottom: 8px; } .target, .fields, .delta { margin-bottom: 8px; } .target { padding: 8px 14px; } }

.comment-input { width: 100%; min-height: 48px; padding: 0 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); margin-bottom: 12px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; }

/* ---------- Forms / login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 400px; padding: 28px 24px; }
.login .brand { justify-content: center; margin-bottom: 20px; font-size: 26px; font-weight: 800; }
.login .brand img { width: 48px; height: 48px; }
.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
.form input, .form select {
  min-height: 52px; padding: 0 14px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); font-size: 17px;
}
.form input:focus, .form select:focus { outline: 2px solid var(--primary); border-color: transparent; }
.error { color: var(--danger); font-size: 14px; min-height: 1.4em; margin: 0; }

/* ---------- Admin table ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 12px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.table-wrap { overflow-x: auto; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin: 4px 0 12px; }
.section-head h2 { margin: 0; font-size: 20px; }
.inactive { opacity: .5; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.actions .btn { min-height: 40px; padding: 0 12px; font-size: 14px; }

#toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 12px); z-index: 100;
  transform: translate(-50%, -20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-weight: 600;
  max-width: calc(100% - 32px); transition: .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--danger); color: #fff; }

/* ---------- Affectation ---------- */
.badge-assigned { background: var(--primary); color: var(--primary-ink); }
.order-card.for-me { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); }
.unassigned { color: var(--warn); font-weight: 600; }
.assign { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 8px 12px; }
.assign span { font-weight: 600; color: var(--muted); font-size: 14px; }
.assign select, .role-select {
  flex: 1; min-width: 180px; min-height: 44px; padding: 0 10px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
}
.role-select { flex: none; min-width: 150px; min-height: 40px; }
