/* ===== リセット & ベース ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #0f0f0f;
    --bg-card: #1a1a1a;
    --bg-input: #252525;
    --text: #e8e8e8;
    --text-sub: #888;
    --accent: #1d9bf0;
    --accent-hover: #1a8cd8;
    --green: #00ba7c;
    --red: #f4212e;
    --border: #333;
    --radius: 12px;
    --max-w: 720px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== ヘッダー ===== */
.site-header {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    padding: 12px 16px;
    position: sticky;
    top: 0;
    z-index: 100;
}
.header-inner {
    max-width: var(--max-w);
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}
.site-title { font-size: 18px; font-weight: 700; }
.site-title a { color: var(--text); }
.site-title a:hover { text-decoration: none; }
.site-stats { font-size: 12px; color: var(--text-sub); display: none; gap: 12px; }
.site-stats strong { color: var(--accent); }
.admin-mode .site-stats { display: flex; }

/* ===== コンテナ ===== */
.container {
    max-width: var(--max-w);
    margin: 0 auto;
    padding: 16px;
}

/* ===== DL入力セクション ===== */
.download-section {
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 16px;
}
.download-section h2 {
    font-size: 16px;
    margin-bottom: 12px;
    color: var(--text-sub);
}
.url-input-area {
    display: flex;
    gap: 8px;
}
.url-input-area input {
    flex: 1;
    padding: 12px 16px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: 9999px;
    color: var(--text);
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
}
.url-input-area input:focus { border-color: var(--accent); }
.url-input-area input::placeholder { color: #555; }

.url-input-area button,
.download-btn {
    padding: 12px 24px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 9999px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.2s;
}
.url-input-area button:hover,
.download-btn:hover { background: var(--accent-hover); }
.url-input-area button:disabled,
.download-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* 抽出ステータス */
.extract-status {
    margin-top: 10px;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
}
.extract-status.loading { background: #1a2a3a; color: var(--accent); }
.extract-status.error { background: #2a1a1a; color: var(--red); }

/* 抽出結果 */
.extract-result {
    margin-top: 16px;
    padding: 12px;
    background: var(--bg-input);
    border-radius: var(--radius);
}
.result-preview {
    display: flex;
    gap: 12px;
    margin-bottom: 12px;
}
.result-preview img {
    width: 120px;
    height: 68px;
    object-fit: cover;
    border-radius: 8px;
    background: #333;
}
.result-info { flex: 1; min-width: 0; }
.result-text {
    font-size: 13px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.result-author { font-size: 12px; color: var(--text-sub); margin-top: 4px; }

.download-btn {
    display: block;
    width: 100%;
    padding: 14px;
    font-size: 16px;
    text-align: center;
    background: var(--green);
}
.download-btn:hover { background: #00a36c; }

/* 画質選択ボタン */
.quality-buttons {
    display: flex;
    gap: 6px;
    margin-top: 8px;
    flex-wrap: wrap;
}
.quality-btn {
    padding: 8px 14px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-sub);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
}
.quality-btn:hover { border-color: var(--accent); color: var(--text); }
.quality-btn.best { border-color: var(--green); color: var(--green); }

/* ===== 広告枠 ===== */
.ad-slot {
    margin: 16px 0;
    text-align: center;
}
.ad-placeholder {
    padding: 20px;
    background: var(--bg-card);
    border: 1px dashed var(--border);
    border-radius: var(--radius);
    color: var(--text-sub);
    font-size: 12px;
}

/* ===== ランキング ===== */
.ranking-section {
    margin-bottom: 16px;
}
.ranking-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;
}
.ranking-header h2 { font-size: 18px; }

.period-tabs { display: flex; gap: 4px; }
.period-tab {
    padding: 6px 16px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 9999px;
    color: var(--text-sub);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.2s;
}
.period-tab:hover { border-color: var(--accent); color: var(--text); }
.period-tab.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

/* ランキングリスト: JS制御Masonryレイアウト（行順配置） */
.ranking-list {
    display: flex;
    align-items: flex-start;
    gap: 12px;
}
.ranking-list > .masonry-col {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* カード縦型 */
.ranking-item {
    width: 100%;
    background: var(--bg-card);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s, background 0.2s;
    position: relative;
}
.ranking-item:hover { transform: translateY(-2px); background: var(--bg-input); }

/* サムネコンテナ — 元動画のアスペクト比をそのまま表示（黒帯なし） */
.rank-poster {
    position: relative;
    width: 100%;
    overflow: hidden;
}
.rank-poster img {
    width: 100%;
    height: auto;
    display: block;
    border-radius: 0;
}

/* 再生ボタンオーバーレイ */
.rank-play-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.25);
    opacity: 0;
    transition: opacity 0.2s;
}
.ranking-item:hover .rank-play-overlay { opacity: 1; }
.rank-play-overlay svg {
    width: 48px;
    height: 48px;
    fill: #fff;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
}
/* video_urlなし → Xアイコン（グレー背景で区別） */
.rank-play-overlay.rank-ext-link {
    background: rgba(0,0,0,0.45);
}
.rank-play-overlay.rank-ext-link svg {
    width: 28px;
    height: 28px;
    fill: #aaa;
}

/* ランク番号（左上に重ねる） */
.rank-number {
    position: absolute;
    top: 6px;
    left: 6px;
    font-size: 14px;
    font-weight: 700;
    background: rgba(0,0,0,0.7);
    color: #fff;
    padding: 2px 7px;
    border-radius: 6px;
    min-width: unset;
    z-index: 2;
}
.rank-number.top-1 { background: rgba(180, 130, 0, 0.85); color: #ffd700; }
.rank-number.top-2 { background: rgba(100, 100, 120, 0.85); color: #c0c0c0; }
.rank-number.top-3 { background: rgba(140, 80, 20, 0.85); color: #cd7f32; }

/* 下部情報エリア（非表示） */
.rank-info { display: none; }
.rank-text {
    font-size: 12px;
    margin-bottom: 4px;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    display: -webkit-box;
    overflow: hidden;
}
.rank-meta {
    font-size: 11px;
    color: var(--text-sub);
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

/* DL数・外部参照カウント */
.rank-dl-count {
    font-size: 12px;
    font-weight: 700;
    color: var(--green);
    text-align: right;
}

/* ロード中 */
.loading {
    text-align: center;
    padding: 40px;
    color: var(--text-sub);
}

.load-more {
    display: block;
    width: 100%;
    padding: 18px;
    margin-top: 16px;
    background: #2563eb;
    border: none;
    border-radius: var(--radius);
    color: #fff;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
}
.load-more:hover { background: #1d4ed8; }

/* ===== モーダル ===== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.85);
    z-index: 1000;
    justify-content: center;
    align-items: center;
}
.modal.active { display: flex; }
.modal-content {
    width: 90%;
    max-width: 640px;
    background: var(--bg-card);
    border-radius: var(--radius);
    overflow: hidden;
    position: relative;
}
.modal-close {
    position: absolute;
    top: 8px;
    right: 12px;
    font-size: 28px;
    color: #fff;
    cursor: pointer;
    z-index: 10;
    line-height: 1;
}
.modal-content video {
    width: 100%;
    display: block;
    max-height: 70vh;
}
.modal-actions { padding: 12px; }
.modal-actions .download-btn { font-size: 14px; padding: 10px; }

/* ===== フッター ===== */
.site-footer {
    text-align: center;
    padding: 24px 16px;
    color: var(--text-sub);
    font-size: 12px;
    border-top: 1px solid var(--border);
    margin-top: 32px;
}
.site-footer a { color: var(--text-sub); }
.copyright { margin-top: 4px; }

/* ===== About ===== */
.about-page h2 { margin-bottom: 20px; }
.about-page h3 { margin: 20px 0 8px; font-size: 16px; }
.about-page ol, .about-page ul { padding-left: 20px; margin-bottom: 12px; }
.about-page li { margin-bottom: 6px; font-size: 14px; }
.about-page p { font-size: 14px; margin-bottom: 8px; }

/* ===== メインナビゲーション ===== */
.main-nav {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-bottom: 12px;
    flex-wrap: wrap;
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 8px;
}
.main-nav-sep {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 4px;
    flex-shrink: 0;
}
.main-tab {
    padding: 8px 18px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 9999px;
    color: var(--text-sub);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s;
}
.main-tab:hover { border-color: var(--accent); color: var(--text); }
.main-tab.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}
.main-tab.save-tab { font-weight: 600; }
.main-tab.save-tab.active {
    background: var(--green);
    border-color: var(--green);
}
.main-tab.ext-tab.active {
    background: #1a6830;
    border-color: #2a8840;
    color: #6ee77a;
}

/* パネル */
.main-panel { animation: fadeIn 0.15s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* モバイル */
@media (max-width: 480px) {
    .main-nav { gap: 3px; padding: 6px; }
    .main-tab { padding: 7px 12px; font-size: 12px; }
    .main-nav-sep { display: none; }
}

/* ===== 外部参照ランキング ===== */
.ext-tab { border-left: 1px solid var(--border) !important; margin-left: 4px; }
.period-tab.active.ext-tab { background: #1a6830; color: #6ee77a; }

.ext-controls {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 0 12px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 8px;
    flex-wrap: wrap;
}
.ext-last-scraped { font-size: 12px; color: var(--text-sub); flex: 1; display: none; }
.ext-sites { font-size: 11px; color: var(--text-sub); }
.scrape-btn {
    display: none;
    background: #1a6830;
    color: #6ee77a;
    border: 1px solid #2a8840;
    padding: 6px 14px;
    border-radius: 6px;
    font-size: 13px;
    cursor: pointer;
    transition: opacity .2s;
}
.admin-mode .ext-last-scraped { display: inline; }
.admin-mode .scrape-btn { display: inline-block; }
.scrape-btn:hover { opacity: .85; }
.scrape-btn:disabled { opacity: .5; cursor: default; }

.rank-ext-count {
    font-size: 11px;
    color: #6ee77a;
    line-height: 1.3;
    text-align: right;
}
.rank-ext-count.no-video { color: #888; }
.ext-count-num { font-size: 18px; font-weight: 700; display: block; }

.source-tags { display: flex; flex-wrap: wrap; gap: 3px; justify-content: flex-end; margin-top: 3px; }
.source-tag {
    background: #1a3020;
    color: #5db870;
    border: 1px solid #2a5030;
    font-size: 10px;
    padding: 1px 4px;
    border-radius: 3px;
    white-space: nowrap;
}

/* ===== 管理モード ===== */

/* 管理トグルボタン（ヘッダー右端） */
.admin-toggle {
    background: transparent;
    border: none;
    font-size: 20px;
    cursor: default;
    opacity: 0;
    padding: 4px 8px;
    line-height: 1;
    flex-shrink: 0;
    -webkit-user-select: none;
    user-select: none;
}
.admin-toggle:hover { opacity: 0; cursor: default; }
.admin-mode .admin-toggle { opacity: 0; }

/* 管理バー */
.admin-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: #0d2818;
    border-bottom: 1px solid #1a5030;
    padding: 6px 16px;
    font-size: 12px;
    color: #6ee77a;
    position: sticky;
    top: 49px;
    z-index: 99;
}
.admin-bar-label { font-weight: 600; }
.admin-off-btn {
    background: #3a1515;
    color: #f4212e;
    border: 1px solid #5a2020;
    padding: 2px 10px;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    font-weight: 600;
    transition: background 0.2s;
}
.admin-off-btn:hover { background: #5a2020; }

/* 管理オーバーレイ（各ランキングアイテム上） */
.admin-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 6px;
    background: rgba(0, 0, 0, 0.82);
    z-index: 5;
    flex-wrap: wrap;
}

/* 管理コントロール共通 */
.admin-ctrl {
    font-size: 11px;
    border: none;
    cursor: pointer;
    border-radius: 3px;
    line-height: 1;
}

/* フラグボタン */
.admin-flag {
    background: transparent;
    font-size: 14px;
    padding: 2px 4px;
    opacity: 0.35;
    transition: opacity 0.2s;
}
.admin-flag:hover { opacity: 0.7; }
.admin-flag.active { opacity: 1; }

/* ブーストコントロール */
.admin-boost-wrap {
    display: flex;
    align-items: center;
    gap: 2px;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 4px;
    padding: 1px 3px;
}
.admin-boost-btn {
    background: rgba(255, 255, 255, 0.12);
    color: #ccc;
    padding: 2px 6px;
    font-size: 12px;
    font-weight: 700;
    border-radius: 3px;
    transition: background 0.2s;
}
.admin-boost-btn:hover { background: rgba(255, 255, 255, 0.25); }
.admin-boost-val {
    font-size: 11px;
    color: #e8e8e8;
    min-width: 16px;
    text-align: center;
    font-weight: 600;
}

/* ティアドロップダウン */
.admin-tier {
    background: rgba(255, 255, 255, 0.1);
    color: #e8e8e8;
    padding: 2px 4px;
    font-size: 10px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 3px;
    outline: none;
}
.admin-tier option {
    background: #1a1a1a;
    color: #e8e8e8;
}

/* リンクチェックボタン */
.admin-link {
    background: transparent;
    font-size: 13px;
    padding: 2px 4px;
}
.admin-link:disabled { opacity: 0.5; cursor: wait; }

/* ブーストバッジ（カード上） */
.admin-boost-badge {
    position: absolute;
    top: 6px;
    right: 6px;
    background: rgba(255, 140, 0, 0.9);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 4px;
    z-index: 3;
}

/* ===== 管理モード ビジュアルインジケーター ===== */

/* フラグ済み: 左ゴールドボーダー */
.admin-mode .ranking-item[data-flagged="1"] {
    border-left: 3px solid #ffd700;
}

/* ティア: 上位 = 緑グロウ */
.admin-mode .ranking-item[data-tier="top"] {
    box-shadow: 0 0 8px 1px rgba(0, 186, 124, 0.45);
    border-color: #00ba7c;
}

/* ティア: 除外 = グレーアウト+取り消し線 */
.admin-mode .ranking-item[data-tier="excluded"] {
    opacity: 0.3;
    filter: grayscale(0.8);
}
.admin-mode .ranking-item[data-tier="excluded"] .rank-text {
    text-decoration: line-through;
}

/* ティア: 下位 = 薄暗く */
.admin-mode .ranking-item[data-tier="bottom"] {
    opacity: 0.55;
}

/* ─── ランキング24h 縦リスト ─── */
.ranking-vertical {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 640px;
    margin: 0 auto;
    padding: 16px;
}

/* 共通カードスタイル */
.ranking-vertical-item {
    position: relative;
    width: 100%;
    background: #1a1a2e;
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    border: 1px solid rgba(255,255,255,0.06);
}
.ranking-vertical-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0,0,0,0.5);
    border-color: rgba(255,255,255,0.12);
}

/* 1位: ゴールド枠線 */
.rank-tier-hero {
    border: 1px solid rgba(255,215,0,0.25);
}

/* ─ サムネイル ─ */
.vertical-rank-poster {
    position: relative;
    width: 100%;
    overflow: hidden;
    background: #000;
}
.vertical-rank-poster img {
    width: 100%;
    height: auto;
    display: block;
}

/* ─ ランク番号（左上端に常時表示、サムネと一体化） ─ */
.vertical-rank-num {
    position: absolute;
    top: 0;
    left: 0;
    font-size: 15px;
    font-weight: 800;
    background: rgba(0,0,0,0.7);
    color: #fff;
    padding: 4px 10px;
    border-radius: 0 0 8px 0;
    z-index: 2;
    line-height: 1.2;
}
.vertical-rank-num.top-1 { background: rgba(180,130,0,0.9); color: #ffd700; font-size: 18px; }
.vertical-rank-num.top-2 { background: rgba(100,100,120,0.9); color: #c0c0c0; font-size: 17px; }
.vertical-rank-num.top-3 { background: rgba(140,80,20,0.9); color: #cd7f32; font-size: 17px; }

/* ─ PC: 縦長ペア行（2列表示） ─ */
.portrait-pair-row {
    display: flex;
    gap: 12px;
    justify-content: center;
}
.portrait-pair-row .ranking-vertical-item {
    flex: 0 1 calc(50% - 6px);
    max-width: calc(50% - 6px);
}

/* 縦動画（9:16）: 自然な縦長アスペクト比で全体表示 */
.vertical-rank-poster.is-portrait img {
    aspect-ratio: auto;
    object-fit: cover;
    height: auto;
}

/* リアルタイムCTAボタン */
.realtime-cta-btn {
    display: block;
    width: 100%;
    max-width: 640px;
    margin: 20px auto 0;
    padding: 14px;
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: opacity 0.2s, transform 0.2s;
}
.realtime-cta-btn:hover {
    opacity: 0.9;
    transform: translateY(-1px);
}

/* フレッシュアイテムのインジケータ */
.ranking-item[data-fresh="1"] .rank-number {
    background: #e74c3c;
}

/* ランキング24h モバイル対応 */
@media (max-width: 768px) {
    .ranking-vertical { padding: 8px; gap: 10px; }
    /* SP: 全てフルサイズ1列（JSがisPortrait=falseで描画） */
    .ranking-vertical-item .vertical-rank-poster img { max-height: none; }
}

/* ===== レスポンシブ ===== */
@media (max-width: 480px) {
    .url-input-area { flex-direction: column; }
    .url-input-area button { width: 100%; }
    .ranking-list { gap: 8px; }
    .rank-number { font-size: 12px; padding: 1px 5px; }
    .rank-info { padding: 6px 8px; }
    .rank-play-overlay svg { width: 36px; height: 36px; }
    .header-inner { flex-direction: column; align-items: flex-start; }
    .admin-overlay { gap: 3px; padding: 3px 4px; }
    .admin-tier { font-size: 9px; }
    .admin-boost-btn { padding: 2px 5px; font-size: 11px; }
}
