/* ============================================================
   Nav — iOS-style Bookmark Navigation
   style.css
   ============================================================ */

/* ── Design Tokens ─────────────────────────────────────────── */
:root {
  --bg:              #f2f2f7;
  --text:            #1c1c1e;
  --text-secondary:  #8e8e93;
  --card:            rgba(255, 255, 255, 0.85);
  --overlay-bg:      rgba(0, 0, 0, 0.45);
  --icon-size:       60px;
  --icon-radius:     15px;
  --label-size:      11px;
  --cell-size:       76px;
  --gap:             20px;
  --blur:            blur(24px);
  --transition-fast: 0.15s ease;
  --transition-med:  0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
  --transition-slide:0.35s cubic-bezier(0.25, 1, 0.5, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:           #000;
    --text:         #f5f5f7;
    --text-secondary:#636366;
    --card:         rgba(28, 28, 30, 0.88);
    --overlay-bg:   rgba(0, 0, 0, 0.65);
  }
}

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

/* ── Body & App Shell ───────────────────────────────────────── */
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  min-height: 100dvh;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  background: var(--bg);
  background-size: cover;
  background-position: center;
  transition: background-color 0.3s;
}

/* ── Overlays ───────────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--overlay-bg);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  animation: fadeIn 0.2s ease;
}

.overlay.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* Modal card */
.modal-card {
  background: var(--card);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border-radius: 20px;
  padding: 28px 24px;
  width: min(360px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 13px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.22);
  animation: slideUp 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes slideUp {
  from { transform: translateY(20px) scale(0.96); opacity: 0; }
  to   { transform: translateY(0)    scale(1);    opacity: 1; }
}

.modal-logo {
  font-size: 52px;
  text-align: center;
  line-height: 1.1;
}

.modal-card h2 {
  font-size: 18px;
  font-weight: 650;
  text-align: center;
  color: var(--text);
  letter-spacing: -0.2px;
}

/* Inputs inside modals */
.modal-card input[type="password"],
.modal-card input[type="text"],
.modal-card input[type="url"] {
  width: 100%;
  padding: 11px 14px;
  border-radius: 11px;
  border: 1px solid rgba(120, 120, 128, 0.22);
  background: rgba(120, 120, 128, 0.08);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
  -webkit-appearance: none;
}

.modal-card input:focus {
  border-color: rgba(0, 122, 255, 0.6);
  background: rgba(0, 122, 255, 0.04);
}

.error-text {
  color: #ff3b30;
  font-size: 12.5px;
  min-height: 16px;
  text-align: center;
  line-height: 1.3;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 2px;
}

/* Buttons */
.btn-primary {
  flex: 1;
  padding: 12px;
  background: #007aff;
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: filter 0.15s;
}
.btn-primary:hover  { filter: brightness(0.9); }
.btn-primary:active { filter: brightness(0.8); }

.btn-secondary {
  flex: 1;
  padding: 12px;
  background: rgba(120, 120, 128, 0.14);
  color: var(--text);
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-secondary:hover  { background: rgba(120, 120, 128, 0.22); }
.btn-secondary:active { background: rgba(120, 120, 128, 0.3); }

.btn-small {
  padding: 7px 13px;
  font-size: 13px;
  flex: none;
  border-radius: 10px;
}

/* Icon preview row (in edit item form) */
.icon-preview-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.icon-preview {
  width: 54px;
  height: 54px;
  border-radius: 13px;
  overflow: hidden;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(120, 120, 128, 0.14);
  font-size: 28px;
}

.icon-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Toggle switch */
.toggle-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
}

.toggle-label > span {
  font-size: 15px;
  color: var(--text);
}

.toggle-wrap {
  position: relative;
  width: 46px;
  height: 27px;
  flex-shrink: 0;
}

.toggle-wrap input[type="checkbox"] {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background: rgba(120, 120, 128, 0.3);
  transition: background 0.22s;
  cursor: pointer;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 23px;
  height: 23px;
  left: 2px;
  top: 2px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.22s;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

.toggle-wrap input:checked + .toggle-slider {
  background: #34c759;
}

.toggle-wrap input:checked + .toggle-slider::before {
  transform: translateX(19px);
}

/* ── Edit Toolbar ───────────────────────────────────────────── */
.edit-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 500;
  height: 52px;
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  background: var(--card);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border-bottom: 1px solid rgba(120, 120, 128, 0.14);
  /* safe area for notched phones */
  padding-top: env(safe-area-inset-top, 0);
  height: calc(52px + env(safe-area-inset-top, 0px));
}

.edit-toolbar.hidden {
  display: none;
}

.tb-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 9px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  background: rgba(120, 120, 128, 0.12);
  color: var(--text);
  white-space: nowrap;
  transition: background 0.15s;
}

.tb-btn:hover  { background: rgba(120, 120, 128, 0.22); }
.tb-btn:active { background: rgba(120, 120, 128, 0.32); }

.tb-done {
  background: #007aff;
  color: #fff;
  font-weight: 600;
  margin-left: auto;
}

.tb-done:hover  { background: #0066d6; }
.tb-done:active { background: #005bbf; }

/* ── Pages Layout ───────────────────────────────────────────── */
#pages-wrapper {
  flex: 1;
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
  transition: padding-top 0.22s ease;
}

body.edit-mode #pages-wrapper {
  padding-top: calc(52px + env(safe-area-inset-top, 0px));
}

#pages-track {
  display: flex;
  height: 100%;
  will-change: transform;
  /* transition set by JS during snap; removed during drag */
}

.page {
  width: 100vw;
  height: 100%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 0 8px;
}

/* Icon grid on each page */
.icon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, var(--cell-size));
  justify-content: center;
  gap: var(--gap);
  padding: 12px 24px;
  align-content: start;
  max-height: 100%;
  overflow: visible;
  width: 100%;
}

/* ── Icon Cell ──────────────────────────────────────────────── */
.icon-cell {
  width: var(--cell-size);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  position: relative;
  touch-action: none; /* we handle all touch ourselves */
  -webkit-user-drag: none;
}

.icon-wrap {
  width: var(--icon-size);
  height: var(--icon-size);
  border-radius: var(--icon-radius);
  overflow: visible; /* allow badges & shadows to breathe without clipping */
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.14);
  position: relative;
  transition: transform var(--transition-fast), box-shadow var(--transition-fast);
  flex-shrink: 0;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transform-origin: 50% 50%;
}

.icon-cell:not(body.edit-mode *) .icon-wrap:active {
  transform: scale(0.88);
}

.icon-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  border-radius: var(--icon-radius);
  -webkit-user-drag: none;
}

.icon-letter {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: -0.5px;
  border-radius: var(--icon-radius);
}

.icon-label {
  font-size: var(--label-size);
  color: var(--text);
  text-align: center;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.3;
  /* text shadow for legibility */
  text-shadow: 0 0 4px rgba(0,0,0,0.22);
}

/* Private lock badge on icon */
.private-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  color: #fff;
  font-size: 11px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
  pointer-events: none;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

/* ── Wobble animation (edit mode) ───────────────────────────── */
@keyframes wobble {
  0%   { transform: rotate(-2.5deg) translateY(0px); }
  50%  { transform: rotate(0deg)    translateY(-1px); }
  100% { transform: rotate(2.5deg)  translateY(0px); }
}

body.edit-mode .icon-cell:not(.is-dragging) .icon-wrap {
  animation: wobble 0.18s ease-in-out infinite alternate;
  will-change: transform;
}

/* Dragging state */
.icon-cell.is-dragging {
  opacity: 0.3;
}

/* Drop target highlight (merge into folder) */
.icon-cell.drop-target .icon-wrap {
  box-shadow: 0 0 0 3px #007aff, 0 4px 14px rgba(0, 0, 0, 0.2);
  transform: scale(1.08);
}

/* ── Delete badge ───────────────────────────────────────────── */
.delete-badge {
  position: absolute;
  top: -6px;
  left: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #ff3b30;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10;
  cursor: pointer;
  line-height: 1;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  transition: transform 0.12s;
}

.delete-badge:hover {
  transform: scale(1.15);
}

body.edit-mode .delete-badge {
  display: flex;
}

/* ── Add button ─────────────────────────────────────────────── */
.add-cell {
  width: var(--cell-size);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}

.add-icon {
  width: var(--icon-size);
  height: var(--icon-size);
  border-radius: var(--icon-radius);
  background: rgba(120, 120, 128, 0.10);
  border: 2px dashed rgba(120, 120, 128, 0.36);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 30px;
  color: var(--text-secondary);
  transition: background 0.15s, transform 0.12s;
}

.add-cell:hover .add-icon {
  background: rgba(120, 120, 128, 0.18);
}
.add-cell:active .add-icon {
  transform: scale(0.9);
}

.add-label {
  font-size: var(--label-size);
  color: var(--text-secondary);
}

/* ── Folder icon (in grid) ──────────────────────────────────── */
.folder-wrap {
  width: 100%;
  height: 100%;
  border-radius: var(--icon-radius);
  background: linear-gradient(145deg, #7b8ef0, #4a5be8);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 9px;
  align-items: center;
  justify-items: center;
}

.folder-mini-icon {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 5px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
}

.folder-mini-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ── Folder Popup Overlay ───────────────────────────────────── */
#folder-overlay {
  z-index: 200;
  align-items: center;
}

.folder-popup {
  background: var(--card);
  backdrop-filter: var(--blur);
  -webkit-backdrop-filter: var(--blur);
  border-radius: 22px;
  padding: 22px 20px 24px;
  width: min(340px, 90vw);
  max-height: 70vh;
  overflow-y: auto;
  box-shadow: 0 14px 50px rgba(0, 0, 0, 0.28);
  transform-origin: center center;
  animation: folderOpen 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes folderOpen {
  from { transform: scale(0.55); opacity: 0; }
  to   { transform: scale(1);    opacity: 1; }
}

.folder-popup-header {
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 16px;
  letter-spacing: -0.1px;
}

.folder-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, var(--cell-size));
  justify-content: center;
  gap: var(--gap);
}

/* ── Page Dots ──────────────────────────────────────────────── */
#page-dots {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 7px;
  padding: 10px 0 max(14px, env(safe-area-inset-bottom, 14px));
  flex-shrink: 0;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(120, 120, 128, 0.38);
  transition: background 0.2s, transform 0.2s;
  cursor: pointer;
}

.dot.active {
  background: var(--text);
  transform: scale(1.25);
}

/* ── Loading screen ─────────────────────────────────────────── */
#loading-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  font-size: 40px;
  transition: opacity 0.3s;
}

#loading-screen.fade-out {
  opacity: 0;
  pointer-events: none;
}

/* ── Utility ────────────────────────────────────────────────── */
.hidden {
  display: none !important;
}

/* Press effect on icon cells in normal (non-edit) mode */
body:not(.edit-mode) .icon-cell:active .icon-wrap {
  transform: scale(0.88);
  transition: transform 0.08s ease;
}

/* ── Wallpaper Mode & Text Readability ──────────────────────── */
#app.has-wallpaper::before {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.12); /* Subtle contrast scrim */
  pointer-events: none;
  z-index: 0;
}

#app.has-wallpaper #pages-wrapper,
#app.has-wallpaper #page-dots,
#app.has-wallpaper .edit-toolbar {
  position: relative;
  z-index: 1;
}

/* Outstanding text legibility against any custom wallpaper */
.has-wallpaper .icon-label {
  color: #ffffff !important;
  font-weight: 600;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.95),
    0 0 6px rgba(0, 0, 0, 0.85),
    0 0 14px rgba(0, 0, 0, 0.7);
  letter-spacing: 0.15px;
}

.has-wallpaper .add-icon {
  background: rgba(255, 255, 255, 0.22);
  border-color: rgba(255, 255, 255, 0.55);
  color: #ffffff;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.has-wallpaper .add-label {
  color: #ffffff !important;
  font-weight: 600;
  text-shadow:
    0 1px 2px rgba(0, 0, 0, 0.95),
    0 0 6px rgba(0, 0, 0, 0.85);
}

.has-wallpaper #page-dots .dot {
  background: rgba(255, 255, 255, 0.45);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

.has-wallpaper #page-dots .dot.active {
  background: #ffffff;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.85);
}

/* ── Wallpaper Modal Styling ────────────────────────────────── */
.wallpaper-modal {
  width: min(440px, 94vw);
  max-height: 85vh;
  overflow-y: auto;
  gap: 14px;
}

.wp-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.wp-label {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}

.wp-presets-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 4px;
  border-top: 1px solid rgba(120, 120, 128, 0.15);
  padding-top: 10px;
}

.wp-presets-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  letter-spacing: 0.3px;
}

.wp-preset-buttons {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.btn-preset {
  padding: 10px 14px;
  background: rgba(120, 120, 128, 0.10);
  border: 1px solid rgba(120, 120, 128, 0.18);
  border-radius: 12px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn-preset:hover {
  background: rgba(120, 120, 128, 0.20);
  border-color: rgba(0, 122, 255, 0.5);
}

.btn-preset:active {
  transform: scale(0.98);
}

.preset-tag {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 6px;
  background: rgba(0, 122, 255, 0.15);
  color: #007aff;
  font-weight: 600;
}
