<?php
// The dark theme's stylesheet (lib/dark-theme.php): ?f=app,visual,… → the dark rules of those stylesheets, made from
// them on each request so they follow every change, then assets/dark.css, the fixes by hand. Pages ask for it with
// &v=<asset version>, so browsers keep it.
require __DIR__ . '/../lib/dark-theme.php';

$names = array_values(array_intersect(array_keys(AC_DARK_SOURCES), explode(',', (string)($_GET['f'] ?? 'app'))));
$files = array_map(fn($name) => AC_DIR . '/' . AC_DARK_SOURCES[$name], $names);
$files[] = __DIR__ . '/dark.css';
$files[] = AC_DIR . '/lib/dark-theme.php';
$etag = '"' . md5(implode('|', array_map(fn($f) => $f . '@' . @filemtime($f), $files))) . '"';

header('Content-Type: text/css; charset=utf-8');
header('ETag: ' . $etag);
header(isset($_GET['v']) ? 'Cache-Control: public, max-age=31536000, immutable' : 'Cache-Control: no-cache');
if (trim((string)($_SERVER['HTTP_IF_NONE_MATCH'] ?? '')) === $etag) { http_response_code(304); exit; }

$renames = [];
ac_dark_tokens(array_map(fn($name) => (string)@file_get_contents(AC_DIR . '/' . AC_DARK_SOURCES[$name]), $names));
foreach ($names as $name) {
    // One stylesheet the maker can't read leaves its part light, not the whole page.
    try { echo "/* $name */\n", ac_dark_css((string)@file_get_contents(AC_DIR . '/' . AC_DARK_SOURCES[$name]), $renames); }
    catch (Throwable $e) { error_log("algo-claude dark.php $name: " . $e->getMessage()); }
}
echo "/* by hand */\n", (string)@file_get_contents(__DIR__ . '/dark.css');
