// Mobile — TOP page
// Pixel target: assets/.../A3-orange-main-logo-orange-v3.png (B option, mobile, ~426 logical width)

const M_TOP = window.MobileSite;
const S_TM = window.Site;
const { T: TM, MSectionHeading: SH_TM, MPage: Page_TM, Img: Img_TM, Ic: Ic_TM } = M_TOP;
const { COMPANY: COTM, ASSETS: AS_TM, SERVICES: SV_TM, FLOW_STEPS: FL_TM, ARTICLES: ART_TM, CASES: CASES_TM, BudouXText: BT_TM, FLOW_NO_BREAK: FLOW_NO_BREAK_TM, copyText: copyTM, rawCopyValue: rawCopyTM } = S_TM;
const { mobileHref: hrefTM, mobileServiceHref: serviceHrefTM } = M_TOP;
const articleHrefTM = (articleId) => `${hrefTM('knowledge-detail')}&article=${encodeURIComponent(articleId)}`;

// --- Mockup-matched orange palette (brighter / more saturated than brand tokens) ---
const O = {
  main: TM.orange,       // primary action orange (fills, big number, headline accents)
  deep: TM.orangeDeep,   // deep orange (gradient bottoms, text on white)
  soft: TM.orangeSoft,
  line: TM.orangeLine,   // thin orange borders
};
// CMSの会社名に二重空白が入っているため、見出し内だけ空白を除いて正式表記に戻す（CMS値は変えない）
const CO_NAME_TM = String(COTM.name || '').replace(/[\s　]+/g, '').trim();
const AREA_TEXT = String(COTM.area || '').trim() || '広島市周辺';
// CMSのエリア表記は「広島市周辺/近郊地域もご対応可能」のようにスラッシュ区切りで長くなることがある
const AREA_SEGMENTS = AREA_TEXT.split('/').map((segment) => segment.trim()).filter(Boolean);
const AREA_SEGMENT_LINES = AREA_SEGMENTS.map((segment, index) => (index < AREA_SEGMENTS.length - 1 ? `${segment} /` : segment));

function MTopHeroSlides() {
  const seen = new Set();
  return [AS_TM.heroMain, AS_TM.heroFlip, AS_TM.heroWide2].filter((src) => {
    if (typeof src !== 'string' || src === '' || seen.has(src)) return false;
    seen.add(src);
    return true;
  });
}

function MTopHeroRotator({ onError }) {
  const slides = MTopHeroSlides();
  const [active, setActive] = React.useState(0);
  const reduceMotion = typeof window !== 'undefined'
    && window.matchMedia
    && window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  React.useEffect(() => {
    setActive(0);
    if (slides.length < 2 || reduceMotion) return undefined;
    const timer = window.setInterval(() => {
      if (document.hidden) return;
      setActive((current) => (current + 1) % slides.length);
    }, 6000);
    return () => window.clearInterval(timer);
  }, [slides.join('\n'), reduceMotion]);

  if (slides.length < 2) {
    return (
      <img
        src={AS_TM.heroMain}
        alt=""
        loading="eager"
        decoding="async"
        fetchpriority="high"
        sizes="100vw"
        width="1200"
        height="743"
        onError={onError}
        style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center top', display: 'block' }}
      />
    );
  }

  if (reduceMotion) {
    return (
      <img
        src={slides[0]}
        alt=""
        loading="eager"
        decoding="async"
        fetchpriority="high"
        sizes="100vw"
        width="1200"
        height="743"
        onError={onError}
        style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center top', display: 'block' }}
      />
    );
  }

  return (
    <div style={{ position: 'relative', width: '100%', height: '100%', overflow: 'hidden' }}>
      {slides.map((src, index) => (
        <img
          key={src}
          src={src}
          alt=""
          loading={index === 0 ? 'eager' : 'lazy'}
          decoding="async"
          fetchpriority={index === 0 ? 'high' : undefined}
          sizes="100vw"
          width="1200"
          height="743"
          onError={index === 0 ? onError : undefined}
          style={{
            position: index === 0 ? 'relative' : 'absolute',
            inset: index === 0 ? undefined : 0,
            width: '100%',
            height: '100%',
            objectFit: 'cover',
            objectPosition: 'center top',
            display: 'block',
            opacity: active === index ? 1 : 0,
            transition: 'opacity 1.2s ease',
          }}
        />
      ))}
    </div>
  );
}

function MTopHero() {
  const hero = S_TM.topHeroLines();
  const [heroFailed, setHeroFailed] = React.useState(false);
  React.useEffect(() => { setHeroFailed(false); }, [AS_TM.heroMain]);
  return (
    <section data-top-hero style={{
      position: 'relative',
      overflow: 'hidden',
      minHeight: 440,
      padding: '16px 16px 0',
      background: '#fff',
    }}>
      {/* 写真: 全面背景。白の斜めグラデ（スクリム）で左上の文字領域を確保する */}
      <div style={{ position: 'absolute', inset: 0, zIndex: 0 }}>
        {heroFailed ? (
          <div aria-hidden="true" style={{ width: '100%', height: '100%', background: `linear-gradient(135deg, ${TM.imageFallbackA}, ${TM.imageFallbackCoolB})` }} />
        ) : (
          <MTopHeroRotator onError={() => setHeroFailed(true)} />
        )}
        <div style={{ position: 'absolute', inset: 0, background: 'linear-gradient(118deg, rgba(255,255,255,0.97) 0%, rgba(255,255,255,0.94) 34%, rgba(255,255,255,0.55) 52%, rgba(255,255,255,0) 70%), linear-gradient(180deg, rgba(255,255,255,0.92) 0%, rgba(255,255,255,0) 38%)' }} />
      </div>
      {/* 広い幅（〜767px）でも本文とバッジが散らばらないよう、内容を600pxの器に収めて中央寄せする */}
      <div style={{ position: 'relative', zIndex: 1, maxWidth: 600, margin: '0 auto', minHeight: 384, paddingBottom: 176 }}>
      {/* top row: エリアチップのみ。電話導線はヘッダーのアイコンと下部固定バー・電話CTAカードに集約する */}
      <div style={{ position: 'relative', zIndex: 1, display: 'flex', flexWrap: 'nowrap', alignItems: 'flex-start', marginBottom: 12 }}>
        {/* CMSで長い表記にされたときは「/」の塊ごとに改行し、語の途中で切れないようにする */}
        <div style={{
          display: 'inline-flex', alignItems: 'center', gap: 6, background: '#fff', color: TM.green,
          fontWeight: 800, fontSize: 'clamp(10px, 3vw, 12px)', padding: '8px clamp(9px, 2.8vw, 12px)',
          border: `1.5px solid ${TM.green}`, borderRadius: 999,
          whiteSpace: 'normal', wordBreak: 'keep-all', overflowWrap: 'anywhere',
          lineHeight: 1.4, letterSpacing: -0.2, textAlign: 'left', flexShrink: 1, minWidth: 0,
        }}>
          <Ic_TM.pin width="clamp(12px, 3.8vw, 14px)" height="clamp(12px, 3.8vw, 14px)" color={TM.green} style={{ flexShrink: 0 }} />
          <span style={{ minWidth: 0 }}>
            {AREA_SEGMENT_LINES.length > 1
              ? AREA_SEGMENT_LINES.map((line, index) => (
                <React.Fragment key={`${line}-${index}`}>
                  <span style={{ whiteSpace: line.length <= 12 ? 'nowrap' : 'normal' }}>{line}</span>
                  {index < AREA_SEGMENT_LINES.length - 1 ? ' ' : null}
                </React.Fragment>
              ))
              : AREA_TEXT}
          </span>
        </div>
      </div>

      <div data-top-h1 style={{
        position: 'relative', zIndex: 1,
        fontFamily: '"Noto Sans JP", sans-serif', color: TM.ink,
        margin: '8px 0 14px',
        fontFeatureSettings: "'palt'",
        textShadow: '0 0 14px rgba(255,255,255,0.85)',
      }}>
        {hero.line1 && <h1 data-top-hero-line="line1" style={{ margin: 0, fontSize: 29, fontWeight: 900, lineHeight: 1.3, letterSpacing: -1, wordBreak: 'keep-all', overflowWrap: 'normal', WebkitTextStroke: '5px #fff', paintOrder: 'stroke fill' }}><S_TM.HeroAccentText>{hero.line1}</S_TM.HeroAccentText></h1>}
        {hero.line2a && <div data-top-hero-line="line2a" style={{ marginTop: 3, fontSize: 16, fontWeight: 800, lineHeight: 1.5, letterSpacing: -0.3, wordBreak: 'auto-phrase', overflowWrap: 'break-word', WebkitTextStroke: '5px #fff', paintOrder: 'stroke fill' }}><BT_TM>{hero.line2a}</BT_TM></div>}
        <S_TM.HeroBrandLogo
          height="auto"
          maxWidth="min(258px, calc(100vw - 90px))"
          fallbackText={hero.line2b}
          style={{ marginTop: 16, marginBottom: 0, width: 'min(258px, calc(100vw - 90px))', height: 'auto' }}
          fallbackStyle={{ fontSize: 15, letterSpacing: -0.2, WebkitTextStroke: '5px #fff', paintOrder: 'stroke fill' }}
        />
        {hero.line2c && <div data-top-hero-line="line2c" style={{ marginTop: 6, color: O.main, fontSize: 'clamp(20px, 6vw, 22.5px)', fontWeight: 900, lineHeight: 1.35, letterSpacing: '-0.035em', whiteSpace: 'nowrap', WebkitTextStroke: '5px #fff', paintOrder: 'stroke fill' }}>{hero.line2c}</div>}
      </div>
      {(() => {
        // CMSに空で保存されたリード行は「消した」意図として非表示にする（既定文へ戻さない）
        const resolveLead = (id, fallback) => {
          const raw = rawCopyTM(id);
          return String(raw === undefined ? fallback : raw).trim();
        };
        const lead1 = resolveLead('top.hero.lead1', '住まいの修理は、株式会社スマートライフへ。');
        const lead2 = resolveLead('top.hero.lead2', '広島での屋根・外壁・住まいのメンテナンスを、ひとつの窓口で承ります。');
        if (!lead1 && !lead2) return null;
        return (
          <p data-text-contract="top-hero-leads" style={{ position: 'relative', zIndex: 1, fontFamily: '"Noto Sans JP", sans-serif', fontSize: 13, fontWeight: 600, lineHeight: '24px', color: TM.ink, margin: 0, maxWidth: 270, fontFeatureSettings: "'palt'" }}>
            {lead1 && <span style={{ display: 'block' }}><BT_TM>{lead1}</BT_TM></span>}
            {lead2 && <span style={{ display: 'block' }}><BT_TM>{lead2}</BT_TM></span>}
          </p>
        );
      })()}

      {/* 2010 round badge — overlaps photo lower-left */}
      <div style={{
        position: 'absolute', left: 'max(0px, calc(50% - 208px))', bottom: 24,
        width: 128, height: 128, borderRadius: '50%', background: '#fff',
        border: `2px solid ${TM.heritageGold}`, boxShadow: '0 10px 24px -14px rgba(0,0,0,0.3)',
        display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-start',
        textAlign: 'center', padding: '15px 12px 0',
      }}>
        <span style={{ fontSize: 12, fontWeight: 800, color: TM.ink, lineHeight: 1 }}>広島で</span>
        <span style={{ fontWeight: 800, lineHeight: 1, margin: '3px 0 2px', whiteSpace: 'nowrap' }}>
          <span style={{ fontFamily: '"M PLUS 1p", sans-serif', fontSize: 22, letterSpacing: -0.5, color: O.main }}>2010</span>
          <span style={{ fontSize: 11, color: O.main, marginLeft: 1 }}>年</span>
          <span style={{ fontSize: 11, fontWeight: 800, color: TM.ink, marginLeft: 2 }}>から</span>
        </span>
        <span style={{ width: 72, height: 1.5, background: 'rgba(31,111,79,0.55)', margin: '2px 0 3px' }} />
        <span style={{ fontSize: 10, fontWeight: 700, color: TM.ink, lineHeight: 1.4 }}>地域に根ざした<br />施工店</span>
        {/* slot: BADGE-HOUSES-ART — 円の内側に完全に収める（金の輪を切らない）。
            絵の高さ18.8px・絵の下端は円の下端から15px上（右バッジの家と同じ下端ライン）。透過余白1.55px分を引いた位置に置く */}
        <img src="assets/badge-houses-row-v1.png" alt="" decoding="async" width="1656" height="453" style={{
          position: 'absolute', left: '50%', bottom: 13.45, transform: 'translateX(-50%)',
          width: 80, height: 'auto', display: 'block',
          filter: 'drop-shadow(0 1px 0 #fff) drop-shadow(0 -1px 0 #fff) drop-shadow(1px 0 0 #fff) drop-shadow(-1px 0 0 #fff) drop-shadow(0 0 2px #fff)',
        }} />
      </div>

      {/* キャッチコピーバッジ — 2010バッジと同型の円形で写真右下に対で置く。緑の家アイコンを顔に、「お困りごとは」→「スマートライフへ」を焦点にした階層 */}
      <div style={{
        position: 'absolute', zIndex: 1, right: 'max(4px, calc(50% - 208px))', bottom: 24,
        width: 128, height: 128, borderRadius: '50%', background: '#fff',
        border: `2px solid ${TM.heritageGold}`, boxShadow: '0 10px 24px -14px rgba(0,0,0,0.3)',
        display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'flex-start',
        textAlign: 'center', padding: '14px 12px 0', fontFeatureSettings: "'palt'",
      }}>
        {/* 家アイコンの真上にかかる虹のアーチ。家より少し広く円の内側に収め、アイコンより先に描いて背面に回す */}
        <svg viewBox="0 0 50.4 26.6" width="60" height="31.6" aria-hidden="true" focusable="false" style={{
          position: 'absolute', left: '50%', bottom: 18, transform: 'translateX(-50%)',
          display: 'block',
        }}>
          <defs>
            <linearGradient id="slBadgeRainbowFade" x1="0" y1="0" x2="0" y2="1">
              <stop offset="0.68" stopColor="#fff" stopOpacity="1" />
              <stop offset="1" stopColor="#fff" stopOpacity="0.22" />
            </linearGradient>
            <mask id="slBadgeRainbowMask">
              <rect x="0" y="0" width="50.4" height="26.6" fill="url(#slBadgeRainbowFade)" />
            </mask>
          </defs>
          <g fill="none" strokeWidth="2.4" strokeLinecap="butt" mask="url(#slBadgeRainbowMask)">
            <path d="M1.2 25.2 A24 24 0 0 1 49.2 25.2" stroke="#DD6F58" />
            <path d="M3.6 25.2 A21.6 21.6 0 0 1 46.8 25.2" stroke="#E9A055" />
            <path d="M6 25.2 A19.2 19.2 0 0 1 44.4 25.2" stroke="#E0C463" />
            <path d="M8.4 25.2 A16.8 16.8 0 0 1 42 25.2" stroke="#78B683" />
            <path d="M10.8 25.2 A14.4 14.4 0 0 1 39.6 25.2" stroke="#6DA6D2" />
            <path d="M13.2 25.2 A12 12 0 0 1 37.2 25.2" stroke="#9C92CB" />
          </g>
        </svg>
        {/* slot: BADGE-HOUSE-ICON — 左バッジ（2010）と絵の上端・下端が水平に揃うサイズと位置。
            下端ラインは左バッジの街並みと同じ（円の下端から15px上）。
            円内下部が寂しくならないよう、虹＋家のクラスタは左と同等の密度になるまで拡大している */}
        <img src="assets/badge-house-icon-v1.png" alt="" decoding="async" width="144" height="152" style={{
          position: 'absolute', left: '50%', bottom: 15, transform: 'translateX(-50%)',
          width: 24.5, height: 'auto', display: 'block',
          filter: 'drop-shadow(0 1px 0 #fff) drop-shadow(0 -1px 0 #fff) drop-shadow(1px 0 0 #fff) drop-shadow(-1px 0 0 #fff) drop-shadow(0 0 2px #fff)',
        }} />
        <span style={{ fontSize: 11.5, fontWeight: 800, color: TM.ink, lineHeight: 1.2, whiteSpace: 'nowrap', letterSpacing: -0.2 }}>建物・住まいの</span>
        <span style={{ fontSize: 14.5, fontWeight: 800, color: TM.ink, lineHeight: 1.2, whiteSpace: 'nowrap', letterSpacing: -0.3 }}>お困りごとは</span>
        <span style={{ width: 76, height: 1.5, background: 'rgba(31,111,79,0.55)', margin: '4px 0 4px' }} />
        {/* 円の下部は弦が短いため、12.5px×8字＋paltで95px程度に収めて端切れを防ぐ */}
        <span style={{ fontSize: 12.5, fontWeight: 800, color: TM.green, lineHeight: 1.2, whiteSpace: 'nowrap', letterSpacing: -0.3 }}>スマートライフへ</span>
      </div>
      </div>
    </section>
  );
}

// Green Lucide line icon per service (all green — orange is reserved for actions)
// 対応サービス — 6サービスの2列リッチカードグリッド（モック01のTOP版レイアウト）
function MTopServices() {
  return (
    <section style={{ background: '#fff', padding: '14px 16px 12px' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 12px' }}>
        <span style={{ flex: 1, height: 1, background: TM.line }} />
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7, color: TM.greenInk, fontWeight: 800, fontSize: 17 }}>
          <Ic_TM.home width="19" height="19" color={TM.green} /> 対応サービス
        </span>
        <span style={{ flex: 1, height: 1, background: TM.line }} />
      </div>
      {/* 画像なしのチップ形式（正典準拠）。6項目を2列で */}
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 9 }}>
        {SV_TM.map((s, i) => {
          const iconKey = S_TM.serviceIconKey(s);
          const SvIc = Ic_TM[iconKey] || Ic_TM.home;
          const fullWidth = SV_TM.length % 2 === 1 && i === SV_TM.length - 1;
          return (
            <a key={s.id} href={serviceHrefTM(s.id)} style={{
              gridColumn: fullWidth ? '1 / -1' : 'auto',
              display: 'inline-flex', alignItems: 'center', gap: 6,
              border: `1px solid ${TM.line}`, borderRadius: 8, background: '#fff',
              padding: '12px 8px', textDecoration: 'none', minHeight: 44,
            }}>
              <span data-service-icon={iconKey}><SvIc width="22" height="22" color={TM.green} style={{ flexShrink: 0 }} /></span>
              {/* slot: TEXT-SERVICE-TOP-NAME-{s.id} */}
              <span style={{ color: TM.ink, fontWeight: 800, fontSize: 13.5, letterSpacing: -0.2, lineHeight: 1.35, minWidth: 0, wordBreak: 'auto-phrase', overflowWrap: 'break-word', fontFeatureSettings: "'palt'" }}>{s.name}</span>
            </a>
          );
        })}
      </div>
    </section>
  );
}

// 許可・資格 — 認可プレート（白カード＋金罫）。信頼系なので緑×黒のみ（オレンジは使わない）。
// 許可が増えたらプレートを縦に足せる構造にしてある。
function MTopLicense() {
  const { heading, name, number, certifications } = S_TM.licenseContent();
  if (!name || !number) return null;
  return (
    <section style={{ background: '#fff', padding: '4px 16px 16px' }}>
      {/* 見出しが空にされたら罫線ごと出さない */}
      {heading && (
        <div style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 14px' }}>
          <span style={{ flex: 1, height: 1, background: TM.line }} />
          <span style={{ color: TM.greenInk, fontWeight: 800, fontSize: 15, textAlign: 'center', wordBreak: 'auto-phrase' }}>{heading}</span>
          <span style={{ flex: 1, height: 1, background: TM.line }} />
        </div>
      )}
      <div style={{ display: 'grid', gap: 12 }}>
        <S_TM.LicensePlate name={name} number={number} />
        {certifications.map((certification, index) => (
          <S_TM.LicensePlate key={`${certification.label}-${certification.name}-${index}`} {...certification} />
        ))}
      </div>
    </section>
  );
}

// ご相談内容から選べます — 工事名ではなく、訪問者の目的から行き先を選ぶ入口。
function MTopPurposes() {
  const items = S_TM.topPurposeItems();
  return (
    <section style={{ background: '#fff', padding: '14px 16px 14px' }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 12px' }}>
        <span style={{ flex: 1, height: 1, background: TM.line }} />
        <span style={{ color: TM.greenInk, fontWeight: 800, fontSize: 15, whiteSpace: 'nowrap' }}>ご相談内容から選べます</span>
        <span style={{ flex: 1, height: 1, background: TM.line }} />
      </div>
      <div data-top-purpose-grid style={{ display: 'grid', gridTemplateColumns: 'repeat(2, minmax(0, 1fr))', gap: 9 }}>
        {items.map((it, index) => {
          const PurposeIcon = Ic_TM[it.icon] || Ic_TM.home;
          const fullWidth = items.length % 2 === 1 && index === items.length - 1;
          return (
          <a key={it.label} data-top-purpose data-top-purpose-label={it.label} data-top-purpose-full-width={fullWidth ? 'true' : 'false'} href={it.href} style={{
            gridColumn: fullWidth ? '1 / -1' : 'auto',
            display: 'grid', gridTemplateColumns: '22px minmax(0, 1fr) 12px', alignItems: 'center', gap: 7,
            border: `1px solid ${TM.line}`, borderRadius: 8, background: '#fff',
            padding: '10px 9px', textDecoration: 'none', minWidth: 0, minHeight: 58,
            color: TM.ink, fontWeight: 800, fontSize: 'clamp(11.5px, 3.25vw, 13px)',
          }}>
            <PurposeIcon width="21" height="21" color={TM.green} />
            <span style={{ minWidth: 0, lineHeight: 1.4, wordBreak: 'keep-all', overflowWrap: 'anywhere' }}>
              <BT_TM>{it.label}</BT_TM>
            </span>
            <Ic_TM.arrow width="12" height="12" color={O.deep} />
          </a>
          );
        })}
      </div>
    </section>
  );
}

function MTopCases() {
  const pairs = CASES_TM
    .filter((c) => c && c.before && c.after && c.before !== c.after)
    .slice(0, 2)
    .map((c) => ({ before: c.before, after: c.after, afterLabel: '施工後' }));
  const Tag = ({ children, bg }) => (
    <div style={{ position: 'absolute', top: 6, left: 6, zIndex: 1, background: bg, color: '#fff', fontSize: 12, fontWeight: 800, padding: '3px 7px', borderRadius: 3, lineHeight: 1 }}>{children}</div>
  );
  const Arrow = () => (
    <span style={{ position: 'absolute', top: '50%', right: -11, transform: 'translateY(-50%)', zIndex: 2, width: 22, height: 22, borderRadius: '50%', background: O.main, color: '#fff', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 2px 6px rgba(0,0,0,0.25)' }}>
      <Ic_TM.arrow width="13" height="13" color="#fff" />
    </span>
  );
  const Pair = ({ p }) => (
    <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 6 }}>
      <div style={{ position: 'relative' }}>
        <Tag bg={TM.ink}>施工前</Tag>
        <Img_TM src={p.before} ratio="1/1" radius={5} style={{ filter: 'saturate(0.7) brightness(0.96)' }} />
        <Arrow />
      </div>
      <div style={{ position: 'relative' }}>
        <Tag bg={O.main}>{p.afterLabel}</Tag>
        <Img_TM src={p.after} ratio="1/1" radius={5} />
      </div>
    </div>
  );
  if (!pairs.length) return null;
  return (
    <section style={{ background: '#fff', padding: '8px 16px 16px' }}>
      <div style={{ border: `1px solid ${TM.line}`, borderRadius: 9, overflow: 'hidden', background: '#fff' }}>
        {/* 見出し帯は情報表示なので中立色（オレンジは押す場所だけに残す） */}
        <div style={{ background: TM.greenSoft, color: TM.greenInk, textAlign: 'center', fontSize: 15, fontWeight: 800, padding: '9px 12px', letterSpacing: 0.3, borderBottom: `1px solid ${TM.line}` }}>
          施工事例でわかる！実際の施工をご紹介
        </div>
        <div style={{ padding: 10 }}>
          <div style={{ display: 'grid', gridTemplateColumns: pairs.length === 1 ? '1fr' : '1fr 1fr', gap: 14 }}>
            {pairs.map((p, i) => <Pair key={i} p={p} />)}
          </div>
          <a href={hrefTM('cases')} style={{
            marginTop: 10, height: 44, border: `1.5px solid ${O.line}`, borderRadius: 8,
            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
            color: O.deep, fontWeight: 800, fontSize: 16, textDecoration: 'none', position: 'relative',
          }}>
            <Ic_TM.camera width="18" height="18" color={O.deep} /> 施工事例を見る
            <Ic_TM.arrow width="16" height="16" color={O.deep} style={{ position: 'absolute', right: 14 }} />
          </a>
        </div>
      </div>
    </section>
  );
}

// ご相談から完工までの流れ — メニュー「相談の流れ」(top#flow) の飛び先。service-detail と同じ縦リスト
function MTopFlow() {
  return (
    <section id="flow" style={{ background: '#fff', padding: '14px 16px 10px', scrollMarginTop: 76 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 6px' }}>
        <span style={{ flex: 1, height: 1, background: TM.line }} />
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 7, color: TM.greenInk, fontWeight: 800, fontSize: 17 }}>
          <Ic_TM.check width="19" height="19" color={TM.green} /> ご相談から完工までの流れ
        </span>
        <span style={{ flex: 1, height: 1, background: TM.line }} />
      </div>
      <div style={{ display: 'grid', gap: 0 }}>
        {FL_TM.map((x, i) => (
          <div key={x.n} style={{ display: 'grid', gridTemplateColumns: '36px 1fr', gap: 12, padding: '13px 0', borderBottom: i < FL_TM.length - 1 ? `1px solid ${TM.line}` : 'none' }}>
            <span style={{ width: 30, height: 30, borderRadius: '50%', background: TM.green, color: '#fff', display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontWeight: 800, fontSize: 14, lineHeight: 1 }}>{x.n || i + 1}</span>
            <div>
              <div style={{ color: TM.ink, fontWeight: 800, fontSize: 14.5, lineHeight: 1.5 }}>{x.title}</div>
              <div data-flow-body={x.n} style={{ color: TM.ink70, fontSize: 12.5, lineHeight: 1.85, marginTop: 3, wordBreak: 'auto-phrase', overflowWrap: 'break-word' }}><BT_TM noBreakPhrases={FLOW_NO_BREAK_TM}>{x.body}</BT_TM></div>
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}

function MTopKnowledge() {
  const top3 = S_TM.knowledgeCardsFromArticles(ART_TM, S_TM.KNOWLEDGE).slice(0, 3);
  if (!top3.length) return null;
  return (
    <section style={{ background: TM.paper, padding: '18px 16px 20px' }}>
      <div style={{ display: 'flex', alignItems: 'end', justifyContent: 'space-between', gap: 12, marginBottom: 12 }}>
        <div>
          <div style={{ color: TM.green, fontSize: 10.5, fontWeight: 800, letterSpacing: 2.5, marginBottom: 4 }}>KNOWLEDGE</div>
          <h2 style={{ margin: 0, color: TM.ink, fontSize: 20, fontWeight: 900 }}>住まいのお役立ちコラム</h2>
        </div>
        <a href={hrefTM('knowledge')} style={{ color: O.deep, textDecoration: 'none', display: 'inline-flex', alignItems: 'center', gap: 4, fontSize: 12, fontWeight: 800, whiteSpace: 'nowrap' }}>
          一覧へ <Ic_TM.arrow width="13" height="13" color={O.deep} />
        </a>
      </div>
      <div style={{ display: 'grid', gap: 10 }}>
        {top3.map((article) => (
          <a
            key={article.id}
            data-top-knowledge-card
            data-article-id={article.id}
            href={articleHrefTM(article.id)}
            style={{
              display: 'grid', gridTemplateColumns: '92px minmax(0, 1fr) 16px',
              alignItems: 'center', gap: 11, minHeight: 82, background: '#fff',
              border: `1px solid ${TM.line}`, borderRadius: 8, overflow: 'hidden',
              color: TM.ink, textDecoration: 'none',
            }}
          >
            <Img_TM src={article.photo} ratio="1/1" style={{ height: '100%', minHeight: 82 }} />
            <span style={{ display: 'grid', gap: 4, minWidth: 0, padding: '9px 0' }}>
              <span style={{ color: TM.green, fontSize: 10.5, fontWeight: 800 }}>{article.tag || article.cat || 'その他'}</span>
              <span style={{ fontSize: 13.5, fontWeight: 800, lineHeight: 1.55, wordBreak: 'auto-phrase', overflowWrap: 'break-word' }}>{article.title}</span>
            </span>
            <Ic_TM.arrow width="14" height="14" color={O.deep} />
          </a>
        ))}
      </div>
    </section>
  );
}

function MTopPhoneCTA() {
  const PhoneCard = M_TOP.MPhoneCTACard;
  return (
    <section style={{ background: '#fff', padding: '0 16px 10px' }}>
      <PhoneCard />
    </section>
  );
}

// 大切な想い — トップの締め。写真はCMS assets.philosophyPhotoで設定。
// 空のときはグリーン背景（2026-08-08 クライアント依頼で写真なし状態を正式対応）
function MTopPhilosophy() {
  const title = copyTM('services.philosophy.title', 'スマートライフの大切な想い');
  const body = String(copyTM('services.philosophy.body', '私たちは工事をする会社ではなく、お客様の暮らしに寄り添うパートナーでありたいと考えています。\n一つひとつのご縁を大切にし、「相談して良かった」「任せて良かった」そう思っていただける仕事を積み重ね、地域に必要とされる会社を目指します。'));
  const photo = AS_TM.philosophyPhoto;
  if (!String(title).trim() && !body.trim()) return null;
  // 「」で囲まれた言葉は白のまま太字（写真上なのでオレンジは沈むため、太字＋わずかな字間で強調する）
  const renderPara = (para, pi) => (
    <p key={pi} style={{
      margin: pi === 0 ? '0 0 10px' : '0 0 10px', color: 'rgba(255,255,255,0.94)',
      fontSize: 13, lineHeight: 2, fontFeatureSettings: "'palt'",
      wordBreak: 'auto-phrase', overflowWrap: 'break-word',
      textShadow: '0 1px 12px rgba(0,0,0,0.45)',
    }}>
      {para.split(/(「[^」]*」)/).map((chunk, ci) => (
        chunk.startsWith('「') && chunk.endsWith('」')
          ? <span key={ci} style={{ fontWeight: 800, color: '#fff', letterSpacing: 0.2 }}>{chunk}</span>
          : <React.Fragment key={ci}>{chunk}</React.Fragment>
      ))}
    </p>
  );
  return (
    <section data-philosophy-section style={{ background: '#fff', padding: '4px 16px 22px' }}>
      {/* サイト全体が白カード＋細罫の文法なので、想いも同じ「カード」に収める。
          フルブリードの暗い帯にすると流れの中で浮くため（2026-07-30 調整） */}
      <div style={{
        position: 'relative', overflow: 'hidden', borderRadius: 12,
        background: photo ? TM.ink : 'linear-gradient(120deg, #0a2f22 0%, #0f4d36 60%, #1f6f4f 100%)',
        minHeight: 356, display: 'flex', alignItems: 'center',
      }}>
        {photo && <img src={photo} alt="" aria-hidden="true" loading="lazy" decoding="async" style={{
          position: 'absolute', inset: 0, width: '100%', height: '100%',
          objectFit: 'cover', objectPosition: '68% 40%', display: 'block',
        }} />}
        {/* A案の構図は保ち、黒い重さだけを抜いた深緑の薄いベールにする。
            写真を明るく見せつつ、白文字の可読性は中央〜下部で確保する */}
        {photo && <div aria-hidden="true" style={{
          position: 'absolute', inset: 0,
          background: 'linear-gradient(180deg, rgba(27,42,34,0.18) 0%, rgba(27,42,34,0.44) 38%, rgba(23,37,30,0.58) 74%, rgba(28,43,35,0.42) 100%)',
        }} />}
        <div style={{ position: 'relative', zIndex: 1, padding: '34px 20px 36px' }}>
        <span style={{
          display: 'inline-block', fontSize: 10.5, fontWeight: 800, letterSpacing: 2,
          color: 'rgba(255,255,255,0.72)', marginBottom: 10,
        }}>OUR PHILOSOPHY</span>
        <h2 style={{
          fontFamily: '"Noto Sans JP", sans-serif', fontWeight: 900, fontSize: 20, lineHeight: 1.6,
          color: '#fff', margin: '0 0 14px', letterSpacing: -0.4, fontFeatureSettings: "'palt'",
          wordBreak: 'auto-phrase', textShadow: '0 2px 16px rgba(0,0,0,0.5)',
        }}>{title}</h2>
        <span aria-hidden="true" style={{ display: 'block', width: 38, height: 2, background: 'rgba(255,255,255,0.55)', marginBottom: 16 }} />
          {body.split(/\n+/).filter(Boolean).map(renderPara)}
        </div>
      </div>
    </section>
  );
}

function MPageTop() {
  return (
    <Page_TM active="home">
      <MTopHero />
      <MTopServices />
      <MTopLicense />
      <MTopPurposes />
      <MTopCases />
      <MTopFlow />
      <MTopKnowledge />
      <MTopPhoneCTA />
      <MTopPhilosophy />
    </Page_TM>
  );
}

window.MPageTop = MPageTop;
window.MTopPhilosophy = MTopPhilosophy;
