* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f3f4f6;
  color: #111827;
}
.hidden { display: none !important; }
.muted  { color: #6b7280; }
.small  { font-size: 12px; }
.error  { color: #b91c1c; margin-top: 8px; min-height: 1em; }

.view { min-height: 100vh; }

/* ========== Login =========================================================== */
.login-card {
  max-width: 420px;
  margin: 8vh auto;
  padding: 32px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 18px rgba(0,0,0,.06);
  display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { margin: 0; font-size: 22px; color: #dc2626; }
.role-tabs {
  display: flex; gap: 6px; margin: 8px 0;
  background: #f3f4f6; padding: 4px; border-radius: 10px;
}
.role-tab {
  flex: 1; padding: 8px;
  background: transparent; color: #374151;
  border: none; border-radius: 6px; font-size: 13px; cursor: pointer;
}
.role-tab.active { background: #fff; color: #dc2626; box-shadow: 0 1px 3px rgba(0,0,0,.1); }

label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: #374151; }
input, select, textarea {
  padding: 10px 12px; border: 1px solid #d1d5db; border-radius: 8px;
  font-size: 14px; font-family: inherit; background: #fff;
}
.phone-input {
  display: flex; align-items: stretch;
  border: 1px solid #d1d5db; border-radius: 8px; overflow: hidden;
  background: #fff;
}
.phone-input .phone-prefix {
  padding: 10px 12px; background: #f3f4f6; border-right: 1px solid #d1d5db;
  color: #374151; font-size: 14px; user-select: none;
}
.phone-input input {
  flex: 1; border: none; border-radius: 0; outline: none;
  padding: 10px 12px; font-size: 14px; min-width: 0;
}
.phone-input input:focus { background: #fffbeb; }

/* === Адрес с автоподсказками от /api/geo/suggest ========================= */
.addr-input { position: relative; display: block; }
.addr-input > input {
  width: 100%; box-sizing: border-box;
  padding: 10px 12px; border: 1px solid #d1d5db; border-radius: 8px;
  font-size: 14px; font-family: inherit; background: #fff; outline: none;
}
.addr-input > input:focus { border-color: #2563eb; background: #fffbeb; }
.addr-suggest {
  position: absolute; top: calc(100% + 2px); left: 0; right: 0; z-index: 20;
  background: #fff; border: 1px solid #d1d5db; border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0,0,0,.12);
  max-height: 240px; overflow-y: auto;
}
.addr-suggest-item {
  padding: 8px 12px; font-size: 13px; cursor: pointer;
  border-bottom: 1px solid #f3f4f6;
}
.addr-suggest-item:last-child { border-bottom: none; }
.addr-suggest-item:hover { background: #f3f4f6; }

/* Структурный адрес: улица+дом сверху, под ней четыре коротких поля. */
.addr-block { display: flex; flex-direction: column; gap: 6px; }
.addr-block .addr-extras {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
}
.addr-block .addr-extras > input {
  padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 8px;
  font-size: 13px; font-family: inherit; background: #fff; outline: none;
  min-width: 0;
}
.addr-block .addr-extras > input:focus { border-color: #2563eb; background: #fffbeb; }
button {
  padding: 10px 14px;
  border: none;
  border-radius: 8px;
  background: #dc2626;
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
button:hover { background: #b91c1c; }
button.secondary { background: #e5e7eb; color: #111827; }
button.secondary:hover { background: #d1d5db; }
button.small { padding: 6px 10px; font-size: 12px; }

/* admin-only элементы скрыты для остальных ролей; класс is-admin
   ставится на <body> после логина если me.role === 'admin'. */
body:not(.is-admin) .admin-only { display: none !important; }
.col-bulk { width: 36px; text-align: center; }
.col-bulk input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; }

/* ========== Topbar ========================================================== */
.topbar {
  background: #fff;
  padding: 12px 24px;
  display: flex; align-items: center; justify-content: space-between;
  border-bottom: 1px solid #e5e7eb;
}
.topbar h1 { margin: 0; font-size: 18px; color: #dc2626; }
.topbar nav { display: flex; gap: 4px; flex: 1; justify-content: center; flex-wrap: wrap; }
.user-block { display: flex; align-items: center; gap: 12px; font-size: 13px; color: #374151; }

.tab {
  background: transparent; color: #374151; padding: 8px 14px;
  border-radius: 6px; cursor: pointer; border: none;
}
.tab.active { background: #fee2e2; color: #dc2626; }
.tab:hover  { background: #fee2e2; color: #dc2626; }
.tab-badge {
  display: inline-block; margin-left: 6px; padding: 1px 7px;
  background: #dc2626; color: #fff; border-radius: 10px;
  font-size: 11px; font-weight: 600; min-width: 18px; text-align: center;
}

main { padding: 24px; }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; gap: 12px; }
.page-head h2 { margin: 0; }
.filters   { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.section-h { margin: 24px 0 12px; font-size: 16px; }

/* ========== Stat cards (главная + карточка курьера) ======================== */
.stat-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat-card {
  background: #fff; padding: 18px 20px; border-radius: 12px;
  border-left: 4px solid #d1d5db;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.stat-card.stat-online  { border-left-color: #16a34a; }
.stat-card.stat-orders  { border-left-color: #2563eb; }
.stat-card.stat-money   { border-left-color: #ca8a04; }
.stat-card.stat-cancel  { border-left-color: #dc2626; }
.stat-label { font-size: 12px; color: #6b7280; text-transform: uppercase; }
.stat-value { font-size: 32px; font-weight: 700; margin: 4px 0; }
.stat-sub   { font-size: 12px; color: #6b7280; }

/* ========== Dashboard cards ================================================ */
.dash-cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px;
}
.dash-card {
  background: #fff; padding: 16px; border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
  display: flex; align-items: center; gap: 14px; cursor: pointer;
  transition: transform .1s;
}
.dash-card:hover { transform: translateY(-2px); }
.dash-icon {
  width: 56px; height: 56px; border-radius: 14px;
  background: #fee2e2; color: #dc2626; font-size: 28px;
  display: flex; align-items: center; justify-content: center; position: relative;
}
.dash-card-title { font-weight: 600; }
.dash-card-sub   { font-size: 12px; color: #6b7280; }

/* ========== Active orders list ============================================= */
.active-orders { display: flex; flex-direction: column; gap: 8px; }
.order-row {
  background: #fff; border-radius: 12px; padding: 12px 16px;
  display: grid; grid-template-columns: 60px 1fr 120px 80px 60px 220px 220px;
  align-items: center; gap: 12px;
  border: 2px solid transparent;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.order-row.order-red    { border-color: #dc2626; background: #fef2f2; }
.order-row.order-yellow { border-color: #ca8a04; background: #fefce8; }
.order-row.order-green  { border-color: #16a34a; background: #f0fdf4; }
.order-no { font-weight: 700; font-size: 18px; color: #6b7280; }
.order-title { font-weight: 600; }
.order-pay  { font-weight: 700; color: #16a34a; }
.order-time { font-family: monospace; font-size: 18px; color: #374151; }
.time-expired { color: #dc2626; font-weight: 700; }
.order-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

/* ========== Couriers ====================================================== */
.couriers-list {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px;
}
.courier-card {
  background: #fff; padding: 14px 16px; border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.courier-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.courier-row.clickable { cursor: pointer; }
.courier-row.clickable:hover .courier-name { color: #dc2626; }
.status-dot {
  width: 10px; height: 10px; border-radius: 999px;
}
.status-dot.online  { background: #16a34a; }
.status-dot.offline { background: #d1d5db; }
.courier-name { font-weight: 600; flex: 1; }
.courier-comment {
  background: #fefce8; padding: 6px 10px; border-radius: 8px;
  font-size: 12px; color: #92400e; margin: 8px 0;
}
.courier-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

/* ========== Карточка курьера (детально) =================================== */
.cd-grid {
  display: grid; grid-template-columns: 1fr 280px; gap: 24px; margin-top: 16px;
}
.cd-side {}
.cd-info {
  background: #fff; padding: 16px; border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
  display: flex; flex-direction: column; gap: 8px;
  font-size: 14px;
}
.cd-tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.cd-chat-feed {
  background: #fff; border-radius: 10px; padding: 12px;
  height: 400px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
@media (max-width: 900px) {
  .cd-grid { grid-template-columns: 1fr; }
}

/* ========== Tables ======================================================== */
.data-table {
  width: 100%;
  background: #fff;
  border-collapse: collapse;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.data-table th, .data-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid #f3f4f6;
  font-size: 14px;
  vertical-align: top;
}
.data-table th { background: #f9fafb; font-weight: 600; font-size: 12px; color: #6b7280; text-transform: uppercase; }
.data-table tr:last-child td { border-bottom: none; }

.price-old { text-decoration: line-through; color: #9ca3af; margin-right: 4px; font-size: 12px; }
.discount-badge {
  background: #16a34a; color: #fff; padding: 2px 8px; border-radius: 99px;
  font-size: 12px; font-weight: 600;
}

/* ========== Badges ======================================================== */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 500;
}
.badge-new                { background: #dbeafe; color: #1e40af; }
.badge-courier_assigned   { background: #fef3c7; color: #92400e; }
.badge-working,
.badge-on_site_pickup,
.badge-picked_up,
.badge-on_site_delivery   { background: #e0e7ff; color: #3730a3; }
.badge-delivered          { background: #d1fae5; color: #065f46; }
.badge-cancelled          { background: #fee2e2; color: #991b1b; }
.badge-pending            { background: #fef3c7; color: #92400e; }
.badge-approved           { background: #e0f2fe; color: #075985; }
.badge-paid               { background: #d1fae5; color: #065f46; }
.badge-rejected           { background: #fee2e2; color: #991b1b; }

/* ========== Chat ========================================================== */
.chat-tabs { display: flex; gap: 4px; margin-bottom: 16px; }
.chat-controls { margin: 8px 0 16px; }
.chat-feed { display: flex; flex-direction: column; gap: 10px; max-height: 60vh; overflow-y: auto; }
.chat-msg {
  background: #fff; padding: 12px 14px; border-radius: 10px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04); border-left: 4px solid #d1d5db;
}
.chat-msg.label-new         { border-left-color: #dc2626; background: #fef2f2; }
.chat-msg.label-in_progress { border-left-color: #16a34a; background: #f0fdf4; }
.chat-msg.label-done        { border-left-color: #d1d5db; background: #fff; }
.chat-msg.msg-mine  { border-left-color: #2563eb; background: #eff6ff; }
.chat-msg.msg-from  { border-left-color: #ca8a04; }
.chat-msg-head  { font-size: 13px; }
.chat-msg-body  { margin: 6px 0; white-space: pre-wrap; }
.chat-msg-actions { display: flex; gap: 4px; }

/* Кнопки меток в чате диспетчера: тон у каждой свой (по неактивке — бледный
   фон, активная — насыщенный). Совпадает с фоном сообщения, чтобы статус был
   читаем и без открытия. */
.chat-msg-actions .lbl-new       { background: #fee2e2; color: #991b1b; }
.chat-msg-actions .lbl-prog      { background: #d1fae5; color: #065f46; }
.chat-msg-actions .lbl-done      { background: #f3f4f6; color: #6b7280; }
.chat-msg-actions .lbl-new.active   { background: #dc2626; color: #fff; }
.chat-msg-actions .lbl-prog.active  { background: #16a34a; color: #fff; }
.chat-msg-actions .lbl-done.active  { background: #6b7280; color: #fff; }

.chat-send { display: flex; gap: 8px; margin-top: 16px; }
.chat-send input { flex: 1; }

/* ----- Поддержка курьеров (inbox + thread) ----- */
.support-grid {
  display: grid; grid-template-columns: 320px 1fr; gap: 16px;
}
.support-inbox {
  background: #fff; border-radius: 12px; padding: 8px;
  max-height: 70vh; overflow-y: auto; box-shadow: 0 1px 2px rgba(0,0,0,.04);
  display: flex; flex-direction: column; gap: 4px;
}
.support-item {
  padding: 10px 12px; border-radius: 8px; cursor: pointer;
  border-left: 4px solid #d1d5db;
}
.support-item:hover    { background: #fef2f2; }
.support-item.selected { background: #fee2e2; }
/* Новый (есть непрочитанное от курьера) — красный */
.support-item.has-unread          { background: #fef2f2; border-left-color: #dc2626; }
/* В работе (последнее сообщение от диспетчера, нет непрочитанных) — зелёный */
.support-item.is-replied          { background: #f0fdf4; border-left-color: #16a34a; }
/* Выполнено (нет переписки или давно) — серая полоска без выделения */
.support-item-head { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.support-item-text { margin-top: 4px; line-clamp: 2; -webkit-line-clamp: 2; overflow: hidden; }
.unread-badge {
  background: #dc2626; color: #fff; padding: 1px 6px; border-radius: 99px;
  font-size: 11px; font-weight: 700; margin-left: auto;
}
#chat-support-badge {
  background: #dc2626; color: #fff; padding: 1px 6px; border-radius: 99px;
  font-size: 11px; font-weight: 700; margin-left: 6px;
}
.support-thread {
  background: #fff; border-radius: 12px; padding: 16px;
  display: flex; flex-direction: column; min-height: 70vh;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.support-thread-feed {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
  padding-right: 4px;
}
@media (max-width: 768px) {
  .support-grid { grid-template-columns: 1fr; }
}

/* ========== Forms ========================================================= */
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.details-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; font-size: 14px;
}

/* ========== Modal ========================================================= */
#modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center; z-index: 10;
}
.modal {
  background: #fff; padding: 24px; border-radius: 12px;
  min-width: 400px; max-width: 720px; max-height: 90vh; overflow: auto;
  display: flex; flex-direction: column; gap: 12px;
}
.modal h3 { margin: 0 0 4px 0; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ========== Mobile compaction (< 1024) ==================================== */
@media (max-width: 1023px) {
  .order-row {
    grid-template-columns: 50px 1fr 80px;
    grid-template-rows: auto auto auto;
  }
  .order-cargo, .order-pay, .order-time { grid-column: 1 / -1; font-size: 13px; }
  .order-status, .order-actions { grid-column: 1 / -1; }
  .topbar { flex-wrap: wrap; }
  .topbar nav { order: 3; flex-basis: 100%; justify-content: flex-start; }
}
