* { box-sizing: border-box; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    margin: 0;
    background: #f6f7f9;
    color: #1c1f23;
}
header {
    background: #1f2937;
    color: #fff;
    padding: .9rem 1.5rem;
    display: flex;
    align-items: baseline;
    gap: 1.5rem;
}
header h1 { margin: 0; font-size: 1.15rem; }
header nav a { color: #cbd5e1; text-decoration: none; margin-right: .25rem; }
header nav a:hover { color: #fff; }
header nav a.active { color: #fff; font-weight: 600; }

main { max-width: 1300px; margin: 1rem auto; padding: 0 1rem; }

.search-bar {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: .75rem 1rem;
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
    align-items: end;
}
.search-bar label {
    display: flex;
    flex-direction: column;
    font-size: .8rem;
    color: #4b5563;
}
.search-bar input, .search-bar select {
    padding: .4rem .6rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 1rem;
}
.search-bar input[name="zip"] { width: 6em; }
.search-bar input[name="radius"] { width: 4.5em; }
.search-bar .check-inline {
    flex-direction: row;
    align-items: center;
    gap: .35rem;
    color: #1f2937;
    padding-bottom: .35rem;
}
.search-bar button {
    padding: .55rem 1.1rem;
    background: #2563eb;
    color: #fff;
    border: 0;
    border-radius: 6px;
    font-size: 1rem;
    cursor: pointer;
}
.search-bar button:hover { background: #1d4ed8; }

.layout {
    display: grid;
    grid-template-columns: minmax(0, 5fr) minmax(0, 4fr);
    gap: 1rem;
    margin-top: 1rem;
}
@media (max-width: 900px) {
    .layout { grid-template-columns: 1fr; }
    .map-pane #map { height: 300px; }
}

.results-pane { min-width: 0; }
.summary { font-size: .9rem; color: #4b5563; margin-bottom: .5rem; }
.route-bar { display: flex; gap: .5rem; margin-bottom: .75rem; flex-wrap: wrap; align-items: center; }
.route-bar .check-inline { display: inline-flex; align-items: center; gap: .3rem; font-size: .9rem; color: #374151; }
.route-warnings { margin-bottom: .75rem; }
.route-warning {
    background: #fef3c7;
    border: 1px solid #f59e0b;
    color: #78350f;
    padding: .5rem .75rem;
    border-radius: 6px;
    font-size: .9rem;
    margin-bottom: .35rem;
}
.route-bar button {
    padding: .45rem .9rem;
    border: 1px solid #d1d5db;
    background: #fff;
    border-radius: 6px;
    cursor: pointer;
}
.route-bar button:disabled { opacity: .5; cursor: not-allowed; }
#build-route { background: #059669; color: #fff; border-color: #059669; }
#build-route:disabled { background: #fff; color: inherit; border-color: #d1d5db; }
#view-map { margin-left: auto; }

#results-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: .6rem;
}
.result-card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: .85rem 1rem;
}
.result-card.visited { border-left: 4px solid #059669; }
.result-card.skipped { opacity: .55; border-left: 4px solid #9ca3af; }

.r-head {
    display: flex;
    align-items: flex-start;
    gap: .75rem;
    margin-bottom: .35rem;
}
.r-head h3 {
    font-size: 1rem;
    line-height: 1.3;
    margin: 0;
    flex: 1;
    min-width: 0;
}
.r-distance {
    font-size: .75rem;
    background: #f3f4f6;
    color: #1f2937;
    padding: 2px 9px;
    border-radius: 999px;
    flex-shrink: 0;
    align-self: flex-start;
    white-space: nowrap;
    font-weight: 600;
}
.select-row {
    display: inline-flex;
    align-items: center;
    gap: .4rem;
    font-size: .85rem;
    color: #4b5563;
    cursor: pointer;
    user-select: none;
}
.select-row input[type=checkbox] { margin: 0; }
.route-num { font-weight: 700; color: #2563eb; min-width: 1.6rem; }
.result-card.on-route .route-num { color: #059669; }

.r-meta, .r-address { margin: .25rem 0; font-size: .9rem; color: #374151; }
.r-address { color: #1f2937; }
.r-session { padding: 2px 0; font-size: .92rem; font-variant-numeric: tabular-nums; }
.r-session.is-today { color: #b45309; }
.r-session .time-tba { color: #9ca3af; font-style: italic; font-size: .85rem; }
.today-pill {
    background: #b45309; color: #fff; font-size: .7rem; padding: 1px 6px;
    border-radius: 999px; margin-left: .35rem; vertical-align: 1px;
}
.r-desc summary { cursor: pointer; font-size: .85rem; color: #4b5563; }
.r-desc-body { margin-top: .4rem; font-size: .9rem; color: #374151; white-space: pre-wrap; }

.r-actions { display: flex; gap: .4rem; margin-top: .55rem; flex-wrap: wrap; align-items: center; }
.r-actions button {
    background: #fff;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    padding: .3rem .7rem;
    font-size: .85rem;
    cursor: pointer;
}
.r-actions button:hover { background: #f3f4f6; }
.r-actions .btn-status.is-active {
    background: #1f2937;
    color: #fff;
    border-color: #1f2937;
}

#back-to-top {
    position: fixed;
    right: 1.1rem;
    bottom: 1.1rem;
    z-index: 900;
    background: #1f2937;
    color: #fff;
    border: 0;
    border-radius: 999px;
    padding: .55rem 1rem;
    font-size: .85rem;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(15, 23, 42, .3);
}
#back-to-top:hover { background: #111827; }
#back-to-top[hidden] { display: none; }
.btn-sources { margin-left: auto; }

.r-notes-wrap {
    position: relative;
    margin-top: .55rem;
}
.r-notes {
    width: 100%;
    padding: .4rem .6rem;
    padding-bottom: 1.4rem;        /* leave room for the inline save indicator */
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    font: inherit;
    resize: vertical;
    min-height: 2.4rem;
    box-sizing: border-box;
    display: block;
}
.r-notes-status {
    position: absolute;
    right: .55rem;
    bottom: .35rem;
    font-size: .72rem;
    pointer-events: none;
    transition: opacity .15s ease;
}
.r-notes-status.saving { color: #6b7280; }
.r-notes-status.saved  { color: #047857; }
.r-notes-status.error  { color: #b91c1c; }

.empty-state {
    list-style: none;
    background: #fff7ed;
    border: 1px solid #fdba74;
    border-radius: 8px;
    padding: 1rem 1.2rem;
    text-align: center;
}
.empty-state p { margin: 0 0 .75rem; color: #7c2d12; }
.empty-actions { display: flex; gap: .5rem; justify-content: center; flex-wrap: wrap; }
.empty-actions button {
    padding: .5rem .9rem;
    border-radius: 6px;
    border: 1px solid #d1d5db;
    background: #fff;
    cursor: pointer;
    font-size: .9rem;
}
.empty-actions .jump-next {
    background: #2563eb; color: #fff; border-color: #2563eb;
}
.empty-actions .jump-next:hover { background: #1d4ed8; }

.r-purchases { list-style: none; padding: 0; margin: .55rem 0 0; font-size: .85rem; }
.r-purchases li {
    padding: .35rem .55rem;
    background: #f3f4f6;
    border-radius: 6px;
    margin-bottom: .25rem;
    display: flex;
    gap: .5rem;
    align-items: center;
}
.r-purchases img { max-height: 32px; border-radius: 4px; }
.r-purchases .price { color: #059669; font-weight: 600; margin-left: auto; }
.r-purchases .del { background: none; border: 0; color: #b91c1c; cursor: pointer; }
.r-totals { margin: .25rem 0 0; font-size: .85rem; color: #374151; text-align: right; }
.r-totals strong { color: #059669; }

.purchase-form {
    margin-top: .6rem;
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    background: #f9fafb;
    padding: .55rem;
    border-radius: 6px;
}
.purchase-form input[type=text] {
    padding: .35rem .55rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    flex: 1;
    min-width: 100px;
}
.purchase-form input[name=item_name] { flex: 2; }
.purchase-form button { padding: .35rem .8rem; border-radius: 6px; border: 0; background: #2563eb; color: #fff; cursor: pointer; }
.purchase-form button.cancel-purchase { background: #6b7280; }

.map-pane { position: sticky; top: 1rem; align-self: start; }
.map-pane #map {
    height: 70vh;
    min-height: 400px;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
}
.leaflet-popup-content { font-size: .9rem; }

/* Sources modal */
.modal[hidden] { display: none; }
.modal {
    position: fixed; inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}
.modal-backdrop {
    position: absolute; inset: 0;
    background: rgba(15, 23, 42, .5);
}
.modal-card {
    position: relative;
    background: #fff;
    border-radius: 10px;
    max-width: 420px;
    width: 100%;
    box-shadow: 0 20px 50px rgba(15, 23, 42, .25);
    overflow: hidden;
}
.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .85rem 1rem;
    border-bottom: 1px solid #e5e7eb;
    gap: .5rem;
}
.modal-head h4 {
    margin: 0;
    font-size: 1rem;
    color: #1f2937;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.modal-close {
    background: transparent;
    border: 0;
    font-size: 1.4rem;
    line-height: 1;
    color: #6b7280;
    cursor: pointer;
    padding: 0 .25rem;
}
.modal-close:hover { color: #111827; }
.modal-list {
    list-style: none;
    margin: 0;
    padding: .25rem 0;
    max-height: 60vh;
    overflow-y: auto;
}
.modal-list li { border-bottom: 1px solid #f3f4f6; }
.modal-list li:last-child { border-bottom: 0; }
.modal-list a {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .7rem 1rem;
    text-decoration: none;
    color: #2563eb;
    font-size: .95rem;
}
.modal-list a:hover { background: #f9fafb; }
.open-arrow { color: #6b7280; font-size: .9rem; }
.numbered-marker { background: transparent; border: 0; }
.num-pin {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    color: #fff;
    font-weight: 700;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
    box-shadow: 0 2px 6px rgba(15, 23, 42, .35);
    line-height: 1;
}
.result-card.on-route .route-num { color: #059669; font-weight: 700; }
