/* Reset & base */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }

/* App layout: topbar + main area */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* ====== Top bar (full width) ====== */
#topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    background: #1a2332;
    color: white;
    flex-shrink: 0;
    z-index: 1200;
}
.topbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}
.town-select {
    font-size: 17px;
    font-weight: 700;
    color: white;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' fill='none' stroke='rgba(255,255,255,0.6)' stroke-width='2'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right center;
    padding-right: 18px;
}
.town-select option {
    color: #1a2332;
    background: white;
}
.topbar-nav {
    display: flex;
    gap: 4px;
}
.topbar-link {
    color: rgba(255,255,255,0.55);
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    padding: 6px 14px;
    border-radius: 6px;
    white-space: nowrap;
    transition: all 0.15s;
}
.topbar-link:hover { color: white; background: rgba(255,255,255,0.1); }
.topbar-link.active { color: white; background: rgba(255,255,255,0.15); }

/* ====== Main area: sidebar + map ====== */
#main {
    display: flex;
    flex: 1;
    overflow: hidden;
    position: relative;
}

/* ====== Sidebar ====== */
#sidebar {
    width: 380px;
    min-width: 380px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid #ddd;
    background: #fafafa;
    z-index: 1000;
    transition: margin-left 0.25s ease, transform 0.25s ease;
}
#sidebar.collapsed {
    margin-left: -380px;
}

/* Search */
#search-box {
    padding: 12px 16px;
    border-bottom: 1px solid #ddd;
    background: white;
    flex-shrink: 0;
}
#search-input {
    width: 100%;
    padding: 10px 14px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 6px;
    outline: none;
    transition: border-color 0.2s;
}
#search-input:focus {
    border-color: #3388ff;
    box-shadow: 0 0 0 3px rgba(51, 136, 255, 0.15);
}

/* Results list */
#results-list {
    flex: 1;
    overflow-y: auto;
}

.result-item {
    padding: 12px 16px;
    border-bottom: 1px solid #eee;
    cursor: pointer;
    transition: background 0.15s;
}
.result-item:hover { background: #e8f4fd; }

.result-address {
    font-weight: 600;
    font-size: 14px;
    color: #1a2332;
    margin-bottom: 2px;
}
.result-owner {
    font-size: 13px;
    color: #666;
    margin-bottom: 2px;
}
.result-meta {
    font-size: 12px;
    color: #999;
}
.result-meta span {
    margin-right: 8px;
}

.no-results {
    padding: 24px 16px;
    color: #999;
    text-align: center;
    font-size: 14px;
}

.results-count {
    padding: 8px 16px;
    font-size: 12px;
    color: #888;
    border-bottom: 1px solid #eee;
    background: #f5f5f5;
}

/* ====== Market Stats (sidebar default content) ====== */
#market-stats {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px;
}
#market-stats:empty { display: none; }
.stats-title {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #888;
    margin-bottom: 12px;
    font-weight: 600;
}
.stat-card {
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 14px 16px;
    margin-bottom: 10px;
}
.stat-card-title {
    font-size: 12px;
    color: #888;
    margin-bottom: 4px;
}
.stat-card-value {
    font-size: 22px;
    font-weight: 700;
    color: #1a2332;
}
.stat-card-sub {
    font-size: 12px;
    color: #999;
    margin-top: 2px;
}
.stat-row {
    display: flex;
    gap: 10px;
}
.stat-row .stat-card {
    flex: 1;
}
.stat-row .stat-card-value {
    font-size: 18px;
}
.stats-recent-title {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #888;
    margin: 16px 0 8px;
    font-weight: 600;
}
.recent-sale {
    padding: 10px 0;
    border-bottom: 1px solid #f0f0f0;
}
.recent-sale:last-child { border-bottom: none; }
.recent-sale-addr {
    font-weight: 600;
    font-size: 13px;
    color: #1a2332;
}
.recent-sale-meta {
    font-size: 12px;
    color: #888;
    margin-top: 2px;
}
.recent-sale-meta span {
    margin-right: 10px;
}

/* ====== Property detail panel ====== */
#property-detail {
    flex: 1;
    overflow-y: auto;
    padding: 0;
}
#property-detail.hidden { display: none; }

.back-btn {
    display: block;
    width: 100%;
    padding: 10px 16px;
    background: #f5f5f5;
    border: none;
    border-bottom: 1px solid #ddd;
    cursor: pointer;
    text-align: left;
    font-size: 13px;
    color: #3388ff;
}
.back-btn:hover { background: #eee; }

.detail-header {
    padding: 16px;
    background: white;
    border-bottom: 1px solid #eee;
}
.detail-header h2 {
    font-size: 18px;
    color: #1a2332;
    margin-bottom: 4px;
}
.detail-quick-stats {
    display: flex;
    gap: 8px;
    margin: 6px 0;
    font-size: 13px;
    color: #555;
}
.detail-quick-stats span {
    background: #f0f2f5;
    padding: 2px 8px;
    border-radius: 4px;
}
.detail-header .pin {
    font-size: 12px;
    color: #999;
    font-family: monospace;
}

.detail-section {
    background: white;
    margin-top: 8px;
}
.detail-section h3 {
    padding: 10px 16px 6px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #888;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 16px;
    border-bottom: 1px solid #f5f5f5;
    font-size: 14px;
}
.detail-label { color: #666; }
.detail-value { font-weight: 600; text-align: right; color: #1a2332; }

.detail-row.total {
    background: #f0f7ff;
}
.detail-row.total .detail-value {
    color: #2563eb;
    font-size: 16px;
}

/* ====== Map ====== */
#map { flex: 1; }

/* Flood zone styles */
.flood-badge {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.5px;
    vertical-align: middle;
    margin-right: 4px;
}
.flood-hazard {
    background: #fef2f2;
    color: #dc2626;
    border: 1px solid #fca5a5;
}
.flood-minimal {
    background: #f0fdf4;
    color: #16a34a;
    border: 1px solid #86efac;
}
.detail-row.flood-warning {
    background: #fef2f2;
}

/* Zoning legend */
.zoning-legend {
    background: white;
    padding: 8px 12px;
    border-radius: 6px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
    font-size: 12px;
    line-height: 1.6;
    max-height: 300px;
    overflow-y: auto;
}
.zoning-legend strong {
    display: block;
    margin-bottom: 4px;
    font-size: 13px;
}
.zoning-legend .zl-group {
    font-weight: 600;
    margin-top: 6px;
    font-size: 11px;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.zoning-legend .zl-swatch {
    display: inline-block;
    width: 12px;
    height: 12px;
    border-radius: 2px;
    margin-right: 6px;
    vertical-align: middle;
}

/* Permits */
.permits-loading, .permits-empty {
    padding: 12px 16px;
    color: #999;
    font-size: 13px;
}
.permit-item {
    padding: 10px 16px;
    border-bottom: 1px solid #eee;
}
.permit-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2px;
}
.permit-type {
    font-weight: 600;
    font-size: 13px;
    color: #1a2332;
}
.permit-date {
    font-size: 12px;
    color: #888;
}
.permit-details {
    font-size: 12px;
    color: #666;
    margin-bottom: 2px;
}
.permit-cert {
    font-size: 11px;
    color: #16a34a;
}

/* County Clerk */
.clerk-record {
    padding: 4px 0;
}
.clerk-row {
    display: flex;
    justify-content: space-between;
    padding: 6px 16px;
    border-bottom: 1px solid #f5f5f5;
    font-size: 13px;
}
.clerk-label {
    color: #666;
    min-width: 90px;
}
.clerk-row span:last-child {
    text-align: right;
    font-weight: 600;
    color: #1a2332;
}
.clerk-pdf {
    padding: 10px 16px;
}
.clerk-pdf a {
    display: inline-block;
    padding: 6px 14px;
    font-size: 13px;
    font-weight: 600;
    color: white;
    background: #3388ff;
    border-radius: 6px;
    text-decoration: none;
    transition: background 0.15s;
}
.clerk-pdf a:hover {
    background: #2266cc;
}

/* Comps button */
.comps-btn {
    display: block;
    margin: 8px 16px 12px;
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 600;
    color: white;
    background: #3388ff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.15s;
}
.comps-btn:hover { background: #2266cc; }
.comp-item:hover { background: #e8f4fd; }

/* Edge length labels on selected parcel */
.edge-label {
    background: rgba(26, 35, 50, 0.85);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    text-align: center;
    line-height: 16px;
    border-radius: 3px;
    white-space: nowrap;
    pointer-events: none;
}

/* ====== Sidebar toggle (mobile + collapsed) ====== */
#sidebar-toggle {
    display: none;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 1100;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 8px;
    background: #1a2332;
    color: white;
    font-size: 20px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    line-height: 40px;
    text-align: center;
    padding: 0;
}
#sidebar.collapsed ~ #sidebar-toggle {
    display: block;
}

/* Sidebar close (hidden on desktop, visible on mobile) */
#sidebar-close {
    display: none;
}

/* ====== Mobile ====== */
@media (max-width: 768px) {
    #topbar {
        padding: 0 12px;
        height: 44px;
    }
    .town-select {
        font-size: 15px;
    }
    .topbar-link {
        font-size: 12px;
        padding: 5px 10px;
    }
    #sidebar {
        position: fixed;
        top: 44px;
        left: 0;
        width: 100%;
        min-width: 0;
        height: calc(100vh - 44px);
        margin-left: 0;
        z-index: 1100;
    }
    #sidebar.collapsed {
        margin-left: 0;
        transform: translateX(-100%);
    }
    #sidebar-toggle {
        display: block;
    }
    #sidebar.collapsed ~ #sidebar-toggle {
        display: block;
    }
    #map {
        width: 100%;
    }
}

/* Leaflet overrides */
.leaflet-tooltip {
    font-size: 12px;
    padding: 4px 8px;
}
