:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e3e5e9;
  --text: #1a1d22;
  --text-dim: #6b7280;
  --accent: #2563eb;
  --accent-press: #1e40af;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #f59e0b;
  --urgent: #ef4444;

  --status-awaiting: #f59e0b;
  --status-progress: #2563eb;
  --status-done: #16a34a;
  --status-bad: #dc2626;
  --status-warehouse: #8b5cf6;

  --field-bg: #ffffff;
  --hover-bg: rgba(0,0,0,0.02);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115;
  --surface: #1a1d23;
  --border: #2a2e36;
  --text: #e6e8eb;
  --text-dim: #9ba1ac;
  --accent: #4f8cff;
  --accent-press: #6ba2ff;
  --danger: #f87171;
  --success: #4ade80;
  --warning: #fbbf24;
  --urgent: #f87171;

  --status-awaiting: #d97706;
  --status-progress: #2563eb;
  --status-done: #15803d;
  --status-bad: #b91c1c;
  --status-warehouse: #7c3aed;

  --field-bg: #0f1115;
  --hover-bg: rgba(255,255,255,0.04);
}

/* Авто-определение темы, если пользователь не выбирал явно */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #1a1d23;
    --border: #2a2e36;
    --text: #e6e8eb;
    --text-dim: #9ba1ac;
    --accent: #4f8cff;
    --accent-press: #6ba2ff;
    --danger: #f87171;
    --success: #4ade80;
    --warning: #fbbf24;
    --urgent: #f87171;
    --status-awaiting: #d97706;
    --status-progress: #2563eb;
    --status-done: #15803d;
    --status-bad: #b91c1c;
    --status-warehouse: #7c3aed;
    --field-bg: #0f1115;
    --hover-bg: rgba(255,255,255,0.04);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body, #root { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg);
  /* плавное переключение темы */
  transition: background-color 0.2s ease, color 0.2s ease;
  /* запрет «pull-to-refresh» от системы (мы делаем свой) */
  overscroll-behavior-y: contain;
}

button { font: inherit; cursor: pointer; }
/* iOS Safari зумит при фокусе на input < 16px. Делаем 16px на инпутах */
input, textarea, select {
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background-color: var(--field-bg);
}
/* Принудительно красим выпадающий список (Windows/Chrome берёт системные цвета) */
select option {
  background-color: var(--field-bg);
  color: var(--text);
}
a { color: var(--accent); text-decoration: none; }

.app {
  display: flex; flex-direction: column;
  min-height: 100vh; max-width: 640px; margin: 0 auto;
  background: var(--bg);
  /* Safe area для iPhone с челкой */
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  gap: 8px;
}
.topbar .title { font-weight: 600; font-size: 16px; }
.topbar .who { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.theme-btn {
  font-size: 22px; padding: 4px 8px; min-height: auto !important;
  flex-shrink: 0;
}

.tabs {
  display: flex; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 49px; z-index: 9;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.tabs::-webkit-scrollbar { height: 0; }
.tabs button {
  flex: 1 0 auto; padding: 12px 10px; border: 0; background: transparent; font-weight: 500;
  color: var(--text-dim); border-bottom: 2px solid transparent;
  white-space: nowrap; min-width: max-content;
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
.tabs .badge { display: inline-block; margin-left: 6px; padding: 1px 6px; font-size: 11px;
  background: var(--bg); border-radius: 8px; color: var(--text-dim); }

/* ─── Подвкладки ─── Меньше и компактнее главных tabs, без sticky */
.subtabs {
  display: flex; gap: 4px; padding: 6px 8px; background: var(--bg);
  border-bottom: 1px solid var(--border); overflow-x: auto;
}
.subtabs::-webkit-scrollbar { height: 0; }
.subtab {
  flex: 0 0 auto; padding: 6px 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text-dim); font-size: 13px;
  border-radius: 16px; white-space: nowrap; cursor: pointer;
}
.subtab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.subtab-badge {
  display: inline-block; margin-left: 6px; padding: 0 6px; font-size: 11px;
  background: rgba(0,0,0,0.15); border-radius: 8px; line-height: 16px;
}
.subtab.active .subtab-badge { background: rgba(255,255,255,0.25); }

/* Бейдж "запланировано" на карточках — едва заметный, чтобы не отвлекал, но видный */
.scheduled-badge {
  display: inline-block; padding: 2px 8px; font-size: 11px;
  background: rgba(120, 120, 120, 0.2); color: var(--text-dim);
  border-radius: 10px; white-space: nowrap;
}

.list { padding: 8px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; margin-bottom: 8px;
}
.card .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.card .id { font-weight: 600; }
.card .meta { font-size: 12px; color: var(--text-dim); margin-top: 4px; }

.status {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 12px; padding: 3px 8px; border-radius: 999px; color: white; font-weight: 500;
}
.status.awaiting_courier, .status.pending { background: var(--status-awaiting); }
.status.in_transit, .status.bought { background: var(--status-progress); }
.status.awaiting_confirmation { background: #0891b2; }
.status.completed, .status.received, .status.done { background: var(--status-done); }
.status.cancelled, .status.failed, .status.return_requested, .status.return_in_transit, .status.returned { background: var(--status-bad); }
.status.on_warehouse { background: var(--status-warehouse); }
.status.delivered { background: #0d9488; }

.urgent-badge {
  display: inline-block; background: var(--urgent); color: white; font-size: 11px;
  padding: 2px 6px; border-radius: 4px; margin-right: 6px; font-weight: 600;
}

.btn {
  display: inline-block; border: 0; padding: 12px 14px; border-radius: 8px;
  font-weight: 500; min-height: 44px; /* mobile */
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:active { background: var(--accent-press); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.full { width: 100%; }
.btn[disabled] { opacity: 0.5; pointer-events: none; }

.fab {
  position: fixed; right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom)); z-index: 20;
  background: var(--accent); color: #fff; border: 0; border-radius: 50%;
  width: 56px; height: 56px; font-size: 28px;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
}

.modal-bg {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 30;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--surface); width: 100%; max-width: 640px; max-height: 90vh;
  overflow-y: auto; overflow-x: hidden; box-sizing: border-box;
  border-radius: 12px 12px 0 0; padding: 16px;
}

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; max-width: 100%; box-sizing: border-box;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--field-bg); color: var(--text);
}
/* Фикс: некоторые WebView (MAX/Chrome) расширяют <select> под самый длинный option,
   игнорируя width родителя. text-overflow ограничивает текст в свёрнутом виде. */
.field select {
  appearance: none; -webkit-appearance: none; -moz-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
                    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 12px) 50%;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 32px;
  text-overflow: ellipsis;
}
.field textarea { resize: vertical; min-height: 64px; }
.field .hint { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.field .err { font-size: 12px; color: var(--danger); margin-top: 4px; }

.btn.dense { padding: 4px 8px; min-height: auto; font-size: 13px; }
.switch-row { display: flex; gap: 8px; align-items: center; cursor: pointer; user-select: none; }
.switch-row input { width: auto; margin: 0; }
.switch-row span { font-size: 14px; }

.actions { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.actions.row { flex-direction: row; }

.empty { text-align: center; color: var(--text-dim); padding: 32px 16px; }

.photos { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.photos img { width: 80px; height: 80px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }

.event-list { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border); }
.event-list .ev { font-size: 13px; padding: 6px 0; color: var(--text-dim); }

.pull-hint {
  text-align: center; color: var(--text-dim); font-size: 12px; padding: 8px 0;
  height: 28px;
}

.item-row {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  margin-bottom: 6px; background: var(--hover-bg);
}
.item-row .row1 { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.item-row .row2 { font-size: 12px; color: var(--text-dim); margin-top: 4px; }

.preview-thumb { width: 48px; height: 48px; object-fit: cover; border-radius: 4px; cursor: pointer; }

.photos img { cursor: pointer; transition: transform 0.1s; }
.photos img:active { transform: scale(0.95); }

.lightbox-bg {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0, 0, 0, 0.92);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.lightbox-img {
  max-width: 100%; max-height: 100%;
  object-fit: contain;
  user-select: none; -webkit-user-select: none;
}
.lightbox-controls {
  position: absolute; top: max(16px, env(safe-area-inset-top)); right: 16px;
  display: flex; gap: 8px; z-index: 101;
}
.lb-btn {
  background: rgba(0, 0, 0, 0.5); color: #fff !important;
  font-size: 22px; padding: 6px 14px; border-radius: 8px;
  min-height: 44px;
}
.lb-btn:active { background: rgba(0, 0, 0, 0.8); }
.lightbox-loading { color: rgba(255,255,255,0.8); font-size: 14px; }

.tag {
  display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 11px;
  background: var(--bg); color: var(--text-dim); margin-right: 4px;
}

.center { text-align: center; }
.muted { color: var(--text-dim); }
.dense { font-size: 13px; }
.spaced > * + * { margin-top: 8px; }

.section-header {
  font-weight: 600; font-size: 14px; color: var(--text);
  padding: 16px 4px 6px; text-transform: uppercase; letter-spacing: 0.5px;
}
.section-header:first-child { padding-top: 8px; }
.group-header {
  font-weight: 600; font-size: 13px; color: var(--text-dim);
  padding: 6px 4px; background: var(--hover-bg); border-radius: 6px; margin: 6px 0 4px;
}
.comment-block { font-size: 13px; padding: 4px 0; color: var(--text-dim); font-style: italic; }
