diff --git a/src/main/java/com/samsonmedia/barn/config/CleanupConfig.java b/src/main/java/com/samsonmedia/barn/config/CleanupConfig.java
new file mode 100644
index 0000000..20b8b9c
--- /dev/null
+++ b/src/main/java/com/samsonmedia/barn/config/CleanupConfig.java
@@ -0,0 +1,64 @@
+package com.samsonmedia.barn.config;
+
+/**
+ * Configuration for automatic job cleanup.
+ *
+ * @param enabled whether automatic cleanup is enabled
+ * @param maxAgeHours remove jobs older than this
+ * @param cleanupIntervalMinutes how often the cleanup task runs
+ * @param keepFailedJobs preserve failed jobs for debugging
+ * @param keepFailedJobsHours how long to keep failed jobs
+ */
+public record CleanupConfig(
+ boolean enabled,
+ int maxAgeHours,
+ int cleanupIntervalMinutes,
+ boolean keepFailedJobs,
+ int keepFailedJobsHours
+) {
+
+ /** Default enabled state. */
+ public static final boolean DEFAULT_ENABLED = true;
+
+ /** Default max age in hours (3 days). */
+ public static final int DEFAULT_MAX_AGE_HOURS = 72;
+
+ /** Default cleanup interval in minutes. */
+ public static final int DEFAULT_CLEANUP_INTERVAL_MINUTES = 60;
+
+ /** Default keep failed jobs setting. */
+ public static final boolean DEFAULT_KEEP_FAILED_JOBS = true;
+
+ /** Default keep failed jobs hours (1 week). */
+ public static final int DEFAULT_KEEP_FAILED_JOBS_HOURS = 168;
+
+ /**
+ * Creates a CleanupConfig with validation.
+ */
+ public CleanupConfig {
+ if (maxAgeHours < 1) {
+ throw new IllegalArgumentException("maxAgeHours must be at least 1");
+ }
+ if (cleanupIntervalMinutes < 1) {
+ throw new IllegalArgumentException("cleanupIntervalMinutes must be at least 1");
+ }
+ if (keepFailedJobsHours < 1) {
+ throw new IllegalArgumentException("keepFailedJobsHours must be at least 1");
+ }
+ }
+
+ /**
+ * Creates a CleanupConfig with default values.
+ *
+ * @return a CleanupConfig with all defaults
+ */
+ public static CleanupConfig withDefaults() {
+ return new CleanupConfig(
+ DEFAULT_ENABLED,
+ DEFAULT_MAX_AGE_HOURS,
+ DEFAULT_CLEANUP_INTERVAL_MINUTES,
+ DEFAULT_KEEP_FAILED_JOBS,
+ DEFAULT_KEEP_FAILED_JOBS_HOURS
+ );
+ }
+}
diff --git a/src/main/java/com/samsonmedia/barn/config/Config.java b/src/main/java/com/samsonmedia/barn/config/Config.java
new file mode 100644
index 0000000..d2dd974
--- /dev/null
+++ b/src/main/java/com/samsonmedia/barn/config/Config.java
@@ -0,0 +1,43 @@
+package com.samsonmedia.barn.config;
+
+import java.util.Objects;
+
+/**
+ * Top-level configuration for the Barn application.
+ *
+ * @param service service daemon configuration
+ * @param jobs job execution configuration
+ * @param cleanup automatic cleanup configuration
+ * @param storage storage and filesystem configuration
+ */
+public record Config(
+ ServiceConfig service,
+ JobsConfig jobs,
+ CleanupConfig cleanup,
+ StorageConfig storage
+) {
+
+ /**
+ * Creates a Config with validation.
+ */
+ public Config {
+ Objects.requireNonNull(service, "service must not be null");
+ Objects.requireNonNull(jobs, "jobs must not be null");
+ Objects.requireNonNull(cleanup, "cleanup must not be null");
+ Objects.requireNonNull(storage, "storage must not be null");
+ }
+
+ /**
+ * Creates a Config with all default values.
+ *
+ * @return a Config with all defaults
+ */
+ public static Config withDefaults() {
+ return new Config(
+ ServiceConfig.withDefaults(),
+ JobsConfig.withDefaults(),
+ CleanupConfig.withDefaults(),
+ StorageConfig.withDefaults()
+ );
+ }
+}
diff --git a/src/main/java/com/samsonmedia/barn/config/ConfigDefaults.java b/src/main/java/com/samsonmedia/barn/config/ConfigDefaults.java
new file mode 100644
index 0000000..945b8e4
--- /dev/null
+++ b/src/main/java/com/samsonmedia/barn/config/ConfigDefaults.java
@@ -0,0 +1,92 @@
+package com.samsonmedia.barn.config;
+
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Provides platform-specific default values for configuration.
+ */
+public final class ConfigDefaults {
+
+ private ConfigDefaults() {
+ // Utility class - prevent instantiation
+ }
+
+ /**
+ * Gets the default base directory for Barn data.
+ *
+ * @return the default base directory
+ */
+ public static Path getDefaultBaseDir() {
+ return switch (OperatingSystem.current()) {
+ case WINDOWS -> Path.of(System.getenv("TEMP"), "barn");
+ case LINUX, MACOS -> Path.of("/tmp/barn");
+ };
+ }
+
+ /**
+ * Gets the default IPC socket path.
+ *
+ * @return the default IPC socket path
+ */
+ public static Path getDefaultIpcSocket() {
+ return switch (OperatingSystem.current()) {
+ case WINDOWS -> Path.of("\\\\.\\pipe\\barn");
+ case LINUX, MACOS -> Path.of("/tmp/barn/barn.sock");
+ };
+ }
+
+ /**
+ * Gets the system-wide configuration file path.
+ *
+ * @return the system config path
+ */
+ public static Path getSystemConfigPath() {
+ return switch (OperatingSystem.current()) {
+ case WINDOWS -> {
+ String programData = System.getenv("PROGRAMDATA");
+ if (programData == null || programData.isBlank()) {
+ programData = "C:\\ProgramData";
+ }
+ yield Path.of(programData, "barn", "barn.conf");
+ }
+ case LINUX, MACOS -> Path.of("/etc/barn/barn.conf");
+ };
+ }
+
+ /**
+ * Gets the user-specific configuration file path.
+ *
+ * @return the user config path
+ */
+ public static Path getUserConfigPath() {
+ String userHome = System.getProperty("user.home", "");
+ return switch (OperatingSystem.current()) {
+ case WINDOWS -> {
+ String appData = System.getenv("APPDATA");
+ if (appData == null || appData.isBlank()) {
+ appData = Path.of(userHome, "AppData", "Roaming").toString();
+ }
+ yield Path.of(appData, "barn", "barn.conf");
+ }
+ case MACOS -> Path.of(userHome, "Library", "Application Support", "barn", "barn.conf");
+ case LINUX -> Path.of(userHome, ".config", "barn", "barn.conf");
+ };
+ }
+
+ /**
+ * Gets the list of configuration file paths in priority order.
+ *
+ *
Paths are returned in order of increasing priority - later paths
+ * override earlier ones.
+ *
+ * @return list of config paths to check
+ */
+ public static List getConfigSearchPaths() {
+ List paths = new ArrayList<>();
+ paths.add(getSystemConfigPath());
+ paths.add(getUserConfigPath());
+ return paths;
+ }
+}
diff --git a/src/main/java/com/samsonmedia/barn/config/ConfigLoader.java b/src/main/java/com/samsonmedia/barn/config/ConfigLoader.java
new file mode 100644
index 0000000..05bd014
--- /dev/null
+++ b/src/main/java/com/samsonmedia/barn/config/ConfigLoader.java
@@ -0,0 +1,319 @@
+package com.samsonmedia.barn.config;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.function.Function;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Loads and validates Barn configuration from files and environment variables.
+ *
+ * Configuration is loaded in the following priority order (later overrides earlier):
+ *
+ * - Default values
+ * - System config file (e.g., /etc/barn/barn.conf)
+ * - User config file (e.g., ~/.config/barn/barn.conf)
+ * - Explicit config file (via --config flag)
+ * - Environment variables (BARN_SECTION_KEY format)
+ *
+ */
+public final class ConfigLoader {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ConfigLoader.class);
+ private static final String ENV_PREFIX = "BARN_";
+
+ private final Map environment;
+
+ /**
+ * Creates a ConfigLoader using the system environment.
+ */
+ public ConfigLoader() {
+ this(System.getenv());
+ }
+
+ /**
+ * Creates a ConfigLoader with a custom environment (for testing).
+ *
+ * @param environment the environment variables to use
+ */
+ public ConfigLoader(Map environment) {
+ this.environment = Objects.requireNonNull(environment, "environment must not be null");
+ }
+
+ /**
+ * Loads configuration using default search paths.
+ *
+ * @return the loaded configuration
+ * @throws ConfigException if configuration is invalid
+ */
+ public Config load() {
+ return load(Optional.empty());
+ }
+
+ /**
+ * Loads configuration, optionally with an explicit config file.
+ *
+ * @param explicitPath optional explicit config file path
+ * @return the loaded configuration
+ * @throws ConfigException if configuration is invalid
+ */
+ public Config load(Optional explicitPath) {
+ Objects.requireNonNull(explicitPath, "explicitPath must not be null");
+
+ Map> merged = new HashMap<>();
+
+ // Load from search paths
+ for (Path path : ConfigDefaults.getConfigSearchPaths()) {
+ loadIfExists(path).ifPresent(data -> mergeInto(merged, data));
+ }
+
+ // Load from explicit path if provided
+ explicitPath.flatMap(this::loadIfExists).ifPresent(data -> mergeInto(merged, data));
+
+ // Apply environment overrides
+ applyEnvironmentOverrides(merged);
+
+ return buildConfig(merged);
+ }
+
+ /**
+ * Loads configuration from a specific file.
+ *
+ * @param path the path to the config file
+ * @return the loaded configuration
+ * @throws ConfigException if the file cannot be read or is invalid
+ */
+ public Config loadFromFile(Path path) {
+ Objects.requireNonNull(path, "path must not be null");
+
+ Map> data = loadFile(path);
+ applyEnvironmentOverrides(data);
+ return buildConfig(data);
+ }
+
+ private Optional