/* Mobile & tablet — iOS / Android / Xiaomi safe-area, touch targets
   Applies when viewport width <= 899px (phone portrait & mobile layout).
   "Desktop site" mode widens viewport (~980px+) so these rules do NOT apply. */

@supports (padding: env(safe-area-inset-top)) {
  .site-header {
    padding-top: env(safe-area-inset-top);
    height: calc(var(--header-height) + env(safe-area-inset-top));
  }

  .site-main {
    padding-bottom: env(safe-area-inset-bottom);
  }

  .site-footer {
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  }
}

@media (max-width: 899px) {
  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  body {
    overflow-x: clip;
    min-height: 100dvh;
    min-height: -webkit-fill-available;
  }

  :root {
    --header-height: 60px;
  }

  .site-header {
    gap: 0.5rem;
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .logo {
    font-size: 1rem;
  }

  .header-actions {
    gap: 0.35rem;
    min-width: 0;
    flex-shrink: 1;
  }

  .header-auth-group {
    min-width: 0;
    flex-shrink: 1;
  }

  .header-user {
    gap: 0.25rem;
    max-width: min(52vw, 14rem);
  }

  .header-user .profile-nickname-btn {
    max-width: 6.5rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-user .saint-bgm {
    flex-shrink: 0;
  }

  .header-user .saint-bgm__btn {
    width: 1.65rem;
    height: 1.65rem;
    font-size: 0.68rem;
  }

  .header-user .tier-icon {
    width: 22px;
    height: 22px;
  }

  #header-guest .saint-bgm__btn {
    width: 1.65rem;
    height: 1.65rem;
  }

  .site-main {
    padding: calc(var(--header-height) + 1.25rem) max(1rem, env(safe-area-inset-left)) 2.5rem max(1rem, env(safe-area-inset-right));
  }

  .site-main.site-main--home-canvas {
    min-height: calc(100dvh - var(--header-height));
  }

  .page-shell {
    max-width: 100%;
  }

  .page-header h1 {
    font-size: 1.5rem;
  }

  .site-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 1.25rem max(1rem, env(safe-area-inset-left)) max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-right));
  }

  .footer-copy {
    font-size: 0.7rem;
  }

  .hero {
    min-height: calc(100dvh - var(--header-height));
    padding: 1rem;
  }

  .hero-title {
    font-size: clamp(3rem, 18vw, 5rem);
  }

  .hero-aside {
    position: relative;
    top: auto;
    right: auto;
    display: flex;
    align-items: flex-start;
    margin-top: 2rem;
    width: 100%;
  }

  .hero-cta-block {
    max-width: none;
    text-align: left;
  }

  .hero-bottom {
    flex-direction: column;
    align-items: flex-start;
  }

  .hero-bg-trail {
    width: 100%;
    right: -30%;
    opacity: 0.5;
  }

  .feature-grid {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .post-editor-full,
  .form-card.post-editor-full {
    max-width: 100% !important;
    padding: 1.25rem;
  }

  .post-card {
    flex-direction: column;
    align-items: stretch;
  }

  .post-card-thumb {
    width: 100%;
    height: 140px;
  }

  .post-card-meta {
    gap: 0.5rem 0.75rem;
    flex-wrap: wrap;
  }

  .post-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  .post-sort {
    flex-wrap: wrap;
  }

  .post-detail-body {
    overflow-x: auto;
    word-break: break-word;
  }

  .post-detail-body img {
    max-width: 100%;
    height: auto;
  }

  .file-upload-card {
    padding: 1rem;
  }

  .file-limit-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.65rem;
  }

  .file-limit-row select,
  .limit-custom-wrap input {
    width: 100%;
    max-width: 100%;
  }

  .file-row {
    flex-direction: column;
    align-items: stretch;
  }

  .file-row-actions {
    justify-content: flex-end;
  }

  .product-grid {
    grid-template-columns: 1fr;
  }

  .charge-layout {
    grid-template-columns: 1fr;
  }

  .site-main.site-main--with-sidebar {
    flex-direction: column;
    padding-left: max(1rem, env(safe-area-inset-left));
    padding-right: max(1rem, env(safe-area-inset-right));
  }

  .site-sidebar {
    position: static;
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
    margin-right: 0;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
  }

  .site-sidebar-nav {
    flex-direction: row;
    gap: 0.35rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .site-sidebar-nav::-webkit-scrollbar {
    display: none;
  }

  .site-sidebar-nav a {
    flex: 0 0 auto;
    border-left: none;
    border-bottom: 2px solid transparent;
    padding: 0.55rem 0.85rem;
    white-space: nowrap;
  }

  .site-sidebar-nav a.is-active {
    border-left-color: transparent;
    border-bottom-color: var(--color-accent);
  }

  .charge-history-table,
  .ranking-table-wrap,
  .membership-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  .pagination {
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.25rem;
  }

  .pagination .btn {
    min-width: 2.75rem;
    min-height: 2.75rem;
    padding: 0.35rem 0.5rem;
  }

  .modal-card {
    width: calc(100vw - 2rem);
    max-width: calc(100vw - 2rem);
    max-height: calc(100dvh - 2rem);
    overflow-y: auto;
  }

  .theme-picker-pop {
    left: 0;
    right: auto;
    max-width: calc(100vw - 2rem);
  }

  .btn,
  .mobile-nav a,
  .menu-toggle,
  .saint-bgm__btn {
    min-height: 44px;
    min-width: 44px;
  }

  .saint-bgm__btn {
    min-height: 1.75rem;
    min-width: 1.75rem;
  }

  .menu-toggle {
    padding: 0.5rem;
    flex-shrink: 0;
  }

  .mobile-nav {
    padding-bottom: env(safe-area-inset-bottom);
  }

  .mobile-nav a {
    display: flex;
    align-items: center;
    padding-top: 0.85rem;
    padding-bottom: 0.85rem;
  }

  .tab-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .tab-bar::-webkit-scrollbar {
    display: none;
  }

  .tab-btn {
    flex: 0 0 auto;
    min-width: 5rem;
  }

  .comment-form textarea {
    font-size: 16px;
  }

  .form-group input,
  .form-group select,
  .form-group textarea {
    font-size: 16px;
  }
}

@media (min-width: 600px) and (max-width: 899px) {
  .product-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .feature-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .header-auth-group .btn-sm {
    padding: 0.4rem 0.65rem;
    font-size: 0.7rem;
  }

  #header-guest {
    gap: 0.35rem;
  }

  .header-user {
    max-width: min(58vw, 12rem);
  }

  .header-user .profile-nickname-btn {
    max-width: 4.75rem;
  }
}

@media (max-width: 899px) and (orientation: landscape) {
  .hero {
    min-height: auto;
    padding-top: 0.75rem;
    padding-bottom: 1.5rem;
  }

  .hero-title {
    font-size: clamp(2.4rem, 12vw, 3.5rem);
  }
}
