<?php
if ( ! defined( 'ABSPATH' ) ) exit;

// Allow output only when loaded from newsai_render_sidebar() (sets this flag). Any other include path exits silently.
if ( empty( $GLOBALS['newsai_sidebar_metabox_render'] ) ) {
	return;
}

$geo_opt = function_exists( 'newsai_get_target_geo' ) ? newsai_get_target_geo() : 'US';
$use_circular_gauge = (get_option('newsai_ui_circular_gauge', 'yes') === 'yes');
$show_onboarding_modal = (get_option('newsai_ui_onboarding_modal', 'yes') === 'yes');
$ai_assist_active = function_exists( 'newsai_ai_is_active' ) && newsai_ai_is_active();
$editor_faq_enabled = ( get_option( 'newsai_editor_faq_enabled', 'no' ) === 'yes' );

// Sidebar i18n dictionary — all strings flow through the WordPress gettext stack
// (load_plugin_textdomain newsroom-ai-assistant). Keep the same array shape so the
// downstream PHP echo $i18n[...] usages further down stay untouched.
$i18n = [
    'readiness_title' => __( 'AI Readiness Engine', 'newsroom-ai-assistant' ),
    'readiness_subtitle' => __( 'Pre-publish score for Search, Discover, entities, and AI-friendly structure.', 'newsroom-ai-assistant' ),
    'pillar_search' => __( 'Search', 'newsroom-ai-assistant' ),
    'pillar_discover' => __( 'Discover', 'newsroom-ai-assistant' ),
    'pillar_entity' => __( 'Entity', 'newsroom-ai-assistant' ),
    'pillar_structure' => __( 'Structure', 'newsroom-ai-assistant' ),
    'top_actions_title' => __( 'Top fixes before publish', 'newsroom-ai-assistant' ),
    'tab_structure' => __( 'Structure', 'newsroom-ai-assistant' ),
    'editorial_label' => __( 'Editorial', 'newsroom-ai-assistant' ),
    'discover_label' => __( 'Discover', 'newsroom-ai-assistant' ),
    'discover_score_help' => __( 'Signals that correlate with Google Discover-style surfacing (hero image, trends, entities, author trust, links).', 'newsroom-ai-assistant' ),
    'tab_editorial' => __( 'Editorial', 'newsroom-ai-assistant' ),
    'tab_discover' => __( 'Discover', 'newsroom-ai-assistant' ),
    'focus_mode' => __( 'Focus Mode', 'newsroom-ai-assistant' ),
    'fix_now' => __( 'Fix Now', 'newsroom-ai-assistant' ),
    'open_panel' => __( 'Open Detailed Panel', 'newsroom-ai-assistant' ),
    'active_task' => __( 'Active Event', 'newsroom-ai-assistant' ),
    'overdue_task' => __( 'Overdue Task', 'newsroom-ai-assistant' ),
    'keyword' => __( 'Keyword:', 'newsroom-ai-assistant' ),
    'ref' => __( 'Ref:', 'newsroom-ai-assistant' ),
    'brief' => __( 'Brief:', 'newsroom-ai-assistant' ),
    'mark_done' => __( 'Mark as Done', 'newsroom-ai-assistant' ),
    'readiness' => __( 'Editorial Readiness', 'newsroom-ai-assistant' ),
    'pub_score' => __( 'Publish Confidence', 'newsroom-ai-assistant' ),
    'next_action' => __( '⚡ Next Best Action:', 'newsroom-ai-assistant' ),
    'details' => __( 'Score Details', 'newsroom-ai-assistant' ),
    'quick_actions' => __( 'Quick Actions', 'newsroom-ai-assistant' ),
    'sug_tags' => __( 'Suggested Tags:', 'newsroom-ai-assistant' ),
    'tag_ph' => __( 'Write text for suggestions...', 'newsroom-ai-assistant' ),
    'btn_faq' => __( 'FAQ Block', 'newsroom-ai-assistant' ),
    'btn_kp' => __( 'Key Points', 'newsroom-ai-assistant' ),
    'ins_cluster' => __( 'Insert Story Cluster:', 'newsroom-ai-assistant' ),
    'smart_links' => __( 'Find archive links', 'newsroom-ai-assistant' ),
    'smart_links_hint' => __( 'Scans older published posts that match your title and body for internal links.', 'newsroom-ai-assistant' ),
    'search_ph' => __( 'Search articles...', 'newsroom-ai-assistant' ),
    'find' => __( 'Find', 'newsroom-ai-assistant' ),
    'ins_links' => __( 'Insert Links', 'newsroom-ai-assistant' ),
    'ins_tl' => __( 'Insert Manual Timeline:', 'newsroom-ai-assistant' ),
    'btn_tl' => __( 'Insert Timeline Here', 'newsroom-ai-assistant' ),
    'dis_tl' => __( 'Disable Auto-Timeline', 'newsroom-ai-assistant' ),
    'breaking_title' => __( 'Breaking news mode', 'newsroom-ai-assistant' ),
    'breaking_help' => __( 'LiveBlogPosting schema, faster indexing ping, and optional “developing story” badge until you resolve or auto-expire.', 'newsroom-ai-assistant' ),
    'breaking_enable' => __( 'Enable breaking coverage', 'newsroom-ai-assistant' ),
    'breaking_active' => __( 'Breaking coverage active', 'newsroom-ai-assistant' ),
    'breaking_legacy' => __( 'Legacy live coverage — resolve or re-enable to use auto-expire.', 'newsroom-ai-assistant' ),
    /* translators: %s: approximate hours until breaking coverage auto-expires. */
    'breaking_expires' => __( 'Auto-expires in ~%s hours', 'newsroom-ai-assistant' ),
    'breaking_resolve' => __( 'Resolve now', 'newsroom-ai-assistant' ),
    'breaking_end' => __( 'End coverage', 'newsroom-ai-assistant' ),
    'breaking_guide_title' => __( 'How to write live coverage', 'newsroom-ai-assistant' ),
    'breaking_guide_intro' => __( 'Structured headings become liveBlogUpdate entries in JSON-LD (up to 5).', 'newsroom-ai-assistant' ),
    'breaking_guide_li1' => __( 'Use H3 for each update; put the time in the title (e.g. 14:30 — …).', 'newsroom-ai-assistant' ),
    'breaking_guide_li2' => __( 'One H3 = one schema update. Add a short paragraph under each heading.', 'newsroom-ai-assistant' ),
    'breaking_guide_li3' => __( 'Use H2 only for major section breaks, not every sentence.', 'newsroom-ai-assistant' ),
    'breaking_guide_li4' => __( 'Resolve breaking when the story is no longer developing.', 'newsroom-ai-assistant' ),
    'breaking_insert_live' => __( 'Insert live update (H3)', 'newsroom-ai-assistant' ),
    'breaking_warn_no_headings' => __( 'Breaking is on but there are no H2/H3 headings. Add an update block for valid LiveBlog schema.', 'newsroom-ai-assistant' ),
    /* translators: %d: number of H2/H3 update headings found in post content. */
    'breaking_headings_ok' => __( '%d live update headings in content', 'newsroom-ai-assistant' ),
    'radar' => __( 'Trend Radar', 'newsroom-ai-assistant' ),
    'prompts' => __( 'AI Prompts', 'newsroom-ai-assistant' ),
    'p_title' => __( 'Click-worthy Titles', 'newsroom-ai-assistant' ),
    'p_seo' => __( 'SEO & Meta Data', 'newsroom-ai-assistant' ),
    'p_rewrite' => __( 'Proofread & FAQs', 'newsroom-ai-assistant' ),
    'onb_title' => __( 'Welcome to Newsroom OS', 'newsroom-ai-assistant' ),
    'onb_line_1' => __( 'Editorial and Discover scores show how ready the story is for your newsroom and for Discover-style distribution.', 'newsroom-ai-assistant' ),
    'onb_line_2' => __( 'Use Open Detailed Panel for instant fix suggestions.', 'newsroom-ai-assistant' ),
    'onb_line_3' => __( 'Trend Radar brings live topic ideas directly in the editor.', 'newsroom-ai-assistant' ),
    'onb_btn' => __( "Let's go", 'newsroom-ai-assistant' ),
];

global $post;
$post_id = ( isset( $post ) && $post instanceof WP_Post ) ? (int) $post->ID : 0;
$post_geo = ( $post_id > 0 && function_exists( 'newsai_normalize_geo_code' ) )
    ? newsai_normalize_geo_code( (string) get_post_meta( $post_id, '_newsai_target_geo', true ) )
    : '';
$claim_review_enabled = ( $post_id > 0 && get_post_meta( $post_id, '_newsai_claim_review_enabled', true ) === 'yes' );
$claim_text           = $post_id > 0 ? (string) get_post_meta( $post_id, '_newsai_claim_text', true ) : '';
$claim_rating         = $post_id > 0 ? (string) get_post_meta( $post_id, '_newsai_claim_rating', true ) : '';
$claim_source_url     = $post_id > 0 ? (string) get_post_meta( $post_id, '_newsai_claim_source_url', true ) : '';
if ( $claim_rating === '' ) {
	$claim_rating = 'False';
}
global $wpdb;
$table = $wpdb->prefix . 'newsai_alerts';
$current_user_id = get_current_user_id();

// 🚀 FIX: Transient Cache 5 λεπτών για τα Tasks του χρήστη
$cache_key_tasks = 'nros_tasks_user_v3_' . $current_user_id;
$my_tasks = get_transient($cache_key_tasks);

if (false === $my_tasks) {
    // Chiefs see every active task. Authors see personal assignments plus unassigned pool tasks.
    if (current_user_can('edit_others_posts')) {
        $my_tasks = $wpdb->get_results("SELECT * FROM {$table} WHERE status != 'done' ORDER BY id DESC LIMIT 200");
    } else {
        $my_tasks = $wpdb->get_results($wpdb->prepare(
            "SELECT * FROM {$table} WHERE (author_id = 0 OR author_id = %d) AND status != 'done' ORDER BY id DESC LIMIT 200",
            $current_user_id
        ));
    }
    set_transient($cache_key_tasks, $my_tasks, 5 * MINUTE_IN_SECONDS);
}

$active_tasks = []; 
$now_time = strtotime(gmdate('Y-m-d'));
if ($my_tasks) {
    foreach ($my_tasks as $t) {
        $t_year = ($t->year > 0) ? $t->year : (int)gmdate('Y');
        $t_month = ($t->month > 0) ? $t->month : (int)gmdate('m');
        $task_time = strtotime(sprintf('%04d-%02d-%02d', $t_year, $t_month, $t->day));
        
        if ($t->year == 0 && $t->month > 0) { 
            if (($now_time - $task_time) > (15 * 86400)) { $t_year++; $task_time = strtotime(sprintf('%04d-%02d-%02d', $t_year, $t_month, $t->day)); }
        } elseif ($t->year == 0 && $t->month == 0) { 
            if (($now_time - $task_time) > (5 * 86400)) { $t_month++; if ($t_month > 12) { $t_month = 1; $t_year++; } $task_time = strtotime(sprintf('%04d-%02d-%02d', $t_year, $t_month, $t->day)); }
        }
        
        $diff_days = ($task_time - $now_time) / 86400;
        if ($diff_days <= $t->lead_days) { 
            $t->is_overdue = ($diff_days < 0);
            $active_tasks[] = $t; 
        }
    }
}

// 🚀 ENTERPRISE FIX: Πραγματικό transient cache για να μην τρέχει βαριά query σε κάθε φόρτωση
$tag_pool_limit = function_exists( 'newsai_tag_pool_editor_size_get' ) ? newsai_tag_pool_editor_size_get() : 300;
$top_tags_key   = 'nros_top_tags_cache_editor_v1_' . $tag_pool_limit;
$cached_tags    = get_transient( $top_tags_key );
if (false === $cached_tags) {
    $all_tags = get_tags([
        'hide_empty' => false,
        'orderby'    => 'count',
        'order'      => 'DESC',
        'number'     => $tag_pool_limit,
    ]);
    $cached_tags = array_map(function($t) {
        return [
            'id' => $t->term_id,
            'name' => $t->name,
            'slug' => $t->slug,
            'count' => (int)$t->count
        ];
    }, $all_tags);
    set_transient( $top_tags_key, $cached_tags, HOUR_IN_SECONDS );
}

wp_nonce_field('newsai_sidebar_save', 'newsai_sidebar_nonce');
?>

<script>
    var nros_site_tags = <?php echo wp_json_encode($cached_tags, JSON_UNESCAPED_UNICODE); ?>;
</script>
<style>
    .nros-sb-wrapper { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; padding: 5px; box-sizing: border-box; max-width: 100%; color: var(--nros-text, #1d2327); }
    .nros-sb-card { background: var(--nros-surface, #fff); border: 1px solid var(--nros-border, #e2e4e7); border-radius: 8px; padding: 12px; margin-bottom: 15px; box-shadow: var(--nros-shadow-md, 0 1px 3px rgba(0,0,0,0.04)); box-sizing: border-box; max-width: 100%; overflow-wrap: anywhere; word-wrap: break-word; }
    .nros-sb-title { margin: 0 0 12px 0; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; color: var(--nros-text, #1d2327); }
    .nros-sb-ul { margin: 0; padding: 0; list-style: none; font-size: 12px; color: var(--nros-text-muted, #50575e); }
    .nros-sb-ul li { margin-bottom: 8px; display: flex; align-items: flex-start; gap: 8px; }
    .nros-btn-outline { background: var(--nros-surface, #fff); border: 1px solid var(--nros-accent, #2271b1); color: var(--nros-accent, #2271b1); border-radius: 4px; padding: 6px 12px; font-size: 12px; cursor: pointer; text-align: center; font-weight: 500; transition: all 0.2s; }
    .nros-btn-outline:hover { background: var(--nros-accent-soft, #f0f6fc); }
    .nros-pill-btn { display: inline-block; padding: 4px 10px; border-radius: 12px; background: var(--nros-accent-soft, #f0f6fc); color: var(--nros-accent, #2271b1); border: 1px solid var(--nros-accent-border, #cce0f0); font-size: 11px; cursor: pointer; margin: 0 4px 6px 0; transition: all 0.2s; }
    .nros-pill-btn:hover { background: var(--nros-accent, #2271b1); color: #fff; }
    .nros-icon-wrapper { display: inline-flex; align-items: center; justify-content: center; width: 22px; text-align: center; flex-shrink: 0; }
    .nros-accordion { border:1px solid var(--nros-border, #e2e4e7); border-radius:8px; margin-bottom:12px; background:var(--nros-surface, #fff); overflow:hidden; }
    .nros-accordion summary { cursor:pointer; list-style:none; padding:12px 14px; font-size:13px; font-weight:600; color:var(--nros-text, #1d2327); border-bottom:1px solid transparent; }
    .nros-accordion[open] summary { border-bottom-color:var(--nros-border-subtle, #f0f0f1); }
    .nros-readiness-hero { text-align: center; margin-bottom: 12px; }
    .nros-readiness-hero .nros-gauge-wrap { width: 104px; height: 104px; margin: 0 auto; }
    .nros-readiness-hero .nros-gauge-donut { width: 104px; height: 104px; }
    .nros-readiness-hero .nros-gauge-donut::after { width: 74px; height: 74px; }
    .nros-readiness-hero .nros-gauge-center strong { font-size: 18px; }
    .nros-readiness-pillars { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
    .nros-readiness-pillar { display: grid; grid-template-columns: 62px 1fr 34px; gap: 8px; align-items: center; font-size: 11px; }
    .nros-readiness-pillar__label { font-weight: 600; color: var(--nros-text-muted, #50575e); }
    .nros-readiness-pillar__track { background: var(--nros-border-subtle, #f0f0f1); border-radius: 999px; height: 7px; overflow: hidden; }
    .nros-readiness-pillar__fill { height: 100%; width: 0%; background: var(--nros-accent, #2271b1); border-radius: 999px; transition: width 0.3s ease, background 0.3s ease; }
    .nros-readiness-pillar__pct { font-weight: 700; text-align: right; color: var(--nros-text, #1d2327); }
    .nros-readiness-top3 { margin: 0 0 12px 0; padding: 0; list-style: none; font-size: 12px; }
    .nros-readiness-top3 li { display: flex; gap: 8px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--nros-border-subtle, #f0f0f1); line-height: 1.4; color: var(--nros-text-muted, #50575e); }
    .nros-readiness-top3 li:last-child { border-bottom: 0; }
    .nros-readiness-top3__badge { flex-shrink: 0; font-size: 9px; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase; padding: 2px 5px; border-radius: 4px; color: #fff; background: #646970; margin-top: 2px; }
    .nros-readiness-top3__badge--high { background: #dc2626; }
    .nros-readiness-top3__badge--medium { background: #d97706; }
    .nros-readiness-gauges--legacy { display: none; }
    .nros-readiness-gauges .nros-gauge-col { text-align: center; }
    .nros-readiness-gauges .nros-gauge-col-label { font-size: 10px; font-weight: 600; color: var(--nros-text-subtle, #646970); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 4px; }
    .nros-readiness-gauges .nros-gauge-wrap { width: 88px; height: 88px; }
    .nros-readiness-gauges .nros-gauge-donut { width: 88px; height: 88px; }
    .nros-readiness-gauges .nros-gauge-donut::after { width: 62px; height: 62px; }
    .nros-readiness-gauges .nros-gauge-center strong { font-size: 15px; color: var(--nros-text, #1d2327); }
    .nros-readiness-tabrow { display: flex; gap: 6px; margin-bottom: 8px; }
    .nros-readiness-tab { flex: 1; border: 1px solid var(--nros-border, #dcdcde); background: var(--nros-bg-muted, #f6f7f7); color: var(--nros-text-muted, #50575e); border-radius: 4px; padding: 6px 6px; font-size: 11px; font-weight: 600; cursor: pointer; line-height: 1.25; white-space: normal; min-height: 36px; display: flex; align-items: center; justify-content: center; text-align: center; }
    .nros-readiness-tab.nros-readiness-tab--active { background: var(--nros-accent, #2271b1); color: #fff; border-color: var(--nros-accent, #2271b1); }
    #nros-readiness-pane-discover .nros-sb-ul { margin-top: 8px; padding-top: 12px; border-top: 1px dashed var(--nros-border, #e2e4e7); }
    #nros-discover-checklist > li { margin: 0 0 0 0; padding: 10px 0; border-bottom: 1px solid var(--nros-border-subtle, #f0f0f1); align-items: flex-start; }
    #nros-discover-checklist > li:first-child { padding-top: 4px; }
    #nros-discover-checklist > li:last-child { border-bottom: 0; padding-bottom: 4px; }
    #nros-discover-checklist .nros-dc-ico { flex-shrink: 0; margin-top: 1px; }
    #nros-discover-checklist .nros-dc-body { flex: 1; min-width: 0; }
    #nros-discover-checklist .nros-dc-main { font-size: 12px; line-height: 1.45; overflow-wrap: anywhere; word-wrap: break-word; color: var(--nros-text, #1d2327); }
    #nros-discover-checklist .nros-dc-sub { font-size: 11px; line-height: 1.35; color: var(--nros-text-subtle, #787c82); margin-top: 5px; overflow-wrap: anywhere; }
    #nros-readiness-actions { display: flex; flex-direction: column; width: 100%; gap: 8px; box-sizing: border-box; }
    #nros-readiness-actions .button { width: 100%; max-width: 100%; box-sizing: border-box; white-space: normal !important; word-wrap: break-word; overflow-wrap: anywhere; line-height: 1.3 !important; padding: 8px 10px !important; height: auto !important; min-height: 0 !important; text-align: center; justify-content: center; }
    .nros-breaking-panel { font-size:12px; border:1px solid #fecaca; background:#fef2f2; border-radius:8px; padding:10px; margin-bottom:10px; }
    .nros-breaking-panel.is-idle { border-color:var(--nros-border, #e2e4e7); background:var(--nros-bg-muted, #f8f9fa); }
    .nros-breaking-badge-pill { display:inline-block; font-size:10px; font-weight:700; letter-spacing:0.04em; text-transform:uppercase; background:#dc2626; color:#fff; padding:3px 8px; border-radius:4px; margin-bottom:6px; }
    .nros-breaking-guide summary { cursor:pointer; font-weight:600; color:var(--nros-text, #1d2327); }
    .nros-breaking-guide ul { margin:6px 0 0 16px; padding:0; color:var(--nros-text-muted, #50575e); }
    .nros-breaking-guide li { margin-bottom:4px; }
    .nros-breaking-heading-warn { font-size:11px; color:#92400e; background:#fffbeb; border:1px solid #fde68a; border-radius:6px; padding:7px 9px; margin-bottom:6px; }
    .nros-breaking-heading-ok { font-size:11px; color:#166534; background:#ecfdf3; border:1px solid #bbf7d0; border-radius:6px; padding:7px 9px; margin-bottom:6px; }
    .nros-accordion-content { padding:12px; }
    .nros-score-details > summary { list-style: none; }
    .nros-score-details > summary::-webkit-details-marker { display: none; }
    #newsai_sidebar .newsai-tab { color: #50575e !important; border-bottom: 2px solid transparent; padding-bottom: 8px; margin-bottom: -2px; font-size: 12px; font-weight: 600; cursor: pointer; }
    #newsai_sidebar .newsai-tab.active { color: #2271b1 !important; border-bottom-color: #2271b1 !important; }
    .nros-sb-tabrow { display: flex; gap: 4px; margin: 0 0 12px 0; flex-wrap: wrap; }
    .nros-sb-tab { flex: 1; min-width: 0; border: 1px solid var(--nros-border, #dcdcde); background: var(--nros-bg-muted, #f6f7f7); color: var(--nros-text-muted, #50575e); border-radius: 6px; padding: 7px 4px; font-size: 11px; font-weight: 600; cursor: pointer; line-height: 1.2; text-align: center; }
    .nros-sb-tab.nros-sb-tab--active { background: var(--nros-accent, #2271b1); color: #fff; border-color: var(--nros-accent, #2271b1); }
    .nros-sb-pane { display: none; }
    .nros-sb-pane.nros-sb-pane--active { display: block; }
    .nros-radar-panel { overflow: visible !important; min-height: 120px; }
    .nros-post-geo-row { margin: 0 0 10px 0; font-size: 11px; }
    .nros-post-geo-row label { display: block; font-weight: 600; margin-bottom: 4px; color: var(--nros-text-muted, #646970); }
    .nros-post-geo-row select { width: 100%; max-width: 100%; font-size: 11px; }
    @media (prefers-color-scheme: dark) {
        #newsai_sidebar .newsai-tab { color: #94a3b8 !important; }
        #newsai_sidebar .newsai-tab.active { color: #60a5fa !important; border-bottom-color: #60a5fa !important; }
    }
    body.wp-admin.is-dark-theme #newsai_sidebar .newsai-tab { color: #94a3b8 !important; }
    body.wp-admin.is-dark-theme #newsai_sidebar .newsai-tab.active { color: #60a5fa !important; border-bottom-color: #60a5fa !important; }
    @media (prefers-color-scheme: dark) {
        .nros-breaking-heading-warn { color:#fcd34d; background:rgba(146,64,14,.25); border-color:rgba(251,191,36,.35); }
        .nros-breaking-heading-ok { color:#6ee7b7; background:rgba(6,78,59,.3); border-color:rgba(52,211,153,.35); }
        .nros-breaking-panel { background:rgba(127,29,29,.25); border-color:rgba(248,113,113,.35); }
    }
    body.wp-admin.is-dark-theme .nros-breaking-heading-warn { color:#fcd34d; background:rgba(146,64,14,.25); border-color:rgba(251,191,36,.35); }
    body.wp-admin.is-dark-theme .nros-breaking-heading-ok { color:#6ee7b7; background:rgba(6,78,59,.3); border-color:rgba(52,211,153,.35); }
    body.wp-admin.is-dark-theme .nros-breaking-panel { background:rgba(127,29,29,.25); border-color:rgba(248,113,113,.35); }
</style>

<div class="nros-sb-wrapper">
    <div class="nros-sb-card" id="nros-focus-mode-card">
        <h4 class="nros-sb-title">
            <span class="nros-icon-wrapper" style="font-size:16px;">🧠</span>
            <span><?php echo esc_html( $i18n['readiness_title'] ); ?></span>
        </h4>
        <p class="description" style="margin:0 0 10px 0;font-size:11px;color:#646970;"><?php echo esc_html( $i18n['readiness_subtitle'] ); ?></p>

        <div class="nros-readiness-hero" aria-live="polite">
            <?php if ( $use_circular_gauge ) : ?>
            <div class="nros-gauge-wrap">
                <div id="nros-readiness-ring" class="nros-gauge-donut" role="img" aria-label="<?php echo esc_attr( $i18n['readiness_title'] ); ?>"></div>
                <div class="nros-gauge-center">
                    <strong id="nros-readiness-score">—</strong>
                </div>
            </div>
            <?php else : ?>
            <div style="width:100%;">
                <strong id="nros-readiness-score" style="font-size:18px; color:#9ca3af;">—</strong>
                <div style="background:#f0f0f1; border-radius:4px; height:10px; width:100%; overflow:hidden;margin-top:6px;">
                    <div id="nros-readiness-bar" style="background:#9ca3af; width:0%; height:100%; transition:all 0.3s ease;"></div>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <div class="nros-readiness-pillars">
            <div class="nros-readiness-pillar">
                <span class="nros-readiness-pillar__label"><?php echo esc_html( $i18n['pillar_search'] ); ?></span>
                <div class="nros-readiness-pillar__track"><div id="nros-pillar-search-fill" class="nros-readiness-pillar__fill"></div></div>
                <span id="nros-pillar-search-val" class="nros-readiness-pillar__pct">—</span>
            </div>
            <div class="nros-readiness-pillar">
                <span class="nros-readiness-pillar__label"><?php echo esc_html( $i18n['pillar_discover'] ); ?></span>
                <div class="nros-readiness-pillar__track"><div id="nros-pillar-discover-fill" class="nros-readiness-pillar__fill"></div></div>
                <span id="nros-pillar-discover-val" class="nros-readiness-pillar__pct">—</span>
            </div>
            <div class="nros-readiness-pillar">
                <span class="nros-readiness-pillar__label"><?php echo esc_html( $i18n['pillar_entity'] ); ?></span>
                <div class="nros-readiness-pillar__track"><div id="nros-pillar-entity-fill" class="nros-readiness-pillar__fill"></div></div>
                <span id="nros-pillar-entity-val" class="nros-readiness-pillar__pct">—</span>
            </div>
            <div class="nros-readiness-pillar">
                <span class="nros-readiness-pillar__label"><?php echo esc_html( $i18n['pillar_structure'] ); ?></span>
                <div class="nros-readiness-pillar__track"><div id="nros-pillar-structure-fill" class="nros-readiness-pillar__fill"></div></div>
                <span id="nros-pillar-structure-val" class="nros-readiness-pillar__pct">—</span>
            </div>
        </div>

        <div style="font-size:11px;font-weight:600;color:#646970;margin-bottom:6px;text-transform:uppercase;letter-spacing:0.04em;"><?php echo esc_html( $i18n['top_actions_title'] ); ?></div>
        <ol id="nros-readiness-top3" class="nros-readiness-top3" aria-live="polite">
            <li><span class="nros-readiness-top3__badge">…</span><span><?php echo esc_html__( 'Start writing to see readiness fixes.', 'newsroom-ai-assistant' ); ?></span></li>
        </ol>

        <div class="nros-readiness-gauges nros-readiness-gauges--legacy" aria-hidden="true">
            <div class="nros-gauge-col">
                <div class="nros-gauge-col-label"><?php echo esc_html( $i18n['editorial_label'] ); ?></div>
                <?php if ( $use_circular_gauge ) : ?>
                <div class="nros-gauge-wrap" aria-live="polite">
                    <div id="nros-focus-ring" class="nros-gauge-donut" role="img" aria-label="<?php echo esc_attr( $i18n['pub_score'] ); ?>"></div>
                    <div class="nros-gauge-center">
                        <strong id="nros-focus-score">0%</strong>
                    </div>
                </div>
                <?php else : ?>
                <div style="width:100%;min-width:100px;">
                    <strong id="nros-focus-score" style="font-size:15px; color:#d63638;">0%</strong>
                    <div style="background:#f0f0f1; border-radius:4px; height:8px; width:100%; overflow:hidden;margin-top:4px;">
                        <div id="nros-focus-bar" style="background:#d63638; width:0%; height:100%; transition:all 0.3s ease;"></div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            <div class="nros-gauge-col">
                <div class="nros-gauge-col-label"><?php echo esc_html( $i18n['discover_label'] ); ?></div>
                <?php if ( $use_circular_gauge ) : ?>
                <div class="nros-gauge-wrap" aria-live="polite">
                    <div id="nros-discover-ring" class="nros-gauge-donut" role="img" aria-label="<?php echo esc_attr( $i18n['discover_label'] ); ?>"></div>
                    <div class="nros-gauge-center">
                        <strong id="nros-discover-score">—</strong>
                    </div>
                </div>
                <?php else : ?>
                <div style="width:100%;min-width:100px;">
                    <strong id="nros-discover-score" style="font-size:15px; color:#9ca3af;">—</strong>
                    <div style="background:#f0f0f1; border-radius:4px; height:8px; width:100%; overflow:hidden;margin-top:4px;">
                        <div id="nros-discover-bar" style="background:#9ca3af; width:0%; height:100%; transition:all 0.3s ease;"></div>
                    </div>
                </div>
                <?php endif; ?>
            </div>
        </div>
        <div id="nros-focus-action" style="font-size:12px; color:#50575e; background:#f8f9fa; border:1px solid #e2e4e7; border-radius:6px; padding:10px; margin-bottom:10px; line-height:1.45; overflow-wrap:anywhere; word-wrap:break-word;">-</div>
        <div id="nros-readiness-actions">
            <button type="button" class="button button-primary" id="nros-fix-now-btn"><?php echo esc_html( $i18n['fix_now'] ); ?></button>
            <button type="button" class="button" id="nros-open-details-btn"><?php echo esc_html( $i18n['open_panel'] ); ?></button>
        </div>
        <div class="nros-post-geo-row">
            <label for="newsai-post-geo"><?php echo esc_html__( 'Article market', 'newsroom-ai-assistant' ); ?></label>
            <?php
			if ( function_exists( 'newsai_render_geo_select' ) ) {
                newsai_render_geo_select( 'newsai_post_target_geo', $post_geo, 'newsai-post-geo', true, $geo_opt );
            }
            ?>
            <p class="description" style="margin:4px 0 0;font-size:10px;color:#646970;">
                <?php
                echo esc_html(
                    sprintf(
                        /* translators: %s: site default geo code */
                        __( 'Trend Radar & Discover use this market. Site default: %s.', 'newsroom-ai-assistant' ),
                        $geo_opt
                    )
                );
                ?>
            </p>
        </div>
        <details id="nros-score-details-panel" class="nros-score-details" style="font-size:12px; color:#50575e; margin-top:10px;">
            <summary style="cursor:pointer; font-weight:600; outline:none; color:#2271b1; line-height:1.35; overflow-wrap:anywhere;"><?php echo esc_html( $i18n['details'] ); ?></summary>
            <div class="nros-readiness-tabrow" style="margin-top:12px;">
                <button type="button" class="nros-readiness-tab nros-readiness-tab--active" data-readiness="editorial" id="nros-tab-readiness-editorial"><?php echo esc_html( $i18n['tab_editorial'] ); ?></button>
                <button type="button" class="nros-readiness-tab" data-readiness="discover" id="nros-tab-readiness-discover"><?php echo esc_html( $i18n['tab_discover'] ); ?></button>
                <button type="button" class="nros-readiness-tab" data-readiness="structure" id="nros-tab-readiness-structure"><?php echo esc_html( $i18n['tab_structure'] ); ?></button>
            </div>
            <div id="nros-readiness-pane-editorial">
            <ul class="nros-sb-ul" style="margin-top:8px; padding-top:12px; border-top:1px dashed #e2e4e7;">
                <li id="hc-length"><span class="nros-icon-wrapper">⏳</span> <span>...</span></li>
                <li id="hc-number"><span class="nros-icon-wrapper">🔢</span> <span>...</span></li>
                <li id="hc-power"><span class="nros-icon-wrapper">⚡</span> <span>...</span></li>
                <li id="check-meta"><span class="nros-icon-wrapper">📝</span> <span>...</span></li>
                <li id="check-links"><span class="nros-icon-wrapper">🔗</span> <span>...</span></li>
                <li id="check-words"><span class="nros-icon-wrapper">✍️</span> <span>...</span></li>
            </ul>
            </div>
            <div id="nros-readiness-pane-discover" style="display:none;">
                <ul id="nros-discover-checklist" class="nros-sb-ul">
                    <li id="dc-image"><span class="nros-icon-wrapper">🖼</span> <span>…</span></li>
                    <li id="dc-momentum"><span class="nros-icon-wrapper">📈</span> <span>…</span></li>
                    <li id="dc-headline"><span class="nros-icon-wrapper">✍️</span> <span>…</span></li>
                    <li id="dc-entities"><span class="nros-icon-wrapper">🏷</span> <span>…</span></li>
                    <li id="dc-og"><span class="nros-icon-wrapper">📎</span> <span>…</span></li>
                    <li id="dc-author"><span class="nros-icon-wrapper">👤</span> <span>…</span></li>
                    <li id="dc-links"><span class="nros-icon-wrapper">🔗</span> <span>…</span></li>
                </ul>
            </div>
            <div id="nros-readiness-pane-structure" style="display:none;">
                <ul id="nros-structure-checklist" class="nros-sb-ul" style="margin-top:8px; padding-top:12px; border-top:1px dashed #e2e4e7;">
                    <li id="st-chunk"><span class="nros-icon-wrapper">📄</span> <span>…</span></li>
                    <li id="st-answers"><span class="nros-icon-wrapper">❓</span> <span>…</span></li>
                    <li id="st-timeline"><span class="nros-icon-wrapper">🕐</span> <span>…</span></li>
                    <li id="st-takeaways"><span class="nros-icon-wrapper">✅</span> <span>…</span></li>
                    <li id="st-faq"><span class="nros-icon-wrapper">💬</span> <span>…</span></li>
                    <li id="st-citation"><span class="nros-icon-wrapper">📚</span> <span>…</span></li>
                    <li id="st-igain"><span class="nros-icon-wrapper">📈</span> <span>…</span></li>
                </ul>
            </div>
        </details>
    </div>

    <?php if (!empty($active_tasks)): ?>
        <?php foreach ($active_tasks as $task): ?>
        <div style="background: <?php echo esc_attr( $task->is_overdue ? '#fef2f2' : '#fffdf5' ); ?>; border-left: 4px solid <?php echo esc_attr( $task->is_overdue ? '#dc2626' : '#f0b849' ); ?>; padding: 15px; border-radius: 8px; margin-bottom: 15px; border-top: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
            <div style="font-size: 11px; font-weight: 700; text-transform: uppercase; color: <?php echo esc_attr( $task->is_overdue ? '#991b1b' : '#926c0a' ); ?>; margin-bottom: 8px; display: flex; align-items: center; gap: 6px;">
                <span style="font-size:14px;"><?php echo $task->is_overdue ? '🚨' : '📌'; ?></span> 
                <span><?php echo esc_html( $task->is_overdue ? $i18n['overdue_task'] : $i18n['active_task'] ); ?></span>
            </div>
            
            <strong style="font-size: 14px; color: #1d2327; display: block; margin-bottom: 12px;"><?php echo esc_html($task->title); ?></strong>        
            
            <?php if(!empty($task->target_keyword)): ?>
                <div style="font-size: 12px; margin-bottom: 8px; background: #f0f6fc; padding: 6px 10px; border-radius: 4px; border: 1px solid #cce0f0;">
                    <span style="color:#2271b1; font-weight:600;">🔑 <?php echo esc_html( $i18n['keyword'] ); ?></span> 
                    <span style="color:#1d2327; font-weight: 500;"><?php echo esc_html($task->target_keyword); ?></span>
                </div>
            <?php endif; ?>
            
            <?php if(!empty($task->ref_link)): ?>
                <div style="font-size: 12px; margin-bottom: 10px; background: #f8f9fa; padding: 6px 10px; border-radius: 4px; border: 1px solid #e2e4e7;">
                    <span style="color:#50575e; font-weight:600;">🔗 <?php echo esc_html( $i18n['ref'] ); ?></span> 
                    <a href="<?php echo esc_url($task->ref_link); ?>" target="_blank" style="color:#2271b1; text-decoration:none; word-break: break-all;"><?php echo esc_html($task->ref_link); ?></a>
                </div>
            <?php endif; ?>

            <?php if(!empty($task->notes)): ?>
                <div style="font-size: 12px; color: #555; margin-bottom: 12px;"><strong style="color:#1d2327;"><?php echo esc_html( $i18n['brief'] ); ?></strong><br><?php echo nl2br( esc_html( $task->notes ) ); ?></div>
            <?php endif; ?>
            
            <button type="button" id="nros-mark-done-btn" data-id="<?php echo (int)$task->id; ?>" class="nros-btn-outline" style="width: 100%; border-color: #f0b849; color: #926c0a; display:flex; align-items:center; justify-content:center; gap:6px;">
                <span style="font-size:13px;">✅</span> 
                <span><?php echo esc_html( $i18n['mark_done'] ); ?></span>
            </button>
        </div>
        <?php endforeach; ?>
    <?php endif; ?>

    <div class="nros-sb-tabrow" role="tablist" aria-label="<?php echo esc_attr__( 'Newsroom OS sections', 'newsroom-ai-assistant' ); ?>">
        <button type="button" class="nros-sb-tab nros-sb-tab--active" data-sb-pane="readiness" role="tab" aria-selected="true">📊 <?php echo esc_html__( 'Readiness', 'newsroom-ai-assistant' ); ?></button>
        <button type="button" class="nros-sb-tab" data-sb-pane="ai" role="tab" aria-selected="false">🤖 <?php echo esc_html__( 'AI', 'newsroom-ai-assistant' ); ?></button>
        <?php if ( ! function_exists( 'newsai_lite_mode_active' ) || ! newsai_lite_mode_active() ) : ?>
        <button type="button" class="nros-sb-tab" data-sb-pane="radar" role="tab" aria-selected="false">🔥 <?php echo esc_html__( 'Radar', 'newsroom-ai-assistant' ); ?></button>
        <?php endif; ?>
    </div>

    <div id="nros-sb-pane-readiness" class="nros-sb-pane nros-sb-pane--active" data-sb-pane="readiness" role="tabpanel">
        <div class="nros-sb-card" style="margin-bottom:12px;">
            <div style="background:#f9fafb; border:1px solid #e5e7eb; padding:12px; border-radius:6px; border-left:4px solid #646970;" id="nros-next-action-box">
                <strong style="font-size:12px; color:#1d2327; display:flex; justify-content:space-between; align-items:center; margin-bottom:6px;">
                    <span><?php echo esc_html( $i18n['next_action'] ); ?></span>
                    <span id="nros-action-priority" style="font-size:9px; background:#646970; color:#fff; padding:2px 5px; border-radius:4px;">...</span>
                </strong>
                <span style="font-size:12px; color:#50575e; line-height:1.4;" id="nros-next-action-text"><?php echo esc_html__( 'Write the article title…', 'newsroom-ai-assistant' ); ?></span>
            </div>
        </div>

        <details class="nros-accordion" data-nros-persist="editor-tools">
        <summary>⚡ <?php echo esc_html__( 'Editor tools', 'newsroom-ai-assistant' ); ?></summary>
        <div class="nros-accordion-content">
        <div style="margin-bottom: 15px;">
            <span style="font-size:12px; color:#646970; display:flex; align-items:center; gap:6px; margin-bottom:6px;">
                <span class="nros-icon-wrapper" style="font-size:14px;">🏷️</span> 
                <span><?php echo esc_html( $i18n['sug_tags'] ); ?></span>
            </span>
            <div id="nros-tag-suggestions" style="min-height:20px;">
                <span style="font-size:11px; color:#a7aaad; font-style:italic;"><?php echo esc_html( $i18n['tag_ph'] ); ?></span>
            </div>
            <?php if ( function_exists( 'newsai_ai_is_active' ) && newsai_ai_is_active() ) : ?>
                <?php
                $newsai_sidebar_post_id = ( isset( $post ) && $post instanceof WP_Post ) ? (int) $post->ID : 0;
                ?>
                <button type="button" class="nros-btn-outline" id="newsai-ai-tag-match" style="width:100%; margin-top:8px; display:flex; align-items:center; justify-content:center; gap:6px;"
                    data-ajax-url="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>"
                    data-nonce="<?php echo esc_attr( wp_create_nonce( 'newsai_ajax_nonce' ) ); ?>"
                    data-post-id="<?php echo (int) $newsai_sidebar_post_id; ?>"
                    data-ai-active="1">
                    <span>✨</span>
                    <span><?php echo esc_html__( 'AI Tag Match', 'newsroom-ai-assistant' ); ?></span>
                </button>
                <p class="description" style="margin:6px 0 0; font-size:11px; color:#646970;">
                    <?php echo esc_html__( 'Semantic tag picks from your site pool (BYOK).', 'newsroom-ai-assistant' ); ?>
                </p>
            <?php endif; ?>
        </div>

        <?php
        $newsai_show_keypoints_btn = ! ( function_exists( 'newsai_ai_is_active' ) && newsai_ai_is_active() );
        ?>
        <?php if ( $editor_faq_enabled || $newsai_show_keypoints_btn ) : ?>
        <div style="display:flex; gap:8px; margin-bottom: 15px;">
            <?php if ( $editor_faq_enabled ) : ?>
            <button class="nros-btn-outline nros-quick-block" data-type="faq" style="flex:1; display:flex; align-items:center; justify-content:center; gap:5px;">
                <span style="font-size:14px;">💬</span> <span>+ <?php echo esc_html( $i18n['btn_faq'] ); ?></span>
            </button>
            <?php endif; ?>
            <?php if ( $newsai_show_keypoints_btn ) : ?>
            <button class="nros-btn-outline nros-quick-block" data-type="keypoints" style="flex:1; display:flex; align-items:center; justify-content:center; gap:5px;<?php echo $editor_faq_enabled ? '' : ' width:100%;'; ?>">
                <span style="font-size:14px;">💡</span> <span>+ <?php echo esc_html( $i18n['btn_kp'] ); ?></span>
            </button>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div style="background:#f8f9fa; padding:12px; border-radius:6px; border:1px solid #e2e4e7; margin-bottom:15px;">
            <span style="font-size:12px; color:#3c434a; font-weight:600; display:flex; align-items:center; gap:6px; margin-bottom:8px;">
                <span class="nros-icon-wrapper" style="font-size:14px;">🔗</span> 
                <span><?php echo esc_html( $i18n['ins_cluster'] ); ?></span>
            </span>
            <div style="display:flex; gap:5px; margin-bottom:10px;">
                <input type="text" id="newsai-keyword" placeholder="<?php echo esc_attr($i18n['search_ph']); ?>" style="width: 70%; font-size:12px; border:1px solid #ccd0d4; border-radius:4px; padding:4px 8px;">
                <button class="nros-btn-outline" id="newsai-search-links" style="width: 30%;"><?php echo esc_html( $i18n['find'] ); ?></button>
            </div>
            <button class="nros-btn-outline" id="newsai-smart-links" style="width:100%; margin-bottom:8px; display:flex; align-items:center; justify-content:center; gap:6px;">
                <span>🧠</span><span><?php echo esc_html($i18n['smart_links']); ?></span>
            </button>
            <div style="font-size:11px; color:#646970; margin-bottom:8px;"><?php echo esc_html($i18n['smart_links_hint']); ?></div>
            <div id="newsai-links-results" style="display:none; max-height:120px; overflow-y:auto; font-size:12px; margin-bottom:10px;"></div>
            <button class="button button-primary" id="newsai-insert-links" style="width: 100%; display:none;"><?php echo esc_html( $i18n['ins_links'] ); ?></button>

            <hr style="margin: 15px 0; border:0; border-top:1px solid #e2e4e7;">
            <span style="font-size:12px; color:#3c434a; font-weight:600; display:flex; align-items:center; gap:6px; margin-bottom:8px;">
                <span class="nros-icon-wrapper" style="font-size:14px;">⏳</span> 
                <span><?php echo esc_html( $i18n['ins_tl'] ); ?></span>
            </span>
            <button class="nros-btn-outline" id="newsai-insert-timeline" style="width:100%; display:flex; align-items:center; justify-content:center; gap:6px;">
                <span style="font-size:12px;">➕</span> 
                <span><?php echo esc_html( $i18n['btn_tl'] ); ?></span>
            </button>
        </div>

        <?php $is_timeline_disabled = get_post_meta($post_id, '_newsai_disable_auto_timeline', true); ?>
        <?php
        $liveblog_enabled = function_exists( 'newsai_breaking_liveblog_globally_enabled' ) && newsai_breaking_liveblog_globally_enabled();
        $breaking_state   = function_exists( 'newsai_breaking_ui_state' ) ? newsai_breaking_ui_state( $post_id ) : [ 'active' => false, 'legacy' => false, 'hours_left' => 0 ];
        $breaking_on      = ! empty( $breaking_state['active'] ) || ! empty( $breaking_state['legacy'] );
        $breaking_heading_count = function_exists( 'nros_count_liveblog_headings' )
            ? nros_count_liveblog_headings( (string) get_post_field( 'post_content', $post_id ) )
            : 0;
        ?>
        <?php if ( $liveblog_enabled ) : ?>
        <div class="nros-breaking-panel <?php echo esc_attr( $breaking_on ? '' : 'is-idle' ); ?>" id="nros-breaking-panel"
            data-expires-template="<?php echo esc_attr( $i18n['breaking_expires'] ); ?>"
            data-warn-no-headings="<?php echo esc_attr( $i18n['breaking_warn_no_headings'] ); ?>"
            data-headings-ok-template="<?php echo esc_attr( $i18n['breaking_headings_ok'] ); ?>">
            <div style="font-weight:600; color:#1d2327; margin-bottom:4px; display:flex; align-items:center; gap:6px;">
                <span>🔴</span>
                <span><?php echo esc_html( $i18n['breaking_title'] ); ?></span>
            </div>
            <p style="margin:0 0 8px 0; font-size:11px; color:#646970;"><?php echo esc_html( $i18n['breaking_help'] ); ?></p>
            <details class="nros-breaking-guide" style="margin:0 0 8px 0;font-size:11px;">
                <summary><?php echo esc_html( $i18n['breaking_guide_title'] ); ?></summary>
                <p style="margin:6px 0 4px 0;color:#646970;"><?php echo esc_html( $i18n['breaking_guide_intro'] ); ?></p>
                <ul>
                    <li><?php echo esc_html( $i18n['breaking_guide_li1'] ); ?></li>
                    <li><?php echo esc_html( $i18n['breaking_guide_li2'] ); ?></li>
                    <li><?php echo esc_html( $i18n['breaking_guide_li3'] ); ?></li>
                    <li><?php echo esc_html( $i18n['breaking_guide_li4'] ); ?></li>
                </ul>
            </details>
            <button type="button" class="nros-btn-outline" id="newsai-insert-live-update" style="width:100%;margin-bottom:6px;">
                <?php echo esc_html( $i18n['breaking_insert_live'] ); ?>
            </button>
            <div id="nros-breaking-heading-warn" class="nros-breaking-heading-warn" style="<?php echo esc_attr( ( $breaking_on && $breaking_heading_count < 1 ) ? '' : 'display:none;' ); ?>">
                ⚠️ <?php echo esc_html( $i18n['breaking_warn_no_headings'] ); ?>
            </div>
            <div id="nros-breaking-heading-ok" class="nros-breaking-heading-ok" style="<?php echo esc_attr( ( $breaking_on && $breaking_heading_count > 0 ) ? '' : 'display:none;' ); ?>">
                <?php
                if ( $breaking_on && $breaking_heading_count > 0 ) {
                    echo esc_html( sprintf( $i18n['breaking_headings_ok'], $breaking_heading_count ) );
                }
                ?>
            </div>
            <div id="nros-breaking-active-ui" style="<?php echo esc_attr( $breaking_on ? '' : 'display:none;' ); ?>">
                <span class="nros-breaking-badge-pill"><?php echo esc_html( $i18n['breaking_active'] ); ?></span>
                <p id="nros-breaking-expires-text" style="margin:0 0 8px 0; color:#991b1b; font-size:11px;">
                    <?php
                    if ( ! empty( $breaking_state['active'] ) && ! empty( $breaking_state['hours_left'] ) ) {
                        echo esc_html( sprintf( $i18n['breaking_expires'], $breaking_state['hours_left'] ) );
                    } elseif ( ! empty( $breaking_state['legacy'] ) ) {
                        echo esc_html( $i18n['breaking_legacy'] );
                    }
                    ?>
                </p>
                <button type="button" class="nros-btn-outline" id="newsai-breaking-resolve" style="width:100%; margin-bottom:6px;">
                    <?php echo esc_html( $i18n['breaking_resolve'] ); ?>
                </button>
                <input type="hidden" name="newsai_breaking_resolve" id="newsai-breaking-resolve-field" value="0" />
            </div>
            <div id="nros-breaking-idle-ui" style="<?php echo esc_attr( $breaking_on ? 'display:none;' : '' ); ?>">
                <input type="hidden" name="newsai_enable_breaking" value="0" />
                <label style="cursor:pointer; display:flex; align-items:center; gap:6px; margin:0;">
                    <input type="checkbox" id="newsai-enable-breaking" name="newsai_enable_breaking" value="1" style="margin:0;" />
                    <span><?php echo esc_html( $i18n['breaking_enable'] ); ?></span>
                </label>
            </div>
        </div>
        <?php endif; ?>
        <label style="font-size: 12px; color: #50575e; cursor: pointer; display:flex; align-items:center; gap:6px;">
            <input type="checkbox" name="newsai_disable_auto_timeline" value="yes" <?php checked($is_timeline_disabled, 'yes'); ?> style="margin:0;">
            <span class="nros-icon-wrapper" style="font-size:14px;">🚫</span> 
            <span><?php echo esc_html( $i18n['dis_tl'] ); ?></span>
        </label>
        </div>
        </details>
    </div>

    <?php if ( ! function_exists( 'newsai_lite_mode_active' ) || ! newsai_lite_mode_active() ) : ?>
    <div id="nros-sb-pane-radar" class="nros-sb-pane" data-sb-pane="radar" role="tabpanel">
        <div class="nros-sb-card nros-radar-panel" style="padding:0; overflow:visible;">
        <div style="padding: 8px 12px 0 12px;">
            <h4 class="nros-sb-title" style="margin-bottom:12px; border:none; justify-content:space-between;">
                <span style="font-size:11px; color:#646970; font-weight:600;"><?php echo esc_html__( 'Live feed', 'newsroom-ai-assistant' ); ?></span>
                <span style="font-size: 10px; background: #fee2e2; color: #dc2626; padding: 2px 6px; border-radius: 4px; font-weight: bold;">LIVE</span>
            </h4>
            <div class="nros-radar-tabrow" style="display:flex; gap:20px; border-bottom: 2px solid #f0f0f1;">
                <span class="newsai-tab active" data-target="nros-pane-trends">TRENDS</span>
                <span class="newsai-tab" data-target="nros-pane-news">NEWS</span>
            </div>
        </div>
        <div id="nros-pane-trends" class="newsai-tab-content" data-nros-radar-lazy="1" style="padding: 15px; max-height:280px; overflow-y:auto;"><span class="nros-radar-idle"><?php echo esc_html__( 'Loading trends for your article market…', 'newsroom-ai-assistant' ); ?></span></div>
        <div id="nros-pane-news" class="newsai-tab-content" data-nros-radar-lazy="1" style="display:none; padding: 15px; max-height:280px; overflow-y:auto;"><span class="nros-radar-idle"><?php echo esc_html__( 'Tap NEWS to load headlines.', 'newsroom-ai-assistant' ); ?></span></div>
        </div>
    </div>
    <?php endif; ?>

    <div id="nros-sb-pane-ai" class="nros-sb-pane" data-sb-pane="ai" role="tabpanel">
    <?php if ( function_exists( 'newsai_ai_is_active' ) && newsai_ai_is_active() ) : ?>
        <div class="nros-sb-card" id="newsai-ai-assist">
            <h4 class="nros-sb-title"><span>🧠</span> <?php echo esc_html__( 'AI Assist (Pro)', 'newsroom-ai-assistant' ); ?></h4>
            <div style="display:flex;flex-direction:column;gap:8px;">
                <button type="button" class="nros-btn-outline newsai-ai-action" data-task="geo_summary" style="text-align:left;display:flex;align-items:center;gap:8px;">
                    <span style="font-size:14px;">🌐</span>
                    <span><?php echo esc_html__( 'GEO summary (AI Overviews)', 'newsroom-ai-assistant' ); ?></span>
                </button>
                <button type="button" class="nros-btn-outline newsai-ai-action" data-task="title_suggest" style="text-align:left;display:flex;align-items:center;gap:8px;">
                    <span style="font-size:14px;">🎯</span>
                    <span><?php echo esc_html__( 'Suggest new titles', 'newsroom-ai-assistant' ); ?></span>
                </button>
                <button type="button" class="nros-btn-outline newsai-ai-action" data-task="optimize_content" style="text-align:left;display:flex;align-items:center;gap:8px;">
                    <span style="font-size:14px;">✨</span>
                    <span><?php echo esc_html__( 'Optimize content (no rewrite)', 'newsroom-ai-assistant' ); ?></span>
                </button>
                <button type="button" class="nros-btn-outline newsai-ai-action" data-task="trend_keywords" style="text-align:left;display:flex;align-items:center;gap:8px;">
                    <span style="font-size:14px;">🔥</span>
                    <span><?php echo esc_html__( 'Trending keywords (Google Trends)', 'newsroom-ai-assistant' ); ?></span>
                </button>
                <button type="button" class="nros-btn-outline newsai-ai-action" data-task="meta_description" style="text-align:left;display:flex;align-items:center;gap:8px;">
                    <span style="font-size:14px;">📝</span>
                    <span><?php echo esc_html__( 'Generate meta description', 'newsroom-ai-assistant' ); ?></span>
                </button>
            </div>
            <div id="newsai-ai-output" style="margin-top:12px;font-size:13px;color:#1f2937;"></div>
            <p class="description" style="margin-top:10px;">
                <?php echo esc_html__( 'Uses the API key you set in Settings (BYOK). Results are cached for 30 minutes.', 'newsroom-ai-assistant' ); ?>
            </p>
        </div>
    <?php elseif ( function_exists( 'newsai_is_pro' ) && ! newsai_is_pro() && function_exists( 'newsai_pro_promo_link' ) ) : ?>
        <div class="nros-sb-card">
            <p style="margin:0 0 10px 0;color:#4b5563;">
                <?php echo esc_html__( 'Plug in OpenAI, Gemini or Groq for title suggestions, targeted edits and trend-aware keywords. BYOK — your own API key, nothing sent to NewsOS.', 'newsroom-ai-assistant' ); ?>
            </p>
            <a href="<?php echo esc_url( newsai_pro_promo_link( 'sidebar-ai' ) ); ?>" target="_blank" rel="noopener" class="button button-primary" style="background:#6366f1;border-color:#6366f1;">
                🔒 <?php echo esc_html__( 'Unlock with Pro', 'newsroom-ai-assistant' ); ?>
            </a>
        </div>
    <?php endif; ?>

        <div class="nros-sb-card" id="nros-claim-review-panel">
            <h4 class="nros-sb-title"><span>✅</span> <?php echo esc_html__( 'Fact-check (ClaimReview)', 'newsroom-ai-assistant' ); ?></h4>
            <p class="description" style="margin:0 0 10px 0;"><?php echo esc_html__( 'Mark debunk / fact-check articles so JSON-LD emits ClaimReview for higher publisher trust.', 'newsroom-ai-assistant' ); ?></p>
            <label style="display:flex;align-items:center;gap:8px;font-size:12px;margin-bottom:10px;cursor:pointer;">
                <input type="checkbox" id="newsai-claim-review-enabled" name="newsai_claim_review_enabled" value="yes" <?php checked( $claim_review_enabled ); ?> />
                <span><?php echo esc_html__( 'This article reviews or debunks a claim', 'newsroom-ai-assistant' ); ?></span>
            </label>
            <div id="nros-claim-review-fields" style="<?php echo esc_attr( $claim_review_enabled ? '' : 'display:none;' ); ?>">
                <label for="newsai-claim-text" style="display:block;font-size:11px;font-weight:600;margin-bottom:4px;"><?php echo esc_html__( 'Claim being addressed', 'newsroom-ai-assistant' ); ?></label>
                <textarea id="newsai-claim-text" name="newsai_claim_text" rows="3" style="width:100%;font-size:12px;margin-bottom:8px;"><?php echo esc_textarea( $claim_text ); ?></textarea>
                <label for="newsai-claim-rating" style="display:block;font-size:11px;font-weight:600;margin-bottom:4px;"><?php echo esc_html__( 'Verdict', 'newsroom-ai-assistant' ); ?></label>
                <select id="newsai-claim-rating" name="newsai_claim_rating" style="width:100%;font-size:12px;margin-bottom:8px;">
                    <option value="False" <?php selected( $claim_rating, 'False' ); ?>><?php echo esc_html__( 'False / Debunked', 'newsroom-ai-assistant' ); ?></option>
                    <option value="True" <?php selected( $claim_rating, 'True' ); ?>><?php echo esc_html__( 'True / Confirmed', 'newsroom-ai-assistant' ); ?></option>
                    <option value="Misleading" <?php selected( $claim_rating, 'Misleading' ); ?>><?php echo esc_html__( 'Misleading', 'newsroom-ai-assistant' ); ?></option>
                </select>
                <label for="newsai-claim-source-url" style="display:block;font-size:11px;font-weight:600;margin-bottom:4px;"><?php echo esc_html__( 'Primary source URL (optional)', 'newsroom-ai-assistant' ); ?></label>
                <input type="url" id="newsai-claim-source-url" name="newsai_claim_source_url" value="<?php echo esc_url( $claim_source_url ); ?>" style="width:100%;font-size:12px;" placeholder="https://" />
            </div>
        </div>

        <details class="nros-accordion" data-nros-persist="prompts">
        <summary>🤖 <?php echo esc_html( $i18n['prompts'] ); ?></summary>
        <div class="nros-accordion-content">
        <p class="description" style="margin:0 0 10px 0;"><?php echo esc_html__( 'Copy a prompt to your clipboard for ChatGPT or similar. Works alongside AI Assist — use either in-editor AI or your own external tool.', 'newsroom-ai-assistant' ); ?></p>
        <div style="display:flex; flex-direction:column; gap:8px;">
            <button class="nros-btn-outline newsai-prompt-btn" data-type="title" style="text-align:left; display:flex; align-items:center; gap:8px;">
                <span class="nros-icon-wrapper" style="font-size:14px;">✍️</span> 
                <span><?php echo esc_html( $i18n['p_title'] ); ?></span>
            </button>
            <button class="nros-btn-outline newsai-prompt-btn" data-type="seo" style="text-align:left; display:flex; align-items:center; gap:8px;">
                <span class="nros-icon-wrapper" style="font-size:14px;">🔍</span> 
                <span><?php echo esc_html( $i18n['p_seo'] ); ?></span>
            </button>
            <button class="nros-btn-outline newsai-prompt-btn" data-type="rewrite" style="text-align:left; display:flex; align-items:center; gap:8px;">
                <span class="nros-icon-wrapper" style="font-size:14px;">📝</span> 
                <span><?php echo esc_html( $i18n['p_rewrite'] ); ?></span>
            </button>
        </div>
        </div>
        </details>
    </div>

    <?php if ($show_onboarding_modal): ?>
    <div id="nros-onboarding-modal" style="display:none;">
        <div class="nros-onboarding-backdrop"></div>
        <div class="nros-onboarding-dialog" role="dialog" aria-modal="true" aria-label="<?php echo esc_attr($i18n['onb_title']); ?>">
            <h3><?php echo esc_html($i18n['onb_title']); ?></h3>
            <ul>
                <li><?php echo esc_html($i18n['onb_line_1']); ?></li>
                <li><?php echo esc_html($i18n['onb_line_2']); ?></li>
                <li><?php echo esc_html($i18n['onb_line_3']); ?></li>
            </ul>
            <button type="button" class="button button-primary" id="nros-onboarding-close"><?php echo esc_html($i18n['onb_btn']); ?></button>
        </div>
    </div>
    <?php endif; ?>

</div>