/* ============================================================
   MihaMini — Mobile layer
   Adapts the EXISTING desktop UI for phones. Pure CSS, scoped to
   small screens via @media. Does NOT change desktop layout.
   Companion: mobile.js (drives existing controls + deep-links).
   ============================================================ */

/* Injected mobile-only chrome is hidden on desktop */
.m-ctx-trigger, .m-backdrop { display: none; }

@media (max-width: 760px) {

  /* ---- base / scroll model ---- */
  body.rt { height: 100dvh; overflow: hidden; }

  /* Bigger base type for arm's-length reading */
  :root {
    --rt-fs-12: 12.5px;
    --rt-fs-13: 14px;
    --rt-fs-14: 15.5px;
    --rt-fs-16: 17px;
    --rt-fs-18: 19px;
    --rt-fs-20: 21px;
    --rt-fs-24: 24px;
    --rt-fs-32: 27px;
  }

  /* ============================ HEADER ============================ */
  .app-header__inner {
    grid-template-columns: auto 1fr auto;
    gap: 10px;
    padding: 9px 14px;
    max-width: none;
  }
  .app-brand { gap: 9px; }
  .app-brand__logo { width: 38px; height: 38px; box-shadow: 0 4px 12px -4px rgba(168,85,247,.6), 0 0 0 2px rgba(168,85,247,.18); }
  .app-brand__name { font-size: 16px; }
  .app-brand__meta { display: none; }

  /* Search collapses on mobile — not a top-5 task; freeing the row */
  .searchbox { display: none; }

  /* Keep only essential actions; search collapses, main actions stay visible */
  .header-actions { gap: 4px; }
  .header-actions .icon-btn { width: 40px; height: 40px; }
  #ops-btn { display: inline-grid; }

  /* Injected by mobile.js — current-context trigger (4-letter, bottom-left). */
  .m-ctx-trigger {
    position: fixed;
    left: 14px;
    bottom: calc(78px + env(safe-area-inset-bottom));
    z-index: 291;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 52px;
    padding: 0 14px;
    background: var(--rt-surface);
    border: 1.5px solid var(--rt-purple-300);
    border-radius: var(--rt-radius-pill);
    box-shadow: 0 12px 28px -10px rgba(119,0,255,.45);
    font: inherit;
    color: var(--rt-purple-700);
    font-weight: 700;
    cursor: pointer;
  }
  .m-ctx-trigger__avatar { display: none; }
  .m-ctx-trigger__name { font-size: 15px; letter-spacing: .01em; white-space: nowrap; }
  .m-ctx-trigger__chev { flex: 0 0 auto; opacity: .6; transform: rotate(180deg); }

  /* ============================ CONTEXT SHEET ============================ */
  /* The existing .role-rail becomes a bottom sheet, opened by the trigger. */
  .role-rail {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 320;
    max-height: 76dvh;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    border-bottom: none;
    box-shadow: 0 -16px 48px -16px rgba(16,16,24,.32);
    transform: translateY(110%);
    transition: transform .26s cubic-bezier(.22,1,.36,1);
    overflow: hidden;
  }
  body.m-ctx-open .role-rail { transform: translateY(0); }
  .role-rail__inner {
    max-width: none;
    padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
    overflow-y: auto;
    max-height: 76dvh;
  }
  /* grab handle */
  .role-rail::before {
    content: '';
    position: absolute; top: 8px; left: 50%;
    transform: translateX(-50%);
    width: 40px; height: 4px; border-radius: 99px;
    background: var(--rt-border-strong);
    z-index: 1;
  }
  .role-rail__inner::before {
    content: 'Контекст';
    display: block;
    font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
    color: var(--rt-fg-muted);
    padding: 14px 0 10px;
  }
  /* context tree → comfortable tappable rows */
  .context-tree { gap: 10px; }
  .context-tree__lane { grid-template-columns: 1fr; gap: 8px; }
  .context-tree__lane-label { text-align: left; }
  .context-tree__row { gap: 8px; }
  .context-node {
    width: 100%;
    padding: 12px 14px 12px 8px;
    border-radius: var(--rt-radius-md);
  }
  .context-node__name { font-size: 15px; white-space: normal; }
  .context-node__desc { font-size: 12px; }
  .context-node.is-open::after { display: none; }

  /* backdrop for sheets (context + capture); toggled by mobile.js */
  .m-backdrop {
    position: fixed; inset: 0;
    display: block;
    background: rgba(16,16,24,.34);
    z-index: 310;
    opacity: 0; pointer-events: none;
    transition: opacity .22s ease;
  }
  body.m-ctx-open .m-backdrop { opacity: 1; pointer-events: auto; }

  /* ============================ BOTTOM NAV ============================ */
  /* The existing .app-nav docks to the bottom as a tab bar. */
  .app-nav {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 300;
    border-top: 1px solid var(--rt-border);
    border-bottom: none;
    background: rgba(255,255,255,.96);
    backdrop-filter: blur(14px) saturate(140%);
    -webkit-backdrop-filter: blur(14px) saturate(140%);
    box-shadow: 0 -8px 24px -16px rgba(16,16,24,.18);
  }
  .app-nav__inner {
    max-width: none;
    padding: 0 6px;
    padding-bottom: env(safe-area-inset-bottom);
    gap: 0;
    justify-content: space-around;
  }
  .app-nav__inner .tab {
    flex: 1;
    flex-direction: column;
    gap: 3px;
    padding: 9px 4px 8px;
    font-size: 11px;
    font-weight: 600;
    border-bottom: none;
    margin-bottom: 0;
    color: var(--rt-fg-muted);
  }
  .app-nav__inner .tab svg { width: 22px; height: 22px; }
  .app-nav__inner .tab.active { color: var(--rt-purple-700); border-bottom: none; }
  .app-nav__inner .tab.active::after { display: none; }

  /* ============================ MAIN ============================ */
  .main {
    display: flex;
    flex-direction: column;
    max-width: none;
    padding: 14px 14px calc(128px + env(safe-area-inset-bottom));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .main__content { overflow: visible; padding-right: 0; }
  /* On mobile the aside is dismantled: Top-10 becomes its own tab, Inbox &
     digest are removed, capture lives in the FAB. Hide the whole aside. */
  .main__aside { display: none; }
  .card--chat { display: none !important; }   /* chat removed entirely */
  .card--digest { display: block; }
  #aside-inbox-card { display: none !important; }

  /* «Сегодня» = обзор без чата; keep digest and quick navigation */
  .today-grid { display: block; }
  .today-grid > .card--digest { display: block; }
  #quick-capture > .quick-card:first-child,
  #quick-capture > .quick-card--ghost { display: none; }

  /* Top-10 list shouldn't be height-capped on mobile (no fixed sidebar) */
  .quick-card--tasks { min-height: 0; max-height: none; display: block; }
  .quick-card--tasks .today-list {
    max-height: none;
    mask-image: none;
    -webkit-mask-image: none;
  }
  .today-list--compact .today-task__title { font-size: 15px !important; }
  .today-list--compact .today-task__meta .chip { font-size: 11px; padding: 2px 8px; }
  .today-list--compact .today-task__check { width: 22px !important; height: 22px !important; }
  /* Top-10 ranking number */
  .today-list--compact .today-task {
    display: flex !important;
    grid-template-columns: none !important;
    align-items: center;
    gap: 11px !important;
    padding: 11px 10px !important;
  }
  .today-list--compact .today-task > div { flex: 1; min-width: 0; }
  .today-task__rank {
    flex: 0 0 auto;
    min-width: 22px; text-align: center;
    font-size: 15px; font-weight: 800;
    color: var(--rt-purple-500);
  }
  .today-task.is-overdue .today-task__rank { color: var(--rt-orange-500); }

  /* ============================ PAGE HEADS ============================ */
  /* Section name lives in the bottom tab bar — drop the in-page title + the
     explanatory subtitle. The primary «+ добавить» action floats above the
     capture FAB instead of sitting in a header. */
  .page-head { margin-bottom: 12px; min-height: 0; }
  .page-head__title, .page-head__sub { display: none; }
  .page-head__hint { display: none; }

  #section-tasks.active .page-head .btn[data-jump-capture] {
    position: fixed;
    right: 16px;
    bottom: calc(150px + env(safe-area-inset-bottom));
    z-index: 291;
    width: 52px; height: 52px; padding: 0;
    gap: 0; font-size: 0; overflow: hidden; white-space: nowrap;
    border-radius: 50%;
    justify-content: center;
    box-shadow: 0 10px 26px -8px rgba(119,0,255,.5);
  }
  #section-tasks.active .page-head .btn[data-jump-capture] svg { width: 24px; height: 24px; flex: 0 0 auto; }

  /* ============================ TODAY / CHAT ============================ */
  /* «Сегодня» = только чат: убираем заголовок карточки, чтобы помещалось больше */
  .card--chat .card__head { display: none; }
  .chat-topic-controls { max-width: 100%; }
  .topic-select { min-width: 0; flex: 1; max-width: none; }
  .msg { max-width: 88%; font-size: 15.5px; }
  .composer { grid-template-columns: 1fr; gap: 10px; }
  .composer__actions { justify-content: space-between; }
  .composer__actions .btn { flex: 1; justify-content: center; }

  /* ============================ TASKS — CARDS ============================ */
  /* Targets the REAL app.js markup: tr.task-row (draggable) with 6 <td>:
     1 .task-title(.task-order+.task-title__text) · 2 .task-context · 3 .task-due-date
     · 4 status text · 5 closed date · 6 button.task-toggle */
  #section-tasks .panel-h { display: block; padding: 10px 12px; }
  #section-tasks .panel-h > span:first-child { display: none; }  /* «Мои задачи» — личный заголовок */

  /* both filter rows: strictly ONE line, horizontal scroll if needed */
  #section-tasks .filter-row {
    display: flex; gap: 6px; flex-wrap: nowrap;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    margin: 0 -12px 7px; padding: 0 12px 2px;
  }
  #section-tasks .filter-row:last-child { margin-bottom: 0; }
  #section-tasks .filter-row::-webkit-scrollbar { display: none; }
  #section-tasks .filter-row .btn-sm {
    flex: 0 0 auto; white-space: nowrap;
    padding: 6px 12px; font-size: 13px; border-radius: 99px;
  }
  #section-tasks .filter-row__search { display: none; }  /* поиск скрыт на мобиле */
  #section-tasks .task-count { font-size: 11px; opacity: .65; }

  /* table → cards */
  #section-tasks .table-wrap { overflow: visible; }
  #section-tasks .data-table thead { display: none; }
  #section-tasks .data-table,
  #section-tasks .data-table tbody { display: block; }

  #section-tasks .data-table tr.task-row {
    display: block; position: relative;
    padding: 12px 92px 12px 10px;       /* right room for the toggle button */
    border-bottom: 1px solid var(--rt-border);
    border-left: none !important;
    background: var(--rt-surface);
  }
  #section-tasks .data-table td {
    display: inline-block;
    border: none !important;
    padding: 0;
    vertical-align: middle;
  }
  /* 1) title — full-width block */
  #section-tasks .data-table td:nth-child(1) { display: block; margin-bottom: 7px; }
  #section-tasks .task-title {
    grid-template-columns: 26px minmax(0,1fr) !important;  /* number(grip) + text */
    gap: 10px !important; align-items: start;
  }
  #section-tasks .task-title::before { display: none; }  /* desktop ®® off — number is the grip */
  #section-tasks .task-order {
    cursor: grab; touch-action: none;
    width: 26px !important; height: 26px !important; font-size: 12px;
    margin-top: 1px;
  }
  #section-tasks .task-order:active { cursor: grabbing; }
  #section-tasks .task-title__text {
    font-size: 15.5px !important; font-weight: 500;
    white-space: normal !important; line-height: 1.34;
    padding: 0 !important;
  }
  /* 2) context chip + 3) due — small inline meta */
  #section-tasks .data-table td:nth-child(2) { margin-right: 7px; }
  #section-tasks .task-context { font-size: 12px !important; padding: 2px 9px !important; white-space: nowrap; }
  #section-tasks .data-table td:nth-child(3) { font-size: 12px; color: var(--rt-fg-muted); }
  #section-tasks .task-due-date.is-overdue { color: var(--rt-orange-600); font-weight: 600; }
  /* 4) status text + 5) closed date — hidden (status comes from the filter) */
  #section-tasks .data-table td:nth-child(4),
  #section-tasks .data-table td:nth-child(5) { display: none; }
  /* 6) toggle button — compact, neutral, vertically centered on the right */
  #section-tasks .data-table td:nth-child(6) {
    position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
    margin: 0;
  }
  #section-tasks .task-toggle {
    padding: 8px 12px !important; font-size: 12px !important; white-space: nowrap;
    background: var(--rt-surface) !important;
    color: var(--rt-fg-muted) !important;
    border: 1px solid var(--rt-border) !important;
  }
  #section-tasks .task-row.is-done .task-title__text { text-decoration: line-through; color: var(--rt-fg-muted); }

  /* drag feedback */
  #section-tasks .data-table tr.task-row.dragging { opacity: .6; background: var(--rt-purple-50); box-shadow: 0 8px 20px -8px rgba(119,0,255,.35); }
  #section-tasks .data-table tr.task-row.drop-above { box-shadow: inset 0 2px 0 var(--rt-purple-500); }
  #section-tasks .data-table tr.task-row.drop-below { box-shadow: inset 0 -2px 0 var(--rt-purple-500); }

  /* ============================ NOTEBOOK ============================ */
  .notebook-search input { height: 46px; }
  .nb-tabs { gap: 6px; align-items: center; }
  .nb-tab { flex: 1; }
  .nb-add { flex: 0 0 auto; width: 42px; height: 42px; padding: 0; border-radius: 11px; justify-content: center; }
  .nb-add svg { width: 20px; height: 20px; }

  /* nb tab switches notes → lists */
  #section-notebook .notebook-lists-panel { display: none; }
  #section-notebook[data-nb-active="lists"] .notebook-primary { display: none; }
  #section-notebook[data-nb-active="lists"] .notebook-lists-panel { display: block; }

  /* iOS master→detail drill: list → tap → detail + back */
  .notes-ios-layout { display: block; grid-template-columns: 1fr; min-height: 0; }
  .note-list-panel { max-height: none; border-right: none; border-bottom: none; display: block; }
  .note-list-item { padding: 13px 14px; border-bottom: 1px solid var(--rt-border); }
  .note-list-item__title { font-size: 16px; }
  .note-editor-panel { display: none; padding: 14px 4px 0; }
  #section-notebook.is-detail .note-list-panel { display: none; }
  #section-notebook.is-detail .note-editor-panel { display: block; }
  .note-back { display: inline-flex; align-items: center; gap: 6px; background: none; border: none; color: var(--rt-purple-600); font-weight: 600; font-size: 15px; padding: 4px 0 14px; cursor: pointer; }
  .note-detail__title { font-size: 21px; font-weight: 700; margin-bottom: 4px; line-height: 1.2; }
  .note-detail__meta { font-size: 12px; color: var(--rt-fg-muted); margin-bottom: 16px; }
  .note-preview { min-height: 220px; font-size: 16px; line-height: 1.55; }

  /* ============================ DOCUMENTS ============================ */
  #section-documents .panel-h { flex-direction: column; align-items: stretch; }
  #section-documents .data-table thead { display: none; }
  #section-documents .data-table,
  #section-documents .data-table tbody { display: block; }
  #section-documents .data-table tr { display: block; padding: 12px 14px; border-bottom: 1px solid var(--rt-border); }
  #section-documents .data-table td { display: block; border: none; padding: 2px 0; font-size: 14px; }

  /* ============================ CAPTURE (mobile bottom-sheet) ============================ */
  /* capture-widget.js injects <style id="cap-widget-css"> AFTER mobile.css, so
     these overrides are prefixed with `body.rt` to win on specificity. */
  /* Puck — compact FAB above the bottom nav */
  body.rt .cap-puck-wrap {
    right: 16px;
    bottom: calc(78px + env(safe-area-inset-bottom));
    z-index: 290;
  }
  body.rt .cap-puck {
    padding: 0;
    width: 58px; height: 58px;
    border-radius: 50%;
    justify-content: center;
    box-shadow: 0 12px 28px -8px rgba(119,0,255,.5);
  }
  body.rt .cap-puck__label, body.rt .cap-kbd, body.rt .cap-puck__dots { display: none; }
  body.rt .cap-puck__spark { width: 30px; height: 30px; }
  body.rt .cap-puck__spark svg { width: 20px; height: 20px; }
  /* inbox badge floats on the FAB */
  body.rt .cap-puck__inbox {
    position: absolute; top: -2px; right: -2px;
    border: 2px solid var(--rt-surface);
  }

  /* Composer popover → full-width bottom sheet */
  body.rt .cap-pop {
    left: 0; right: 0; bottom: 0;
    top: auto;
    width: 100%;
    max-width: 100%;
    border-radius: 22px 22px 0 0;
    max-height: 90dvh;
    overflow-y: auto;
    transform: none;
    animation: none;
  }
  body.rt .cap-pop__body { padding: 18px 16px calc(20px + env(safe-area-inset-bottom)); }
  body.rt .cap-pop::before {
    content: '';
    position: sticky; top: 0;
    display: block;
    width: 40px; height: 4px; margin: 8px auto 0;
    border-radius: 99px; background: var(--rt-border-strong);
  }
  body.rt .cap-ta { min-height: 120px; font-size: 16px; } /* 16px avoids iOS zoom-on-focus */
  body.rt .cap-route { min-height: 0; }
  body.rt .cap-favs { gap: 8px; }
  body.rt .cap-fav { padding: 8px 13px 8px 7px; font-size: 13px; }
  body.rt .cap-foot { flex-direction: column; align-items: stretch; gap: 10px; }
  body.rt .cap-foot-hint { order: 2; text-align: center; }
  body.rt .cap-save { order: 1; width: 100%; padding: 13px 22px; font-size: 15px; }

  /* Inbox triage items — larger tap targets */
  body.rt .inbox-route-btn { padding: 6px 12px; font-size: 12px; }

  /* ============================ ADMIN OVERLAY ============================ */
  .admin-panel { width: 100%; height: 100dvh; border-radius: 0; }
  .admin-panel__body { grid-template-columns: 1fr; }
  .admin-rail { flex-direction: row; overflow-x: auto; }

  /* toasts above the bottom nav */
  .toast-wrap { bottom: calc(140px + env(safe-area-inset-bottom)); }

  /* ============================ TOP-10 / DOCUMENTS SWITCH ============================ */
  /* On mobile, replace Documents tab with Top-10 */
  .app-nav .tab[data-section="documents"] { display: none; }
  .app-nav .tab[data-section="top10"] { display: inline-flex !important; }

  #section-documents { display: none; }
}

/* Very small phones */
@media (max-width: 380px) {
  .app-nav__inner .tab { font-size: 10px; }
  .m-ctx-trigger { max-width: 40vw; }
}
