/**
 * HaLeonPanel Mobile Responsive & iOS PWA Architecture
 * Standardized Design System & Behavioral Rules
 */

/* 1. iOS Safe Areas & Dynamic Viewport Height & Core Variables */
:root {
  --kb: 0px;
  --dock-height: 60px;
  --hl-gutter: 0.75rem;
}

html {
  -webkit-text-size-adjust: 100%;
}

html, body {
  /* Prevent horizontal text piercing and table spill from expanding the document */
  overflow-x: clip;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

/* Mobile body padding to avoid content being hidden under the fixed bottom dock */
@media (max-width: 767px) {
  body:not(.hl-no-dock) {
    padding-bottom: calc(var(--dock-height) + env(safe-area-inset-bottom) + 12px) !important;
  }
}

/* 2. iOS Safari Input Focus Auto-Zoom Prevention */
@media (max-width: 767px) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select,
  textarea {
    font-size: 16px !important;
  }
}

/* 3. Touch target optimization & manipulation */
a, button, [role="button"], label, summary {
  touch-action: manipulation;
}

/* Fix for Chinese text squishing vertically in action buttons ("新\n建\n任\n务") */
button, a.btn, .btn, [role="button"] {
  white-space: nowrap;
}

/* Micro-interaction active feedback for mobile cards and dock items */
.hl-tap-feedback {
  transition: transform 0.1s ease, opacity 0.1s ease;
}
.hl-tap-feedback:active {
  transform: scale(0.96);
  opacity: 0.9;
}

/* 3.1 Global Defensive Text Piercing & Anti-Crush Rules (Mobile Screens) */
@media (max-width: 767px) {
  /* Using Claude-reviewed :where(main) selector to lower specificity */
  :where(main) :is(h1, h2, h3, h4, p, li, a, td, dd, span, code, .font-mono) {
    overflow-wrap: anywhere;
  }

  /* Safe grid items shrinkage without breaking flex layouts */
  :where(main) .grid > * {
    min-width: 0;
  }

  pre, code, kbd {
    max-width: 100%;
  }

  pre {
    overflow-x: auto;
  }

  /* Dedicated badge class: immune to flex row crush & wrapping */
  .hl-badge {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
  }

  /* Numeric, Currency and Timestamps resilience */
  .hl-num, time {
    white-space: nowrap !important;
    overflow-wrap: normal !important;
    font-variant-numeric: tabular-nums;
  }

  /* Neutralize nested double-padding on mobile so 360px viewport remains usable */
  main > :where(.max-w-7xl, .max-w-6xl, .max-w-5xl, .max-w-4xl, .max-w-\[1720px\], .max-w-\[1780px\], .max-w-\[1440px\], .container-fluid) {
    padding-left: 0.25rem !important;
    padding-right: 0.25rem !important;
  }
}

/* 4. Bottom Sheet & Modal System */
html.hl-sheet-open,
body.hl-sheet-open {
  overflow: hidden !important;
  overscroll-behavior: none !important;
}

.hl-sheet-content {
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 767px) {
  .hl-sheet-modal {
    max-height: calc(92vh - var(--kb)) !important;
    max-height: calc(92dvh - var(--kb)) !important;
    margin-bottom: var(--kb) !important;
    transition: max-height 0.2s ease, margin-bottom 0.2s ease, transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.2s ease;
  }
  .hl-sheet-footer {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom)) !important;
  }
}

/* Bottom Sheet Drag Indicator Handle */
.hl-drag-handle {
  width: 36px;
  height: 4px;
  border-radius: 9999px;
  background-color: #cbd5e1;
  margin: 0 auto 0.75rem auto;
  flex-shrink: 0;
}
.dark .hl-drag-handle {
  background-color: #4b5563;
}
@media (min-width: 768px) {
  .hl-drag-handle {
    display: none !important;
  }
}

/* Hide scrollbars while allowing touch swiping */
.hl-hide-scrollbar {
  -ms-overflow-style: none;
  scrollbar-width: none;
}
.hl-hide-scrollbar::-webkit-scrollbar {
  display: none;
}

/* 5. Generic Table-to-Card Engine (.hl-cards) */
@media (max-width: 767px) {
  table.hl-cards {
    display: block;
    width: 100%;
    border: none !important;
    background: transparent !important;
  }

  table.hl-cards thead {
    display: none !important;
  }

  table.hl-cards tbody {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    width: 100%;
  }

  table.hl-cards tr {
    display: flex;
    flex-direction: column;
    background: #ffffff;
    border-radius: 1rem;
    padding: 0.875rem 1rem;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.05), 0 1px 2px -1px rgba(0, 0, 0, 0.05);
    border: 1px solid #f1f5f9;
    gap: 0.35rem;
  }

  .dark table.hl-cards tr {
    background: #1f2937;
    border-color: #374151;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
  }

  /* Highlighted/Warning card variant (e.g. advance window / due alert) */
  table.hl-cards tr.hl-card-warn {
    border-left: 4px solid #f59e0b !important;
    background: #fffbeb !important;
  }
  .dark table.hl-cards tr.hl-card-warn {
    border-left-color: #f59e0b !important;
    background: rgba(120, 53, 15, 0.25) !important;
  }

  table.hl-cards td {
    display: grid;
    grid-template-columns: minmax(auto, 42%) minmax(0, 1fr);
    align-items: center;
    gap: 0.5rem;
    padding: 0.25rem 0 !important;
    border: none !important;
    font-size: 0.8125rem;
    text-align: end;
  }

  table.hl-cards td[data-label]::before {
    content: attr(data-label);
    font-size: 0.75rem;
    font-weight: 600;
    color: #64748b;
    text-align: start;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .dark table.hl-cards td[data-label]::before {
    color: #94a3b8;
  }

  /* Primary item cell (e.g. Server Name or Main Title) spans full width */
  table.hl-cards td.hl-primary {
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-start !important;
    justify-content: flex-start;
    text-align: start !important;
    padding-bottom: 0.35rem !important;
    border-bottom: 1px dashed #e2e8f0 !important;
    margin-bottom: 0.25rem;
    white-space: normal !important;
  }

  .dark table.hl-cards td.hl-primary {
    border-bottom-color: #374151 !important;
  }

  table.hl-cards td.hl-primary::before {
    display: none !important;
  }

  /* Actions container cell at card bottom */
  table.hl-cards td.hl-actions {
    display: flex !important;
    justify-content: flex-end !important;
    flex-wrap: wrap !important;
    padding-top: 0.5rem !important;
    margin-top: 0.25rem;
    border-top: 1px solid #f1f5f9 !important;
    gap: 0.5rem !important;
    white-space: normal !important;
  }

  table.hl-cards td.hl-actions > * {
    margin-left: 0 !important;
  }

  .dark table.hl-cards td.hl-actions {
    border-top-color: #374151 !important;
  }

  table.hl-cards td.hl-actions::before {
    display: none !important;
  }

  /* Hide minor columns on mobile */
  table.hl-cards td.hl-hide-sm {
    display: none !important;
  }

  /* Empty state or full-width colspan cells */
  table.hl-cards td[colspan] {
    display: flex !important;
    justify-content: center !important;
    text-align: center !important;
    width: 100% !important;
    padding: 1.5rem 0 !important;
  }

  table.hl-cards td[colspan]::before,
  table.hl-cards td.hl-hide-label::before {
    display: none !important;
  }

  /* Selection checkbox cell on card */
  table.hl-cards td.hl-select,
  table.hl-cards td[data-label="选择"] {
    display: flex !important;
    justify-content: flex-start !important;
    align-items: center !important;
    padding: 0.15rem 0 !important;
  }
  table.hl-cards td.hl-select::before,
  table.hl-cards td[data-label="选择"]::before {
    display: none !important;
  }

  /* Universal Swipe Action Rail (Fade Mask on right edge) */
  .hl-action-rail {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.25rem;
    margin-left: calc(-1 * var(--hl-gutter, 0.75rem));
    margin-right: calc(-1 * var(--hl-gutter, 0.75rem));
    padding-left: var(--hl-gutter, 0.75rem);
    padding-right: var(--hl-gutter, 0.75rem);
    mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent);
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 24px), transparent);
  }
}
