        </section><!-- /tool-panel -->

        <aside class="reasoning-panel" aria-labelledby="reasoningTitle">
<?php if (!empty($reasoningPanelOverride)): ?>
<?= $reasoningPanelOverride ?>
<?php else: ?>
            <div class="reasoning-head">
                <p class="eyebrow"><?= htmlspecialchars(dbnToolsT('reasoning_eyebrow', $uiLang ?? dbnToolsCurrentLanguage())) ?></p>
                <h2 id="reasoningTitle"><?= htmlspecialchars(dbnToolsT('reasoning_title', $uiLang ?? dbnToolsCurrentLanguage())) ?></h2>
            </div>
            <ol id="traceList" class="trace-list">
                <li>
                    <span class="trace-status waiting"></span>
                    <div>
                        <strong><?= htmlspecialchars(dbnToolsT('waiting_title', $uiLang ?? dbnToolsCurrentLanguage())) ?></strong>
                        <p><?= htmlspecialchars(dbnToolsT('waiting_text', $uiLang ?? dbnToolsCurrentLanguage())) ?></p>
                    </div>
                </li>
            </ol>
<?php endif; ?>
        </aside>
    </section><!-- /workspace -->
</main><!-- /appShell -->
<?php require_once __DIR__ . '/footer.php'; ?>
<link rel="stylesheet" href="assets/css/doc-picker.css">
<?php
// Expose current UI language + add-on i18n map for tools.js (legal-analysis add-on
// can fire from any tool page, so it needs strings without depending on per-tool maps).
$_footerLang = $uiLang ?? dbnToolsCurrentLanguage();
$_footerAddonI18n = [
    'addonButton'      => dbnToolsT('la_addon_button',      $_footerLang),
    'addonButtonBusy'  => dbnToolsT('la_addon_button_busy', $_footerLang),
    'addonSection'     => dbnToolsT('la_addon_section',     $_footerLang),
    'pass1'            => dbnToolsT('la_pipeline_pass1',    $_footerLang),
    'pass2'            => dbnToolsT('la_pipeline_pass2',    $_footerLang),
    'pass3'            => dbnToolsT('la_pipeline_pass3',    $_footerLang),
    'pass1Extracting'  => dbnToolsT('la_pass1_extracting',  $_footerLang),
    'pass1Found'       => dbnToolsT('la_pass1_found',       $_footerLang),
    'pass2Asking'      => dbnToolsT('la_pass2_asking',      $_footerLang),
    'pass2Answered'    => dbnToolsT('la_pass2_answered',    $_footerLang),
    'pass3Synthesis'   => dbnToolsT('la_pass3_synthesis',   $_footerLang),
    'waiting'          => dbnToolsT('la_waiting',           $_footerLang),
    'searchingCorpus'  => dbnToolsT('la_searching_corpus',  $_footerLang),
    'askingFinetune'   => dbnToolsT('la_asking_finetune',   $_footerLang),
    'overall'          => dbnToolsT('la_overall',           $_footerLang),
    'nextSteps'        => dbnToolsT('la_next_steps',        $_footerLang),
    'answerHeader'     => dbnToolsT('la_answer_header',     $_footerLang),
    'legalBasis'       => dbnToolsT('la_legal_basis',       $_footerLang),
    'errorPrefix'      => dbnToolsT('la_error_prefix',      $_footerLang),
    'serverReturned'   => dbnToolsT('la_server_returned',   $_footerLang),
];
?>
<script>
    window.DBN_CURRENT_LANG  = <?= json_encode($_footerLang) ?>;
    window.DBN_ADDON_I18N    = <?= json_encode($_footerAddonI18n, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
</script>
<script src="assets/js/tools.js" defer></script>
<?php if (!empty($extraScripts) && is_array($extraScripts)): foreach ($extraScripts as $extraScript): ?>
<script src="<?= htmlspecialchars((string)$extraScript) ?>" defer></script>
<?php endforeach; endif; ?>
<script src="assets/js/corpus-save.js" defer></script>
<script src="assets/js/doc-picker.js" defer></script>

<!-- Doc picker modal (shared across all tool pages) -->
<div id="docPickerBackdrop" class="doc-picker-backdrop" hidden role="dialog" aria-modal="true" aria-labelledby="docPickerTitle">
    <div class="doc-picker-dialog">
        <div class="doc-picker-dialog__head">
            <h3 id="docPickerTitle"><?= htmlspecialchars(dbnToolsT('doc_picker_modal_title', $uiLang)) ?></h3>
            <button class="doc-picker-dialog__close" aria-label="Close">&times;</button>
        </div>
        <input type="search" class="doc-picker-dialog__search" placeholder="Search documents…" aria-label="Search documents">
        <div class="doc-picker-list" role="listbox" aria-multiselectable="true">
            <p class="doc-picker-list__loading">Loading…</p>
        </div>
        <div class="doc-picker-dialog__foot">
            <span class="doc-picker-dialog__count"></span>
            <button class="doc-picker-dialog__confirm" disabled>Add to tool</button>
        </div>
    </div>
</div>

<!-- Upgrade modal for free-tier users -->
<div id="docPickerUpgradeBackdrop" class="doc-picker-upgrade-backdrop" hidden role="dialog" aria-modal="true" aria-labelledby="docPickerUpgradeTitle">
    <div class="doc-picker-upgrade-card">
        <span class="doc-picker-upgrade-card__icon">📂</span>
        <h3 id="docPickerUpgradeTitle">Plus &amp; Pro feature</h3>
        <p>Select documents from your uploaded corpus and feed them directly into any tool. Available on Plus and Pro plans.</p>
        <div class="doc-picker-upgrade-card__actions">
            <a href="/pricing.php" class="doc-picker-upgrade-card__cta">View plans</a>
            <button class="doc-picker-upgrade-card__dismiss">Maybe later</button>
        </div>
    </div>
</div>

<!-- Save-to-corpus dialog (shared across all tool pages) -->
<dialog id="save-corpus-dialog" class="save-corpus-dialog">
  <form method="dialog" id="save-corpus-form">
    <h3>Save to corpus</h3>
    <p class="save-corpus-hint">This will be indexed and searchable in your private corpus.</p>
    <label>
      <span>Title <span aria-hidden="true">*</span></span>
      <input id="save-corpus-title" type="text" required placeholder="Give this entry a title…" autocomplete="off">
    </label>
    <label>
      <span>Tags <span class="save-corpus-optional">(comma-separated)</span></span>
      <input id="save-corpus-tags" type="text" placeholder="e.g. barnevern, 2024, kjennelse">
    </label>
    <menu>
      <button type="submit" class="btn-primary">Save to corpus</button>
      <button type="button" id="save-corpus-cancel">Cancel</button>
    </menu>
  </form>
</dialog>

<?php if (!empty($layoutIsGuest)): ?>
<!-- Auth gate modal — shown to unauthenticated visitors unless they dismissed it -->
<div id="authGate" class="auth-gate-backdrop" hidden aria-modal="true" role="dialog" aria-labelledby="authGateTitle">
    <div class="auth-gate-card">
        <p class="auth-gate-eyebrow">Do Better Norge</p>
        <h2 id="authGateTitle" class="auth-gate-title">Logg inn for å bruke verktøyene</h2>
        <p class="auth-gate-body">Våre juridiske AI-verktøy krever en gratis konto. Registrer deg på sekunder med Google.</p>
        <div class="auth-gate-actions">
            <a id="authGateLogin" href="/?return=<?= htmlspecialchars($layoutReturnUrl ?? '') ?>" class="auth-gate-btn auth-gate-btn--primary">Logg inn / Registrer deg</a>
            <button id="authGateDismiss" class="auth-gate-btn auth-gate-btn--ghost">Fortsett uten konto</button>
        </div>
        <p class="auth-gate-note">Gratis tilgang · Ingen kredittkort</p>
    </div>
</div>
<script>
(function () {
    'use strict';
    var GATE_KEY   = 'dbn-auth-gate-v1';
    var BANNER_KEY = 'dbn-guest-banner-v1';
    var gate       = document.getElementById('authGate');
    var banner     = document.getElementById('guestBanner');
    var bannerClose = document.getElementById('guestBannerClose');

    // Show or hide the guest banner
    if (banner) {
        if (localStorage.getItem(BANNER_KEY) === 'closed') {
            banner.hidden = true;
        }
        if (bannerClose) {
            bannerClose.addEventListener('click', function () {
                localStorage.setItem(BANNER_KEY, 'closed');
                banner.hidden = true;
            });
        }
    }

    // Show auth gate modal unless already dismissed
    if (gate && localStorage.getItem(GATE_KEY) !== 'dismissed') {
        gate.hidden = false;
        document.getElementById('authGateDismiss').addEventListener('click', function () {
            localStorage.setItem(GATE_KEY, 'dismissed');
            gate.hidden = true;
        });
        // Close on backdrop click
        gate.addEventListener('click', function (e) {
            if (e.target === gate) {
                localStorage.setItem(GATE_KEY, 'dismissed');
                gate.hidden = true;
            }
        });
    }
}());
</script>
<?php endif; ?>
</body>
</html>
