/* Oneiron on a phone.

   Every rule in this file lives inside a media query, so a desktop browser
   reads none of them and the layout there is exactly what it always was.
   Two questions are asked separately: how wide is the screen (that decides
   the layout), and is this a finger rather than a mouse (that decides how
   big things have to be and whether hovering can be relied on).

   The phone layout for every page is here rather than scattered through the
   other stylesheets, so there is one place to look when something is out of
   place on a small screen. */


/* ======================================================================
   A finger, whatever the screen size
   ====================================================================== */
@media (hover: none) {
  /* nothing should need hovering to be found */
  .row-actions { opacity: 1; }
  .drag-handle { opacity: 0.7; }
  .card-edit { opacity: 0.85; }
  .atk-lock, .atk-star { opacity: 0.7; }
  .portrait-box.has-image .portrait-hint { opacity: 0.9; }

  /* the grey flash Android paints over everything that is tapped */
  * { -webkit-tap-highlight-color: transparent; }
}


/* ======================================================================
   Every page, on a phone
   ====================================================================== */
@media (max-width: 760px) {

  /* iOS zooms the whole page in when a field smaller than 16px is tapped, and
     never quite zooms back out. So on a phone no field is under 16px. Each of
     these is a place that sets its own smaller size, and each has to be named
     here or its own rule wins. Fields already larger than 16px are left alone,
     because they are meant to be read at a glance. */
  input, textarea, select { font-size: 16px; }
  .chat-say textarea { font-size: 16px; }
  .entry-editor textarea { font-size: 16px; }
  .search-bar input { font-size: 16px; }
  .party-amount { font-size: 16px; }
  .rename-input { font-size: 16px; }
  .row .value,
  .row-name.custom,
  .attack-row input,
  .attack-row textarea,
  .slot input,
  .spell-row input[type="text"],
  .spell-row textarea,
  .spell-row input:not([type="checkbox"]) { font-size: 16px; }
  body .swal2-input,
  body .swal2-textarea,
  body .swal2-select,
  body .swal-form .swal2-input,
  body .swal-form .swal2-select { font-size: 16px; }

  /* and a double tap should not zoom either */
  button, a, label, summary,
  input, textarea, select,
  .top-btn, .chat-tab, .party-bubble, .rollable, .menu-button,
  .icon-btn, .btn, .add-row, .journal-header { touch-action: manipulation; }

  /* ---------- the bar across the top ---------- */
  .toolbar {
    top: 8px;
    left: 8px;
    right: 8px;
    gap: 6px;
    align-items: center;
  }
  /* The toolbar and the party sit over the page, and on a narrow screen the
     page scrolls right under them. Where that page is words — the journal,
     a character sheet, the list of players — the top of it is faded out, so
     the two do not read as one jumble of text. Over the map there is nothing
     to read through, so the map is left alone. */
  body.journal-mode::before,
  body.sheet-page::before,
  body.dm-page::before {
    content: "";
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    height: 118px;
    z-index: 1400;                /* under the party (1450) and the toolbar */
    pointer-events: none;
    background: linear-gradient(to bottom,
                #0d1017 0%, #0d1017 84%, rgba(13, 16, 23, 0) 100%);
  }
  body.dm-page::before { height: 64px; }
  .top-btn { height: 36px; padding: 0 12px; font-size: 13px; }
  /* the two map buttons keep their icon and drop their words */
  #addMarkerBtn .lbl, #viewModeToggleBtn .lbl { display: none; }
  .user-menu { min-width: 0; }
  #userBtn { min-width: 0; padding: 0 10px; }
  #userBtn .lbl {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .user-drop { min-width: 0; width: max-content; max-width: calc(100vw - 24px); }
  .user-drop a { white-space: normal; }

  /* who else is here: the party bubbles say it better, and there is no room */
  .people { display: none; }

  /* ---------- the party, as a strip that scrolls sideways ---------- */
  .party {
    top: 44px;
    left: 0;
    right: 0;
    flex-direction: row;
    gap: 12px;
    padding: 4px 10px 2px;         /* room for the ring round whoever's turn it is */
    overflow-x: auto;
    overflow-y: visible;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }
  .party::-webkit-scrollbar { display: none; }
  .party-seat { flex: 0 0 auto; }
  .party-stack { width: 42px; gap: 1px; }
  .party-bubble, .party-ring { width: 42px; height: 42px; flex-basis: 42px; }
  .party-face { left: 5px; top: 5px; width: 32px; height: 32px; }
  .party-letter { font-size: 14px; }
  .party-tag { max-width: 60px; font-size: 8.5px; }
  .party-lamp { width: 8px; height: 8px; }

  /* the card opens across the screen rather than beside a 44px bubble */
  .party-card {
    position: fixed;
    left: 10px;
    right: 10px;
    top: 108px;
    width: auto;
    animation: none;
  }
  .party-amount { height: 36px; }
  .party-hurt, .party-heal { width: 36px; height: 36px; flex-basis: 36px; }
  .party-pip { width: 18px; height: 18px; }
  .party-rest, .party-sheet { height: 34px; }
  .party-fight { flex: 0 0 auto; width: 58px; align-self: flex-start; }

  /* ---------- the map ---------- */
  /* Pinching zooms, so the two buttons are just something in the way of the
     map. They come back on a desktop, where there is nothing to pinch. */
  .leaflet-control-zoom { display: none; }
  .leaflet-popup-content { max-width: calc(100vw - 80px); }
  .leaflet-container a.leaflet-popup-close-button { width: 32px; height: 32px; font-size: 20px; }
  .mk-body { max-height: 42vh; }
  .leaflet-bottom.leaflet-right { bottom: 58px; }

  .place-hint {
    top: 104px;
    left: 10px;
    right: 10px;
    transform: none;
    white-space: normal;
    justify-content: space-between;
    font-size: 13px;
    animation: none;
  }

  /* no legend on a phone: the corner is the dice tray's */
  #menu { display: none; }

  /* ---------- the journal ---------- */
  #journalView { padding: 108px 10px 84px; }
  #journalView > h1 { font-size: 26px; }
  #journalView > h1::after { margin: 10px auto 0; }
  .journal-section { padding: 12px 10px; }
  .journal-section h2 { font-size: 15px; }
  .section-btn { padding: 0 8px; font-size: 11px; }
  .search-bar { margin-bottom: 14px; }
  /* A deep row has a handle, an arrow, a name and up to six buttons to fit
     across 390px, so everything but the name is trimmed to the bone. */
  .journal-header { padding: 9px 4px; gap: 2px; }
  .item-label { font-size: 14px; }
  .drag-handle { width: 14px; margin-right: 0; }
  .row-actions { gap: 0; margin-right: -4px; }
  .row-actions .icon-btn, .mk-actions .icon-btn { width: 30px; height: 30px; }
  .row-actions .icon-btn svg, .mk-actions .icon-btn svg { width: 16px; height: 16px; }
  .journal-list, .journal-container > ul.journal-list { padding-left: 8px; }
  .journal-entry-content, .journal-content { padding: 12px 12px; }
  .entry-editor textarea { min-height: 200px; }
  .editor-bar { flex-wrap: wrap; gap: 8px; }
  .editor-bar .hint { width: 100%; margin-right: 0; order: 2; }
  .add-row { padding: 12px; }
  .md img, .content-image { max-width: 100%; height: auto; }

  /* ---------- the chat, as a screen of its own ----------
     Open, it is the whole screen and nothing behind it shows or moves: the
     page underneath is pinned while it is up. The panel is told to fill its
     container rather than to be a screen tall, so there is only one height
     being worked out and the two can never disagree. */
  /* Folded away, the chat still runs the full width of the screen so that a
     message floating up can use it. Only the tab and the messages themselves
     take taps, or the strip would quietly swallow anything under it — the
     dice in the far corner, or a pin near the bottom of the map. */
  .chat { left: 0; right: 0; bottom: 0; align-items: stretch; gap: 8px; }
  .chat:not(.is-open) { pointer-events: none; }
  .chat:not(.is-open) .chat-tab { pointer-events: auto; }
  .chat.is-open {
    top: 0;
    bottom: auto;
    height: 100dvh;               /* chat.js trims this to fit the keyboard */
    gap: 0;
    background: #0d1017;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .chat-panel {
    flex: 1 1 auto;
    width: 100%;
    max-width: none;
    height: auto;
    min-height: 0;
    max-height: none;
    border: 0;
    border-radius: 0;
    background: #151a23;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }
  .chat-toast {
    background: #151a23;
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
  }

  .chat-head { padding: 13px 8px 13px 16px; }
  .chat-fold { padding: 9px; }
  .chat-fold svg { width: 22px; height: 22px; }

  /* the starred rows stay on one line and slide sideways, so they cannot
     push the conversation off the bottom of the screen */
  .chat-macros {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    padding: 9px 12px;
    gap: 7px;
  }
  .chat-macros::-webkit-scrollbar { display: none; }
  .chat-macro { flex: 0 0 auto; padding: 7px 14px; font-size: 13px; }

  .chat-log { padding: 12px 12px 4px; overscroll-behavior: contain; }
  .chat-hint { padding: 10px 12px; max-height: 34dvh; overflow-y: auto; }
  .chat-hint-eg button { padding: 6px 12px; font-size: 13px; }
  .chat-say { padding: 10px 12px; gap: 8px; }
  .chat-say textarea { min-height: 44px; padding: 11px 12px; }
  .chat-say button { width: 44px; height: 44px; flex: 0 0 44px; }
  .attack-answer { padding: 8px 18px; }

  .chat-tab {
    align-self: flex-end;
    height: 46px;
    padding: 0 18px;
    margin: 0 10px calc(10px + env(safe-area-inset-bottom)) 0;
  }
  .chat-toasts { width: auto; max-width: none; padding: 0 10px 6px; }

  /* Nothing behind the chat scrolls while it is open. The pinning itself is
     done in chat.js by fixing the page where it stands — "overflow: hidden"
     here would throw the scroll position away and land you at the top when
     the chat closes, so it is deliberately not used. */
  html.chat-fullscreen, body.chat-fullscreen { overscroll-behavior: none; }

  /* ---------- dialogs ----------
     A dialog is the width of the screen here, so the toolbar, the party and
     the chat tab would all sit on top of it. While one is up they step out of
     the way; they come straight back when it closes. */
  body.swal2-shown .toolbar,
  body.swal2-shown .party,
  body.swal2-shown .chat,
  body.swal2-shown #menu,
  body.swal2-shown .place-hint,
  body.swal2-shown .topbar,
  body.swal2-shown::before { visibility: hidden; }

  .swal2-popup { width: calc(100vw - 16px) !important; padding: 16px 14px; }
  .swal2-title { font-size: 20px; padding-top: 6px; }
  .swal2-actions { flex-wrap: wrap; gap: 8px; width: 100%; }
  .swal2-styled { margin: 0; flex: 1 1 auto; min-height: 44px; }
  .swal-form .field > span { font-size: 11px; }

  /* ---------- pictures ---------- */
  .crop-modal { width: calc(100vw - 20px); padding: 16px 14px; }
  .crop-stage { max-width: 100%; }
  .crop-bar .btn { min-height: 44px; }
  /* stacked, a flex-basis of 200px would make each box 200px TALL */
  .img-slots { flex-direction: column; }
  .img-slot { flex: 0 0 auto; width: 100%; }
  .slot-url { font-size: 16px; }
  .slot-actions .btn.small { height: 36px; font-size: 13px; }
  .overlay { padding: 12px; }
  .overlay-close { width: 44px; height: 44px; top: 10px; right: 10px; }
  .overlay .lightbox img { max-width: 100vw; max-height: 100dvh; }
}


/* ======================================================================
   The sign-in page
   ====================================================================== */
@media (max-width: 760px) {
  body:not(.sheet-page):not(.dm-page) > form.card { padding: 24px 20px; }
  .card .field input, .card .submit { height: 48px; font-size: 16px; }
}


/* ======================================================================
   The Players page
   ====================================================================== */
@media (max-width: 760px) {
  body.dm-page { padding: 110px 12px 48px; }
  .dm-page .topbar {
    top: 10px;
    left: 10px;
    right: 10px;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
  }
  .dm-page .topbar a, .dm-page .topbar .me { height: 36px; padding: 0 12px; font-size: 13px; }
  .dm-page .panel { padding: 14px 12px; }
  .dm-page .player { padding: 12px; gap: 10px; }
  .dm-page .who { min-width: 100%; }
  .dm-page .links { width: 100%; }
  .dm-page .links a, .dm-page .btn-link {
    flex: 1;
    justify-content: center;
    height: auto;
    min-height: 38px;
    padding: 7px 10px;
    text-align: center;
    line-height: 1.25;
  }
  .dm-page .palette .swatch { width: 40px; height: 40px; flex-basis: 40px; }
}


/* ======================================================================
   The character sheet: one column, read from the top down
   ====================================================================== */
@media (max-width: 760px) {
  body.sheet-page { padding: 108px 10px 40px; }
  .sheet-page .page { padding-bottom: 32px; }

  /* the toolbar stays a single row: the tabs shrink, the name is clipped */
  .sheet-page .tabs { gap: 4px; }
  .sheet-page .tabs .tab { padding: 0 11px; font-size: 12.5px; }
  .sheet-page .save-state { font-size: 10px; padding: 0 2px; }
  .sheet-page #userBtn .lbl { max-width: 30vw; }
  /* someone else's sheet: just the eye, the words would not fit */
  .read-only-tag { padding: 6px 8px; }
  .read-only-tag .lbl { display: none; }

  /* ---------- the banner ---------- */
  .banner { grid-template-columns: 96px 1fr; gap: 10px; }
  .banner.no-portrait { grid-template-columns: 1fr; }
  .portrait-box { width: 96px; height: 96px; }
  .name-box { min-height: 96px; }
  .banner > .meta-box { grid-column: 1 / -1; }
  .meta-grid { grid-template-columns: repeat(2, 1fr); }
  .big-name { font-size: 1.35rem; }

  /* ---------- the sheet itself, in a sensible reading order ---------- */
  /* Hit points first: on a phone that is what gets touched, over and over. */
  .sheet-grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "right"
      "mid"
      "abilities"
      "lists"
      "attacks"
      "actions";
    gap: 10px;
  }

  /* the six abilities go across the screen instead of down the side */
  .area-abilities {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  .ability { padding: 8px 4px 14px; }
  .ability-mod { margin: 6px 0 6px; font-size: 26px; }
  .ability-score { width: 52px; height: 32px; }

  /* hit points, armour and the rest */
  .mid-top { grid-template-columns: 1fr 108px; }
  .stat-box { min-height: 80px; }
  .hp-grid { gap: 8px; }
  .hp-field input { height: 42px; }
  .hp-field.current input { height: 48px; }
  .rest-btn { height: 28px; line-height: 26px; }
  /* the class's box: steppers big enough for a thumb */
  .res-row { grid-template-columns: minmax(0, 1fr) 34px auto 34px; padding: 5px 0; }
  .res-step { width: 34px; height: 34px; font-size: 17px; }
  .res-now { font-size: 21px; }
  .font-btn, .font-level { height: 32px; }
  .speed-row input { height: 36px; font-size: 16px; }
  .speed-star { width: 34px; height: 34px; }
  .speed-row { grid-template-columns: 34px 44px minmax(0, 1fr); }

  /* saves and skills: a row has to be tall enough to hit with a thumb */
  .list-box .row { min-height: 32px; }
  .row .value { width: 40px; }
  .row-name { font-size: 14px; }
  input[type="checkbox"] { width: 17px; height: 17px; }
  .pips input[type="checkbox"] { width: 19px; height: 19px; }

  /* ---------- attacks: two lines each, and no inner scrollbar ---------- */
  .attack-rows { max-height: none; overflow-y: visible; }
  .attack-head { display: none; }
  /* a card per row: the name, then Hit, Damage and Notes, labelled and lined
     up under the name, the way the columns line them up on a wide screen */
  .attack-row {
    grid-template-columns: 30px 30px minmax(0, 1fr);
    grid-template-areas:
      "lock  star  name"
      "hit   hit   hit"
      "dmg   dmg   dmg"
      ".     .     notes";
    gap: 4px 6px;
    padding: 10px 0;
  }
  .attack-row + .attack-row { margin-top: 0; border-top: 1px solid var(--border); }
  .attack-row .atk-lock { grid-area: lock; width: 30px; height: 34px; }
  .attack-row .atk-star { grid-area: star; width: 30px; height: 34px; }
  .attack-row .atk-name { grid-area: name; }
  .attack-row > [name$="_hit"] { grid-area: hit; }
  .attack-row > [name$="_damage"] { grid-area: dmg; }
  .attack-row > [name$="_notes"] { grid-area: notes; }
  .attack-row input { height: 36px; }
  .attack-row textarea { min-height: 36px; padding: 7px 8px; line-height: 20px; }
  .attack-row.is-locked .atk-notes:placeholder-shown { display: none; }   /* no empty line under a finished row */
  .attack-row .fx-wrap[data-of="hit"] { grid-area: hit; }
  .attack-row .fx-wrap[data-of="damage"] { grid-area: dmg; }
  .attack-row .fx-view { min-height: 32px; padding: 4px 8px; line-height: 24px; font-size: 16px; }
  /* the label under the lock and the star, the value starting where the name does */
  .attack-row .fx-wrap { display: grid; grid-template-columns: 66px minmax(0, 1fr); column-gap: 6px; }
  .attack-row .fx-wrap::before {
    padding-top: 5px; line-height: 24px;
    font: 600 10px/24px var(--font-ui); letter-spacing: 0.08em; text-transform: uppercase; color: #6b7384;
    text-align: right; padding-right: 2px;
  }
  .attack-row .fx-wrap[data-of="hit"]::before { content: "Hit"; }
  .attack-row .fx-wrap[data-of="damage"]::before { content: "Damage"; }

  /* the free-text boxes need less room here than on a wide screen */
  .tall-box textarea { min-height: 58px; }
  .tall-box.grow textarea { min-height: 120px; }
  .area-actions .tall-box textarea { min-height: 170px; }
  .senses-box textarea { min-height: 52px; }

  /* the inventory keeps its own scroll, tall enough to be worth scrolling */
  .inventory-box { min-height: 0; height: 320px; }
  .inv-row input { height: 38px; font-size: 16px; }
  .inv-row { grid-template-columns: 44px minmax(0, 1fr) 32px; }
  .inv-drop { width: 32px; height: 32px; opacity: 0.6; }

  /* ---------- the spells page ---------- */
  #page2 .banner { grid-template-columns: 1fr; }
  .spell-cols { display: none; }
  .spell-head { flex-wrap: wrap; }
  .spell-head .slot { flex: 1 1 80px; }
  .spell-row {
    grid-template-columns: 22px 30px 30px minmax(0, 1fr);
    grid-template-areas:
      "prep lock star name"
      "cond cond cond cond"
      "eff  eff  eff  eff";
    gap: 6px;
    padding: 8px 0;
  }
  .spell-row + .spell-row { border-top: 1px solid var(--border); }
  .spell-row .spell-prep { grid-area: prep; margin-top: 10px; }
  .spell-row .atk-lock { grid-area: lock; width: 30px; height: 36px; }
  .spell-row .atk-star { grid-area: star; width: 30px; height: 36px; }
  .spell-row .spell-name { grid-area: name; }
  .spell-row .spell-cond { grid-area: cond; }
  .spell-row .spell-eff { grid-area: eff; }
  .spell-row input:not([type="checkbox"]) { height: 36px; }
  .spell-row .spell-cond::placeholder { color: #4d5563; }
  .spell-row.is-locked .spell-cond:placeholder-shown { display: none; }
  .spell-row .fx-wrap[data-of="cond"] { grid-area: cond; }
  .spell-row .fx-wrap[data-of="eff"] { grid-area: eff; }
  .spell-row.is-locked .fx-wrap[data-of="cond"]:has(.spell-cond:placeholder-shown) { display: none; }
  .spell-row .fx-view { font-size: 16px; line-height: 23px; }
  .spell-meta { grid-column: 1 / -1; gap: 10px; }
  .spell-meta .meta-field span { font-size: 9px; letter-spacing: 0.06em; }
  .slot input { height: 30px; }
  .points-box { width: 100%; justify-content: space-between; }
  .points-step { width: 38px; height: 38px; font-size: 19px; }
  .spell-row { gap: 10px; }
}


/* ======================================================================
   The initiative tracker
   ====================================================================== */
@media (max-width: 760px) {
  /* no party strip here — the list is the party — so the page starts higher */
  body.fight-page { padding: 62px 10px 40px; }
  body.fight-page::before {
    content: "";
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    height: 64px;
    z-index: 1400;
    pointer-events: none;
    background: linear-gradient(to bottom, #0d1017 0%, #0d1017 80%, rgba(13, 16, 23, 0) 100%);
  }
  body.swal2-shown.fight-page::before { visibility: hidden; }

  /* the crossed swords say it; the word would not fit beside the others */
  .tab-fight .lbl { display: none; }
  .ai-fill-btn .lbl { display: none; }
  .ai-fill-btn { padding: 0 12px; }
  .tab-fight { padding: 0 12px; }

  .fight-head { margin: 4px 0 14px; gap: 10px; }
  .fight-title h1 { font-size: 1.6rem; margin-bottom: 4px; }
  .fight-controls { width: 100%; }
  .fight-controls .btn { height: 42px; flex: 1 1 auto; justify-content: center; }
  .fight-controls .fight-step { flex: 0 0 42px; width: 42px; }

  .fight-body { grid-template-columns: minmax(0, 1fr); }
  .book-list { max-height: 60vh; }
  .book-drop { opacity: 0.6; width: 34px; height: 34px; }
  .book-sort { padding: 7px 12px; }
  .kit-attack { padding: 11px 12px; }
  .kit-scroll { max-height: 320px; }

  .fight-row {
    grid-template-columns: 42px 42px minmax(0, 1fr) auto;
    gap: 9px;
    padding: 8px 10px 8px 8px;
  }
  .is-dm .fight-row { grid-template-columns: 14px 42px 42px minmax(0, 1fr) auto; padding-left: 4px; }
  .fight-init-box, .fight-init-num { width: 42px; height: 38px; font-size: 18px; }
  .fight-name { font-size: 14.5px; }
  .fight-roll { height: 36px; padding: 0 10px; }
  .fight-row .fight-card { max-width: none; }
  .fight-card-feet .btn { min-height: 38px; }
  .fight-eye { flex-basis: 36px; width: 36px; height: 38px; }
  .fight .fight-input { height: 38px; font-size: 16px; }
  .fight-rename { font-size: 16px; }
}
