/* The card dialog (after the shutter): one layer of CSS for every size.
   Areas: head / stage / chips / note / foot. On a phone it fills the screen and stacks; when the
   screen is wide and tall enough (or a phone lies on its side) the card takes the left column and
   everything else the right. The keyboard shrinks the visible viewport (--card-vv-*): the stage
   gives way first, then the style chips. Nothing else moves. */
#card-dialog {
  --side: 20px;
  --row-gap: 12px;
  --sb: env(safe-area-inset-bottom, 0px);
  --st: env(safe-area-inset-top, 0px);
  --paper: #f5f4ed;
  --go: #c6f24a;
  --go-ink: #14201a;
  --line: hsl(var(--border));
  position: fixed;
  inset: auto;
  top: var(--card-vv-top, 0px);
  left: var(--card-vv-left, 0px);
  width: var(--card-vv-width, 100vw);
  height: var(--card-vv-height, 100dvh);
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  overflow: hidden;
  background: hsl(var(--background));
  color: var(--ink);
}
#card-dialog[open] {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  grid-template-areas: "head" "stage" "chips" "note" "foot";
}

/* head */
#card-dialog .card-head { grid-area: head; display: flex; align-items: center; justify-content: space-between; height: calc(56px + var(--st)); padding: var(--st) 12px 0 var(--side); }
#card-dialog h2 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.01em; }
#card-dialog .card-preview-tools { display: flex; gap: 4px; }
#card-dialog .card-preview-tools > button, #card-dialog .card-options > summary { display: grid; place-items: center; width: 44px; height: 44px; padding: 0; margin: 0; border: 0; border-radius: 12px; background: transparent; color: inherit; cursor: pointer; list-style: none; }
#card-dialog .card-options > summary::-webkit-details-marker { display: none; }
#card-dialog .card-preview-tools > button:hover, #card-dialog .card-options > summary:hover, #card-dialog .card-options[open] > summary { background: hsl(var(--secondary)); }
#card-dialog #card-close { font-size: 28px; line-height: 1; }

/* ratio and photo fit, behind the sliders icon */
#card-dialog .card-options { position: relative; }
#card-dialog .card-options-body { position: absolute; right: 0; top: 48px; z-index: 30; width: min(320px, calc(100vw - 40px)); padding: 12px; border: 1px solid var(--line); border-radius: 14px; background: hsl(var(--popover)); color: hsl(var(--popover-foreground)); box-shadow: 0 16px 40px -16px rgb(0 0 0 / .7); }
#card-dialog .card-choice { display: flex; flex-wrap: wrap; gap: 6px; }
#card-dialog .card-choice + .card-choice { margin-top: 10px; }
#card-dialog .card-choice::before { content: attr(aria-label); width: 100%; font-size: 12px; color: var(--muted-ink); padding-bottom: 2px; }
#card-dialog .card-choice[hidden] { display: none; }
#card-dialog .card-choice button { flex: 1 1 auto; min-height: 36px; padding: 0 12px; border: 1.5px solid var(--line); border-radius: 999px; background: transparent; color: inherit; font-size: 13px; font-weight: 600; }
#card-dialog .card-choice button[aria-pressed="true"] { border-color: var(--ink); color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
#card-dialog .card-options-body .fine { margin: 10px 0 0; font-size: 12px; color: var(--muted-ink); }

/* stage: the card, as large as the room allows, nothing around it */
/* The image is sized to the whole stage and letterboxes itself (object-fit): that is the one sizing that stays
   definite in a grid row at every viewport. The chip is placed on the drawn card from its measured box (app.js). */
#card-dialog .card-stage { grid-area: stage; position: relative; min-height: 0; min-width: 0; padding: 4px var(--side) 0; box-sizing: border-box; overflow: hidden; }
#card-dialog #card-preview { display: block; width: 100%; height: 100%; object-fit: contain; object-position: center; filter: drop-shadow(0 14px 18px rgb(0 0 0 / .45)); }
#card-dialog #card-preview[hidden] { display: none; }
#card-dialog #card-preview[hidden] ~ .card-title-editor { visibility: hidden; }
/* the place name: a chip on the photo (the card prints it there), tap to edit in place */
/* the card prints the name at (36, 36) of its 1080 width; the chip covers exactly that */
#card-dialog .card-title-editor { position: absolute; left: calc(var(--img-left, var(--side)) + 36px * var(--img-scale, 0.3)); top: calc(var(--img-top, 4px) + 36px * var(--img-scale, 0.3)); right: calc(var(--side) + 10px); max-width: calc(var(--img-width, 100%) - 72px * var(--img-scale, 0.3)); min-width: 0; display: flex; align-items: flex-start; line-height: 1; pointer-events: none; }
#card-dialog #card-title-chip { pointer-events: auto; display: inline-flex; align-items: center; gap: 6px; max-width: 100%; height: max(28px, 60px * var(--img-scale, 0.3)); padding: 0 calc(12px * var(--img-scale, 0.3)) 0 calc(12px * var(--img-scale, 0.3)); border: 0; border-radius: calc(6px * var(--img-scale, 0.3)); background: rgb(14 22 48 / .94); color: #fff; font-size: clamp(12px, 42px * var(--img-scale, 0.3), 16px); font-weight: 600; cursor: pointer; }
#card-dialog #card-title-chip > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#card-dialog #card-title-chip > svg { flex: none; }
#card-dialog #card-title-chip[hidden] { display: none; }
#card-dialog .card-title-row { pointer-events: auto; display: flex; gap: 6px; align-items: center; }
#card-dialog .card-title-row[hidden] { display: none; }
/* While the name is being typed the editor spans the stage: pinned to the card's corner it ran off the screen once the keyboard
   shrank the card, and "done" could not be reached. */
#card-dialog .card-title-editor:has(> .card-title-row:not([hidden])) { left: var(--side); right: var(--side); max-width: none; z-index: 3; }
#card-dialog .card-title-editor:has(> .card-title-row:not([hidden])) .card-title-row { flex: 1 1 auto; min-width: 0; }
#card-dialog #card-title-input { flex: 1 1 auto; min-width: 0; height: 40px; padding: 0 12px; border: 1.5px solid var(--line); border-radius: 10px; background: hsl(var(--popover)); color: var(--ink); font-size: 16px; }
#card-dialog #card-title-done { flex: none; height: 40px; padding: 0 12px; border: 0; border-radius: 10px; background: var(--ink); color: hsl(var(--background)); font-size: 14px; font-weight: 700; }

/* style chips */
#card-dialog .card-chips { grid-area: chips; display: flex; justify-content: center; gap: 8px; padding: var(--row-gap) var(--side) 0; }
/* Icon chips (index.html): one width for all, the style's name is the button's name and tooltip, so nothing wraps on a narrow phone. */
#card-dialog .card-chips button { display: grid; place-items: center; width: 52px; height: 40px; padding: 0; border: 1.5px solid var(--line); border-radius: 999px; background: transparent; color: inherit; }
#card-dialog .card-chips button svg { display: block; }
#card-dialog .card-chips button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: hsl(var(--background)); font-weight: 700; }

/* one line for the receiver */
#card-dialog .card-note { grid-area: note; padding: var(--row-gap) var(--side) 0; }
#card-dialog #card-text-input { width: 100%; box-sizing: border-box; height: 48px; padding: 0 16px; border: 1.5px solid var(--line); border-radius: 14px; background: hsl(var(--secondary)); color: var(--ink); font-size: 16px; }
#card-dialog #card-text-input::placeholder { color: var(--muted-ink); }

/* actions */
#card-dialog .card-foot { grid-area: foot; padding: var(--row-gap) var(--side) calc(16px + var(--sb)); }
#card-dialog #card-progress { min-height: 18px; margin-bottom: 4px; font-size: 12px; line-height: 18px; color: var(--muted-ink); }
#card-dialog #card-progress:empty { margin: 0; min-height: 0; }
#card-dialog[aria-busy="true"] #card-progress::before { content: ""; display: inline-block; vertical-align: middle; width: 10px; height: 10px; margin-right: 6px; border: 2px solid var(--line); border-top-color: var(--ink); border-radius: 50%; animation: map-spin 0.9s linear infinite; }
#card-dialog #card-retry { width: 100%; min-height: 44px; margin: 0 0 8px; }
#card-dialog #card-retry[hidden] { display: none; }
/* The save icon and the send button take the whole row (the footer is a flex row; on their own they drifted to the right). */
#card-dialog .dialog-actions { display: flex; flex: 1 1 100%; min-width: 0; gap: 10px; margin: 0; }
#card-dialog .card-foot > #card-progress:empty { display: none; } /* an empty slot plus the gap pushed the row 12 px off centre */
#card-dialog #download-card { flex: none; width: 56px; height: 56px; display: grid; place-items: center; padding: 0; border: 1.5px solid var(--line); border-radius: 16px; background: transparent; color: inherit; text-decoration: none; }
#card-dialog #download-card[aria-disabled="true"] { opacity: 0.45; cursor: wait; }
#card-dialog #share-card { flex: 1 1 auto; height: 56px; padding: 0 16px; border: 0; border-radius: 16px; background: var(--go); color: var(--go-ink); font-size: 17px; font-weight: 800; display: flex; align-items: center; justify-content: center; gap: 8px; }
#card-dialog #share-note { margin: 6px 0 0; font-size: 11px; line-height: 1.45; color: var(--muted-ink); text-align: left; }
#card-dialog #share-note[hidden] { display: none; }

/* small phones */
@media (max-width: 360px) {
  #card-dialog { --side: 16px; --row-gap: 8px; }
  #card-dialog .card-chips button { width: 46px; height: 36px; }
  #card-dialog #card-text-input { height: 44px; border-radius: 12px; }
  #card-dialog #download-card { width: 48px; height: 48px; border-radius: 14px; }
  #card-dialog #share-card { height: 48px; font-size: 16px; border-radius: 14px; }
}

/* two columns: the card left, the rest right (a phone on its side, a tablet, a desktop) */
@media (min-width: 600px) and (max-height: 560px), (min-width: 720px) and (min-height: 560px) {
  #card-dialog[open] {
    grid-template-columns: minmax(0, 1fr) minmax(280px, 42%);
    grid-template-rows: auto auto auto minmax(0, 1fr);
    grid-template-areas: "stage head" "stage chips" "stage note" "stage foot";
    column-gap: 8px;
  }
  #card-dialog .card-stage { padding: 14px 0 14px var(--side); }
  #card-dialog .card-head { padding-left: 4px; }
  #card-dialog .card-chips { justify-content: flex-start; padding-left: 4px; }
  #card-dialog .card-note { padding-left: 4px; }
  #card-dialog .card-foot { align-self: end; padding-left: 4px; padding-bottom: calc(14px + var(--sb)); }
}
/* on a big screen the dialog is a window over the map, not the whole screen */
@media (min-width: 720px) and (min-height: 560px) {
  #card-dialog {
    top: calc(var(--card-vv-top, 0px) + (var(--card-vv-height, 100dvh) - min(600px, var(--card-vv-height, 100dvh) - 32px)) / 2);
    left: calc(var(--card-vv-left, 0px) + (var(--card-vv-width, 100vw) - min(780px, var(--card-vv-width, 100vw) - 32px)) / 2);
    width: min(780px, calc(var(--card-vv-width, 100vw) - 32px));
    height: min(600px, calc(var(--card-vv-height, 100dvh) - 32px));
    border: 1px solid var(--line);
    border-radius: 22px;
    box-shadow: 0 30px 80px -30px rgb(0 0 0 / .9);
  }
  #card-dialog .card-stage { background: color-mix(in srgb, hsl(var(--background)) 85%, black); padding: 24px; }
  #card-dialog .card-head { padding-right: 16px; }
  #card-dialog .card-chips, #card-dialog .card-note, #card-dialog .card-foot { padding-right: 24px; }
}

/* the keyboard: little height left — the chips go, the stage shrinks, the line stays reachable */
#card-dialog.keyboard-compact .card-chips, #card-dialog.keyboard-compact #card-progress:empty { display: none; }
#card-dialog.keyboard-compact .card-stage { padding-top: 0; }
#card-dialog.keyboard-compact .card-foot { padding-top: 8px; padding-bottom: 8px; }
#card-dialog.keyboard-narrow .card-head { height: 44px; }
#card-dialog.keyboard-narrow #card-text-input, #card-dialog.keyboard-narrow #share-card, #card-dialog.keyboard-narrow #download-card { height: 44px; }
#card-dialog.keyboard-narrow #download-card { width: 44px; }

@media (prefers-reduced-motion: reduce) {
  #card-dialog[aria-busy="true"] #card-progress::before { animation: none; }
}
#card-dialog .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* The postcard chip: the place mailbox's red (atlas.css #8e2f3f), apart from the three styles, so it stands out. */
#card-dialog .card-chips .card-postcard-chip { margin-left: 8px; border-color: #8e2f3f; background: #8e2f3f; color: #fff; }
#card-dialog .card-chips .card-postcard-chip[hidden] { display: none; }
#card-dialog .card-chips .card-postcard-chip[aria-pressed="true"] { background: #8e2f3f; border-color: #8e2f3f; color: #fff; box-shadow: 0 0 0 2px hsl(var(--background)), 0 0 0 4px #c45a6c; }
#card-dialog .card-chips .card-postcard-chip:focus-visible { outline: 3px solid hsl(var(--ring)); outline-offset: 3px; }
