# Margin of Error R Companion — shared setup
# Sourced at the top of every walkthrough so the theme and palette are consistent.

library(tidyverse)

# Color palette — matches the figures in the print book
moe_colors <- list(
  navy      = "#1B2A4A",
  teal      = "#2A9D8F",
  amber     = "#E9C46A",
  coral     = "#E76F51",
  slate     = "#6C757D",
  light_bg  = "#F8F9FA",
  dark_text = "#212529",
  mid_text  = "#495057"
)

moe_palette <- c(
  moe_colors$teal,
  moe_colors$coral,
  moe_colors$amber,
  moe_colors$navy,
  moe_colors$slate,
  "#8ECAE6"
)

theme_moe <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(
      plot.title    = element_text(color = moe_colors$navy, face = "bold", size = rel(1.15)),
      plot.subtitle = element_text(color = moe_colors$mid_text, size = rel(0.95)),
      plot.caption  = element_text(color = moe_colors$slate, size = rel(0.8), hjust = 0),
      axis.title    = element_text(color = moe_colors$dark_text),
      axis.text     = element_text(color = moe_colors$mid_text),
      panel.grid.minor = element_blank(),
      legend.position  = "bottom",
      plot.background  = element_rect(fill = "white", color = NA),
      panel.background = element_rect(fill = "white", color = NA)
    )
}

scale_color_moe <- function(...) scale_color_manual(values = moe_palette, ...)
scale_fill_moe  <- function(...) scale_fill_manual(values = moe_palette, ...)
