<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/FreeTier.php';

dbnToolsRequirePageAuth($_SERVER['REQUEST_URI'] ?? '/dashboard.php');

$uiLang    = dbnToolsCurrentLanguage();
$tools     = dbnToolsLaunchedTools($uiLang);
$workbench = dbnToolsWorkbenchMeta($uiLang);
$langPath  = '/dashboard.php';

$dashIsSso  = dbnToolsIsFreeTier();
$dashUserId = $dashIsSso ? (int)($_SESSION['dbn_tools_sso_uid'] ?? 0) : 0;
$dashTier   = $dashIsSso ? FreeTier::tier($dashUserId) : 'caveau';
$dashDetail = $dashIsSso ? FreeTier::balanceDetail($dashUserId) : null;

$tierLabels = [
    'free'   => ['Free',        '#f3f4f6', '#374151'],
    'plus'   => ['Plus',        '#ddd6fe', '#5b21b6'],
    'pro'    => ['Pro Familie', '#bfdbfe', '#1e40af'],
    'caveau' => ['CaveauAI',    '#d1fae5', '#065f46'],
];
$tierLabel     = $tierLabels[$dashTier] ?? $tierLabels['free'];
$showSurveyCta = $dashIsSso && empty($dashDetail['survey_completed_at']);

// User display name
$dashAuthUser = dbnToolsAuthenticatedUser();
$dashEmail    = '';
if ($dashAuthUser !== null) {
    $e = (string)($dashAuthUser['email'] ?? '');
    $dashEmail = strstr($e, '@', true) ?: $e;
}

$dashProfile = $dashIsSso ? dbnToolsMainUserProfile($dashUserId) : null;
$dashProfileNeedsPrompt = dbnToolsProfileNeedsPrompt($dashProfile);
$dashProfileValue = static function (string $key) use ($dashProfile): string {
    return (string)($dashProfile[$key] ?? '');
};
$dashSeatLimit = match ($dashTier) {
    'pro' => 3,
    default => 1,
};
$dashTeamLabel = $dashSeatLimit > 1 ? '1 / ' . $dashSeatLimit . ' seats in use' : 'Single-user workspace';

// Next refill / billing date
$dashNextBilling     = '';
$dashNextBillingKey  = 'next_refill';
if ($dashIsSso && $dashDetail) {
    if (!empty($dashDetail['subscription_period_end'])) {
        $ts = strtotime((string)$dashDetail['subscription_period_end']);
        $dashNextBilling    = $ts ? date('j M Y', $ts) : '';
        $dashNextBillingKey = 'next_billing';
    } else {
        $m = (int)date('m'); $y = (int)date('Y');
        if ($m === 12) { $m = 1; $y++; } else { $m++; }
        $dashNextBilling = date('j M Y', mktime(0, 0, 0, $m, 1, $y));
    }
}

// Tool emoji icons
$toolEmoji = [
    'transcribe'    => '🎙️',
    'timeline'      => '📅',
    'redact'        => '🔒',
    'summarize'     => '📝',
    'legal-analysis'=> '🏛️',
    'korrespond'    => '✉️',
    'barnevernet'   => '🔍',
    'advocate'      => '⚖️',
    'deep-research' => '🔬',
    'discrepancy'   => '🔎',
    'corpus'        => '📚',
    'citations'     => '🔗',
    'translate'     => '🌐',
];

// Tool → MCP slug
$toolMcpSlugs = [
    'transcribe'    => 'dbn.transcribe_audio',
    'timeline'      => 'dbn.timeline',
    'redact'        => 'dbn.redact',
    'korrespond'    => 'dbn.korrespond',
    'barnevernet'   => 'dbn.barnevernet_analyze',
    'advocate'      => 'dbn.advocate_brief',
    'deep-research' => 'dbn.deep_research',
    'discrepancy'   => 'dbn.discrepancy_find',
    'corpus'        => 'dbn.list_documents',
    'citations'     => 'dbn.citation_graph',
];

// Tool → [about, guide, tech] doc page links
$toolDocLinks = [
    'advocate'   => ['/advocate-about.php',   '/advocate-guide.php',   '/advocate-tech.php'],
    'timeline'   => ['/timeline-about.php',   '/timeline-guide.php',   '/timeline-tech.php'],
    'korrespond' => ['/korrespond-about.php', '/korrespond-guide.php', '/korrespond-tech.php'],
];

// Localized strings for new sections
$dashL = [
    'en' => [
        'acct_header'     => 'Account',
        'signed_in_as'    => 'Signed in as',
        'manage_plan'     => 'Manage plan',
        'upgrade_plan'    => 'Upgrade plan',
        'top_up'          => 'Top up credits',
        'next_refill'     => 'Credits refill',
        'next_billing'    => 'Next billing',
        'monthly_quota'   => 'monthly quota',
        'trial_badge'     => 'Trial — %d days left',
        'about_link'      => 'About',
        'mcp_copy_slug'   => 'Copy MCP slug',
        'mcp_section'     => 'Developers & MCP',
        'mcp_desc'        => 'Connect Claude Desktop, Claude Code, Cursor, or any MCP-compatible client to the full tool suite.',
        'mcp_token_lbl'   => 'API token',
        'mcp_no_token'    => 'No active token',
        'mcp_copy'        => 'Copy',
        'mcp_not_avail'   => 'MCP tokens require Plus or Pro — upgrade to connect AI clients.',
        'mcp_stdio_lbl'   => 'Claude Desktop / Claude Code (stdio)',
        'mcp_remote_lbl'  => 'Remote HTTP — Cursor, Zed, Windsurf',
        'mcp_full_docs'   => 'Full setup guide & token management →',
        'tool_ref_title'  => 'Tool reference',
        'tool_ref_sub'    => 'Deep-dive docs for the three flagship tools.',
        'guide_link'      => 'Guide',
        'tech_link'       => 'Technical',
        'open_tool'       => 'Open tool',
        'tool_info'       => 'Tool info',
    ],
    'no' => [
        'acct_header'     => 'Konto',
        'signed_in_as'    => 'Innlogget som',
        'manage_plan'     => 'Administrer plan',
        'upgrade_plan'    => 'Oppgrader plan',
        'top_up'          => 'Kjøp kreditter',
        'next_refill'     => 'Kreditter fornyes',
        'next_billing'    => 'Neste fakturering',
        'monthly_quota'   => 'månedlig kvote',
        'trial_badge'     => 'Prøveperiode — %d dager igjen',
        'about_link'      => 'Om',
        'mcp_copy_slug'   => 'Kopier MCP-slug',
        'mcp_section'     => 'Utviklere & MCP',
        'mcp_desc'        => 'Koble Claude Desktop, Claude Code, Cursor eller annen MCP-klient til hele verktøysuiten.',
        'mcp_token_lbl'   => 'API-token',
        'mcp_no_token'    => 'Ingen aktiv token',
        'mcp_copy'        => 'Kopier',
        'mcp_not_avail'   => 'MCP-tokens krever Plus eller Pro — oppgrader for å koble til AI-klienter.',
        'mcp_stdio_lbl'   => 'Claude Desktop / Claude Code (stdio)',
        'mcp_remote_lbl'  => 'Ekstern HTTP — Cursor, Zed, Windsurf',
        'mcp_full_docs'   => 'Full oppsettsguide & token-administrasjon →',
        'tool_ref_title'  => 'Verktøyreferanse',
        'tool_ref_sub'    => 'Dybdedokumentasjon for de tre flaggskipverktøyene.',
        'guide_link'      => 'Guide',
        'tech_link'       => 'Teknisk',
        'open_tool'       => 'Åpne verktøy',
        'tool_info'       => 'Verktøyinfo',
    ],
    'uk' => [
        'acct_header'     => 'Обліковий запис',
        'signed_in_as'    => 'Ввійшли як',
        'manage_plan'     => 'Управляти планом',
        'upgrade_plan'    => 'Покращити план',
        'top_up'          => 'Поповнити кредити',
        'next_refill'     => 'Кредити поновлюються',
        'next_billing'    => 'Наступне списання',
        'monthly_quota'   => 'місячна квота',
        'trial_badge'     => 'Пробний — %d дн. залишилось',
        'about_link'      => 'Про',
        'mcp_copy_slug'   => 'Копіювати MCP-ідентифікатор',
        'mcp_section'     => 'Розробники & MCP',
        'mcp_desc'        => 'Підключайте Claude Desktop, Claude Code, Cursor або будь-який MCP-клієнт до повного набору інструментів.',
        'mcp_token_lbl'   => 'API-токен',
        'mcp_no_token'    => 'Немає активного токена',
        'mcp_copy'        => 'Копіювати',
        'mcp_not_avail'   => 'MCP-токени потребують Plus або Pro — оновіться для підключення AI-клієнтів.',
        'mcp_stdio_lbl'   => 'Claude Desktop / Claude Code (stdio)',
        'mcp_remote_lbl'  => 'Віддалений HTTP — Cursor, Zed, Windsurf',
        'mcp_full_docs'   => 'Повна документація та управління токенами →',
        'tool_ref_title'  => 'Довідник інструментів',
        'tool_ref_sub'    => 'Детальна документація трьох флагманських інструментів.',
        'guide_link'      => 'Посібник',
        'tech_link'       => 'Технічний',
        'open_tool'       => 'Відкрити',
        'tool_info'       => 'Про інструмент',
    ],
    'pl' => [
        'acct_header'     => 'Konto',
        'signed_in_as'    => 'Zalogowany jako',
        'manage_plan'     => 'Zarządzaj planem',
        'upgrade_plan'    => 'Ulepsz plan',
        'top_up'          => 'Doładuj kredyty',
        'next_refill'     => 'Kredyty odnawiają się',
        'next_billing'    => 'Następne rozliczenie',
        'monthly_quota'   => 'miesięczny limit',
        'trial_badge'     => 'Próba — %d dni pozostało',
        'about_link'      => 'O narzędziu',
        'mcp_copy_slug'   => 'Kopiuj identyfikator MCP',
        'mcp_section'     => 'Deweloperzy & MCP',
        'mcp_desc'        => 'Podłącz Claude Desktop, Claude Code, Cursor lub dowolnego klienta MCP do pełnego zestawu narzędzi.',
        'mcp_token_lbl'   => 'Token API',
        'mcp_no_token'    => 'Brak aktywnego tokenu',
        'mcp_copy'        => 'Kopiuj',
        'mcp_not_avail'   => 'Tokeny MCP wymagają Plus lub Pro — zaktualizuj, aby połączyć klientów AI.',
        'mcp_stdio_lbl'   => 'Claude Desktop / Claude Code (stdio)',
        'mcp_remote_lbl'  => 'Zdalny HTTP — Cursor, Zed, Windsurf',
        'mcp_full_docs'   => 'Pełna dokumentacja i zarządzanie tokenami →',
        'tool_ref_title'  => 'Dokumentacja narzędzi',
        'tool_ref_sub'    => 'Szczegółowa dokumentacja trzech flagowych narzędzi.',
        'guide_link'      => 'Poradnik',
        'tech_link'       => 'Techniczny',
        'open_tool'       => 'Otwórz',
        'tool_info'       => 'Info',
    ],
];
$dl = $dashL[$uiLang] ?? $dashL['en'];

require_once __DIR__ . '/includes/tool-svgs.php';
$langSuffix = $uiLang !== 'en' ? '?lang=' . urlencode($uiLang) : '';
?>
<!doctype html>
<html lang="<?= htmlspecialchars($uiLang) ?>">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= htmlspecialchars(dbnToolsT('dashboard_title', $uiLang)) ?> — Do Better Norge</title>
    <meta name="robots" content="noindex, nofollow">
    <meta name="theme-color" content="#00205B">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@400;600;700&family=IBM+Plex+Sans:wght@400;500;600;700&display=swap">
    <link rel="stylesheet" href="assets/css/tools.css">
    <link rel="stylesheet" href="assets/css/dbn-tools-redesign.css">
    <link rel="stylesheet" href="assets/css/dashboard.css">
    <style>
        /* ── Dashboard enhancements ───────────────────────────── */
        details.dash-mcp-details summary::-webkit-details-marker { display: none; }
        details.dash-mcp-details summary::marker { display: none; }
        .dash-chevron { transition: transform .2s ease; display: inline-block; }
        details.dash-mcp-details[open] .dash-chevron { transform: rotate(180deg); }
        /* card footer links always sit above the div onclick */
        .dash-card-footer a,
        .dash-card-footer button { position: relative; z-index: 1; }
        .dash-card-footer { padding-top: 0.85rem; margin-top: auto; border-top: 1px solid rgba(0,0,0,.07); display: flex; align-items: center; gap: 0.65rem; flex-wrap: wrap; }
        /* pill badges on acct bar */
        .dash-tier-badge { display: inline-flex; align-items: center; font-size: .82rem; font-weight: 700; padding: 3px 12px; border-radius: 999px; text-transform: uppercase; letter-spacing: .06em; border: 1px solid currentColor; flex-shrink: 0; }
    </style>
</head>
<body data-authenticated="true" class="lt-app">
<script>
window.DBN_TOOLS_AUTHENTICATED = true;
window.DBN_TOOLS_LANG = <?= json_encode($uiLang, JSON_UNESCAPED_UNICODE) ?>;
</script>

<?php include __DIR__ . '/includes/nav.php'; ?>

<div class="dbn-context-bar" role="note">
    <span class="dbn-context-bar__tag"><?= htmlspecialchars(dbnToolsT('context_bar_tag', $uiLang)) ?></span>
    <nav class="dbn-context-bar__links" aria-label="About">
        <a href="/why-ours.php<?= $langSuffix ?>"><?= htmlspecialchars(dbnToolsT('context_bar_why', $uiLang)) ?></a>
        <a href="/pricing.php<?= $langSuffix ?>"><?= htmlspecialchars(dbnToolsT('context_bar_pricing', $uiLang)) ?></a>
        <a href="/mcp-tool.php<?= $langSuffix ?>"><?= htmlspecialchars(dbnToolsT('context_bar_mcp', $uiLang)) ?></a>
        <a href="/privacy.php<?= $langSuffix ?>"><?= htmlspecialchars(dbnToolsT('context_bar_privacy', $uiLang)) ?></a>
    </nav>
</div>

<main id="appShell" class="app-shell dashboard-shell">

    <?php if ($dashIsSso && $dashDetail):
        $eff = (int)$dashDetail['balance'] + (int)$dashDetail['bonus_balance'];
        $isPaid = in_array($dashTier, ['plus', 'pro'], true);
    ?>

    <!-- ── Account overview bar ─────────────────────────────────────── -->
    <div class="dash-overview-bar">
        <div class="dash-overview-bar__left">
            <span class="dash-tier-badge" style="background:<?= $tierLabel[1] ?>; color:<?= $tierLabel[2] ?>;"><?= htmlspecialchars($tierLabel[0]) ?></span>
            <?php if (!empty($dashDetail['trial_active'])): ?>
            <span class="dash-tier-badge" style="background:#fef3c7; color:#92400e;"><?= htmlspecialchars(sprintf($dl['trial_badge'], (int)$dashDetail['trial_days_remaining'])) ?></span>
            <?php endif; ?>
            <span class="dash-overview-bar__credits"><?= number_format($eff) ?> <?= $uiLang === 'no' ? 'kred.' : 'credits' ?></span>
            <span class="dash-overview-bar__meta"><?= (int)$dashDetail['balance'] ?> <?= $uiLang === 'no' ? 'månedlige' : 'monthly' ?> · <?= (int)$dashDetail['bonus_balance'] ?> <?= $uiLang === 'no' ? 'forhåndsbetalt' : 'prepaid' ?></span>
            <?php if ($dashNextBilling): ?>
            <span class="dash-overview-bar__meta"><?= htmlspecialchars($dl[$dashNextBillingKey]) ?>: <strong><?= htmlspecialchars($dashNextBilling) ?></strong></span>
            <?php endif; ?>
            <?php if ($dashEmail): ?>
            <span class="dash-overview-bar__meta dash-email-sep" style="display:none;">·</span>
            <span class="dash-overview-bar__meta"><?= htmlspecialchars($dl['signed_in_as']) ?>: <strong><?= htmlspecialchars($dashEmail) ?></strong></span>
            <?php endif; ?>
        </div>
        <div class="dash-overview-bar__right">
            <?php if ($isPaid): ?>
            <a href="/account.php#plan" class="dash-overview-bar__action dash-overview-bar__action--manage"><?= htmlspecialchars($dl['manage_plan']) ?></a>
            <?php else: ?>
            <a href="/pricing.php" class="dash-overview-bar__action dash-overview-bar__action--upgrade"><?= htmlspecialchars($dl['upgrade_plan']) ?> →</a>
            <?php endif; ?>
            <a href="/pricing.php#topup" class="dash-overview-bar__action dash-overview-bar__action--topup"><?= htmlspecialchars($dl['top_up']) ?> →</a>
        </div>
    </div>

    <!-- ── Status cards row ─────────────────────────────────────────── -->
    <section class="dashboard-status-row" style="display:grid; grid-template-columns:repeat(auto-fit, minmax(260px, 1fr)); gap:1.1rem; margin:0 0 1.25rem;">
        <div class="status-card" style="background:#fff; border:1px solid #e5e7eb; border-radius:10px; padding:1.3rem 1.5rem;">
            <p style="margin:0; color:#6b7280; font-size:.95rem; text-transform:uppercase; letter-spacing:.06em;"><?= htmlspecialchars(dbnToolsT('credits_available', $uiLang)) ?></p>
            <p style="margin:.35rem 0 0; font-size:2rem; font-weight:700; color:#00205B;"><?= number_format($eff, 0, ',', ' ') ?></p>
            <p style="margin:0; color:#6b7280; font-size:.95rem;"><?= (int)$dashDetail['balance'] ?> <?= htmlspecialchars(dbnToolsT('credits_monthly', $uiLang)) ?> · <?= (int)$dashDetail['bonus_balance'] ?> <?= $uiLang === 'no' ? 'forhåndsbetalte' : 'prepaid' ?> · <a href="/account.php#plan"><?= htmlspecialchars(dbnToolsT('details_link', $uiLang)) ?></a></p>
        </div>
        <?php if ($isPaid): ?>
        <a class="status-card" href="/account.php#case" style="background:#fff; border:1px solid #e5e7eb; border-radius:10px; padding:1.3rem 1.5rem; text-decoration:none; color:inherit;">
            <p style="margin:0; color:#6b7280; font-size:.95rem; text-transform:uppercase; letter-spacing:.06em;"><?= htmlspecialchars(dbnToolsT('my_case', $uiLang)) ?></p>
            <p style="margin:.35rem 0 0; font-size:1.55rem; font-weight:700; color:#00205B;"><?= htmlspecialchars(dbnToolsT('build_your_case', $uiLang)) ?> →</p>
            <?php
                $used    = (int)$dashDetail['storage_used_bytes'];
                $quota   = (int)$dashDetail['storage_quota_bytes'];
                $usedMb  = $used  > 0 ? round($used  / 1048576, 1) : 0;
                $quotaMb = $quota > 0 ? round($quota / 1048576, 0) : 0;
            ?>
            <p style="margin:0; color:#6b7280; font-size:.95rem;"><?= $usedMb ?> MB / <?= $quotaMb ?> MB</p>
        </a>
        <?php else: ?>
        <a class="status-card status-card--cta" href="/pricing.php" style="text-decoration:none;">
            <p style="margin:0; font-size:.95rem; text-transform:uppercase; letter-spacing:.06em;"><?= htmlspecialchars(dbnToolsT('build_your_case', $uiLang)) ?></p>
            <p style="margin:.35rem 0 0; font-size:1.55rem; font-weight:700;"><?= htmlspecialchars(dbnToolsT('upload_documents', $uiLang)) ?> →</p>
            <p style="margin:0; font-size:.95rem;"><?= htmlspecialchars(dbnToolsT('upgrade_from_plus', $uiLang)) ?></p>
        </a>
        <?php endif; ?>
        <a id="corpusSummaryCard" class="status-card" href="/dashboard/" style="background:#fff; border:1px solid #e5e7eb; border-radius:10px; padding:1.3rem 1.5rem; text-decoration:none; color:inherit; display:block;">
            <p style="margin:0; color:#6b7280; font-size:.95rem; text-transform:uppercase; letter-spacing:.06em;"><?= htmlspecialchars(dbnToolsT('my_corpus', $uiLang)) ?></p>
            <p id="corpusDocCount" style="margin:.35rem 0 0; font-size:1.4rem; font-weight:700; color:#00205B;">—</p>
            <p id="corpusUpdated" style="margin:0; color:#6b7280; font-size:.95rem;"><?= htmlspecialchars(dbnToolsT('open_corpus', $uiLang)) ?> →</p>
        </a>
        <?php if ($showSurveyCta): ?>
        <a class="status-card" href="https://dobetternorge.no/survey.php" style="background:#fef3c7; color:#92400e; border-radius:10px; padding:1.3rem 1.5rem; text-decoration:none;">
            <p style="margin:0; font-size:.95rem; text-transform:uppercase; letter-spacing:.06em; opacity:.85;"><?= htmlspecialchars(dbnToolsT('earn_credits_eyebrow', $uiLang)) ?></p>
            <p style="margin:.35rem 0 0; font-size:1.2rem; font-weight:700;"><?= htmlspecialchars(dbnToolsT('survey_btn', $uiLang)) ?> →</p>
            <p style="margin:0; font-size:.95rem; opacity:.85;"><?= htmlspecialchars(dbnToolsT('survey_cta_text', $uiLang)) ?></p>
        </a>
        <?php endif; ?>
    </section>

    <?php endif; ?>

    <div class="disclaimer" role="note"><?= htmlspecialchars(dbnToolsT('disclaimer', $uiLang)) ?></div>

    <!-- ── Tool cards grid ──────────────────────────────────────────── -->
    <section class="tool-dashboard-grid" aria-label="Available tools">

        <?php /* Workbench card */ ?>
        <?php $wbUrl = htmlspecialchars($workbench['url']); ?>
        <div class="dashboard-tool-card dashboard-tool-card--workbench"
             tabindex="0" role="link"
             onclick="location.href='<?= $wbUrl ?>'"
             onkeydown="if(event.key==='Enter'||event.key===' ')location.href='<?= $wbUrl ?>'">
            <div style="display:flex; align-items:center; gap:.55rem; margin-bottom:.55rem; flex-wrap:wrap;">
                <span style="font-size:1.7rem; line-height:1; flex-shrink:0;" aria-hidden="true">🗂️</span>
                <span style="font-size:1.08rem; font-weight:700; color:#111827; flex:1; min-width:0;"><?= htmlspecialchars($workbench['label']) ?></span>
                <code onclick="event.stopPropagation();" data-copy-slug="dbn.case_workbench_plan"
                      title="<?= htmlspecialchars($dl['mcp_copy_slug']) ?>"
                      style="font-size:.78rem; background:#f1f5f9; border:1px solid #e2e8f0; color:#64748b; padding:3px 9px; border-radius:4px; cursor:pointer; white-space:nowrap; flex-shrink:0;">dbn.case_workbench_plan</code>
            </div>
            <p style="margin:0; font-size:.98rem; color:#4b5563; line-height:1.55;"><?= htmlspecialchars($workbench['description']) ?></p>
            <div class="dash-card-footer">
                <a href="<?= $wbUrl ?>" onclick="event.stopPropagation();"
                   style="margin-left:auto; color:#00205B; font-size:.94rem; font-weight:700; text-decoration:none; white-space:nowrap;">
                    <?= htmlspecialchars(dbnToolsT('enter_workbench', $uiLang)) ?> →
                </a>
            </div>
        </div>

        <?php foreach ($tools as $slug => $item):
            $mcpSlug = $toolMcpSlugs[$slug] ?? null;
            $docs    = $toolDocLinks[$slug] ?? null;
            $emoji   = $toolEmoji[$slug] ?? '🛠️';
            $cardUrl = htmlspecialchars($item['url']);
        ?>
        <div class="dashboard-tool-card"
             tabindex="0" role="link"
             onclick="location.href='<?= $cardUrl ?>'"
             onkeydown="if(event.key==='Enter'||event.key===' ')location.href='<?= $cardUrl ?>'">
            <div style="display:flex; align-items:center; gap:.55rem; margin-bottom:.55rem; flex-wrap:wrap;">
                <span style="font-size:1.7rem; line-height:1; flex-shrink:0;" aria-hidden="true"><?= $emoji ?></span>
                <span style="font-size:1.08rem; font-weight:700; color:#111827; flex:1; min-width:0;"><?= htmlspecialchars($item['label']) ?></span>
                <?php if ($mcpSlug): ?>
                <code onclick="event.stopPropagation();" data-copy-slug="<?= htmlspecialchars($mcpSlug) ?>"
                      title="<?= htmlspecialchars($dl['mcp_copy_slug']) ?>"
                      style="font-size:.78rem; background:#f1f5f9; border:1px solid #e2e8f0; color:#64748b; padding:3px 9px; border-radius:4px; cursor:pointer; white-space:nowrap; flex-shrink:0;"><?= htmlspecialchars($mcpSlug) ?></code>
                <?php endif; ?>
            </div>
            <p style="margin:0; font-size:.98rem; color:#4b5563; line-height:1.55;"><?= htmlspecialchars($item['description']) ?></p>
            <div class="dash-card-footer">
                <a href="/preview.php?tool=<?= htmlspecialchars($slug) ?><?= $uiLang !== 'en' ? '&amp;lang=' . urlencode($uiLang) : '' ?>" onclick="event.stopPropagation();"
                   style="color:#374151; font-size:.88rem; text-decoration:none; white-space:nowrap;"><?= htmlspecialchars($dl['tool_info']) ?></a>
                <?php if ($docs): ?>
                <span style="color:#d1d5db;" aria-hidden="true">·</span>
                <a href="<?= htmlspecialchars($docs[0] . $langSuffix) ?>" onclick="event.stopPropagation();"
                   style="color:#374151; font-size:.88rem; text-decoration:none; white-space:nowrap;"><?= htmlspecialchars($dl['about_link']) ?></a>
                <span style="color:#d1d5db;" aria-hidden="true">·</span>
                <a href="<?= htmlspecialchars($docs[1] . $langSuffix) ?>" onclick="event.stopPropagation();"
                   style="color:#374151; font-size:.88rem; text-decoration:none; white-space:nowrap;"><?= htmlspecialchars($dl['guide_link']) ?></a>
                <span style="color:#d1d5db;" aria-hidden="true">·</span>
                <a href="<?= htmlspecialchars($docs[2] . $langSuffix) ?>" onclick="event.stopPropagation();"
                   style="color:#374151; font-size:.88rem; text-decoration:none; white-space:nowrap;"><?= htmlspecialchars($dl['tech_link']) ?></a>
                <?php endif; ?>
                <a href="<?= $cardUrl ?>" onclick="event.stopPropagation();"
                   style="margin-left:auto; color:#00205B; font-size:.94rem; font-weight:700; text-decoration:none; white-space:nowrap;"><?= htmlspecialchars($dl['open_tool']) ?> →</a>
            </div>
        </div>
        <?php endforeach; ?>

    </section>

    <!-- ── MCP quick-start (collapsed) ─────────────────────────────── -->
    <details class="dash-mcp-details" style="background:#fff; border:1px solid #e5e7eb; border-radius:12px; margin:1.5rem 0; overflow:hidden;">
        <summary style="display:flex; align-items:center; gap:.75rem; padding:1rem 1.5rem; cursor:pointer; list-style:none; font-weight:600; font-size:.95rem; color:#111827; user-select:none;">
            <span aria-hidden="true" style="font-size:1.1rem;">⚙️</span>
            <?= htmlspecialchars($dl['mcp_section']) ?>
            <span style="color:#6b7280; font-weight:400; font-size:.82rem; margin-left:.25rem; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">— <?= htmlspecialchars($dl['mcp_desc']) ?></span>
            <span class="dash-chevron" aria-hidden="true" style="margin-left:auto; color:#9ca3af; font-size:.75rem; flex-shrink:0;">▼</span>
        </summary>

        <div style="padding:0 1.5rem 1.5rem; border-top:1px solid #f3f4f6;">

            <?php if ($dashIsSso && in_array($dashTier, ['plus','pro'], true)): ?>
            <!-- Token prefix row -->
            <div style="display:flex; align-items:center; gap:.75rem; flex-wrap:wrap; margin:.9rem 0; padding:.7rem 1rem; background:#f8fafc; border:1px solid #e2e8f0; border-radius:8px;">
                <span style="font-size:.82rem; font-weight:600; color:#374151; white-space:nowrap;"><?= htmlspecialchars($dl['mcp_token_lbl']) ?>:</span>
                <code id="dashMcpTokenPrefix" style="font-size:.82rem; color:#111827; flex:1; min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;"><?= $uiLang === 'no' ? 'Laster…' : 'Loading…' ?></code>
                <a href="/mcp.php<?= $langSuffix ?>" onclick="event.stopPropagation();" style="font-size:.78rem; color:#00205B; font-weight:600; text-decoration:none; white-space:nowrap; flex-shrink:0;"><?= $uiLang === 'no' ? 'Administrer tokens →' : 'Manage tokens →' ?></a>
            </div>
            <?php elseif ($dashIsSso): ?>
            <!-- Upgrade prompt -->
            <div style="display:flex; align-items:center; justify-content:space-between; gap:.75rem; flex-wrap:wrap; margin:.9rem 0; padding:.7rem 1rem; background:#fefce8; border:1px solid #fef08a; border-radius:8px;">
                <span style="font-size:.82rem; color:#713f12;"><?= htmlspecialchars($dl['mcp_not_avail']) ?></span>
                <a href="/pricing.php" style="font-size:.8rem; background:#00205B; color:#fff; font-weight:600; text-decoration:none; padding:4px 12px; border-radius:6px; white-space:nowrap; flex-shrink:0;"><?= htmlspecialchars($dl['upgrade_plan']) ?> →</a>
            </div>
            <?php endif; ?>

            <!-- stdio config -->
            <p style="margin:1.1rem 0 .35rem; font-size:.82rem; font-weight:600; color:#374151;"><?= htmlspecialchars($dl['mcp_stdio_lbl']) ?></p>
            <div style="position:relative;">
                <pre id="dashStdioBlock" style="background:#101828; color:#f9fafb; padding:.85rem 3.5rem .85rem 1rem; border-radius:8px; font-size:.76rem; overflow-x:auto; margin:0; line-height:1.75; white-space:pre;">claude mcp add dobetternorge -- npx -y @bluenotelogic/mcp dobetternorge-mcp --stdio

# Set token (add to ~/.bashrc or ~/.zshrc to persist):
export DBN_MCP_TOKEN=<span id="dashStdioToken" style="color:#86efac;">dbn_user_mcp_…</span></pre>
                <button onclick="copyDashBlock('dashStdioBlock', this)" style="position:absolute; top:.5rem; right:.5rem; background:rgba(249,250,251,.12); border:1px solid rgba(249,250,251,.2); color:#f9fafb; font-size:.72rem; padding:3px 8px; border-radius:5px; cursor:pointer;"><?= htmlspecialchars($dl['mcp_copy']) ?></button>
            </div>

            <!-- Remote HTTP -->
            <p style="margin:1.1rem 0 .35rem; font-size:.82rem; font-weight:600; color:#374151;"><?= htmlspecialchars($dl['mcp_remote_lbl']) ?></p>
            <div style="position:relative;">
                <pre style="background:#101828; color:#f9fafb; padding:.85rem 3.5rem .85rem 1rem; border-radius:8px; font-size:.76rem; overflow-x:auto; margin:0; line-height:1.75; white-space:pre;">URL:           https://mcp.dobetternorge.no/mcp
Authorization: Bearer <span id="dashRemoteToken" style="color:#86efac;">dbn_user_mcp_…</span></pre>
                <button onclick="copyDashRemote(this)" style="position:absolute; top:.5rem; right:.5rem; background:rgba(249,250,251,.12); border:1px solid rgba(249,250,251,.2); color:#f9fafb; font-size:.72rem; padding:3px 8px; border-radius:5px; cursor:pointer;"><?= htmlspecialchars($dl['mcp_copy']) ?></button>
            </div>

            <p style="margin:1rem 0 0; font-size:.85rem;">
                <a href="/mcp.php<?= $langSuffix ?>" style="color:#00205B; font-weight:600;"><?= htmlspecialchars($dl['mcp_full_docs']) ?></a>
            </p>
        </div>
    </details>

    <!-- ── Welcome modal ────────────────────────────────────────────── -->
    <div id="welcomeModal" class="wlc-backdrop" role="dialog" aria-modal="true" aria-labelledby="wlcTitle" hidden>
        <div class="wlc-card">
            <div class="wlc-header">
                <p class="wlc-eyebrow"><?= htmlspecialchars(dbnToolsT('brand_line', $uiLang)) ?></p>
                <h2 class="wlc-title" id="wlcTitle">Welcome to Do Better Norge Legal Tools</h2>
                <p class="wlc-sub">Here&rsquo;s a quick look at what each tool does &mdash; click any card on the dashboard to go straight in.</p>
            </div>
            <div class="wlc-workbench-tip">
                <span class="wlc-tip-icon" aria-hidden="true">💡</span>
                <span><strong>Start with the Case Workbench</strong> &mdash; frame your situation first, then use the tools below with context already loaded.</span>
            </div>
            <div class="wlc-tools-grid">
<?php
$welcomeTips = [
    'transcribe'    => 'Upload a hearing recording to get an accurate, speaker-separated transcript',
    'timeline'      => 'Paste case notes to instantly map all key dates and Barnevernet milestones',
    'redact'        => 'Strip names and ID numbers before sharing any document with third parties',
    'korrespond'    => 'Draft authority letters in Norwegian + your language with verified citations',
    'barnevernet'   => 'Upload child-welfare documents to flag procedural violations and red flags',
    'advocate'      => 'Generate a fully-cited brief for your position from ECHR and Lovdata',
    'deep-research' => 'Ask a complex legal question and get a multi-angle cited research brief',
    'discrepancy'   => 'Upload two doc versions to surface deleted facts or new allegations',
    'corpus'        => 'Browse the 220 K+ indexed legal passages behind every AI answer',
    'citations'     => 'Trace how cases cite each other to find supporting precedents',
];
$welcomeIcons = [
    'transcribe'    => '🎙',
    'timeline'      => '📅',
    'redact'        => '🔒',
    'korrespond'    => '✉️',
    'barnevernet'   => '🔍',
    'advocate'      => '⚖️',
    'deep-research' => '🔬',
    'discrepancy'   => '🔎',
    'corpus'        => '📚',
    'citations'     => '🔗',
];
foreach ($tools as $slug => $item):
    $tip  = $welcomeTips[$slug] ?? $item['description'];
    $icon = $welcomeIcons[$slug] ?? '🛠';
?>
                <a class="wlc-tool-item" href="<?= htmlspecialchars($item['url']) ?>">
                    <span class="wlc-tool-icon" aria-hidden="true"><?= $icon ?></span>
                    <span class="wlc-tool-name"><?= htmlspecialchars($item['label']) ?></span>
                    <span class="wlc-tool-tip"><?= htmlspecialchars($tip) ?></span>
                </a>
<?php endforeach; ?>
            </div>
            <div class="wlc-footer">
                <label class="wlc-no-show">
                    <input type="checkbox" id="wlcDontShow" checked>
                    Don&rsquo;t show this again
                </label>
                <button id="wlcGetStarted" class="wlc-btn-start" type="button">Get started &rarr;</button>
            </div>
        </div>
    </div>

    <?php if ($dashIsSso && $dashProfileNeedsPrompt): ?>
    <div id="profilePromptModal" class="profile-prompt-backdrop" role="dialog" aria-modal="true" aria-labelledby="profilePromptTitle">
        <div class="profile-prompt-card">
            <p class="dash-section-kicker">Optional profile</p>
            <h2 id="profilePromptTitle">Add contact details for your tools account</h2>
            <p>These details help support and billing conversations. They are optional and never block tool access.</p>
            <form id="profilePromptForm" class="dash-profile-form">
                <label>
                    <span>Display name</span>
                    <input name="display_name" type="text" maxlength="100" autocomplete="name" value="<?= htmlspecialchars($dashProfileValue('display_name')) ?>">
                </label>
                <label>
                    <span>Phone</span>
                    <input name="phone" type="tel" maxlength="40" autocomplete="tel" value="<?= htmlspecialchars($dashProfileValue('phone')) ?>">
                </label>
                <label class="span-2">
                    <span>Address line 1</span>
                    <input name="address_line1" type="text" maxlength="180" autocomplete="address-line1" value="<?= htmlspecialchars($dashProfileValue('address_line1')) ?>">
                </label>
                <label>
                    <span>Postal code</span>
                    <input name="postal_code" type="text" maxlength="32" autocomplete="postal-code" value="<?= htmlspecialchars($dashProfileValue('postal_code')) ?>">
                </label>
                <label>
                    <span>City</span>
                    <input name="city" type="text" maxlength="100" autocomplete="address-level2" value="<?= htmlspecialchars($dashProfileValue('city')) ?>">
                </label>
                <label>
                    <span>Country</span>
                    <input name="country" type="text" maxlength="2" autocomplete="country" placeholder="NO" value="<?= htmlspecialchars($dashProfileValue('country')) ?>">
                </label>
                <input type="hidden" name="preferred_language" value="<?= htmlspecialchars($uiLang) ?>">
                <div class="profile-prompt-actions span-2">
                    <button type="button" id="profilePromptSkip">Skip for now</button>
                    <button type="submit">Save details</button>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

</main>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
<script src="assets/js/tools.js" defer></script>
<script>
/* ── Welcome modal ──────────────────────────────────────────────────── */
(function () {
    var STORAGE_KEY = 'dbn-welcome-v1-seen';
    var modal       = document.getElementById('welcomeModal');
    var btnStart    = document.getElementById('wlcGetStarted');
    var chkDontShow = document.getElementById('wlcDontShow');

    function closeModal(saveFlag) {
        modal.hidden = true;
        document.body.style.overflow = '';
        if (saveFlag) { try { localStorage.setItem(STORAGE_KEY, '1'); } catch (e) {} }
    }

    if (modal && !localStorage.getItem(STORAGE_KEY)) {
        modal.hidden = false;
        document.body.style.overflow = 'hidden';
    }
    if (btnStart) {
        btnStart.addEventListener('click', function () { closeModal(chkDontShow && chkDontShow.checked); });
    }
    if (modal) {
        modal.addEventListener('click', function (e) { if (e.target === modal) closeModal(false); });
        document.addEventListener('keydown', function (e) { if (e.key === 'Escape' && !modal.hidden) closeModal(false); });
    }
}());

/* Profile details */
(function () {
    function payloadFromForm(form, dismiss) {
        var data = {};
        if (form) {
            Array.prototype.forEach.call(form.elements, function (el) {
                if (!el.name) return;
                data[el.name] = el.value || '';
            });
        }
        if (dismiss) data.dismiss_prompt = true;
        return data;
    }

    function saveProfile(payload) {
        return fetch('/api/profile.php', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'same-origin',
            body: JSON.stringify(payload)
        }).then(function (r) {
            return r.json().then(function (data) {
                if (!r.ok || !data.ok) {
                    throw new Error(data.error && data.error.message ? data.error.message : 'Could not save profile');
                }
                return data;
            });
        });
    }

    var profileForm = document.getElementById('profileForm');
    var profileStatus = document.getElementById('profileStatus');
    if (profileForm) {
        profileForm.addEventListener('submit', function (e) {
            e.preventDefault();
            if (profileStatus) profileStatus.textContent = 'Saving...';
            saveProfile(payloadFromForm(profileForm, true))
                .then(function () { if (profileStatus) profileStatus.textContent = 'Saved'; })
                .catch(function (err) { if (profileStatus) profileStatus.textContent = err.message; });
        });
    }

    var promptModal = document.getElementById('profilePromptModal');
    var promptForm = document.getElementById('profilePromptForm');
    var promptSkip = document.getElementById('profilePromptSkip');
    function closePrompt() {
        if (promptModal) promptModal.hidden = true;
        document.body.style.overflow = '';
    }
    if (promptModal) document.body.style.overflow = 'hidden';
    if (promptForm) {
        promptForm.addEventListener('submit', function (e) {
            e.preventDefault();
            saveProfile(payloadFromForm(promptForm, true)).then(closePrompt).catch(function (err) { alert(err.message); });
        });
    }
    if (promptSkip) {
        promptSkip.addEventListener('click', function () {
            saveProfile(payloadFromForm(promptForm, true)).then(closePrompt).catch(closePrompt);
        });
    }
}());

/* ── Corpus summary ─────────────────────────────────────────────────── */
<?php if ($dashIsSso): ?>
(function () {
    var card    = document.getElementById('corpusSummaryCard');
    var countEl = document.getElementById('corpusDocCount');
    var updEl   = document.getElementById('corpusUpdated');
    if (!card || !countEl) return;
    fetch('/api/corpus-summary.php')
        .then(function (r) { return r.ok ? r.json() : null; })
        .then(function (data) {
            if (!data) return;
            countEl.textContent = data.doc_count !== undefined
                ? data.doc_count.toLocaleString() + ' docs' : '—';
            if (data.last_updated && updEl) {
                var d = new Date(data.last_updated);
                updEl.textContent = d.toLocaleDateString(undefined, { day: 'numeric', month: 'short', year: 'numeric' });
            }
        })
        .catch(function () {});
}());
<?php endif; ?>

/* ── MCP section ────────────────────────────────────────────────────── */
(function () {
    /* chevron toggle */
    var det = document.querySelector('details.dash-mcp-details');
    if (det) {
        det.addEventListener('toggle', function () {
            var chev = det.querySelector('.dash-chevron');
            if (chev) chev.style.transform = det.open ? 'rotate(180deg)' : '';
        });
    }

    <?php if ($dashIsSso && in_array($dashTier, ['plus','pro'], true)): ?>
    /* load token prefix */
    fetch('/api/mcp-tokens.php', { credentials: 'same-origin' })
        .then(function (r) { return r.ok ? r.json() : null; })
        .then(function (data) {
            if (!data || !data.ok) return;
            var active = (data.tokens || []).filter(function (t) { return t.is_active; });
            var prefixEl = document.getElementById('dashMcpTokenPrefix');
            if (prefixEl) {
                prefixEl.textContent = active.length > 0
                    ? active[0].token_prefix + '…'
                    : <?= json_encode($dl['mcp_no_token'], JSON_UNESCAPED_UNICODE) ?>;
            }
            if (active.length > 0) {
                var p = active[0].token_prefix + '…';
                var s = document.getElementById('dashStdioToken');
                var r = document.getElementById('dashRemoteToken');
                if (s) s.textContent = p;
                if (r) r.textContent = p;
            }
        })
        .catch(function () {});
    <?php endif; ?>

    /* copy a pre block */
    window.copyDashBlock = function (id, btn) {
        var el = document.getElementById(id);
        if (!el) return;
        navigator.clipboard.writeText(el.innerText || el.textContent).then(function () {
            var orig = btn.textContent; btn.textContent = '✓';
            setTimeout(function () { btn.textContent = orig; }, 1500);
        }).catch(function () {});
    };

    /* copy remote HTTP config */
    window.copyDashRemote = function (btn) {
        var tok = document.getElementById('dashRemoteToken');
        var txt = 'URL:           https://mcp.dobetternorge.no/mcp\nAuthorization: Bearer ' + (tok ? tok.textContent : 'dbn_user_mcp_…');
        navigator.clipboard.writeText(txt).then(function () {
            var orig = btn.textContent; btn.textContent = '✓';
            setTimeout(function () { btn.textContent = orig; }, 1500);
        }).catch(function () {});
    };

    /* copy MCP slug from tool card button */
    window.copyDashSlug = function (btn) {
        var slug = btn.getAttribute('data-slug');
        if (!slug) return;
        navigator.clipboard.writeText(slug).then(function () {
            var orig = btn.textContent;
            btn.textContent = '✓ copied';
            btn.style.cssText += '; color:#065f46 !important; background:#d1fae5 !important; border-color:#6ee7b7 !important;';
            setTimeout(function () {
                btn.textContent = orig;
                btn.style.color = '';
                btn.style.background = '';
                btn.style.borderColor = '';
            }, 1500);
        }).catch(function () {});
    };

    /* copy MCP slug from tool cards */
    document.querySelectorAll('[data-copy-slug]').forEach(function (el) {
        el.addEventListener('click', function (e) {
            e.stopPropagation();
            var slug = el.getAttribute('data-copy-slug');
            if (!slug) return;
            navigator.clipboard.writeText(slug).then(function () {
                var orig = el.textContent; el.textContent = '✓';
                setTimeout(function () { el.textContent = orig; }, 1200);
            }).catch(function () {});
        });
    });
}());
</script>
</body>
</html>
