:root {
  --bg: #f4f7fb;
  --card: #ffffff;
  --text: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --primary: #2563eb;
  --primary-soft: #eaf1ff;
  --ok: #16a34a;
  --ok-soft: #e8f7ee;
  --bad: #dc2626;
  --bad-soft: #fdecec;
  --warn: #d97706;
  --warn-soft: #fff4e0;
  --radius: 12px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; color: var(--text); background: var(--bg); display: flex; min-height: 100vh; }
a { color: var(--primary); text-decoration: none; }
h1 { font-size: 24px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Боковое меню */
.sidebar { width: 230px; background: var(--card); border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh; flex-shrink: 0; }
.logo { font-weight: 700; font-size: 20px; padding: 0 10px 18px; color: var(--primary); }
.logo span { color: var(--ok); }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--text); }
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sidebar .btn { margin-top: 16px; }

main { flex: 1; padding: 28px 32px 60px; max-width: 1200px; min-width: 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: 1fr 1fr; }
.g3 { grid-template-columns: repeat(3, 1fr); }
.g4 { grid-template-columns: repeat(4, 1fr); }
.stat .num { font-size: 26px; font-weight: 700; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; }
.btn:hover { border-color: #cbd5e1; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.sm { padding: 4px 10px; font-size: 13px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.info { background: var(--primary-soft); color: var(--primary); }
.badge.gray { background: #f1f5f9; color: var(--muted); }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr.click { cursor: pointer; }
tr.click:hover td { background: #f8fafc; }
.val-bad { color: var(--bad); font-weight: 700; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 5px 12px; border-radius: 99px; border: 1px solid var(--line); background: var(--card); cursor: pointer; font-size: 13px; }
.chip.active { background: var(--primary); border-color: var(--primary); color: #fff; }

input, select, textarea { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--text); width: 100%; }
textarea { min-height: 70px; resize: vertical; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.search { max-width: 360px; }

.list-item { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-item:last-child { border-bottom: none; }
.doc-icon { width: 38px; height: 46px; border-radius: 6px; background: var(--primary-soft); color: var(--primary); display: flex; align-items: center; justify-content: center; font-size: 18px; flex-shrink: 0; }
.year-sep { margin: 22px 0 8px; font-weight: 700; color: var(--muted); }

/* Бумажный документ */
.paper { background: #fff; border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 4px 18px rgba(15,23,42,.08); padding: 26px 22px; font-size: 13px; line-height: 1.5; min-height: 420px; color: #334155; }
.md-line { white-space: pre-wrap; }
.md-h { font-weight: 700; margin: 12px 0 4px; }
.md-page { margin: 18px 0 10px; padding-top: 8px; border-top: 1px dashed var(--line); font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.md-page:first-child { margin-top: 0; border-top: 0; padding-top: 0; }
.md-table { overflow-x: auto; margin: 6px 0 10px; }
.md-table table { border-collapse: collapse; font-size: 12.5px; }
.md-table th, .md-table td { border: 1px solid var(--line); padding: 4px 8px; text-align: left; vertical-align: top; }
.md-table th { background: #f8fafc; font-weight: 600; }
.pages { display: flex; gap: 6px; margin-top: 10px; }
.pages span { width: 34px; height: 44px; border: 1px solid var(--line); border-radius: 4px; background: #fff; font-size: 11px; display: flex; align-items: flex-end; justify-content: center; }
.pages span.active { border-color: var(--primary); }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; }
.kv div:nth-child(odd) { color: var(--muted); }

/* Хронология */
.tl { position: relative; padding-left: 26px; }
.tl::before { content: ""; position: absolute; left: 7px; top: 4px; bottom: 4px; width: 2px; background: var(--line); }
.tl-item { position: relative; margin-bottom: 14px; }
.tl-dot { position: absolute; left: -25px; top: 16px; width: 14px; height: 14px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 1px var(--line); }
.tl-item .card { padding: 12px 16px; }

/* Модальное окно */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.45); display: flex; align-items: flex-start; justify-content: center; padding: 50px 16px; overflow-y: auto; z-index: 10; }
.modal.hidden { display: none; }
.modal-box { background: var(--card); border-radius: 14px; padding: 24px; width: 100%; max-width: 620px; }
.modal-box.wide { max-width: 1000px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.drop { border: 2px dashed #cbd5e1; border-radius: 12px; padding: 30px; text-align: center; color: var(--muted); cursor: pointer; display: block; }
.drop:hover { border-color: var(--primary); background: var(--primary-soft); }
.drop input { display: none; }
.steps div { padding: 8px 0; display: flex; gap: 10px; align-items: center; }
.spin { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.extract td { padding: 6px; }
.extract input, .extract select { padding: 5px 7px; }
.row-off td { opacity: .4; }
.note { background: var(--warn-soft); color: #92400e; border-radius: 8px; padding: 10px 12px; font-size: 13px; }

.empty { color: var(--muted); padding: 16px 0; }

/* AI ассистент */
.ai-fab { position: fixed; bottom: 22px; right: 22px; z-index: 5; background: linear-gradient(135deg, #2563eb, #7c3aed); color: #fff; border: none; padding: 12px 20px; border-radius: 99px; font: inherit; font-weight: 600; cursor: pointer; box-shadow: 0 6px 20px rgba(37,99,235,.35); }
body.on-ai .ai-fab, body.ai-open .ai-fab { display: none; }
.ai-panel { position: fixed; top: 0; right: 0; bottom: 0; width: 420px; max-width: 100%; background: var(--card); border-left: 1px solid var(--line); box-shadow: -8px 0 30px rgba(15,23,42,.08); display: flex; flex-direction: column; z-index: 8; }
.ai-panel.hidden { display: none; }
.ai-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.ai-head b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-head .btns { display: flex; gap: 6px; flex-shrink: 0; }
.ai-ctx { padding: 6px 16px; font-size: 12px; color: var(--primary); background: var(--primary-soft); }
.ai-msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }
.msg.user { align-self: flex-end; max-width: 85%; text-align: right; }
.msg.user .bubble { background: var(--primary); color: #fff; padding: 9px 13px; border-radius: 14px 14px 4px 14px; text-align: left; display: inline-block; }
.msg-ctx { font-size: 11px; color: var(--muted); margin-top: 3px; }
.msg.ai { max-width: 95%; }
.steps-ai { font-size: 12px; color: var(--muted); background: #f8fafc; border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px; margin-bottom: 8px; }
.step { display: flex; gap: 6px; align-items: center; }
.step .ok { color: var(--ok); }
.spin.sm { width: 11px; height: 11px; border-width: 2px; display: inline-block; }
.answer { background: #f1f5f9; padding: 10px 13px; border-radius: 14px 14px 14px 4px; }
.msg.ai .chips { margin-top: 8px; }
.disclaimer { font-size: 11px; color: var(--muted); margin-top: 4px; }
.ai-action { border: 1px solid var(--primary); background: var(--primary-soft); border-radius: 10px; padding: 10px 12px; margin-top: 8px; }
.ai-input { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); align-items: flex-end; }
.ai-input textarea { min-height: 42px; max-height: 120px; resize: none; }
.ai-sugg { display: flex; flex-direction: column; gap: 8px; margin: auto 0; }
.ai-sugg .chip { text-align: left; border-radius: 10px; padding: 8px 12px; }
.ai-page { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 16px; height: calc(100vh - 150px); min-height: 480px; }
.ai-list-card { overflow-y: auto; padding: 12px; }
.chat-item { padding: 9px 10px; border-radius: 8px; cursor: pointer; margin-top: 4px; }
.chat-item:hover { background: var(--bg); }
.chat-item.active { background: var(--primary-soft); }
.chat-item b { display: block; font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-chat-card { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.ai-card { background: linear-gradient(135deg, #f5f8ff, #faf5ff); border-color: #dfe6fb; }
@media (min-width: 1150px) { body.ai-open main { margin-right: 420px; } }

/* План профилактики */
.plan-item { flex-wrap: wrap; }
.plan-item .badge { margin-right: 4px; }
.month { padding: 14px; }
.month h3 { margin-bottom: 8px; }
.month .small { padding: 3px 0; }


/* Телефон: элементы, которые на компьютере скрыты */
.tabbar, .more, .mobile-only { display: none; }

@media (max-width: 900px) {
  body { flex-direction: column; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .sidebar, .ai-fab { display: none; }
  main { padding: 16px 16px 24px; }
  h1 { font-size: 21px; }
  .card { padding: 14px; }
  .g2, .g3, .split { grid-template-columns: minmax(0, 1fr) !important; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .grid > * { min-width: 0; }
  .search { max-width: none; }
  /* iPhone увеличивает страницу при фокусе на поле с шрифтом меньше 16px */
  input, select, textarea { font-size: 16px; }

  /* Нижняя панель */
  .tabbar { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 9; background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  .tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 7px 0 6px; font-size: 20px; color: var(--muted); }
  .tabbar a span { font-size: 11px; }
  .tabbar a.active { color: var(--primary); font-weight: 600; }
  .more { display: flex; position: fixed; inset: 0; z-index: 8; background: rgba(15,23,42,.45); align-items: flex-end; }
  .more.hidden { display: none; }
  .more-box { width: 100%; background: var(--card); border-radius: 16px 16px 0 0; padding: 14px 14px calc(78px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 4px; }
  .more-nav a { display: block; padding: 12px; border-radius: 8px; color: var(--text); font-size: 16px; }
  .more-nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
  .more-box .btn { margin-top: 8px; padding: 12px; }

  /* Таблицы превращаются в карточки */
  table.cards, table.cards tbody { display: block; }
  table.cards tr:has(th) { display: none; }
  table.cards tr { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
  table.cards tr:last-child { border-bottom: none; }
  table.cards td { display: block; padding: 0; border: none; }
  table.cards td:empty { display: none; }
  table.cards .c-full { flex-basis: 100%; }
  table.cards .c-hide { display: none; }
  table.cards .c-end { margin-left: auto; }
  table.cards .c-last { order: 1; }
  tr.click:hover td { background: none; }
  /* Строка документа: бейджи переносятся под название */
  .doc-row { flex-wrap: wrap; row-gap: 6px; }
  .doc-row > div { flex: 1 1 calc(100% - 50px) !important; }
  .doc-row > div + .badge { margin-left: 50px; }

  /* Окна на весь экран */
  .modal { padding: 0; }
  .modal-box, .modal-box.wide { max-width: none; min-height: 100%; border-radius: 0; padding: 18px 16px calc(24px + env(safe-area-inset-bottom)); }
  .modal-foot { flex-wrap: wrap; }
  .modal-foot .btn { flex: 1 1 auto; }
  .mobile-only { display: flex; }
  .camera-btn { width: 100%; margin-top: 10px; padding: 12px; }
  .drop { padding: 18px; }
  .kv { grid-template-columns: 100px minmax(0, 1fr); }

  /* Ассистент */
  .ai-panel { width: 100%; border-left: none; z-index: 10; padding-bottom: env(safe-area-inset-bottom); }
  .ai-page { grid-template-columns: minmax(0, 1fr); height: auto; min-height: 0; }
  /* Ассистент на телефоне: две страницы. #/ai: список чатов, #/ai/<id> и #/ai/new: чат на весь экран */
  body.on-ai:not(.ai-chat-view) .ai-chat-card { display: none; }
  body.ai-chat-view { padding-bottom: 0; }
  body.ai-chat-view .tabbar, body.ai-chat-view .page-head, body.ai-chat-view .ai-list-card { display: none; }
  body.ai-chat-view .ai-chat-card { position: fixed; inset: 0; z-index: 9; height: 100dvh; border: none; border-radius: 0; padding-bottom: env(safe-area-inset-bottom); }
  .ai-chat-head { align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
  .ai-chat-head a { flex-shrink: 0; }
  .ai-chat-head b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
.list-item.clickable { cursor: pointer; border-radius: 6px; }
.list-item.clickable:hover { background: #f1f5f9; }
.print-only { display: none; }
@media print {
  .sidebar, .no-print, .btn, .ai-fab, .ai-panel, .ai-card { display: none !important; }
  .print-only { display: block; }
  body { background: #fff; }
  main { padding: 0; }
  .g2 { grid-template-columns: 1fr; }
  .card { border: none; padding: 8px 0; box-shadow: none; break-inside: avoid; }
  a { color: inherit; text-decoration: none; }
}

/* Сканы и загрузка */
.btn:disabled { opacity: .5; cursor: default; }
.note.info { background: var(--primary-soft); color: var(--primary); display: flex; gap: 8px; align-items: center; }
.note.ok { background: var(--ok-soft); color: #166534; }
.note.bad { background: var(--bad-soft); color: #991b1b; }
.scan { width: 100%; display: block; border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 4px 18px rgba(15,23,42,.08); background: #fff; }
.pages-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.page-thumb { width: 52px; height: 68px; object-fit: cover; border: 2px solid var(--line); border-radius: 4px; cursor: pointer; }
.page-thumb.active { border-color: var(--primary); }
.page-add { width: 52px; height: 68px; border: 2px dashed #cbd5e1; border-radius: 4px; display: flex; align-items: center; justify-content: center; cursor: pointer; color: var(--muted); font-size: 20px; }
.page-add:hover { border-color: var(--primary); color: var(--primary); }
.doc-thumb { width: 38px; height: 46px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); flex-shrink: 0; }
.up-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; margin-top: 12px; }
.up-item { display: flex; flex-direction: column; gap: 4px; }
.up-item img, .up-noprev { width: 100%; height: 120px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }
.up-noprev { display: flex; align-items: center; justify-content: center; background: #f1f5f9; color: var(--muted); font-weight: 600; }
.doc-concl { color: var(--text); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge-btn { border: 0; cursor: pointer; font-family: inherit; }
.hist-row { padding: 10px 0; border-bottom: 1px solid var(--line); }
.hist-row:last-child { border-bottom: none; }
.hist-head { display: flex; gap: 12px; align-items: center; border-radius: 6px; }
.hist-head.clickable { cursor: pointer; }
.hist-head.clickable:hover { background: #f1f5f9; }
.hist-doc { display: flex; gap: 8px; align-items: flex-start; margin: 6px 0 0 30px; padding: 6px 10px; border-left: 2px solid var(--line); }
