/* Phone & installed-app polish. Loaded last: touch comfort, safe areas, the install sheet. */

/* ---- touch behaviour ---------------------------------------------------- */
html { -webkit-tap-highlight-color: transparent; scroll-padding-top: calc(var(--topbar-h, 60px) + 12px); }
body { overscroll-behavior-y: none; min-height: 100dvh; }
a, button, summary, label, [role="button"] { touch-action: manipulation; }

@media (hover: none) and (pointer: coarse) {
  /* iOS zooms the page when a field's text is under 16px */
  input, select, textarea { font-size: 16px !important; }
  /* fingers need 44px */
  .btn--sm { min-height: 44px; padding: 8px 16px; }
  .icon-btn { width: 44px; height: 44px; }
  .chip-btn { min-height: 40px; }
  .langs a { min-height: 36px; }
  .menu__panel a, .menu__panel button { min-height: 48px; }
  .table td, .table th { padding-top: 12px; padding-bottom: 12px; }
}

/* ---- safe areas (notch, home indicator) ---------------------------------- */
.topbar { padding-top: env(safe-area-inset-top, 0px); height: calc(var(--topbar-h) + env(safe-area-inset-top, 0px)); }
.topbar__back { flex: none; }
.topbar__back svg { width: 22px; height: 22px; }

/* ---- phones --------------------------------------------------------------- */
@media (max-width: 699px) {
  :root { --topbar-h: 56px; }
  .frame { padding-top: var(--s3); gap: var(--s3); padding-left: max(10px, env(safe-area-inset-left, 0px)); padding-right: max(10px, env(safe-area-inset-right, 0px)); }
  .topbar__brand img { width: 38px; height: 38px; }
  .topbar__end { gap: 6px; }
  /* the account menu becomes a sheet that never leaves the screen */
  .menu__panel { position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top, 0px) + 6px); right: 8px; left: 8px; width: auto; max-height: calc(100dvh - var(--topbar-h) - 90px); overflow-y: auto; }
  .card { border-radius: 16px; }
  h1 { font-size: var(--fs-xl); }
  .row--between { flex-wrap: wrap; gap: var(--s2); }
  .table-wrap { margin-inline: calc(-1 * var(--s3)); padding-inline: var(--s3); overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .bottomnav a { min-height: 58px; }
  .bottomnav a span:not(.nav-badge) { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 2px; }
  .bottomnav a[aria-current="page"]::before { content: ""; position: absolute; top: 0; left: 28%; right: 28%; height: 3px; border-radius: 0 0 3px 3px; background: var(--leaf); }
}
.bottomnav a:active, .toptabs a:active, .side-nav a:active { background: var(--surface-2); }
.btn:active { transform: scale(.98); }

/* ---- installed app (no browser bar) --------------------------------------- */
@media (display-mode: standalone) {
  body { -webkit-user-select: none; user-select: none; }
  input, textarea, [contenteditable], .post__body, .comment__body, p, q, dd, code, pre { -webkit-user-select: text; user-select: text; }
  a { -webkit-touch-callout: none; }
}

/* ---- install sheet --------------------------------------------------------- */
.install {
  position: fixed; z-index: 60; left: 10px; right: 10px;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 10px 12px; align-items: center;
  padding: 14px 14px 14px 14px; border-radius: 20px;
  background: var(--surface); color: var(--text);
  border: 1px solid color-mix(in srgb, var(--leaf) 45%, var(--border));
  box-shadow: 0 18px 50px rgba(0, 0, 0, .5), 0 0 0 1px rgba(0, 0, 0, .08);
  transform: translateY(140%); opacity: 0; transition: transform .26s cubic-bezier(.2, .8, .2, 1), opacity .2s;
}
.install.is-in { transform: none; opacity: 1; }
.app .install { bottom: calc(70px + env(safe-area-inset-bottom, 0px)); }
.install__icon { width: 52px; height: 52px; border-radius: 13px; box-shadow: 0 2px 8px rgba(0, 0, 0, .35); }
.install__text { display: flex; flex-direction: column; gap: 2px; min-width: 0; padding-right: 30px; }
.install__text strong { font-family: var(--font-display); font-size: var(--fs-md); }
.install__sub { font-size: var(--fs-sm); color: var(--muted); line-height: 1.45; }
.install__key { display: inline-flex; align-items: center; gap: 4px; padding: 1px 7px; border-radius: 7px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text); font-weight: 600; white-space: nowrap; vertical-align: middle; }
.install__key svg { width: 16px; height: 16px; color: var(--blue); }
.install__actions { grid-column: 1 / -1; display: flex; }
.install__actions .btn { flex: 1; min-height: 46px; font-size: var(--fs-md); }
.install__actions .btn svg { width: 20px; height: 20px; }
.install__actions .btn[hidden] + .install__close { top: 8px; }
.install__close {
  position: absolute; top: 8px; right: 8px; width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; background: transparent; border: 0; color: var(--muted); font-size: 24px; line-height: 1; cursor: pointer;
}
.install__close:hover { background: var(--surface-2); color: var(--text); }
.install:has([data-install-go][hidden]) .install__actions { display: contents; }
@media (min-width: 700px) {
  .install, .app .install { left: auto; right: 20px; bottom: 20px; width: 380px; }
}
@media (prefers-reduced-motion: reduce) { .install { transition: none; } .btn:active { transform: none; } }

/* ---- tree on phones: as tall as the screen allows --------------------------- */
@media (max-width: 699px) {
  .tree__canvas { height: calc(100dvh - 270px); min-height: 360px; }
  .tree__chips { scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .tree__chips::-webkit-scrollbar { display: none; }
}
