/* Merit's ONE feedback layer: the status modal, the confirmation dialog, the busy lock, toasts, field errors,
   the request progress bar and the error page.

   Markup comes from controllers/errorhandling/templates/globalmodal.html and errorpage.html; behaviour from
   static/javascript/controllers/errorhandling/script.js, which
     - opens #merit-modal, #merit-confirm and #merit-busy with showModal() and, to close, adds .is-closing and
       calls close() on animationend (so every exit here is a CSS animation on the dialog itself),
     - sets data-tone (success | danger | warning | info) on a dialog and draws its icon as
       <svg class="gm-svg"><circle class="gm-ring"/><path class="gm-draw"/></svg>,
     - builds toasts as .toast[data-variant] (.is-in to enter, .is-out to leave, .is-paused on hover or focus,
       duration in --toast-ms),
     - marks fields with .field.has-error + p.field-error + .shake, and toggles #nav-progress.is-active / .is-done.
   Tokens and every shared component come from the theme stylesheet, which loads first. */

/* ==========================================================================================================
   Status modal and confirmation (dialog.gm)
   ========================================================================================================== */

dialog.gm {
  --tone: var(--info);
  --tone-soft: var(--info-soft);
  width: min(440px, calc(100% - 32px));
  max-width: none;
  max-height: none;
  margin: auto;
  padding: 0;
  overflow: visible;
  border: 0;
  background: transparent;
  color: var(--text);
}

dialog.gm[data-tone="success"] { --tone: var(--success); --tone-soft: var(--success-soft); }
dialog.gm[data-tone="danger"] { --tone: var(--danger); --tone-soft: var(--danger-soft); }
dialog.gm[data-tone="warning"] { --tone: var(--warning); --tone-soft: var(--warning-soft); }
dialog.gm[data-tone="info"] { --tone: var(--info); --tone-soft: var(--info-soft); }

dialog.gm::backdrop {
  background: var(--scrim);
  -webkit-backdrop-filter: blur(6px) saturate(1.15);
  backdrop-filter: blur(6px) saturate(1.15);
}

/* Entrance: a soft spring (scale .96 to 1, rise 12 px, fade). Exit: a quick settle and fade. */
dialog.gm[open] { animation: gm-in var(--dur-3) var(--ease-spring) both; }
dialog.gm[open]::backdrop { animation: gm-fade-in var(--dur-2) var(--ease-out) both; }
dialog.gm.is-closing { animation: gm-out 200ms var(--ease-out) both; }
dialog.gm.is-closing::backdrop { animation: gm-fade-out 200ms var(--ease-out) both; }

.gm-card {
  position: relative;
  display: grid;
  justify-items: center;
  width: 100%;
  max-height: calc(100dvh - 32px);
  margin: 0;
  padding: var(--space-8) var(--space-7) var(--space-6);
  overflow: hidden auto;
  overscroll-behavior: contain;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--surface);
  box-shadow: var(--shadow-3);
  text-align: center;
}

/* A faint wash of the tone behind the icon. */
.gm-card::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 150px;
  background: radial-gradient(60% 100% at 50% 0%, color-mix(in oklch, var(--tone) 13%, transparent), transparent);
  pointer-events: none;
}

.gm-accent {
  position: absolute;
  inset: 0 0 auto;
  height: 4px;
  background: linear-gradient(90deg, var(--tone), color-mix(in oklch, var(--tone) 55%, var(--accent)));
}

.gm-card > :not(.gm-accent) { position: relative; }

/* The status icon: the ring springs in and pulses, then the symbol draws itself. */
.gm-icon {
  position: relative;
  width: 64px;
  height: 64px;
  margin-bottom: var(--space-4);
  color: var(--tone);
}

.gm-icon:empty { display: none; }

.gm-icon::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 2px solid var(--tone);
  border-radius: 50%;
  opacity: 0;
  pointer-events: none;
  animation: gm-halo 1.6s var(--ease-out) 480ms 2;
}

.gm-svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.gm-ring {
  fill: var(--tone-soft);
  stroke: var(--tone);
  stroke-width: 2;
  transform-box: fill-box;
  transform-origin: center;
  animation:
    gm-ring-in 540ms var(--ease-spring) both,
    gm-ring-pulse 1.6s var(--ease-in-out) 900ms 2;
}

/* --len is the symbol's exact stroke length, set by the runtime when it draws the icon. */
.gm-draw {
  --gm-len: calc((var(--len, 60) + 1) * 1px);
  fill: none;
  stroke: var(--tone);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: var(--gm-len);
  stroke-dashoffset: var(--gm-len);
  animation: gm-draw 620ms var(--ease-out) 280ms forwards;
}

.gm.is-closing .gm-icon::after,
.gm.is-closing .gm-ring {
  animation: none;
}

.gm-title {
  margin: 0 0 6px;
  color: var(--text);
  font-size: var(--step-2);
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.018em;
  text-wrap: balance;
}

.gm-msg {
  max-width: 38ch;
  color: var(--text-2);
  font-size: var(--step-0);
  line-height: 1.55;
  overflow-wrap: anywhere;
  text-wrap: pretty;
  white-space: pre-line;
}

.gm-msg:empty { display: none; }

/* Problems, one per line (field errors, the failing checks before real money can be armed). */
.gm-list {
  display: grid;
  gap: 6px;
  width: 100%;
  margin: var(--space-4) 0 0;
  padding: var(--space-3) var(--space-4);
  border: 1px solid color-mix(in oklch, var(--tone) 22%, var(--border));
  border-radius: var(--radius-md);
  background: color-mix(in oklch, var(--tone-soft) 70%, var(--surface));
  color: var(--text);
  font-size: var(--step--1);
  line-height: 1.45;
  list-style: none;
  text-align: left;
}

.gm-list li {
  position: relative;
  padding-left: 18px;
  overflow-wrap: anywhere;
}

.gm-list li::before {
  content: "";
  position: absolute;
  top: 0.55em;
  left: 3px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--tone);
}

.gm-ref {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  margin-top: var(--space-4);
  padding: 3px 3px 3px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface-2);
  color: var(--text-3);
  font-size: 12px;
}

.gm-ref code {
  min-width: 0;
  padding: 0;
  overflow: hidden;
  background: none;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.gm-copy {
  display: inline-grid;
  flex: none;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-3);
  transition: background-color var(--dur-1) ease, color var(--dur-1) ease;
}

.gm-copy:hover {
  background: var(--surface-3);
  color: var(--text);
}

.gm-copy[data-copied="true"] {
  background: var(--success-soft);
  color: var(--success);
}

.gm-copy[data-copied="false"] {
  background: var(--danger-soft);
  color: var(--danger);
}

.gm-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  width: 100%;
  margin-top: var(--space-6);
}

.gm-actions .btn {
  flex: 1 1 140px;
  min-height: 44px;
}

/* Confirmation fields: a typed word (for example STOP) and the password for critical actions. */
.gm-fields {
  display: grid;
  gap: var(--space-4);
  width: 100%;
  margin-top: var(--space-5);
  text-align: left;
}

.gm-fields:not(:has(> .field:not([hidden]))) { display: none; }

.input-confirm {
  font-family: var(--font-mono);
  font-weight: 600;
  letter-spacing: 0.32em;
  text-align: center;
  text-transform: uppercase;
}

.input-confirm::placeholder { letter-spacing: normal; }

/* ==========================================================================================================
   Busy lock (dialog.busy): every database write holds the screen still until it is done
   ========================================================================================================== */

dialog.busy {
  width: auto;
  max-width: calc(100% - 32px);
  margin: auto;
  padding: 0;
  overflow: visible;
  border: 0;
  background: transparent;
  color: var(--text);
}

dialog.busy::backdrop {
  background: color-mix(in oklch, var(--scrim) 70%, transparent);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}

/* A short delay before it shows, so quick saves do not flash. */
dialog.busy[open] { animation: busy-in var(--dur-2) var(--ease-out) 120ms both; }
dialog.busy[open]::backdrop { animation: gm-fade-in var(--dur-2) var(--ease-out) 120ms both; }
dialog.busy.is-closing { animation: busy-out 160ms var(--ease-out) both; }
dialog.busy.is-closing::backdrop { animation: gm-fade-out 160ms var(--ease-out) both; }

.busy-card {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 220px;
  max-width: 380px;
  padding: 16px 22px 16px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-3);
}

.busy-spinner {
  flex: none;
  width: 28px;
  height: 28px;
  border: 3px solid color-mix(in oklch, var(--accent) 18%, transparent);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.busy-msg {
  min-width: 0;
  margin: 0;
  color: var(--text);
  font-size: var(--step-0);
  font-weight: 500;
  overflow-wrap: anywhere;
}

/* ==========================================================================================================
   Toasts: small confirmations that slide in, count down and pause while hovered
   ========================================================================================================== */

.toast-region {
  position: fixed;
  z-index: 90;
  right: max(20px, env(safe-area-inset-right));
  bottom: max(20px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
  width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}

.toast {
  --tone: var(--info);
  --tone-soft: var(--info-soft);
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: start;
  gap: 12px;
  width: 100%;
  padding: 12px 10px 13px 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: color-mix(in oklch, var(--surface) 94%, transparent);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  backdrop-filter: blur(14px) saturate(1.4);
  box-shadow: var(--shadow-2);
  color: var(--text);
  pointer-events: auto;
  opacity: 0;
  transform: translateX(28px) scale(0.98);
  transition: opacity var(--dur-2) var(--ease-out), transform var(--dur-3) var(--ease-spring);
}

.toast[data-variant="success"] { --tone: var(--success); --tone-soft: var(--success-soft); }
.toast[data-variant="danger"] { --tone: var(--danger); --tone-soft: var(--danger-soft); }
.toast[data-variant="warning"] { --tone: var(--warning); --tone-soft: var(--warning-soft); }
.toast[data-variant="info"] { --tone: var(--info); --tone-soft: var(--info-soft); }

.toast.is-in {
  opacity: 1;
  transform: none;
}

.toast.is-out {
  opacity: 0;
  transform: translateX(28px) scale(0.96);
  transition: opacity 200ms var(--ease-out), transform 260ms var(--ease-out);
}

.toast-icon {
  display: grid;
  flex: none;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--tone-soft);
  color: var(--tone);
}

.toast-icon svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.toast-msg {
  margin: 0;
  padding-top: 4px;
  color: var(--text);
  font-size: var(--step--1);
  font-weight: 500;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.toast-close {
  display: grid;
  flex: none;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-3);
  transition: background-color var(--dur-1) ease, color var(--dur-1) ease;
}

.toast-close:hover {
  background: var(--surface-3);
  color: var(--text);
}

.toast-close svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
}

.toast-progress {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: 2px;
  background: var(--tone);
  opacity: 0.6;
  transform-origin: left center;
  animation: toast-life var(--toast-ms, 4000ms) linear forwards;
}

.toast.is-paused .toast-progress { animation-play-state: paused; }

/* ==========================================================================================================
   Field errors (api() marks the fields the server named)
   ========================================================================================================== */

.field.has-error > label,
.field.has-error > legend {
  color: var(--danger);
}

.field.has-error :is(.input, .select, .textarea),
:is(.input, .select, .textarea)[aria-invalid="true"] {
  border-color: var(--danger);
  background-color: color-mix(in oklch, var(--danger-soft) 40%, var(--field-bg));
}

.field.has-error :is(.input, .select, .textarea):focus,
:is(.input, .select, .textarea)[aria-invalid="true"]:focus {
  border-color: var(--danger);
  box-shadow: 0 0 0 4px color-mix(in oklch, var(--danger) 22%, transparent);
}

.field-error {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  margin: 0;
  color: var(--danger);
  font-size: 12.5px;
  font-weight: 500;
  line-height: 1.4;
  animation: field-error-in var(--dur-3) var(--ease-out) both;
}

.field-error::before {
  content: "";
  flex: none;
  width: 14px;
  height: 14px;
  margin-top: 1px;
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M12 7.5v5.5M12 16.5h.01'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.2' stroke-linecap='round'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M12 7.5v5.5M12 16.5h.01'/%3E%3C/svg%3E") center / contain no-repeat;
}

.shake { animation: shake 450ms var(--ease-out); }

/* ==========================================================================================================
   Request progress bar (top of the window while api() requests run)
   ========================================================================================================== */

.nav-progress {
  position: fixed;
  z-index: 100;
  top: 0;
  right: 0;
  left: 0;
  height: 3px;
  pointer-events: none;
}

.nav-progress > span {
  display: block;
  height: 100%;
  border-radius: 0 3px 3px 0;
  background: linear-gradient(90deg, var(--accent), color-mix(in oklch, var(--accent) 45%, var(--viz-4)));
  box-shadow: 0 0 12px color-mix(in oklch, var(--accent) 60%, transparent);
  opacity: 0;
  transform: scaleX(0);
  transform-origin: left center;
}

/* Creeps toward 90 % while waiting, then completes and fades. */
.nav-progress.is-active > span {
  opacity: 1;
  animation: nav-creep 9s cubic-bezier(0.08, 0.82, 0.17, 1) forwards;
}

.nav-progress.is-done > span {
  opacity: 0;
  transform: scaleX(1);
  animation: none;
  transition: transform 220ms var(--ease-out), opacity 360ms ease 160ms;
}

/* ==========================================================================================================
   Error page
   ========================================================================================================== */

.page.error-shell {
  min-height: calc(100dvh - 140px);
  place-items: center;
  align-content: center;
}

.error-card {
  display: grid;
  justify-items: center;
  gap: var(--space-3);
  width: 100%;
  max-width: 540px;
  text-align: center;
}

.error-art {
  --tone: var(--warning);
  --tone-soft: var(--warning-soft);
  position: relative;
  display: grid;
  place-items: center;
  width: 148px;
  height: 148px;
  margin-bottom: var(--space-2);
}

.error-art[data-tone="danger"] { --tone: var(--danger); --tone-soft: var(--danger-soft); }

.error-ring {
  position: absolute;
  inset: 0;
  border: 1px solid color-mix(in oklch, var(--tone) 38%, transparent);
  border-radius: 50%;
  opacity: 0;
  animation: error-ripple 3.4s var(--ease-out) infinite;
}

.error-ring-2 { animation-delay: 1.7s; }

.error-tile {
  position: relative;
  display: grid;
  place-items: center;
  width: 76px;
  height: 76px;
  border-radius: 24px;
  background:
    radial-gradient(120% 120% at 30% 0%, color-mix(in oklch, var(--tone) 16%, transparent), transparent 70%),
    var(--tone-soft);
  color: var(--tone);
  box-shadow: inset 0 0 0 1px color-mix(in oklch, var(--tone) 24%, transparent), var(--shadow-2);
  animation: error-float 6s var(--ease-in-out) infinite;
}

.error-tile .icon-lg {
  width: 34px;
  height: 34px;
  stroke-width: 1.6;
}

.error-code {
  padding: 3px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  color: var(--text-3);
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.12em;
}

.error-title {
  font-size: var(--step-4);
  font-weight: 500;
  letter-spacing: -0.032em;
}

.error-msg {
  max-width: 46ch;
  color: var(--text-2);
  font-size: var(--step-0);
  line-height: 1.6;
}

.error-ref {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  color: var(--text-3);
  font-size: 12.5px;
}

.error-ref code {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.error-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: var(--space-4);
}

/* ==========================================================================================================
   Phones: dialogs become bottom sheets, toasts sit above the tab bar
   ========================================================================================================== */

@media (max-width: 639.98px) {
  dialog.gm {
    width: 100%;
    margin: auto 0 0;
  }

  .gm-card {
    max-height: calc(100dvh - 24px);
    padding: var(--space-7) var(--space-5) calc(var(--space-5) + env(safe-area-inset-bottom));
    border-bottom: 0;
    border-radius: 24px 24px 0 0;
  }

  dialog.gm[open] { animation: gm-sheet-in var(--dur-3) var(--ease-out) both; }
  dialog.gm.is-closing { animation: gm-sheet-out 240ms var(--ease-in) both; }

  .gm-actions { flex-direction: column-reverse; }

  .gm-actions .btn {
    flex: none;
    width: 100%;
  }

  .toast-region {
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    left: 12px;
    align-items: stretch;
    width: auto;
  }

  .app-tabbar ~ .toast-region {
    bottom: calc(var(--tabbar-h) + 10px + env(safe-area-inset-bottom));
  }

  .toast { transform: translateY(18px) scale(0.98); }

  .toast.is-out { transform: translateY(12px) scale(0.96); }

  .error-art {
    width: 124px;
    height: 124px;
  }

  .error-actions > .btn { flex: 1 1 100%; }
}

@media (pointer: coarse) {
  .gm-copy::after,
  .toast-close::after {
    content: "";
    position: absolute;
    inset: -8px;
  }

  .toast-close { position: relative; }
}

/* ==========================================================================================================
   Keyframes
   ========================================================================================================== */

@keyframes gm-in {
  from { opacity: 0; transform: translateY(12px) scale(0.96); }
  to { opacity: 1; transform: none; }
}

@keyframes gm-out {
  from { opacity: 1; transform: none; }
  to { opacity: 0; transform: translateY(8px) scale(0.97); }
}

@keyframes gm-sheet-in {
  from { transform: translateY(100%); }
  to { transform: none; }
}

@keyframes gm-sheet-out {
  from { transform: none; }
  to { transform: translateY(100%); }
}

@keyframes gm-fade-in { from { opacity: 0; } to { opacity: 1; } }

@keyframes gm-fade-out { from { opacity: 1; } to { opacity: 0; } }

@keyframes gm-ring-in {
  from { opacity: 0; transform: scale(0.55); }
  to { opacity: 1; transform: none; }
}

@keyframes gm-ring-pulse {
  0%, 100% { transform: none; }
  45% { transform: scale(1.06); }
}

@keyframes gm-draw { to { stroke-dashoffset: 0; } }

@keyframes gm-halo {
  0% { opacity: 0.45; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.6); }
}

@keyframes busy-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

@keyframes busy-out {
  from { opacity: 1; transform: none; }
  to { opacity: 0; transform: scale(0.98); }
}

@keyframes toast-life {
  from { transform: scaleX(1); }
  to { transform: scaleX(0); }
}

@keyframes field-error-in {
  from { opacity: 0; transform: translateY(-4px); }
}

@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(3px); }
  30%, 50%, 70% { transform: translateX(-5px); }
  40%, 60% { transform: translateX(5px); }
}

@keyframes nav-creep {
  from { transform: scaleX(0); }
  to { transform: scaleX(0.9); }
}

@keyframes error-ripple {
  0% { opacity: 0; transform: scale(0.5); }
  30% { opacity: 1; }
  100% { opacity: 0; transform: scale(1); }
}

@keyframes error-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

/* Reduced motion: the theme stylesheet shortens every animation to its final frame; the exits here still
   fire animationend, so dialogs close at once. */
@media (prefers-reduced-motion: reduce) {
  .gm-icon::after,
  .error-ring { display: none; }
}
