:root {
    --bg: #111;
    --fg: #fff;
    --accent: #30B0C7; /* systemTeal */
}

html, body {
    width: 100%;
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, sans-serif;

    /* Safe‑Area Padding (modern & legacy) */
    padding: env(safe-area-inset-top) env(safe-area-inset-right)
             env(safe-area-inset-bottom) env(safe-area-inset-left);
    padding: constant(safe-area-inset-top) constant(safe-area-inset-right)
             constant(safe-area-inset-bottom) constant(safe-area-inset-left);

    box-sizing: border-box;
}

/* ───────────────────────────────────────── Top Bar */
.top-bar {
    background: var(--accent);
    height: 48px;
    width: 100vw;
    position: fixed;
    top: 0;
    left: 0;

    display: flex;
    align-items: center;
    justify-content: center;

    box-sizing: border-box;
    z-index: 20;
}

.top-bar-content {
    display: flex;
    width: 100%;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px; /* 内側に余白を寄せる */
}

.logo-link {
    display: flex;
    align-items: center;
    height: 100%;
}

.logo-img {
    height: 32px;
    width: auto;
    object-fit: contain; /* ロゴが潰れず綺麗に表示される */
    image-rendering: auto;
}

.top-bar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

/* Close button uses an image */
.close-button {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
}

.close-button img {
    width: 28px;
    height: 28px;
    filter: invert(100%);
    transition: filter 0.15s ease;
}

.close-button:hover img {
    filter: brightness(0) saturate(100%)
            invert(71%) sepia(13%) saturate(727%)
            hue-rotate(153deg) brightness(95%) contrast(92%);
}

/* ───────────────────────────────────────── Menu */
.menu {
    position: fixed;
    background: var(--bg);
    display: flex;
    z-index: 10;
    padding-left: env(safe-area-inset-left);
    padding-left: constant(safe-area-inset-left);
    box-sizing: border-box;
}

.menu ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
}

.menu a {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
}

.menu img {
    width: 28px;
    height: 28px;
    filter: invert(100%);
    transition: filter 0.15s ease;
}

.menu a:hover img,
.menu a.active img,
.menu a[aria-current="page"] img {
    filter: brightness(0) saturate(100%) invert(52%) sepia(28%) saturate(642%) hue-rotate(142deg) brightness(95%) contrast(88%);
}

/* ───────────────────────────────────────── Buttons */
.btn {                           /* すべてのボタンの土台 */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 24px;            /* ← 高さ統一ポイント */
  min-height: 44px;              /* iOS ヒット領域基準 */
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;            /* ← 角丸も統一 */
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

/* primary（検索／保存） */
.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: #2695a8; }

/* social（Google / Apple） */
.btn-social {
  background: #fff;
  color: #3c4043;
  border: 1px solid #dadce0;
}
.btn-social:hover { background: #f1f3f4; }

/* danger（連携解除） */
.btn-danger {
  background: #332;
  color: #f88;
  border: 1px solid #833;
}
.btn-danger:hover { background: #553; }

/* logout は primary の色違いで幅 auto */
.btn-logout {
  background: #444;
  color: #fff;
}
.btn-logout:hover { background: #666; }

/* ───────────────────────────────────────── Main */
.main-content {
    padding-top: 48px; /* トップバー48px + α (余白) */
    padding-left: 16px;
    padding-right: 16px;
    padding-bottom: env(safe-area-inset-bottom);
    padding-bottom: constant(safe-area-inset-bottom);
    box-sizing: border-box;
}

/* ───────────────────────────────────────── Responsive */
@media (max-width: 768px) {
    .menu {
        bottom: 0;
        left: 0;
        width: 100%;
        flex-direction: row;
        border-top: 1px solid #333;
    }

    .menu ul {
        width: 100%;
        justify-content: space-around;
    }

    .main-content {
        padding-bottom: 64px; /* menu height */
    }
}

@media (min-width: 769px) {
    .menu {
        top: 48px;
        left: 0;
        width: 64px;
        height: calc(100vh - 48px);
        flex-direction: column;
        align-items: center;
        border-right: 1px solid #333;
    }

    .menu ul {
        flex-direction: column;
    }

    .main-content {
        margin-left: 64px;
    }
}

/* ───────────────────────────────────────── Placeholder */
.placeholder {
    margin-top: 32px;
    text-align: center;
    font-size: 18px;
    opacity: 0.7;
}

/* ───────────────────────────────────────── Search Form */
.search-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 600px;
    margin: 12px auto 0 auto; /* 上寄せ */
}

.form-row {
    display: flex;
    align-items: center;
    max-width: 100%;
    /* gap: 12px; ← これを削除 */
}

.form-row label {
    width: 140px;             /* 明示的に固定幅 */
    text-align: right;
    margin-right: 12px;       /* gap の代わり */
    font-size: 14px;
    font-weight: 500;
}

.form-row select {
    flex: 1;
    min-width: 0;
    padding: 8px;
    font-size: 16px;
    border-radius: 6px;
    border: 1px solid #555;
    background-color: #222;
    color: var(--fg);
    appearance: none;
}

/* 検索ボタン */
.form-buttons {
    margin-top: 12px;
    text-align: center;
}

.form-buttons .btn { width: auto; }   /* ← full 幅を解除 */

/* 検索結果エリア */
#search-results {
    margin-top: 24px;
    padding: 12px;
    background: #1a1a1a;
    border-radius: 8px;
    min-height: 200px;
}

.engine-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 16px;
    margin-top: 16px;
}

a.engine-card {
    display: block; /* ブロック要素化でgridレイアウト崩れ防止 */
    background: #222;
    border-radius: 8px;
    overflow: hidden;
    text-align: center;
    padding: 8px;
    text-decoration: none; /* 下線消し */
    color: inherit;         /* 親要素の文字色を継承 */
    transition: box-shadow 0.15s;
}

a.engine-card:hover,
a.engine-card:active {
    box-shadow: 0 0 12px 0 #30B0C7, 0 2px 8px #0006;
}

.engine-card img {
    width: 100%;
    aspect-ratio: 1 / 1;
    object-fit: cover;
    border-radius: 4px;
}

.engine-info {
    margin-top: 8px;
    font-size: 14px;
    color: #fff;
}

.engine-maker {
    font-weight: 600;
    opacity: 0.9;
}

.engine-model {
    opacity: 0.7;
}

/* ───────────────────────────────────────── Engine Detail */

.engine-detail {
    display: flex;
    flex-direction: column;
    gap: 16px;
    margin-top: 16px;
}

.ogp-image {
    width: 100%;
    border-radius: 8px;
    object-fit: cover;
    background-color: #222;
    aspect-ratio: 1.91 / 1;
}

.ogp-placeholder {
    width: 100%;
    height: 0;
    padding-top: 75%;
    border-radius: 8px;
    background-color: #444;
    text-align: center;
    line-height: 1;
    position: relative;
    color: #ccc;
}

.ogp-placeholder::after {
    content: "No Image";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.engine-spec-table {
    width: 100%;
    border-collapse: collapse;
    background: #1a1a1a;
    border-radius: 8px;
    overflow: hidden;
}

.engine-spec-table th,
.engine-spec-table td {
    border: 1px solid #333;
    padding: 8px 12px;
    text-align: center;
    font-size: 14px;
}

.engine-spec-table th {
    background-color: #222;
    font-weight: 500;
    color: #ccc;
}

/* 横長（PC）で左右分割表示 */
@media (min-width: 700px) {
    .engine-detail {
        flex-direction: row;
        align-items: flex-start;
    }

    .ogp-area {
        flex: 1;
        max-width: 600px;
    }

    .engine-spec-table {
        flex: 1;
        margin-left: 24px;
    }
}

.engine-actions {
    display: flex;
    justify-content: space-around;  /* ← 等間隔に配置 */
    align-items: center;
    margin-top: 12px;               /* 画像との間隔 */
    padding: 0 12px;                /* 左右に少し余白 */
    gap: 8px;                       /* モバイル時に少し余裕 */
}

.icon-button {
    background: none;
    border: none;
    padding: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s ease;
}

.icon-button img {
    width: 28px;
    height: 28px;
    transition: filter 0.2s;
}

.icon-button img.invert {
    filter: invert(100%);
}

.icon-button:hover img:not(.invert) {
    filter: brightness(0) saturate(100%) invert(52%) sepia(28%) saturate(642%) hue-rotate(142deg) brightness(95%) contrast(88%);
}

.icon-button img.liked {
  filter: invert(14%) sepia(90%) saturate(6000%) hue-rotate(-5deg)
          brightness(96%) contrast(110%) !important;
}

.icon-button img.flash {
    filter: brightness(0) saturate(100%)
            invert(52%) sepia(28%) saturate(642%)
            hue-rotate(142deg) brightness(95%) contrast(88%);
}

.like-count {
    margin-left: 8px;           /* アイコンとの間隔 */
    font-size: 0.9rem;   /* ボタンより一回り小さく */
    color: #CCC;         /* X(Twitter) 風に薄グレー */
}

/* ===== In-App (WKWebView) 調整: トップバーを非表示＋上余白をゼロ化 ===== */
html.in-app .top-bar {
  display: none !important;
}

/* SafeAreaはアプリ側で処理されている想定：上だけ 0、左右/下は維持 */
html.in-app body {
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left) !important;
  padding: 0 constant(safe-area-inset-right) constant(safe-area-inset-bottom) constant(safe-area-inset-left) !important;
}

/* 上部の隙間を消す（トップバー分の48pxを無効化） */
html.in-app .main-content {
  padding-top: 0 !important;
}

/* デスクトップ幅のサイドメニューも、トップバーなし前提に合わせる */
@media (min-width: 769px) {
  html.in-app .menu {
    top: 0 !important;
    height: 100vh !important;
  }
  /* 既存の .main-content { margin-left: 64px; } はそのままでOK */
}

/* ───────────────────────────────────────── Compact search bar (landscape & low height) */
@media (orientation: landscape) and (max-height: 500px) {

    /* 排気量・変速方式などの高度フィルタは隠す */
    .search-form .filter-advanced {
        display: none;
    }

    /* フォーム全体：3つの項目＋検索ボタンを wrap 可能に */
    .search-form {
        flex-direction: row;
        flex-wrap: wrap;              /* ← ここでボタンを 2 行目に落とせる */
        align-items: flex-end;
        justify-content: flex-start;
        gap: 12px;
        max-width: none;
        margin: 8px 8px 0 8px;
    }

    /* 各フォーム行：ラベル＋セレクトを縦積み */
    .search-form .form-row {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        flex: 1 1 0;
        min-width: 0;
    }

    .search-form .form-row label {
        display: block;
        width: auto;
        margin: 0 0 4px 0;
        text-align: left;
        font-size: 14px;
        font-weight: 500;
    }

    .search-form .form-row select {
        width: 100%;
        padding: 6px 8px;
        font-size: 16px;
        border-radius: 6px;
    }

    /* 検索ボタンだけ 2 行目に配置 */
    .search-form .form-buttons {
        flex: 0 0 100%;      /* 2 行目いっぱいを占有 */
        margin-top: 0;
        text-align: center;  /* ← 中央寄せに変更 */
        align-self: auto;
    }

    .search-form .form-buttons .btn {
        padding: 8px 16px;
        min-height: 36px;
        font-size: 16px;
    }

    #search-results {
        margin-top: 12px;
        padding: 8px;
    }

    .engine-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 12px;
    }

    .engine-card img {
        aspect-ratio: 4 / 3;
    }
}

/* ───────────────────────────────────────── Static Pages (upload_ja, etc.) */
.static-page {
    /* max-width: 720px;  ← いったん外してしまうか */
    margin: 8px 0 24px 0;   /* auto をやめて中央寄せしない */
    padding-left: 12px;     /* 左だけ少しインデント */
    font-size: 14px;
    line-height: 1.7;
}

/* h3 はそのまま */
.static-page h3 {
    font-size: 16px;
    margin: 16px 0 8px 0;
}

/* p は特別な左マージンは不要になるので、シンプルに */
.static-page p {
    margin-top: 8px;
    margin-bottom: 8px;
}

/* ul もデフォルトのインデントに任せる */
.static-page ul {
    margin-top: 8px;
    margin-bottom: 8px;
}
