:root {
  --bg: #0a1628;
  --bg2: #0f1f33;
  --card: #14253d;
  --card2: #1b3050;
  --line: #24405f;
  --txt: #e8eef5;
  --muted: #8aa0ba;
  --accent: #3aa0ff;
  --accent2: #00d4a0;
  --warn: #ffb648;
  --radius: 14px;
  --maxw: 1200px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--txt);
  font-family: 'Inter', -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  padding-bottom: 90px;
}

/* ---- Header ---- */
.app-header {
  position: sticky; top: 0; z-index: 60;
  background-color: #0a1628;
  background-image: linear-gradient(180deg, #0c1c30 0%, #0a1628 100%);
  border-bottom: 1px solid var(--line);
  padding: 12px 16px 10px;
  padding-top: max(12px, env(safe-area-inset-top));
}
.header-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; max-width: var(--maxw); margin: 0 auto; }
h1 { font-size: 19px; margin: 0; font-weight: 700; letter-spacing: .3px; display: flex; align-items: baseline; gap: 8px; }
h1 .sub { font-size: 12px; color: var(--muted); font-weight: 500; }
.search {
  width: 100%; max-width: var(--maxw); margin: 10px auto 0; display: block;
  background: var(--card); border: 1px solid var(--line); color: var(--txt);
  border-radius: 10px; padding: 10px 12px; font-size: 15px; outline: none;
}
.search:focus { border-color: var(--accent); }
.filters { display: flex; gap: 7px; flex-wrap: wrap; max-width: var(--maxw); margin: 10px auto 0; }
.chip {
  background: var(--card); border: 1px solid var(--line); color: var(--muted);
  border-radius: 999px; padding: 5px 12px; font-size: 12.5px; cursor: pointer; white-space: nowrap;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #03101f; font-weight: 600; }

/* ---- Grid ---- */
.grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px; padding: 14px 16px; max-width: var(--maxw); margin: 0 auto;
}
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; cursor: pointer; position: relative; transition: border-color .15s, transform .05s;
}
.card:active { transform: scale(.99); }
.card.selected { border-color: var(--accent2); box-shadow: 0 0 0 1px var(--accent2) inset; }
.card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card-model { font-size: 16.5px; font-weight: 700; line-height: 1.25; }
.card-fam { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.tops-badge {
  flex-shrink: 0; text-align: right; background: var(--card2); border: 1px solid var(--line);
  border-radius: 9px; padding: 5px 9px; min-width: 64px;
}
.tops-badge .n { font-size: 17px; font-weight: 800; color: var(--accent2); line-height: 1; }
.tops-badge .l { font-size: 9.5px; color: var(--muted); letter-spacing: .5px; }
.tops-badge.none .n { color: var(--muted); font-size: 13px; }
.card-seg { font-size: 12px; color: var(--muted); margin: 9px 0 10px; min-height: 2.6em; }
.card-specs { display: flex; flex-wrap: wrap; gap: 5px; }
.spec-pill { font-size: 11px; background: var(--bg2); border: 1px solid var(--line); border-radius: 7px; padding: 3px 7px; color: var(--txt); }
.spec-pill b { color: var(--muted); font-weight: 500; }
.tier-dot { position: absolute; top: 14px; right: 14px; }
.cmp-add {
  position: absolute; bottom: 10px; right: 10px; width: 30px; height: 30px; border-radius: 8px;
  background: var(--card2); border: 1px solid var(--line); color: var(--muted); font-size: 18px;
  display: grid; place-items: center; cursor: pointer;
}
.card.selected .cmp-add { background: var(--accent2); color: #03101f; border-color: var(--accent2); }
.no-results { grid-column: 1/-1; text-align: center; color: var(--muted); padding: 40px 0; }

/* ---- Compare button ---- */
.cmp-btn { background: var(--card); border: 1px solid var(--line); color: var(--txt); border-radius: 9px; padding: 7px 12px; font-size: 13px; cursor: pointer; font-weight: 600; }
.cmp-count { display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 4px; margin-left: 3px; background: var(--accent2); color: #03101f; border-radius: 9px; font-size: 11px; }
.cmp-count.zero { background: var(--line); color: var(--muted); }

/* ---- Sheet (detail / compare) ---- */
.sheet { position: fixed; inset: 0; z-index: 70; background: rgba(4,10,20,.72); display: flex; align-items: flex-end; justify-content: center; }
.sheet-inner {
  background: var(--bg); border: 1px solid var(--line); border-radius: 18px 18px 0 0;
  width: 100%; max-width: 760px; max-height: 92vh; overflow-y: auto; padding: 18px 18px 40px; position: relative;
  animation: slideUp .18s ease;
}
@keyframes slideUp { from { transform: translateY(30px); opacity: .5; } to { transform: translateY(0); opacity: 1; } }
.sheet-close { position: absolute; top: 12px; right: 12px; width: 34px; height: 34px; border-radius: 50%; background: var(--card); border: 1px solid var(--line); color: var(--txt); font-size: 20px; cursor: pointer; z-index: 2; }

/* ---- Detail ---- */
.d-head { padding-right: 40px; }
.d-model { font-size: 22px; font-weight: 800; }
.d-fam { color: var(--muted); font-size: 13px; margin-top: 2px; }
.d-seg { font-size: 13.5px; margin: 8px 0 4px; color: var(--accent); }
.d-note { font-size: 12.5px; color: var(--warn); background: rgba(255,182,72,.08); border: 1px solid rgba(255,182,72,.25); border-radius: 8px; padding: 8px 10px; margin: 10px 0; }
.d-hero { display: flex; gap: 10px; margin: 14px 0; flex-wrap: wrap; }
.hero-stat { flex: 1; min-width: 90px; background: var(--card); border: 1px solid var(--line); border-radius: 11px; padding: 10px 12px; }
.hero-stat .v { font-size: 17px; font-weight: 800; color: var(--accent2); }
.hero-stat .k { font-size: 11px; color: var(--muted); margin-top: 2px; }
.spec-section { margin-top: 16px; }
.spec-section h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); margin: 0 0 6px; border-bottom: 1px solid var(--line); padding-bottom: 5px; }
.spec-row { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px solid rgba(36,64,95,.4); font-size: 13.5px; }
.spec-row .k { flex: 0 0 92px; color: var(--muted); }
.spec-row .v { flex: 1; }
.d-actions { margin-top: 18px; display: flex; gap: 10px; flex-wrap: wrap; }
.btn-link { display: inline-block; background: var(--accent); color: #03101f; text-decoration: none; padding: 10px 16px; border-radius: 10px; font-weight: 700; font-size: 13.5px; }
.btn-link.ghost2 { background: var(--card); color: var(--txt); border: 1px solid var(--line); }

/* ---- Compare bar ---- */
.compare-bar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  background: linear-gradient(180deg, #102443, #0a1628); border-top: 1px solid var(--line);
  padding: 10px 14px; padding-bottom: max(10px, env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
}
.compare-chips { display: flex; gap: 6px; overflow-x: auto; flex: 1; }
.cc { background: var(--card2); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; font-size: 12px; white-space: nowrap; display: flex; gap: 6px; align-items: center; }
.cc button { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 0; }
.compare-actions { display: flex; gap: 8px; flex-shrink: 0; }
.primary { background: var(--accent2); color: #03101f; border: none; border-radius: 9px; padding: 9px 14px; font-weight: 700; font-size: 13px; cursor: pointer; }
.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); border-radius: 9px; padding: 9px 12px; font-size: 13px; cursor: pointer; }

/* ---- Compare table ---- */
.cmp-table-wrap { overflow-x: auto; margin-top: 8px; }
table.cmp { border-collapse: collapse; width: 100%; font-size: 12.5px; }
table.cmp th, table.cmp td { border: 1px solid var(--line); padding: 8px 10px; text-align: left; vertical-align: top; }
table.cmp thead th { background: var(--card2); position: sticky; top: 0; font-size: 13px; }
table.cmp tbody th { background: var(--card); color: var(--muted); font-weight: 500; white-space: nowrap; position: sticky; left: 0; }
table.cmp td { min-width: 130px; }
table.cmp tr.hl td, table.cmp tr.hl th { background: rgba(0,212,160,.06); }
.cmp-best { color: var(--accent2); font-weight: 700; }

/* ---- Footer ---- */
.app-footer { text-align: center; color: var(--muted); font-size: 11px; padding: 18px 16px 6px; }

@media (min-width: 640px) {
  .sheet { align-items: center; }
  .sheet-inner { border-radius: 18px; }
}

/* =========================================================
   折りたたみ/大画面 2ペイン（Galaxy Z Fold 等）
   狭い画面は従来どおり（一覧＋モーダル詳細）。
   広い画面では 左=一覧 / 右=詳細 を同時表示。
   ========================================================= */
/* =========================================================
   広い画面 / 折りたたみ展開: 開閉式 2ペイン
   - 詳細が閉じている時: 一覧が全幅（複数カラム）で広く見れる
   - 機種タップで詳細が右に開き 2カラムに。× で閉じれば全幅へ戻る
   - ページ全体スクロール + 不透明 sticky ヘッダー（被り構造的に発生しない）
   ========================================================= */
@media (min-width: 720px) {
  /* 詳細を開いている時だけ 2カラム。閉じている時は #grid が全幅グリッド。
     開いている時はヘッダー下を固定高さにして、左右ペインが独立スクロール */
  .layout.show-detail {
    display: flex;
    align-items: stretch;
    max-width: var(--maxw);
    margin: 0 auto;
    height: calc(100dvh - var(--header-h, 150px));
    overflow: hidden;
  }
  .layout.show-detail #grid {
    flex: 0 0 clamp(300px, 36%, 440px);
    width: auto;
    grid-template-columns: 1fr;
    align-content: start;
    border-right: 1px solid var(--line);
    margin: 0;
    height: 100%;
    overflow-y: auto;
    padding: 14px 14px 90px;
  }
  /* 右ペイン: 詳細（独立スクロール。position:static で常にヘッダーの下） */
  .layout.show-detail #detail.sheet {
    position: static;
    z-index: auto;
    inset: auto;
    background: none;
    display: block;
    flex: 1 1 auto;
    min-width: 0;
    height: 100%;
    max-height: none;
    overflow-y: auto;
  }
  .layout.show-detail #detail .sheet-inner {
    position: relative;
    background: none;
    border: none;
    border-radius: 0;
    max-width: none;
    max-height: none;
    animation: none;
    padding: 16px 22px 60px;
  }
  /* 2ペイン時の閉じるボタン: 詳細ペイン内の最上部に sticky 常駐（独立スクロールしても押せる） */
  .layout.show-detail #detail .sheet-close {
    display: grid;
    place-items: center;
    position: sticky;
    top: 0;
    width: 34px;
    height: 34px;
    margin: 0 0 8px auto;
    z-index: 6;
    background: var(--card2);
  }
  .card.detail-active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
}

/* 折りたたみ展開時のヒンジ対応（実機のFoldで要確認）。
   2ペインを開いている時、左ペインを左セグメント幅に合わせ、詳細を折り目の先へ寄せる */
@media (horizontal-viewport-segments: 2) and (min-width: 600px) {
  .layout.show-detail #grid {
    flex: 0 0 env(viewport-segment-right 0 0, 360px);
    width: auto;
  }
  .layout.show-detail #detail.sheet {
    margin-left: calc(env(viewport-segment-left 1 0, 0px) - env(viewport-segment-right 0 0, 0px));
  }
}
