/* ============================================================
   PyFDA Web — MD3 Expressive Components
   ============================================================ */

/* ---------- Modal / Dialog ---------- */
.md-modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 5000;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--md-sys-motion-duration-medium) var(--md-sys-motion-easing-standard);
}

.md-modal-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.md-modal {
  background: var(--md-sys-color-surface-container-high);
  border-radius: var(--md-sys-shape-corner-extra-large);
  padding: var(--space-lg);
  min-width: 320px;
  max-width: 420px;
  width: 90vw;
  box-shadow: var(--md-sys-elevation-5);
  transform: translateY(24px) scale(0.95);
  transition: transform var(--md-sys-motion-duration-medium) var(--md-sys-motion-easing-emphasized);
}

.md-modal-backdrop.open .md-modal {
  transform: translateY(0) scale(1);
}

/* ---------- Cards ---------- */
.md-card {
  background: var(--md-sys-color-surface-container-low);
  border-radius: var(--md-sys-shape-corner-large);
  padding: var(--space-md);
  box-shadow: var(--md-sys-elevation-0);
  border: 1px solid var(--md-sys-color-outline-variant);
  transition: box-shadow var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
}

.md-card--elevated {
  box-shadow: var(--md-sys-elevation-1);
  border: none;
}

.md-card__title {
  font-family: var(--md-sys-typescale-display-font);
  font-size: var(--md-sys-typescale-title-medium);
  font-weight: 600;
  color: var(--md-sys-color-on-surface);
  margin-bottom: var(--space-md);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.md-card__title-icon {
  width: 20px;
  height: 20px;
  color: var(--md-sys-color-primary);
}

/* ---------- Segmented Button (MD3) ---------- */
.md-segmented {
  display: flex;
  border-radius: var(--md-sys-shape-corner-full);
  overflow: hidden;
  border: 1px solid var(--md-sys-color-outline);
  flex-wrap: wrap;
  gap: 0;
}

.md-segmented__btn {
  flex: 1;
  min-width: 0;
  padding: var(--space-sm) var(--space-md);
  font-family: var(--md-sys-typescale-body-font);
  font-size: var(--md-sys-typescale-label-large);
  font-weight: 500;
  color: var(--md-sys-color-on-surface);
  background: transparent;
  border: none;
  cursor: pointer;
  position: relative;
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    color var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    box-shadow var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
  border-right: 1px solid var(--md-sys-color-outline);
  white-space: nowrap;
  text-align: center;
  letter-spacing: 0.02em;
}

.md-segmented__btn:last-child {
  border-right: none;
}

@media (hover: hover) {
  .md-segmented__btn:hover {
    background: var(--md-sys-color-surface-container-high);
  }
}

.md-segmented__btn.active {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
  font-weight: 600;
  z-index: 1;
  box-shadow: inset 0 0 0 2px var(--md-sys-color-secondary-container);
}

/* ---------- Text Fields (Outlined MD3) ---------- */
.md-field {
  position: relative;
  margin-bottom: var(--space-sm);
}

.md-field__input {
  width: 100%;
  padding: 14px 16px;
  font-family: var(--md-sys-typescale-body-font);
  font-size: var(--md-sys-typescale-body-large);
  color: var(--md-sys-color-on-surface);
  background: transparent;
  border: 1.5px solid var(--md-sys-color-outline);
  border-radius: var(--md-sys-shape-corner-extra-small);
  outline: none;
  transition: border-color var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
  user-select: text;
  -webkit-user-select: text;
}

.md-field__input:focus {
  border-color: var(--md-sys-color-primary);
  border-width: 2px;
  padding: 13px 15px;
}

.md-field__input.error {
  border-color: var(--md-sys-color-error);
}

.md-field__input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background: var(--md-sys-color-surface-container);
}

.md-field__label {
  position: absolute;
  top: -8px;
  left: 12px;
  padding: 0 4px;
  font-size: var(--md-sys-typescale-label-medium);
  color: var(--md-sys-color-on-surface-variant);
  background: var(--md-sys-color-surface-container-low);
  pointer-events: none;
  transition: color var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
}

.md-field__input:focus+.md-field__label {
  color: var(--md-sys-color-primary);
}

.md-field__input.error+.md-field__label {
  color: var(--md-sys-color-error);
}

.md-field__helper {
  font-size: var(--md-sys-typescale-body-small);
  color: var(--md-sys-color-on-surface-variant);
  padding: var(--space-xs) var(--space-md) 0;
}

.md-field__helper.error {
  color: var(--md-sys-color-error);
}

/* ---------- Select (Outlined) ---------- */
.md-select {
  position: relative;
}

.md-select__input {
  width: 100%;
  padding: 14px 16px;
  font-family: var(--md-sys-typescale-body-font);
  font-size: var(--md-sys-typescale-body-large);
  color: var(--md-sys-color-on-surface);
  background: var(--md-sys-color-surface-container-low);
  border: 1.5px solid var(--md-sys-color-outline);
  border-radius: var(--md-sys-shape-corner-extra-small);
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24' fill='none' stroke='%2374777f' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 40px;
  transition: border-color var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
}

.md-select__input:focus {
  border-color: var(--md-sys-color-primary);
  border-width: 2px;
  padding: 13px 39px 13px 15px;
}

.md-select__label {
  position: absolute;
  top: -8px;
  left: 12px;
  padding: 0 4px;
  font-size: var(--md-sys-typescale-label-medium);
  color: var(--md-sys-color-on-surface-variant);
  background: var(--md-sys-color-surface-container-low);
  pointer-events: none;
}

/* ---------- Buttons ---------- */
.md-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: 10px 24px;
  font-family: var(--md-sys-typescale-body-font);
  font-size: var(--md-sys-typescale-label-large);
  font-weight: 500;
  letter-spacing: 0.02em;
  border: none;
  border-radius: var(--md-sys-shape-corner-full);
  cursor: pointer;
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    box-shadow var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    transform 100ms ease;
  outline: none;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
}

.md-btn:active {
  transform: scale(0.97);
}

.md-btn--filled {
  background: var(--md-sys-color-primary);
  color: var(--md-sys-color-on-primary);
}

@media (hover: hover) {
  .md-btn--filled:hover {
    box-shadow: var(--md-sys-elevation-1);
  }
}

.md-btn--tonal {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
}

@media (hover: hover) {
  .md-btn--tonal:hover {
    box-shadow: var(--md-sys-elevation-1);
  }
}

.md-btn--outlined {
  background: transparent;
  color: var(--md-sys-color-primary);
  border: 1px solid var(--md-sys-color-outline);
}

@media (hover: hover) {
  .md-btn--outlined:hover {
    background: var(--md-sys-color-primary-container);
  }
}

.md-btn--text {
  background: transparent;
  color: var(--md-sys-color-primary);
  padding: 10px 12px;
}

@media (hover: hover) {
  .md-btn--text:hover {
    background: var(--md-sys-color-primary-container);
  }
}

.md-btn--icon {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: var(--md-sys-shape-corner-full);
  background: transparent;
  color: var(--md-sys-color-on-surface-variant);
  border: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
  -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) {
  .md-btn--icon:hover {
    background: var(--md-sys-color-surface-container-highest);
  }
}

.md-btn--icon svg {
  width: 24px;
  height: 24px;
}

/* FAB */
.md-fab {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 16px 20px;
  background: var(--md-sys-color-primary-container);
  color: var(--md-sys-color-on-primary-container);
  border: none;
  border-radius: var(--md-sys-shape-corner-large);
  box-shadow: var(--md-sys-elevation-3);
  font-family: var(--md-sys-typescale-body-font);
  font-size: var(--md-sys-typescale-label-large);
  font-weight: 600;
  cursor: pointer;
  transition: box-shadow var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    transform 100ms ease;
  -webkit-tap-highlight-color: transparent;
  letter-spacing: 0.02em;
  width: 100%;
  justify-content: center;
}

@media (hover: hover) {
  .md-fab:hover {
    box-shadow: var(--md-sys-elevation-4);
  }
}

.md-fab:active {
  transform: scale(0.97);
}

.md-fab svg {
  width: 24px;
  height: 24px;
}

.md-fab:disabled {
  opacity: 0.5;
  pointer-events: none;
}

/* ---------- Data Table ---------- */
.md-table-wrap {
  overflow-x: auto;
  border-radius: var(--md-sys-shape-corner-medium);
  border: 1px solid var(--md-sys-color-outline-variant);
}

.md-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--md-sys-typescale-body-font);
  font-size: var(--md-sys-typescale-body-medium);
}

.md-table th {
  text-align: left;
  padding: var(--space-xs) var(--space-sm);
  font-weight: 600;
  font-size: var(--md-sys-typescale-label-medium);
  color: var(--md-sys-color-on-surface-variant);
  background: var(--md-sys-color-surface-container);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  white-space: nowrap;
}

.md-table td {
  padding: var(--space-xs) var(--space-sm);
  border-bottom: 1px solid var(--md-sys-color-outline-variant);
  color: var(--md-sys-color-on-surface);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.md-table tr:last-child td {
  border-bottom: none;
}

.md-table td[contenteditable="true"] {
  cursor: text;
  outline: none;
  border-radius: var(--md-sys-shape-corner-extra-small);
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
  user-select: text;
  -webkit-user-select: text;
}

.md-table td[contenteditable="true"]:focus {
  background: var(--md-sys-color-primary-container);
  box-shadow: inset 0 0 0 2px var(--md-sys-color-primary);
}

/* ---------- Chips ---------- */
.md-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 6px 16px;
  font-size: var(--md-sys-typescale-label-large);
  font-weight: 500;
  border: 1px solid var(--md-sys-color-outline);
  border-radius: var(--md-sys-shape-corner-small);
  background: transparent;
  color: var(--md-sys-color-on-surface);
  cursor: pointer;
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
  -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) {
  .md-chip:hover {
    background: var(--md-sys-color-surface-container-high);
  }
}

.md-chip.active {
  background: var(--md-sys-color-secondary-container);
  color: var(--md-sys-color-on-secondary-container);
  border-color: transparent;
}

/* ---------- Switch (Toggle) ---------- */
.md-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  cursor: pointer;
  font-size: var(--md-sys-typescale-body-medium);
  color: var(--md-sys-color-on-surface);
  -webkit-tap-highlight-color: transparent;
}

.md-switch__track {
  width: 52px;
  height: 32px;
  border-radius: var(--md-sys-shape-corner-full);
  background: var(--md-sys-color-surface-container-highest);
  border: 2px solid var(--md-sys-color-outline);
  position: relative;
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    border-color var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
}

.md-switch__thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--md-sys-color-outline);
  position: absolute;
  top: 50%;
  left: 6px;
  transform: translateY(-50%);
  transition: transform var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    width var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    height var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
}

.md-switch input {
  display: none;
}

.md-switch input:checked~.md-switch__track {
  background: var(--md-sys-color-primary);
  border-color: var(--md-sys-color-primary);
}

.md-switch input:checked~.md-switch__track .md-switch__thumb {
  background: var(--md-sys-color-on-primary);
  transform: translateY(-50%) translateX(16px);
  width: 24px;
  height: 24px;
}

/* ---------- Checkbox ---------- */
.md-checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  cursor: pointer;
  font-size: var(--md-sys-typescale-body-medium);
  color: var(--md-sys-color-on-surface);
  -webkit-tap-highlight-color: transparent;
}

.md-checkbox input {
  display: none;
}

.md-checkbox__box {
  width: 18px;
  height: 18px;
  border: 2px solid var(--md-sys-color-on-surface-variant);
  border-radius: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard),
    border-color var(--md-sys-motion-duration-short) var(--md-sys-motion-easing-standard);
}

.md-checkbox input:checked~.md-checkbox__box {
  background: var(--md-sys-color-primary);
  border-color: var(--md-sys-color-primary);
}

.md-checkbox__box svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: var(--md-sys-color-on-primary);
  stroke-width: 3;
  opacity: 0;
  transform: scale(0.5);
  transition: opacity 150ms, transform 150ms;
}

.md-checkbox input:checked~.md-checkbox__box svg {
  opacity: 1;
  transform: scale(1);
}

/* ---------- Divider ---------- */
.md-divider {
  height: 1px;
  background: var(--md-sys-color-outline-variant);
  border: none;
  margin: var(--space-sm) 0;
}

/* ---------- Section Label ---------- */
.md-section-label {
  font-size: var(--md-sys-typescale-label-medium);
  font-weight: 600;
  color: var(--md-sys-color-primary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: var(--space-sm) 0 var(--space-xs);
}

/* ---------- Tooltip ---------- */
[data-tooltip], [data-tooltip-bottom] {
  position: relative;
}

[data-tooltip]::after, [data-tooltip-bottom]::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  transform: translateX(-50%) scale(0.8);
  background: var(--md-sys-color-inverse-surface);
  color: var(--md-sys-color-inverse-on-surface);
  padding: 4px 12px;
  border-radius: var(--md-sys-shape-corner-extra-small);
  font-size: var(--md-sys-typescale-body-small);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 150ms, transform 150ms;
  z-index: 1000;
}

[data-tooltip]::after {
  bottom: calc(100% + 8px);
  content: attr(data-tooltip);
}

[data-tooltip-bottom]::after {
  top: calc(100% + 8px);
  content: attr(data-tooltip-bottom);
}

[data-tooltip]:hover::after, [data-tooltip-bottom]:hover::after {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

/* Completely eliminate tooltips on mobile */
@media (max-width: 600px) {
  [data-tooltip]::after,
  [data-tooltip]::before,
  [data-tooltip-bottom]::after,
  [data-tooltip-bottom]::before {
    display: none !important;
  }
}

/* ---------- Spinner ---------- */
.md-spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 3px solid var(--md-sys-color-surface-container-highest);
  border-top-color: var(--md-sys-color-primary);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

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

/* ---------- Badge ---------- */
.md-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  font-size: 11px;
  font-weight: 600;
  background: var(--md-sys-color-error);
  color: var(--md-sys-color-on-error);
  border-radius: var(--md-sys-shape-corner-full);
}

/* ---------- Row Helpers ---------- */
.row {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.row--between {
  justify-content: space-between;
}

.row--wrap {
  flex-wrap: wrap;
}

.col {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.gap-md {
  gap: var(--space-md);
}

.gap-lg {
  gap: var(--space-lg);
}

.w-full {
  width: 100%;
}

.mt-md {
  margin-top: var(--space-md);
}

.mb-md {
  margin-bottom: var(--space-md);
}

/* ---------- Small Select (Toolbar) ---------- */
.md-select--small {
  display: inline-block;
  vertical-align: middle;
}

.md-select--small .md-select__input {
  padding: 6px 28px 6px 10px;
  font-size: var(--md-sys-typescale-body-medium);
  font-weight: 500;
  height: 32px;
  background-position: right 6px center;
  background-size: 14px;
  background-color: var(--md-sys-color-surface-container-high);
  border-color: var(--md-sys-color-outline-variant);
  border-radius: var(--md-sys-shape-corner-small);
}

.md-select--small .md-select__input:focus {
  border-color: var(--md-sys-color-primary);
  border-width: 1.5px;
  padding: 6px 27px 6px 9px;
}

/* ---------- Mobile Enhancements ---------- */
@media (max-width: 600px) {
  .md-segmented {
    flex-wrap: nowrap;
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .md-segmented::-webkit-scrollbar {
    display: none;
  }

  .md-segmented__btn {
    flex-shrink: 0;
  }

  .md-select--small .md-select__input {
    height: 30px;
    font-size: 12px;
    padding: 4px 24px 4px 8px;
    background-position: right 4px center;
    background-size: 12px;
  }
  .md-select--small .md-select__input:focus {
    padding: 4px 23px 4px 7px;
  }
}