<?php if(isset($is_demo) && $is_demo): ?>
    <div style="background:#fff3cd; padding:12px 15px; color:#856404; margin-bottom:20px; border-radius:6px; border-left: 4px solid #ffeeba; font-size: 14px; line-height: 1.6;">
        <span class="dashicons dashicons-info" style="vertical-align: middle;"></span>
        <strong><?php echo $this->t('Sandbox 演示模式说明：'); ?></strong><br>
        <?php echo $this->t('当前为沙盒测试环境。下方展示的排盘与解析均为系统预设的固定模拟数据，与您填写的测算信息完全无关，仅供预览界面排版效果。'); ?>
    </div>
<?php endif; ?>

<?php
// 安全拦截
if (empty($data) || !is_array($data)) {
    echo '<div style="color:red; text-align:center; padding: 20px;">' . $this->t('求签失败，暂无数据。') . '</div>';
    return;
}

// 提取数据[cite: 13]
$base = $data['list'] ?? $data['data'] ?? $data;
$zhuge = $base['zhuge'] ?? [];
$content = $zhuge['content'] ?? [];

// 诸葛专属小头像路径
$current_lang = get_option('yfj_language', 'zh-cn');
$lang_suffix = ($current_lang === 'zh-tw') ? 'zh-tw' : 'zh-cn';
$image_url = untrailingslashit(YFJ_PLUGIN_URL) . '/assets/image/lingqian/' . $lang_suffix . '/zhuge.jpg';
?>

<style>
    .yfj-lq-wrapper { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; color: #334155; }
    .yfj-lq-wrapper * { box-sizing: border-box; }
    .yfj-panel { background: #fff; border: 1px solid #e2e8f0; border-radius: 8px; margin-bottom: 24px; overflow: hidden; }
    .yfj-panel-heading { background: #f8fafc; padding: 14px 20px; border-bottom: 1px solid #e2e8f0; font-weight: bold; color: #0f172a; font-size: 16px; }
    .yfj-panel-body { padding: 20px; font-size: 14.5px; line-height: 1.8; position: relative; }

    /* 方块宫格布局 */
    .yfj-block-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
    .yfj-block-item { background: #fff; padding: 15px; border-radius: 8px; border: 1px solid #e2e8f0; box-shadow: 0 2px 4px rgba(0,0,0,0.02); display: flex; flex-direction: column; transition: transform 0.2s; }
    .yfj-block-item:hover { transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.05); }
    .yfj-block-item.is-last-odd { grid-column: 1 / -1; align-items: center; text-align: center; }
    .yfj-block-title { font-weight: bold; color: #ea580c; display: inline-block; margin-bottom: 8px; border-bottom: 1px dashed #fed7aa; padding-bottom: 6px; font-size: 15px; }
    .yfj-block-content { color: #334155; font-size: 14px; line-height: 1.6; }

    /* 诸葛灵签移动端专属适配 */
    @media (max-width: 600px) {
        /* 1. 缩小面板整体内边距，释放横向可见空间 */
        .yfj-panel-body { padding: 15px; }

        /* 2. 缩小诸葛亮头像图片宽度，防止左侧核心文字被挤压成窄面条 */
        .yfj-panel-body > div[style*="float: right"] { width: 85px !important; margin: 0 0 10px 10px !important; }

        /* 3. 核心：在手机端将原本的双列十宫格布局强制拉回单列向下排布，杜绝溢出和挤压 */
        .yfj-block-grid { grid-template-columns: 1fr; gap: 10px; }

        /* 4. 调整手机端单列卡片内的对齐方向和留白 */
        .yfj-block-item { padding: 12px; }
        .yfj-block-item.is-last-odd { align-items: flex-start; text-align: left; }
        .yfj-block-item.is-last-odd > div { text-align: left !important; }
    }
</style>

<div class="yfj-lq-wrapper">

    <!-- 1. 基本信息 -->
    <div class="yfj-panel">
        <div class="yfj-panel-heading"><span class="dashicons dashicons-visibility"></span> <?php echo $this->t('抽签结果'); ?></div>
        <div class="yfj-panel-body">

            <!-- 右侧诸葛小画像 -->
            <div style="float: right; margin: 0 0 15px 15px; border-radius: 6px; border: 1px solid #e2e8f0; overflow: hidden; width: 110px; box-shadow: 0 4px 6px rgba(0,0,0,0.05);">
                <img src="<?php echo esc_url($image_url); ?>" style="width: 100%; display: block; border-bottom: 3px solid #f97316;">
            </div>

            <p style="margin: 0 0 10px 0; font-size: 15px;">
                <strong><?php echo $this->t('您抽出诸葛灵签第：'); ?></strong>
                <span style="color: #dc2626; font-size: 18px; font-weight: bold;"><?php echo esc_html($base['id'] ?? ''); ?></span>
                <?php echo $this->t('签'); ?>
            </p>

            <p style="margin: 0 0 10px 0; font-size: 15px;">
                <strong><?php echo $this->t('签曰：'); ?></strong>
                <span style="color: #dc2626; font-weight: bold;">
                    <?php echo esc_html(!empty($zhuge['title']) ? $zhuge['title'] : $this->t('无')); ?>
                </span>
            </p>

            <div style="clear: both;"></div>

            <!-- 注解（原名解释）作为长文本单独显示[cite: 13] -->
            <?php if (!empty($content['解释'])): ?>
                <div style="background: #fff7ed; padding: 18px; border-radius: 6px; border: 1px solid #fed7aa; margin-top: 15px;">
                    <p style="margin: 0;">
                        <strong style="color: #0f172a;"><?php echo $this->t('注解：'); ?></strong>
                        <span style="color: #334155; line-height: 1.7;"><?php echo esc_html($content['解释']); ?></span>
                    </p>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- 2. 解签详细信息 (完美十宫格展示)[cite: 13] -->
    <div class="yfj-panel">
        <div class="yfj-panel-heading"><span class="dashicons dashicons-book-alt"></span> <?php echo $this->t('解签信息'); ?></div>
        <div class="yfj-panel-body" style="background: #f8fafc;">
            <div class="yfj-block-grid">
                <?php
                // 诸葛灵签专属的具体预测字段[cite: 13]
                $detail_keys = ['功名', '行人', '婚姻', '官司', '丁口', '生意', '疾病', '出行', '失物', '田畜'];

                $valid_items = [];
                foreach ($detail_keys as $k) {
                    if (!empty($content[$k])) {
                        $valid_items[$k] = $content[$k];
                    }
                }

                $total = count($valid_items);
                $curr = 0;

                foreach ($valid_items as $label => $val):
                    $curr++;
                    $is_last_odd = ($total % 2 !== 0 && $curr === $total);
                    ?>
                    <div class="yfj-block-item <?php echo $is_last_odd ? 'is-last-odd' : ''; ?>">
                        <div style="<?php echo $is_last_odd ? '' : 'text-align: center;'; ?>">
                            <!-- UI 标题必须翻译 -->
                            <span class="yfj-block-title"><?php echo esc_html(sprintf($this->t('【%s】'), $this->t($label))); ?></span>
                        </div>
                        <!-- 接口返回断语直接输出 -->
                        <span class="yfj-block-content"><?php echo esc_html($val); ?></span>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <!-- 免责声明与按钮 -->
    <?php echo $this->get_disclaimer_html(); ?>

    <div style="text-align: center; margin-top: 10px;">
        <button type="button"
                onclick="this.disabled=true; this.style.opacity='0.6'; this.innerText='<?php echo $this->t('正在重置...'); ?>'; window.location.reload();"
                style="background: #e2e8f0; color: #334155; border: none; padding: 12px 30px; border-radius: 50px; font-size: 15px; font-weight: bold; cursor: pointer; transition: all 0.2s;">
            <?php echo $this->t('返回重求'); ?>
        </button>
    </div>
</div>