/* ==================== BASE RESET ==================== */

*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  scroll-behavior: smooth;
}

body {
  font-family: "Noto Sans Thai", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--text-primary);
  background: var(--app-bg);
  min-height: 100vh;
  overflow-x: hidden;
}

a {
  color: var(--primary-color);
  text-decoration: none;
  transition: color var(--transition-fast);
}

a:hover {
  color: var(--primary-dark);
}

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

button {
  font-family: inherit;
  cursor: pointer;
}

input,
textarea,
select {
  font-family: inherit;
  font-size: inherit;
}

/* ==================== ERROR DISPLAY ==================== */

.page-root > section {
  text-align: center;
  padding: 4rem 2rem;
  background: var(--surface-muted);
  border-radius: var(--radius-lg);
  margin: 3rem auto;
  max-width: 640px;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border-color);
}

.page-root > section h1 {
  font-size: var(--text-3xl);
  font-weight: var(--font-bold);
  color: var(--text-primary);
  margin-bottom: 1rem;
  line-height: var(--leading-tight);
}

.page-root > section h1::before {
  content: "⚠️";
  display: inline-block;
  margin-right: 0.5rem;
}

.page-root > section p {
  color: var(--text-secondary);
  font-size: var(--text-base);
  line-height: var(--leading-relaxed);
  margin-bottom: 1rem;
  max-width: 500px;
  margin-left: auto;
  margin-right: auto;
}

.page-root > section a {
  display: inline-block;
  margin-top: 1.5rem;
  padding: 0.75rem 1.5rem;
  background: var(--primary-color);
  color: var(--text-white);
  text-decoration: none;
  border-radius: var(--radius-md);
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  transition: all var(--transition-base);
  box-shadow: var(--shadow-sm);
}

.page-root > section a:hover {
  background: var(--primary-light);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.page-root > section a:active {
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}

/* ==================== TOAST NOTIFICATIONS ==================== */

.toast {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  min-width: 320px;
  max-width: 500px;
  padding: 1rem 1.25rem;
  background: var(--bg-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  border-left: 4px solid var(--primary-color);
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  animation: toast-slide-in 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: var(--z-toast);
  backdrop-filter: blur(10px);
  border: 1px solid var(--border-color);
}

.toast-success {
  border-left-color: var(--success-color);
  background: var(--success-bg);
}

.toast-error {
  border-left-color: var(--error-color);
  background: var(--error-bg);
}

.toast-warning {
  border-left-color: var(--warning-color);
  background: var(--warning-bg);
}

.toast-info {
  border-left-color: var(--info-color);
  background: var(--info-bg);
}

.toast-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
  line-height: 1;
}

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  color: var(--text-primary);
  margin-bottom: 0.25rem;
  line-height: var(--leading-tight);
}

.toast-message {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  line-height: var(--leading-normal);
}

@keyframes toast-slide-in {
  from {
    transform: translateX(calc(100% + 2rem));
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* ==================== FLASH POPUP ==================== */

.flash-popup {
  position: fixed;
  top: 1.5rem;
  right: 1.5rem;
  z-index: var(--z-toast);
  max-width: 400px;
  background: var(--bg-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  border: 1px solid var(--border-color);
  animation: flash-slide-down 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes flash-slide-down {
  from {
    transform: translateY(-100%);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.flash-popup__bar {
  height: 4px;
  width: 100%;
}

.flash-popup[data-type="success"] .flash-popup__bar {
  background: var(--success-color);
}

.flash-popup[data-type="error"] .flash-popup__bar {
  background: var(--error-color);
}

.flash-popup[data-type="warning"] .flash-popup__bar {
  background: var(--warning-color);
}

.flash-popup[data-type="info"] .flash-popup__bar {
  background: var(--info-color);
}

.flash-popup__content {
  padding: 1rem 1.25rem;
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
}

.flash-popup__icon {
  flex-shrink: 0;
  font-size: 1.25rem;
  line-height: 1;
}

.flash-popup[data-type="success"] .flash-popup__icon {
  color: var(--success-color);
}

.flash-popup[data-type="error"] .flash-popup__icon {
  color: var(--error-color);
}

.flash-popup[data-type="warning"] .flash-popup__icon {
  color: var(--warning-color);
}

.flash-popup[data-type="info"] .flash-popup__icon {
  color: var(--info-color);
}

.flash-popup__msg {
  flex: 1;
  color: var(--text-primary);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}

.flash-popup__actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0 1.25rem 1rem;
}

.flash-popup__close {
  background: var(--text-primary);
  color: var(--text-white);
  border: none;
  border-radius: var(--radius-md);
  padding: 0.5rem 1rem;
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  cursor: pointer;
  transition: all var(--transition-base);
}

.flash-popup__close:hover {
  background: var(--text-black);
  transform: translateY(-1px);
}

.flash-popup__close:active {
  transform: translateY(0);
}

.flash-popup__close:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.flash-popup__close.enabled {
  background: var(--primary-color);
}

.flash-popup__close.enabled:hover {
  background: var(--primary-dark);
}

.flash-popup__count {
  color: var(--text-tertiary);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
  white-space: nowrap;
}

/* ==================== BUTTONS ==================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.625rem 1.25rem;
  border-radius: var(--radius-md);
  font-weight: var(--font-semibold);
  font-size: var(--text-sm);
  text-decoration: none;
  border: 1px solid transparent;
  transition: all var(--transition-base);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  pointer-events: none;
}

.btn-primary {
  background: var(--primary-color);
  color: var(--text-white);
  box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
  background: var(--primary-light);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn-primary:active {
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}

.btn-secondary {
  background: var(--bg-white);
  color: var(--text-primary);
  border-color: var(--border-color);
}

.btn-secondary:hover {
  background: var(--surface-hover);
  border-color: var(--border-dark);
}

.btn-secondary:active {
  background: var(--surface-active);
}

.btn-success {
  background: var(--success-color);
  color: var(--text-white);
  box-shadow: var(--shadow-sm);
}

.btn-success:hover {
  background: var(--success-light);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn-error {
  background: var(--error-color);
  color: var(--text-white);
  box-shadow: var(--shadow-sm);
}

.btn-error:hover {
  background: var(--error-light);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

.btn-sm {
  padding: 0.5rem 1rem;
  font-size: var(--text-xs);
}

.btn-lg {
  padding: 0.875rem 1.75rem;
  font-size: var(--text-base);
}

/* ==================== FORM ELEMENTS ==================== */

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="url"],
input[type="search"],
input[type="date"],
textarea,
select {
  width: 100%;
  padding: 0.625rem 0.875rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  color: var(--text-primary);
  background: var(--bg-white);
  transition: all var(--transition-base);
}

input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: var(--focus-ring);
}

input:disabled,
textarea:disabled,
select:disabled {
  background: var(--surface-muted);
  cursor: not-allowed;
  opacity: 0.6;
}

textarea {
  resize: vertical;
  min-height: 100px;
}

/* ==================== MODAL ==================== */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-dark);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: var(--z-modal-backdrop);
  padding: 1rem;
  animation: modal-fade-in 0.2s ease;
  backdrop-filter: blur(4px);
}

@keyframes modal-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal {
  background: var(--bg-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2xl);
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow: hidden;
  animation: modal-scale-in 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: var(--z-modal);
}

@keyframes modal-scale-in {
  from {
    opacity: 0;
    transform: scale(0.95);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.modal-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--divider-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  margin: 0;
}

.modal-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-tertiary);
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
  transition: color var(--transition-fast);
  width: 2rem;
  height: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
}

.modal-close:hover {
  color: var(--text-primary);
  background: var(--surface-hover);
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  max-height: calc(90vh - 140px);
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--divider-color);
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

/* ==================== RESPONSIVE ==================== */

@media (max-width: 768px) {
  .toast,
  .flash-popup {
    left: 1rem;
    right: 1rem;
    max-width: calc(100% - 2rem);
  }

  .flash-popup {
    top: 1rem;
  }

  .toast {
    bottom: 1rem;
  }

  .page-root > section {
    margin: 2rem 1rem;
    padding: 3rem 1.5rem;
  }

  .modal {
    margin: 0;
    border-radius: var(--radius-lg);
  }
}

@media (max-width: 480px) {
  .btn {
    width: 100%;
  }

  .modal-footer {
    flex-direction: column;
  }

  .modal-footer .btn {
    width: 100%;
  }
}
