:root {
    --ink: #1b2430;
    --ink-soft: #55606e;
    --paper: #f7f2e7;
    --paper-deep: #efe8d8;
    --card: #ffffff;
    --line: #e6dfcc;
    --line-card: #e6e1d3;

    --brand: #1e5a82;
    --brand-dark: #123449;
    --brand-tint: #e8f1f7;

    --gold: #b9791f;
    --gold-tint: #faf0dc;

    --crimson: #a2312c;
    --crimson-tint: #faeceb;

    --green: #2f7a4f;
    --green-tint: #eaf5ee;

    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 18px;
    --shadow: 0 1px 2px rgba(27, 36, 48, 0.04), 0 8px 24px -12px rgba(27, 36, 48, 0.18);
    --shadow-lift: 0 12px 40px -12px rgba(27, 36, 48, 0.35);
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
}

body {
    margin: 0;
    font-family: "Manrope", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--paper);
    color: var(--ink);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

h1, h2, h3, .serif { font-family: "Source Serif 4", Georgia, serif; }

a { color: var(--brand); }

/* ---------- Header ---------- */

header.topbar {
    display: flex;
    align-items: center;
    gap: 1.25rem;
    padding: 0.7rem 1.25rem;
    background: var(--card);
    border-bottom: 1px solid var(--line);
    flex-wrap: wrap;
    z-index: 30;
}

.brand {
    display: flex;
    align-items: center;
    gap: 0.65rem;
}

.brand-mark { width: 34px; height: 34px; flex: none; }

.brand-text h1 {
    margin: 0;
    font-size: 1.28rem;
    line-height: 1.1;
    color: var(--brand-dark);
    letter-spacing: 0.01em;
}

.brand-text .byline {
    margin: 0;
    font-size: 0.72rem;
    color: var(--ink-soft);
    letter-spacing: 0.02em;
}

.event-chip {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    background: var(--crimson-tint);
    color: var(--crimson);
    border: 1px solid rgba(162, 49, 44, 0.18);
    padding: 0.32rem 0.7rem;
    border-radius: 999px;
    font-size: 0.74rem;
    font-weight: 600;
}
.event-chip .dot {
    width: 6px; height: 6px; border-radius: 50%; background: var(--crimson);
}

.topbar-spacer { flex: 1; }

.status {
    font-size: 0.72rem;
    font-weight: 600;
    padding: 0.3rem 0.65rem;
    border-radius: 999px;
    background: var(--paper-deep);
    color: var(--ink-soft);
}
.status.ok { background: var(--green-tint); color: var(--green); }
.status.busy { background: var(--brand-tint); color: var(--brand); }
.status.error { background: var(--crimson-tint); color: var(--crimson); }

.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.5rem 0.85rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line-card);
    background: var(--card);
    color: var(--ink);
    font-family: inherit;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease;
    white-space: nowrap;
}
.btn:hover { background: var(--paper); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 15px; height: 15px; flex: none; }

.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-gold { background: var(--gold); border-color: var(--gold); color: #fff; }
.btn-gold:hover { background: #a06b19; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--paper-deep); }
.btn-full { width: 100%; justify-content: center; }
.btn-sm { padding: 0.35rem 0.6rem; font-size: 0.74rem; }

/* ---------- Layout ---------- */

main.layout {
    flex: 1;
    display: flex;
    min-height: 0;
}

.sidebar {
    width: 392px;
    min-width: 340px;
    background: var(--paper);
    border-right: 1px solid var(--line);
    overflow-y: auto;
    padding: 1rem 1rem 3rem;
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
}

.card {
    background: var(--card);
    border: 1px solid var(--line-card);
    border-radius: var(--radius);
    padding: 1rem;
    box-shadow: var(--shadow);
}

.card-head {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.55rem;
}
.card-head svg { width: 18px; height: 18px; color: var(--brand); flex: none; }
.card-head h2 {
    margin: 0;
    font-size: 0.98rem;
    color: var(--ink);
}
.card-head .count {
    margin-left: auto;
    font-size: 0.72rem;
    color: var(--ink-soft);
    font-weight: 600;
}

.muted { color: var(--ink-soft); margin: 0.2rem 0; font-size: 0.82rem; line-height: 1.45; }
.tiny { font-size: 0.72rem; }

/* ---------- Product list ---------- */

#product-search {
    width: 100%;
    padding: 0.5rem 0.6rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line-card);
    background: var(--paper);
    color: var(--ink);
    font-family: inherit;
    font-size: 0.82rem;
    margin-bottom: 0.5rem;
}

.product-list {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 260px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.product-card {
    padding: 0.55rem 0.65rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    border: 1px solid var(--line-card);
    background: var(--paper);
    transition: border-color 0.12s ease, background 0.12s ease;
}
.product-card:hover { border-color: var(--brand); }
.product-card.selected { border-color: var(--brand); background: var(--brand-tint); }

.product-card .title {
    font-weight: 700;
    font-size: 0.82rem;
    display: block;
}
.product-card .filename {
    display: block;
    font-size: 0.68rem;
    color: var(--ink-soft);
    word-break: break-all;
    margin-top: 0.15rem;
}
.product-card .meta-row {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.35rem;
    flex-wrap: wrap;
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    padding: 0.15rem 0.45rem;
    border-radius: 999px;
    text-transform: uppercase;
}
.badge-confirmed { background: var(--green-tint); color: var(--green); }
.badge-unconfirmed { background: var(--gold-tint); color: var(--gold); }
.badge-unknown { background: var(--paper-deep); color: var(--ink-soft); }
.badge-size { background: var(--paper-deep); color: var(--ink-soft); text-transform: none; font-weight: 600; }

/* ---------- Forms ---------- */

label {
    display: block;
    margin: 0.6rem 0 0.25rem;
    font-size: 0.76rem;
    font-weight: 700;
    color: var(--ink-soft);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
label:first-of-type { margin-top: 0; }

select, input[type="number"], input[type="text"], input[type="email"], input[type="tel"], textarea {
    width: 100%;
    padding: 0.5rem 0.6rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line-card);
    background: var(--paper);
    color: var(--ink);
    font-family: inherit;
    font-size: 0.85rem;
}
textarea { resize: vertical; min-height: 4.5rem; }
select:focus, input:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }

.field-hint { font-size: 0.72rem; color: var(--ink-soft); margin: 0.2rem 0 0; }

details.advanced {
    margin-top: 0.7rem;
    border-top: 1px dashed var(--line-card);
    padding-top: 0.5rem;
}
details.advanced summary {
    cursor: pointer;
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--brand);
    list-style: none;
}
details.advanced summary::-webkit-details-marker { display: none; }
details.advanced summary::before { content: "▸ "; }
details.advanced[open] summary::before { content: "▾ "; }

.resolution-toggle {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.3rem;
}
.resolution-toggle button {
    flex: 1;
    padding: 0.45rem 0.3rem;
    font-size: 0.72rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line-card);
    background: var(--paper);
    color: var(--ink-soft);
    cursor: pointer;
    font-family: inherit;
    font-weight: 700;
}
.resolution-toggle button.active { background: var(--brand); border-color: var(--brand); color: #fff; }

/* ---------- Caution / callout boxes ---------- */

.callout {
    display: flex;
    gap: 0.55rem;
    padding: 0.6rem 0.7rem;
    border-radius: var(--radius-sm);
    font-size: 0.78rem;
    line-height: 1.45;
    margin-top: 0.6rem;
}
.callout svg { width: 16px; height: 16px; flex: none; margin-top: 0.1rem; }
.callout-caution { background: var(--gold-tint); color: #7a5416; border: 1px solid rgba(185, 121, 31, 0.25); }
.callout-info { background: var(--brand-tint); color: var(--brand-dark); border: 1px solid rgba(30, 90, 130, 0.2); }
.callout-success { background: var(--green-tint); color: #205937; border: 1px solid rgba(47, 122, 79, 0.2); }

/* ---------- Results ---------- */

.results-summary {
    background: var(--paper);
    border: 1px solid var(--line-card);
    border-radius: var(--radius-sm);
    padding: 0.75rem;
    font-size: 0.86rem;
    line-height: 1.55;
}
.results-summary strong { color: var(--brand-dark); }

.stat-row {
    display: flex;
    gap: 0.6rem;
    margin: 0.6rem 0;
}
.stat {
    flex: 1;
    background: var(--card);
    border: 1px solid var(--line-card);
    border-radius: var(--radius-sm);
    padding: 0.55rem 0.6rem;
    text-align: center;
}
.stat .num { font-family: "Source Serif 4", serif; font-size: 1.35rem; color: var(--brand-dark); display: block; }
.stat .label { font-size: 0.66rem; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.03em; }

.priority-list { list-style: none; margin: 0.5rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.priority-list li {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    padding: 0.4rem 0.55rem;
    background: var(--paper);
    border: 1px solid var(--line-card);
    border-radius: var(--radius-sm);
    font-size: 0.76rem;
}
.priority-list .rank {
    font-family: "Source Serif 4", serif;
    color: var(--crimson);
    font-weight: 700;
    font-size: 0.9rem;
}
.priority-list .p-main { flex: 1; }
.priority-list .p-loc { color: var(--ink-soft); }

.result-actions { display: flex; gap: 0.5rem; margin-top: 0.7rem; flex-wrap: wrap; }

/* ---------- Reference sites ---------- */

.site-legend { display: flex; flex-direction: column; gap: 0.35rem; }
.site-row { display: flex; align-items: center; gap: 0.5rem; font-size: 0.78rem; }
.site-row .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.site-row .dot.high { background: var(--brand); }
.site-row .dot.medium { background: var(--gold); }
.site-row .dot.derived { background: var(--ink-soft); }
.site-row .note { color: var(--ink-soft); font-size: 0.72rem; }

/* ---------- Map ---------- */

.map-wrap { flex: 1; position: relative; min-width: 0; }
#map { width: 100%; height: 100%; background: var(--paper-deep); }

.map-legend {
    position: absolute;
    left: 12px;
    bottom: 12px;
    z-index: 500;
    background: rgba(255, 255, 255, 0.94);
    border: 1px solid var(--line-card);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.65rem;
    font-size: 0.7rem;
    box-shadow: var(--shadow);
    display: flex;
    gap: 0.9rem;
}
.map-legend .item { display: flex; align-items: center; gap: 0.35rem; }
.map-legend .swatch { width: 12px; height: 12px; border-radius: 3px; }

.leaflet-popup-content { color: var(--ink); font-family: "Manrope", sans-serif; font-size: 0.8rem; }
.leaflet-popup-content-wrapper { border-radius: var(--radius-sm); }

/* ---------- Chat drawer ---------- */

.chat-drawer {
    position: fixed;
    top: 0;
    right: -420px;
    width: 400px;
    max-width: 92vw;
    height: 100%;
    background: var(--card);
    border-left: 1px solid var(--line);
    box-shadow: var(--shadow-lift);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    transition: right 0.24s cubic-bezier(.2,.8,.2,1);
}
.chat-drawer.open { right: 0; }

.chat-head {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--line);
    background: var(--brand-tint);
}
.chat-head .avatar {
    width: 30px; height: 30px; border-radius: 50%;
    background: var(--brand); color: #fff;
    display: flex; align-items: center; justify-content: center;
    flex: none;
}
.chat-head .avatar svg { width: 16px; height: 16px; }
.chat-head h3 { margin: 0; font-size: 0.95rem; color: var(--brand-dark); }
.chat-head p { margin: 0; font-size: 0.7rem; color: var(--ink-soft); }
.chat-close { margin-left: auto; }

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
}

.msg { max-width: 88%; padding: 0.55rem 0.75rem; border-radius: 14px; font-size: 0.84rem; line-height: 1.5; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--paper); border: 1px solid var(--line-card); border-bottom-left-radius: 4px; }
.msg.user { align-self: flex-end; background: var(--brand); color: #fff; border-bottom-right-radius: 4px; }
.msg.typing { color: var(--ink-soft); font-style: italic; }

.chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    padding: 0 1rem 0.7rem;
}
.chip {
    font-size: 0.72rem;
    padding: 0.35rem 0.65rem;
    border-radius: 999px;
    border: 1px solid var(--line-card);
    background: var(--paper);
    color: var(--ink-soft);
    cursor: pointer;
    font-family: inherit;
}
.chip:hover { border-color: var(--brand); color: var(--brand); }

.chat-input-row {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem;
    border-top: 1px solid var(--line);
}
.chat-input-row input {
    flex: 1;
}
.chat-fab {
    position: fixed;
    right: 1.4rem;
    bottom: 1.4rem;
    z-index: 900;
    border-radius: 999px;
    padding: 0.8rem 1.15rem;
    box-shadow: var(--shadow-lift);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
}

/* ---------- Modal ---------- */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(20, 26, 33, 0.5);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 1100;
    padding: 1rem;
}
.modal-overlay.open { display: flex; }

.modal {
    background: var(--card);
    border-radius: var(--radius-lg);
    width: 480px;
    max-width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lift);
}
.modal-head {
    padding: 1.1rem 1.3rem 0.4rem;
}
.modal-head h2 { margin: 0 0 0.2rem; font-size: 1.15rem; color: var(--brand-dark); }
.modal-body { padding: 0.4rem 1.3rem 1.3rem; }
.modal-foot { padding: 0 1.3rem 1.3rem; display: flex; gap: 0.6rem; justify-content: flex-end; }

.urgency-toggle { display: flex; gap: 0.4rem; }
.urgency-toggle button {
    flex: 1;
    padding: 0.5rem 0.4rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line-card);
    background: var(--paper);
    font-family: inherit;
    font-size: 0.76rem;
    font-weight: 700;
    cursor: pointer;
    color: var(--ink-soft);
}
.urgency-toggle button.active { border-color: var(--crimson); color: var(--crimson); background: var(--crimson-tint); }
.urgency-toggle button[data-level="standard"].active { border-color: var(--brand); color: var(--brand); background: var(--brand-tint); }

/* ---------- Spinner / toast ---------- */

.spinner-overlay {
    position: fixed;
    inset: 0;
    background: rgba(247, 242, 231, 0.88);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    gap: 0.8rem;
}
.spinner-overlay.show { display: flex; }
.spinner-ring {
    width: 44px; height: 44px;
    border: 4px solid var(--line-card);
    border-top-color: var(--brand);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.spinner-overlay p { color: var(--ink-soft); font-size: 0.85rem; margin: 0; }

.toast {
    position: fixed;
    bottom: 1.4rem;
    left: 50%;
    transform: translateX(-50%) translateY(120%);
    background: var(--ink);
    color: #fff;
    padding: 0.65rem 1.1rem;
    border-radius: 999px;
    font-size: 0.82rem;
    box-shadow: var(--shadow-lift);
    z-index: 2100;
    transition: transform 0.25s ease;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ---------- Footer note ---------- */

.footnote {
    font-size: 0.7rem;
    color: var(--ink-soft);
    line-height: 1.5;
    border-top: 1px dashed var(--line-card);
    padding-top: 0.7rem;
    margin-top: 0.2rem;
}

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
    main.layout { flex-direction: column; overflow-y: auto; }
    .sidebar { width: 100%; min-width: 0; border-right: none; border-bottom: 1px solid var(--line); overflow-y: visible; max-height: none; }
    .map-wrap { min-height: 62vh; }
    .chat-drawer { width: 100%; max-width: 100%; }
    header.topbar { padding: 0.6rem 0.8rem; }
    .event-chip { display: none; }
}
