/* ============================================================
   v19 — Mobile Responsive + Performance + UX Polish
   모바일 반응형 강화, 터치 최적화, 모바일 하단 탭 바
   ============================================================ */

/* ─── Mobile Bottom Tab Bar ─── */
.mobile-tab-bar {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 90;
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  border-top: 1px solid var(--c-line);
  padding: 4px 0 calc(4px + env(safe-area-inset-bottom));
  height: calc(56px + env(safe-area-inset-bottom));
}
[data-theme="dark"] .mobile-tab-bar {
  background: rgba(10,24,22,0.92);
}
.mobile-tab-bar .tab-items {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 52px;
  max-width: 480px;
  margin: 0 auto;
}
.mobile-tab-bar .tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px 0;
  min-width: 56px;
  background: none;
  border: none;
  color: var(--c-text-mute);
  font-size: 10px;
  font-weight: 500;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s;
  position: relative;
}
.mobile-tab-bar .tab-item [data-lucide] {
  width: 22px; height: 22px;
  stroke-width: 1.8;
}
.mobile-tab-bar .tab-item.active {
  color: var(--c-primary);
  font-weight: 700;
}
.mobile-tab-bar .tab-item.active::after {
  content: '';
  position: absolute;
  top: -4px;
  left: 50%; transform: translateX(-50%);
  width: 24px; height: 3px;
  background: var(--c-primary);
  border-radius: 0 0 3px 3px;
}
.mobile-tab-bar .tab-item:active {
  transform: scale(0.92);
}

/* ─── Network Status Bar ─── */
.network-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 9998;
  padding: 6px 16px;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
}
.network-bar.show { transform: translateY(0); }
.network-bar.offline {
  background: var(--c-bad);
  color: #fff;
}
.network-bar.online {
  background: var(--c-good);
  color: #fff;
}
.network-bar.slow {
  background: var(--c-warn);
  color: #fff;
}

/* ─── Skeleton Loading ─── */
.skeleton {
  background: linear-gradient(90deg,
    var(--c-surface-2) 25%,
    var(--c-line-soft) 50%,
    var(--c-surface-2) 75%);
  background-size: 200% 100%;
  animation: sk-shimmer 1.5s ease-in-out infinite;
  border-radius: var(--r-2);
}
@keyframes sk-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.skeleton-line {
  height: 14px;
  margin-bottom: 8px;
  border-radius: 4px;
}
.skeleton-line.w60 { width: 60%; }
.skeleton-line.w80 { width: 80%; }
.skeleton-line.w40 { width: 40%; }
.skeleton-card {
  height: 120px;
  border-radius: var(--r-3);
}
.skeleton-circle {
  width: 40px; height: 40px;
  border-radius: 50%;
}

/* ─── Empty State ─── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
  color: var(--c-text-mute);
}
.empty-state [data-lucide] {
  width: 48px; height: 48px;
  margin-bottom: 12px;
  opacity: 0.4;
}
.empty-state .empty-title {
  font-size: var(--t-lg);
  font-weight: 600;
  color: var(--c-text-soft);
  margin-bottom: 4px;
}
.empty-state .empty-desc {
  font-size: var(--t-sm);
  max-width: 280px;
  line-height: 1.5;
}

/* ─── View Error Boundary ─── */
.view-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 24px;
  text-align: center;
}
.view-error [data-lucide] {
  width: 48px; height: 48px;
  color: var(--c-bad);
  margin-bottom: 16px;
}
.view-error .error-title {
  font-size: var(--t-lg);
  font-weight: 700;
  color: var(--c-text);
  margin-bottom: 6px;
}
.view-error .error-msg {
  font-size: var(--t-sm);
  color: var(--c-text-mute);
  max-width: 360px;
  margin-bottom: 16px;
  word-break: break-word;
}
.view-error .btn { margin-top: 4px; }

/* ─── Pull-to-Refresh ─── */
.ptr-spinner {
  position: fixed;
  top: calc(var(--top-h) + 8px);
  left: 50%; transform: translateX(-50%) scale(0);
  z-index: 100;
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  box-shadow: var(--sh-2);
  display: grid; place-items: center;
  transition: transform 0.2s;
}
.ptr-spinner.pulling { transform: translateX(-50%) scale(1); }
.ptr-spinner.refreshing {
  transform: translateX(-50%) scale(1);
}
.ptr-spinner [data-lucide] {
  width: 18px; height: 18px;
  color: var(--c-primary);
}
.ptr-spinner.refreshing [data-lucide] {
  animation: spin 0.9s linear infinite;
}

/* ─── Improved Touch Targets ─── */
@media (hover: none) and (pointer: coarse) {
  .btn, .chip, .pill { min-height: 40px; }
  .icon-btn { min-width: 40px; min-height: 40px; }
  .nav-item { min-height: 44px; padding: 10px 14px !important; }
  .rank-row { min-height: 44px; }
  .itab button { min-height: 38px; padding: 8px 14px; }
  .search-box { height: 38px; }
  .pm-row { padding: 12px 10px; }
  .tl-item { padding: 10px 12px; }
  input[type="text"], input[type="date"], input[type="number"],
  input[type="email"], input[type="tel"], select, textarea {
    min-height: 40px;
    font-size: 16px !important; /* prevent iOS zoom */
  }
}

/* ─── CRUD Modal Mobile Fullscreen ─── */
@media (max-width: 599px) {
  .crud-modal-backdrop .crud-modal {
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100vh !important;
    max-height: 100vh !important;
    border-radius: 0 !important;
    margin: 0 !important;
  }
  .crud-modal .crud-modal-head {
    position: sticky;
    top: 0;
    background: var(--c-surface);
    z-index: 5;
    border-bottom: 1px solid var(--c-line);
    padding: 12px 16px;
  }
  .crud-modal .crud-modal-foot {
    position: sticky;
    bottom: 0;
    background: var(--c-surface);
    border-top: 1px solid var(--c-line);
    padding: 12px 16px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }
}

/* ─── AI Chat Panel Mobile ─── */
@media (max-width: 599px) {
  .ai-panel {
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100vh !important;
    border-radius: 0 !important;
    z-index: 100 !important;
  }
}

/* ─── Responsive Table → Card (mobile) ─── */
@media (max-width: 599px) {
  .responsive-table {
    display: block;
  }
  .responsive-table thead { display: none; }
  .responsive-table tbody,
  .responsive-table tr {
    display: block;
  }
  .responsive-table tr {
    background: var(--c-surface);
    border: 1px solid var(--c-line);
    border-radius: var(--r-2);
    padding: 10px 12px;
    margin-bottom: 8px;
  }
  .responsive-table td {
    display: flex;
    justify-content: space-between;
    padding: 3px 0;
    border: none;
    font-size: 13px;
  }
  .responsive-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--c-text-soft);
    margin-right: 8px;
    flex-shrink: 0;
  }
}

/* ─── View-specific Mobile Fixes ─── */

/* Money: KPI row → 2 col on mobile */
@media (max-width: 599px) {
  .money-kpis {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  .money-kpis .kpi {
    padding: 10px !important;
  }
  .money-kpis .kpi-value {
    font-size: 20px !important;
  }
}

/* Stats: summary grid responsive */
@media (max-width: 599px) {
  .wr-summary {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .wr-stat-v { font-size: 16px; }
}

/* Home: widget grid on mobile */
@media (max-width: 599px) {
  .home-widgets {
    display: flex !important;
    flex-direction: column !important;
    gap: 10px !important;
  }
  .home-widgets .card {
    padding: 12px !important;
  }
}

/* Calendar day detail: full-width on mobile */
@media (max-width: 599px) {
  .day-detail {
    border-radius: 0 !important;
    margin: 0 -10px !important;
    padding: 12px !important;
  }
}

/* Chart containers: prevent overflow */
.chart-wrap {
  position: relative;
  width: 100%;
  overflow: hidden;
}
.chart-wrap canvas {
  max-width: 100% !important;
  height: auto !important;
}

/* ─── Scroll Snap for Horizontal Lists ─── */
.hscroll {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding-bottom: 4px;
}
.hscroll::-webkit-scrollbar { display: none; }
.hscroll > * {
  scroll-snap-align: start;
  flex-shrink: 0;
}

/* ─── Safe-area aware bottom padding for main view ─── */
@media (max-width: 899px) {
  .view-root {
    padding-bottom: calc(68px + env(safe-area-inset-bottom)) !important;
  }
  .toast-stack {
    bottom: calc(68px + env(safe-area-inset-bottom)) !important;
    right: 12px !important;
    left: 12px !important;
  }
  .toast {
    max-width: 100% !important;
  }
}

/* ─── Image Lazy Loading Fade-in ─── */
img[loading="lazy"] {
  opacity: 0;
  transition: opacity 0.3s ease;
}
img[loading="lazy"].loaded,
img[loading="lazy"][complete] {
  opacity: 1;
}
/* Force loaded images visible even without JS event */
img[loading="lazy"][src]:not([src=""]) {
  opacity: 1;
}

/* ─── Print improvements ─── */
@media print {
  .mobile-tab-bar,
  .network-bar,
  .ptr-spinner,
  .toast-stack { display: none !important; }
}

/* ─── Reduced Motion ─── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .skeleton { animation: none; }
}

/* ─── High Contrast Mode ─── */
@media (prefers-contrast: high) {
  :root {
    --c-line: #999;
    --c-text-mute: #555;
    --sh-1: 0 1px 3px rgba(0,0,0,.2);
  }
}

/* ─── Landscape Phone ─── */
@media (max-height: 500px) and (orientation: landscape) {
  .mobile-tab-bar { height: 44px; padding-bottom: 0; }
  .mobile-tab-bar .tab-items { height: 40px; }
  .mobile-tab-bar .tab-item { font-size: 0; gap: 0; }
  .mobile-tab-bar .tab-item [data-lucide] { width: 20px; height: 20px; }
  .view-root { padding-bottom: 52px !important; }
}
