/* ==================================================================================================
   Tablet and touch (UX review C2, C3, H6, M6 and mockup B). js/touch.js, js/tablet.js.
   ================================================================================================== */

/* ---- C2 / C3: who gets the finger.
   The page scrolls with one finger (pan) on the canvas, the panels and the top bar, and nothing there
   zooms the browser UI (pinch-zoom is left out of touch-action): a pinch on the canvas is the page zoom
   (touch.js). A selected element and the handles take the finger (move / resize). Dialogs keep the
   default, so their text can still be pinch-zoomed. */
.topbar, .layout, .lock-bar, .tab-bar, .tab-sheet, .tab-placing, .tab-hint { touch-action: pan-x pan-y; }
.canvas-scroll { touch-action: pan-x pan-y; -webkit-touch-callout: none; }
.design-page .fd-el.touch-sel, .sel-layer .handle, .tab-grip { touch-action: none; }
.tab-pin { touch-action: manipulation; }

/* ---- H6: fingers (a coarse pointer, whatever the screen width) */
@media (pointer: coarse) {
  :root { --topbar-h: 60px; }
  .tb, .btn, .tab, .mtab, .tpl-switch, .lang-select { min-height: 44px; }
  .tb.icon, .mini, .col-actions .mini { min-width: 44px; min-height: 44px; height: auto; }
  .btn-sm { min-height: 40px; }
  .f input[type=text], .f input[type=number], .f input:not([type]), .f select, .f textarea, .f-num input,
  .search input, .input-lg, .mfilters select, .mfilters input { min-height: 44px; font-size: 16px; }
  .f input[type=color] { width: 44px; height: 44px; }
  input[type=checkbox], input[type=radio] { width: 22px; height: 22px; }
  .tool { min-height: 64px; cursor: pointer; }
  .dt-row { min-height: 44px; }
  .page-card { min-height: 56px; }
  .fd-mi { min-height: 44px; padding: 10px 12px; }
  .pg > summary, .col-card > summary { min-height: 44px; }
  .page-move span { display: inline-grid; place-items: center; min-width: 44px; min-height: 44px; }
  /* handles: 16 px on screen (tablet.js sizes them for the zoom), round, with a 44 px hit area */
  .sel-layer .handle { border-radius: 50%; border-width: calc(2 * var(--hz, 1px)); box-shadow: 0 0 0 calc(1px * var(--hzn, 1)) rgba(255, 255, 255, .8); }
  .sel-layer .handle::before { content: ''; position: absolute; inset: calc(-16 * var(--hz, 1px)); border-radius: 50%; }
  /* a small box keeps only the corners, so the hit areas do not cover each other */
  /* (a short box crowds the middle handles of its sides between the corners; a narrow one those of its top and bottom) */
  .short-touch > .h-e, .short-touch > .h-w, .narrow-touch > .h-n, .narrow-touch > .h-s { display: none; }
  /* a box under 44 px: the handles of opposite sides moved outwards (tablet.js sets --out-x / --out-y) */
  .sel-layer .h-nw { translate: calc(-1 * var(--out-x, 0px)) calc(-1 * var(--out-y, 0px)); }
  .sel-layer .h-n { translate: 0 calc(-1 * var(--out-y, 0px)); }
  .sel-layer .h-ne { translate: var(--out-x, 0px) calc(-1 * var(--out-y, 0px)); }
  .sel-layer .h-e { translate: var(--out-x, 0px) 0; }
  .sel-layer .h-se { translate: var(--out-x, 0px) var(--out-y, 0px); }
  .sel-layer .h-s { translate: 0 var(--out-y, 0px); }
  .sel-layer .h-sw { translate: calc(-1 * var(--out-x, 0px)) var(--out-y, 0px); }
  .sel-layer .h-w { translate: calc(-1 * var(--out-x, 0px)) 0; }
}

/* ---- the tablet mode UI exists only in tablet mode */
body:not(.tablet-mode) .tablet-only { display: none !important; }
body.tablet-mode { --tab-bar-h: 68px; --sheet-h: 0px; }

/* top bar: the template, the mode switch, save, publish, preview and "⋯"; the rest is in the bottom bar */
body.tablet-mode .topbar [data-action="new"], body.tablet-mode .topbar [data-action="open"]:not(.tpl-switch),
body.tablet-mode .topbar [data-action="save-as"], body.tablet-mode .topbar [data-action="import"],
body.tablet-mode .topbar [data-action="data"], body.tablet-mode .topbar [data-action="undo"],
body.tablet-mode .topbar [data-action="redo"], body.tablet-mode .topbar .tb-group.zoom,
body.tablet-mode .topbar .tb-toggle, body.tablet-mode .topbar .tour-btn, body.tablet-mode .app-ver,
body.tablet-mode .brand-sub { display: none !important; }
body.tablet-mode .topbar .tb-group { border: 0; padding: 0; margin: 0; }
body.tablet-mode .topbar { gap: 8px; }
/* the template's name gives way, so "⋯" and the main buttons always fit (C1 on a tablet) */
body.tablet-mode .topbar .tpl-id { flex: 0 1 auto; min-width: 72px; }
body.tablet-mode .topbar .tpl-switch { max-width: 100%; min-width: 0; }
body.tablet-mode .topbar .lang-select { max-width: 110px; }
.tab-switch { display: inline-flex; flex: none; background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 2px; }
.tab-switch button { border: 0; background: none; border-radius: 9px; padding: 0 14px; min-height: 44px; color: var(--muted); font-weight: 600; white-space: nowrap; }
.tab-switch button[aria-checked="true"] { background: var(--panel); color: var(--accent); box-shadow: 0 1px 3px rgba(20, 30, 50, .15); }
.tab-desk { color: var(--muted); font-size: 12.5px; }

/* layout: the canvas takes the width; the side panels and the properties become bottom sheets */
body.tablet-mode .layout { grid-template-columns: 1fr; height: calc(100% - var(--topbar-h) - var(--tab-bar-h)); }
body.tablet-mode .center { position: relative; }
body.tablet-mode .side-start, body.tablet-mode .side-end {
  position: fixed; inset-inline: 0; bottom: var(--tab-bar-h); height: var(--sheet-h); z-index: 40; display: none;
  border: 0; border-top: 1px solid var(--line); box-shadow: 0 -10px 30px rgba(20, 30, 50, .14); overflow: auto; overscroll-behavior: contain;
}
body.tablet-mode[data-sheet="props"] .side-end { display: block; }
body.tablet-mode[data-sheet="side"] .side-start { display: flex; }
/* the rail of panels.js gives way to the bottom bar; the canvas is the only column; the sheets have no fold button
   (the grip closes them) */
body.tablet-mode .layout > .rail { display: none; }
body.tablet-mode .layout > .center { grid-column: 1; }
body.tablet-mode .side .side-collapse, body.tablet-mode .side-end .side-head { display: none; }
body.tablet-mode .side-start .tools { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
body.tablet-mode .side-start .agent-pane { max-width: 760px; margin: 0 auto; width: 100%; }
body.tablet-mode:not(.tab-dragging) .side-start, body.tablet-mode:not(.tab-dragging) .side-end, body.tablet-mode:not(.tab-dragging) .tab-sheet,
body.tablet-mode:not(.tab-dragging) .tab-grip { transition: height .18s ease, bottom .18s ease; }
body.tablet-mode[data-sheet] .canvas-scroll { padding-bottom: calc(var(--sheet-h) + 80px); }
/* the props panel in a wide sheet: its groups side by side when there is room */
@media (min-width: 900px) {
  body.tablet-mode #props .pg { break-inside: avoid; }
}

/* the sheet's grip */
.tab-grip { position: fixed; inset-inline: 0; bottom: calc(var(--tab-bar-h) + var(--sheet-h)); height: 52px; z-index: 41; display: none; align-items: center; gap: 10px;
  padding: 6px 16px 0; background: var(--panel); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 20px rgba(20, 30, 50, .10); cursor: grab; }
body.tablet-mode[data-sheet] .tab-grip { display: flex; }
.tab-grip-bar { position: absolute; top: 7px; left: 50%; width: 44px; height: 5px; margin-left: -22px; border-radius: 3px; background: #cbd2dd; }
.tab-grip b { font-size: 15px; }

/* my own sheet: the element picker and the comments */
.tab-sheet { position: fixed; inset-inline: 0; bottom: var(--tab-bar-h); height: var(--sheet-h); z-index: 40; background: var(--panel); overflow: auto; display: none; padding: 4px 16px 16px; overscroll-behavior: contain; }
body.tablet-mode[data-sheet="add"] .tab-sheet, body.tablet-mode[data-sheet="comments"] .tab-sheet { display: block; }
.tab-add, .tab-comments { display: none; }
body[data-sheet="add"] .tab-add { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; }
body[data-sheet="comments"] .tab-comments { display: block; max-width: 860px; margin: 0 auto; }
.tab-tool { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; min-height: 76px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); padding: 8px; font-size: 13px; }
.tab-tool:active { background: var(--accent-soft); border-color: var(--accent); }
.tab-tool .tool-icon { font-size: 20px; }

/* the bottom toolbar in the thumb zone */
.tab-bar { position: fixed; inset-inline: 0; bottom: 0; height: var(--tab-bar-h); padding: 6px 12px calc(6px + env(safe-area-inset-bottom, 0px)); z-index: 45;
  display: flex; align-items: center; justify-content: space-between; gap: 8px; background: var(--panel); border-top: 1px solid var(--line); }
.tab-bar-main, .tab-bar-view { display: flex; align-items: center; gap: 4px; min-width: 0; }
.tab-b { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-width: 60px; min-height: 54px; padding: 2px 8px; border: 0; background: none; border-radius: 12px; color: #4a5567; }
.tab-b .tab-i { font-size: 19px; line-height: 1; }
.tab-b .tab-l { font-size: 11.5px; white-space: nowrap; }
.tab-b.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.tab-b.ok.done { color: var(--ok); background: #ecfdf3; }
.tab-b.done:not(.ok) { color: var(--warn); background: #fff7e6; }
.tab-z { min-width: 46px; min-height: 46px; border: 1px solid var(--line); background: var(--panel); border-radius: 12px; font-size: 17px; padding: 0 10px; white-space: nowrap; }
.tab-z:active, .tab-b:active { background: var(--panel-2); }
.tab-zv { font-size: 14px; font-variant-numeric: tabular-nums; border-color: transparent; min-width: 62px; }
.tab-fit { font-size: 14px; }
body.tab-review .tab-bar .edit-only, body.tablet-mode:not(.tab-review) .tab-bar .review-only { display: none; }

/* tap-to-add: what is being placed */
.tab-placing { position: fixed; top: calc(var(--topbar-h) + 10px); left: 50%; transform: translateX(-50%); z-index: 46; display: flex; align-items: center; gap: 12px;
  background: var(--accent); color: #fff; padding: 6px 8px 6px 16px; border-radius: 999px; box-shadow: 0 6px 20px rgba(37, 99, 235, .35); font-weight: 600; max-width: calc(100vw - 32px); }
.tab-placing .btn { background: #fff; color: var(--accent); border: 0; }
body.tab-is-placing .canvas-scroll .design-page { cursor: crosshair; outline: 2px dashed rgba(37, 99, 235, .5); }

/* the gestures, explained once */
.tab-hint { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 8; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 6px 16px;
  max-width: calc(100% - 32px); background: rgba(15, 23, 42, .88); color: #fff; padding: 8px 8px 8px 18px; border-radius: 22px; font-size: 13px; }
.tab-hint { transition: opacity .4s; }
.tab-hint.fade { opacity: 0; }
.tab-hint button { border: 0; background: rgba(255, 255, 255, .15); color: #fff; border-radius: 50%; min-width: 36px; min-height: 36px; }

/* review: read-only */
body.tab-review .side-end { display: none !important; }
body.tab-review .sel-layer .handle { display: none; }
body.tab-review .side-start .tabs .tab:not([data-tab="pages"]) { display: none; }
body.tab-review .side-start .btn-row { display: none; }

/* comment pins (in the page, counter-scaled to stay 28 px) */
.tab-pin { position: absolute; z-index: 6; width: 28px; height: 28px; padding: 0; border: 2px solid #fff; border-radius: 50% 50% 50% 4px; background: #f59e0b; color: #fff;
  font: 700 12px/1 'Heebo', system-ui, sans-serif; box-shadow: 0 2px 6px rgba(0, 0, 0, .3); cursor: pointer; pointer-events: auto; }
.tab-pin::before { content: ''; position: absolute; inset: -8px; }
.tab-st { margin: 6px 0 10px; padding: 8px 12px; border-radius: 10px; background: var(--panel-2); color: var(--muted); font-weight: 600; }
.tab-st.ok { background: #ecfdf3; color: var(--ok); }
.tab-st.warn { background: #fff7e6; color: var(--warn); }
.tab-c-list { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.tab-c { display: flex; align-items: flex-start; gap: 10px; padding: 10px; border: 1px solid var(--line); border-radius: 12px; }
.tab-c.done { opacity: .6; }
.tab-c.done .tab-c-text { text-decoration: line-through; }
.tab-c-n { flex: none; width: 26px; height: 26px; border-radius: 50% 50% 50% 4px; background: #f59e0b; color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 12px; }
.tab-c.done .tab-c-n { background: #94a3b8; }
.tab-c-body { flex: 1; min-width: 0; }
.tab-c-meta { font-size: 12px; color: var(--muted); }
.tab-c-text { margin-top: 3px; white-space: pre-wrap; overflow-wrap: anywhere; }
.tab-c .btn { flex: none; }
.tab-c-empty { padding: 10px 2px; }
.tab-c-add { display: flex; flex-direction: column; gap: 6px; }
.tab-c-add label { font-weight: 600; font-size: 13px; }
.tab-c-add textarea { width: 100%; min-height: 64px; font-size: 16px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; resize: vertical; }
.tab-c-add .btn { align-self: flex-start; min-height: 44px; }

/* portrait (narrow tablet): labels shrink, the desktop note becomes an icon */
@media (max-width: 900px) {
  body.tablet-mode .tab-desk { font-size: 0; }
  body.tablet-mode .tab-desk span { font-size: 18px; }
  body.tablet-mode .tpl-switch { max-width: 130px; }
  body.tablet-mode .brand-logo { height: 24px; }
  .tab-fit { display: none; }
  .tab-b { min-width: 50px; padding: 2px 4px; }
  .tab-bar { padding-inline: 6px; }
  .tab-switch button { padding: 0 10px; }
  body.tablet-mode .topbar .lang-btn { display: none; }
  body.tablet-mode .topbar .tb.primary { font-size: 0; }
  body.tablet-mode .topbar .tb.primary::before { content: '👁'; font-size: 17px; }
}
@media (max-width: 700px) {
  .tab-b .tab-l { display: none; }
}

/* ==================================================================================================
   M6: the management screen on a tablet: a card per template instead of the wide table
   ================================================================================================== */
@media (max-width: 1023px) {
  .mwrap { padding: 16px 16px 60px; }
  .mfilters { flex-wrap: wrap; }
  .mfilters select { width: auto; flex: 1 1 200px; }
  .mtable-wrap { background: none; border: 0; overflow: visible; }
  #tpl-table, #tpl-table tbody { display: block; }
  #tpl-table thead { display: none; }
  #tpl-table tr { display: grid; grid-template-columns: auto 1fr 1fr; grid-template-areas: "id name name" "cust cust cust" "pages ph upd" "ver ver ver" "act act act";
    gap: 8px 12px; padding: 14px 16px; margin-bottom: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
  #tpl-table tr:hover { background: var(--panel); }
  #tpl-table td { display: block; padding: 0; border: 0; min-width: 0; }
  #tpl-table .t-id { grid-area: id; font-size: 15px; }
  #tpl-table .t-name { grid-area: name; }
  #tpl-table .t-name a { font-size: 15px; }
  #tpl-table .t-name small { max-width: none; white-space: normal; }
  #tpl-table .t-cust { grid-area: cust; }
  #tpl-table .t-pages { grid-area: pages; }
  #tpl-table .t-ph { grid-area: ph; }
  #tpl-table .t-upd { grid-area: upd; }
  #tpl-table .t-ver { grid-area: ver; }
  #tpl-table td[data-label]::before { content: attr(data-label); display: block; font-size: 11.5px; color: var(--muted); margin-bottom: 2px; }
  #tpl-table .t-actions { grid-area: act; display: flex; gap: 8px; text-align: start; padding-top: 6px; border-top: 1px solid var(--line-2); }
  #tpl-table .t-actions .btn { min-height: 44px; padding: 8px 16px; font-size: 14px; display: inline-flex; align-items: center; justify-content: center; }
  #tpl-table .t-actions > a.btn { flex: 1; background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
  #tpl-table .t-actions .more .btn { min-width: 48px; }
  .role-customer #tpl-table tr { grid-template-areas: "id name name" "pages ph upd" "ver ver ver" "act act act"; }
  .tag.add { min-height: 32px; }
}
/* 1024-1279 (an iPad in landscape, a small laptop): the table stays, and its actions column stays in view
   at the end of the scrolling table */
@media (min-width: 1024px) and (max-width: 1279px) {
  #tpl-table .t-actions, #tpl-table th:last-child { position: sticky; inset-inline-end: 0; background: var(--panel); box-shadow: 8px 0 12px -10px rgba(20, 30, 50, .25); z-index: 1; }
  #tpl-table tbody tr:hover .t-actions { background: #fafbfd; }
  #tpl-table .t-name small { max-width: 260px; }
}

/* ==================================================================================================
   Touch / pen drag from the data tree (js/treedrag.js): long press lifts the field, a ghost follows the
   finger; in tablet mode the sheet is lowered to a strip (body.tab-peek) while the finger is over the page.
   ================================================================================================== */
.dt-row { -webkit-touch-callout: none; }
.dt-row.dt-lifted { background: var(--accent-soft); box-shadow: inset 0 0 0 2px var(--accent); }
.dt-ghost { position: fixed; left: 0; top: 0; z-index: 1000; pointer-events: none; display: flex; flex-direction: column; gap: 1px;
  margin: -64px 0 0 -60px; min-width: 120px; max-width: 240px; padding: 6px 12px; border-radius: 12px; background: var(--panel); color: var(--text, #1f2937);
  border: 2px solid #94a3b8; box-shadow: 0 8px 24px rgba(20, 30, 50, .25); font-size: 14px; opacity: .95; white-space: nowrap; }
.dt-ghost b { overflow: hidden; text-overflow: ellipsis; }
.dt-ghost small { color: var(--muted); font-size: 12px; }
.dt-ghost.ok { border-color: #16a34a; }
.dt-ghost.ok small { color: #15803d; }
.dt-ghost-i { color: var(--accent); }
body.dt-touch-drag, body.dt-touch-drag * { user-select: none; -webkit-user-select: none; }
body.tablet-mode.tab-peek[data-sheet] .canvas-scroll { padding-bottom: var(--peek-pad); }
