:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}
.mobile-only { display: none; }
.control-icon { display: none; }
.mobile-drawer[hidden] { display: none !important; }

@media (max-width: 760px), (max-width: 1024px) and (max-height: 500px) {
  html { min-height: 100%; background: #000; }
  body.custom-page { height: 100dvh; overflow: hidden; background: #000; --mobile-footer: calc(104px + var(--safe-bottom)); --mobile-drawer-height: 0px; }
  .mobile-only { display: flex; }
  .custom-page .topbar, .gallery-page .topbar {
    height: calc(58px + var(--safe-top)); padding: var(--safe-top) max(12px, var(--safe-right)) 0 max(12px, var(--safe-left));
    gap: 6px;
  }
  .custom-page .topbar > a, .gallery-page .topbar > a { gap: 7px; }
  .custom-page .topbar .logo, .gallery-page .topbar .logo { height: 29px; }
  .custom-page .topbar .badge, .gallery-page .topbar .badge { height: 24px; margin-left: -4px; }
  .custom-page .topbar .wordmark, .gallery-page .topbar .wordmark { height: 22px; }
  .custom-page .topbar .bar-nav, .gallery-page .topbar .bar-nav { left: var(--safe-left); top: var(--safe-top); padding: 0 10px; }
  .custom-page .back, .gallery-page .back { font-size: 0; min-width: 36px; min-height: 44px; justify-content: center; }
  .custom-page .back::before, .gallery-page .back::before { content: '\2190'; font-size: 23px; }
  .custom-page .content { margin-inline: var(--safe-left) var(--safe-right); }
  .custom-page.mobile-drawer-open .content { margin-bottom: var(--mobile-drawer-height); }
  .custom-page .view-toggle:not(.saya-side) { display: none; }
  .custom-page .view-toggle.saya-side {
    top: 10px; left: auto; right: 10px; padding: 2px; gap: 2px;
    background: rgba(0, 0, 0, 0.78); border-radius: 24px;
  }
  .custom-page .view-toggle.saya-side button {
    display: inline-flex; align-items: center; justify-content: center;
    width: 42px; height: 42px; min-height: 42px; padding: 10px; border-radius: 50%;
    color: #fff; text-decoration: none; transform: none;
  }
  .custom-page .view-toggle .control-text { display: none; }
  .custom-page .view-toggle .control-icon {
    display: block; width: 22px; height: 22px; fill: none; stroke: currentColor;
    stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round;
  }
  .custom-page #sayaBtn.active { background: rgba(255, 255, 255, 0.16); }
  .custom-page #bgBtn .icon-studio, .custom-page #bgBtn.is-studio .icon-garden,
  .custom-page #mobileViewMode .icon-to-handle, .custom-page #mobileViewMode.is-full .icon-to-full { display: none; }
  .custom-page #bgBtn.is-studio .icon-studio, .custom-page #mobileViewMode.is-full .icon-to-handle { display: block; }
  .custom-page .bottombar {
    height: var(--mobile-footer); display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 52px 52px;
    padding: 0 max(10px, var(--safe-right)) var(--safe-bottom) max(10px, var(--safe-left)); flex-shrink: 0;
  }
  .custom-page .bar-scroll { grid-column: 1 / -1; grid-row: 1; width: 100%; }
  .custom-page .bar-items { gap: 24px; padding: 0 8px; }
  .custom-page .part-btn > button { min-height: 48px; font-size: 16px; letter-spacing: 0.4px; }
  .custom-page .part-btn.current > button, .custom-page .part-btn > button:hover { transform: none; text-underline-offset: 6px; }
  .custom-page .bar-nav button:hover:not(:disabled), .mobile-drawer .sub-btn.current > button { transform: none; }
  .custom-page .bottombar > .bar-nav { padding: 0 8px; justify-content: flex-start; grid-row: 2; border-top: 1px solid #333; }
  .custom-page .bottombar > .bar-nav:last-child { justify-content: flex-end; }
  .custom-page .bar-nav button { min-height: 44px; font-size: 17px; letter-spacing: 0.5px; white-space: normal; }
  .custom-page .mobile-drawer {
    position: fixed; z-index: 30; left: var(--safe-left); right: var(--safe-right); bottom: var(--mobile-footer);
    height: auto; max-height: min(48dvh, 420px); display: flex; flex-direction: column; background: #111; color: #fff;
    border-top: 1px solid #555; border-radius: 16px 16px 0 0; overflow: hidden;
  }
  .mobile-drawer-head { display: flex; align-items: center; justify-content: space-between; flex-shrink: 0; min-height: 48px; border-bottom: 1px solid #333; padding: 0 8px; gap: 10px; }
  .mobile-drawer-head h2 { font-size: 18px; font-weight: normal; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .mobile-drawer-head button { font-family: inherit; color: #fff; background: none; border: 0; min-width: 48px; min-height: 44px; font-size: 15px; }
  .mobile-drawer-panels { overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; padding: 0 12px 12px; flex: 0 1 auto; min-height: 0; }
  .mobile-drawer-tabs { flex-shrink: 0; padding: 0 12px; }
  .mobile-drawer-panels[hidden], .mobile-drawer-tabs[hidden] { display: none !important; }
  .mobile-drawer .style-pop {
    position: static !important; transform: none !important; width: 100%; max-width: none; min-width: 0; margin: 0;
    border: 0; padding: 8px 0; background: transparent; max-height: none; overflow: visible;
  }
  .mobile-drawer .style-pop.open { display: block; }
  .mobile-drawer .style-pop.mobile-suppressed { display: none !important; }
  .mobile-drawer .style-pop.group-bar.open { display: flex; overflow-x: auto; gap: 4px; padding: 0; border-top: 1px solid #333; position: static !important; background: #111; }
  .mobile-drawer .sub-btn { flex-shrink: 0; }
  .mobile-drawer .sub-btn > button { padding: 0 12px; min-height: 44px; font-size: 15px; letter-spacing: 0.2px; }
  .mobile-drawer .sub-btn.current > button { transform: none; text-underline-offset: 5px; }
  .mobile-drawer .style-pop.has-aside.open { display: flex; flex-direction: column; align-items: stretch; }
  .mobile-drawer .pop-col { min-width: 0; width: 100%; max-height: none; overflow: visible; padding: 4px 0; }
  .mobile-drawer .style-pop.multi-col .pop-col, .mobile-drawer .opt-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .mobile-drawer .opt { padding: 12px 8px; min-height: 46px; font-size: 16px; letter-spacing: 0; overflow-wrap: anywhere; }
  .mobile-drawer .opt-section, .mobile-drawer .hamon-col { width: 100%; min-width: 0; }
  .mobile-drawer .aside-toggle { min-height: 44px; font-size: 14px; margin-top: 12px; grid-column: 1 / -1; }
  .mobile-drawer .pop-aside { width: 100%; max-width: none; max-height: none; overflow: visible; border-left: 0; border-top: 1px solid #333; padding: 12px 0; font-size: 15px; }
  .mobile-drawer .pop-aside .shape-ref { max-width: 100%; height: auto; }
  .mobile-drawer .pop-aside .ref-grid { grid-template-columns: 1fr; }
  .mobile-drawer .pop-aside .ref-row { flex-direction: column; }
  .custom-page .notice { padding: calc(var(--safe-top) + 14px) 18px calc(var(--safe-bottom) + 14px); }
  .custom-page .notice-card { padding: 44px 23px 24px; max-height: min(80dvh, 650px); }
  .custom-page .notice-close { min-width: 44px; min-height: 44px; top: 4px; right: 6px; }
  .custom-page .notice-card h2 { font-size: 24px; }
  .custom-page .notice-card p { font-size: 16px; }
  .custom-page .loading-progress { width: 85%; bottom: calc(var(--safe-bottom) + 25px); }
  .custom-page .loading-caption { font-size: 15px; gap: 12px; }
  .custom-page .loading-caption > span:first-child { flex: 1; }
  .custom-page .loading-stage { padding-top: calc(var(--safe-top) + 20px); padding-bottom: calc(var(--safe-bottom) + 20px); }
  .custom-page .loading-entry-space span { display: flex; align-items: center; min-height: 44px; }
  .custom-page .lightbox { padding: calc(var(--safe-top) + 15px) max(15px, var(--safe-right)) calc(var(--safe-bottom) + 15px) max(15px, var(--safe-left)); }
  .home-page { min-height: 100svh; padding-top: calc(var(--safe-top) + 20px); padding-bottom: calc(var(--safe-bottom) + 20px); }
  .home-page .entry { min-height: 44px; display: flex; align-items: center; }
  .gallery-page { min-height: 100dvh; height: auto; padding-bottom: var(--safe-bottom); }
  .gallery-page .list { padding: 10px max(14px, var(--safe-right)) 20px max(14px, var(--safe-left)); gap: 18px; }
  .gallery-page .row { grid-template-columns: 1fr 1fr; grid-template-rows: auto auto auto auto; gap: 14px 10px; padding: 16px; border: 1px solid #333; min-width: 0; border-radius: 8px; }
  .gallery-page .row > .idx { grid-column: 1 / -1; grid-row: 1; justify-self: start; font-size: 16px; color: #aaa; }
  .gallery-page .row > .shot, .gallery-page .row > .no-shot { grid-column: 1 / -1; grid-row: 2; height: auto; min-height: 100px; aspect-ratio: 36 / 13; }
  .gallery-page .row > .middle { grid-column: 1 / -1; grid-row: 3; min-width: 0; }
  .gallery-page .recipe-head { flex-wrap: wrap; gap: 8px 16px; }
  .gallery-page .recipe-head .name { font-size: 16px; letter-spacing: 0; }
  .gallery-page .recipe-head .toggle { min-height: 44px; width: 100%; text-align: left; }
  .gallery-page .spec, .gallery-page .more { grid-template-columns: minmax(0, 0.85fr) minmax(0, 1.15fr); font-size: 15px; }
  .gallery-page .spec span { overflow-wrap: anywhere; }
  .gallery-page .row > .copy { grid-column: 1; grid-row: 4; justify-self: start; }
  .gallery-page .row > .remove { grid-column: 2; grid-row: 4; justify-self: end; }
  .gallery-page .copy, .gallery-page .remove { min-height: 44px; font-size: 17px; }
  .gallery-page .site-foot { display: grid; grid-template-columns: 1fr; padding: 28px max(16px, var(--safe-right)) 28px max(16px, var(--safe-left)); --foot-text: 15px; gap: 30px; }
  .gallery-page .empty { padding: 45px 12px; font-size: 20px; }
}

@media (max-width: 1024px) and (max-height: 500px) {
  body.custom-page { --mobile-footer: calc(92px + var(--safe-bottom)); }
  .custom-page .topbar, .gallery-page .topbar { height: calc(44px + var(--safe-top)); }
  .custom-page .bottombar { grid-template-rows: 46px 46px; }
  .custom-page.mobile-drawer-open .content { margin-bottom: 0; margin-right: calc(var(--safe-right) + min(46vw, 430px)); }
  .custom-page .mobile-drawer { left: auto; width: min(46vw, 430px); top: auto; max-height: calc(100dvh - 44px - var(--safe-top) - var(--mobile-footer)); border-radius: 0; border-left: 1px solid #444; border-top: 0; }
  .custom-page .loading-progress { bottom: calc(var(--safe-bottom) + 10px); }
  .custom-page .loading-help { margin-top: 8px; min-height: 0; }
}
