:root {
  /* Colors - iOS System Light Palette */
  --bg-app: #f2f2f7; /* Primary system background (light gray) */
  --bg-panel: #ffffff; /* Panel background (white) */
  --text-primary: #000000; /* Primary text (black) */
  --text-secondary: #3c3c43; /* Darker text for labels */
  --text-muted: #8e8e93; /* Secondary/Hint text */
  --accent: #007aff; /* System Blue */
  --danger: #ff3b30; /* System Red */
  --border-line: #c6c6c8; /* Light gray separator/border */

  /* Sizing and Spacing */
  --space-unit: 8px; /* Base unit for spacing */
  --padding-h: 16px; /* Horizontal padding for list items */
  --radius-l: 10px; /* System button radius */
  --radius-xl: 14px; /* Grouped list radius */
  
  /* Layout Dimensions (FIXED FOR POSITIONING) */
  --list-row-min-height: 44px;
  --list-row-padding-v: 11px;
  /* Calculated row height: 44px + 2 * 11px = 66px */
  --list-row-height: calc(var(--list-row-min-height) + 2 * var(--list-row-padding-v)); 
  
  /* Large Title Spacing */
  --title-margin-top: 52px;
  --title-margin-bottom: 24px;
}

/* Global Reset and Typography */
* {
  box-sizing: border-box;
}
html, body {
  height: 100%;
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--text-primary);
  font-size: 17px;
  line-height: 1.3;
  background: var(--bg-app);
  background-attachment: fixed;
}

/* Layout */
.container {
  max-width: 768px;
  margin: 0 auto;
  padding: 0 0;
}

/* -------------------------------------------------------------------------- */
/* 1. Title and Section Headers */
/* -------------------------------------------------------------------------- */
h1 {
  font-size: 34px;
  font-weight: 700;
  margin: var(--title-margin-top) 0 var(--title-margin-bottom);
  padding: 0 var(--padding-h);
}

h2 {
  font-size: 13px;
  font-weight: 500;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 24px 0 4px; 
  padding: 0 var(--padding-h);
}

/* Base Panel Styles */
form, .results, .errors {
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 0;
  box-shadow: none;
}

/* -------------------------------------------------------------------------- */
/* 2. Grouped List Wrapper */
/* -------------------------------------------------------------------------- */

.route-planner, #calc-form, #results, #errors {
    margin: 0 0 24px;
}

/* The primary container for the "grouped list" style */
.route-planner form, #calc-form, #results, #errors {
    background: var(--bg-panel);
    border-radius: var(--radius-xl);
    border: 0.5px solid var(--border-line);
    overflow: hidden;
    box-shadow: 0 0.5px 0 0 var(--border-line); /* Subtle bottom shadow for lift */
    position: relative; 
}

/* -------------------------------------------------------------------------- */
/* 3. Form Fields (Rows) */
/* -------------------------------------------------------------------------- */

.grid {
    display: block; 
    gap: 0;
}

.field {
  display: flex; 
  justify-content: space-between;
  align-items: center;
  min-height: var(--list-row-min-height);
  padding: var(--list-row-padding-v) var(--padding-h); 
  gap: var(--space-unit);
  border-top: 0.5px solid var(--border-line); 
  position: static; /* Default to static for regular flow */
}

/* Remove top border from the first item in the list */
.route-planner form .field:first-child,
#calc-form .field:first-child {
    border-top: none;
}

.field span {
    color: var(--text-secondary);
    font-size: 17px;
    font-weight: 400;
    flex-shrink: 0;
    width: 50%;
    transition: transform 0.4s ease-in-out; /* Label transition for counter-animation */
}
.field input, .field select {
  width: 100%; 
  max-width: 50%; 
  background: transparent;
  color: var(--text-primary);
  border: none;
  padding: 0;
  text-align: right;
  font-size: 17px;
  font-weight: 400;
  outline: none;
}

.field input:focus, .field select:focus {
  box-shadow: none;
  background: transparent;
  color: var(--accent);
}

.field input::placeholder {
  color: var(--text-muted);
  opacity: 0.8;
}

/* -------------------------------------------------------------------------- */
/* 3.5. Swap Animation Logic (MOBILE ONLY) */
/* -------------------------------------------------------------------------- */
.input-group {
    position: relative;
}
.input-pair {
    position: relative;
    /* Height must accommodate the two absolutely positioned fields on mobile */
    min-height: calc(var(--list-row-height) * 2); 
}

/* Mobile: Set Fields to Absolute for Animation */
@media (max-width: 600px) {
    .input-pair .field {
        position: absolute; 
        left: 0;
        right: 0;
        border-top: none; 
        transition: top 0.4s ease-in-out; 
    }
    
    /* Default positions */
    #origin-field {
        top: 0;
    }
    #destination-field {
        top: var(--list-row-height);
        border-top: 0.5px solid var(--border-line); 
    }

    /* State: Swapped Layout */
    .route-form.swapped-layout #origin-field {
        top: var(--list-row-height); 
        border-top: 0.5px solid var(--border-line);
    }
    .route-form.swapped-layout #origin-field span {
        /* Counter-animation UP */
        transform: translateY(calc( -1 * var(--list-row-height) )); 
    }

    .route-form.swapped-layout #destination-field {
        top: 0;
        border-top: none;
    }
    .route-form.swapped-layout #destination-field span {
        /* Counter-animation DOWN */
        transform: translateY(var(--list-row-height));
    }
}


/* Swap Button (Floating Icon) */
#swapBtn {
    position: absolute;
    top: var(--list-row-height); 
    transform: translateY(-50%); 
    right: var(--padding-h);
    z-index: 10;
    background: var(--bg-panel);
    border: 2px solid var(--bg-app);
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    transition: transform 0.4s ease-in-out, background-color 0.1s;
}
#swapBtn:hover {
    background-color: var(--bg-app);
}
#swapBtn svg {
    width: 18px;
    height: 18px;
    color: var(--accent); 
    transition: transform 0.4s ease-in-out;
}
.route-form.swapped-layout #swapBtn svg {
    transform: rotate(180deg);
}
/* Hide the action button version of the swap button */
.actions button#swapBtn {
    display: none;
}


/* -------------------------------------------------------------------------- */
/* 4. Actions & Buttons */
/* -------------------------------------------------------------------------- */

.actions {
  display: flex;
  gap: var(--space-unit);
  margin: 32px var(--padding-h);
  align-items: center;
}
.route-planner .actions {
    /* Ensure action button is below the input-pair container */
    margin-top: 24px; 
}
button {
  flex-grow: 1;
  border: none;
  background: var(--accent);
  color: var(--bg-panel); 
  font-weight: 600;
  padding: 12px 16px; 
  border-radius: var(--radius-l); 
  cursor: pointer;
  transition: opacity 0.1s;
  box-shadow: none; 
}
button.secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

/* -------------------------------------------------------------------------- */
/* 5. Results Section */
/* -------------------------------------------------------------------------- */

#results h2 { display: none; }
.results ul { list-style: none; padding: 0; margin: 0; }
.results li {
  min-height: var(--list-row-min-height);
  padding: var(--list-row-padding-v) var(--padding-h);
  border-top: 0.5px solid var(--border-line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 17px;
}
.results li:first-child {
    border-top: none;
}
.results strong {
  color: var(--text-secondary);
  font-weight: 400;
}
.results span {
    font-weight: 400;
    color: var(--text-muted); 
}
.results small {
    color: var(--text-secondary);
    margin-left: 4px;
}

/* -------------------------------------------------------------------------- */
/* 6. Errors and Hints */
/* -------------------------------------------------------------------------- */

#errors {
    padding: var(--list-row-padding-v) var(--padding-h);
    color: var(--danger);
    font-size: 15px;
    background-color: #ffe0e0; /* light red background */
    border: 0.5px solid var(--danger);
    border-radius: var(--radius-xl);
}
.hint {
    color: var(--text-muted);
    margin: 12px var(--padding-h) 0;
    font-size: 13px;
    text-align: center;
}

/* -------------------------------------------------------------------------- */
/* 7. Map and Suggestions (FIXED LIST STYLING) */
/* -------------------------------------------------------------------------- */

.route-planner { margin-top: 0; }

/* Suggestions Positioning Container (relative to .route-planner form) */
.field .suggestions {
    position: absolute; 
    left: 0;
    right: 0;
    z-index: 50;
    margin: 0;
    padding: 0;
}

/* Specific placement for suggestions based on row height */
#origin-suggestions {
    top: var(--list-row-height); 
}
#destination-suggestions {
    top: calc(var(--list-row-height) * 2); 
}

/* Suggestions List Styling */
.suggestions ul {
  position: static; 
  margin: 0 var(--padding-h); 
  padding: 0;
  list-style: none;
  background: var(--bg-panel);
  border: 0.5px solid var(--border-line);
  border-radius: var(--radius-xl);
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-top: 0; 
  overflow: hidden;
  max-height: 300px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); 
}

.suggestions li {
  display: flex;
  align-items: center;
  min-height: var(--list-row-min-height); 
  padding: var(--list-row-padding-v) var(--padding-h);
  border-bottom: 0.5px solid var(--border-line);
  font-size: 17px;
  color: var(--text-primary);
  background: var(--bg-panel);
  cursor: pointer;
  text-align: left; 
}

.suggestions li:last-child {
  border-bottom: none;
}
.suggestions li:hover, .suggestions li.active {
  background: var(--bg-app); 
}

.map {
  margin: 16px var(--padding-h) 0;
  height: 400px;
  border: 0.5px solid var(--border-line);
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}


/* Desktop/Tablet Adjustments (min-width: 601px) */
@media (min-width: 601px) {
    /* Reset all swap-related positioning for desktop */
    .input-pair {
        min-height: unset;
        display: contents;
    }
    .input-pair .field {
        position: relative; 
        transition: none;
        border-top: none !important; 
    }
    
    .field span {
        transform: none !important;
        transition: none !important;
    }

    /* Reset suggestion positioning for standard flow */
    #origin-suggestions, #destination-suggestions {
        top: unset;
        margin-top: calc(var(--list-row-height) - 1px); 
    }
    
    /* Reposition Floating Swap Button */
    #swapBtn {
        position: absolute;
        top: calc(50% + 4px); 
        right: calc(50% - 16px);
        transform: none;
    }
}


/* Mobile Adjustments (Full-width sections) */
@media (max-width: 600px) {
    h1 {
        font-size: 28px;
        margin-top: 24px;
        margin-bottom: 16px;
    }
    .container {
        padding: 0;
    }
    .actions {
        flex-direction: column;
        margin: 24px 0;
    }
    .actions button {
        width: 90%;
        margin: 0 auto;
    }
    /* Full-width sections with no radius on mobile */
    .route-planner form, #calc-form, #results, #errors {
        border-left: none;
        border-right: none;
        border-radius: 0;
        box-shadow: none;
    }
    .map {
        margin-left: 0;
        margin-right: 0;
        border-left: none;
        border-right: none;
        border-radius: 0;
        box-shadow: none;
    }
    
    /* Suggestions: Full width, aligned to the edge */
    .field .suggestions {
        left: 0;
        right: 0;
    }
    .suggestions ul {
        margin: 0; 
        border-radius: 0;
        border-top: 0.5px solid var(--border-line); 
    }
}