//! Custom theme tuned for a sharp black/white macOS-inspired palette.

use eframe::egui;

/// Apply the envseal theme.
pub fn apply_theme(ctx: &egui::Context, dark: bool) {
    let mut visuals = if dark {
        egui::Visuals::dark()
    } else {
        egui::Visuals::light()
    };

    if dark {
        // True near-black background, layered surfaces in subtly lighter
        // greys. Borders are barely-visible; we lean on contrast between
        // surfaces rather than hard lines (Apple style).
        let bg_base = egui::Color32::from_rgb(20, 20, 22);
        let bg_surface = egui::Color32::from_rgb(28, 28, 30);
        let bg_elevated = egui::Color32::from_rgb(36, 36, 38);
        let text = egui::Color32::from_rgb(240, 240, 242);
        let text_soft = egui::Color32::from_rgb(155, 155, 160);
        let border = egui::Color32::from_rgb(54, 54, 58);
        let border_strong = egui::Color32::from_rgb(86, 86, 92);

        visuals.panel_fill = bg_base;
        visuals.window_fill = bg_surface;
        visuals.extreme_bg_color = bg_base;
        visuals.faint_bg_color = bg_surface;
        visuals.override_text_color = Some(text);
        visuals.selection.bg_fill = egui::Color32::from_rgb(50, 50, 56);
        visuals.selection.stroke = egui::Stroke::new(1.0, text);
        visuals.hyperlink_color = egui::Color32::from_rgb(120, 170, 255);

        // Buttons: surface fill, hairline border, soft hover lift,
        // pressed = elevated (mimics translucency).
        visuals.widgets.noninteractive.bg_fill = bg_surface;
        visuals.widgets.noninteractive.bg_stroke = egui::Stroke::new(1.0, border);
        visuals.widgets.noninteractive.fg_stroke = egui::Stroke::new(1.0, text_soft);

        visuals.widgets.inactive.bg_fill = bg_elevated;
        visuals.widgets.inactive.bg_stroke = egui::Stroke::new(1.0, border);
        visuals.widgets.inactive.fg_stroke = egui::Stroke::new(1.0, text);
        visuals.widgets.inactive.weak_bg_fill = bg_surface;

        visuals.widgets.hovered.bg_fill = egui::Color32::from_rgb(48, 48, 52);
        visuals.widgets.hovered.bg_stroke = egui::Stroke::new(1.0, border_strong);
        visuals.widgets.hovered.fg_stroke = egui::Stroke::new(1.0, text);
        visuals.widgets.hovered.weak_bg_fill = egui::Color32::from_rgb(40, 40, 44);

        visuals.widgets.active.bg_fill = egui::Color32::from_rgb(58, 58, 62);
        visuals.widgets.active.bg_stroke = egui::Stroke::new(1.0, border_strong);
        visuals.widgets.active.fg_stroke = egui::Stroke::new(1.0, text);

        visuals.window_stroke = egui::Stroke::new(1.0, border);
        visuals.window_shadow = egui::Shadow {
            offset: [0, 8],
            blur: 24,
            spread: 0,
            color: egui::Color32::from_black_alpha(128),
        };
        visuals.popup_shadow = egui::Shadow {
            offset: [0, 4],
            blur: 16,
            spread: 0,
            color: egui::Color32::from_black_alpha(96),
        };
    } else {
        // Off-white panel, slightly tinted surfaces. Borders are very
        // faint — the eye separates by background luminance, not lines.
        let bg_base = egui::Color32::from_rgb(252, 252, 252);
        let bg_surface = egui::Color32::from_rgb(247, 247, 248);
        let bg_elevated = egui::Color32::from_rgb(255, 255, 255);
        let text = egui::Color32::from_rgb(28, 28, 32);
        let text_soft = egui::Color32::from_rgb(110, 110, 118);
        let border = egui::Color32::from_rgb(220, 220, 224);
        let border_strong = egui::Color32::from_rgb(190, 190, 196);

        visuals.panel_fill = bg_base;
        visuals.window_fill = bg_elevated;
        visuals.faint_bg_color = bg_surface;
        visuals.override_text_color = Some(text);
        visuals.selection.bg_fill = egui::Color32::from_rgb(220, 232, 252);
        visuals.selection.stroke = egui::Stroke::new(1.0, text);
        visuals.hyperlink_color = egui::Color32::from_rgb(0, 105, 217);

        visuals.widgets.noninteractive.bg_fill = bg_surface;
        visuals.widgets.noninteractive.bg_stroke = egui::Stroke::new(1.0, border);
        visuals.widgets.noninteractive.fg_stroke = egui::Stroke::new(1.0, text_soft);

        visuals.widgets.inactive.bg_fill = bg_elevated;
        visuals.widgets.inactive.bg_stroke = egui::Stroke::new(1.0, border);
        visuals.widgets.inactive.fg_stroke = egui::Stroke::new(1.0, text);
        visuals.widgets.inactive.weak_bg_fill = bg_surface;

        visuals.widgets.hovered.bg_fill = egui::Color32::from_rgb(245, 245, 246);
        visuals.widgets.hovered.bg_stroke = egui::Stroke::new(1.0, border_strong);
        visuals.widgets.hovered.fg_stroke = egui::Stroke::new(1.0, text);
        visuals.widgets.hovered.weak_bg_fill = egui::Color32::from_rgb(240, 240, 242);

        visuals.widgets.active.bg_fill = egui::Color32::from_rgb(232, 232, 234);
        visuals.widgets.active.bg_stroke = egui::Stroke::new(1.0, border_strong);
        visuals.widgets.active.fg_stroke = egui::Stroke::new(1.0, text);

        visuals.window_stroke = egui::Stroke::new(1.0, border);
        visuals.window_shadow = egui::Shadow {
            offset: [0, 8],
            blur: 24,
            spread: 0,
            color: egui::Color32::from_black_alpha(40),
        };
        visuals.popup_shadow = egui::Shadow {
            offset: [0, 4],
            blur: 16,
            spread: 0,
            color: egui::Color32::from_black_alpha(28),
        };
    }

    // Generous rounding — pills feel premium; sharp corners feel like a
    // form. 7px is the macOS Big Sur default for primary buttons.
    let rounding = egui::CornerRadius::same(7);
    visuals.widgets.inactive.corner_radius = rounding;
    visuals.widgets.hovered.corner_radius = rounding;
    visuals.widgets.active.corner_radius = rounding;
    visuals.widgets.noninteractive.corner_radius = rounding;
    visuals.window_corner_radius = egui::CornerRadius::same(12);
    visuals.menu_corner_radius = egui::CornerRadius::same(8);

    ctx.set_visuals(visuals);

    let mut style = (*ctx.global_style()).clone();
    style
        .text_styles
        .insert(egui::TextStyle::Heading, egui::FontId::proportional(18.0));
    style
        .text_styles
        .insert(egui::TextStyle::Body, egui::FontId::proportional(14.0));
    style
        .text_styles
        .insert(egui::TextStyle::Small, egui::FontId::proportional(12.0));
    style
        .text_styles
        .insert(egui::TextStyle::Button, egui::FontId::proportional(13.0));
    // Roomier spacing — Apple HIG calls for breathing space between
    // controls. Bumped from (8, 4) → (10, 6) and button padding from
    // (12, 6) → (14, 8).
    style.spacing.item_spacing = egui::vec2(10.0, 6.0);
    style.spacing.button_padding = egui::vec2(14.0, 8.0);
    style.spacing.window_margin = egui::Margin::same(20);
    // Slightly slower animations feel more deliberate / premium.
    style.animation_time = 0.22;
    ctx.set_global_style(style);
}
