/* =========================================================
   Recipe Photo Project – Phase 8
   iPhone Safari UX polish + locked grid tiles
   (offline-first PWA; no backend assumptions)
   ========================================================= */

/* ==================== THEME ==================== */
:root{
  --bg: #f6f7fb;
  --panel: #ffffff;
  --tile: #ffffff;
  --text: #121426;
  --muted: #5a617a;
  --border: rgba(18,20,38,.10);
  --shadow: 0 6px 18px rgba(18,20,38,.12);

  --radius: 14px;
  --tileRadius: 18px;

  /* Layout */
  --gap: 14px;
  --capH: 54px;

  /* Default tile sizing (desktop / larger screens) */
  --tileSize: clamp(120px, calc((100vw - 48px) / 3), 240px);

  /* iOS safe-area */
  --safeB: env(safe-area-inset-bottom, 0px);
  --safeT: env(safe-area-inset-top, 0px);
}

/* ==================== BASE ==================== */
*{ box-sizing: border-box; }
html, body{ height: 100%; }

body{
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

.app{
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ==================== TOP BAR ==================== */
.topbar{
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;

  padding: calc(10px + var(--safeT)) 12px 12px;
  background: linear-gradient(to bottom, rgba(246,247,251,.96), rgba(246,247,251,.75));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);

  border-bottom: 1px solid var(--border);
}

.iconBtn{
  width: 38px;
  height: 38px;
  min-width: 38px;
  min-height: 38px;

  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(18,20,38,.04);

  display: grid;
  place-items: center;

  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.iconBtn:disabled{ opacity: .45; cursor: default; }

.titleArea{ flex: 1; min-width: 0; }

.pageTitle{
  font-size: 18px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sortSelect{
  height: 38px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(18,20,38,.04);
  padding: 0 10px;
  font-weight: 800;
}

/* Breadcrumbs */
.crumbs{
  margin-top: 6px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.crumbChip{
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(18,20,38,.04);
  color: var(--muted);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.crumbSep{
  padding: 4px 0;
  font-size: 12px;
  color: rgba(90,97,122,.55);
}

/* ==================== GRID ==================== */
#grid{
  padding: 14px 14px calc(96px + var(--safeB));
  display: grid;
  grid-template-columns: repeat(3, var(--tileSize));
  gap: var(--gap);
  justify-content: start;
  align-content: start;

  /* Helps Safari scroll performance */
  -webkit-overflow-scrolling: touch;
}

/* Center grid on large screens */
@media (min-width: 900px){
  #grid{
    max-width: calc((3 * var(--tileSize)) + (2 * var(--gap)));
    margin: 0 auto;
  }
}

/* ==================== iPHONE GRID RULES ==================== */
/* iPhone portrait: 2 columns (up to iPhone 15 Pro Max width = 430px) */
@media (max-width: 430px){
  :root{
    --tileSize: calc((100vw - 14px - 14px - var(--gap)) / 2);
    --capH: 50px;
  }
  #grid{
    grid-template-columns: repeat(2, var(--tileSize));
  }
}

/* iPhone landscape / small tablets: 3 columns */
@media (min-width: 431px) and (max-width: 900px){
  :root{
    --tileSize: clamp(120px, calc((100vw - 14px - 14px - (2 * var(--gap))) / 3), 220px);
  }
  #grid{
    grid-template-columns: repeat(3, var(--tileSize));
  }
}

/* ==================== TILE ==================== */
.tile{
  width: var(--tileSize);
  height: calc(var(--tileSize) + var(--capH));

  display: flex;
  flex-direction: column;

  background: var(--tile);
  border-radius: var(--tileRadius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);

  overflow: hidden;
  cursor: pointer;
  user-select: none;

  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;

  /* Avoid Safari layout jitter when images load */
  contain: layout paint;
}

.tile:active{ transform: translateY(1px); }

/* Image / folder preview */
.thumbWrap{
  width: 100%;
  height: var(--tileSize);
  background: rgba(18,20,38,.04);
  display: block;
  overflow: hidden;
}

.tile img,
.thumb{
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Folder placeholder (if using .thumb div) */
.tile.folder .thumbWrap{
  display: grid;
  place-items: center;
  font-size: 42px;
  color: rgba(18,20,38,.35);
}

/* ==================== CAPTION ==================== */
.cap{
  height: var(--capH);
  padding: 8px 10px;

  background: rgba(255,255,255,0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);

  border-top: 1px solid rgba(18,20,38,.15);

  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.capTitle{
  flex: 1;
  min-width: 0;

  font-size: 13px;
  font-weight: 800;

  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Count badge */
.capBadge{
  flex: 0 0 auto;
  padding: 4px 8px;

  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;

  border: 1px solid rgba(18,20,38,.10);
  background: rgba(18,20,38,.04);
}

/* ==================== BOTTOM BAR ==================== */
.bottombar{
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 25;

  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;

  padding: 10px 10px calc(14px + var(--safeB));

  background: linear-gradient(to top, rgba(246,247,251,.98), rgba(246,247,251,.75));
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);

  border-top: 1px solid var(--border);
}

.barBtn{
  min-height: 44px; /* iOS tap target */

  border: 1px solid var(--border);
  border-radius: 16px;
  background: rgba(18,20,38,.04);

  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;

  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.barLabel{
  font-size: 13px;
  font-weight: 800;
}

/* ==================== MODALS ==================== */
.overlay{
  position: fixed;
  inset: 0;
  background: rgba(18,20,38,.35);
  z-index: 50;
  display: flex;

  padding: calc(10px + var(--safeT)) 10px calc(10px + var(--safeB));
}
.overlay.hidden{ display: none; }

.modal{
  margin: auto;
  width: min(860px, 94vw);
  height: min(86vh, 900px);

  background: #fff;
  border-radius: 18px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);

  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modalHeader{
  padding: 12px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.modalTitle{ font-size: 14px; font-weight: 800; }

.modalBody{
  padding: 12px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
