<!DOCTYPE html>
<html lang="en" class="h-full">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% block title %}Monitoring{% endblock %} · TMI</title>

    <!-- apply saved theme before paint (no flash); light is the default -->
    <script>(function(){var t=localStorage.getItem('portal-theme')||'light';document.documentElement.setAttribute('data-theme',t);})();</script>

    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Hanken+Grotesk:wght@400;500;600;700;800&family=IBM+Plex+Mono:wght@400;500;600&display=swap" rel="stylesheet">

    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: { extend: {
                colors: { seismo: { orange: '#f48b1c', navy: '#142a66', burgundy: '#7d234d' } },
                fontFamily: {
                    sans: ['"Hanken Grotesk"', 'ui-sans-serif', 'system-ui', 'sans-serif'],
                    mono: ['"IBM Plex Mono"', 'ui-monospace', 'monospace'],
                },
            } }
        }
    </script>
    <link rel="icon" type="image/png" sizes="32x32" href="/static/icons/favicon-32.png">
    <meta name="theme-color" content="#eef2f9">

    <style>
        /* ---- dark (default) ---- */
        :root {
            --bg: #080b14;
            --grid: rgba(124, 146, 188, 0.045);
            --aurora-1: rgba(20, 42, 102, 0.55);
            --aurora-2: rgba(125, 35, 77, 0.18);
            --text: #e7ecf6;
            --text-dim: #8c98b0;
            --border: rgba(124, 146, 188, 0.14);
            --border-bright: rgba(168, 188, 224, 0.30);
            --panel-a: rgba(24, 33, 54, 0.72);
            --panel-b: rgba(12, 18, 31, 0.62);
            --panel-inset: rgba(255, 255, 255, 0.05);
            --panel-shadow: 0 22px 48px -28px rgba(0, 0, 0, 0.85);
            --header-bg: rgba(8, 11, 20, 0.72);
            --accent: #f48b1c;
            --accent-glow: rgba(244, 139, 28, 0.40);
            --lvl-ok: #34d399; --lvl-warn: #fbbf24; --lvl-bad: #f87171;
            --m-lp: #60a5fa; --m-lmax: #f87171; --m-l1: #c084fc; --m-l10: #fbbf24;
        }
        /* ---- light (cool) — solid cards on a cool ground ---- */
        html[data-theme="light"] {
            --bg: #eef2f9;                               /* cool light */
            --grid: rgba(20, 42, 102, 0.05);             /* cool faint grid */
            --aurora-1: rgba(120, 150, 220, 0.18);       /* cool wash */
            --aurora-2: rgba(244, 139, 28, 0.08);        /* faint brand accent */
            --text: #16203a;                             /* cool navy ink */
            --text-dim: #5d6b86;                         /* cool muted */
            --border: rgba(20, 42, 102, 0.13);
            --border-bright: rgba(20, 42, 102, 0.18);
            --panel-a: #ffffff;                          /* solid — kept from the un-ghosting pass */
            --panel-b: #f7f9fc;
            --panel-inset: rgba(255, 255, 255, 0.9);
            --panel-shadow: 0 14px 30px -16px rgba(40, 55, 95, 0.22), 0 2px 6px -2px rgba(40, 55, 95, 0.07);
            --header-bg: rgba(238, 242, 249, 0.85);
            --lvl-ok: #16a34a; --lvl-warn: #d97706; --lvl-bad: #dc2626;
            --m-lp: #2563eb; --m-lmax: #dc2626; --m-l1: #9333ea; --m-l10: #d97706;
        }
        /* On light, the hover-lift shadow wants cool depth (the dark one vanishes on light). */
        html[data-theme="light"] .panel-hover:hover {
            box-shadow: 0 22px 44px -20px rgba(40, 55, 95, 0.26), 0 0 0 1px var(--accent-glow);
        }

        html, body { height: 100%; }
        body {
            margin: 0;
            color: var(--text);
            font-family: "Hanken Grotesk", ui-sans-serif, system-ui, sans-serif;
            font-feature-settings: "ss01";
            background-color: var(--bg);
            background-image:
                radial-gradient(1100px 560px at 50% -12%, var(--aurora-1), transparent 68%),
                radial-gradient(700px 400px at 88% 8%, var(--aurora-2), transparent 70%),
                linear-gradient(var(--grid) 1px, transparent 1px),
                linear-gradient(90deg, var(--grid) 1px, transparent 1px);
            background-size: auto, auto, 46px 46px, 46px 46px;
            background-attachment: fixed;
            transition: background-color .3s ease, color .3s ease;
        }
        ::selection { background: rgba(244, 139, 28, 0.30); }

        .font-mono, .reading { font-family: "IBM Plex Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }

        .panel {
            position: relative;
            background: linear-gradient(180deg, var(--panel-a), var(--panel-b));
            border: 1px solid var(--border);
            border-radius: 16px;
            backdrop-filter: blur(8px);
            -webkit-backdrop-filter: blur(8px);
            box-shadow: 0 1px 0 var(--panel-inset) inset, var(--panel-shadow);
        }
        .panel::before {
            content: ''; position: absolute; inset: 0 0 auto 0; height: 1px;
            background: linear-gradient(90deg, transparent, var(--border-bright), transparent);
        }
        .panel-hover { transition: transform .22s ease, border-color .22s ease, box-shadow .22s ease; }
        .panel-hover:hover {
            transform: translateY(-3px);
            border-color: rgba(244, 139, 28, 0.55);
            box-shadow: 0 30px 60px -30px rgba(0, 0, 0, 0.55), 0 0 0 1px var(--accent-glow);
        }
        .hairline { border-top: 1px solid var(--border); }

        /* metric accent colors (flip per theme) */
        .c-lp { color: var(--m-lp); } .c-lmax { color: var(--m-lmax); }
        .c-l1 { color: var(--m-l1); } .c-l10 { color: var(--m-l10); }

        .live-dot { width: 8px; height: 8px; border-radius: 999px; background: var(--accent); box-shadow: 0 0 0 0 var(--accent-glow); animation: pulse 2.2s infinite; }
        @keyframes pulse { 0% { box-shadow: 0 0 0 0 var(--accent-glow); } 70% { box-shadow: 0 0 0 9px rgba(244, 139, 28, 0); } 100% { box-shadow: 0 0 0 0 rgba(244, 139, 28, 0); } }

        @keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
        .reveal { opacity: 0; animation: rise .55s cubic-bezier(.2, .7, .2, 1) forwards; }

        .signal-bars { display: inline-flex; align-items: flex-end; gap: 2px; height: 16px; }
        .signal-bars i { width: 3px; background: var(--accent); border-radius: 1px; animation: bars 1.4s ease-in-out infinite; }
        .signal-bars i:nth-child(1) { height: 40%; } .signal-bars i:nth-child(2) { height: 70%; animation-delay: .2s; }
        .signal-bars i:nth-child(3) { height: 100%; animation-delay: .4s; } .signal-bars i:nth-child(4) { height: 55%; animation-delay: .6s; }
        @keyframes bars { 0%, 100% { transform: scaleY(.5); opacity: .7; } 50% { transform: scaleY(1); opacity: 1; } }

        .theme-toggle { color: var(--text-dim); transition: color .2s ease, background .2s ease; }
        .theme-toggle:hover { color: var(--text); }
        html[data-theme="light"] .moon { display: none; }
        html[data-theme="dark"] .sun, :root:not([data-theme="light"]) .sun { display: none; }

        /* Leaflet polish (dark default; .leaflet-light tweaks tooltip for light) */
        .leaflet-container { background: var(--bg) !important; }
        .leaflet-tooltip { background: var(--panel-a); border: 1px solid var(--border-bright); color: var(--text); box-shadow: none; font-family: inherit; font-size: 12px; }
        .leaflet-tooltip-top::before { border-top-color: var(--border-bright); }
        .leaflet-control-attribution { background: rgba(0,0,0,0.25) !important; color: var(--text-dim) !important; }
        .leaflet-control-attribution a { color: var(--text-dim) !important; }
        ::-webkit-scrollbar { width: 9px; height: 9px; }
        ::-webkit-scrollbar-thumb { background: rgba(124, 146, 188, 0.22); border-radius: 9px; }
    </style>
    {% block head %}{% endblock %}
</head>
<body class="min-h-full antialiased">
    <header class="sticky top-0 z-30 border-b border-[var(--border)] bg-[var(--header-bg)] backdrop-blur-xl">
        <div class="max-w-5xl mx-auto px-5 py-3.5 flex items-center justify-between">
            <a href="/portal" class="flex items-center gap-2.5">
                <span class="signal-bars"><i></i><i></i><i></i><i></i></span>
                <span class="font-semibold tracking-tight text-[15px]">
                    TMI <span class="text-[var(--text-dim)] font-normal">Monitoring</span>
                    {% if client %}<span class="text-[var(--text-dim)] font-normal mx-0.5">/</span>
                    <span class="text-seismo-orange">{{ client.name }}</span>{% endif %}
                </span>
            </a>
            <div class="flex items-center gap-1.5">
                <button onclick="togglePortalTheme()" class="theme-toggle p-2 rounded-lg" title="Toggle light / dark" aria-label="Toggle theme">
                    <svg class="moon w-[18px] h-[18px]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"/></svg>
                    <svg class="sun w-[18px] h-[18px]" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 3v1m0 16v1m9-9h-1M4 12H3m15.364 6.364l-.707-.707M6.343 6.343l-.707-.707m12.728 0l-.707.707M6.343 17.657l-.707.707M16 12a4 4 0 11-8 0 4 4 0 018 0z"/></svg>
                </button>
                {% if client %}
                <a href="/portal/logout" class="text-[13px] text-[var(--text-dim)] hover:text-[var(--text)] transition-colors px-2">Sign out</a>
                {% endif %}
            </div>
        </div>
    </header>

    <main class="max-w-5xl mx-auto px-5 py-8">
        {% block content %}{% endblock %}
    </main>

    <footer class="max-w-5xl mx-auto px-5 py-10 text-[11px] text-[var(--text-dim)] flex items-center gap-2 opacity-70">
        <span class="w-1 h-1 rounded-full bg-[var(--text-dim)]"></span>
        Read-only monitoring view · data provided as-is for informational purposes
    </footer>

    <script>
        // Theme toggle. Pages can listen for 'portal-theme' to re-skin canvases/maps.
        function cssVar(n) { return getComputedStyle(document.documentElement).getPropertyValue(n).trim(); }
        // HTML-escape operator-set strings (location/rule names) before innerHTML/tooltip injection.
        function esc(s) { return String(s == null ? '' : s).replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c])); }
        function togglePortalTheme() {
            const cur = document.documentElement.getAttribute('data-theme') === 'light' ? 'light' : 'dark';
            const next = cur === 'light' ? 'dark' : 'light';
            document.documentElement.setAttribute('data-theme', next);
            try { localStorage.setItem('portal-theme', next); } catch (e) {}
            const mc = document.querySelector('meta[name="theme-color"]');
            if (mc) mc.setAttribute('content', next === 'light' ? '#eef2f9' : '#080b14');
            document.dispatchEvent(new CustomEvent('portal-theme', { detail: next }));
        }
    </script>
    {% block scripts %}{% endblock %}
</body>
</html>
