:root {
  --bg: #0b0c0f;
  --card: #12141a;
  --muted: #79808b;
  --text: #e7ecf3;
  --brand: #0a84ff; /* iOS blue */
  --accent: #34c759; /* iOS green */
  --warn: #ff9f0a;
  --error: #ff453a;
  --border: #222633;
  --shadow: 0 8px 30px rgba(0,0,0,0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(1200px 800px at 20% -20%, rgba(10,132,255,0.15), transparent),
              radial-gradient(1200px 800px at 120% 20%, rgba(52,199,89,0.12), transparent),
              var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  background: rgba(18,20,26,0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 20px; }
.title { font-weight: 600; letter-spacing: 0.2px; }

.controls { 
  display: flex; 
  gap: 16px; 
  align-items: center; 
  flex-wrap: nowrap; 
  overflow-x: auto; 
  overflow-y: hidden; 
  -webkit-overflow-scrolling: touch; 
  overscroll-behavior-x: contain; 
  scrollbar-width: none; 
}
.controls::-webkit-scrollbar { display: none; }
.controls > .control { flex: 0 0 auto; min-width: max-content; }
.control { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.control select { 
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 10px;
  outline: none;
}
.control.switch { flex-direction: row; gap: 10px; align-items: center; }

body {
  display: block; /* Single document scroll; avoid nested body scroll */
  min-height: 100vh;
  overflow-x: hidden; /* Prevent page-level horizontal scroll; keep it inside .table-wrap */
}

.container { 
  max-width: 1100px; 
  margin: 0 auto; 
  padding: 32px 20px 20px 20px;
}

.searchbar { position: relative; }
#searchInput {
  width: 100%;
  padding: 16px 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  outline: none;
  box-shadow: var(--shadow);
}
.hint { color: var(--muted); margin-top: 8px; }

.suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 12px 12px;
  box-shadow: var(--shadow);
  max-height: 360px;
  overflow: auto;
  display: none;
}
.suggestions.visible { display: block; }
.suggestion-item { padding: 10px 12px; display: flex; align-items: center; gap: 10px; cursor: pointer; }
.suggestion-item:hover { background: #0f1420; }
.suggestion-item img { width: 28px; height: 28px; border-radius: 6px; }
.suggestion-title { font-size: 14px; }
.suggestion-sub { font-size: 12px; color: var(--muted); }

.app-summary { display: flex; gap: 16px; align-items: center; margin-top: 28px; padding: 16px; background: rgba(18,20,26,0.6); border: 1px solid var(--border); border-radius: 14px; }
.app-icon { width: 72px; height: 72px; border-radius: 16px; }
.app-name { font-size: 18px; font-weight: 600; }
.app-dev { font-size: 14px; color: var(--muted); }
.app-links a { color: var(--brand); text-decoration: none; }

.results { 
  margin-top: 24px; 
}
.results-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.progress { color: var(--muted); font-size: 12px; }
.progressbar { margin-top: 8px; width: 320px; max-width: 80vw; height: 8px; background: #1a2130; border-radius: 999px; overflow: hidden; border: 1px solid var(--border); }
.progressbar__fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--brand), var(--accent)); transition: width 0.3s ease; }
.filter { 
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--card); color: var(--text); outline: none; 
}

.table-wrap { 
  overflow-x: auto;
  overflow-y: auto; 
  background: rgba(18,20,26,0.6); 
  border: 1px solid var(--border); 
  border-radius: 14px;
  min-height: 300px; /* Minimum table height */
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Responsive adjustments for smaller screens */
@media (max-height: 600px) {
  .table-wrap {
    max-height: calc(100vh - 280px); /* Less space for header on small screens */
    min-height: 250px;
  }
}

@media (max-height: 500px) {
  .table-wrap {
    max-height: calc(100vh - 240px); /* Even less space for very small screens */
    min-height: 200px;
  }
}
.table { width: max-content; min-width: 100%; border-collapse: separate; border-spacing: 0; }
.table thead th { 
  text-align: left; 
  background: #12141a; 
  border-bottom: 1px solid var(--border); 
  padding: 12px; 
  font-weight: 600; 
  cursor: pointer;
  position: relative;
}
.table thead th:hover { background: #1a1f2b; }
.table thead th.sort-asc::after { 
  content: ' ▲'; 
  color: var(--brand); 
  font-size: 10px; 
  position: absolute; 
  right: 8px; 
  top: 50%; 
  transform: translateY(-50%);
}
.table thead th.sort-desc::after { 
  content: ' ▼'; 
  color: var(--brand); 
  font-size: 10px; 
  position: absolute; 
  right: 8px; 
  top: 50%; 
  transform: translateY(-50%);
}
.table tbody td { padding: 12px; border-bottom: 1px solid #1a1f2b; }
.table tbody tr:hover { background: #0f1420; }
.numeric { text-align: right; }

/* Sticky Header Styles */
.table.sticky-header thead th { 
  position: sticky; 
  top: 0; 
  z-index: 10;
  background: #12141a; /* Ensure sticky header overlays cells when scrolling */
}

/* Sticky First Column Styles */
.table.sticky-first-column th:first-child,
.table.sticky-first-column td:first-child {
  position: sticky;
  left: 0;
  background: #12141a;
  z-index: 5;
  border-right: 2px solid var(--border);
  box-shadow: 2px 0 0 rgba(0,0,0,0.25); /* Visual separation on mobile */
}

/* Mobile UX refinements for top bar and table navigation */
@media (max-width: 720px) {
  .topbar { 
    flex-direction: column; 
    align-items: stretch; 
    gap: 8px;
    padding: max(12px, env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 12px calc(16px + env(safe-area-inset-left));
  }
  .brand { justify-content: space-between; }
  .title { font-size: 16px; }
  .controls {
    display: flex;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    padding-bottom: 4px;
    touch-action: pan-x;
    gap: 12px;
  }
  .controls::-webkit-scrollbar { display: none; }
  .controls .control { flex: 0 0 auto; min-width: max-content; }

  /* Prevent iOS zoom on focus and increase touch targets */
  #searchInput,
  .filter,
  .control select,
  .country-selector-btn,
  .force-refresh-btn { font-size: 16px; }
  .control.switch input[type="checkbox"] { width: 22px; height: 22px; }

  /* Results header stacks; progressbar and filter go full width */
  .results-header { 
    flex-direction: column; 
    align-items: stretch; 
    gap: 8px; 
  }
  .progressbar { width: 100%; max-width: none; }
  .results-header .right { display: flex; gap: 8px; align-items: center; }
  .results-header .right .filter { flex: 1; }

  /* Table: allow full pan and encourage horizontal scrolling */
  .table-wrap {
    max-height: none;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-x pan-y;
  }
  .table thead th,
  .table tbody td { white-space: nowrap; }
}

/* When both sticky header and first column are active */
.table.sticky-header.sticky-first-column th:first-child {
  z-index: 15; /* Higher than other sticky elements */
  background: #12141a;
}

/* Hover effects for sticky first column */
.table.sticky-first-column tbody td:first-child {
  background: #131721;
}
.table.sticky-first-column tbody tr:hover td:first-child {
  background: #0f1420;
}

/* Drag and Drop Styles */
.iap-col {
  transition: all 0.2s ease;
}

.iap-col.drop-target {
  background: rgba(10, 132, 255, 0.2) !important;
  border: 2px dashed var(--brand);
}

.iap-col.drag-over {
  background: rgba(10, 132, 255, 0.4) !important;
  transform: scale(1.02);
}

.iap-col[draggable="true"] {
  cursor: grab;
}

.iap-col[draggable="true"]:active {
  cursor: grabbing;
}

/* Merged Column Styles */
.merged-column {
  background: rgba(52, 199, 89, 0.2) !important;
  border-left: 3px solid var(--accent);
}

.merge-indicator {
  font-size: 10px;
  color: var(--accent);
  font-weight: bold;
}

.merge-icon {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 4px;
  border-radius: 3px;
  background: rgba(52, 199, 89, 0.3);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s ease;
  user-select: none;
}

.merge-icon:hover {
  background: rgba(52, 199, 89, 0.6);
  transform: scale(1.1);
}

/* Merge Modal Styles */
.merged-columns-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 16px;
  max-height: 300px;
  overflow-y: auto;
}

.merged-column-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background: rgba(18, 20, 26, 0.8);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.column-name {
  font-weight: 500;
  flex: 1;
}

.split-btn {
  margin-left: 12px;
  padding: 6px 12px;
  font-size: 12px;
  background: var(--warn);
  border: 1px solid rgba(255, 159, 10, 0.3);
  color: white;
}

.split-btn:hover {
  background: rgba(255, 159, 10, 0.8);
}

.modal-description {
  color: var(--muted);
  margin: 0;
  font-size: 14px;
}

/* Split All Button Styles */
.split-all-item {
  display: flex;
  justify-content: center;
  margin-bottom: 8px;
}

.split-all-btn {
  padding: 10px 20px;
  font-size: 14px;
  background: var(--warn);
  border: 1px solid rgba(255, 159, 10, 0.3);
  color: white;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.split-all-btn:hover {
  background: rgba(255, 159, 10, 0.8);
  transform: translateY(-1px);
}

.separator {
  height: 1px;
  background: var(--border);
  margin: 12px 0;
}

.hidden { display: none; }

.footer { text-align: center; color: var(--muted); font-size: 12px; margin: 28px 0 40px; }
.footer a { color: var(--brand); text-decoration: none; }

/* Country Selector Button */
.country-selector-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 12px;
  transition: background-color 0.2s ease;
}
.country-selector-btn:hover {
  background: #1a1f2b;
}

.force-refresh-btn {
  padding: 8px 12px;
  border: 1px solid rgba(255, 159, 10, 0.3);
  border-radius: 6px;
  background: linear-gradient(135deg, var(--warn), rgba(255, 159, 10, 0.8));
  color: white;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.force-refresh-btn:hover {
  background: linear-gradient(135deg, rgba(255, 159, 10, 0.9), var(--warn));
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 159, 10, 0.3);
}

/* Bulk Actions Styles */
.bulk-actions {
  display: flex;
  gap: 8px;
  margin-right: 16px;
  align-items: center;
}

.bulk-actions.hidden {
  display: none;
}

.bulk-btn {
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid transparent;
}

.bulk-btn.btn-secondary {
  background: var(--muted-bg);
  color: var(--text);
  border-color: var(--border);
}

.bulk-btn.btn-secondary:hover {
  background: var(--hover-bg);
  transform: translateY(-1px);
}

.bulk-btn.btn-primary {
  background: var(--brand);
  color: white;
  border-color: rgba(10, 132, 255, 0.3);
}

.bulk-btn.btn-primary:hover {
  background: rgba(10, 132, 255, 0.8);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(10, 132, 255, 0.3);
}

.bulk-btn.btn-warn {
  background: var(--warn);
  color: white;
  border-color: rgba(255, 159, 10, 0.3);
}

.bulk-btn.btn-warn:hover {
  background: rgba(255, 159, 10, 0.8);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 159, 10, 0.3);
}

/* Column Header Checkbox Styles */
.column-checkbox {
  display: inline-block;
  margin-right: 8px;
  cursor: pointer;
  vertical-align: middle;
}

.column-checkbox input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--brand);
  margin: 0;
  vertical-align: middle;
}

.column-text {
  vertical-align: middle;
}

.iap-col.selected {
  background: rgba(10, 132, 255, 0.1) !important;
  border-left: 3px solid var(--brand) !important;
}

.iap-col.selected .column-checkbox input[type="checkbox"] {
  accent-color: var(--brand);
}

/* Modal Styles */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(4px);
  display: none;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}
.modal[aria-hidden="false"] {
  display: flex;
}

.modal-content {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 90%;
  max-width: 800px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 24px;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background-color 0.2s ease;
}
.close-btn:hover {
  background: var(--border);
  color: var(--text);
}

.modal-body {
  padding: 20px 24px;
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-controls {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  align-items: center;
}

.btn {
  padding: 8px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s ease;
}
.btn-primary {
  background: var(--brand);
  color: white;
  border-color: var(--brand);
}
.btn-primary:hover {
  background: #0070e6;
}
.btn-secondary {
  background: var(--card);
  color: var(--text);
}
.btn-secondary:hover {
  background: var(--border);
}

.filter-input {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  outline: none;
  flex: 1;
  min-width: 200px;
}

.country-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 8px;
  overflow: auto;
  flex: 1;
  padding: 8px 0;
  max-height: 400px;
}

.country-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  border: 1px solid transparent;
}
.country-item:hover {
  background: var(--border);
}
.country-item.selected {
  background: rgba(10, 132, 255, 0.1);
  border-color: var(--brand);
}

.country-checkbox {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.country-item.selected .country-checkbox {
  background: var(--brand);
  border-color: var(--brand);
}
.country-checkbox::after {
  content: '✓';
  color: white;
  font-size: 10px;
  font-weight: bold;
  display: none;
}
.country-item.selected .country-checkbox::after {
  display: block;
}

.country-info {
  flex: 1;
  min-width: 0;
}
.country-name {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.country-code {
  font-size: 12px;
  color: var(--muted);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  border-top: 1px solid var(--border);
}

@media (max-width: 720px) {
  .controls { gap: 10px; }
  .control:not(.switch) label { display: none; }
  
  .modal-content {
    width: 95%;
    max-height: 90vh;
  }
  
  .country-list {
    grid-template-columns: 1fr;
  }
  
  .modal-controls {
    flex-direction: column;
    align-items: stretch;
  }
  .filter-input {
    min-width: auto;
  }
}


