/* =====================================================
   components.css - reusable building blocks.
   Needs tokens.css to be loaded first.
   ===================================================== */

/* ---------- Basic reset ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: 1.5;
  color: var(--ink-700);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1,
h2,
h3 {
  color: var(--ink-900);
  line-height: 1.2;
}

img {
  max-width: 100%;
  display: block;
}

a {
  color: var(--red-600);
}

/* Visible keyboard focus on everything clickable */
:focus-visible {
  outline: 2px solid var(--ink-900);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Lets keyboard users jump past the header */
/* The browser's built-in [hidden] rule only covers HTML elements, so SVG icons need their own */
svg[hidden] {
  display: none;
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: -4rem;
  padding: var(--space-2) var(--space-4);
  background: var(--toast-bg);
  color: var(--toast-ink);
  border-radius: var(--radius-sm);
  z-index: 100;
}
.skip-link:focus {
  top: var(--space-4);
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-height: var(--touch-target);
  padding: 0 var(--space-5);
  font: inherit;
  font-weight: 600;
  font-size: var(--text-base);
  text-decoration: none;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition:
    background-color 0.15s,
    border-color 0.15s;
}

.btn-primary {
  background: var(--red-600);
  color: var(--on-red, #fff);
  --spinner-color: var(--on-red, #fff);
}
.btn-primary:hover {
  background: var(--red-700);
}

.btn-secondary {
  background: var(--surface);
  color: var(--ink-900);
  border-color: var(--control-edge);
  --spinner-color: var(--ink-900);
}
.btn-secondary:hover {
  background: var(--canvas);
}

.btn-block {
  width: 100%;
}

/* Loading state: set aria-busy="true" on a button to show the spinner */
.btn[aria-busy='true'] {
  position: relative;
  color: transparent;
  pointer-events: none;
}
.btn[aria-busy='true']::after {
  content: '';
  position: absolute;
  width: 1.1rem;
  height: 1.1rem;
  border: 2px solid var(--spinner-color, #fff);
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Small text-only button, for things like "Forgot password?" */
.link-btn {
  min-height: var(--touch-target);
  padding: var(--space-1) var(--space-3);
  font: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--red-600);
  background: none;
  border: none;
  cursor: pointer;
}
.link-btn:hover {
  text-decoration: underline;
}

/* ---------- Form fields ---------- */
.field {
  display: grid;
  gap: var(--space-2);
}
.field label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-900);
}

.input {
  width: 100%;
  min-height: var(--touch-target);
  padding: 0 var(--space-4);
  font: inherit;
  color: var(--ink-900);
  background: var(--surface);
  border: 1px solid var(--control-edge);
  border-radius: var(--radius-md);
  transition: border-color 0.15s;
}
.input::placeholder {
  color: var(--ink-400);
}
.input:hover {
  border-color: var(--ink-500);
}
.input:focus-visible {
  outline: 2px solid var(--ink-900);
  outline-offset: 1px;
  border-color: var(--ink-900);
}
.input[aria-invalid='true'] {
  border-color: var(--danger-600);
}

/* Input with an icon button inside its right edge (show / hide password) */
.input-group {
  position: relative;
}
.input-group .input {
  padding-right: calc(var(--touch-target) + var(--space-1));
}
.input-group .input-toggle {
  position: absolute;
  top: 0;
  right: 0;
  width: var(--touch-target);
  height: 100%;
  display: grid;
  place-items: center;
  padding: 0;
  color: var(--ink-500);
  background: none;
  border: none;
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  cursor: pointer;
}
.input-group .input-toggle:hover,
.input-group .input-toggle[aria-pressed='true'] {
  color: var(--ink-900);
}

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--card-edge);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

/* ---------- Inline alert (for form errors) ---------- */
.alert {
  display: flex;
  gap: var(--space-3);
  align-items: flex-start;
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  border-radius: var(--radius-md);
}
.alert[hidden] {
  display: none;
}
.alert-danger {
  color: var(--danger-600);
  background: var(--danger-50);
}
.alert-danger::before {
  content: '!';
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--on-red, #fff);
  background: var(--danger-600);
  border-radius: 50%;
}

.alert-warning {
  color: var(--warning-600);
  background: var(--warning-50);
}
.alert-warning::before {
  content: '!';
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--surface);
  background: var(--warning-600);
  border-radius: 50%;
}

/* ---------- Status badges ---------- */
.badge {
  display: inline-block;
  padding: 2px var(--space-3);
  font-size: var(--text-sm);
  font-weight: 600;
  border-radius: 999px;
  background: var(--canvas);
  color: var(--ink-700);
}
.badge-success {
  background: var(--success-50);
  color: var(--success-600);
}
.badge-warning {
  background: var(--warning-50);
  color: var(--warning-600);
}
.badge-danger {
  background: var(--danger-50);
  color: var(--danger-600);
}

/* ---------- Toasts (replace alert() popups) ---------- */
.toast-region {
  position: fixed;
  left: 50%;
  bottom: var(--space-5);
  transform: translateX(-50%);
  display: grid;
  gap: var(--space-2);
  width: min(26rem, calc(100vw - 2rem));
  z-index: 200;
}
.toast {
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--toast-ink);
  background: var(--toast-bg);
  border-left: 4px solid var(--info-600);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
}
.toast-success {
  border-left-color: var(--success-600);
}
.toast-error {
  border-left-color: var(--danger-600);
}
/* Two live lanes (polite + alert) share one fixed stack so they never overlap */
.toast-lane { display: grid; gap: var(--space-2); }
.toast-lane:empty { display: none; }
.toast { display: flex; align-items: flex-start; gap: var(--space-3); }
.toast-text { flex: 1; min-width: 0; }
.toast-close {
  flex: none; display: inline-grid; place-items: center;
  min-width: 2.25rem; min-height: 2.25rem; margin: calc(var(--space-2) * -1) calc(var(--space-3) * -1) calc(var(--space-2) * -1) 0;
  padding: 0; font: inherit; font-size: 1.5rem; line-height: 1;
  color: inherit; background: transparent; border: 0; border-radius: var(--radius-md); cursor: pointer;
}
.toast-close:hover { background: rgb(128 128 128 / 0.25); }
.toast-close:focus-visible { outline: 2px solid currentColor; outline-offset: -2px; }

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Respect people who turn off animations in their device settings */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* ---------- Added in phase 4 ---------- */
[hidden] { display: none !important; }

fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.field legend { padding: 0; margin-bottom: var(--space-2); font-size: var(--text-sm); font-weight: 600; color: var(--ink-900); }

/* Loading placeholder (give it a width and height) */
.skeleton {
  background: linear-gradient(90deg, var(--line) 25%, var(--canvas) 50%, var(--line) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s linear infinite;
  border-radius: var(--radius-md);
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* Either/or choice built from radio buttons: label > input + span */
.segmented { display: flex; border: 1px solid var(--control-edge); border-radius: var(--radius-md); overflow: hidden; }
.segmented label { position: relative; flex: 1; }
.segmented input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.segmented span { display: grid; place-items: center; min-height: var(--touch-target); font-weight: 600; color: var(--ink-700); }
.segmented input:checked + span { background: var(--ink-900); color: var(--surface); }
.segmented input:focus-visible + span { outline: 2px solid var(--red-600); outline-offset: -4px; }

/* Native <dialog> modal */
dialog.dialog { width: min(28rem, calc(100vw - 2rem)); padding: var(--space-5); border: none; border-radius: var(--radius-lg); box-shadow: var(--shadow-md); color: var(--ink-700); }
dialog.dialog::backdrop { background: rgba(32, 35, 40, 0.5); }
.dialog h2 { font-size: var(--text-xl); margin-bottom: var(--space-2); }
.dialog-actions { display: flex; justify-content: flex-end; gap: var(--space-3); margin-top: var(--space-5); }

/* ---------- Added in phase 5 ---------- */

/* Destructive action (delete). Outlined by default so it never looks like the main action; fills red on hover. */
.btn-danger {
  background: var(--surface);
  color: var(--danger-600);
  border-color: var(--danger-600);
  --spinner-color: var(--danger-600);
}
.btn-danger:hover { background: var(--danger-600); color: var(--on-red, #fff); }
/* Solid version, used on the final "Yes, delete" button in a confirm dialog */
.btn-danger.btn-solid { background: var(--danger-600); color: var(--on-red, #fff); --spinner-color: var(--on-red, #fff); }
.btn-danger.btn-solid:hover { background: var(--danger-700, #8f1c13); }

/* Compact button for table rows */
.btn-sm { min-height: 36px; padding: 0 var(--space-3); font-size: var(--text-sm); }

/* Message under a form field: error (!) or success (check). Icon + text, never colour alone. */
.field-msg { display: flex; gap: var(--space-2); align-items: baseline; font-size: var(--text-sm); }
.field-msg.err { color: var(--danger-600); }
.field-msg.err::before { content: '!'; font-weight: 700; }
.field-msg.ok { color: var(--success-600); }
.field-msg.ok::before { content: '\2713'; font-weight: 700; }
.field-msg.info { color: var(--ink-500); }
select.input { padding-right: var(--space-3); }

/* ---------- Added in phase 6 ---------- */

/* Dark theme: white text on the lighter dark-mode reds/greens would be hard to read, so buttons use dark text there */
@media (prefers-color-scheme: dark) { :root:not([data-theme='light']) { --danger-700: #ffb3ab; } }
:root[data-theme='dark'] { --danger-700: #ffb3ab; }

/* Placeholder-sized focus: dialogs and cards that receive programmatic focus show no stray ring */
dialog:focus { outline: none; }

/* While a dialog is open the page behind it stays put (needs :has; older browsers simply scroll behind it) */
body:has(dialog[open]) { overflow: hidden; }
dialog.dialog { overscroll-behavior: contain; }
/* A form dialog with unsaved edits shakes instead of closing when the dark area is clicked (js/utils/dialog.js) */
@keyframes dialog-nudge { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }
dialog.dialog[data-nudge] { animation: dialog-nudge 0.3s ease; }
@media (prefers-reduced-motion: reduce) { dialog.dialog[data-nudge] { animation: none; outline: 2px solid var(--red-600); } }

/* Icon-only or small controls get a full-size tap area */
.icon-btn { display: inline-grid; place-items: center; width: var(--touch-target); height: var(--touch-target); padding: 0; color: var(--ink-700); background: none; border: 1px solid var(--control-edge); border-radius: var(--radius-md); cursor: pointer; }
.icon-btn:hover { background: var(--canvas); color: var(--ink-900); }
.icon-btn svg { width: 20px; height: 20px; }
.theme-toggle .icon-sun { display: none; }
:root[data-theme='dark'] .theme-toggle .icon-sun { display: block; }
:root[data-theme='dark'] .theme-toggle .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .theme-toggle .icon-sun { display: block; }
  :root:not([data-theme='light']) .theme-toggle .icon-moon { display: none; }
}

/* ---------- Theme switch ----------
   A real on/off switch (role="switch", aria-checked). On means dark. theme.js keeps aria-checked in step with the theme. */
.theme-switch {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch-target);
  padding: 0;
  background: none;
  border: none;
  cursor: pointer;
}
.theme-switch-track {
  position: relative;
  display: block;
  flex: none;
  width: 3.5rem;
  height: 1.875rem;
  background: linear-gradient(145deg, #e1e4ea, #cbd0d9);
  border: 1px solid #aeb4bf;
  border-radius: 999px;
  box-shadow:
    0 1px 1px rgba(255, 255, 255, 0.7),
    inset 0 1px 2px rgba(32, 35, 40, 0.3),
    inset 0 -1px 1px rgba(255, 255, 255, 0.45);
  transition: box-shadow 0.22s ease;
}
.theme-switch-icon {
  display: grid;
  place-items: center;
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
  color: var(--ink-500);
}
.theme-switch-icon svg {
  width: 100%;
  height: 100%;
}
.theme-switch > .theme-switch-icon {
  display: none;
}
.theme-switch-thumb {
  position: absolute;
  top: 50%;
  left: 0.15rem;
  width: 1.4rem;
  height: 1.4rem;
  margin-top: -0.7rem;
  background: linear-gradient(145deg, #ffffff, #e8ebf0);
  border: 1px solid #ffffff;
  border-radius: 999px;
  box-shadow: 0 2px 5px rgba(32, 35, 40, 0.22);
  transition:
    transform 0.22s ease,
    background-color 0.22s ease,
    border-color 0.22s ease,
    box-shadow 0.22s ease;
  display: grid;
  place-items: center;
}
.theme-switch:not(.account-item)[aria-checked='true'] .theme-switch-thumb {
  transform: translateX(1.75rem);
}
.theme-switch-thumb svg {
  position: absolute;
  width: 0.85rem;
  height: 0.85rem;
  color: #30343c;
  opacity: 0;
  transform: scale(0.65) rotate(-25deg);
  visibility: hidden;
  transition:
    opacity 0.18s ease,
    transform 0.18s ease,
    visibility 0s linear 0.18s;
}
.theme-switch[aria-checked='false'] .theme-switch-thumb .ts-sun,
.theme-switch[aria-checked='true'] .theme-switch-thumb .ts-moon {
  opacity: 1;
  transform: scale(1) rotate(0);
  visibility: visible;
  transition-delay: 0s;
}
.account-item.theme-switch .theme-switch-track {
  width: 3.5rem;
  height: 1.875rem;
  background: var(--canvas);
  border-color: var(--control-edge);
  box-shadow: inset 0 2px 5px rgba(32, 35, 40, 0.12);
}
.account-item.theme-switch .theme-switch-track::after {
  display: none;
}
.account-item.theme-switch .theme-switch-thumb {
  left: 0.15rem;
  width: 1.4rem;
  height: 1.4rem;
  margin-top: -0.7rem;
  background: var(--surface);
  border-color: var(--control-edge);
  box-shadow: var(--shadow-sm);
}
.account-item.theme-switch[aria-checked='true'] .theme-switch-thumb {
  transform: translateX(1.75rem);
}
.account-item.theme-switch .theme-switch-thumb svg {
  width: 0.85rem;
  height: 0.85rem;
  color: var(--ink-700);
}
.theme-switch:focus-visible .theme-switch-track {
  outline: 3px solid var(--ink-900);
  outline-offset: 2px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .theme-switch:not(.account-item) .theme-switch-track {
    background: linear-gradient(145deg, #202431, #141720);
    border-color: #11141c;
    box-shadow:
    0 1px 1px rgba(190, 199, 222, 0.2),
    inset 0 1px 2px rgba(0, 0, 0, 0.78),
    inset 0 -1px 1px rgba(160, 173, 204, 0.22);
  }
  :root:not([data-theme='light']) .theme-switch:not(.account-item) .theme-switch-thumb {
    background: linear-gradient(145deg, #ffffff, #e8ebf0);
    border-color: #ffffff;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  }
  :root:not([data-theme='light']) .theme-switch:not(.account-item) .theme-switch-thumb svg {
    color: #30343c;
  }
}
:root[data-theme='dark'] .theme-switch:not(.account-item) .theme-switch-track {
  background: linear-gradient(145deg, #202431, #141720);
  border-color: #11141c;
  box-shadow:
    0 1px 1px rgba(190, 199, 222, 0.2),
    inset 0 1px 2px rgba(0, 0, 0, 0.78),
    inset 0 -1px 1px rgba(160, 173, 204, 0.22);
}
:root[data-theme='dark'] .theme-switch:not(.account-item) .theme-switch-thumb {
  background: linear-gradient(145deg, #ffffff, #e8ebf0);
  border-color: #ffffff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}
:root[data-theme='dark'] .theme-switch:not(.account-item) .theme-switch-thumb svg {
  color: #30343c;
}
@media (prefers-reduced-motion: reduce) {
  .theme-switch-track,
  .theme-switch-thumb,
  .theme-switch-thumb svg { transition: none; }
}
@media (forced-colors: active) {
  .theme-switch-track,
  .theme-switch-thumb { border: 1px solid ButtonText; }
}

/* Long words/emails never push a card wider than the screen */
.card, .alert, .toast { overflow-wrap: anywhere; }

/* Make sure text can be zoomed to 200% without hiding content under the fixed bars */
html { scroll-padding-top: 4.5rem; }

/* High-contrast / forced-colours mode: keep focus and borders visible */
@media (forced-colors: active) {
  .btn, .input, .card, .badge { border: 1px solid CanvasText; }
  .segmented input:checked + span { outline: 2px solid Highlight; outline-offset: -2px; }
}

/* Headings that receive focus from script (after a tab change) do not need a ring */
[tabindex='-1']:focus { outline: none; }
