/* Shared surfaces */
.ui-card,
.panel {
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  background: var(--surface-card);
  box-shadow: var(--shadow-card);
}

.ui-elevated {
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  background: var(--surface-elevated);
  box-shadow: var(--shadow-elevated);
}

/* Buttons */
.ui-button,
.btn {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 14px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  background: var(--surface-elevated);
  font: inherit;
  font-weight: 750;
  line-height: 1;
  text-decoration: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.ui-button--primary,
.btn-primary {
  color: var(--text-on-accent);
  border-color: transparent;
  background: var(--accent-primary);
}

#appView .btn-light {
  color: var(--text-primary);
  border-color: var(--border-default);
  background: var(--surface-elevated);
  box-shadow: none;
}

.btn-account {
  width: 100%;
  justify-content: flex-start;
  color: var(--text-secondary);
  border-color: transparent;
  background: transparent;
}

.ui-button:hover:not(:disabled),
.btn:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: color-mix(in srgb, var(--surface-elevated) 88%, var(--accent-primary));
}

.ui-button--primary:hover:not(:disabled),
.btn-primary:hover:not(:disabled) {
  color: var(--text-on-accent);
  border-color: transparent;
  background: var(--accent-hover);
}

.btn-account:hover:not(:disabled) {
  color: var(--text-primary);
  border-color: transparent;
  background: var(--accent-soft);
}

.ui-button:disabled,
.ui-button[aria-busy="true"],
.btn:disabled,
.btn[aria-busy="true"] {
  cursor: wait;
  opacity: .64;
}

.btn.is-success {
  border-color: color-mix(in srgb, var(--status-success) 54%, transparent);
}

.btn.is-error {
  border-color: color-mix(in srgb, var(--status-danger) 58%, transparent);
}

/* Fields */
.ui-field,
.field {
  display: grid;
  gap: 7px;
}

.field label,
.ui-field label {
  color: var(--text-secondary);
  font-size: .78rem;
  font-weight: 700;
}

.ui-field input,
.ui-field select,
.ui-field textarea,
.ui-input,
.field input,
.field select,
.field textarea,
select.class-filter {
  min-height: 42px;
  width: 100%;
  padding: 0 12px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  outline: none;
  color: var(--text-primary);
  background: var(--surface-input);
  box-shadow: none;
  font: inherit;
  caret-color: var(--accent-primary);
}

.ui-field textarea,
.field textarea {
  min-height: 104px;
  padding-block: 10px;
  resize: vertical;
}

.ui-field input::placeholder,
.ui-field textarea::placeholder,
.field input::placeholder,
.field textarea::placeholder {
  color: var(--text-muted);
  opacity: .76;
}

.ui-field input:focus,
.ui-field select:focus,
.ui-field textarea:focus,
.ui-input:focus,
.field input:focus,
.field select:focus,
.field textarea:focus,
select.class-filter:focus {
  border-color: color-mix(in srgb, var(--accent-primary) 76%, var(--border-strong));
  box-shadow: 0 0 0 3px var(--accent-soft);
}

input[type="checkbox"],
input[type="radio"] {
  accent-color: var(--accent-primary);
}

/* Custom select — visual contract for every class filter */
.field,
.financial-head,
.reports-head,
.toolbar {
  position: relative;
}

.custom-select-host-open {
  z-index: 3000 !important;
}

#appView .custom-select {
  position: relative;
  min-width: 180px;
  max-width: 100%;
}

#appView .field .custom-select {
  width: 100%;
}

#appView .custom-select.is-open {
  z-index: 3100;
}

#appView .custom-select-native {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

#appView .custom-select-trigger {
  width: 100%;
  min-height: 42px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 12px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  background: var(--surface-input);
  box-shadow: none;
  font: inherit;
  font-size: .82rem;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

#appView .custom-select-trigger:hover:not(:disabled) {
  border-color: var(--border-strong);
  background: color-mix(in srgb, var(--surface-input) 90%, var(--accent-primary));
}

#appView .custom-select-trigger:focus-visible {
  outline: none;
  border-color: color-mix(in srgb, var(--accent-primary) 76%, var(--border-strong));
  box-shadow: 0 0 0 3px var(--accent-soft);
}

#appView .custom-select-trigger:disabled {
  opacity: .55;
  cursor: not-allowed;
}

#appView .custom-select-value {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#appView .custom-select-chevron {
  flex: 0 0 auto;
  color: var(--text-muted);
  font-size: 1rem;
  line-height: 1;
  transition: transform 260ms var(--motion-ease), color var(--motion-fast) ease;
}

#appView .custom-select.is-open .custom-select-chevron {
  transform: rotate(180deg);
}

#appView .custom-select-menu {
  position: absolute;
  top: calc(100% + 7px);
  left: 0;
  right: 0;
  z-index: 3200;
  max-height: min(280px, 42vh);
  padding: 6px;
  overflow: auto;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  background: var(--surface-elevated);
  box-shadow: var(--shadow-elevated);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(-10px) scale(.99);
  transform-origin: top center;
  clip-path: inset(0 0 100% 0);
  transition:
    opacity 180ms ease,
    transform 280ms var(--motion-ease),
    clip-path 280ms var(--motion-ease),
    visibility 0s linear 280ms;
  will-change: opacity, transform, clip-path;
}

#appView .custom-select.is-open .custom-select-menu {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(0) scale(1);
  clip-path: inset(0);
  transition-delay: 0s;
}

#appView .custom-select-option {
  width: 100%;
  min-height: 38px;
  display: flex;
  align-items: center;
  padding: 8px 10px;
  border: 0;
  border-radius: calc(var(--radius-sm) - 2px);
  color: var(--text-secondary);
  background: transparent;
  font: inherit;
  font-size: .8rem;
  font-weight: 650;
  text-align: left;
  cursor: pointer;
}

#appView .custom-select-option:hover:not(:disabled),
#appView .custom-select-option:focus-visible {
  outline: none;
  color: var(--text-primary);
  background: var(--accent-soft);
}

#appView .custom-select-option.is-selected,
#appView .custom-select-option[aria-selected="true"] {
  color: var(--text-on-accent);
  background: var(--accent-primary);
}

#appView .custom-select-option:disabled {
  opacity: .45;
  cursor: not-allowed;
}

.financial-head.custom-select-host-open,
.reports-head.custom-select-host-open {
  transform: none !important;
}

/* Metric cards */
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.stat {
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  background: var(--surface-card);
  box-shadow: var(--shadow-card);
}

.stat > span,
.stat > small {
  display: block;
  color: var(--text-muted);
  font-size: .7rem;
  font-weight: 750;
}

.stat > strong {
  display: block;
  margin-top: 7px;
  color: var(--text-primary);
  font-size: clamp(1.35rem, 2vw, 1.8rem);
  letter-spacing: -.025em;
}

.stat.featured {
  color: var(--text-on-accent);
  border-color: transparent;
  background: var(--accent-primary);
}

.stat.featured > span,
.stat.featured > small,
.stat.featured > strong {
  color: inherit;
}

/* Tables */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  border-radius: inherit;
}

table {
  width: 100%;
  border-collapse: collapse;
  color: var(--text-primary);
  background: transparent;
}

thead th {
  padding: 11px 13px;
  border-bottom: 1px solid var(--border-default);
  color: var(--text-muted);
  background: color-mix(in srgb, var(--surface-elevated) 68%, transparent);
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .035em;
  text-align: left;
  text-transform: uppercase;
  white-space: nowrap;
}

tbody td {
  padding: 13px;
  border-bottom: 1px solid var(--border-default);
  color: var(--text-secondary);
  font-size: .78rem;
  vertical-align: middle;
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:hover td {
  background: color-mix(in srgb, var(--accent-soft) 42%, transparent);
}

.class-name {
  color: var(--text-primary);
  font-weight: 750;
}

/* Payment/status controls */
.pill,
.ui-badge,
.badge {
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 9px;
  border: 1px solid var(--border-default);
  border-radius: 999px;
  color: var(--text-secondary);
  background: var(--surface-elevated);
  font: inherit;
  font-size: .7rem;
  font-weight: 800;
}

.pill.paid,
.ui-badge--success,
.badge.success {
  color: var(--status-success);
  border-color: color-mix(in srgb, var(--status-success) 36%, transparent);
  background: color-mix(in srgb, var(--status-success) 12%, var(--surface-card));
}

.pill.pending,
.ui-badge--warning,
.badge.warning {
  color: var(--status-warning);
  border-color: color-mix(in srgb, var(--status-warning) 36%, transparent);
  background: color-mix(in srgb, var(--status-warning) 12%, var(--surface-card));
}

.ui-badge--danger,
.badge.danger {
  color: var(--status-danger);
  border-color: color-mix(in srgb, var(--status-danger) 38%, transparent);
  background: color-mix(in srgb, var(--status-danger) 12%, var(--surface-card));
}

.month,
.icon-btn {
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--border-default);
  color: var(--text-secondary);
  background: var(--surface-elevated);
  box-shadow: none;
  font: inherit;
  cursor: pointer;
}

.month {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  font-size: .7rem;
  font-weight: 800;
}

.month.on {
  color: var(--text-on-accent);
  border-color: transparent;
  background: var(--accent-primary);
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  font-size: .82rem;
}

.icon-btn:hover:not(:disabled) {
  color: var(--text-primary);
  border-color: var(--border-strong);
  background: var(--accent-soft);
}

/* Switches */
.ui-switch {
  position: relative;
  width: 44px;
  height: 24px;
  display: inline-flex;
  flex: 0 0 auto;
}

.ui-switch input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.ui-switch-track {
  width: 100%;
  height: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface-input);
}

.ui-switch-track::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--text-muted);
}

.ui-switch input:checked + .ui-switch-track {
  border-color: transparent;
  background: var(--accent-primary);
}

.ui-switch input:checked + .ui-switch-track::after {
  transform: translateX(20px);
  background: var(--text-on-accent);
}

/* Progress */
.ui-progress,
.progress {
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--accent-soft);
}

.ui-progress > span,
.progress > span,
.progress > i {
  display: block;
  width: var(--progress, 0%);
  height: 100%;
  border-radius: inherit;
  background: var(--accent-primary);
}

.ui-status-success { color: var(--status-success); }
.ui-status-warning { color: var(--status-warning); }
.ui-status-danger { color: var(--status-danger); }

/* Dialogs */
#appView dialog {
  width: min(720px, calc(100vw - 28px));
  max-height: min(88vh, 860px);
  padding: 0;
  overflow: auto;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  background: var(--surface-elevated);
  box-shadow: var(--shadow-elevated);
}

#appView dialog::backdrop {
  background: color-mix(in srgb, var(--surface-page) 76%, transparent);
  backdrop-filter: blur(4px);
}

#appView .modal-head,
#appView .modal-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 18px 20px;
}

#appView .modal-head {
  border-bottom: 1px solid var(--border-default);
}

#appView .modal-head h2,
#appView .modal-head p {
  margin: 0;
}

#appView .modal-head h2 {
  color: var(--text-primary);
  font-family: inherit;
  font-size: 1.25rem;
}

#appView .modal-head p {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: .78rem;
}

#appView dialog form {
  padding: 20px;
}

#appView #classModal .class-item {
  background: var(--surface-elevated);
  border: 1px solid var(--border-default);
  color: var(--text-primary);
  box-shadow: none;
}

#appView #classModal .class-item strong {
  color: var(--text-primary);
}

#appView #classModal .class-item small {
  color: var(--text-muted);
}

#appView .financial-person-block {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  background: var(--surface-card);
  color: var(--text-primary);
  box-shadow: none;
}

#appView .financial-person-block h3 {
  margin: 0;
  color: var(--text-primary);
  font-family: inherit;
  font-size: .9rem;
}

#appView .financial-person-block .check {
  border-color: var(--border-default);
  background: var(--surface-elevated);
  color: var(--text-secondary);
  box-shadow: none;
}

#appView .financial-person-block input,
#appView .financial-person-block select,
#appView .month,
#appView .icon-btn {
  box-shadow: none;
}

#appView .modal-actions {
  margin: 20px -20px -20px;
  border-top: 1px solid var(--border-default);
  background: color-mix(in srgb, var(--surface-card) 74%, transparent);
}

#appView .close {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  background: var(--surface-card);
  font: inherit;
  font-size: 1.1rem;
  cursor: pointer;
}

#appView .close:hover {
  color: var(--text-primary);
  border-color: var(--border-strong);
  background: var(--accent-soft);
}

/* Toast / empty / error */
.toast,
.ui-toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 5000;
  max-width: min(360px, calc(100vw - 32px));
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  background: var(--surface-elevated);
  box-shadow: var(--shadow-elevated);
}

.empty,
.ui-empty,
.ui-error,
.loading-state {
  padding: 24px 16px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.55;
}

.empty b,
.ui-empty b,
.ui-error b {
  display: block;
  margin-bottom: 4px;
  color: var(--text-primary);
}

.ui-error {
  color: var(--status-danger);
}

/* Async state */
.is-loading {
  cursor: progress;
}

[aria-busy="true"] {
  --ui-busy-opacity: .72;
}

/* Skeleton system */
.ui-skeleton {
  position: relative;
  overflow: hidden;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--surface-elevated) 82%, var(--border-default));
}

.ui-skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(
    90deg,
    transparent,
    color-mix(in srgb, var(--text-primary) 7%, transparent),
    transparent
  );
  animation: ui-skeleton-shimmer 1.4s linear infinite;
}

.ui-skeleton-line {
  height: .8rem;
  border-radius: 999px;
}

.ui-skeleton-line--short { width: 42%; }
.ui-skeleton-line--medium { width: 68%; }
.ui-skeleton-line--long { width: 88%; }

.ui-skeleton-circle {
  aspect-ratio: 1;
  border-radius: 50%;
}

.ui-skeleton-card {
  min-height: 110px;
  border-radius: var(--radius-lg);
}

.ui-skeleton-row {
  min-height: 48px;
  border-radius: var(--radius-sm);
}

.ui-skeleton-chart {
  min-height: 240px;
  border-radius: var(--radius-lg);
}

@keyframes ui-skeleton-shimmer {
  to { transform: translateX(100%); }
}

@media (max-width: 900px) {
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 560px) {
  .stats {
    grid-template-columns: 1fr;
  }

  #appView .custom-select {
    width: 100%;
    min-width: 0;
  }

  #appView .modal-head,
  #appView .modal-actions {
    align-items: stretch;
  }

  #appView .modal-actions {
    flex-direction: column-reverse;
  }

  #appView .modal-actions .btn {
    width: 100%;
  }

  .toast,
  .ui-toast {
    right: 16px;
    bottom: calc(88px + env(safe-area-inset-bottom));
  }
}

@media (prefers-reduced-motion: reduce) {
  #appView .custom-select-menu,
  #appView .custom-select-chevron {
    transition-duration: .01ms !important;
  }

  #appView .custom-select-menu {
    transform: none;
    clip-path: inset(0);
  }
}