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

:root {
  --bg: #ffffff;
  --bg-image: none;
  --bg-size: cover;
  --bg-position: center;
  --text: #111;
  --text-secondary: #666;
  --text-glow: rgba(255,255,255,0.8);
  --border: rgba(0,0,0,0.1);
  --icon-shadow: 0 1px 4px rgba(0,0,0,0.15);
  --icon-size: 60px;
  --icon-radius: 14px;
  --grid-columns: 4;
  --search-bg: rgba(0,0,0,0.06);
  --panel-bg: #fff;
  --overlay-bg: rgba(0,0,0,0.35);
  --is-mobile: 1;
}

@media (min-width: 768px) {
  :root {
    --is-mobile: 0;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --text: #eee;
    --text-secondary: #888;
    --text-glow: rgba(0,0,0,0.7);
    --border: rgba(255,255,255,0.1);
    --icon-shadow: 0 2px 8px rgba(0,0,0,0.5);
    --search-bg: rgba(255,255,255,0.1);
    --panel-bg: #2c2c2e;
    --overlay-bg: rgba(0,0,0,0.55);
  }
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  background-color: var(--bg);
  background-image: var(--bg-image);
  background-size: var(--bg-size);
  background-position: var(--bg-position);
  background-repeat: no-repeat;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.4s ease;
}

body {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 8px);
}

/* ===== Search Bar ===== */
.search-container {
  padding: 12px 14px 6px;
  flex-shrink: 0;
}

.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--search-bg);
  border-radius: 10px;
  padding: 8px 12px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.search-bar svg {
  flex-shrink: 0;
  opacity: 0.4;
  color: var(--text);
}

.search-bar input {
  flex: 1;
  border: none;
  background: none;
  font-size: 15px;
  color: var(--text);
  outline: none;
  font-family: inherit;
  min-width: 0;
}
.search-bar input::placeholder {
  color: var(--text-secondary);
}

.search-clear {
  display: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: rgba(128,128,128,0.25);
  color: var(--text);
  font-size: 10px;
  cursor: pointer;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  padding: 0;
  line-height: 1;
}
.search-clear.visible {
  display: flex;
}

/* ===== Top Bar (edit mode) ===== */
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  transition: height 0.3s ease, opacity 0.3s ease, padding 0.3s ease;
}
.top-bar.visible {
  height: 44px;
  opacity: 1;
  padding: 6px 16px;
}

.hint-text {
  font-size: 13px;
  color: var(--text-secondary);
  text-shadow:
    0 0 4px var(--text-glow),
    0 0 8px var(--text-glow);
}

.btn-done {
  background: var(--text);
  color: var(--bg);
  border: none;
  border-radius: 18px;
  padding: 7px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: opacity 0.15s;
}
.btn-done:active {
  opacity: 0.6;
}

/* ===== App Grid ===== */
.app-grid {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  padding: 16px 14px 24px;
  align-content: flex-start;
  gap: 8px 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Paginated mode: horizontal swipe on mobile */
.app-grid.paginated {
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  padding: 8px 0 4px;
  gap: 0;
}

.app-grid::-webkit-scrollbar {
  display: none;
}

.app-page {
  flex: 0 0 auto;
  scroll-snap-align: start;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  box-sizing: border-box;
  padding: 0 6px;
}

/* ===== Page Dots ===== */
.page-dots {
  display: none;
  justify-content: center;
  gap: 6px;
  padding: 0 0 8px;
  flex-shrink: 0;
}
.page-dots.visible {
  display: flex;
}
.page-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(128,128,128,0.3);
  transition: background 0.25s, transform 0.25s;
}
.page-dot.active {
  background: var(--text);
}

/* ===== App Icon ===== */
.app-icon-wrapper {
  width: calc(100% / var(--grid-columns));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  margin-bottom: 18px;
  cursor: grab;
  touch-action: manipulation;
  transition: transform 0.15s ease;
  position: relative;
  box-sizing: border-box;
  padding: 0 6px;
}

/* Tighter spacing inside paginated pages */
.app-page .app-icon-wrapper {
  margin-bottom: 0;
  padding: 0 6px;
}

/* Tap feedback */
.app-icon-wrapper.tap-active {
  transform: scale(0.88);
  transition: transform 0.1s ease;
}

.app-icon {
  width: var(--icon-size);
  height: var(--icon-size);
  border-radius: var(--icon-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  box-shadow: var(--icon-shadow);
  position: relative;
  transition: box-shadow 0.2s;
}

.app-label {
  font-size: 11px;
  font-weight: 500;
  text-align: center;
  color: var(--text);
  max-width: 76px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.2;
  text-shadow:
    0 0 4px var(--text-glow),
    0 0 8px var(--text-glow),
    0 1px 2px var(--text-glow);
}

/* ===== Edit Mode - Jiggle ===== */
@keyframes jiggle {
  0%   { transform: rotate(0deg); }
  25%  { transform: rotate(-1.2deg); }
  50%  { transform: rotate(0deg); }
  75%  { transform: rotate(1.2deg); }
  100% { transform: rotate(0deg); }
}

.app-grid.editing .app-icon-wrapper {
  animation: jiggle 0.3s ease-in-out infinite;
}

/* ===== Drag Styles ===== */
.drag-origin {
  opacity: 0.2 !important;
  animation: none !important;
  transform: none !important;
}

.drag-clone {
  animation: none !important;
  transform: scale(1.12) !important;
  transition: none !important;
}
.drag-clone .app-icon {
  box-shadow: 0 10px 36px rgba(0,0,0,0.4);
}

/* Pause jiggle on ALL items while dragging */
.app-grid.dragging .app-icon-wrapper {
  animation: none !important;
}

/* ===== Empty State ===== */
.empty-state {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px 20px;
  color: var(--text-secondary);
  flex: 1;
}
.empty-state.visible {
  display: flex;
}

.empty-icon {
  font-size: 40px;
  opacity: 0.5;
}

.empty-text {
  font-size: 13px;
  text-align: center;
  opacity: 0.7;
  text-shadow:
    0 0 4px var(--text-glow),
    0 0 8px var(--text-glow);
}

/* ===== Context Menu ===== */
.context-menu {
  display: none;
  position: fixed;
  background: var(--panel-bg);
  border-radius: 14px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.2);
  overflow: hidden;
  z-index: 5000;
  min-width: 160px;
}
.context-menu.visible {
  display: block;
}

.ctx-item {
  display: block;
  width: 100%;
  padding: 14px 20px;
  border: none;
  background: none;
  font-size: 15px;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  border-bottom: 0.5px solid var(--border);
}
.ctx-item:last-child {
  border-bottom: none;
}
.ctx-item:active {
  background: rgba(128,128,128,0.1);
}

/* ===== Settings Overlay ===== */
.settings-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  z-index: 6000;
  align-items: flex-end;
  justify-content: center;
}
.settings-overlay.visible {
  display: flex;
}

.settings-panel {
  background: var(--panel-bg);
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 500px;
  padding: 20px 20px 30px;
  max-height: 75vh;
  overflow-y: auto;
  box-shadow: 0 -4px 30px rgba(0,0,0,0.25);
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.settings-header h3 {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
}

.btn-close {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: rgba(128,128,128,0.2);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.settings-section {
  margin-bottom: 20px;
}
.settings-section h4 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 10px;
}

.preset-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.preset-item {
  aspect-ratio: 1;
  border-radius: 12px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s, transform 0.15s;
  position: relative;
}
.preset-item:active {
  transform: scale(0.95);
}
.preset-item.active {
  border-color: var(--text);
}
.preset-item.active::after {
  content: '✓';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  background: rgba(255,255,255,0.25);
  border-radius: 10px;
}

.custom-color {
  display: flex;
  align-items: center;
  gap: 10px;
}
.custom-color input[type="color"] {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  background: none;
  padding: 2px;
}
#colorValue {
  font-size: 13px;
  color: var(--text-secondary);
  font-family: 'SF Mono', 'Menlo', monospace;
}

.bg-image-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.btn-upload {
  display: inline-block;
  padding: 10px 16px;
  background: var(--text);
  color: var(--bg);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: opacity 0.15s;
}
.btn-upload:active {
  opacity: 0.6;
}

.btn-remove-image {
  padding: 10px 16px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
}
.btn-remove-image:active {
  background: rgba(128,128,128,0.1);
}

/* ===== Desktop ===== */
@media (min-width: 768px) {
  :root {
    --grid-columns: 6;
    --icon-size: 72px;
    --icon-radius: 17px;
  }

  body {
    max-width: 960px;
    margin: 0 auto;
  }

  .search-container {
    padding: 16px 24px 10px;
  }
  .search-bar {
    max-width: 480px;
    margin: 0 auto;
  }

  .app-grid {
    gap: 18px 0;
    padding: 24px 28px 28px;
  }

  .app-icon-wrapper {
    margin-bottom: 22px;
    padding: 0 8px;
  }

  .app-label {
    font-size: 12px;
    max-width: 84px;
  }
}

@media (min-width: 1200px) {
  :root {
    --grid-columns: 8;
  }
  body {
    max-width: 1100px;
  }
}
