/* Документы в дороге — журнал регистрации.
   Палитра: архивная бумага #f3f4f1, чернила #1c2b33,
   штемпельная синь #2b3fa6 (входящие), фиолетовый штемпель #6d28d9 (исходящие),
   сургуч #b42318 (ошибки/удаление). */
:root {
  --paper: #f3f4f1;
  --card: #ffffff;
  --ink: #1c2b33;
  --ink-soft: #5b6b73;
  --line: #d9dcd6;
  --stamp-in: #2b3fa6;
  --stamp-out: #6d28d9;
  --wax: #b42318;
  --ok: #1a7f4e;
  --radius: 10px;
  --font: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "PT Mono", ui-monospace, "SF Mono", Consolas, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  background: var(--paper);
  color: var(--ink);
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; font-size: inherit; cursor: pointer; touch-action: manipulation; }
input, select, textarea {
  font-family: inherit; font-size: 16px; color: var(--ink);
  width: 100%; padding: 11px 12px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: #fff;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--stamp-in); outline-offset: 1px;
}
label { display: block; font-size: 13px; color: var(--ink-soft); margin: 12px 0 5px; }

/* ---- каркас ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--ink); color: #fff;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
  display: flex; align-items: center; gap: 10px;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 600; flex: 1; letter-spacing: .2px; }
.topbar button {
  background: none; border: none; color: #fff; font-size: 22px; padding: 4px 6px;
}
.searchrow { padding: 10px 14px 0; }
.searchrow input { border-radius: 999px; padding-left: 16px; }
.tabs { display: flex; gap: 6px; padding: 8px 10px; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  white-space: nowrap; flex: 0 0 auto; padding: 6px 12px; border-radius: 14px;
  font-size: 12.5px; line-height: 1.1; min-width: 62px;
}
.tab-ic { font-size: 18px; line-height: 1; }
.tabs button {
  border: 1px solid var(--line); background: #fff; color: var(--ink-soft);
  font-weight: 600;
}
.tabs button.active-in { background: var(--stamp-in); border-color: var(--stamp-in); color: #fff; }
.tabs button.active-out { background: var(--stamp-out); border-color: var(--stamp-out); color: #fff; }
.tabs button.active-svc { background: var(--ok); border-color: var(--ok); color: #fff; }
.svc-banner {
  background: #fdf1e3; border: 1px solid #f3ddc0; color: #92400e;
  border-radius: var(--radius); padding: 10px 12px; margin: 6px 0 12px;
  font-size: 14px; line-height: 1.45;
}
.svc-banner.hot { background: #fdecea; border-color: #f0c9c4; color: var(--wax); }

.list { padding: 4px 14px 110px; }
.empty { text-align: center; color: var(--ink-soft); padding: 60px 20px; line-height: 1.5; }

/* ---- карточка документа: штемпель ---- */
.doc {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px; margin-bottom: 10px;
  display: block; width: 100%; text-align: left;
}
.doc:active { background: #fafaf8; }
.stamp {
  display: inline-block; font-family: var(--mono); font-size: 13px; font-weight: 700;
  padding: 3px 9px; border-radius: 4px;
  border: 2px solid currentColor; box-shadow: inset 0 0 0 1.5px #fff, inset 0 0 0 2.5px currentColor;
  transform: rotate(-1.2deg); letter-spacing: .4px;
  margin-bottom: 7px;
}
.stamp.in { color: var(--stamp-in); }
.stamp.out { color: var(--stamp-out); }
.doc .title { font-weight: 600; line-height: 1.35; }
.doc .meta { font-size: 13px; color: var(--ink-soft); margin-top: 5px; line-height: 1.5; }
.badge {
  display: inline-block; font-size: 11.5px; padding: 2px 8px; border-radius: 999px;
  background: #eceee9; color: var(--ink-soft); margin-left: 6px; vertical-align: 1px;
}
.badge.wait { background: #fdf1e3; color: #92400e; }
.badge.done { background: #e6f4ec; color: var(--ok); }
.badge.err  { background: #fdecea; color: var(--wax); }

/* ---- плавающая кнопка ---- */
.fab {
  position: fixed; right: 18px; bottom: calc(env(safe-area-inset-bottom) + 20px);
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--ink); color: #fff; font-size: 30px; line-height: 0;
  border: none; box-shadow: 0 6px 18px rgba(28,43,51,.35); z-index: 25;
}

/* ---- шторки ---- */
.sheet-back {
  position: fixed; inset: 0; background: rgba(28,43,51,.45); z-index: 40;
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--paper); width: 100%; max-width: 560px;
  max-height: 94vh; overflow-y: auto;
  border-radius: 16px 16px 0 0;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 22px);
}
.sheet h2 { font-size: 18px; margin: 2px 0 4px; }
.sheet .sub { color: var(--ink-soft); font-size: 13px; margin: 0 0 8px; }
/* grabber styled below with tappable area */

.seg { display: flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { flex: 1; padding: 11px 0; border: none; background: #fff; color: var(--ink-soft); font-weight: 600; }
.seg button.on-in  { background: var(--stamp-in); color: #fff; }
.seg button.on-out { background: var(--stamp-out); color: #fff; }

.btn {
  display: block; width: 100%; padding: 13px; margin-top: 14px;
  border: none; border-radius: var(--radius);
  background: var(--ink); color: #fff; font-weight: 600;
}
.btn.secondary { background: #fff; color: var(--ink); border: 1px solid var(--line); }
.btn.danger { background: #fff; color: var(--wax); border: 1px solid #f0c9c4; }
.btn:disabled { opacity: .5; }
.row2 { display: flex; gap: 10px; }
.row2 > * { flex: 1; }

.filechip {
  display: flex; align-items: center; gap: 8px;
  background: #fff; border: 1px dashed var(--line); border-radius: var(--radius);
  padding: 10px 12px; margin-top: 6px; font-size: 14px; color: var(--ink-soft);
}
.suggest {
  background: #eef1ff; border: 1px solid #c9d1f5; border-radius: var(--radius);
  padding: 10px 12px; margin-top: 12px; font-size: 14px; line-height: 1.45;
}
.suggest button {
  margin-top: 8px; background: var(--stamp-in); color: #fff; border: none;
  border-radius: 8px; padding: 7px 14px; font-weight: 600;
}

/* ---- цепочка ---- */
.chain { margin-top: 14px; border-left: 2px solid var(--line); padding-left: 12px; }
.chain .item { padding: 7px 0; }
.chain .item.current { font-weight: 700; }
.chain .n { font-family: var(--mono); font-size: 13px; }
.chain .in  .n { color: var(--stamp-in); }
.chain .out .n { color: var(--stamp-out); }
.chain .t { font-size: 13.5px; color: var(--ink-soft); }

/* ---- настройки ---- */
.settings-item {
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 11px 12px; margin-bottom: 8px;
  display: flex; align-items: center; gap: 10px;
}
.settings-item .grow { flex: 1; min-width: 0; }
.settings-item .nm { font-weight: 600; }
.settings-item .sc { font-size: 12.5px; color: var(--ink-soft); }
.iconbtn { background: none; border: none; font-size: 18px; padding: 4px 6px; color: var(--ink-soft); }
.defmark { font-size: 11px; color: var(--ok); font-weight: 700; }

/* ---- вход ---- */
.login {
  max-width: 380px; margin: 16vh auto 0; padding: 0 22px; text-align: center;
}
.login .logo {
  font-family: var(--mono); font-weight: 700; font-size: 20px;
  color: var(--stamp-in);
  border: 2.5px solid var(--stamp-in); border-radius: 6px;
  display: inline-block; padding: 8px 18px; transform: rotate(-2deg);
  margin-bottom: 20px;
}
.error { color: var(--wax); font-size: 14px; margin-top: 10px; min-height: 18px; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 96px);
  background: var(--ink); color: #fff; padding: 10px 18px;
  border-radius: 999px; font-size: 14px; z-index: 60;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
}
@media (min-width: 700px) {
  .list, .searchrow, .tabs { max-width: 640px; margin-left: auto; margin-right: auto; }
  .sheet { border-radius: 16px; margin-bottom: 4vh; }
  .sheet-back { align-items: center; }
}
@media (prefers-reduced-motion: no-preference) {
  .sheet { animation: up .18s ease-out; }
  @keyframes up { from { transform: translateY(24px); opacity: .6; } }
}

/* ---- проекты и чеки (этап 4) ---- */
.tabs button.active-proj { background: #b45309; border-color: #b45309; color: #fff; }
table.items {
  width: 100%; border-collapse: collapse; margin-top: 6px; font-size: 14px;
  background: #fff; border-radius: var(--radius); overflow: hidden;
}
table.items th, table.items td {
  padding: 8px 9px; border-bottom: 1px solid var(--line); text-align: left;
}
table.items th { background: #f6f7f4; font-size: 12px; color: var(--ink-soft); font-weight: 600; }
table.items td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.items tr:last-child td { border-bottom: none; }
table.items .iconbtn { padding: 0 2px; color: var(--wax); }


/* ---- этап 5: зарплата, ИИ, сортировка ---- */
.tabs button.active-sal { background: #7c3aed; border-color: #7c3aed; color: #fff; }
.monthbar {
  display: flex; align-items: center; justify-content: center; gap: 16px;
  padding: 6px 0 12px; font-weight: 600; font-size: 16px;
}
.monthbar .iconbtn { font-size: 26px; color: var(--ink); }
.sortbar { font-size: 13px; color: var(--ink-soft); margin: 4px 0 12px;
  display: flex; align-items: center; gap: 8px; }
.sortbar select { width: auto; padding: 6px 10px; }
.ai-box { background: #f0eefc; border: 1px solid #d6cef5; border-radius: var(--radius);
  padding: 12px; margin-top: 14px; }
.ai-row { display: flex; gap: 10px; }
.ai-row .btn { flex: 1; }
.ai-result { background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; margin-top: 10px; font-size: 14px; line-height: 1.5; white-space: normal; }
.ai-opt { display: flex; align-items: center; gap: 8px; background: #fff;
  border: 1px solid var(--line); border-radius: var(--radius); padding: 11px 12px;
  margin-bottom: 8px; font-weight: 500; cursor: pointer; }
.ai-opt.on { border-color: #7c3aed; background: #f0eefc; }
.ai-opt.off { opacity: .6; }
.ai-opt input { width: auto; }

/* ---- предпросмотр PDF/фото в карточке ---- */
.pdf-frame {
  width: 100%; height: 60vh; border: 1px solid var(--line);
  border-radius: var(--radius); margin-top: 8px; background: #fff;
}
img.pdf-frame { height: auto; object-fit: contain; }

/* ---- превью страниц (эскизы) ---- */
.thumbs {
  display: flex; flex-wrap: wrap; gap: 10px; margin-top: 4px;
}
.thumb {
  position: relative; width: 92px; height: 120px;
  border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-pdf {
  width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--wax); background: #fdf0ee; font-size: 15px;
}
.thumb-num {
  position: absolute; top: 3px; left: 3px; z-index: 2;
  background: var(--ink); color: #fff; font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 5px;
  display: flex; align-items: center; justify-content: center;
}
.thumb-size {
  position: absolute; bottom: 0; left: 0; right: 0;
  background: rgba(28,43,51,.72); color: #fff; font-size: 10.5px;
  text-align: center; padding: 2px 0;
}
.thumb-x {
  position: absolute; top: 3px; right: 3px; z-index: 2;
  width: 22px; height: 22px; border-radius: 11px; border: none;
  background: rgba(180,35,24,.92); color: #fff; font-size: 13px;
  display: flex; align-items: center; justify-content: center; padding: 0;
}


/* ---- закрытие карточки ---- */
.sheet { position: relative; }
.sheet-close {
  position: absolute; top: 10px; right: 12px; z-index: 5;
  width: 34px; height: 34px; border-radius: 17px; border: none;
  background: #eceee9; color: var(--ink); font-size: 17px; line-height: 1;
  display: flex; align-items: center; justify-content: center; padding: 0;
}
.sheet-close:active { background: #dfe2dc; }
.grabber { display: block; cursor: pointer; padding: 6px 0; margin: -6px auto 8px; width: 60px; }
.grabber::after {
  content: ""; display: block; width: 42px; height: 4px;
  border-radius: 2px; background: var(--line); margin: 0 auto;
}

/* ---- предпросмотр страниц PDF ---- */
.pdf-page {
  width: 100%; border: 1px solid var(--line); border-radius: var(--radius);
  margin-top: 8px; background: #fff; display: block;
}
.pdf-nav {
  display: flex; align-items: center; justify-content: center; gap: 16px;
  padding: 8px 0; font-size: 14px; font-weight: 600;
}
.pdf-nav .iconbtn { font-size: 24px; color: var(--ink); }

/* ---- тёмная тема ---- */
[data-theme="dark"] {
  --paper: #1a1f24;
  --card: #222830;
  --ink: #e8ecef;
  --ink-soft: #9aa3ac;
  --line: #333a44;
  --stamp-in: #5b8bf7;
  --stamp-out: #9b78f5;
  --wax: #f06050;
  --ok: #3cb878;
}
  [data-theme="dark"] body { background: var(--paper); color: var(--ink); }
[data-theme="dark"] input,
[data-theme="dark"] select,
[data-theme="dark"] textarea { background: #2a303a; color: var(--ink); border-color: var(--line); }
[data-theme="dark"] .doc { background: var(--card); }
[data-theme="dark"] .doc:active { background: #2a303a; }
[data-theme="dark"] .topbar { background: #14181e; }
[data-theme="dark"] .sheet { background: var(--paper); }
[data-theme="dark"] .badge { background: #2a303a; color: var(--ink-soft); }
[data-theme="dark"] .badge.wait { background: #3d2e1a; color: #f5c06b; }
[data-theme="dark"] .badge.done { background: #1a3d2a; color: var(--ok); }
[data-theme="dark"] .badge.err { background: #3d1a1a; color: var(--wax); }
[data-theme="dark"] .btn { background: var(--ink); color: var(--paper); }
[data-theme="dark"] .btn.secondary { background: var(--card); color: var(--ink); }
[data-theme="dark"] .btn.danger { background: var(--card); color: var(--wax); }
[data-theme="dark"] .seg button { background: #2a303a; }
[data-theme="dark"] .settings-item { background: var(--card); }
[data-theme="dark"] .ai-box { background: #242435; border-color: #3a3470; }
[data-theme="dark"] .ai-result { background: var(--card); }
[data-theme="dark"] .suggest { background: #242435; border-color: #3a3470; }
[data-theme="dark"] .svc-banner { background: #3d2e1a; border-color: #5c451f; color: #f5c06b; }
[data-theme="dark"] .svc-banner.hot { background: #3d1a1a; border-color: #5c2820; color: var(--wax); }
[data-theme="dark"] table.items th { background: #2a303a; }
[data-theme="dark"] table.items td { border-color: var(--line); }
[data-theme="dark"] .filechip { background: #2a303a; }
[data-theme="dark"] .sheet-close { background: #333a44; color: var(--ink); }
[data-theme="dark"] .sheet-close:active { background: #444c58; }
[data-theme="dark"] .grabber::after { background: #444c58; }
[data-theme="dark"] .pdf-frame,
[data-theme="dark"] .pdf-page { background: var(--card); }
[data-theme="dark"] .thumb { background: #2a303a; }

/* ---- touch-цели: минимум 44x44px ---- */
.iconbtn { min-width: 44px; min-height: 44px; padding: 8px; }
.thumb-x { width: 32px; height: 32px; border-radius: 16px; font-size: 15px; }
.sheet-close { width: 44px; height: 44px; border-radius: 22px; font-size: 20px; }
.chain .item { min-height: 44px; display: flex; align-items: center; flex-wrap: wrap; }

/* ---- таблица с горизонтальным скроллом ---- */
.items-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 6px; }
table.items { min-width: 340px; }

/* ---- скелетон загрузки ---- */
@keyframes skel-pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 0.8; } }
.skel {
  background: var(--line); border-radius: var(--radius);
  animation: skel-pulse 1.4s ease-in-out infinite;
}
.skel.card { height: 72px; margin-bottom: 10px; }
.skel.line { height: 16px; margin-bottom: 8px; width: 100%; }
.skel.line.short { width: 60%; }
.skel.line.h1 { height: 22px; width: 80%; }

/* ---- поиск с очисткой ---- */
.searchrow { position: relative; }
.searchrow input { padding-right: 36px; }
.searchrow .clear-search {
  position: absolute; right: 20px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: var(--ink-soft); font-size: 18px;
  width: 32px; height: 32px; border-radius: 16px; display: none;
  align-items: center; justify-content: center; padding: 0;
}
.searchrow .clear-search.visible { display: flex; }
.search-result-count {
  font-size: 13px; color: var(--ink-soft); padding: 4px 14px 0;
}

/* ---- pull-to-refresh ---- */
.ptr-indicator {
  text-align: center; color: var(--ink-soft); font-size: 13px;
  padding: 8px 0; display: none;
}
.ptr-indicator.show { display: block; }

/* ---- swipe на карточках ---- */
.doc-wrap { position: relative; overflow: hidden; border-radius: var(--radius); margin-bottom: 10px; }
.doc { margin-bottom: 0; border-radius: 0; border: none; }
.doc-swipe-actions {
  position: absolute; right: 0; top: 0; bottom: 0;
  display: flex; align-items: stretch;
  transform: translateX(100%); transition: transform .2s ease;
}
.doc-swipe-actions button {
  border: none; color: #fff; font-weight: 600; padding: 0 20px; font-size: 14px;
  min-width: 80px; white-space: nowrap;
}
.doc-swipe-actions .swipe-edit { background: var(--stamp-in); }
.doc-swipe-actions .swipe-delete { background: var(--wax); }

/* ---- swipe sheet (жест вниз для закрытия) ---- */
.sheet {
  transition: transform .25s cubic-bezier(.32,.72,0,1);
  will-change: transform;
}
.sheet.swiping { transition: none; }
.sheet-back.swiping .sheet { transition: none; }

/* ---- вложенная навигация: slide-in-right ---- */
@keyframes slideInRight { from { transform: translateX(100%); } to { transform: translateX(0); } }
.sheet.nested { animation: slideInRight .25s cubic-bezier(.32,.72,0,1); }

/* ---- контекстный FAB ---- */
.fab-label {
  position: fixed; right: 86px; bottom: calc(env(safe-area-inset-bottom) + 34px);
  background: var(--ink); color: #fff; padding: 6px 14px; border-radius: 999px;
  font-size: 13px; font-weight: 600; white-space: nowrap; z-index: 25;
  box-shadow: 0 4px 12px rgba(28,43,51,.3); opacity: 0;
  transform: translateX(10px); transition: opacity .2s, transform .2s;
  pointer-events: none;
}
.fab-label.visible { opacity: 1; transform: translateX(0); }

/* ---- multi-step form ---- */
.step-indicator {
  display: flex; gap: 6px; align-items: center; margin-bottom: 12px;
}
.step-dot {
  width: 10px; height: 10px; border-radius: 5px; background: var(--line);
  transition: background .2s;
}
.step-dot.active { background: var(--ink); }
.step-dot.done { background: var(--stamp-in); }
.step-line { flex: 1; height: 1px; background: var(--line); max-width: 30px; }

/* ---- inline prompt замена ---- */
.inline-prompt {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; margin-top: 10px;
}
.inline-prompt input { margin-top: 6px; }
.ibtn-row { display: flex; gap: 8px; margin-top: 10px; }
.ibtn-row button { flex: 1; }

/* ---- keyboard: scroll padding ---- */
.sheet { scroll-padding-bottom: 320px; }

/* ---- улучшенный AI вывод ---- */
.ai-result { white-space: pre-wrap; overflow-wrap: break-word; }
.ai-result b, .ai-result strong { color: var(--ink); }
.ai-result em { color: var(--ink-soft); }
.ai-result h3 { font-size: 15px; margin: 10px 0 4px; }
.ai-result ul { padding-left: 18px; margin: 4px 0; }
.ai-result li { margin: 2px 0; }

/* ---- скелетон в шторке ---- */
.skel.card.small { height: 44px; }
