/* ========== Reset & Base ========== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: var(--font-size-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body {
  font-family: var(--font-sans);
  color: var(--text-primary);
  background: var(--bg-secondary);
  transition: background var(--transition-base), color var(--transition-base);
}

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

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

ul, ol { list-style: none; }

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

/* ========== Scrollbar ========== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-primary); border-radius: var(--radius-full); }
::-webkit-scrollbar-thumb:hover { background: var(--text-tertiary); }

/* ========== Button ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: 8px 16px;
  font-size: var(--font-size-sm);
  font-weight: 500;
  line-height: 1;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
  user-select: none;
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}
.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
  border-color: var(--color-primary-hover);
}

.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border-color: var(--border-primary);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--border-primary);
}

.btn-danger {
  background: var(--color-danger);
  color: #fff;
  border-color: var(--color-danger);
}
.btn-danger:hover:not(:disabled) {
  background: #dc2626;
  border-color: #dc2626;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: transparent;
}
.btn-ghost:hover:not(:disabled) {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.btn-lg { padding: 10px 20px; font-size: var(--font-size-base); }

/* ========== Form ========== */
.form-group {
  margin-bottom: var(--space-lg);
}
.form-label {
  display: block;
  margin-bottom: var(--space-xs);
  font-size: var(--font-size-sm);
  font-weight: 500;
  color: var(--text-primary);
}
.form-input {
  width: 100%;
  padding: 8px 12px;
  font-size: var(--font-size-base);
  color: var(--text-primary);
  background: var(--bg-input);
  border: 1px solid var(--border-input);
  border-radius: var(--radius-md);
  outline: none;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
.form-input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}
.form-input::placeholder {
  color: var(--text-tertiary);
}
textarea.form-input {
  resize: vertical;
  min-height: 80px;
}
select.form-input {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

/* ========== Badge ========== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  font-size: var(--font-size-xs);
  font-weight: 500;
  border-radius: var(--radius-full);
  background: var(--bg-badge);
  color: var(--text-secondary);
}
.badge-primary { background: var(--color-primary-light); color: var(--color-primary); }
.badge-success { background: var(--color-success-light); color: var(--color-success); }
.badge-warning { background: var(--color-warning-light); color: var(--color-warning); }
.badge-danger { background: var(--color-danger-light); color: var(--color-danger); }
.badge-info { background: var(--color-info-light); color: var(--color-info); }

/* ========== Card ========== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-profile {
  margin-bottom: var(--space-lg);
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--border-primary);
}

.card-body {
  padding: var(--space-lg);
}
.card-header + .card-body {
  padding-top: var(--space-md);
}

/* ========== Table ========== */
.table-container {
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  padding: 12px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border-primary);
  font-size: var(--font-size-sm);
}
th {
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--bg-secondary);
  white-space: nowrap;
}
td {
  color: var(--text-primary);
}
tr:last-child td {
  border-bottom: none;
}
tr:hover td {
  background: var(--bg-secondary);
}

/* ========== Modal ========== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg-modal-overlay);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.2s ease;
}
.modal-overlay.active {
  display: flex;
}
.modal {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  width: 90%;
  max-width: 520px;
  max-height: 92vh;
  overflow-y: visible;
  animation: slideUp 0.25s ease;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-lg);
  border-bottom: 1px solid var(--border-primary);
}
.modal-title {
  font-size: var(--font-size-lg);
  font-weight: 600;
}
.modal-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
}
.modal-close:hover {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}
.modal-body {
  padding: var(--space-lg);
}
.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-lg);
  border-top: 1px solid var(--border-primary);
}

/* ========== Empty State ========== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-2xl);
  color: var(--text-tertiary);
  text-align: center;
}
.empty-state svg {
  width: 24px;
  height: 24px;
  margin-bottom: var(--space-md);
  opacity: 0.4;
}
.empty-state p {
  font-size: var(--font-size-base);
}

/* ========== Dropdown ========== */
.dropdown {
  position: relative;
  display: inline-block;
}
.dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  min-width: 160px;
  background: var(--bg-card);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  z-index: 100;
  padding: 4px;
  margin-top: 4px;
}
.dropdown-menu.show {
  display: block;
  animation: fadeIn 0.15s ease;
}
.dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  width: 100%;
  padding: 8px 12px;
  font-size: var(--font-size-sm);
  color: var(--text-primary);
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition-fast);
  text-align: left;
}
.dropdown-item:hover {
  background: var(--bg-secondary);
}
.dropdown-item-danger {
  color: var(--color-danger);
}
.dropdown-divider {
  height: 1px;
  background: var(--border-primary);
  margin: 4px 0;
}

/* ========== Animations ========== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ========== Toast ========== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}
.toast {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 12px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  font-size: var(--font-size-sm);
  animation: slideUp 0.3s ease;
  min-width: 280px;
}
.toast-success { border-left: 3px solid var(--color-success); }
.toast-error { border-left: 3px solid var(--color-danger); }
.toast-warning { border-left: 3px solid var(--color-warning); }
.toast-info { border-left: 3px solid var(--color-info); }

/* ========== Loading Spinner ========== */
.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border-primary);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
.spinner-lg {
  width: 36px;
  height: 36px;
  border-width: 3px;
}

/* ========== Back to Top ========== */
.back-to-top {
  position: fixed;
  bottom: 32px;
  right: 32px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-card);
  color: var(--text-secondary);
  border: 1px solid var(--border-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  z-index: 100;
}
.back-to-top:hover {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}
.back-to-top:active {
  transform: translateY(0);
}

/* ========================================================
   Mobile / iOS Global Optimizations
   ======================================================== */
html {
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  scroll-behavior: smooth;
}

body {
  -webkit-tap-highlight-color: transparent;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  padding-top: env(safe-area-inset-top, 0);
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

/* Touch-friendly global buttons/links */
a,
button,
input[type="button"],
input[type="submit"],
input[type="reset"],
input[type="checkbox"],
input[type="radio"],
select,
label[for] {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Active states for touch devices (faster feedback than hover) */
@media (hover: none) and (pointer: coarse) {
  a:active,
  button:active,
  .btn:active,
  .badge:active {
    opacity: 0.85;
    transform: scale(0.98);
    transition: transform 0.08s ease, opacity 0.08s ease;
  }
  .card:active,
  .stat-card:active {
    box-shadow: var(--shadow-sm);
    transform: translateY(0);
  }
  .home-link-card:active {
    transform: translateY(0);
    box-shadow: var(--shadow-sm);
  }
}

/* Prevent input zoom on iOS by ensuring >=16px font size */
input,
select,
textarea {
  -webkit-appearance: none;
  appearance: none;
}
input[type="checkbox"],
input[type="radio"] {
  -webkit-appearance: checkbox;
  appearance: auto;
}
input[type="radio"] {
  -webkit-appearance: radio;
}

/* Image rendering optimization */
img {
  -webkit-user-drag: none;
  user-select: none;
}

/* Scroll behavior on mobile */
@media (max-width: 768px) {
  body {
    overscroll-behavior-y: contain;
  }
  ::-webkit-scrollbar {
    width: 4px;
    height: 4px;
  }
}

/* Safe area for fixed elements */
@supports (padding: env(safe-area-inset-top)) {
 
  .home-sidebar-fab,
  .back-to-top {
    margin-bottom: env(safe-area-inset-bottom, 0);
  }
  .toast-container {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
}

