:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #14181f;
  --muted: #6b7280;
  --border: #e2e4e9;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --vip: #b45309;
  --vip-bg: #fef3c7;
  --nav-h: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #1a1d24;
    --text: #f0f1f3;
    --muted: #9aa1ad;
    --border: #2c303a;
    --accent: #3b82f6;
    --accent-text: #ffffff;
    --vip-bg: #3a2c0c;
    --vip: #fbbf24;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  height: 100%;
}

#app {
  padding: 16px 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 24px);
  padding-top: max(16px, env(safe-area-inset-top));
  min-height: 100vh;
}

h1 { font-size: 1.3rem; margin: 0 0 4px; }
h2 { font-size: 1.05rem; margin: 20px 0 8px; }
.subtitle { color: var(--muted); font-size: 0.9rem; margin: 0 0 16px; }

.nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  z-index: 20;
}
.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.72rem;
  font-family: inherit;
  padding: 6px 0;
}
.nav-icon { font-size: 1.3rem; line-height: 1; }
.nav-btn.active { color: var(--accent); font-weight: 600; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

.order-card { position: relative; }
.order-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.order-customer { font-weight: 700; font-size: 1.05rem; }
.order-phone { color: var(--accent); font-size: 0.85rem; text-decoration: none; }
.badge-vip {
  display: inline-block;
  background: var(--vip-bg);
  color: var(--vip);
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  margin-left: 6px;
  vertical-align: middle;
}
.order-items { margin: 8px 0 4px; font-size: 0.92rem; }
.order-item-row { display: flex; justify-content: space-between; padding: 2px 0; }
.order-item-row .date { color: var(--muted); }
.order-item-row .date.today { color: var(--accent); font-weight: 600; }
.order-item-row .done { color: #16a34a; }
.order-note { font-size: 0.85rem; color: var(--muted); font-style: italic; margin-top: 4px; }
.item-note { font-size: 0.82rem; color: var(--muted); font-style: italic; margin: -2px 0 4px; }

.order-actions { display: flex; gap: 6px; margin-top: 10px; }
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 9px;
  padding: 10px 12px;
  font-size: 0.9rem;
  font-family: inherit;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:active { opacity: 0.6; }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-danger { color: var(--danger); border-color: var(--danger); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 10px; min-height: 36px; font-size: 0.85rem; flex: 1; }
.btn-icon { padding: 6px; min-width: 40px; min-height: 40px; flex: 0 0 auto; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin: 12px 0 4px; }
input[type="text"], input[type="tel"], input[type="number"], textarea, select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.product-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.product-row:last-child { border-bottom: none; }
.product-row input[type="number"] { width: 90px; text-align: right; }
.product-name { font-weight: 500; }
.product-meta { color: var(--muted); font-size: 0.8rem; }

.product-row-block { padding: 8px 0; border-bottom: 1px solid var(--border); }
.product-row-block:last-child { border-bottom: none; }
.product-row-block .product-row { border-bottom: none; padding: 0 0 6px; }
.item-note-input { padding: 7px 10px; font-size: 0.85rem; }
.item-note-inline { color: var(--muted); font-size: 0.85rem; font-style: italic; }

.autocomplete { position: relative; }
.autocomplete-list {
  position: absolute;
  left: 0; right: 0; top: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  margin-top: 4px;
  max-height: 220px;
  overflow-y: auto;
  z-index: 10;
  box-shadow: 0 6px 18px rgba(0,0,0,0.15);
}
.autocomplete-item { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:active { background: var(--bg); }
.autocomplete-item .name { font-weight: 600; }
.autocomplete-item .phone { color: var(--muted); font-size: 0.8rem; }

.segmented { display: flex; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 3px; margin-bottom: 14px; }
.segmented button { flex: 1; border: none; background: none; padding: 9px 4px; border-radius: 8px; font-family: inherit; font-size: 0.88rem; color: var(--muted); }
.segmented button.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }

.empty { color: var(--muted); text-align: center; padding: 40px 10px; }

.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; }
.switch { position: relative; width: 46px; height: 28px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; z-index: 1; }
.switch .track { position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: background 0.15s; }
.switch .thumb { position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; background: #fff; border-radius: 50%; transition: transform 0.15s; box-shadow: 0 1px 3px rgba(0,0,0,0.3); }
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track + .thumb { transform: translateX(18px); }

.banner {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: 12px; right: 12px;
  background: var(--text);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  z-index: 50;
  text-align: center;
}
.banner.error { background: var(--danger); color: #fff; }

.dialog-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: flex-end;
  z-index: 40;
}
.dialog {
  background: var(--surface);
  color: var(--text);
  width: 100%;
  border-radius: 16px 16px 0 0;
  padding: 18px 18px calc(18px + env(safe-area-inset-bottom));
  max-height: 80vh;
  overflow-y: auto;
}
.dialog h3 { margin-top: 0; }
.dialog-actions { display: flex; gap: 10px; margin-top: 16px; }
.dialog-actions .btn { flex: 1; }
.change-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 0.9rem; }

.dispatch-customer { padding: 10px 0; border-bottom: 1px solid var(--border); }
.dispatch-customer:last-child { border-bottom: none; }
.dispatch-customer .name { font-weight: 700; }
.dispatch-customer .phone { color: var(--muted); font-size: 0.85rem; }
.dispatch-items { margin-top: 4px; font-size: 0.92rem; }
.totals-row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 0.95rem; }
.totals-row .qty { font-weight: 700; }

.back-row { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.back-btn { background: none; border: none; font-size: 1rem; color: var(--accent); padding: 6px 4px; font-family: inherit; }

.history-row { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--border); }
.history-row:last-child { border-bottom: none; }

@media print {
  #nav, .no-print { display: none !important; }
  #app { padding: 0; }
  body { background: #fff; color: #000; }
  .card { border: none; box-shadow: none; }
}
