/* ─────────────────────────────────────────────────────────────────────────────
   mobile.css — the responsive layer for the Ninjora Editor (30 Oct 2026).

   Loaded LAST so it can refine every stylesheet before it. Three ideas:
     1. The shell: below 960px the sidebar is an off-canvas drawer opened from a
        compact top bar (mobile-shell.js builds the bar, the backdrop and the
        drawer chrome); the main column gets the full width.
     2. Touch: every control is at least 44px tall on coarse pointers, hover-only
        affordances are always visible, and text inputs are 16px so iOS does not
        zoom into them.
     3. Content: two-pane sections (list + editor) become one pane at a time,
        grids stack, dialogs go full-screen, tables scroll inside their own box.
   Nothing here hides overflow to paper over a bug: the fixed widths that caused
   the overflow are replaced by fluid ones.
   ───────────────────────────────────────────────────────────────────────────── */

:root {
  --nj-sidebar-w: 220px;
  --nj-topbar-h: 56px;
  --nj-tap: 44px;
  --nj-tap-lg: 48px;
  --nj-safe-top: env(safe-area-inset-top, 0px);
  --nj-safe-bottom: env(safe-area-inset-bottom, 0px);
  --nj-safe-left: env(safe-area-inset-left, 0px);
  --nj-safe-right: env(safe-area-inset-right, 0px);
}

/* Fixed screen height everywhere: Safari's shrinking browser bars resize dvh,
   100vh would leave the bottom of the app under the toolbar. */
html, body { height: 100%; }
.app-container { height: 100vh; height: 100dvh; min-height: 0; }
.main-content { min-width: 0; }
.content-area { min-height: 0; }
.section-content { min-width: 0; }

/* The mobile top bar and drawer chrome exist in the DOM at every width; they
   only show below the breakpoint. */
.nj-topbar { display: none; }
.nj-backdrop { display: none; }
.nj-drawer-close { display: none; }
.nj-drawer-footer { display: none; }
.nj-pane-bar { display: none; }

/* ── Touch targets (any device with a coarse pointer, at any width) ───────── */
@media (pointer: coarse) {
  button, .btn, [role="button"], select, input[type="button"], input[type="submit"] {
    min-height: var(--nj-tap);
  }
  button.btn-sm { min-height: 40px; }
  input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; flex-shrink: 0; }
  .checkbox-label, .checkbox-group { min-height: var(--nj-tap); }
  /* Hover-only affordances must be reachable with a finger. */
  .item-actions { opacity: 1; }
  .list-item { min-height: var(--nj-tap-lg); }
  .nav-item { min-height: var(--nj-tap-lg); }
  /* Pressed feedback where hover used to be the only cue. */
  button:active, .nav-item:active, .list-item:active, [role="tab"]:active { filter: brightness(0.9); }
}

/* ── Phones and small tablets: the drawer shell ───────────────────────────── */
@media (max-width: 959px) {
  body.nj-mobile .app-container {
    position: relative;
    overflow: hidden;
    padding-top: var(--nj-safe-top);
  }

  /* The sidebar becomes a drawer over the content. When closed it is fully
     hidden (visibility) so it can never intercept a tap through the fade. */
  body.nj-mobile .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(84vw, 320px);
    max-width: 100%;
    z-index: 12000;   /* above the full-screen workspaces (.jw 3000, .mw 3100, .items-popup 9000) */
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.22s ease, visibility 0s linear 0.22s;
    box-shadow: 0 0 0 rgba(0,0,0,0);
    padding-top: var(--nj-safe-top);
    padding-left: var(--nj-safe-left);
    padding-bottom: var(--nj-safe-bottom);
  }
  body.nj-mobile.nj-nav-open .sidebar {
    transform: none;
    visibility: visible;
    transition: transform 0.22s ease, visibility 0s;
    box-shadow: 0 0 40px rgba(0,0,0,0.5);
  }
  @media (prefers-reduced-motion: reduce) {
    body.nj-mobile .sidebar { transition: none; }
  }

  body.nj-mobile .nj-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 11900;
    background: rgba(2, 6, 23, 0.55);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.22s ease;
    touch-action: none;
  }
  body.nj-mobile.nj-nav-open .nj-backdrop { opacity: 1; pointer-events: auto; }

  /* Drawer header: logo + a big close button. */
  body.nj-mobile .logo {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    min-height: var(--nj-topbar-h);
    padding: 8px 8px 8px 16px;
  }
  body.nj-mobile .nj-drawer-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--nj-tap-lg);
    height: var(--nj-tap-lg);
    min-height: 0;
    padding: 0;
    border-radius: 10px;
    background: rgba(255,255,255,0.08);
    color: #f1ead8;
    font-size: 22px;
    line-height: 1;
  }
  body.nj-mobile .nj-drawer-close:active { background: rgba(255,255,255,0.18); }

  body.nj-mobile .nav-menu {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding-bottom: 12px;
  }
  body.nj-mobile .nav-item {
    min-height: var(--nj-tap-lg);
    padding: 10px 16px;
    font-size: 15px;
    gap: 12px;
  }
  body.nj-mobile .nav-item:active { background: rgba(255,255,255,0.12); }
  body.nj-mobile .nav-category { padding: 14px 16px 6px; font-size: 11px; }

  /* Account controls + the data buttons (moved here from the header) stay
     reachable at the bottom of the drawer. */
  body.nj-mobile .nj-drawer-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 12px calc(10px + var(--nj-safe-bottom));
    border-top: 1px solid rgba(255,255,255,0.1);
    background: #1a1e28;
    flex-shrink: 0;
  }
  body.nj-mobile .nj-drawer-footer button {
    flex: 1 1 calc(50% - 8px);
    min-height: var(--nj-tap);
    justify-content: center;
    color: #f1ead8;
    border: 1px solid rgba(255,255,255,0.25);
    background: rgba(255,255,255,0.06);
  }
  body.nj-mobile #sidebar-user-badge > div { margin: 0 12px 4px; }
  body.nj-mobile #editor-logout-btn { min-height: var(--nj-tap); padding: 4px 14px !important; font-size: 13px !important; }

  /* Compact top bar: menu, title, presence. */
  body.nj-mobile .nj-topbar {
    display: flex;
    align-items: center;
    gap: 6px;
    min-height: var(--nj-topbar-h);
    padding: 4px 8px;
    padding-right: calc(8px + var(--nj-safe-right));
    padding-left: calc(8px + var(--nj-safe-left));
    background: #1a1e28;
    color: #f1ead8;
    flex-shrink: 0;
    position: relative;
    z-index: 50;
  }
  body.nj-mobile .nj-topbar button {
    min-height: var(--nj-tap-lg);
    min-width: var(--nj-tap-lg);
    padding: 0 10px;
    border-radius: 10px;
    background: rgba(255,255,255,0.08);
    color: #f1ead8;
    font-size: 15px;
    justify-content: center;
  }
  body.nj-mobile .nj-topbar button:active { background: rgba(255,255,255,0.2); }
  body.nj-mobile .nj-topbar .nj-menu-btn { font-size: 22px; }
  body.nj-mobile .nj-topbar .nj-topbar-title {
    flex: 1;
    min-width: 0;
    font-size: 17px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 0 4px;
  }
  body.nj-mobile .nj-presence-btn[hidden] { display: none; }
  body.nj-mobile .nj-presence-btn .nj-presence-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: #8d7a4e;
    font-size: 12px;
    font-weight: 700;
  }
  body.nj-mobile .nj-presence-pop {
    position: absolute;
    top: calc(100% + 4px);
    right: 8px;
    left: 8px;
    z-index: 60;
    background: #1a1e28;
    border: 1px solid #38352f;
    border-radius: 10px;
    padding: 10px;
    box-shadow: 0 12px 32px rgba(0,0,0,0.45);
  }
  body.nj-mobile .nj-presence-pop[hidden] { display: none; }
  body.nj-mobile .nj-presence-pop #presence-strip {
    margin: 0 !important;
    gap: 8px !important;
  }
  body.nj-mobile .nj-presence-pop #presence-strip > span {
    font-size: 13px !important;
    padding: 6px 10px 6px 8px !important;
    white-space: normal !important;
  }
  body.nj-mobile .nj-presence-pop .nj-presence-empty { color: #9b9788; font-size: 13px; }

  /* The desktop header is replaced by the top bar; its buttons live in the
     drawer footer while on a phone. Sections that hid the header (Maps) keep
     the top bar, which is the only way back to the menu. */
  body.nj-mobile .header { display: none; }

  /* Main column takes the whole width. */
  body.nj-mobile .main-content { width: 100%; }

  /* Two-pane sections: one pane at a time. mobile-shell.js stamps
     data-nj-pane="list" | "editor" on the active section. */
  body.nj-mobile .section-content.active { display: flex; flex-direction: column; }
  body.nj-mobile .section-content > .list-panel {
    width: 100%;
    max-width: 100%;
    border-right: none;
    flex: 1 1 auto;
    min-height: 0;
  }
  body.nj-mobile .section-content[data-nj-pane="editor"] > .list-panel { display: none; }
  body.nj-mobile .section-content[data-nj-pane="list"] > .editor-panel,
  body.nj-mobile .section-content[data-nj-pane="list"] > .nj-pane-bar { display: none; }
  body.nj-mobile .section-content > .editor-panel { width: 100%; min-width: 0; }
  body.nj-mobile .nj-pane-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    background: #1a1e28;
    border-bottom: 1px solid #38352f;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 5;
  }
  body.nj-mobile .nj-pane-bar button {
    min-height: var(--nj-tap);
    background: #21242c;
    color: #d9d3c4;
    border: 1px solid #8d7a4e;
  }
  body.nj-mobile .nj-pane-bar .nj-pane-title {
    flex: 1;
    min-width: 0;
    font-weight: 600;
    color: #d9d3c4;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  /* Studio pages live in iframes: let them take the whole column. */
  body.nj-mobile .section-content > iframe { flex: 1 1 auto; min-height: 0; width: 100%; }
  body.nj-mobile .editor-content { padding: 12px; }
  body.nj-mobile .editor-section { padding: 14px; margin-bottom: 14px; }

  /* Grids collapse to a column. */
  body.nj-mobile .form-row,
  body.nj-mobile .form-row.triple,
  body.nj-mobile .form-row.quad { grid-template-columns: 1fr; gap: 10px; }

  /* Toasts span the width instead of sticking to a 300px box off the edge. */
  .toast { left: 12px; right: 12px; min-width: 0; top: calc(12px + var(--nj-safe-top)); }
}

/* ── Phones: readable form text (no iOS zoom), full-width fields ─────────── */
@media (max-width: 767px) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
  select, textarea { font-size: 16px; }
  .search-box { font-size: 16px; min-height: var(--nj-tap); }
  .form-group input, .form-group select, .form-group textarea { font-size: 16px; min-height: var(--nj-tap); }
  .header-actions { flex-wrap: wrap; }
  button { padding: 8px 14px; }
}

/* ── Dashboard ─────────────────────────────────────────────────────────── */
.dash-grid { display: grid; gap: 10px; margin-bottom: 10px; }
.dash-grid-2 { grid-template-columns: 1fr 1fr; }
.dash-grid-3 { grid-template-columns: 1fr 1fr 1fr; }
.dash-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.dash-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.dash-actions > button { flex: 1 1 auto; justify-content: center; min-height: var(--nj-tap); }
.dash-card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.dash-card-head > div { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.dash-card-head button { min-height: 36px; }

/* Task rows: a title line and a meta line that can each wrap, actions stay
   on the right and never shrink. */
.task-row { display: flex; align-items: flex-start; gap: 8px; padding: 8px 0; border-bottom: 1px solid #38352f; font-size: 12px; }
.task-row .task-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; margin-top: 6px; }
.task-row .task-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.task-row .task-title { color: #e2d3a6; overflow-wrap: anywhere; line-height: 1.4; }
.task-row .task-meta { display: flex; flex-wrap: wrap; gap: 4px 10px; font-size: 11px; color: #9b9788; }
.task-row .task-status { font-weight: 600; }
.task-row .task-actions { display: flex; gap: 4px; flex-shrink: 0; }
.task-row .task-actions button { min-height: 36px; min-width: 36px; justify-content: center; }
.task-row.task-done .task-title { text-decoration: line-through; opacity: 0.5; }
.task-input-row { display: flex; gap: 8px; margin-bottom: 8px; }
.task-input-row input { flex: 1; min-width: 0; }

@media (max-width: 959px) {
  .dash-grid-2, .dash-grid-3 { grid-template-columns: 1fr; }
  #dashboard-content { font-size: 13px; }
  #task-list { max-height: none !important; }
  #section-dashboard > div { padding: 12px !important; }
}
@media (pointer: coarse) {
  .task-row .task-actions button { min-height: var(--nj-tap); min-width: var(--nj-tap); }
  .dash-card-head button { min-height: var(--nj-tap); }
}

/* ── Dialogs and overlays on phones ────────────────────────────────────── */
@media (max-width: 767px) {
  /* Generic modal panels become full-screen sheets with a reachable footer. */
  body.nj-mobile .items-popup .items-dialog,
  body.nj-mobile .modal-content,
  body.nj-mobile .modal-dialog,
  body.nj-mobile .dialog-panel {
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    border-radius: 0 !important;
    margin: 0 !important;
    display: flex;
    flex-direction: column;
    padding-top: var(--nj-safe-top);
    padding-bottom: var(--nj-safe-bottom);
  }
  body.nj-mobile .items-popup { padding: 0 !important; align-items: stretch !important; }
  body.nj-mobile .items-dialog .editor-panel { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  body.nj-mobile .items-dialog-footer { flex-wrap: wrap; gap: 8px; position: sticky; bottom: 0; background: inherit; }
  body.nj-mobile .items-dialog-footer button { min-height: var(--nj-tap); }
  body.nj-mobile .items-close { width: var(--nj-tap-lg); height: var(--nj-tap-lg); font-size: 26px; }
  body.nj-mobile .items-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; flex-wrap: nowrap; scrollbar-width: none; }
  body.nj-mobile .items-tabs::-webkit-scrollbar { display: none; }
  body.nj-mobile .items-tabs [role="tab"] { min-height: var(--nj-tap); white-space: nowrap; flex-shrink: 0; }
}

/* ── Tables: scroll inside their own box, never the page ───────────────── */
@media (max-width: 959px) {
  body.nj-mobile .editor-panel table,
  body.nj-mobile .section-content table.nj-scroll-table { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  body.nj-mobile .wk-tablewrap, body.nj-mobile .table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* ── Keyboard: keep the focused field and the save row reachable ───────── */
@media (max-width: 767px) {
  body.nj-mobile .editor-panel { scroll-padding-bottom: 120px; padding-bottom: calc(96px + var(--nj-safe-bottom)); }
}

/* Visible focus for keyboard and switch users; the drawer relies on it. */
body.nj-mobile .nav-item:focus-visible,
body.nj-mobile .nj-topbar button:focus-visible,
body.nj-mobile .nj-drawer-close:focus-visible { outline: 2px solid #8d7a4e; outline-offset: -2px; }
