/* ═════════════════════════════════════════════════════════
   PHONES AND TABLETS (≤900px): the daf view as a reading app.
   A title bar on top (the shelf, the daf's name, which opens a
   picker, the star, profile and settings), the daf's tools in a
   dock at the bottom, both tucked away while reading down the
   page. The explanation is a bottom sheet that rides on the
   keyboard. Loaded after style.css and ui.css, so it wins over
   both; the look (type, fields, the sheet's parts) is ui.css's.
   ═════════════════════════════════════════════════════════ */
@keyframes sheetUp { from { transform: translateY(100%); } }
@keyframes dockIn { from { opacity: 0; transform: translateY(18px); } }
@keyframes pickerIn { from { opacity: 0; transform: translateY(-8px); } }

@media (max-width: 900px) {
  :root {
    --hdr-h: calc(58px + env(safe-area-inset-top, 0px));
    --dock-h: 62px;
    --dock-gap: max(10px, env(safe-area-inset-bottom, 0px));
    --side-l: max(10px, env(safe-area-inset-left, 0px));
    --side-r: max(10px, env(safe-area-inset-right, 0px));
  }

  /* ─── Title bar ─── */
  #app-header {
    position: sticky; top: 0; z-index: 25;
    height: var(--hdr-h);
    padding: env(safe-area-inset-top, 0px) calc(var(--side-r) - 4px) 0 calc(var(--side-l) - 4px);
    gap: 0; flex-wrap: nowrap;
    background: rgba(250, 250, 247, 0.9);
    -webkit-backdrop-filter: saturate(1.4) blur(16px);
    backdrop-filter: saturate(1.4) blur(16px);
    border-bottom: 1px solid rgba(216, 216, 210, 0.85);
    transition: transform 0.34s var(--xp-ease);
  }
  body.bars-hidden #app-header,
  body:has(.vilna-page.vz-on) #app-header { transform: translateY(-100%); }   /* zoomed in: just the page */
  #app-header.picker-open { background: var(--card); }
  #app-header .brand-header {
    flex: none; display: grid; place-items: center;
    width: 44px; height: 44px;
    border-radius: 12px; color: var(--ink-soft);
  }
  #app-header .brand-header:active { background: var(--hover); }
  #app-header .brand-mark { display: none; }
  .hb-home {
    display: block; width: 24px; height: 24px;
    fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round;
  }
  .hb-daf {
    display: flex; align-items: center; gap: 7px;
    flex: 1; min-width: 0; height: 48px;
    margin: 0 2px; padding: 0 8px;
    border: 0; border-radius: 12px;
    background: transparent; color: var(--title);
    text-align: left; cursor: pointer;
    transition: background 0.15s;
  }
  .hb-daf:active, .picker-open .hb-daf { background: var(--hover); scale: 1; }
  .hb-daf-text { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
  .hb-daf-en {
    font: 600 17px/1.1 'Fraunces', Georgia, serif;
    font-variation-settings: "opsz" 36;
    letter-spacing: -0.01em;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .hb-daf-he { font: 500 13.5px/1 'Frank Ruhl Libre', serif; color: var(--brass); white-space: nowrap; }
  .hb-daf-he:empty { display: none; }
  .hb-chev {
    flex: none; width: 12px; height: 12px;
    fill: none; stroke: var(--muted); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round;
    transition: transform 0.25s var(--xp-ease);
  }
  .picker-open .hb-chev { transform: rotate(180deg); }
  .hb-star-slot { display: contents; }
  #app-header .bookmark-btn { flex: none; width: 44px; height: 44px; margin: 0; }
  /* Profile, account and settings: icons */
  #app-header .header-profile {
    flex: none; display: grid; place-items: center;
    width: 44px; height: 44px; margin: 0;
    border-radius: 12px; color: var(--muted);
  }
  .hp-icon { display: block; width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; }
  .hp-text { display: none; }
  #app-header .header-feedback { flex: none; min-height: 44px; padding: 0 10px; border-radius: 12px; font-size: 14px; }   /* phones: hidden (style.css), it's in Settings */
  #app-header .icon-btn, #app-header #settings-btn { flex: none; width: 44px; height: 44px; min-width: 0; }
  #app-header .icon-btn svg { width: 21px; height: 21px; }
  #app-header .account-control { flex: none; }
  .account-sign-in {
    width: 44px; height: 44px; padding: 0; justify-content: center;
    border: 0; border-radius: 50%; background: transparent; color: var(--muted);
  }
  .account-sign-in span { display: none; }
  .account-sign-in svg { width: 21px; height: 21px; }
  /* Away from a daf (Chassidus, profile, concepts): the wordmark, no daf title */
  #app-header.header-minimal .hb-home,
  #app-header.header-minimal .hb-daf,
  #app-header.header-minimal .bookmark-btn { display: none; }
  #app-header.header-minimal .brand-header { width: auto; margin: 0 auto 0 6px; padding: 0 6px; font-size: 19px; }
  #app-header.header-minimal .brand-mark { display: block; }

  /* ─── The daf picker: drops down from the title ─── */
  #app-header #picker { display: none; }
  #app-header.picker-open #picker {
    position: absolute; top: 100%; left: 0; right: 0;
    display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 10px;
    width: auto;
    padding: 14px var(--side-r) 18px var(--side-l);
    background: var(--card);
    border-bottom: 1px solid var(--rule);
    border-radius: 0 0 18px 18px;
    box-shadow: 0 22px 36px -22px rgba(20, 26, 40, 0.45);
    animation: pickerIn 0.28s var(--xp-ease);
  }
  .picker-find {
    grid-column: 1 / -1;
    display: flex; align-items: center; gap: 10px;
    height: 48px; padding: 0 14px;
    border: 1px solid var(--rule); border-radius: 12px;
    background: var(--ui-surface, var(--bg));
    transition: border-color 0.15s, box-shadow 0.15s;
  }
  .picker-find:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--xp-accent-soft); }
  .picker-find svg { flex: none; width: 15px; height: 15px; fill: none; stroke: var(--muted); stroke-width: 1.6; }
  #app-header #picker .picker-find input {
    flex: 1; min-width: 0; width: auto; height: 100%; min-height: 0; padding: 0;
    border: 0; border-radius: 0; outline: 0; background: none;
    font: 16px var(--ui-font, sans-serif); color: var(--ink);   /* 16px: iOS doesn't zoom in */
  }
  .picker-find input::placeholder { color: var(--muted); }
  .picker-find-msg:not([hidden]) {
    display: block; grid-column: 1 / -1;
    margin-top: -2px; padding: 2px 10px;
    border-left: 3px solid var(--brass);
    font: 14px/1.45 var(--ui-font, sans-serif); color: var(--ink-soft);
  }
  .picker-or {
    display: flex; align-items: center; gap: 10px;
    grid-column: 1 / -1;
    margin: 2px 0 -2px;
    font: 13px/1 var(--ui-font, sans-serif); color: var(--muted);
  }
  .picker-or::before, .picker-or::after { content: ""; flex: 1; height: 1px; background: var(--rule); }
  #app-header #picker select, #app-header #picker #daf-number {
    height: 48px; padding: 0 12px;
    border-radius: 12px;
    font-size: 16px;
  }
  #app-header #picker select { grid-column: 1 / -1; width: 100%; min-width: 0; }
  #app-header #picker #daf-number { width: 100%; min-width: 0; }
  #app-header .amud-toggle { height: 48px; border-radius: 12px; }
  #app-header .amud-btn { width: 48px; padding: 0; font-size: 16px; }
  #app-header #load-btn { height: 48px; padding: 0 20px; border-radius: 12px; font-size: 16px; font-weight: 600; }
  .lb-wide { display: none; }
  .lb-phone { display: inline; }
  #app-header #daf-hint { display: block; grid-column: 1 / -1; margin: -4px 0 0 2px; font-size: 12.5px; color: var(--muted); }
  #app-header #multi-toggle { display: none; }
  .picker-scrim:not([hidden]) {
    display: block;
    position: fixed; inset: 0; z-index: 24;
    background: rgba(15, 20, 32, 0.3);
    animation: fadeIn 0.2s;
  }

  /* ─── The daf view ─── */
  main#app-main {
    padding-top: 12px;
    padding-left: max(var(--main-pad, 2rem), env(safe-area-inset-left, 0px));
    padding-right: max(var(--main-pad, 2rem), env(safe-area-inset-right, 0px));
    padding-bottom: calc(var(--dock-h) + var(--dock-gap) + 24px);
  }
  #daf-header { display: flex; align-items: center; flex-wrap: nowrap; gap: 8px; margin: 0 0 14px; }
  #daf-header h2 { display: none; }
  #daf-actions { flex: 1; min-width: 0; flex-direction: row; flex-wrap: nowrap; gap: 8px; }
  #daf-actions > .view-switch { flex: 1; width: auto; }
  .view-switch .view-btn { min-height: 38px; }
  #summary-btn, #logic-chain-btn, #iyun-btn, #daf-nav { display: none !important; }   /* in the dock */
  .printed-link:not([hidden]) {
    flex: none; display: grid; place-items: center;
    width: 44px; height: 44px; padding: 0;
    border-radius: 10px; color: var(--ink-soft);
  }
  .pl-icon { display: block; width: 21px; height: 21px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linejoin: round; stroke-linecap: round; }
  .pl-text { display: none; }
  /* The column pager's tabs stick under the title bar, and move up with it */
  .dp-tabs { top: var(--hdr-h); transition: top 0.34s var(--xp-ease); }
  body.bars-hidden .dp-tabs { top: 0; }

  /* What came before on the previous daf: a two-line preview */
  .context-section {
    padding: 12px 14px 8px;
    border: 1px solid rgba(var(--brass-rgb), 0.4);
    border-radius: 12px;
    background: rgba(var(--brass-rgb), 0.1);
  }
  .context-header { gap: 6px 8px; font-size: 0.8rem; }
  .context-jump { min-height: 34px; padding: 0 12px; border-radius: 999px; font-size: 0.78rem; }
  .context-section.is-collapsed .context-body {
    max-height: 3.5em; overflow: hidden;
    -webkit-mask-image: linear-gradient(#000 30%, transparent);
    mask-image: linear-gradient(#000 30%, transparent);
  }
  .context-more {
    display: block; margin: 2px auto 0; min-height: 36px; padding: 0 16px;
    border: 0; background: none; cursor: pointer;
    font: 600 0.82rem var(--ui-font, sans-serif); color: var(--accent);
  }
  .context-after { padding-bottom: 12px; }

  /* More meforshim */
  .mm-click { display: none; }
  .mm-tap { display: inline; }
  .more-mef-card { border-radius: 10px; margin-bottom: 0.4rem; }
  .more-mef-card > summary { min-height: 48px; align-items: center; padding: 0.4rem 0.9rem; }

  /* The end of the daf: go on to the next amud */
  #daf-nav-bottom { gap: 10px; }
  #daf-nav-bottom .daf-nav-btn { flex: 1; min-height: 50px; padding: 0 0.75rem; border-radius: 12px; font-size: 0.95rem; }
  #daf-nav-bottom #daf-next-bottom:not(:disabled) { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }

  /* ─── The dock ─── */
  .daf-dock {
    position: fixed; z-index: 26;
    left: var(--side-l); right: var(--side-r); bottom: var(--dock-gap);
    display: flex; gap: 2px;
    max-width: 560px; height: var(--dock-h);
    margin: 0 auto; padding: 5px;
    background: rgba(250, 250, 247, 0.9);
    -webkit-backdrop-filter: saturate(1.4) blur(18px);
    backdrop-filter: saturate(1.4) blur(18px);
    border: 1px solid rgba(216, 216, 210, 0.9);
    border-radius: 22px;
    box-shadow: 0 14px 34px -16px rgba(20, 26, 40, 0.45), 0 2px 8px rgba(20, 26, 40, 0.06);
    transition: transform 0.36s var(--xp-ease);
    animation: dockIn 0.4s var(--xp-ease) both;
  }
  #app-main.app-hidden ~ .daf-dock { display: none; }
  body.bars-hidden .daf-dock,
  body:has(.vilna-page.vz-on) .daf-dock,
  body.multi-select-mode .daf-dock,
  body:has(.multi-bar:not(.multi-bar-hidden)) .daf-dock,
  #app-header.picker-open ~ .daf-dock {
    transform: translateY(calc(100% + var(--dock-gap) + 12px));
  }
  .dock-btn {
    flex: 1; min-width: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
    padding: 0 2px;
    border: 0; border-radius: 17px;
    background: transparent; color: var(--ink-soft);
    font: 500 11px/1 var(--ui-font, sans-serif);
    cursor: pointer;
  }
  .dock-btn svg {
    width: 22px; height: 22px;
    fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round;
  }
  .dock-label { max-width: 100%; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .dock-nav { flex: 0.8; }
  .dock-nav .dock-label { font-size: 12.5px; font-weight: 600; font-variant-numeric: lining-nums tabular-nums; }
  .dock-btn:active { background: var(--hover); }
  .dock-btn:disabled { opacity: 0.3; cursor: default; }
  .dock-btn:disabled:active { background: transparent; }
  .dock-multi[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
  .dock-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

  /* Select mode: the selection bar takes the dock's place */
  .multi-bar {
    left: var(--side-l); right: var(--side-r); bottom: var(--dock-gap);
    transform: none;
    max-width: 560px; height: var(--dock-h);
    margin: 0 auto; padding: 7px 7px 7px 18px; gap: 8px;
    border-radius: 22px;
    box-shadow: 0 14px 34px -16px rgba(20, 26, 40, 0.45), 0 2px 8px rgba(20, 26, 40, 0.06);
    animation: dockIn 0.32s var(--xp-ease) both;
  }
  .multi-bar-count, .multi-bar-hint { margin-right: auto; font-size: 0.95rem; white-space: nowrap; }
  .multi-bar-clear, .multi-bar-explain { height: 46px; padding: 0 16px; font-size: 0.95rem; white-space: nowrap; }

  /* Minimized explanation: a pill above the dock */
  #minimized-pill {
    left: 0; right: 0; bottom: calc(var(--dock-gap) + var(--dock-h) + 10px);
    width: fit-content; max-width: calc(100vw - 24px); min-height: 48px;
    margin: 0 auto; padding: 6px 6px 6px 16px;
    font-size: 0.9rem;
    transition: bottom 0.36s var(--xp-ease), border-color 0.15s, box-shadow 0.15s;
  }
  body.bars-hidden #minimized-pill,
  body:has(.vilna-page.vz-on) #minimized-pill,
  #app-main.app-hidden ~ #minimized-pill { bottom: var(--dock-gap); }
  body:has(.multi-bar:not(.multi-bar-hidden)) #minimized-pill { bottom: calc(var(--dock-gap) + var(--dock-h) + 10px); }
  .pill-close { display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border-radius: 50%; font-size: 1.3rem; }
  .pill-close:active { background: var(--hover); }

  /* The explanation sheet counts what's left; no floating badge on the page */
  .free-tier-badge { display: none !important; }
  .xp-quota { display: inline; }

  /* ─── The explanation sheet ─── */
  #modal.explain-panel .modal-box {
    bottom: var(--kb, 0px);
    height: min(90dvh, calc(var(--vvh, 100dvh) - 12px));
    max-width: 760px; margin: 0 auto;
  }
  /* A handle you can actually grab, and a header you can pull down */
  #modal.explain-panel .xp-handle {
    top: 0; left: 50%; width: 120px; height: 24px; margin-left: -60px;
    background: none; cursor: grab; touch-action: none;
  }
  #modal.explain-panel .xp-handle::before {
    content: ""; position: absolute; top: 8px; left: 50%;
    width: 40px; height: 5px; margin-left: -20px;
    border-radius: 3px; background: var(--rule);
  }
  #modal.explain-panel .modal-header { touch-action: none; }
  /* Typing: the keyboard takes half the screen, so give the conversation the rest */
  html.kb-open #modal.explain-panel .modal-source,
  html.kb-open #modal.explain-panel .xp-styles { display: none; }
  html.kb-open #modal.explain-panel .followup-form { margin-bottom: 10px; }

  /* ─── Settings ─── */
  #settings-modal .modal-close { width: 44px; height: 44px; }
  .settings-body input:not([type="checkbox"]), .key-row button { min-height: 46px; font-size: 16px; }
  .key-status { flex-wrap: wrap; gap: 6px 12px; }
  .key-remove { min-height: 40px; }
}

/* ─── Home and Chassidus on a phone ─── */
@media (max-width: 760px) {
  /* Small links, finger-sized targets: padding out, the same margin back in */
  .sh-top .sh-toplink { padding: 11px 5px; margin: -11px -5px; }
  .sh-dice { width: 36px; height: 36px; }
  /* The reader's bar: the two pickers, then language, Overview and Summary, then prev/next */
  .sf-bar { gap: 8px; }
  .sf-pick select { min-height: 44px; font-size: 16px; }
  .sf-bar > .sf-lang { flex: 1 1 auto; }
  .sf-lang .view-btn { flex: 1; min-height: 38px; padding: 0 12px; white-space: nowrap; }
  .sf-bar > .daf-nav-btn { min-height: 44px; }
  .sf-bar > .sf-nav, .sf-foot .sf-nav { flex: 1 1 100%; }
  .sf-nav .daf-nav-btn { flex: 1; min-height: 44px; }
  /* A long concept wraps inside its chip instead of running off the screen */
  .sf-concept { max-width: 100%; min-height: 36px; align-items: center; white-space: normal; text-align: center; }
}
@media (hover: none) {
  .sh-card-x { width: 36px; height: 36px; }
}

@media (max-width: 400px) {
  .dl-long { display: none; }
  .dl-short { display: inline; }
}

@media (max-width: 380px) {
  .mb-together { display: none; }
  .multi-bar-clear, .multi-bar-explain { padding: 0 13px; }
  /* Room for "Avodah Zarah 36b" beside the icons */
  #app-header .brand-header, #app-header .bookmark-btn, #app-header .header-profile,
  #app-header .icon-btn, #app-header #settings-btn, .account-sign-in { width: 40px; }
  #app-header.header-minimal .brand-header { width: auto; }
  .hb-daf { padding: 0 6px; gap: 5px; }
  .hb-daf-en { font-size: 16px; }
}

@media (max-width: 600px) {
  main#app-main { --main-pad: 1rem; }
  /* Settings come up as a sheet */
  #settings-modal { align-items: flex-end; }
  #settings-modal .modal-box {
    width: 100%; max-width: none;
    max-height: min(92vh, 92dvh);
    border-radius: 20px 20px 0 0; border-bottom: 0;
    animation: sheetUp 0.4s var(--xp-ease) both;
  }
  .settings-body { overscroll-behavior: contain; padding-bottom: max(18px, calc(env(safe-area-inset-bottom, 0px) + 8px)); }
}

/* A phone on its side: the explanation is a panel down the right, not a
   short sheet, and the title bar and dock slim down. */
@media (max-width: 900px) and (max-height: 500px) {
  :root { --hdr-h: calc(50px + env(safe-area-inset-top, 0px)); --dock-h: 54px; --dock-gap: max(6px, env(safe-area-inset-bottom, 0px)); }
  .hb-daf { height: 42px; }
  .hb-daf-text { flex-direction: row; align-items: baseline; gap: 10px; }
  #modal.explain-panel .modal-box {
    inset: 0 0 0 auto;
    bottom: var(--kb, 0px);
    width: min(64vw, 580px); height: auto; max-width: none; margin: 0;
    padding-right: env(safe-area-inset-right, 0px);
    border-radius: 18px 0 0 18px; border-bottom: 1px solid var(--ui-line, var(--rule));
    animation-name: xpSlideIn;
  }
  #modal.explain-panel .xp-handle { display: none; }
  #modal.explain-panel .modal-header { padding-top: 12px; padding-bottom: 10px; touch-action: auto; }
  #modal.explain-panel .modal-source { max-height: 4.4rem; margin-top: 10px; }
  #settings-modal .modal-box { max-height: 94vh; max-height: 94dvh; }
}

@media (prefers-reduced-motion: reduce) {
  #app-header, .daf-dock, .multi-bar, #minimized-pill, .dp-tabs, #app-header.picker-open #picker, .picker-scrim {
    transition: none !important; animation: none !important;
  }
}
