/*
 * EncodeLabel kit: saving and opening labels (goes with assets/js/kit-save.js).
 *
 * Three things live here:
 *   .ksv-save     the Save button on a tool page and the line beside it
 *                 ("Saved", "Kept in this browser", "Unsaved changes")
 *   .ksv-box      what is inside the "Save this label" pop-up: a small
 *                 picture of the label, its name, where it goes, and the
 *                 "Save the datalist too?" question
 *   .ksv-open     what is inside the "Open a label" pop-up: the labels kept
 *                 in this browser and in the account, as cards (rows on a phone)
 *
 * The pop-ups themselves, buttons, tabs, alerts and the status line are the
 * shell's (site.css). Colours come from the design tokens only, so the light
 * and the dark theme both work. The one thing that never follows the theme
 * is the label in a small picture: it is white with black ink, like the
 * paper it prints on, so its shadow is mixed from --sheet-ink.
 */

.ksv-icon { display: inline-grid; flex: none; place-items: center; }

/* A small picture of a label. It is always an <img>. Its box (.ksv-preview, .ksv-thumb) has a
   size of its own and the picture is pinned inside it, so a tall label can never push out of it. */
.ksv-thumb-img {
  position: absolute; top: var(--ksv-pad, 12px); left: var(--ksv-pad, 12px); display: block;
  width: calc(100% - 2 * var(--ksv-pad, 12px)); height: calc(100% - 2 * var(--ksv-pad, 12px));
  object-fit: contain;
  filter: drop-shadow(0 1px 2px color-mix(in srgb, var(--sheet-ink) 30%, transparent));
}

/* ---- The Save button on the page ------------------------------------------- */
.ksv-save { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; min-width: 0; max-width: 100%; }
/* The line beside it is a .status-line; here it only loses its space when empty. */
.ksv-state { min-height: 0; margin: 0; font-size: .85rem; white-space: nowrap; }
.ksv-state:empty { display: none; }
/* "Unsaved changes" is a quiet nudge: muted words, an amber mark. */
.ksv-state.is-changed::before { background: var(--warn); }

/* ---- Inside "Save this label" ------------------------------------------------ */
.ksv-form { margin: 0; }
.ksv-top { display: flex; align-items: flex-start; gap: 14px; margin: 0 0 14px; }
.ksv-preview {
  --ksv-pad: 10px;
  position: relative; display: grid; flex: none; place-items: center; width: 92px; height: 92px;
  border: 1px solid var(--desk-line); border-radius: var(--radius-small);
  background: var(--desk); overflow: hidden;
}
.ksv-preview .skeleton { width: 70%; height: 44%; }
.ksv-name-field { flex: 1 1 auto; min-width: 0; margin: 0; }

/* Where it goes, said plainly: a small picture and a sentence or two. */
.ksv-where {
  display: flex; align-items: flex-start; gap: 10px; margin: 0; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: var(--radius-small);
  background: color-mix(in srgb, var(--ink) 3%, var(--card));
  font-size: .92rem; line-height: 1.45;
}
.ksv-where > .ksv-icon { margin-top: .1em; color: var(--muted); }
.ksv-where > div { min-width: 0; }
.ksv-where p { margin: 0 0 6px; overflow-wrap: anywhere; }
.ksv-where p:last-child { margin-bottom: 0; }
.ksv-quiet { color: var(--muted); font-size: .88rem; }
/* "or log in." moves to the next line in one piece. */
.ksv-together { white-space: nowrap; }

/* Something went wrong: the reason, and what can be done instead. */
.ksv-trouble:empty { display: none; }
.ksv-trouble { margin-top: 14px; }
.ksv-trouble .alert { margin-bottom: 10px; }
.ksv-choices { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 18px; margin: 0; }

/* The datalist question. */
.ksv-done { display: flex; align-items: flex-start; gap: 8px; margin: 0 0 12px; color: var(--ok); font-weight: 600; }
.ksv-done span { min-width: 0; overflow-wrap: anywhere; }
.ksv-box > p { overflow-wrap: anywhere; }
.ksv-plain { margin: 0 0 4px; }
.ksv-plain + .alert { margin: 14px 0 0; }

/* ---- Inside "Open a label" --------------------------------------------------- */
/* A steady height, so the pop-up does not jump when a tab or a search changes the list. */
.ksv-open { display: flex; flex-direction: column; min-height: min(430px, 54dvh); }
.ksv-tabs { flex: 1 1 auto; }
.ksv-tabs > .tab-panel { padding-top: 16px; }
.ksv-count {
  display: inline-block; min-width: 1.6em; margin-left: 4px; padding: 0 5px;
  border-radius: 3px; background: var(--soft); color: var(--ink);
  font-size: .78rem; font-weight: 600; line-height: 1.5; text-align: center;
}
.ksv-count:empty { display: none; }
.ksv-panel { min-width: 0; }
.ksv-find { max-width: 340px; margin: 0 0 14px; }
.ksv-group { margin: 4px 0 10px; color: var(--muted); font-size: .92rem; font-weight: 650; letter-spacing: 0; }
.ksv-none { margin: 0 0 16px; }

.ksv-list {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(224px, 1fr)); gap: 12px;
  margin: 0 0 18px; padding: 0; list-style: none;
}
.ksv-item { position: relative; min-width: 0; margin: 0; }
/* One label: a <button>, or an <a> when it opens in another tool. */
.ksv-card {
  display: flex; flex-direction: column; width: 100%; height: 100%; padding: 0;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--card); color: var(--ink);
  font: inherit; text-align: left; text-decoration: none; cursor: pointer; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.ksv-card:hover { border-color: var(--ink); }
.ksv-card[aria-busy="true"] { opacity: .55; cursor: progress; }
.ksv-open.is-busy .ksv-card { cursor: progress; }
.ksv-thumb {
  --ksv-pad: 14px;
  position: relative; display: grid; flex: none; place-items: center; width: 100%; height: 128px;
  border-bottom: 1px solid var(--line); background: var(--desk);
}
.ksv-text { display: flex; flex: 1 1 auto; flex-direction: column; gap: 1px; min-width: 0; padding: 10px 12px 12px; }
.ksv-name { overflow: hidden; font-weight: 650; line-height: 1.3; text-overflow: ellipsis; white-space: nowrap; }
.ksv-meta { overflow: hidden; color: var(--muted); font-size: .82rem; line-height: 1.4; text-overflow: ellipsis; white-space: nowrap; }
.ksv-meta:empty { display: none; }
/* The paper may take two lines: "30 per sheet, 1 x 2 5/8 in, US Letter" is the part people look for. */
.ksv-paper { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; white-space: normal; }
/* When it was saved and, for a label made somewhere else, the tool it opens in. */
.ksv-row { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; min-width: 0; margin-top: auto; padding-top: 4px; }
.ksv-row:empty { display: none; }
.ksv-tool { flex: none; }
.ksv-remove {
  position: absolute; top: 6px; right: 6px; display: grid; place-items: center; width: 36px; height: 36px; padding: 0;
  border: 1px solid var(--line); border-radius: var(--radius-small);
  background: var(--card); color: var(--ink); cursor: pointer;
}
.ksv-remove:hover { border-color: var(--error); color: var(--error); }
/* "Removed ... Undo" takes the removed card's place. */
.ksv-gone {
  display: flex; flex-wrap: wrap; align-content: center; align-items: baseline; gap: 4px 8px; min-height: 60px; padding: 12px 14px;
  border: 1px dashed var(--field-line); border-radius: var(--radius);
  color: var(--muted); font-size: .9rem;
}
.ksv-gone span { min-width: 0; overflow-wrap: anywhere; }

/* While the account's labels are on their way. */
.ksv-waiting .ksv-card { cursor: default; }
.ksv-waiting .ksv-card:hover { border-color: var(--line); }
.ksv-waiting .ksv-thumb .skeleton { width: 64%; height: 56%; }
.ksv-waiting .ksv-text { gap: 9px; padding-top: 14px; padding-bottom: 18px; }
.ksv-waiting .ksv-text .skeleton { width: 72%; height: 11px; min-height: 0; }
.ksv-waiting .ksv-text .skeleton + .skeleton { width: 44%; }

.ksv-empty { margin: 8px 0 16px; }
.ksv-empty p { margin: 0 0 4px; }
.ksv-foot { margin: 0; }

/* "Opening ..." and anything that went wrong stay in view at the foot of the list,
   with a choice or two under the words when there is one to make. */
.ksv-open-foot {
  position: sticky; bottom: -20px; z-index: 1; margin: 14px -20px -20px; padding: 10px 20px 14px;
  border-top: 1px solid var(--line); background: var(--card);
}
.ksv-open-foot .status-line { align-items: flex-start; min-height: 0; margin: 0; }
.ksv-open-foot .status-line::before { align-self: flex-start; margin-top: .5em; }
.ksv-open-foot .ksv-choices { margin: 6px 0 0 16px; }

@media (max-width: 560px) {
  /* A phone: one label per row, the picture on the left. */
  .ksv-list { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .ksv-card { flex-direction: row; align-items: stretch; min-height: 78px; }
  .ksv-thumb { --ksv-pad: 9px; width: 84px; height: auto; min-height: 78px; border-right: 1px solid var(--line); border-bottom: 0; }
  .ksv-text { justify-content: center; padding: 8px 12px; }
  .ksv-row { margin-top: 0; padding-top: 2px; }
  .ksv-item.has-remove .ksv-text { padding-right: 56px; }
  .ksv-remove { top: 50%; right: 6px; width: 44px; height: 44px; border-color: transparent; background: none; transform: translateY(-50%); }
  .ksv-gone { min-height: 78px; }
  .ksv-waiting .ksv-thumb .skeleton { width: 70%; height: 50%; }
  .ksv-open-foot { bottom: -18px; margin: 12px -16px -18px; padding: 10px 16px calc(12px + env(safe-area-inset-bottom)); }
  .ksv-find { max-width: none; }
  .ksv-preview { --ksv-pad: 8px; width: 80px; height: 80px; }
}

/* Fingers need 44px. */
@media (pointer: coarse) {
  .ksv-remove { width: 44px; height: 44px; }
  .ksv-choices .link-button, .ksv-gone .link-button { min-height: 44px; }
}

@media (prefers-reduced-motion: no-preference) {
  .ksv-card, .ksv-remove { transition: border-color .15s, color .15s; }
}

/* The label keeps its real black and white in Windows' high-contrast colours too. */
@media (forced-colors: active) {
  .ksv-preview, .ksv-thumb { forced-color-adjust: none; }
}
