/* =========================================
   1. FONTS & VARIABLES
   ========================================= */
@font-face {
  font-family: "Manrope";
  src: url("/assets/Manrope-VariableFont_wght.ttf") format("truetype");
  font-weight: 100 900;
  font-style: normal;
}

:root {
  --font-main: "Manrope", sans-serif;

  /* Source: #379aff */
  --primary: rgb(164 201 254);
  --surface-tint: rgb(164 201 254);
  --on-primary: rgb(0 49 92);
  --primary-container: rgb(31 72 118);
  --on-primary-container: rgb(211 227 255);

  --secondary: rgb(188 199 219);
  --on-secondary: rgb(38 49 65);
  --secondary-container: rgb(60 71 88);
  --on-secondary-container: rgb(216 227 248);

  --tertiary: rgb(217 189 227);
  --on-tertiary: rgb(60 41 70);
  --tertiary-container: rgb(84 63 94);
  --on-tertiary-container: rgb(245 217 255);

  --error: rgb(255 180 171);
  --on-error: rgb(105 0 5);
  --error-container: rgb(147 0 10);
  --on-error-container: rgb(255 218 214);

  --surface: rgb(17 19 24);
  --on-surface: rgb(225 226 233);
  --surface-variant: rgb(67 71 78);
  --on-surface-variant: rgb(195 198 207);

  --outline: rgb(141 145 153);
  --outline-variant: rgb(67 71 78);

  --shadow: rgb(0 0 0);
  --scrim: rgb(0 0 0);

  --inverse-surface: rgb(225 226 233);
  --inverse-on-surface: rgb(46 48 53);
  --inverse-primary: rgb(58 96 143);

  --surface-dim: rgb(17 19 24);
  --surface-bright: rgb(55 57 62);
  --surface-container-lowest: rgb(12 14 19);
  --surface-container-low: rgb(25 28 32);
  --surface-container: rgb(29 32 36);
  --surface-container-high: rgb(39 42 47);
  --surface-container-highest: rgb(50 53 58);

  --secondary-container-hover: rgb(71, 83, 102);

  /* Source: #B33B15 */
  --orange-container: #723523;
  --on-orange-container: #ffdbd1;

  /* Source: #006B73 */
  --teal-container: #004f55;
  --on-teal-container: #9df0f9;
}

/* =========================================
   2. GLOBAL RESET
   ========================================= */
* {
  box-sizing: border-box;
  font-weight: 500;
  font-family: var(--font-main);
}

body {
  margin: 0;
  background-color: var(--surface);
  color: var(--on-surface);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: var(--surface);
}
::-webkit-scrollbar-thumb {
  background: var(--outline-variant);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--on-surface-variant);
}

/* Layout */
.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

.grid-card:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

/* =========================================
   3. HEADER
   ========================================= */
.header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 32px 0 24px 0;
}

.header-icon {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  object-fit: contain;
}

.header-info h1 {
  font-size: 22px;
  font-weight: 600;
  margin: 0;
  color: var(--on-surface);
  display: flex;
  gap: 6px;
  align-items: center;
}

.header-info h1 span {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--secondary-container);
  color: var(--on-secondary-container);
}

.header-sub {
  color: var(--on-surface-variant);
  font-size: 14px;
}

.header-links {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.header-link {
  border-radius: 24px;
  background: var(--surface-container);
  color: var(--on-surface-variant);
  width: 48px;
  height: 48px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.header-link:hover {
  background: var(--surface-container-high);
}
.header-link:active {
  background: var(--secondary-container);
}
.header-link svg {
  width: 28px;
  height: 28px;
}

/* =========================================
   4. CONTROLS
   ========================================= */
#controlsSkip {
  position: fixed;
  top: 999999999999999px;
  background: var(--tertiary-container);
  color: var(--on-tertiary-container);
  border-radius: 4px;
  text-decoration: none;
  padding: 8px;
}

#controlsSkip:focus {
  top: 4px;
  left: 4px;
  outline: 1px solid var(--outline)
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 24px;
}

.controls-top {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.controls-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.control-group {
  display: flex;
  gap: 8px;
}

/* Inputs (Search & Path) */
.search-wrapper,
.path-wrapper {
  height: 48px;
  background-color: var(--surface-container-high);
  border-radius: 9999999px;
  display: flex;
  align-items: center;
  transition: box-shadow 0.2s;
}

.search-wrapper {
  flex: 2;
  min-width: 300px;
  padding: 8px;
}

.path-wrapper {
  flex: 1;
  padding: 0 16px;
  gap: 10px;
  border: 1px solid transparent;
}

.search-wrapper:focus-within,
.path-wrapper:focus-within {
  box-shadow: 0 0 0 1px var(--on-surface-variant);
}

.search-wrapper input,
.path-wrapper input {
  width: 100%;
  height: 100%;
  background: transparent;
  border: none;
  outline: none;
  color: var(--on-surface-variant);
  font-weight: 500;
}

/* Icon Button (Inside Input) */
.search-icon {
  background: transparent;
  border: none;
  padding: 8px;
  border-radius: 50%;
  color: var(--on-surface-variant);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

button.search-icon {
  cursor: pointer;
}

button.search-icon:hover {
  background: var(--surface-container-highest);
  color: var(--on-surface);
}

button.search-icon:active {
  background: var(--secondary-container);
  color: var(--on-secondary-container);
}

.search-wrapper svg {
  color: var(--on-surface-variant);
  width: 24px;
  height: 24px;
}
.path-wrapper svg {
  color: var(--on-surface-variant);
  width: 26px;
  height: 24px;
}

/* Dropdowns & Chips */
.chip,
select,
#mobileFilterBtn {
  height: 32px;
  background-color: var(--surface);
  border: 1px solid var(--outline-variant);
  border-radius: 8px;
  color: var(--on-surface);
  font-size: 13px;
  outline: none;
  appearance: none;
  transition: 0.2s;
  text-wrap-mode: nowrap;
}

.chip:focus {
  border-color: var(--outline)
}

/* Arrow Icon for Selects */
select,
#mobileFilterBtn {
  padding: 0 32px 0 12px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 -960 960 960' width='24px' fill='%23e3e3e3'%3E%3Cpath d='M459-381 314-526q-3-3-4.5-6.5T308-540q0-8 5.5-14t14.5-6h304q9 0 14.5 6t5.5 14q0 2-6 14L501-381q-5 5-10 7t-11 2q-6 0-11-2t-10-7Z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px top 50%;
  background-size: 24px auto;
}
select:hover {
  background-color: var(--surface-container-high);
}

/* Filter Tags */
.filter-tags {
  display: flex;
  gap: 8px;
  overflow-x: auto;
}

.tag {
  background: transparent;
  padding: 0 12px;
  border-color: var(--outline-variant);
  /* Checkmark icon (Hidden by default) */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='24px' viewBox='0 -960 960 960' width='24px' fill='%23e3e3e3'%3E%3Cpath d='m382-354 339-339q12-12 28-12t28 12q12 12 12 28.5T777-636L410-268q-12 12-28 12t-28-12L182-440q-12-12-11.5-28.5T183-497q12-12 28.5-12t28.5 12l142 143Z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 8px top 50%;
  background-size: 0;
}
.tag:hover {
  background-color: var(--surface-container-high);
}
.tag.active {
  background-color: var(--primary-container);
  border-color: var(--primary-container);
  color: var(--on-primary-container);
  padding-left: 32px;
  background-size: 18px;
}

/* =========================================
   5. LIST VIEW
   ========================================= */
.list-header,
.list-row {
  display: grid;
  grid-template-columns: 40px 66px 3fr 100px 100px 160px 140px;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--outline-variant);
  min-width: 960px;
  user-select: none;
  -webkit-user-select: none;
}

.list-header {
  color: var(--on-surface);
  font-weight: 700;
}
.list-header .col.name {
  min-width: 300px;
}
.list-header .col.actions {
  display: none;
}
.list-header .col.sel {
  display: flex;
  align-items: center;
}

/* Sortable Headers */
.list-header .col.name,
.list-header .col.req,
.list-header .col.install,
.list-header .col.first {
  cursor: pointer;
  user-select: none;
  position: relative;
  transition: color 0.2s;
}

.list-header .col.name:hover,
.list-header .col.req:hover,
.list-header .col.install:hover,
.list-header .col.first:hover {
  color: var(--primary);
}

.list-row {
  transition: background-color 0.1s;
  cursor: pointer;
}
.list-row:focus-visible {
  background-color: var(--surface-container-high);
  outline: 1px solid var(--outline-variant)
}
.list-row:hover {
  background-color: var(--surface-container-high);
}
.list-row.selected {
  background-color: rgba(164, 201, 254, 0.08);
}

.list-row img {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  outline: 1px solid var(--inverse-on-surface);
  object-fit: contain;
  background: #000;
}
.list-row .col.first {
  font-size: 15px;
  font-weight: 500;
}

/* Name Column */
.name-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
  min-width: 300px;
  margin-right: 8px;
}
.name-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.app-name {
  font-weight: 600;
  color: var(--on-surface);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pkg-name {
  color: var(--on-surface-variant);
  font-size: 12px;
  font-family: monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Status Pills */
.status-pill {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.status-wip {
  background: var(--primary-container);
  color: var(--on-primary-container);
}
.status-easy {
  background: var(--tertiary-container);
  color: var(--on-tertiary-container);
}
.status-missing {
  background: var(--error-container);
  color: var(--on-error-container);
}
.status-conflict {
  background: var(--orange-container);
  color: var(--on-orange-container);
}
.status-link {
  background: var(--teal-container);
  color: var(--on-teal-container);
}
.status-unlabeled {
  display: none;
}

/* Actions */
.actions-col {
  display: flex;
  gap: 4px;
  justify-content: flex-end;
}
.action-btn {
  color: var(--on-surface-variant);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s;
  text-decoration: none;
  outline: none;
  background: transparent;
  border-radius: 50%;
  border: 1px solid transparent;
  transition: border 0.2s;
  width: 40px;
  height: 40px;
}
.action-btn:focus {
  border-color: var(--outline-variant)
}
.action-btn:hover {
  color: var(--primary);
}
.action-btn svg {
  width: 28px;
  height: 28px;
  object-fit: contain;
}
.action-btn.ctx-trigger svg {
  width: 28px;
  height: 28px;
}

/* Checkbox (M3 Style) */
input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 2px;
  border: 2px solid var(--on-surface-variant);
  background: transparent;
  cursor: pointer;
  position: relative;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  margin: 0;
}
input[type="checkbox"]:hover {
  border-color: var(--on-surface);
  background-color: rgba(var(--on-surface-rgb), 0.08);
}
input[type="checkbox"]:checked {
  background-color: var(--primary);
  border-color: var(--primary);
}
input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  left: 4px;
  top: 1px;
  width: 4px;
  height: 9px;
  border: solid var(--on-primary);
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}
input[type="checkbox"]:indeterminate {
  background-color: var(--primary);
  border-color: var(--primary);
}
input[type="checkbox"]:indeterminate::after {
  content: "";
  position: absolute;
  left: 3px;
  top: 7px;
  width: 8px;
  height: 2px;
  background-color: var(--on-primary);
}

/* =========================================
   6. GRID VIEW
   ========================================= */
.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 16px;
  padding-bottom: 100px;
  user-select: none;
  -webkit-user-select: none;
}

.grid-card {
  position: relative;
  aspect-ratio: 1;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.1s;
}
.grid-card:hover {
  border-color: var(--on-surface-variant);
}
.grid-card.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary) inset;
}
.grid-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.grid-overlay-check {
  position: absolute;
  bottom: 8px;
  right: 8px;
  width: 20px;
  height: 20px;
  background: var(--primary);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}
.grid-card.selected .grid-overlay-check {
  opacity: 1;
}

/* Fallbacks */
.fallback-icon-row {
  width: 56px;
  height: 56px;
  background-color: var(--surface-container-high);
  border-radius: 8px;
  outline: 1px solid var(--inverse-on-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 9px;
  font-weight: 700;
  color: var(--on-surface-variant);
  text-transform: uppercase;
  line-height: 1.1;
  padding: 2px;
}

.fallback-icon-grid {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  width: 100%;
  height: 100%;
  padding: 8px;
  font-size: 11px;
  background-color: var(--surface-container-high);
  color: var(--on-surface-variant);
}

/* =========================================
   7. LOADERS & EMPTY STATES
   ========================================= */
#scrollSentinel {
  width: 100%;
  margin: 20px 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#scrollSentinel svg {
  animation: spin 2s linear infinite;
}
@keyframes spin {
  100% {
    transform: rotate(360deg);
  }
}

.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 64px 0;
  text-align: center;
}
.empty-state svg {
  width: 48px;
  height: 48px;
  margin-bottom: 16px;
}
.empty-state h3 {
  margin: 0 0 8px 0;
  font-size: 20px;
  font-weight: 600;
  color: var(--on-surface);
}
.empty-state p {
  margin: 0;
  font-size: 14px;
  color: var(--on-surface-variant);
}

/* =========================================
   8. POPUPS (FAB, Menu, Toasts)
   ========================================= */
/* FAB */
.fab-bar {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  display: flex;
  gap: 8px;
  z-index: 900;
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  visibility: hidden;
}
.fab-bar.visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

.fab-btn {
  height: 48px;
  padding: 0 16px;
  border-radius: 24px;
  border: none;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  color: var(--on-primary);
  outline: none;
}

.fab-btn svg {
  width: 24px;
  height: 24px;
}

.fab-primary {
  background: var(--primary);
}

.fab-secondary {
  width: 56px;
  padding: 0;
  justify-content: center;
  border-radius: 36px;
  background: var(--primary);
}

.fab-primary:hover, .fab-primary:focus,
.fab-secondary:hover, .fab-secondary:focus {
  background: var(--secondary);
}

/* Context Menus */
.context-menu {
  margin: 0;
  border: none;
  padding: 8px 0;
  background: var(--secondary-container);
  color: var(--on-secondary-container);
  border-radius: 4px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  min-width: 240px;
  display: flex;
  flex-direction: column;
}

.row-style {
  position: fixed;
  opacity: 0;
  transform: scale(0.9);
  z-index: 1000;
  transition: opacity 0.1s ease-out, transform 0.1s ease-out,
    display 0.1s allow-discrete;
}
.row-style:popover-open {
  opacity: 1;
  transform: scale(1);
}
@starting-style {
  .row-style:popover-open {
    opacity: 0;
    transform: scale(0.9);
  }
}

.fab-style {
  position: fixed;
  top: auto;
  bottom: 85px;
  left: 50%;
  opacity: 0;
  transform: translateX(-50%) translateY(20px);
  z-index: 1000;
  transform-origin: bottom center;
  transition: opacity 0.2s, transform 0.2s, display 0.2s allow-discrete;
}
.fab-style:popover-open {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
@starting-style {
  .fab-style:popover-open {
    opacity: 0;
    transform: translateX(-50%) translateY(20px);
  }
}

.ctx-item {
  font-weight: 500;
  padding: 0 16px;
  height: 48px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--on-secondary-container);
  cursor: pointer;
  transition: background 0.1s;
  user-select: none;
  outline: none;
}
.ctx-item:hover, .ctx-item:focus {
  background: var(--secondary-container-hover);
}
.ctx-item svg {
  width: 24px;
  height: 24px;
  color: var(--on-secondary-container);
  opacity: 0.8;
}

/* Mobile Filter Popover */
.mobile-filter-style {
  position: fixed;
  max-width: 260px;
  transform-origin: top left;
}
.mobile-filter-style .ctx-item .check-icon {
  width: 0;
  height: 20px;
  color: var(--on-secondary-container);
  opacity: 0;
  transition: opacity 0.1s;
}
.mobile-filter-style .ctx-item.active .check-icon {
  opacity: 1;
  width: 20px;
}

/* Toasts */
.toast-container {
  position: fixed;
  top: 24px;
  right: 24px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 12px;
  pointer-events: none;
}
.toast {
  background: var(--surface-container-high);
  color: var(--on-surface);
  border: 1px solid var(--outline-variant);
  padding: 12px;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: auto;
  max-width: 400px;
  pointer-events: auto;
  position: relative;
  overflow: hidden;
  opacity: 0;
  transform: translateX(20px);
  animation: notifIn 0.3s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}
.toast.hiding {
  animation: notifOut 0.2s ease-in forwards;
}
.toast svg {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
}
.toast-title {
  font-weight: 600;
  font-size: 14px;
}

@keyframes notifIn {
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
@keyframes notifOut {
  to {
    opacity: 0;
    transform: translateX(20px) scale(0.95);
  }
}

/* =========================================
   10. RESPONSIVE (< 840px)
   ========================================= */
.mobile-only {
  display: none !important;
}

@media (max-width: 840px) {
  /* Visibility */
  .path-wrapper {
    display: none;
  }
  .desktop-only {
    display: none !important;
  }
  .mobile-only {
    display: flex !important;
  }

  /* Mobile Button Alignment */
  #mobileFilterBtn {
    align-items: center;
    gap: 2px;
  }

  /* List */
  .list-viewport {
    overflow-x: auto;
    margin: 0 -16px;
    padding: 0 16px;
    -webkit-overflow-scrolling: touch;
  }

  /* Grid */
  .grid-container {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 12px;
    padding-bottom: 120px;
  }

  /* Pagination */
  .pagination-container {
    gap: 4px;
  }
  .page-btn {
    min-width: 32px;
    height: 32px;
    font-size: 12px;
  }

  /* Toasts */
  .toast-container {
    top: 16px;
    left: 16px;
    right: 16px;
    align-items: center;
  }
  .toast {
    width: 100%;
    transform: translateY(-20px);
  }
  @keyframes notifIn {
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
}
