:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --ink: #1f2937;
  --muted: #6b7280;
  --line: #e3e8f0;
  --brand: #2563eb;
  --brand-dark: #1d4ed8;
  --project: #1d4ed8;
  --green: #059669;
  --red: #dc2626;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}
* { box-sizing: border-box; }
html, body { max-width: 100%; }
body { margin: 0; font: 14px/1.45 system-ui, "Segoe UI", Roboto, Arial, sans-serif; background: var(--bg); color: var(--ink); overflow-x: clip; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--red); min-height: 18px; margin-top: 8px; }

/* Login */
.login-screen { min-height: 100vh; display: grid; place-items: center; padding: 16px; overflow: auto; background: linear-gradient(135deg, #0f172a, var(--project)); }
.login-card { background: #fff; padding: 28px; border-radius: 16px; width: 100%; max-width: 360px; box-shadow: 0 20px 40px rgba(0,0,0,.2); display: flex; flex-direction: column; gap: 12px; }
.login-card .console-link { text-align: center; font-size: 12px; margin-top: 4px; }
.login-card h1 { font-size: 20px; margin: 0; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }

/* Акты: детальный просмотр (фото + строки) */
.act-detail-grid { display: grid; grid-template-columns: minmax(280px, 420px) 1fr; gap: 16px; margin-top: 12px; align-items: start; }
.act-detail-grid h3 { margin: 0 0 8px; font-size: 14px; }
.act-photo { position: sticky; top: 74px; }
.act-photo img { width: 100%; border: 1px solid var(--line); border-radius: 10px; display: block; }
@media (max-width: 900px) { .act-detail-grid { grid-template-columns: 1fr; } .act-photo { position: static; } }

/* Header — окрашен в цвет проекта (--project). Текст светлый. */
header { display: flex; align-items: center; gap: 20px; padding: 8px 20px; min-height: 58px; background: var(--project); border-bottom: 1px solid rgba(0,0,0,.12); position: sticky; top: 0; z-index: 10; color: #fff; flex-wrap: wrap; }
.brand { font-weight: 700; color: #fff; }
/* nav: при нехватке ширины переносится на отдельную строку (flex-basis), поэтому
   вкладки никогда не наезжают на блок пользователя. */
nav { display: flex; gap: 4px; flex: 1 1 640px; min-width: 0; flex-wrap: wrap; }
.tab { background: none; border: none; padding: 8px 14px; border-radius: 8px; cursor: pointer; color: rgba(255,255,255,.88); font-size: 14px; white-space: nowrap; }
.tab:hover { background: rgba(255,255,255,.14); color: #fff; }
.tab.active { background: rgba(255,255,255,.24); color: #fff; font-weight: 600; }
.user-box { display: flex; align-items: center; gap: 12px; margin-left: auto; }
header .muted { color: rgba(255,255,255,.88); }
/* Кнопки в шапке — контрастные на цветной шапке проекта. */
header #logout, header #change-password { background: #fff; color: var(--project); border: none; padding: 7px 14px; border-radius: 8px; font-weight: 600; cursor: pointer; }
header #logout:hover, header #change-password:hover { background: rgba(255,255,255,.86); }
.menu-toggle { display: none; align-items: center; gap: 6px; margin-left: auto; background: rgba(255,255,255,.14); border: 1px solid rgba(255,255,255,.4); border-radius: 8px; padding: 7px 12px; font-size: 13px; font-weight: 600; color: #fff; cursor: pointer; }
.menu-toggle:hover { background: rgba(255,255,255,.26); }

main.tab-panel { padding: 18px; display: flex; flex-direction: column; gap: 16px; max-width: 1500px; margin: 0 auto; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card h2 { font-size: 16px; margin: 0; }
.card h3 { font-size: 14px; margin: 0 0 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }

/* Forms */
.grid-form { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 12px; }
.grid-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.grid-form .wide { grid-column: span 3; }
.qty-line { display: flex; align-items: center; gap: 8px; }
.qty-line input[type=number] { flex: 1; min-width: 0; }
.qty-line .dayoff { display: flex; align-items: center; gap: 4px; white-space: nowrap; font-size: 12px; color: var(--ink); }
.qty-line .dayoff input { width: auto; }
.pin-field { display: flex; align-items: center; gap: 6px; }
.pin-field > input { flex: 1; min-width: 0; }
.pin-field > .combo { flex: 1; min-width: 0; }
.pin-btn { width: 34px; min-width: 34px; padding: 6px 0; font-size: 14px; line-height: 1; background: #fff; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; opacity: .55; }
.pin-btn:hover { opacity: 1; }
.pin-btn.active { opacity: 1; background: #dbeafe; border-color: var(--brand); }
.form-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; }
input, select, button { font: inherit; }
input, select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); width: 100%; max-width: 100%; min-width: 0; }
input:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }
input.readonly { background: #f1f5f9; font-weight: 600; }
button.primary { background: var(--brand); color: #fff; border: none; padding: 9px 16px; border-radius: 8px; cursor: pointer; font-weight: 600; }
button.primary:hover { background: var(--brand-dark); }
button.link { background: none; border: none; color: var(--brand); cursor: pointer; padding: 4px; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; max-width: 100%; min-width: 0; }
.filters input, .filters select { width: auto; min-width: 130px; }

/* Поиск-комбобокс контрагентов (от 3 символов) */
.combo { position: relative; }
.combo-input { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink); }
.combo-input:focus { outline: 2px solid #bfdbfe; border-color: var(--brand); }
.combo-list { position: absolute; z-index: 40; left: 0; right: 0; top: 100%; margin-top: 2px; max-height: 260px; overflow: auto; background: #fff; border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); }
.combo-item { padding: 7px 10px; cursor: pointer; font-size: 13px; white-space: nowrap; }
.combo-item:hover, .combo-item.active { background: #eef4ff; }
.combo-empty { padding: 7px 10px; color: var(--muted); font-size: 12px; }
.filters .combo .combo-input { min-width: 220px; }

/* iOS Safari: поля даты имеют собственный внутренний размер и «уезжают» за
   край контейнера — снимаем нативную отрисовку и запрещаем расти. */
input[type="date"], input[type="time"], input[type="datetime-local"] {
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
  max-width: 100%;
}

/* Tables */
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 7px 9px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
th { background: #f8fafc; font-size: 12px; color: var(--muted); position: sticky; top: 0; }
tbody tr:hover { background: #f8fbff; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { white-space: nowrap; }
.totals { margin-bottom: 10px; font-size: 13px; color: var(--ink); }
.totals b { color: var(--brand-dark); }
.pager { display: flex; align-items: center; gap: 16px; margin-top: 10px; justify-content: center; }

/* Dicts */
.dict-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.dict-tab { background: #fff; border: 1px solid var(--line); padding: 7px 14px; border-radius: 999px; cursor: pointer; color: var(--muted); }
.dict-tab.active { background: var(--brand); border-color: var(--brand); color: #fff; }

/* Reports */
.report-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.report-grid .wide { grid-column: 1 / -1; }

/* Акт сверки */
.act-head { margin-bottom: 12px; }
.act-head h3 { font-size: 15px; margin: 0 0 4px; }
.act-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.act-grid .card { box-shadow: none; }
.act-sum { margin-top: 8px; font-weight: 600; font-size: 13px; }
tfoot .total-row td { font-weight: 700; background: #f8fafc; }

/* Загрузка акта */
.mode-switch { display: flex; gap: 4px; background: #eef2f7; padding: 3px; border-radius: 8px; }
.mode-btn { border: none; background: none; padding: 6px 12px; border-radius: 6px; cursor: pointer; color: var(--muted); font-size: 13px; }
.mode-btn.active { background: #fff; color: var(--brand-dark); font-weight: 600; box-shadow: var(--shadow); }
.upload-photo { display: flex; gap: 12px; align-items: center; margin-top: 12px; flex-wrap: wrap; }
.upload-photo input[type=file] { width: auto; }
#up-table input, #up-table select { width: 100%; min-width: 100px; }
#up-table td { padding: 4px 6px; }
#up-table td.flag { font-size: 11px; color: var(--red); white-space: normal; max-width: 220px; }
#up-table td.idx { color: var(--muted); }
#up-table td.fuzzy { background: #fff7d6; }
#up-table td.fuzzy select { border-color: #e0a800; }

/* Toast */
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 100; box-shadow: var(--shadow); }
.toast.error { background: var(--red); }
.toast.ok { background: var(--green); }

/* Токены: индикатор в шапке (на цветной шапке проекта) */
.ok { color: var(--green); font-weight: 600; }
button.primary:disabled { opacity: .55; cursor: not-allowed; }

/* Тестовый проект: форма из 10 кнопок */
.tab.test-tab { display: none; }
body.project-test .tab.tech-tab { display: none; }
body.project-test .tab.test-tab { display: inline-flex; }
.test-btn { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 18px 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer; font-size: 22px; font-weight: 700; color: var(--ink); }
.test-btn:hover:not(:disabled) { border-color: var(--brand); background: #f5f9ff; }
.test-btn small { font-size: 11px; font-weight: 400; color: var(--muted); }
.test-btn:disabled { opacity: .5; cursor: not-allowed; }

@media (max-width: 900px) { .grid-form { grid-template-columns: 1fr 1fr; } .report-grid { grid-template-columns: 1fr; } .act-grid { grid-template-columns: 1fr; } }

/* Мобильное меню: раскрывающийся вниз список вместо скрытой прокрутки */
@media (max-width: 1000px) {
  header { height: auto; flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .brand { font-size: 14px; }
  .menu-toggle { display: inline-flex; }
  nav { order: 3; flex: 0 0 100%; width: 100%; display: none; flex-direction: column; gap: 2px; border-top: 1px solid rgba(255,255,255,.28); padding-top: 8px; margin-top: 2px; }
  header.menu-open nav { display: flex; }
  nav .tab { width: 100%; text-align: left; padding: 11px 12px; font-size: 14px; border-radius: 8px; }
  .user-box { order: 4; flex: 0 0 100%; width: 100%; display: none; justify-content: space-between; border-top: 1px solid rgba(255,255,255,.28); padding-top: 10px; margin-top: 6px; font-size: 13px; }
  header.menu-open .user-box { display: flex; }
  /* iOS Safari зумит страницу при фокусе в поле со шрифтом < 16px — держим 16px */
  input, select, textarea, .combo-input { font-size: 16px; }
}

/* Телефоны и узкие экраны */
@media (max-width: 700px) {
  main.tab-panel { padding: 10px; gap: 12px; }
  .card { padding: 12px; }
  .card h2 { font-size: 15px; }
  .card-head { flex-direction: column; align-items: stretch; }
  .card, .card-head, .filters, .act-grid, .report-grid, main.tab-panel { min-width: 0; max-width: 100%; }
  .filters > * { width: 100%; max-width: 100%; min-width: 0; }
  .grid-form { grid-template-columns: 1fr; }
  .grid-form .wide { grid-column: auto; }
  .filters { flex-direction: column; align-items: stretch; }
  .filters input, .filters select, .filters .combo .combo-input, .filters .combo { width: 100%; min-width: 0; }

  /* Таблицы превращаются в карточки */
  .table-wrap { border: none; }
  table.resp-table { font-size: 13px; }
  table.resp-table thead { display: none; }
  table.resp-table tbody tr { display: block; border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; margin-bottom: 8px; background: #fff; }
  table.resp-table tbody tr:hover { background: #fff; }
  table.resp-table td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; border: none; padding: 3px 0; white-space: normal; overflow-wrap: anywhere; text-align: right; }
  table.resp-table td::before { content: attr(data-label); color: var(--muted); font-weight: 600; text-align: left; flex: 0 0 42%; }
  table.resp-table td:empty { display: none; }
  table.resp-table td.actions { justify-content: flex-end; }
  table.resp-table td.actions::before { content: ''; flex: 0 0 0; }
  table.resp-table tfoot tr { display: block; border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; background: #f8fafc; }
  table.resp-table tfoot td { display: flex; justify-content: space-between; border: none; padding: 3px 0; }
  table.resp-table tfoot td::before { content: attr(data-label); color: var(--muted); font-weight: 600; }
}

/* Телефон в горизонтальной ориентации — компактнее по высоте */
@media (orientation: landscape) and (max-height: 500px) {
  header { padding: 4px 10px; gap: 6px; }
  main.tab-panel { padding: 8px; gap: 8px; }
  .card { padding: 10px; }
  .login-screen { align-items: start; padding: 10px; }
  .login-card { padding: 14px; gap: 8px; max-height: calc(100vh - 20px); overflow: auto; }
.login-card h1 { font-size: 17px; }
  .login-card p { margin: 0; }
}
