/* Action Button system. Pairs with assets/js/action-button.js.
 *
 * Two ways to use it:
 *  1. Existing buttons keep their own look. The script adds the class "ab" and
 *     builds the icon slot and state labels inside them (no redesign).
 *  2. New buttons use the standalone look: class "ab-btn" plus a variant
 *     (ab-primary, ab-secondary, ab-outline, ab-ghost, ab-destructive) and a
 *     size (ab-sm, ab-md, ab-lg).
 *
 * States are driven by data-state on the button: idle | loading | success | error.
 * Nothing in here changes the button's outer size: icons and labels are
 * positioned inside a fixed-width cell, so there are no layout shifts.
 */

:root{
  --ab-dur: .18s;
  --ab-ease: cubic-bezier(.2,.8,.2,1);
  --ab-spring: cubic-bezier(.34,1.56,.64,1);
  --ab-success: #1F7A4D;
  --ab-error: #B3412B;
  --ab-focus: #B8860B;
}

/* ---------- Structure shared by all action buttons ---------- */
.ab{
  position:relative;
  -webkit-tap-highlight-color:transparent;
  touch-action:manipulation;
  --ab-slot:1.15em;
  --ab-gap:.5em;
}
.ab:focus-visible{outline:3px solid var(--ab-focus); outline-offset:2px;}
.ab-inner{
  position:relative; display:inline-grid; align-items:center; justify-items:center;
  vertical-align:middle; max-width:100%;
}
.ab-labels{display:grid; grid-template-columns:minmax(0,1fr); align-items:center; justify-items:center; max-width:100%; min-width:var(--ab-need,0px);}
.ab-l{
  grid-area:1 / 1; width:max-content; max-width:100%; text-align:center;
  opacity:0; transform:translate3d(calc(var(--ab-shift,0px) + 0px), 5px, 0);
  transition:opacity var(--ab-dur) var(--ab-ease), transform var(--ab-dur) var(--ab-ease);
  pointer-events:none;
}
.ab[data-state="idle"] .ab-l-idle,
.ab[data-state="loading"] .ab-l-loading,
.ab[data-state="success"] .ab-l-success,
.ab[data-state="error"] .ab-l-error{
  opacity:1; pointer-events:auto;
  transform:translate3d(var(--ab-shift,0px), 0, 0);
}
/* Label slides over to make room for the icon (icon is positioned, never in flow). */
.ab[data-has-icon="1"], .ab:not([data-state="idle"]){--ab-shift:calc((var(--ab-slot) + var(--ab-gap)) / 2);}

/* Room for the icon when it is showing; long labels wrap instead of overflowing. */
.ab[data-has-icon="1"] .ab-l, .ab:not([data-state="idle"]) .ab-l{max-width:calc(100% - var(--ab-slot) - var(--ab-gap));}
.ab.ab-measuring .ab-l{white-space:nowrap; max-width:none !important;}

/* Icon slot: absolute, so it can never change the button's size. */
.ab-icon{
  position:absolute; left:50%; top:50%; width:var(--ab-slot); height:var(--ab-slot);
  margin-top:calc(var(--ab-slot) / -2);
  transform:translate3d(calc(var(--ab-t, 0px) / -2 - (var(--ab-slot) + var(--ab-gap)) / 2), 0, 0);
  transition:transform var(--ab-dur) var(--ab-ease), opacity var(--ab-dur) var(--ab-ease);
  opacity:0; pointer-events:none;
}
.ab[data-has-icon="1"] .ab-icon, .ab:not([data-state="idle"]) .ab-icon{opacity:1;}
.ab[data-state="idle"]{--ab-t:var(--ab-t-idle,0px);}
.ab[data-state="loading"]{--ab-t:var(--ab-t-loading,0px);}
.ab[data-state="success"]{--ab-t:var(--ab-t-success,0px);}
.ab[data-state="error"]{--ab-t:var(--ab-t-error,0px);}

.ab-g{
  position:absolute; inset:0; display:block; opacity:0;
  transform:scale(.55); transition:opacity var(--ab-dur) var(--ab-ease), transform .26s var(--ab-spring);
}
.ab-g svg{display:block; width:100%; height:100%; overflow:visible;}
.ab[data-state="idle"] .ab-g-idle,
.ab[data-state="loading"] .ab-g-spin,
.ab[data-state="success"] .ab-g-ok,
.ab[data-state="error"] .ab-g-err{opacity:1; transform:scale(1);}

/* Spinner: compositor-only rotation. */
.ab-g-spin svg{animation:abSpin .8s linear infinite; transform-origin:50% 50%;}
.ab-g-spin circle{fill:none; stroke:currentColor; stroke-width:2.4; stroke-linecap:round; stroke-dasharray:40 100;}
.ab-g-spin .ab-track{stroke-dasharray:none; opacity:.22;}
@keyframes abSpin{to{transform:rotate(360deg);}}

/* Check draws itself as the spinner scales away. */
.ab-g-ok path, .ab-g-err path, .ab-g-err circle{fill:none; stroke:currentColor; stroke-width:2.4; stroke-linecap:round; stroke-linejoin:round;}
.ab-g-ok path{stroke-dasharray:24; stroke-dashoffset:24; transition:stroke-dashoffset .34s var(--ab-ease) .1s;}
.ab[data-state="success"] .ab-g-ok path{stroke-dashoffset:0;}
.ab-g-err path{stroke-dasharray:14; stroke-dashoffset:14; transition:stroke-dashoffset .26s var(--ab-ease) .08s;}
.ab[data-state="error"] .ab-g-err path{stroke-dashoffset:0;}
.ab-g-idle svg{fill:none; stroke:currentColor; stroke-width:1.9; stroke-linecap:round; stroke-linejoin:round;}

/* Success and error rings (animated from script; static fallbacks here). */
.ab-ring{
  position:absolute; inset:0; border-radius:inherit; pointer-events:none; opacity:0;
  box-shadow:0 0 0 2px var(--ab-success);
}
.ab[data-state="error"]{box-shadow:0 0 0 2px var(--ab-error) !important;}

/* Busy: stays focusable and announced, but cannot be activated again. */
.ab[aria-disabled="true"]{cursor:progress;}
.ab[data-state="error"]{cursor:pointer;}
.ab[disabled]{opacity:.5; cursor:not-allowed; pointer-events:none;}

/* ---------- Toast for failures (role=alert, never shifts the page) ---------- */
.ab-toasts{
  position:fixed; left:0; right:0; bottom:16px; z-index:2500; display:flex; flex-direction:column;
  align-items:center; gap:8px; padding:0 16px; pointer-events:none;
}
.ab-toast{
  pointer-events:auto; display:flex; align-items:center; gap:12px; max-width:560px; width:100%;
  background:#fff; color:#241F18; border:1px solid #E0B5A8; border-left:4px solid var(--ab-error);
  border-radius:10px; padding:12px 14px; box-shadow:0 8px 28px rgba(16,24,40,.18);
  font-size:.95rem; line-height:1.4;
  animation:abToastIn .28s var(--ab-spring);
}
.ab-toast p{margin:0; flex:1; text-align:left;}
.ab-toast button{
  font:inherit; font-weight:700; min-height:36px; padding:0 12px; border-radius:8px; cursor:pointer;
  border:1px solid currentColor; background:transparent; color:var(--ab-error);
}
.ab-toast button.ab-toast-x{border:0; color:inherit; opacity:.65; font-size:1.2rem; padding:0 8px; line-height:1;}
.ab-toast button:focus-visible{outline:3px solid var(--ab-focus); outline-offset:2px;}
.ab-toast.is-leaving{opacity:0; transform:translateY(8px); transition:opacity .2s ease, transform .2s ease;}
@keyframes abToastIn{from{opacity:0; transform:translateY(12px) scale(.98);} to{opacity:1; transform:none;}}

/* ---------- Standalone premium look ---------- */
.ab-btn{
  --ab-bg:#173B2C; --ab-fg:#fff; --ab-bd:#173B2C; --ab-bg-h:#1F4D3A; --ab-bg-a:#12301F;
  display:inline-flex; align-items:center; justify-content:center;
  font-family:inherit; font-weight:600; font-size:.92rem; line-height:1; letter-spacing:.005em;
  min-height:40px; padding:0 18px; border-radius:10px; cursor:pointer;
  border:1px solid var(--ab-bd); background:var(--ab-bg); color:var(--ab-fg);
  box-shadow:0 1px 2px rgba(16,24,40,.10), inset 0 1px 0 rgba(255,255,255,.10);
  transition:background-color var(--ab-dur) var(--ab-ease), border-color var(--ab-dur) var(--ab-ease),
             box-shadow var(--ab-dur) var(--ab-ease), color var(--ab-dur) var(--ab-ease);
  text-decoration:none; user-select:none;
}
.ab-btn:hover{background:var(--ab-bg-h); box-shadow:0 4px 12px rgba(16,24,40,.16), inset 0 1px 0 rgba(255,255,255,.12);}
.ab-btn:active{background:var(--ab-bg-a); box-shadow:0 1px 2px rgba(16,24,40,.12);}
.ab-btn[aria-disabled="true"]:hover, .ab-btn[disabled]:hover{background:var(--ab-bg);}
.ab-sm{min-height:32px; padding:0 12px; font-size:.82rem; border-radius:8px;}
.ab-lg{min-height:48px; padding:0 24px; font-size:1rem; border-radius:12px;}
@media (pointer:coarse){ .ab-sm{min-height:44px;} .ab-btn{min-height:44px;} .ab-lg{min-height:52px;} }
.ab-btn.ab-block{display:flex; width:100%;}

.ab-secondary{--ab-bg:#EFE7CF; --ab-fg:#2B2417; --ab-bd:#D8CFAE; --ab-bg-h:#E7DDBF; --ab-bg-a:#DDD2AE;}
.ab-outline{--ab-bg:transparent; --ab-fg:#173B2C; --ab-bd:#173B2C; --ab-bg-h:rgba(23,59,44,.07); --ab-bg-a:rgba(23,59,44,.13); box-shadow:none;}
.ab-ghost{--ab-bg:transparent; --ab-fg:#173B2C; --ab-bd:transparent; --ab-bg-h:rgba(23,59,44,.08); --ab-bg-a:rgba(23,59,44,.14); box-shadow:none;}
.ab-destructive{--ab-bg:#B3412B; --ab-fg:#fff; --ab-bd:#B3412B; --ab-bg-h:#9A3522; --ab-bg-a:#852D1C;}

/* Dark mode, following the page's theme switch when there is one. */
@media (prefers-color-scheme: dark){
  :root:not([data-theme="light"]) .ab-btn{--ab-bg:#E3C171; --ab-fg:#14281F; --ab-bd:#E3C171; --ab-bg-h:#EBCF8C; --ab-bg-a:#D6B25C;}
  :root:not([data-theme="light"]) .ab-secondary{--ab-bg:#26332C; --ab-fg:#EDE6D2; --ab-bd:#3A4A41; --ab-bg-h:#2E3D35; --ab-bg-a:#35463C;}
  :root:not([data-theme="light"]) .ab-outline{--ab-bg:transparent; --ab-fg:#E3C171; --ab-bd:#E3C171; --ab-bg-h:rgba(227,193,113,.10); --ab-bg-a:rgba(227,193,113,.18);}
  :root:not([data-theme="light"]) .ab-ghost{--ab-bg:transparent; --ab-fg:#E3C171; --ab-bd:transparent; --ab-bg-h:rgba(227,193,113,.10); --ab-bg-a:rgba(227,193,113,.18);}
  :root:not([data-theme="light"]) .ab-destructive{--ab-bg:#D4604A; --ab-fg:#1A0C08; --ab-bd:#D4604A; --ab-bg-h:#DE735E; --ab-bg-a:#C9523B;}
  :root:not([data-theme="light"]){--ab-success:#4FBF86; --ab-error:#F08A74;}
  :root:not([data-theme="light"]) .ab-toast{background:#1E2A24; color:#EDE6D2; border-color:#5A3A33;}
}
:root[data-theme="dark"] .ab-btn{--ab-bg:#E3C171; --ab-fg:#14281F; --ab-bd:#E3C171; --ab-bg-h:#EBCF8C; --ab-bg-a:#D6B25C;}
:root[data-theme="dark"] .ab-secondary{--ab-bg:#26332C; --ab-fg:#EDE6D2; --ab-bd:#3A4A41; --ab-bg-h:#2E3D35; --ab-bg-a:#35463C;}
:root[data-theme="dark"] .ab-outline{--ab-bg:transparent; --ab-fg:#E3C171; --ab-bd:#E3C171; --ab-bg-h:rgba(227,193,113,.10); --ab-bg-a:rgba(227,193,113,.18);}
:root[data-theme="dark"] .ab-ghost{--ab-bg:transparent; --ab-fg:#E3C171; --ab-bd:transparent; --ab-bg-h:rgba(227,193,113,.10); --ab-bg-a:rgba(227,193,113,.18);}
:root[data-theme="dark"] .ab-destructive{--ab-bg:#D4604A; --ab-fg:#1A0C08; --ab-bd:#D4604A; --ab-bg-h:#DE735E; --ab-bg-a:#C9523B;}
:root[data-theme="dark"]{--ab-success:#4FBF86; --ab-error:#F08A74;}
:root[data-theme="dark"] .ab-toast{background:#1E2A24; color:#EDE6D2; border-color:#5A3A33;}

/* Reduced motion: no spinning, springing, sliding or drawing. States still change clearly. */
@media (prefers-reduced-motion: reduce){
  .ab, .ab *, .ab-btn, .ab-toast{transition-duration:.01ms !important; transition-delay:0s !important; animation-duration:.01ms !important;}
  .ab-g-spin svg{animation:none !important;}
  .ab-g-spin circle:not(.ab-track){stroke-dasharray:70 100;}
  .ab-g-ok path, .ab-g-err path{stroke-dashoffset:0 !important;}
  .ab-l{transform:none !important;}
}
