/* A hidden find bar must not intercept touches on toolbar buttons. */
#findbar { pointer-events: none; }
body.finding #findbar { pointer-events: auto; }

/* Digital Ink mobile layout: preserve the desktop paper-folder metaphor,
   while making touch gestures, overlays and software keyboards usable. */
@media (max-width: 680px) {
  html, body { width: 100%; max-width: 100%; overscroll-behavior: none; }
  #input {
    left: 0; top: 0; width: 1px; height: 1px;
    font-size: 16px; clip-path: inset(50%); pointer-events: none;
  }
  #hudL {
    top: calc(6px + env(safe-area-inset-top, 0px));
    left: 8px; width: calc(100% - 16px); max-width: none;
    justify-content: flex-start; gap: 3px;
  }
  #hudL button { min-height: 38px; min-width: 42px; padding-inline: 8px; background: rgba(253,250,242,.82); }
  #hudL #title { display: none; }
  #hudR {
    top: calc(48px + env(safe-area-inset-top, 0px));
    left: 6px; right: 6px; text-align: left;
  }
  #hudR #stats { display: none; }
  #hudR > div:last-child {
    display: flex; align-items: center; gap: 2px;
    overflow-x: auto; overflow-y: hidden; max-width: 100%;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  #hudR > div:last-child::-webkit-scrollbar { display: none; }
  #hudR button { flex: none; min-height: 35px; min-width: 38px; background: rgba(253,250,242,.8); }
  #hudR #vol { flex: 0 0 46px; margin-right: 3px; }
  #findbar { width: calc(100% - 20px); max-width: 460px; top: calc(92px + env(safe-area-inset-top, 0px)); }
  #findInput { flex: 1; min-width: 0; width: 100%; font-size: 16px; min-height: 40px; }
  #hint { display: none; }

  /* Native-height dialogs: the old three-card column could extend beyond the viewport. */
  #savePanel, #panel {
    width: calc(100% - 20px); max-width: 510px;
    max-height: calc(100dvh - 54px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
    overflow-y: auto; overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 16px 13px 13px;
  }
  #savePanel .dr-tab, #panel .dr-tab { position: sticky; top: -16px; width: fit-content; margin-bottom: 4px; z-index: 1; }
  #savePanel .sp-cards { grid-template-columns: minmax(0,1fr); padding: 5px; gap: 8px; }
  #savePanel .sp-card { padding: 11px 12px; min-width: 0; transform: none; }
  #savePanel .sp-card:hover { transform: none; }
  #savePanel .sp-ic { display: none; }
  #savePanel .sp-card h3 { margin-bottom: 2px; }
  #savePanel .sp-name { white-space: normal; overflow-wrap: anywhere; font-size: 13px; }
  #savePanel .sp-meta { margin: 4px 0 7px; padding-top: 3px; }
  #savePanel .sp-row .sp-btn { min-height: 42px; flex: 1; }
  #savePanel .sp-head { position: sticky; top: 0; z-index: 2; background: #ead9b3; padding: 6px 0; }
  #savePanel .sp-foot { margin-bottom: 0; }
  #panel .help-sheet { max-height: none; padding: 14px; }
  #panel .sp-head { position: sticky; top: 0; z-index: 2; padding: 7px 0; background: #ead9b3; }
  #panel td:first-child { width: auto; white-space: normal; }
  #mechPanel { max-height: calc(100dvh - 40px); overflow: auto; overscroll-behavior: contain; }
  #mechPanel .mech-sheet { max-height: 58dvh; }

  /* Mobile folder: exactly the same page-and-pocket metaphor as desktop.
     The sheet's bottom edge physically enters the manila folder; none of
     the desktop's full-height empty gap survives at phone dimensions. */
  #drawer {
    --pocketH: clamp(208px, 31dvh, 286px);
    --folderEdge: calc(100dvh - var(--pocketH));
    --leafH: clamp(290px, calc(100dvh - var(--pocketH) - 95px), 480px);
    --leafW: min(76vw, calc(var(--leafH) * .70));
    background: radial-gradient(120% 94% at 50% 10%, #faf7ef, #eee5d5 69%, #e6d7b7);
  }
  #drawer .folder-back {
    display: block;
    position: absolute;
    left: 12px; width: calc(100% - 24px);
    top: calc(var(--folderEdge) - 78px);
    bottom: -10px;
    transform: none;
    border-radius: 14px 14px 0 0;
    background: linear-gradient(90deg, #d8c393 0%, #e9d7b2 15%, #edddbc 52%, #d9c392 100%);
    box-shadow: 0 0 0 1px #c7af7e, inset 0 9px 20px -15px rgba(65,46,25,.35),
      0 8px 22px rgba(71,49,26,.15);
  }
  /* Paper edges behind the active sheet make even a single saved page
     feel filed, rather than like a flat, full-width note card. */
  #drawer .folder-back::before, #drawer .folder-back::after {
    content: "";
    position: absolute;
    top: calc(114px - var(--leafH));
    left: 50%;
    width: var(--leafW);
    height: var(--leafH);
    background: #f3ede0;
    border: 1px solid rgba(108,83,50,.18);
    border-radius: 2px;
    box-shadow: 0 8px 20px rgba(86,57,27,.13);
    pointer-events: none;
  }
  #drawer .folder-back::before { transform: translateX(calc(-50% - 17px)) rotate(-3deg); }
  #drawer .folder-back::after {
    transform: translateX(calc(-50% + 17px)) rotate(3deg);
    background: #f9f5eb;
  }
  #drawer #shelf {
    position: absolute;
    top: max(calc(48px + env(safe-area-inset-top, 0px)),
      calc(var(--folderEdge) - var(--leafH) + 40px));
    left: 0; bottom: auto;
    width: 100%; height: calc(var(--leafH) + 60px);
    margin: 0;
    padding: 19px calc((100vw - var(--leafW)) / 2) 35px;
    display: flex;
    flex-direction: row;
    align-items: flex-start;
    gap: 0;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior: contain;
    scroll-snap-type: x mandatory;
    scroll-padding-inline: calc((100vw - var(--leafW)) / 2);
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x;
    scrollbar-width: none;
    perspective: 1000px;
    -webkit-mask-image: none;
    mask-image: none;
    z-index: 1;
  }
  #drawer #shelf::-webkit-scrollbar { display: none; }
  #drawer .leaf {
    flex: 0 0 var(--leafW);
    width: var(--leafW);
    height: var(--leafH);
    margin: 0 calc(var(--leafW) * -.18) 0 0;
    opacity: 1 !important;
    transform-origin: 50% 85%;
    border-radius: 2px;
    background: linear-gradient(95deg, #f3eddf 0%, #fdfaf2 7%, #fcf9f1 92%, #f4eddd 100%);
    box-shadow: 0 2px 3px rgba(70,47,25,.12),
      0 14px 30px rgba(80,56,20,.21), 0 0 0 1px rgba(87,64,39,.13);
    scroll-snap-align: center;
    transition: transform .16s ease-out, box-shadow .16s ease-out;
  }
  #drawer .leaf:last-child { margin-right: 0; }
  #drawer .leaf.center {
    box-shadow: 0 3px 4px rgba(80,56,20,.13), 0 20px 42px rgba(80,56,20,.28),
      0 0 0 1px rgba(87,64,39,.18);
  }
  #drawer .leaf.cur .pg {
    background: linear-gradient(90deg, transparent 5%, rgba(140,74,58,.35) 5%,
      rgba(140,74,58,.35) calc(5% + 1px), transparent calc(5% + 1px));
  }
  #drawer .leaf .pg {
    inset: 0;
    padding: 53px 20px 122px;
    font: clamp(12px, 3.2vw, 15px)/1.75 var(--type);
    color: #312b26;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    overflow: hidden;
    -webkit-mask-image: linear-gradient(#000 72%, transparent 94%);
    mask-image: linear-gradient(#000 72%, transparent 94%);
  }
  #drawer .leaf .stampd {
    top: 22px; right: 19px;
    direction: ltr;
    font: 10px/1.2 var(--type);
    letter-spacing: .03em;
  }
  #drawer .leaf .clip2 { display: block; top: -11px; height: 34px; width: 12px; }
  #drawer .leaf .acts, #drawer .leaf.center .acts {
    top: auto;
    left: 12px;
    right: 12px;
    bottom: 67px;
    justify-content: flex-end;
    gap: 7px;
    opacity: 0;
    pointer-events: none;
  }
  #drawer .leaf.center .acts {
    opacity: 1;
    pointer-events: auto;
  }
  #drawer .leaf .acts button {
    min-height: 42px; min-width: 54px;
    font-size: 12px; padding: 6px 10px;
    border-radius: 3px;
    background: rgba(254,251,244,.92);
  }
  #drawer .shelf-empty {
    margin: auto;
    padding: 20px;
    text-align: center;
    white-space: normal;
  }
  #drawer .pocket {
    position: absolute;
    top: auto;
    left: 0;
    bottom: 0;
    width: 100%;
    height: var(--pocketH);
    max-height: none;
    min-height: 0;
    padding: 16px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    border-radius: 18px 18px 0 0;
    transform: translateY(110%);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: linear-gradient(150deg, #eedfbb 0%, #e8d6ad 72%, #d9c394 100%);
    box-shadow: 0 -5px 15px rgba(80,56,20,.14), inset 0 2px 0 rgba(255,255,255,.55),
      0 0 0 1px var(--manila-edge);
    z-index: 4;
  }
  body.drawer #drawer .pocket {
    transform: translateY(0);
    transition: transform .32s cubic-bezier(.2,.82,.2,1);
  }
  #drawer .pocket::before { width: 72px; height: 11px; }
  #drawer .file-label { display: none; }
  #drawer .pocket-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 7px;
    align-items: center;
  }
  #drawer .dr-search { min-width: 0; grid-column: 1; grid-row: 1; }
  #drawer #search { font-size: 16px; min-height: 43px; }
  #drawer #dClose { min-width: 44px; min-height: 44px; grid-column: 2; grid-row: 1; }
  #drawer #tags { grid-column: 1 / -1; grid-row: 2; max-height: 55px; overflow-y: auto; }
  #drawer .dr-sub { grid-column: 1 / -1; white-space: normal; font-size: 11.5px; }
  #drawer #dNew, #drawer #dSave { min-height: 43px; }
  #drawer #dNew { grid-column: 1; }
  #drawer #dSave { grid-column: 2; }
  #drawer .pocket-hint {
    grid-column: 1 / -1;
    font-size: 10.5px;
    line-height: 1.3;
    text-align: center;
    white-space: normal;
    overflow: visible;
  }
  #drawer .desktop-archive-hint { display: none; }
  #drawer .mobile-archive-hint { display: inline; }

  /* The iPhone keyboard occupies the visual viewport, not always the layout
     viewport. When the user taps the paper, show a readable ink cursor and
     hide nonessential controls while maintaining the machine above the keys. */
  body.mobile-editing #caret::before {
    content: ""; display: block !important;
    position: absolute; left: 0; top: .06em;
    width: 2px; height: 1.18em; border-radius: 0;
    background: #15100c; box-shadow: 0 0 0 .35px rgba(0,0,0,.5);
    animation: blink .95s steps(1) infinite;
  }
  /* Topic selection is available even after typing, without desktop shortcuts.
     Position its input above the iOS keyboard; keep the folder readable. */
  #hudL #btnTopic { font-size: 11px; flex: 0 1 auto; max-width: 105px; }
  #topicPanel {
    top: calc(73px + env(safe-area-inset-top, 0px));
    max-height: calc(100dvh - 98px - env(safe-area-inset-top, 0px));
    transform: translate(-50%, -12px) scale(.98);
    overflow-y: auto; overscroll-behavior: contain;
  }
  body.topicing #topicPanel { transform: translate(-50%, 0) scale(1); }
  #topicPanel .topic-sheet { padding: 14px; }
  #topicChoices { max-height: 90px; }
  #drawer .topic-filter-row { width: 100%; min-height: 35px; display: flex; }
  #drawer .topic-filter-row label { flex: 0 0 auto; }
  #drawer #topicFilter { flex: 1; max-width: 100%; min-width: 0; }
  #drawer .leaf .topic-label { top: 10%; inset-inline-start: 7%; max-width: 79%; font-size: 11px; }
  #drawer .leaf .acts { gap: 5px; }
  #drawer .leaf .acts button { min-width: 46px; padding-inline: 7px; font-size: 11px; }
  body.mobile-keyboard #hudR { display: none; }
  body.mobile-keyboard #hint { display: none; }
  #toast { bottom: calc(12px + env(safe-area-inset-bottom, 0px)); max-width: calc(100vw - 24px); min-width: 0; padding: 10px; }
  #offlineStatus { bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  body.drawer #offlineStatus, body.saving #offlineStatus, body.panel #offlineStatus { display: none; }
}

@media (max-width: 360px) {
  #savePanel, #panel { padding-inline: 9px; }
  #drawer .pocket { padding-inline: 8px; }
  #drawer .leaf .pg { padding-inline: 16px; }
}


/* A touch-first help panel should not advertise keyboard-only shortcuts.
   Keep the complete shortcut reference on desktop and keyboard devices. */
.mobile-touch-guide { display: none; }
@media (max-width: 680px), (hover: none) and (pointer: coarse) {
  #panel .desktop-shortcuts, #savePanel kbd, #hint { display: none !important; }
  #panel .mobile-touch-guide { display: block; }
  #text { touch-action: none; }
  #paper { overscroll-behavior: contain; }
}

/* Archive hints are responsive: desktop keeps wheel/keyboard language. */
.mobile-archive-hint { display: none; }
