/* =========================================================
   KetabGard — Responsive (mobile first adjustments)
   Breakpoints: 1440, 1024, 768, 375, 320
   ========================================================= */

/* ---------- Bottom nav visible only on small/medium; top nav on wide ---------- */
.bottom-nav { display: flex; }
.top-nav { display: none; }

@media (min-width: 1024px) {
  .bottom-nav { display: none; }
  .top-nav { display: flex; }
}

/* ---------- Header collapse of search ---------- */
.header-search { max-width: 420px; }
@media (max-width: 768px) {
  .header-search { display: none; }          /* search lives on Books page / via its own page */
  .brand .brand-text { display: none; }       /* icon-only brand on small screens */
}
@media (max-width: 767px) {
  .brand .brand-text { display: none; }
}

/* ---------- Book grid densities ---------- */
.book-grid {
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 20px;
}

@media (min-width: 1440px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
}

@media (max-width: 1024px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(164px, 1fr)); gap: 16px; }
  .container { padding-inline: 18px; }
}

@media (max-width: 768px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
  .hero { padding: 26px 20px; }
  .hero-heading { font-size: 1.35rem; }
  .hero-figure { width: 110px; height: 110px; }
}

@media (max-width: 375px) {
  .book-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .page { padding-block-end: calc(var(--bottom-nav-h) + 22px); }
}

@media (max-width: 320px) {
  .book-grid { grid-template-columns: 1fr; gap: 12px; }
  .hero-heading { font-size: 1.2rem; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Chat height on mobile ---------- */
.chat-panel {
  height: calc(100vh - var(--header-h) - var(--bottom-nav-h) - 130px);
  min-height: 380px;
}

/* ---------- Filter bar wraps on small screens ---------- */
.filter-bar { flex-wrap: wrap; }
.filter-search { width: 100%; }
@media (min-width: 500px) {
  .filter-search { width: auto; min-width: 160px; }
}

/* =========================================================
   Phase 3.5 — Responsive & mobile polish (bottom sheets etc.)
   ========================================================= */
.bottom-nav .nav-link { min-height: 48px; }
.bottom-nav .nav-link .nav-icon { font-size: 1.3rem; }
.bottom-nav .nav-link span:last-child { font-size: .72rem; }

@media (max-width: 1024px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 16px; }
  .chat-layout { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); gap: 14px; }
  .hero { padding: 30px 22px; }
  .hero-heading { font-size: 1.4rem; }
  .hero-figure { width: 96px; height: 96px; opacity: .85; }
  .book-card { width: 160px; }
  .detail-cover { position: static; border-radius: var(--kg-r-lg); }
  .detail-layout { grid-template-columns: 1fr; gap: 20px; }
  .chat-panel { height: calc(100vh - var(--kg-header-h) - var(--kg-bottom-nav-h) - 40px); min-height: 300px; }
}
@media (max-width: 500px) {
  .book-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
  .book-card { width: auto; }
  .form-card { padding: 20px 16px; }
  .modal-actions { flex-wrap: wrap; }
  .hero-actions .btn { flex: 1 1 100%; }
}

/* -------- Modals become bottom sheets on phones -------- */
@media (max-width: 640px) {
  .modal-backdrop { align-items: flex-end; }
  .modal {
    width: 100vw;
    max-height: 86vh;
    overflow-y: auto;
    align-self: flex-end;
    border-radius: var(--kg-r-xl) var(--kg-r-xl) 0 0;
    border-bottom: none;
    transform: translateY(100%);
    transition: transform var(--kg-transition);
  }
  .modal-backdrop.is-open .modal { transform: translateY(0); }
}

/* Scrollbar-hiding for horizontal book rows on touch (peek affordance kept via snap) */
.book-row { scrollbar-width: thin; }

/* ---------- Phone pass: keep every primary surface inside the viewport ---------- */
@media (max-width: 600px) {
  html, body { max-width: 100%; overflow-x: hidden; }
  .page { padding-block-end: calc(var(--bottom-nav-h) + 34px); }
  .container { width: 100%; max-width: 100%; box-sizing: border-box; padding-inline: 14px; }
  .app-header { height: 58px; }
  .app-header-inner { padding-inline: 12px; gap: 8px; }
  .header-actions { gap: 4px; }
  .header-actions .icon-btn { width: 36px; height: 36px; }
  .page-head { flex-wrap: wrap; gap: 10px; padding-block: 14px 8px; }
  .page-title { font-size: 1.35rem; }
  .page-subtitle { font-size: .82rem; line-height: 1.8; }
  .section-head { align-items: center; gap: 8px; }
  .section-title { font-size: 1.08rem; }
  .stat-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .stat-tile { min-width: 0; padding: 12px 8px; }
  .stat-value { font-size: 1rem; }
  .stat-label { font-size: .72rem; white-space: nowrap; }
  .hero { margin-inline: 0; padding: 22px 16px; border-radius: 22px; }
  .hero-actions { gap: 8px; }
  .hero-actions .btn { min-width: 0; padding-inline: 12px; font-size: .82rem; }
  .form-card { width: 100%; box-sizing: border-box; border-radius: 20px; padding: 16px 14px; }
  .settings-layout { gap: 12px; }
  .settings-side { display: flex; overflow-x: auto; gap: 8px; padding-bottom: 4px; scrollbar-width: none; }
  .settings-side .side-link { flex: 0 0 auto; white-space: nowrap; padding: 9px 14px; border-radius: 999px; }
  .account-password-form .field { min-width: 0; }
  .account-password-form .btn, .account-logout { width: 100%; }
  .bottom-nav { height: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px)); padding-bottom: env(safe-area-inset-bottom, 0px); }
  .bottom-nav .nav-link { min-width: 0; }
  .bottom-nav .nav-link span:last-child { font-size: .66rem; }
  .table-wrap, .data-table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .modal { max-width: 100vw; box-sizing: border-box; }
}

/* My Library: keep copy cards readable on narrow phones. */
@media (max-width: 560px) {
  body[data-page="library"] .copy-grid.book-grid {
    grid-template-columns: minmax(0, 1fr) !important;
    gap: 14px !important;
  }
  body[data-page="library"] .copy-card {
    box-sizing: border-box;
    padding: 14px;
  }
  body[data-page="library"] .copy-card-head {
    align-items: center;
    gap: 14px;
  }
  body[data-page="library"] .copy-cover {
    flex: 0 0 72px;
    width: 72px;
    height: 102px;
  }
  body[data-page="library"] .copy-info { min-width: 0; }
  body[data-page="library"] .copy-title {
    display: block;
    font-size: 15px;
    line-height: 1.7;
    overflow-wrap: break-word;
    word-break: normal;
  }
  body[data-page="library"] .copy-info .muted {
    font-size: 14px;
    line-height: 1.7;
    overflow-wrap: break-word;
    word-break: normal;
  }
  body[data-page="library"] .copy-meta { gap: 7px; }
  body[data-page="library"] .copy-meta .badge {
    max-width: 100%;
    box-sizing: border-box;
    white-space: normal;
    line-height: 1.65;
    font-size: 12px;
  }
  body[data-page="library"] .copy-actions { gap: 8px; }
  body[data-page="library"] .copy-actions .btn {
    flex: 1 1 calc(50% - 8px);
    min-height: 42px;
    justify-content: center;
    font-size: 13px;
  }
  body[data-page="library"] .page {
    padding-block-end: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px) + 48px);
  }
}

/* Profile: give identity details and account actions room on phones. */
@media (max-width: 560px) {
  body[data-page="profile"] #app-main > .container:first-child .user-card {
    display: grid;
    grid-template-columns: 82px minmax(0, 1fr);
    align-items: center;
    gap: 12px 14px;
    padding: 16px !important;
  }
  body[data-page="profile"] .user-card .avatar-block,
  body[data-page="profile"] .user-card .avatar-block img {
    width: 76px;
    height: 76px;
  }
  body[data-page="profile"] .user-card > div:nth-child(2) {
    min-width: 0;
    width: auto;
    flex: initial !important;
  }
  body[data-page="profile"] .user-card > div:nth-child(2) h1 {
    font-size: clamp(20px, 5.5vw, 24px) !important;
    line-height: 1.45;
    overflow-wrap: normal;
  }
  body[data-page="profile"] .user-card > div:nth-child(2) p {
    margin: 5px 0 0;
    font-size: 14px !important;
    line-height: 1.8;
    overflow-wrap: break-word;
    word-break: normal;
  }
  body[data-page="profile"] .user-card > .btn {
    grid-column: 1 / -1;
    width: 100%;
    min-height: 48px;
    box-sizing: border-box;
    justify-content: center;
    white-space: nowrap;
    font-size: 14px;
  }
  body[data-page="profile"] #app-main > .container:nth-child(2) .stat-grid {
    gap: 8px;
  }
  body[data-page="profile"] #app-main > .container:nth-child(2) .stat-tile {
    min-width: 0;
    padding: 12px 8px;
  }
  body[data-page="profile"] #app-main > .container:nth-child(2) .stat-value {
    font-size: 18px;
    line-height: 1.45;
    overflow-wrap: normal;
  }
  body[data-page="profile"] #app-main > .container:nth-child(2) .stat-label {
    font-size: 13px;
    line-height: 1.6;
    white-space: normal;
  }
}

@media (max-width: 380px) {
  body[data-page="profile"] #app-main > .container:nth-child(2) .stat-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  body[data-page="profile"] #app-main > .container:nth-child(2) .stat-grid > :last-child {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .profile-custom-form { max-height: calc(86vh - 32px); gap: 14px; }
  .profile-avatar-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 9px; }
  .profile-theme-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .profile-custom-fields input, .profile-custom-fields textarea { font-size: 16px; }
  .profile-custom-actions { flex-direction: column; }
  .profile-custom-actions .btn { width: 100%; }
}
