/* ФинCRM — мобильная версия (телефоны, ширина до 760px) */
.m-tabs, .m-shade, .m-fab, .m-burger { display: none; }

@media (max-width: 760px) {
  :root { --m-bar: 58px; }
  html { -webkit-text-size-adjust: 100%; }
  body { display: block; padding-bottom: calc(var(--m-bar) + env(safe-area-inset-bottom)); overflow-x: hidden; }
  /* поля ввода 16px — иначе iPhone увеличивает страницу при фокусе */
  input, select, textarea { font-size: 16px !important; }
  .btn { min-height: 38px; }
  .btn.small { min-height: 32px; padding: 5px 10px; font-size: 13px; }
  .icon-btn { min-width: 36px; min-height: 34px; }

  /* боковое меню — выезжает слева */
  .sidebar, body.side-mini .sidebar {
    position: fixed; z-index: 60; top: 0; bottom: 0; left: 0; width: min(300px, 84vw); height: auto;
    padding: 16px 12px calc(16px + env(safe-area-inset-bottom)); overflow-y: auto; -webkit-overflow-scrolling: touch;
    transform: translateX(-102%); transition: transform .22s ease; box-shadow: 4px 0 30px rgba(0,0,0,.35);
  }
  body.m-menu .sidebar { transform: none; }
  body.side-mini .logo span, body.side-mini nav a span, body.side-mini .side-foot { display: revert; }
  body.side-mini nav a { justify-content: flex-start; padding: 10px 12px; }
  body.side-mini .nav-h { font-size: 11px; height: auto; margin: 12px 0 4px; padding: 0 12px; background: none; }
  #side-toggle { display: none; }
  .logo { padding: 2px 8px 12px; }
  nav, body.side-mini nav { display: block; overflow: visible; }
  nav a { padding: 11px 12px; font-size: 15px; white-space: normal; }
  .m-shade { display: block; position: fixed; inset: 0; z-index: 55; background: rgba(5, 8, 15, .55); opacity: 0; pointer-events: none; transition: opacity .2s; }
  body.m-menu .m-shade { opacity: 1; pointer-events: auto; }
  body.m-menu { overflow: hidden; }

  /* нижняя панель разделов */
  .m-tabs {
    display: flex; position: fixed; z-index: 50; left: 0; right: 0; bottom: 0;
    height: calc(var(--m-bar) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--sidebar); border-top: 1px solid rgba(255,255,255,.08);
  }
  .m-tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; border: 0; background: none; color: #9aa3b5; font: inherit; font-size: 11px; cursor: pointer; padding: 0; -webkit-tap-highlight-color: transparent; }
  .m-tabs button i { font-style: normal; font-size: 20px; line-height: 1; filter: grayscale(.6); opacity: .8; }
  .m-tabs button.on { color: #fff; }
  .m-tabs button.on i { filter: none; opacity: 1; }
  .m-tabs button[hidden] { display: none; }

  /* кнопка «+» */
  .m-fab { display: flex; align-items: center; justify-content: center; position: fixed; z-index: 52; right: 16px; bottom: calc(var(--m-bar) + 14px + env(safe-area-inset-bottom));
    width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: #fff; font-size: 30px; line-height: 1; box-shadow: 0 6px 20px rgba(0,0,0,.35); cursor: pointer; }
  .m-fab[hidden], body.ro .m-fab { display: none; }

  /* заголовок раздела */
  main { padding: 0 12px 16px; }
  .topbar { position: sticky; top: 0; z-index: 40; margin: 0 -12px 12px; padding: 10px 12px; padding-top: calc(10px + env(safe-area-inset-top)); background: var(--bg); border-bottom: 1px solid var(--border); flex-wrap: nowrap; gap: 8px; }
  .m-burger { display: flex; align-items: center; justify-content: center; flex: 0 0 auto; width: 38px; height: 38px; border: 1px solid var(--border); border-radius: 10px; background: var(--panel); color: var(--text); font-size: 18px; cursor: pointer; }
  .topbar h1 { font-size: 18px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar-right { flex: 0 0 auto; }
  .topbar-actions { flex-wrap: nowrap; }
  .topbar-actions .btn { padding: 6px 10px; font-size: 13px; white-space: nowrap; }

  /* общие блоки */
  .panel { padding: 12px; border-radius: 12px; }
  .cards, .dcards { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 8px !important; }
  .card { min-width: 0; }
  .card .value, .card .label, .card .sub { overflow: hidden; text-overflow: ellipsis; }
  .card, .dcards .card { padding: 10px 12px !important; }
  .dcards .value, .card .value { font-size: 18px !important; }
  .grid2, .dgrid11, .dgrid21, .dgrid111, .jgrid, .row3 { grid-template-columns: 1fr !important; }
  .row2 { grid-template-columns: 1fr 1fr; }
  .filters { gap: 6px; }
  .filters { flex-wrap: wrap; }
  .filters > select, .filters > input { flex: 1 1 140px; min-width: 0 !important; }
  .filters > .btn { flex: 0 0 auto; }
  .filters input[type=search] { flex-basis: 100%; }
  .filters .dhint { flex-basis: 100%; }
  .filters > label { flex: 0 0 auto; }
  .date-range { display: flex; gap: 6px; align-items: center; flex-basis: 100% !important; }
  .date-range input { flex: 1; min-width: 0; }
  .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; max-width: 100%; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { white-space: nowrap; }
  .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  h2 { font-size: 17px; }
  .pager { flex-wrap: wrap; justify-content: center; gap: 6px; }
  .pager .tb-long { display: none; }

  /* формы — на весь экран */
  dialog, #modal, #modal.wide { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; }
  #modal-form { padding: 14px 14px 0; min-height: 100%; display: flex; flex-direction: column; }
  #modal-form h2 { font-size: 17px; margin-bottom: 12px; padding-top: env(safe-area-inset-top); }
  #modal-body { flex: 1; }
  .modal-actions { position: sticky; bottom: 0; z-index: 3; margin: 10px -14px 0; padding: 10px 14px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--border); flex-wrap: wrap; }
  .modal-actions .btn { flex: 1 1 auto; min-height: 44px; }
  .xp { grid-template-columns: 1fr !important; min-height: 0; gap: 12px; }
  .xp-form { border-left: 0; padding-left: 0; }
  .xp-cols, .xp-cols.focus { grid-template-columns: 1fr !important; }
  .xp-groups { max-height: 150px; }
  .xp-cats { max-height: 300px; }
  .xp-cat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .xp-amount-row { grid-template-columns: minmax(0, 1fr) 96px; }
  .with-btn { flex-wrap: wrap; }

  /* вход */
  #auth { padding: 16px; align-items: flex-start; padding-top: 12vh; }
  .auth-card { width: 100%; }

  /* ===== таблицы по группам ===== */
  .sheet-layout { display: block; }
  .sheet-tabs { display: flex; flex-direction: row; gap: 6px; overflow-x: auto; height: auto !important; max-height: none; position: static; padding: 6px; margin-bottom: 10px; scrollbar-width: none; }
  .sheet-tabs::-webkit-scrollbar { display: none; }
  .sheet-tabs input { display: none; }
  .sheet-tab { flex: 0 0 auto; white-space: nowrap; padding: 7px 11px; font-size: 13px; border-radius: 99px; }
  .sheet-main .panel:last-child { min-height: 0 !important; }
  .sheet-head { gap: 6px; }
  .sheet-sum.head-sum { font-size: 13px; }
  .sheet-main .cat-cards { grid-auto-flow: column; grid-auto-columns: 46%; grid-template-columns: none !important; overflow-x: auto; max-height: none !important; min-height: 0 !important; padding: 3px; margin: -3px -3px 6px; scrollbar-width: none; }
  .sheet-main .cat-cards::-webkit-scrollbar { display: none; }
  .cards-toggle { display: none !important; }
  .sheet-toolbar { flex-wrap: wrap; gap: 6px; }
  .sheet-toolbar > * { flex: 1 1 auto; }
  .tb-period { flex: 1 1 100% !important; display: flex; gap: 4px; flex-wrap: wrap; }
  .tb-period .tabs.pmode { flex: 1 1 100%; display: flex; }
  .tb-period .tabs.pmode .tab { flex: 1; }
  .tb-picker { flex: 1 1 0 !important; width: auto !important; }
  #sh-q { flex: 1 1 100%; }
  .sheet-add-btn { display: none; }

  /* строки записей — карточками */
  table.sheet.dense, table.jtable { min-width: 0 !important; table-layout: auto !important; }
  table.sheet.dense thead, table.jtable thead { display: none; }
  table.sheet.dense tbody, table.jtable tbody, table.sheet.dense tfoot, table.jtable tfoot { display: block; }
  table.sheet.dense tr.sheet-row, table.jtable tbody tr[data-row] {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; column-gap: 10px; row-gap: 2px; align-items: baseline;
    padding: 10px 2px; border-bottom: 1px solid var(--border);
  }
  table.sheet.dense tr.sheet-row td, table.jtable tbody tr[data-row] td { display: block !important; padding: 0 !important; border: 0 !important; max-width: none !important; white-space: normal; text-align: left; }
  table.sheet.dense tr.sheet-row td:empty, table.jtable tbody tr[data-row] td:empty { display: none !important; }
  /* таблица группы: 1 дата, 2 категория, 3 для чего, 4 комментарий, 5 оплата, 6 сумма, 7 USD, 8 кнопки */
  table.sheet.dense tr.sheet-row td:nth-child(2) { grid-column: 1; grid-row: 1; font-weight: 600; font-size: 15px; }
  table.sheet.dense tr.sheet-row td:nth-child(7) { grid-column: 2; grid-row: 1; text-align: right; font-size: 15px; }
  table.sheet.dense tr.sheet-row td:nth-child(1) { grid-column: 1; grid-row: 2; color: var(--muted); font-size: 13px; }
  table.sheet.dense tr.sheet-row td:nth-child(6) { grid-column: 2; grid-row: 2; text-align: right; font-size: 12px; }
  table.sheet.dense tr.sheet-row td:nth-child(5) { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
  table.sheet.dense tr.sheet-row td:nth-child(3), table.sheet.dense tr.sheet-row td:nth-child(4) { grid-column: 1 / -1; font-size: 13px; }
  table.sheet.dense tr.sheet-row td:nth-child(8) { grid-column: 1 / -1; justify-self: end; margin-top: 4px; }
  /* журнал: 1 дата, 2 группа/категория, 3 комментарий, 4 оплата, 5 сумма, 6 USD, 7 кнопка */
  table.jtable tbody tr[data-row] td:nth-child(2) { grid-column: 1; grid-row: 1; }
  table.jtable tbody tr[data-row] td:nth-child(6) { grid-column: 2; grid-row: 1; text-align: right; }
  table.jtable tbody tr[data-row] td:nth-child(1) { grid-column: 1; grid-row: 2; color: var(--muted); font-size: 13px; }
  table.jtable tbody tr[data-row] td:nth-child(1) .tiny { display: inline; margin-left: 4px; }
  table.jtable tbody tr[data-row] td:nth-child(5) { grid-column: 2; grid-row: 2; text-align: right; font-size: 12px; }
  table.jtable tbody tr[data-row] td:nth-child(3) { grid-column: 1 / -1; font-size: 13px; }
  table.jtable tbody tr[data-row] td:nth-child(4) { grid-column: 1; font-size: 12px; color: var(--muted); }
  table.jtable tbody tr[data-row] td:nth-child(7) { grid-column: 2; justify-self: end; }
  table.jtable tbody tr.grp-row { display: flex; flex-wrap: wrap; gap: 4px 10px; padding: 10px 2px; border-bottom: 1px solid var(--border); }
  table.jtable tbody tr.grp-row td { display: block; padding: 0; border: 0; }
  table.jtable tbody tr.grp-row td:first-child { flex: 1 1 100%; }
  table.jtable tbody tr.grp-row td:nth-child(2) { flex: 1; }
  table.sheet.dense tfoot tr, table.jtable tfoot tr { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 4px 10px; padding: 10px 2px; }
  table.sheet.dense tfoot td, table.jtable tfoot td { display: block; padding: 0 !important; border: 0 !important; }
  table.sheet.dense tfoot td:empty, table.jtable tfoot td:empty { display: none; }
  table.sheet.dense tr.day-row { display: flex; padding: 8px 2px; }
  table.sheet.dense tr.day-row td { display: block; border: 0; padding: 0; }
  .jtoolbar { flex-wrap: wrap; gap: 8px; }
  .jper { margin-left: 0; width: 100%; justify-content: space-between; }
  .j-open { padding: 5px 10px; }

  /* журнал: навигатор и дерево — компактнее */
  .jlayout { display: block; }
  .jtree-wrap { max-height: 40vh; overflow-y: auto; margin-bottom: 10px; }

  /* пользователи — карточками */
  table.users-t thead { display: none; }
  table.users-t, table.users-t tbody { display: block; }
  table.users-t tr { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; padding: 10px 2px; border-bottom: 1px solid var(--border); }
  table.users-t td { display: block; padding: 0; border: 0; }
  table.users-t td[data-l]:not(:first-child)::before { content: attr(data-l) ': '; color: var(--muted); font-size: 12px; }
  table.users-t td:first-child { grid-column: 1; }
  table.users-t td.actions { grid-column: 2; grid-row: 1; }
  table.users-t td:not(:first-child):not(.actions) { grid-column: 1 / -1; font-size: 13px; }

  /* безопасность */
  .sec-row { flex-direction: column; align-items: stretch; }
  .sess { flex-direction: column; align-items: stretch; }
  .toast { bottom: calc(var(--m-bar) + 16px + env(safe-area-inset-bottom)) !important; left: 12px; right: 12px; transform: none; }
}

/* очень узкие телефоны */
@media (max-width: 370px) {
  .cards, .dcards { grid-template-columns: 1fr !important; }
  .m-tabs button span { font-size: 10px; }
}
@media print { .m-tabs, .m-fab, .m-shade, .m-burger { display: none !important; } }
@media (max-width: 760px) {
  .tb-period .tabs.pmode { order: -1; }
  .tb-period .tb-latest { flex: 0 0 auto; }
  #sh-q { flex: 1 1 60% !important; min-width: 0; }
  #sh-file { flex: 0 0 auto; }
  .sheet-toolbar .sheet-viewtabs { flex: 1 1 100%; display: flex; }
  .sheet-toolbar .sheet-viewtabs .tab { flex: 1; }
  main { padding-bottom: 90px; } /* место под кнопкой «+» */
}
@media (max-width: 760px) { table.sheet.dense tr.sheet-row td.cur-same { display: none !important; } }
@media (max-width: 760px) {
  .jtoolbar .tabs, .sheet-toolbar .tabs { min-width: 0; flex: 1 1 100%; }
  .card .value { white-space: nowrap; }
}
