/* Material Design 3 Button Component
 * ==========================================================================
 * Following MD3 button specifications with BEM naming
 * Reference: https://m3.material.io/components/buttons
 */

/* Base Button
 * -------------------------------------------------------------------------- */

.button {
  /* Structure */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  position: relative;

  /* Sizing - MD3 default height is 40px */
  /* min-height: 40px; */
  padding-inline: 24px;
  padding-block: 10px;

  /* Typography */
  font-family: inherit;
  font-size: var(--font-size-x-small); /* 15.75px with 18px base */
  font-weight: 500;
  line-height: 1.25;
  letter-spacing: 0.1px;
  text-transform: none;
  text-decoration: none;
  white-space: nowrap;

  /* Interaction */
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;

  /* Appearance */
  border: none;
  border-radius: var(--radius-full);
  overflow: hidden;
  vertical-align: middle;
  isolation: isolate; /* Create stacking context for ripple */

  /* Transitions */
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
  transition-property: background-color, box-shadow, border-color, color;

  /* States */
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

/* Button with icon */
.button__icon {
  inline-size: 18px;
  block-size: 18px;
  flex-shrink: 0;
}

.button__icon + .button__label,
.button__label + .button__icon {
  margin-inline-start: 0.25rem;
}

/* Button Variants (MD3 Types)
 * -------------------------------------------------------------------------- */

/* Filled Button (highest emphasis) */
.button--filled {
  background: var(--color-primary);
  color: var(--color-ink--primary);
}

.button--filled:hover {
  box-shadow: 0 1px 2px 0 oklch(0 0 0 / 0.3), 0 1px 3px 1px oklch(0 0 0 / 0.15);
  background: oklch(from var(--color-primary) calc(l * 1.08) c h);
}

.button--filled:active {
  background: oklch(from var(--color-primary) calc(l * 0.92) c h);
}

.button--filled:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Outlined Button (medium emphasis) */
.button--outlined {
  background: transparent;
  color: var(--color-primary);
  border: 1px solid var(--color-border--light);
}

.button--outlined:hover {
  background: oklch(from var(--color-primary) l c h / 0.08);
  border-color: var(--color-primary);
}

.button--outlined:active {
  background: oklch(from var(--color-primary) l c h / 0.12);
}

.button--outlined:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
  border-color: var(--color-primary);
}

/* Text Button (low emphasis) */
.button--text {
  background: transparent;
  color: var(--color-primary);
  padding-inline: 12px;
}

.button--text:hover {
  background: oklch(from var(--color-primary) l c h / 0.08);
}

.button--text:active {
  background: oklch(from var(--color-primary) l c h / 0.12);
}

.button--text:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Elevated Button (medium emphasis with shadow) */
.button--elevated {
  background: var(--color-bg--surface);
  color: var(--color-primary);
  box-shadow:
    0 1px 2px 0 oklch(0 0 0 / 0.3),
    0 1px 3px 1px oklch(0 0 0 / 0.15);
}

.button--elevated:hover {
  box-shadow:
    0 2px 4px 0 oklch(0 0 0 / 0.3),
    0 4px 6px 1px oklch(0 0 0 / 0.15);
  background: oklch(from var(--color-bg--surface) calc(l * 1.08) c h / 0.95);
}

.button--elevated:active {
  box-shadow:
    0 1px 2px 0 oklch(0 0 0 / 0.3),
    0 1px 3px 1px oklch(0 0 0 / 0.15);
  background: oklch(from var(--color-bg--surface) calc(l * 0.92) c h);
}

.button--elevated:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Tonal Button (medium emphasis, filled with secondary container) */
.button--tonal {
  background: var(--color-bg--primary-container);
  color: var(--color-ink--primary-container);
}

.button--tonal:hover {
  box-shadow: 0 1px 2px 0 oklch(0 0 0 / 0.3), 0 1px 3px 1px oklch(0 0 0 / 0.15);
  background: oklch(from var(--color-bg--primary-container) calc(l * 0.95) c h);
}

.button--tonal:active {
  background: oklch(from var(--color-bg--primary-container) calc(l * 0.9) c h);
}

.button--tonal:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Icon Buttons
 * -------------------------------------------------------------------------- */

.button--icon-only {
  inline-size: 40px;
  padding-inline: 0;
  border-radius: 50%;
}

.button--icon-only .button__icon {
  margin: 0;
}

/* Button Sizes
 * -------------------------------------------------------------------------- */

.button--small {
  min-height: 32px;
  padding-inline: 16px;
  padding-block: 6px;
  font-size: var(--font-size-x-small); /* 13.5px with 18px base */
}

.button--large {
  min-height: 48px;
  padding-inline: 32px;
  padding-block: 14px;
  font-size: var(--font-size-small); /* 18px with 18px base */
}

/* Button States
 * -------------------------------------------------------------------------- */

.button:disabled,
.button--disabled {
  cursor: not-allowed;
  opacity: 0.38;
  pointer-events: none;
}

.button--loading {
  color: transparent;
  pointer-events: none;
}

.button--loading::after {
  content: "";
  position: absolute;
  inline-size: 16px;
  block-size: 16px;
  inset: 50%;
  margin: -8px 0 0 -8px;
  border: 2px solid currentColor;
  border-color: var(--color-primary) transparent var(--color-primary) transparent;
  border-radius: 50%;
  animation: button-loading 0.8s linear infinite;
}

@keyframes button-loading {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Button Group
 * -------------------------------------------------------------------------- */

.button-group {
  display: inline-flex;
  gap: 0.5rem;
  align-items: center;
}

/* Responsive: Stack buttons vertically on mobile */
@media (max-width: 640px) {
  .button-group {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .button-group > .button,
  .button-group > button[type="submit"] {
    width: 100%;
  }

  .button-group .schedule-details {
    width: 100%;
    max-width: 100%;
  }

  .button-group .schedule-details summary {
    width: 100%;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .button-group .context-menu {
    align-self: flex-end;
  }
}

.button-group--vertical {
  flex-direction: column;
  align-items: stretch;
}

.button-group--attached {
  gap: 0;
}

.button-group--attached .button {
  border-radius: 0;
}

.button-group--attached .button:first-child {
  border-start-start-radius: 20px;
  border-end-start-radius: 20px;
}

.button-group--attached .button:last-child {
  border-start-end-radius: 20px;
  border-end-end-radius: 20px;
}

/* Color Variants
 * -------------------------------------------------------------------------- */

/* Secondary color */
.button--secondary.button--filled {
  background: var(--color-secondary);
  color: var(--color-ink--secondary);
}

.button--secondary.button--tonal {
  background: var(--color-bg--secondary-container);
  color: var(--color-ink--secondary-container);
}

.button--secondary.button--outlined,
.button--secondary.button--text,
.button--secondary.button--elevated {
  color: var(--color-secondary);
}

/* Tertiary color */
.button--tertiary.button--filled {
  background: var(--color-tertiary);
  color: var(--color-ink--tertiary);
}

.button--tertiary.button--tonal {
  background: var(--color-bg--tertiary-container);
  color: var(--color-ink--tertiary-container);
}

.button--tertiary.button--outlined,
.button--tertiary.button--text,
.button--tertiary.button--elevated {
  color: var(--color-tertiary);
}

/* Error/Danger color */
.button--error.button--filled,
.button--danger.button--filled {
  background: var(--color-error);
  color: var(--color-ink--error);
}

.button--error.button--tonal,
.button--danger.button--tonal {
  background: var(--color-bg--error-container);
  color: var(--color-ink--error-container);
}

.button--error.button--outlined,
.button--error.button--text,
.button--error.button--elevated,
.button--danger.button--outlined,
.button--danger.button--text,
.button--danger.button--elevated {
  color: var(--color-error);
}

/* Full Width
 * -------------------------------------------------------------------------- */

.button--full-width {
  inline-size: 100%;
}

/* FAB (Floating Action Button)
 * -------------------------------------------------------------------------- */

.button--fab {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  inline-size: 56px;
  block-size: 56px;
  border-radius: 16px;
  padding: 0;
  background: var(--color-primary);
  color: var(--color-ink--primary);
  box-shadow:
    0 4px 8px 0 oklch(0 0 0 / 0.3),
    0 6px 12px 2px oklch(0 0 0 / 0.15);
}

.button--fab:hover {
  box-shadow:
    0 6px 12px 0 oklch(0 0 0 / 0.3),
    0 8px 16px 2px oklch(0 0 0 / 0.15);
}

.button--fab.button--small {
  inline-size: 40px;
  block-size: 40px;
  border-radius: 12px;
}

.button--fab.button--large {
  inline-size: 96px;
  block-size: 96px;
  border-radius: 28px;
}

/* Extended FAB */
.button--fab.button--extended {
  inline-size: auto;
  padding-inline: 16px;
}

/* Ripple Effect
 * -------------------------------------------------------------------------- */

/* Ripple animation keyframes */
@keyframes button-ripple {
  from {
    transform: translate(-50%, -50%) scale(0);
    opacity: 0.5;
  }
  to {
    transform: translate(-50%, -50%) scale(4);
    opacity: 0;
  }
}

/* Base ripple pseudo-element */
.button::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  transform: translate(-50%, -50%) scale(0);
  opacity: 0;
  pointer-events: none;
  z-index: -1; /* Below button content */
}

/* Trigger ripple on active state */
.button:active::after {
  animation: button-ripple 600ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Ripple colors for each variant */
.button--filled::after,
.button--elevated::after {
  background: radial-gradient(circle, oklch(100% 0 0 / 0.3) 0%, transparent 70%);
}

.button--tonal::after {
  background: radial-gradient(circle, oklch(from var(--color-primary) l c h / 0.3) 0%, transparent 70%);
}

.button--outlined::after,
.button--text::after {
  background: radial-gradient(circle, oklch(from var(--color-primary) l c h / 0.2) 0%, transparent 70%);
}

/* Danger button ripples */
.button--danger.button--filled::after {
  background: radial-gradient(circle, oklch(100% 0 0 / 0.3) 0%, transparent 70%);
}

.button--danger.button--tonal::after,
.button--danger.button--outlined::after,
.button--danger.button--text::after {
  background: radial-gradient(circle, oklch(from var(--color-error) l c h / 0.2) 0%, transparent 70%);
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  .button::after {
    animation: none !important;
    transition: opacity 200ms ease;
  }

  .button:active::after {
    animation: none;
    opacity: 0.1;
    transform: translate(-50%, -50%) scale(4);
  }
}

/* Dark mode adjustments
 * -------------------------------------------------------------------------- */

[data-color-scheme="dark"] {
  .button--elevated {
    box-shadow:
      0 1px 3px 1px oklch(0 0 0 / 0.15),
      0 1px 2px 0 oklch(0 0 0 / 0.3);
  }

  .button--elevated:hover {
    box-shadow:
      0 2px 6px 2px oklch(0 0 0 / 0.15),
      0 2px 4px 0 oklch(0 0 0 / 0.3);
  }

  .button--fab {
    box-shadow:
      0 4px 12px 2px oklch(0 0 0 / 0.15),
      0 4px 8px 0 oklch(0 0 0 / 0.3);
  }

  .button--fab:hover {
    box-shadow:
      0 6px 16px 2px oklch(0 0 0 / 0.15),
      0 6px 12px 0 oklch(0 0 0 / 0.3);
  }
}

/* System preference fallback */
@media (prefers-color-scheme: dark) {
  :root:not([data-color-scheme="light"]) {
    .button--elevated {
      box-shadow:
        0 1px 3px 1px oklch(0 0 0 / 0.15),
        0 1px 2px 0 oklch(0 0 0 / 0.3);
    }

    .button--elevated:hover {
      box-shadow:
        0 2px 6px 2px oklch(0 0 0 / 0.15),
        0 2px 4px 0 oklch(0 0 0 / 0.3);
    }
  }
}

/* Like Button
 * -------------------------------------------------------------------------- */

.button-icon {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.5rem;
  background: transparent;
  border: none;
  border-radius: var(--radius-medium);
  color: var(--color-ink--muted);
  font-size: var(--font-size-x-small);
  font-weight: 500;
  cursor: pointer;
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
}

.button-icon:hover {
  background: oklch(from var(--color-primary) l c h / 0.08);
  color: var(--color-primary);
}

.button-icon:active {
  background: oklch(from var(--color-primary) l c h / 0.12);
}

.button-icon svg {
  inline-size: 20px;
  block-size: 20px;
  flex-shrink: 0;
  stroke: currentColor;
  fill: none;
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

.button-icon.liked {
  color: var(--color-error);
}

.button-icon.liked svg {
  fill: currentColor;
}

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  .button-icon,
  .button-icon svg {
    transition: none;
  }
}