/* ==========================================================================
   Ranking Table Maker — Frontend Styles (Modernized)
   ==========================================================================
   Unified with the Gadget Finder design system (--gf-* tokens).
   CSS-only redesign — no HTML/JS changes required.
   @since 8.0.0
   ========================================================================== */

/* --------------------------------------------------------------------------
   § 1. Design Tokens (inherits from gf-finder-shared.css when available)
   -------------------------------------------------------------------------- */
.ranking-table-maker-root {
    --rtm-max-width: 1000px;
    --rtm-font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;

    /* Semantic colors — reference gf-* tokens with fallbacks */
    --rtm-text: var(--gf-gray-900, var(--mg-gray-900));
    --rtm-text-muted: var(--gf-gray-500, var(--mg-gray-500));
    --rtm-bg: var(--gf-white, var(--mg-white));
    --rtm-bg-alt: var(--gf-gray-50, var(--mg-gray-50));
    --rtm-border: var(--gf-gray-200, var(--mg-gray-200));
    --rtm-border-light: var(--gf-gray-100, var(--mg-gray-100));
    --rtm-primary-50: var(--gf-primary-50, var(--mg-cyan-50));
    --rtm-error: var(--gf-error, var(--mg-red-400));
    --rtm-warning: var(--gf-warning, var(--mg-yellow-400));

    /* Rank badge gradients */
    --rtm-gold: var(--gf-gold, linear-gradient(135deg, var(--mg-yellow-400), var(--mg-yellow-400)));
    --rtm-silver: var(--gf-silver, linear-gradient(135deg, var(--mg-gray-300), var(--mg-gray-400)));
    --rtm-bronze: var(--gf-bronze, linear-gradient(135deg, var(--mg-color-warning-border), var(--mg-yellow-500)));

    /* Shop button colors — Japanese affiliate convention (Pochipp/Rinker style) */
    --rtm-amazon: #F99D31;
    --rtm-rakuten: #d0322e; /* 楽天赤。白文字ボタンで #E0423F は 4.18 と AA 僅か未達のため微暗化(白上 ~5.0) */
    --rtm-yahoo: #66A8FF;

    /* Shadows */
    --rtm-shadow-sm: var(--gf-shadow-sm, 0 1px 3px rgba(15, 23, 42, 0.06));
    --rtm-shadow-md: var(--gf-shadow-md, 0 4px 16px rgba(15, 23, 42, 0.08));

    /* Radii */
    --rtm-radius-sm: var(--gf-radius-sm, 6px);
    --rtm-radius-md: var(--gf-radius-md, 10px);
    --rtm-radius-full: var(--gf-radius-full, 50rem);

    /* Transition */
    --rtm-transition: var(--gf-transition, 0.2s cubic-bezier(0.4, 0, 0.2, 1));
}


/* --------------------------------------------------------------------------
   § 2. Root Container
   -------------------------------------------------------------------------- */
.ranking-table-maker-root {
    position: relative;
    font-family: var(--rtm-font);
    margin: 2em auto;
    max-width: var(--rtm-max-width);
}


/* --------------------------------------------------------------------------
   § 3. Loading & Error States
   -------------------------------------------------------------------------- */
.cw-loading,
.cw-error {
    text-align: center;
    padding: 4rem 1rem;
    font-size: 1rem;
    color: var(--rtm-text-muted);
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    background: var(--rtm-bg-alt);
    border-radius: var(--rtm-radius-md);
    border: 1px dashed var(--rtm-border);
    font-weight: 500;
}

.cw-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--rtm-border);
    border-top: 3px solid var(--gf-primary, var(--mg-color-primary-hover));
    border-radius: var(--mg-radius-full);
    animation: rtm-spin 0.8s linear infinite;
    margin-bottom: 1.25rem;
}

@keyframes rtm-spin {
    to {
        transform: rotate(360deg);
    }
}


/* --------------------------------------------------------------------------
   § 4. Table Container (Desktop)
   -------------------------------------------------------------------------- */
#cw-list {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--rtm-border);
    border-radius: var(--rtm-radius-md);
    overflow: hidden;
    background: var(--rtm-bg);
    box-shadow: var(--rtm-shadow-sm);
}


/* --------------------------------------------------------------------------
   § 5. Table Header & Rows
   -------------------------------------------------------------------------- */
.cw-table-header,
.cw-table-row {
    display: flex;
    align-items: stretch;
    border-bottom: 1px solid var(--rtm-border-light);
}

.cw-table-row:last-child {
    border-bottom: none;
}

.cw-table-row {
    transition: background var(--rtm-transition);
}

.cw-table-row:hover {
    background: var(--rtm-primary-50);
}

.cw-table-header {
    background: var(--rtm-bg-alt);
    color: var(--rtm-text-muted);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

.cw-table-cell {
    padding: 0.85em 0.75em;
    display: flex;
    align-items: center;
    font-size: 0.875rem;
    color: var(--rtm-text);
    line-height: 1.5;
}


/* --------------------------------------------------------------------------
   § 6. Rank Badge
   -------------------------------------------------------------------------- */
.cw-cell-rank {
    flex: 0 0 65px;
    justify-content: center;
    font-weight: 700;
    font-size: 1rem;
}

.cw-cell-rank span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--mg-radius-full);
    background: var(--gf-gray-100, var(--mg-gray-100));
    color: var(--gf-gray-600, var(--mg-gray-600));
    font-size: 0.85rem;
    font-weight: 700;
}

.cw-rank-1 span {
    background: var(--rtm-gold);
    color: var(--mg-yellow-500);
    box-shadow: var(--mg-shadow-md);
}

.cw-rank-2 span {
    background: var(--rtm-silver);
    color: var(--mg-gray-700);
}

.cw-rank-3 span {
    background: var(--rtm-bronze);
    color: var(--rtm-bg);
}


/* --------------------------------------------------------------------------
   § 7. Image Cell
   -------------------------------------------------------------------------- */
.cw-cell-image {
    flex: 0 0 90px;
    justify-content: center;
}

.cw-cell-image img {
    width: 70px;
    height: 70px;
    object-fit: contain;
    border-radius: var(--rtm-radius-sm);
}


/* --------------------------------------------------------------------------
   § 8. Title, Price, Rating Cells
   -------------------------------------------------------------------------- */
.cw-cell-title {
    flex: 1 1 32%;
    min-width: 200px;
    font-weight: 500;
    color: var(--rtm-text);
}

.cw-title-multiline {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
}

.cw-cell-price {
    flex: 0 0 140px;
    justify-content: center;
    font-weight: 700;
    color: var(--rtm-error);
    flex-direction: column;
    align-items: flex-end;
    gap: 2px;
}

.cw-cell-rating {
    flex: 0 0 95px;
    justify-content: center;
    font-weight: 600;
    color: var(--rtm-warning);
}


/* --------------------------------------------------------------------------
   § 9. Buy Buttons (Amazon / Rakuten / Yahoo)
   -------------------------------------------------------------------------- */
.cw-cell-buy {
    flex: 0 0 125px;
    display: flex;
    flex-direction: column;
    gap: 0.45em;
    align-items: stretch;
    justify-content: center;
}

.cw-buy-btn {
    padding: 0.5em 0.5em;
    border-radius: var(--rtm-radius-sm);
    text-decoration: none;
    color: var(--rtm-bg) !important;
    font-size: 0.78rem;
    text-align: center;
    display: block;
    font-weight: 600;
    transition: opacity var(--rtm-transition), transform var(--rtm-transition);
}

.cw-buy-btn:visited {
    color: var(--rtm-bg) !important;
}

.cw-buy-btn:hover,
.cw-buy-btn:focus-visible {
    color: var(--rtm-bg) !important;
    opacity: 0.9;
    transform: translateY(-1px);
}

.cw-buy-btn:focus-visible {
    outline: 2px solid var(--rtm-text);
    outline-offset: 2px;
}

.cw-buy-btn.az {
    background: var(--rtm-amazon);
}

.cw-buy-btn.rk {
    background: var(--rtm-rakuten);
}

.cw-buy-btn.yh {
    background: var(--rtm-yahoo);
}

/* Amazon 橙 / Yahoo 明青 は白文字だと低コントラスト (2.1-2.4) のため濃文字に (全リンク状態を上書き) */
.cw-buy-btn.az, .cw-buy-btn.az:visited, .cw-buy-btn.az:hover, .cw-buy-btn.az:focus-visible,
.cw-buy-btn.yh, .cw-buy-btn.yh:visited, .cw-buy-btn.yh:hover, .cw-buy-btn.yh:focus-visible {
    color: #1a1a1a !important;
}


/* --------------------------------------------------------------------------
   § 10. Timestamp & Disclaimer
   -------------------------------------------------------------------------- */
.cw-timestamp {
    display: inline;
    font-size: 0.8em;
    color: var(--rtm-text-muted);
    font-weight: 400;
    line-height: 1.3;
    white-space: nowrap;
    margin-inline-start: 0.5em;
}

.rtm-disclaimer {
    font-size: 0.85em;
    line-height: 1.6;
    color: var(--rtm-text-muted);
    margin-top: 1.5rem;
    padding: 1rem;
    background: var(--rtm-bg-alt);
    border: 1px solid var(--rtm-border-light);
    border-radius: var(--rtm-radius-sm);
    white-space: pre-wrap;
}


/* --------------------------------------------------------------------------
   § 11. Mobile Card Layout (< 800px)
   -------------------------------------------------------------------------- */
@media (max-width: 782px) {
    #cw-list {
        min-width: 0;
        border: none;
        background: transparent;
        gap: 0.75rem;
        box-shadow: none;
    }

    .cw-table-header {
        display: none;
    }

    .cw-table-row {
        position: relative;
        display: grid;
        grid-template-columns: 90px 1fr;
        grid-template-rows: auto auto auto auto;
        gap: 0 0.75rem;
        border: 1px solid var(--rtm-border);
        background: var(--rtm-bg);
        border-radius: var(--rtm-radius-md);
        padding: 1rem;
        box-shadow: var(--rtm-shadow-sm);
        transition: transform var(--rtm-transition), box-shadow var(--rtm-transition);
    }

    .cw-table-row:hover {
        transform: translateY(-2px);
        box-shadow: var(--rtm-shadow-md);
        background: var(--rtm-bg);
    }

    .cw-table-cell {
        padding: 0;
        border-bottom: none;
        height: auto;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
    }

    .cw-cell-rank {
        position: absolute;
        top: 0;
        left: 0;
    }

    .cw-cell-rank span {
        width: 32px;
        height: 32px;
        border-top-left-radius: var(--rtm-radius-md);
        border-bottom-right-radius: var(--rtm-radius-md);
        border-top-right-radius: 0;
        border-bottom-left-radius: 0;
        font-size: 0.8rem;
    }

    .cw-cell-image {
        grid-column: 1 / 2;
        grid-row: 1 / -1;
        align-self: center;
    }

    .cw-cell-image img {
        width: 70px;
        height: 70px;
    }

    .cw-cell-title {
        grid-column: 2 / 3;
        grid-row: 1 / 2;
        font-weight: 600;
        line-height: 1.5;
        align-items: flex-start;
    }

    .cw-cell-price {
        grid-column: 2 / 3;
        grid-row: 2 / 3;
        font-weight: 700;
        color: var(--rtm-error);
        font-size: 1rem;
        align-items: flex-start;
        margin-top: 0.5rem;
    }

    .cw-cell-rating {
        grid-column: 2 / 3;
        grid-row: 3 / 4;
        justify-content: flex-start;
        margin-top: 0.25rem;
    }

    .cw-cell-buy {
        grid-column: 2 / 3;
        grid-row: 4 / 5;
        margin-top: auto;
        padding-top: 0.5rem;
        width: 100%;
        flex-direction: row;
        gap: 0.5rem;
    }

    .cw-buy-btn {
        flex-grow: 1;
    }
}


/* --------------------------------------------------------------------------
   § 12. PA-API Data Display (Sale / Savings / Points / Direct)
   -------------------------------------------------------------------------- */

/* Image wrapper for badge positioning */
.cw-image-wrapper {
    position: relative;
}

/* Sale badge */
.cw-deal-badge {
    position: absolute;
    top: 4px;
    left: 4px;
    background: linear-gradient(135deg, #ff6b35, var(--rtm-error));
    color: var(--rtm-bg);
    padding: 2px 6px;
    border-radius: var(--mg-radius-sm);
    font-size: 0.625rem;
    font-weight: 700;
    z-index: var(--mg-z-base);
    white-space: nowrap;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.cw-deal-badge.cw-lightning {
    background: linear-gradient(135deg, var(--mg-color-amazon-orange), var(--mg-color-warning));
    animation: rtm-pulse 2s ease-in-out infinite;
}

@keyframes rtm-pulse {

    0%,
    100% {
        opacity: 1;
        transform: scale(1);
    }

    50% {
        opacity: 0.9;
        transform: scale(1.02);
    }
}

/* Savings badge */
.cw-savings-badge {
    display: inline-block;
    background: var(--rtm-error);
    color: var(--rtm-bg);
    padding: 2px 6px;
    border-radius: var(--mg-radius-sm);
    font-size: 0.6875rem;
    font-weight: 700;
    margin-inline-start: 4px;
}

/* Points badge */
.cw-points-badge {
    display: block;
    color: var(--rtm-warning);
    font-size: 0.6875rem;
    font-weight: 500;
}

/* Amazon direct badge */
.cw-amazon-direct {
    display: block;
    color: var(--mg-color-sky);
    font-size: 0.625rem;
    font-weight: 700;
    margin-top: 2px;
}


/* --------------------------------------------------------------------------
   § 13. Mobile PA-API Adjustments
   -------------------------------------------------------------------------- */
@media (max-width: 782px) {
    .cw-deal-badge {
        /* Position top-right to avoid overlap with rank badge (top-left) */
        top: 2px;
        left: auto;
        right: 2px;
        font-size: 0.5625rem;
        padding: 1px 4px;
        z-index: var(--mg-z-dropdown);
    }

    .cw-savings-badge {
        font-size: 0.625rem;
        padding: 1px 4px;
    }

    .cw-points-badge,
    .cw-amazon-direct {
        font-size: 0.5625rem;
    }
}


/* --------------------------------------------------------------------------
   § 14. Accessibility: Reduced Motion
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {

    .cw-spinner {
        animation: none;
    }

    .cw-deal-badge.cw-lightning {
        animation: none;
    }

    .cw-buy-btn,
    .cw-table-row {
        transition: none;
    }
}


/* --------------------------------------------------------------------------
   § 15. Display Mode Modifiers (v4.0 — ショートコード属性制御)
   --------------------------------------------------------------------------
   PHP 側の DisplayConfig::cssModifiers() が以下のクラスを付与:
   .rtm-no-badge, .rtm-no-price, .rtm-no-rating, .rtm-no-image,
   .rtm-no-title, .rtm-no-buttons
   -------------------------------------------------------------------------- */

/* フォールバック非表示（JSが条件レンダリングするが CSS でも保証） */
.rtm-no-badge .cw-cell-rank { display: none; }
.rtm-no-price .cw-cell-price { display: none; }
.rtm-no-rating .cw-cell-rating { display: none; }
.rtm-no-image .cw-cell-image { display: none; }
.rtm-no-title .cw-cell-title { display: none; }
.rtm-no-buttons .cw-cell-buy { display: none; }

/* バッジなし時: タイトル列を広く */
.rtm-no-badge .cw-cell-title {
    flex: 1 1 40%;
}

/* ボタンなし時: 価格列を右寄せ */
.rtm-no-buttons .cw-cell-price {
    flex: 0 0 auto;
    margin-inline-start: auto;
}


/* --------------------------------------------------------------------------
   § 16. Table Title
   -------------------------------------------------------------------------- */
.rtm-table-title {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--rtm-text);
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--gf-primary, var(--mg-color-primary));
    display: flex;
    align-items: center;
    gap: 0.5em;
}

.rtm-table-title::before {
    content: '';
    display: inline-block;
    width: 4px;
    height: 1.2em;
    background: var(--gf-primary, var(--mg-color-primary));
    border-radius: 2px;
    flex-shrink: 0;
}


/* --------------------------------------------------------------------------
   § 17. Multi-Column Grid Layout
   -------------------------------------------------------------------------- */
.rtm-columns-2 #cw-list,
.rtm-columns-3 #cw-list,
.rtm-columns-4 #cw-list {
    display: grid;
    gap: 0.75rem;
    border: none;
    background: transparent;
    box-shadow: none;
}

.rtm-columns-2 #cw-list { grid-template-columns: repeat(2, 1fr); }
.rtm-columns-3 #cw-list { grid-template-columns: repeat(3, 1fr); }
.rtm-columns-4 #cw-list { grid-template-columns: repeat(4, 1fr); }

.rtm-columns-2 .cw-table-header,
.rtm-columns-3 .cw-table-header,
.rtm-columns-4 .cw-table-header {
    display: none;
}

.rtm-columns-2 .cw-table-row,
.rtm-columns-3 .cw-table-row,
.rtm-columns-4 .cw-table-row {
    display: flex;
    flex-direction: column;
    border: 1px solid var(--rtm-border);
    border-radius: var(--rtm-radius-md);
    padding: 1rem;
    background: var(--rtm-bg);
    box-shadow: var(--rtm-shadow-sm);
    transition: transform var(--rtm-transition), box-shadow var(--rtm-transition);
}

.rtm-columns-2 .cw-table-row:hover,
.rtm-columns-3 .cw-table-row:hover,
.rtm-columns-4 .cw-table-row:hover {
    transform: translateY(-2px);
    box-shadow: var(--rtm-shadow-md);
}

.rtm-columns-2 .cw-table-cell,
.rtm-columns-3 .cw-table-cell,
.rtm-columns-4 .cw-table-cell {
    padding: 0.35em 0;
    flex: none;
    width: 100%;
    min-width: 0;
}

.rtm-columns-2 .cw-cell-image,
.rtm-columns-3 .cw-cell-image,
.rtm-columns-4 .cw-cell-image {
    justify-content: center;
    margin-bottom: 0.5rem;
}

.rtm-columns-2 .cw-cell-image img,
.rtm-columns-3 .cw-cell-image img,
.rtm-columns-4 .cw-cell-image img {
    width: 100%;
    height: auto;
    max-height: 160px;
    object-fit: contain;
}

.rtm-columns-2 .cw-cell-buy,
.rtm-columns-3 .cw-cell-buy,
.rtm-columns-4 .cw-cell-buy {
    flex-direction: row;
    gap: 0.4em;
    margin-top: 0.5rem;
}

.rtm-columns-2 .cw-buy-btn,
.rtm-columns-3 .cw-buy-btn,
.rtm-columns-4 .cw-buy-btn {
    flex: 1;
    font-size: 0.72rem;
}

/* グリッド: モバイルフォールバック */
@media (max-width: 600px) {
    .rtm-columns-3 #cw-list,
    .rtm-columns-4 #cw-list {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 400px) {
    .rtm-columns-2 #cw-list,
    .rtm-columns-3 #cw-list,
    .rtm-columns-4 #cw-list {
        grid-template-columns: 1fr;
    }
}


/* --------------------------------------------------------------------------
   § 18. Image-Only Gallery Mode
   -------------------------------------------------------------------------- */
.rtm-mode-image_only .cw-table-row {
    align-items: center;
    text-align: center;
}

.rtm-mode-image_only .cw-cell-title {
    font-size: 0.8rem;
    text-align: center;
    justify-content: center;
}

.rtm-mode-image_only .cw-cell-image img {
    border-radius: var(--rtm-radius-md);
    box-shadow: var(--rtm-shadow-sm);
    transition: transform var(--rtm-transition);
}

.rtm-mode-image_only .cw-cell-image img:hover {
    transform: scale(1.05);
}


/* --------------------------------------------------------------------------
   § 19. Compare Mode
   -------------------------------------------------------------------------- */
.rtm-mode-compare .cw-table-row:nth-child(even) {
    background: var(--rtm-bg-alt);
}

.rtm-mode-compare .cw-cell-price {
    font-size: 1rem;
}


/* --------------------------------------------------------------------------
   § 20. Minimal Mode
   -------------------------------------------------------------------------- */
.rtm-mode-minimal .cw-table-row {
    border-bottom: 1px solid var(--rtm-border-light);
    padding: 0.5rem 0;
}

.rtm-mode-minimal .cw-table-row:last-child {
    border-bottom: none;
}