/* VMT's feel: how the working screens answer a touch. One file, loaded by the
   main screen (templates/index.html) and by every page that draws the way home
   (templates/_homebar.html), so a button feels the same everywhere.

   The look and the layout are the pages' own and stay there. This only adds:
     - press, hover and focus on every control, through the classes the pages
       already use (no per-button edits);
     - the save states static/vmt-life.js sets: Saving… on the control, then a
       tick (fields) or "✓ Saved" (Save buttons), or a coral ring that stays;
     - the save light (#uiSavePill): one small status, bottom right (above the
       phone's bottom bar), that says Saving… / ✓ Saved / Not saved;
     - a gentle entrance for the show window and other modals.
   Screen only: printed sheets never see any of it. Nothing here animates on
   its own except the loading pulse; everything answers somebody's action.
   prefers-reduced-motion turns every movement off (states still show). */
@media screen {
  :root {
    --ui-ring: #f2b447;                 /* site-look rule 7: focus is 3px amber */
    --ui-ok: #3fb8a6; --ui-bad: #e8735a; --ui-wait: #f2b447;
    --ui-sheen: rgba(255,255,255,.055); --ui-press: rgba(0,0,0,.38);
    --ui-lift: 0 3px 10px -4px rgba(0,0,0,.6);
    --ui-pill-bg: #121b2e; --ui-pill-line: #33446a; --ui-pill-ink: #ffffff;
    --ui-tick: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.5l3.2 3L13 4.5' fill='none' stroke='%233fb8a6' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    --ui-ease: cubic-bezier(.2,.7,.2,1);
  }
  body[data-theme=dark] { --ui-ring:#f2b447; --ui-ok:#3fb8a6; --ui-bad:#e8735a; --ui-wait:#f2b447;
    --ui-sheen: rgba(255,255,255,.055); --ui-press: rgba(0,0,0,.38); --ui-lift: 0 3px 10px -4px rgba(0,0,0,.6);
    --ui-pill-bg:#121b2e; --ui-pill-line:#33446a; --ui-pill-ink:#ffffff;
    --ui-tick: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.5l3.2 3L13 4.5' fill='none' stroke='%233fb8a6' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); }
  /* light: the same colours, darkened until they pass AA on the paper
     backgrounds (#8a5a00 5.3:1, #0d6b63 5.7:1, #b42318 5.9:1 on #f4f2ec) */
  body[data-theme=light] { --ui-ring:#8a5a00; --ui-ok:#0d6b63; --ui-bad:#b42318; --ui-wait:#8a5a00;
    --ui-sheen: rgba(0,0,0,.04); --ui-press: rgba(0,0,0,.16); --ui-lift: 0 3px 10px -5px rgba(40,30,10,.35);
    --ui-pill-bg:#ffffff; --ui-pill-line:#e3ded2; --ui-pill-ink:#1c1a14;
    --ui-tick: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 8.5l3.2 3L13 4.5' fill='none' stroke='%230d6b63' stroke-width='2.2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"); }

  /* ---- every control: quick, consistent transitions; no grey tap flash ---- */
  :where(button, a, summary, label, select, input, textarea, [role=button], .pbtn, .iconbtn, .reviewbtn,
         .wqbtn, .btn, .cnbtn, .mtoggle, .newBtn, .rowactbtn, .homechip, .chip, .mtab-btn, .readychip.rdyclick) {
    -webkit-tap-highlight-color: transparent;
  }
  :where(button, [role=button], .pbtn, .iconbtn, .reviewbtn, .wqbtn, .btn, .cnbtn, .mtoggle, .newBtn,
         .rowactbtn, .homechip, .chip, .mtab-btn, .vmt-home, .readychip.rdyclick, input[type=submit], input[type=button]) {
    touch-action: manipulation;
    transition: background-color 150ms ease-out, border-color 150ms ease-out, color 150ms ease-out,
                box-shadow 160ms ease-out, opacity 150ms ease-out, translate 140ms var(--ui-ease), scale 140ms var(--ui-ease);
  }
  :where(input, textarea, select) { transition: border-color 150ms ease-out, box-shadow 180ms ease-out, background-color 150ms ease-out; }

  /* hover: a sheen over whatever colour the button already is, and the raised
     buttons lift a pixel. Mice and trackpads only — a phone never keeps a
     "hover" stuck on the last thing tapped. */
  @media (hover: hover) and (pointer: fine) {
    :is(button, [role=button], .pbtn, .iconbtn, .reviewbtn, .wqbtn, .btn, .cnbtn, .mtoggle, .newBtn,
        .rowactbtn, .homechip, .chip, .mtab-btn, .readychip.rdyclick):hover:not(:disabled):not([aria-disabled=true]) {
      background-image: linear-gradient(var(--ui-sheen), var(--ui-sheen));
    }
    :is(.pbtn, .iconbtn, .reviewbtn, .wqbtn, .btn, .cnbtn, .newBtn, .homechip, .rowactbtn):hover:not(:disabled):not([aria-disabled=true]) {
      translate: 0 -1px; box-shadow: var(--ui-lift);
    }
    /* rows and cards answer the pointer too (colour only on table rows: there
       can be hundreds, and nothing here runs on the 60s refresh) */
    .card[data-key] { transition: border-color 150ms ease-out, box-shadow 160ms ease-out, translate 160ms var(--ui-ease); }
    .card[data-key]:hover { translate: 0 -1px; box-shadow: var(--ui-lift); }
    tbody tr[data-key] td { transition: background-color 120ms ease-out; }
  }

  /* press: the button sinks — down a pixel, a touch smaller, shadow inside —
     the instant a finger or click lands (touchstart is listened to in
     vmt-life.js so iOS Safari applies :active at all) */
  :is(button, [role=button], .pbtn, .iconbtn, .reviewbtn, .wqbtn, .btn, .cnbtn, .mtoggle, .newBtn,
      .rowactbtn, .homechip, .chip, .mtab-btn, .vmt-home, .readychip.rdyclick):active:not(:disabled):not([aria-disabled=true]) {
    translate: 0 1px; scale: .975; box-shadow: inset 0 2px 5px var(--ui-press);
    transition-duration: 60ms;
  }
  .card[data-key]:active { scale: .992; transition-duration: 60ms; }

  /* focus: 3px amber, keyboard only, on every control; fields get a soft ring
     on focus (their own border stays) */
  :is(button, a, summary, [role=button], [tabindex], .pbtn, .iconbtn, .reviewbtn, .chip, .mtab-btn):focus-visible {
    outline: 3px solid var(--ui-ring); outline-offset: 2px;
  }
  :is(input, textarea, select):focus-visible {
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--ui-ring) 42%, transparent);
  }
  :is(input[type=checkbox], input[type=radio]):focus-visible { outline: 3px solid var(--ui-ring); outline-offset: 2px; box-shadow: none; }

  /* disabled: plainly not pressable, and it does not move */
  :is(button, .pbtn, .iconbtn, .reviewbtn, .wqbtn, .btn, .cnbtn, .mtoggle, .newBtn, input[type=submit]):disabled,
  [aria-disabled=true] { cursor: not-allowed; translate: none; scale: none; }

  /* ---- save states on a button (vmt-life.js sets these) ---- */
  .ui-busy.ui-busy { cursor: progress;
    background-image: linear-gradient(100deg, transparent 20%, var(--ui-sheen) 40%, var(--ui-sheen) 60%, transparent 80%);
    background-size: 220% 100%; animation: ui-sweep 1.1s linear infinite; }
  .ui-ok.ui-ok { animation: ui-okring 700ms ease-out; color: var(--ui-ok); border-color: var(--ui-ok); }
  .pbtn.primary.ui-ok, .tabs button.on.ui-ok { color: inherit; }
  .ui-fail.ui-fail { animation: ui-shake 340ms ease-out; box-shadow: 0 0 0 2px var(--ui-bad); }

  /* ---- save states on a field: a line while it goes, a tick when the server
     said OK, a coral ring that stays when it did not ---- */
  :is(input, textarea, select).ui-f-saving { box-shadow: inset 0 -2px 0 0 var(--ui-wait); }
  :is(input, textarea).ui-f-ok {
    background-image: var(--ui-tick); background-repeat: no-repeat; background-size: 14px 14px;
    background-position: right 8px top 8px; animation: ui-fieldok 900ms ease-out; }
  input.ui-f-ok { background-position: right 8px center; }
  select.ui-f-ok { animation: ui-fieldok 900ms ease-out; }
  :is(input, textarea, select).ui-f-fail { box-shadow: 0 0 0 2px var(--ui-bad); }

  /* ---- the save light ---- */
  .ui-pill { position: fixed; z-index: 90; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    display: flex; align-items: center; gap: 9px; max-width: min(420px, calc(100vw - 32px));
    padding: 8px 10px 8px 12px; border-radius: 999px; border: 1px solid var(--ui-pill-line);
    background: var(--ui-pill-bg); color: var(--ui-pill-ink); font: 600 13px/1.3 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
    box-shadow: 0 8px 24px -10px rgba(0,0,0,.55);
    opacity: 0; translate: 0 8px; pointer-events: none; visibility: hidden;
    transition: opacity 180ms ease-out, translate 220ms var(--ui-ease), visibility 0s linear 220ms; }
  .ui-pill.ui-show { opacity: 1; translate: 0 0; pointer-events: auto; visibility: visible;
    transition: opacity 160ms ease-out, translate 220ms var(--ui-ease), visibility 0s; }
  .ui-pill .ui-led { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--ui-wait); }
  .ui-pill[data-state=saving] .ui-led { animation: ui-breathe 1.1s ease-in-out infinite; }
  .ui-pill[data-state=ok] .ui-led { background: var(--ui-ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ui-ok) 30%, transparent); }
  .ui-pill[data-state=ok] .ui-pill-t { color: var(--ui-ok); }
  .ui-pill[data-state=fail] { border-color: var(--ui-bad); }
  .ui-pill[data-state=fail] .ui-led { background: var(--ui-bad); }
  .ui-pill .ui-pill-t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .ui-pill[data-state=fail] .ui-pill-t { white-space: normal; }
  .ui-pill .ui-pill-x { display: none; border: 0; background: none; color: inherit; font-size: 17px; line-height: 1;
    padding: 2px 6px; border-radius: 999px; cursor: pointer; opacity: .75; }
  .ui-pill[data-state=fail] .ui-pill-x { display: inline-block; }
  /* the phone's bottom bar (index.html's #mobnav, same breakpoint) is 60-odd px
     tall: sit above it, centred */
  @media (max-width: 640px), (pointer: coarse) and (max-width: 1024px) {
    .ui-pill { right: auto; left: 50%; translate: -50% 8px; bottom: calc(78px + env(safe-area-inset-bottom, 0px)); }
    .ui-pill.ui-show { translate: -50% 0; }
  }

  /* ---- a bit of life, all of it answering somebody ---- */
  .modal-bg.open { animation: ui-fade 160ms ease-out; }
  .modal-bg.open > .modal { animation: ui-rise 220ms var(--ui-ease); }
  dialog[open] { animation: ui-rise 220ms var(--ui-ease); }
  details.ui-opening > :not(summary) { animation: ui-rise 200ms var(--ui-ease); }
  .ui-loading { animation: ui-breathe 1.4s ease-in-out infinite; }
  .ui-bump { animation: ui-bump 460ms var(--ui-ease); }

  @keyframes ui-fade { from { opacity: 0; } to { opacity: 1; } }
  @keyframes ui-rise { from { opacity: 0; translate: 0 8px; } to { opacity: 1; translate: 0 0; } }
  @keyframes ui-breathe { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }
  @keyframes ui-sweep { from { background-position: 120% 0; } to { background-position: -120% 0; } }
  @keyframes ui-okring { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ui-ok) 60%, transparent); }
                         100% { box-shadow: 0 0 0 7px color-mix(in srgb, var(--ui-ok) 0%, transparent); } }
  @keyframes ui-fieldok { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--ui-ok) 55%, transparent); }
                          100% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--ui-ok) 0%, transparent); } }
  @keyframes ui-shake { 0%, 100% { translate: 0 0; } 25% { translate: -3px 0; } 50% { translate: 3px 0; } 75% { translate: -2px 0; } }
  @keyframes ui-bump { 0% { scale: 1; } 40% { scale: 1.22; } 100% { scale: 1; } }

  /* reduced motion: nothing moves anywhere on the page; the states (tick,
     ring, the light's words) still show, they just appear */
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important;
      transition-duration: 1ms !important; scroll-behavior: auto !important; }
    :is(button, [role=button], .pbtn, .iconbtn, .reviewbtn, .wqbtn, .btn, .cnbtn, .mtoggle, .newBtn,
        .rowactbtn, .homechip, .chip, .mtab-btn, .vmt-home, .card, .readychip):is(:hover, :active) { translate: none !important; scale: none !important; }
    .ui-pill, .ui-pill.ui-show { translate: none; }
    @media (max-width: 640px), (pointer: coarse) and (max-width: 1024px) { .ui-pill, .ui-pill.ui-show { translate: -50% 0; } }
  }
}
