:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #dce2ec;
  --text: #182230;
  --muted: #5b6778;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --primary-soft: #dbeafe;
  --amber: #b45309;
  --amber-soft: #fef3c7;
  --green: #15803d;
  --green-soft: #dcfce7;
  --red: #b91c1c;
  --red-soft: #fee2e2;
  --slate-soft: #e2e8f0;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}

.topbar {
  background: #0f172a;
  color: #fff;
  padding: 12px 20px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  background: #2563eb;
  color: #fff;
  font-weight: 700;
  padding: 5px 10px;
  border-radius: 6px;
  letter-spacing: 0.5px;
  font-size: 13px;
}

.brand-title {
  font-weight: 600;
  font-size: 16px;
}

.brand-sub {
  color: #94a3b8;
  font-size: 12px;
}

.topbar-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.role-tabs {
  display: flex;
  background: #1e293b;
  padding: 3px;
  border-radius: 8px;
  gap: 4px;
}

.role-btn {
  border: none;
  background: transparent;
  color: #cbd5e1;
  padding: 7px 13px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  transition: 0.15s;
}

.role-btn:hover {
  color: #fff;
}

.role-btn.active {
  background: #2563eb;
  color: #fff;
}

.period-picker {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #1e293b;
  padding: 4px 10px;
  border-radius: 8px;
}

.period-picker select,
.period-picker input {
  background: #0f172a;
  color: #fff;
  border: 1px solid #334155;
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
}

.container {
  max-width: 1600px;
  margin: 0 auto;
  padding: 16px 20px 48px;
}

.subnav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.subnav-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}

.subnav-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

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

.card-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.kpi-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.kpi-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.kpi-label {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 4px;
}

.kpi-val {
  font-size: 20px;
  font-weight: 700;
}

.btn {
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 7px 13px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-outline {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}

.btn-outline:hover {
  background: var(--surface-alt);
}

.btn-success {
  background: var(--green);
  color: #fff;
}

.btn-danger {
  background: var(--red);
  color: #fff;
}

.btn-sm {
  padding: 4px 9px;
  font-size: 12px;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

th,
td {
  padding: 7px 9px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

th {
  background: var(--surface-alt);
  font-weight: 700;
  color: #334155;
  position: sticky;
  top: 0;
}

tr:hover td {
  background: #f8fbff;
}

.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.sticky-col {
  position: sticky;
  left: 0;
  background: #fff;
  z-index: 2;
  border-right: 2px solid var(--border);
  font-weight: 600;
}

th.sticky-col {
  background: var(--surface-alt);
  z-index: 4;
}

/* Ячейки интерактивного табеля и графика */
.day-cell {
  text-align: center;
  padding: 4px 3px;
  min-width: 40px;
  cursor: pointer;
  border-right: 1px solid #edf1f7;
  user-select: none;
}

.day-cell:hover {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.day-cell.mode-o {
  background: var(--primary-soft);
  color: #1e3a8a;
  font-weight: 700;
}

.day-cell.mode-p {
  background: var(--slate-soft);
  color: #1e293b;
  font-weight: 600;
}

.day-cell.mode-pending {
  background: #ffedd5;
  color: #9a3412;
  font-weight: 800;
  box-shadow: inset 0 0 0 1.5px #f97316;
}

.day-cell.mode-active {
  background: var(--green-soft);
  color: #14532d;
  font-weight: 800;
  box-shadow: inset 0 0 0 1.5px #16a34a;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.shift-state-card {
  margin-bottom: 12px;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 13.5px;
  line-height: 1.45;
  border: 1px solid var(--border);
  background: var(--surface-alt);
}

.shift-state-card.pending {
  background: #fff7ed;
  border-color: #fdba74;
  color: #9a3412;
}

.shift-state-card.active {
  background: #f0fdf4;
  border-color: #86efac;
  color: #14532d;
}

.shift-state-card.closed {
  background: #eff6ff;
  border-color: #93c5fd;
  color: #1e3a8a;
}

.pending-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #dc2626;
  color: #fff;
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 11px;
  font-weight: 800;
  margin-left: 6px;
}

.day-cell.sched-approved {
  background: var(--green-soft);
  color: #14532d;
  font-weight: 700;
}

.day-cell.sched-desired {
  background: #fef9c3;
  color: #854d0e;
}

.day-cell.sched-absent {
  background: var(--red-soft);
  color: var(--red);
}

.day-sub {
  display: block;
  font-size: 9.5px;
  line-height: 1.1;
  font-weight: 600;
}

.badge {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
}

.badge-brig {
  background: var(--amber-soft);
  color: var(--amber);
}

.badge-ok {
  background: var(--green-soft);
  color: var(--green);
}

.badge-warn {
  background: var(--red-soft);
  color: var(--red);
}

.badge-info {
  background: var(--primary-soft);
  color: var(--primary);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  align-items: end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.field label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.field input,
.field select,
.field textarea {
  width: 100%;
  min-width: 0;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  background: #fff;
}

.report-input {
  width: 72px;
  padding: 5px 6px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 12.5px;
}

.report-input.wide {
  width: 130px;
}

/* Календарь выбора смен сотрудника */
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(42px, 1fr));
  gap: 6px;
  max-width: 520px;
}

.cal-day {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 6px;
  text-align: center;
  cursor: pointer;
  background: #fff;
  font-weight: 600;
}

.cal-day.selected {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.cal-day.approved {
  outline: 2px solid var(--green);
}

/* Модальное окно */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  background: #fff;
  border-radius: 12px;
  max-width: 460px;
  max-height: 90vh;
  overflow-y: auto;
  width: 100%;
  padding: 20px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.22);
}

.modal.wide {
  max-width: 680px;
}

.modal h3 {
  margin: 0 0 12px;
  font-size: 16px;
}

.toast {
  position: fixed;
  bottom: 18px;
  right: 18px;
  background: #0f172a;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  display: none;
  z-index: 200;
  font-weight: 600;
}

.toast.show {
  display: block;
}

.hidden {
  display: none !important;
}

/* ========================================================================== */
/* Экран входа и одноразовой привязки смартфона */
.login-screen {
  min-height: 100vh;
  background: linear-gradient(160deg, #0f172a 0%, #1e293b 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.login-card {
  background: #ffffff;
  border-radius: 14px;
  max-width: 420px;
  width: 100%;
  padding: 22px 20px;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.35);
}

.login-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
  gap: 4px;
  margin-bottom: 14px;
}

.login-tab-btn {
  border: none;
  background: transparent;
  padding: 8px 6px;
  border-radius: 7px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
}

.login-tab-btn.active {
  background: var(--primary);
  color: #fff;
}

/* Вспомогательные сетки, баннер GPS и мобильная версия (≤ 768 px)            */
/* ========================================================================== */
.split-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 16px;
}

.search-input {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 270px;
  max-width: 100%;
}

.search-input-sm {
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 180px;
  max-width: 100%;
}

.brig-meta-controls {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

.brig-footer-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.emp-check-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.geo-banner {
  margin-bottom: 12px;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 12.5px;
  line-height: 1.4;
}

.geo-banner.secure {
  background: var(--green-soft);
  color: #14532d;
  border: 1px solid #bbf7d0;
}

.geo-banner.lan-http {
  background: var(--amber-soft);
  color: #78350f;
  border: 1px solid #fde68a;
}

.edit-output-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.mobile-bottom-nav {
  display: none;
}

@media (max-width: 768px) {
  html,
  body {
    overflow-x: hidden;
  }

  .topbar {
    padding: 10px 12px;
    gap: 8px;
  }

  .brand-title {
    font-size: 14px;
    line-height: 1.2;
  }

  .brand-sub {
    display: none;
  }

  .topbar .role-tabs {
    display: none;
  }

  .period-picker {
    padding: 3px 6px;
    gap: 4px;
  }

  .period-picker span {
    display: none;
  }

  .period-picker select,
  .period-picker input {
    font-size: 14px !important;
    padding: 4px 6px;
  }

  .container {
    padding: 12px 10px calc(84px + env(safe-area-inset-bottom));
  }

  .subnav {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
    margin-bottom: 12px;
    scrollbar-width: none;
  }

  .subnav::-webkit-scrollbar {
    display: none;
  }

  .subnav-btn {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 8px 12px;
    font-size: 13px;
  }

  .card {
    padding: 12px;
    margin-bottom: 12px;
  }

  .card-title {
    font-size: 15px;
  }

  .kpi-strip {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .kpi-box {
    padding: 10px;
  }

  .kpi-val {
    font-size: 16px;
  }

  .split-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .search-input,
  .search-input-sm {
    width: 100%;
  }

  /* Защита от авто-зума iOS Safari при фокусе на полях */
  .field input,
  .field select,
  .field textarea,
  .search-input,
  .search-input-sm,
  #repShiftDate,
  #repBrigadierSelect,
  #truancyCheckDate,
  #timesheetGroupFilter {
    font-size: 16px;
    min-height: 42px;
  }

  /* Крупные кнопки ПРИХОД / УХОД под большой палец */
  .emp-check-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    width: 100%;
  }

  .btn-touch {
    justify-content: center;
    min-height: 48px;
    font-size: 14px;
    padding: 10px 8px;
    text-align: center;
  }

  .btn-full-mobile {
    width: 100%;
    justify-content: center;
    min-height: 44px;
  }

  .cal-grid {
    grid-template-columns: repeat(7, minmax(0, 1fr));
    gap: 4px;
    max-width: 100%;
  }

  .cal-day {
    padding: 9px 2px;
    font-size: 13px;
  }

  /* Табель: компактный закреплённый столбец ФИО и плавный тач-скролл */
  .table-wrap {
    -webkit-overflow-scrolling: touch;
  }

  .sticky-col {
    max-width: 122px;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 12px;
    padding: 6px 6px;
  }

  /* Превращение 19-колоночной таблицы ввода бригадира в мобильные карточки */
  .brig-meta-controls {
    display: grid;
    grid-template-columns: auto 1fr;
    width: 100%;
    gap: 8px;
  }

  .brig-entry-wrap {
    border: none;
    overflow: visible;
  }

  table.brig-entry-table,
  table.brig-entry-table tbody {
    display: block;
    width: 100%;
  }

  table.brig-entry-table thead {
    display: none;
  }

  table.brig-entry-table tbody tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 10px;
    padding: 12px;
    margin-bottom: 12px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: 10px;
  }

  table.brig-entry-table tbody tr:hover td {
    background: transparent;
  }

  table.brig-entry-table tbody td {
    display: flex;
    flex-direction: column;
    padding: 0;
    border: none;
    text-align: left !important;
    white-space: normal;
    min-width: 0;
  }

  table.brig-entry-table tbody td::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 3px;
  }

  table.brig-entry-table tbody td.mob-span-2 {
    grid-column: 1 / -1;
  }

  table.brig-entry-table tbody td.mob-stat-box {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 6px 8px;
  }

  table.brig-entry-table .report-input,
  table.brig-entry-table .report-input.wide {
    width: 100% !important;
    font-size: 16px;
    min-height: 40px;
    padding: 7px 9px;
    background: #fff;
  }

  .brig-footer-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .brig-footer-actions .btn {
    width: 100%;
    justify-content: center;
    min-height: 46px;
  }

  /* Модальные окна как нижняя шторка (bottom-sheet) */
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal,
  .modal.wide {
    max-width: 100%;
    max-height: 88vh;
    border-radius: 16px 16px 0 0;
    padding: 18px 14px calc(18px + env(safe-area-inset-bottom));
  }

  /* Нижняя панель ролей на смартфоне */
  .mobile-bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 60;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    background: #0f172a;
    border-top: 1px solid #1e293b;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    gap: 4px;
  }

  .mob-role-btn {
    border: none;
    background: transparent;
    color: #94a3b8;
    border-radius: 8px;
    padding: 6px 4px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    font-size: 11.5px;
    font-weight: 600;
    cursor: pointer;
  }

  .mob-role-btn .mob-icon {
    font-size: 17px;
    line-height: 1.1;
  }

  .mob-role-btn.active {
    background: #1e293b;
    color: #fff;
  }

  .toast {
    left: 12px;
    right: 12px;
    bottom: calc(70px + env(safe-area-inset-bottom));
    text-align: center;
  }
}

@media (max-width: 520px) {
  .edit-output-grid {
    grid-template-columns: 1fr;
  }

  .emp-check-actions {
    grid-template-columns: 1fr;
  }
}

/* ========================================================================== */
/* Итоговые строки графика смен и печать на А4 (альбомная ориентация)         */
/* ========================================================================== */
.sched-group-row td {
  background: #f1f5f9 !important;
  font-weight: 700;
  font-size: 12px;
  color: #334155;
  padding: 5px 10px;
}

tfoot tr.sched-summary-main td {
  background: #eff6ff;
  color: #1e3a8a;
  font-weight: 800;
  border-top: 2px solid #93c5fd;
  text-align: center;
}

tfoot tr.sched-summary-main td.sticky-col {
  background: #eff6ff;
  text-align: left;
}

tfoot tr.sched-summary-sub td {
  background: #f8fafc;
  color: #475569;
  font-weight: 700;
  text-align: center;
}

tfoot tr.sched-summary-sub td.sticky-col {
  background: #f8fafc;
  text-align: left;
}

tfoot tr.sched-summary-total td {
  background: #0f172a;
  color: #ffffff;
  font-weight: 800;
  text-align: center;
}

tfoot tr.sched-summary-total td.sticky-col {
  background: #0f172a;
  color: #ffffff;
  text-align: left;
}

.print-only {
  display: none;
}

@media print {
  @page {
    size: A4 landscape;
    margin: 7mm 8mm;
  }

  html,
  body {
    background: #ffffff !important;
    color: #000000 !important;
    margin: 0 !important;
    padding: 0 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  #view-login,
  #appTopbar,
  #appMain,
  #mobileBottomNav,
  #modalBackdrop,
  #toast {
    display: none !important;
  }

  #printScheduleSheet.print-only {
    display: block !important;
    width: 100%;
  }

  .print-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    border-bottom: 2px solid #0f172a;
    padding-bottom: 5px;
    margin-bottom: 8px;
  }

  .print-title {
    font-size: 14pt;
    font-weight: 800;
    letter-spacing: 0.3px;
  }

  .print-meta {
    font-size: 8.5pt;
    color: #334155;
    text-align: right;
  }

  table.print-sched-table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
    font-size: 7.5pt;
  }

  table.print-sched-table th,
  table.print-sched-table td {
    border: 1px solid #334155;
    padding: 3px 1px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    position: static !important;
  }

  table.print-sched-table th.p-name,
  table.print-sched-table td.p-name {
    width: 48mm;
    text-align: left;
    padding: 3px 4px;
    font-weight: 600;
  }

  table.print-sched-table tr.p-total-shop td.p-name,
  table.print-sched-table tr.p-total-sub td.p-name,
  table.print-sched-table tr.p-total-all td.p-name {
    font-size: 7pt;
    letter-spacing: -0.1px;
  }

  table.print-sched-table tr.p-total-sub td {
    background: #f8fafc !important;
    font-weight: 700;
  }

  table.print-sched-table th.p-sum,
  table.print-sched-table td.p-sum {
    width: 12mm;
    font-weight: 700;
  }

  table.print-sched-table .p-wknd {
    background: #e2e8f0 !important;
  }

  table.print-sched-table .p-work {
    background: #dcfce7 !important;
    font-weight: 700;
  }

  table.print-sched-table .p-brig {
    background: #fde68a !important;
    font-weight: 800;
  }

  table.print-sched-table tr.p-section td {
    background: #cbd5e1 !important;
    text-align: left;
    padding: 3px 6px;
    font-weight: 800;
    font-size: 8pt;
  }

  table.print-sched-table tr.p-total-shop td {
    background: #dbeafe !important;
    font-weight: 800;
    border-top: 2px solid #0f172a;
  }

  table.print-sched-table tr.p-total-all td {
    background: #0f172a !important;
    color: #ffffff !important;
    font-weight: 800;
  }

  .print-footer {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    font-size: 9pt;
  }
}
