* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-1: #eaf3ee;
    --bg-2: #f4f7fb;
    --panel: #ffffff;
    --panel-soft: #f7faf9;
    --text-strong: #18272d;
    --text: #2b3f47;
    --muted: #60747b;
    --border: #d2dde1;
    --accent: #157a68;
    --accent-strong: #0f5f52;
    --danger: #a9442f;
    --danger-bg: #fff1ec;
    --shadow: 0 14px 36px rgba(14, 42, 50, 0.09);
    --radius-lg: 16px;
    --radius-md: 12px;
    --radius-sm: 8px;
}

body {
    font-family: Manrope, "IBM Plex Sans", "Segoe UI", sans-serif;
    color: var(--text);
    background:
        radial-gradient(circle at 10% 10%, rgba(28, 153, 119, 0.14) 0%, rgba(28, 153, 119, 0) 36%),
        radial-gradient(circle at 90% 0%, rgba(237, 145, 96, 0.12) 0%, rgba(237, 145, 96, 0) 34%),
        linear-gradient(140deg, var(--bg-1) 0%, var(--bg-2) 100%);
    min-height: 100vh;
    padding: 12px;
    overflow: hidden;
}

.container {
    display: flex;
    flex-direction: column;
    gap: 10px;
    height: calc(100vh - 24px);
    min-height: 0;
}

.panel-card {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.98) 0%, rgba(248, 252, 251, 0.96) 100%);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 18px;
    flex-shrink: 0;
}

.app-header-main h1 {
    font-size: 20px;
    line-height: 1.2;
    color: var(--text-strong);
    letter-spacing: -0.01em;
}

.subtitle {
    margin-top: 4px;
    color: var(--muted);
    font-size: 13px;
}

.header-badge {
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;
    background: rgba(21, 122, 104, 0.12);
    color: var(--accent-strong);
    border: 1px solid rgba(21, 122, 104, 0.2);
    border-radius: 999px;
    padding: 7px 12px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-weight: 700;
    text-decoration: none;
}

.header-badge:hover {
    background: rgba(21, 122, 104, 0.18);
    border-color: rgba(21, 122, 104, 0.35);
}

.main-layout {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 360px;
    gap: 10px;
}

.workspace {
    min-height: 0;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.workspace-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
}

.workspace-head h2 {
    font-size: 13px;
    letter-spacing: 0.07em;
    text-transform: uppercase;
    font-weight: 800;
    color: var(--text-strong);
}

.workspace-head p {
    font-size: 12px;
    color: var(--muted);
}

.maps-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    min-height: 0;
    flex: 1;
}

.map-panel {
    background: var(--panel-soft);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.map-panel h3 {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-strong);
    font-weight: 700;
    background: #f5faf8;
}

.map-container {
    border-top: none;
    border-radius: 0 0 var(--radius-md) var(--radius-md);
    background: linear-gradient(160deg, #f8fcfa 0%, #f3f7fb 100%);
    position: relative;
    overflow: hidden;
    flex: 1;
    min-height: 0;
    display: flex;
}

.map-container svg {
    width: 100%;
    height: 100%;
    display: block;
    cursor: grab;
    min-width: 100%;
    min-height: 100%;
}

.map-container svg:active {
    cursor: grabbing;
}

.control-dock {
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: auto;
    padding-right: 2px;
}

.dock-section {
    padding: 12px;
}

.data-section h3,
.status h3,
.control-section-title {
    color: var(--text-strong);
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    font-weight: 800;
}

.data-section-content {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.file-upload-wrapper {
    display: flex;
    min-width: 0;
}

.file-upload-wrapper input[type="file"] {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 12px;
    cursor: pointer;
    background: white;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.file-upload-wrapper input[type="file"]:hover {
    border-color: #8eacb3;
}

.file-upload-wrapper input[type="file"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(21, 122, 104, 0.16);
}

.file-info {
    font-size: 12px;
    color: var(--muted);
    font-weight: 500;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    background: #edf5f6;
    border: 1px solid var(--border);
}

.status-content {
    margin-top: 10px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
}

.status-message {
    font-size: 12px;
    color: var(--muted);
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: #f5f9fa;
    line-height: 1.45;
}

.status strong {
    color: var(--text-strong);
}

.status-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.stat-item {
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: #fbfdfd;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.stat-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--muted);
    font-weight: 700;
}

.stat-value {
    font-size: 13px;
    color: var(--text-strong);
    font-weight: 700;
}

.controls {
    padding: 12px;
}

.controls-grid {
    display: grid;
    gap: 14px;
}

.control-section {
    border: 1px solid var(--border);
    border-left: 3px solid var(--accent);
    border-radius: var(--radius-sm);
    padding: 10px;
    background: #f9fcfb;
}

.control-section-title {
    margin-bottom: 8px;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-bottom: 10px;
    position: relative;
}

.control-group:last-child {
    margin-bottom: 0;
}

.control-group label {
    font-weight: 600;
    color: var(--text);
    font-size: 13px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.control-group input[type="range"] {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #d8e6e7;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

.control-group input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 17px;
    height: 17px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
}

.control-group input[type="range"]::-moz-range-thumb {
    width: 17px;
    height: 17px;
    border-radius: 50%;
    background: var(--accent);
    cursor: pointer;
    border: none;
}

.control-group select,
.control-group input[type="number"] {
    padding: 9px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: var(--text);
    background: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.control-group select:focus,
.control-group input[type="number"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(21, 122, 104, 0.14);
}

.control-group input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--accent);
}

.dual-input {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 78px;
    gap: 8px;
    align-items: center;
}

.dual-input input[type="number"] {
    text-align: right;
}

.value-display {
    font-size: 12px;
    color: var(--muted);
    font-weight: 600;
}

.control-group[data-help]::after {
    content: attr(data-help);
    position: absolute;
    right: 0;
    bottom: calc(100% + 8px);
    min-width: 180px;
    max-width: 240px;
    padding: 8px 10px;
    border-radius: 8px;
    border: 1px solid rgba(19, 44, 54, 0.22);
    background: rgba(14, 30, 38, 0.96);
    color: #f2f8fb;
    font-size: 11px;
    line-height: 1.4;
    text-align: left;
    box-shadow: 0 8px 20px rgba(6, 20, 27, 0.28);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transform: translateY(2px);
    transition: opacity 0.16s ease, transform 0.16s ease, visibility 0.16s ease;
    z-index: 6;
}

.control-group[data-help]::before {
    content: "";
    position: absolute;
    right: 6px;
    bottom: calc(100% + 2px);
    width: 8px;
    height: 8px;
    background: rgba(14, 30, 38, 0.96);
    border-left: 1px solid rgba(19, 44, 54, 0.22);
    border-top: 1px solid rgba(19, 44, 54, 0.22);
    transform: rotate(45deg);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.16s ease, visibility 0.16s ease;
    z-index: 5;
}

.control-group[data-help]:hover::after,
.control-group[data-help]:hover::before,
.control-group[data-help]:focus-within::after,
.control-group[data-help]:focus-within::before {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.control-help {
    display: none;
}

.reset-button,
.export-button {
    padding: 9px 12px;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 12px;
    font-weight: 700;
    transition: transform 0.2s, box-shadow 0.2s, background 0.2s, opacity 0.2s;
}

.reset-button {
    background: #4f7680;
    color: #fff;
}

.reset-button:hover:not(:disabled) {
    background: #3f6169;
    transform: translateY(-1px);
}

.export-button {
    background: var(--accent);
    color: #fff;
}

.export-button:hover:not(:disabled) {
    background: var(--accent-strong);
    transform: translateY(-1px);
}

.reset-button:disabled,
.export-button:disabled {
    background: #cfd9dc;
    color: #f7f9fa;
    cursor: not-allowed;
    opacity: 0.9;
    transform: none;
}

#error-container {
    flex-shrink: 0;
}

#error-container:empty {
    display: none;
}

.error {
    background: var(--danger-bg);
    color: var(--danger);
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid #f0c0b2;
    font-size: 13px;
}

.loading {
    text-align: center;
    padding: 40px;
    color: var(--muted);
}

.map-state {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 18px;
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.01em;
    z-index: 1;
    backdrop-filter: blur(1.5px);
}

.map-state-info {
    color: #1e4f5c;
    background: linear-gradient(160deg, rgba(230, 244, 247, 0.9) 0%, rgba(240, 249, 251, 0.9) 100%);
}

.map-state-muted {
    color: #4f6670;
    background: linear-gradient(160deg, rgba(242, 248, 249, 0.92) 0%, rgba(245, 249, 252, 0.92) 100%);
}

.map-state-error {
    color: var(--danger);
    background: linear-gradient(160deg, rgba(255, 239, 234, 0.95) 0%, rgba(255, 245, 242, 0.95) 100%);
}

.borough {
    fill: #4a90e2;
    stroke: #ffffff;
    stroke-width: 1.5;
    cursor: pointer;
    transition: fill 0.2s;
}

.borough:hover {
    fill: #357abd;
}

.grid-cell {
    fill: #4a90e2;
    stroke: #ffffff;
    stroke-width: 1.5;
    cursor: pointer;
    transition: fill 0.2s;
}

.grid-cell:hover {
    fill: #357abd;
}

.borough.linked-highlight,
.grid-cell.linked-highlight {
    stroke: #0f2430;
    stroke-width: 2.5;
    filter: saturate(1.2) brightness(0.95);
}

.grid-cell.editable {
    cursor: move;
}

.grid-cell.dragging {
    opacity: 0.6;
    stroke: #101010;
    stroke-width: 2;
}

.ghost {
    pointer-events: none;
    opacity: 0.7;
    stroke: #111;
    stroke-dasharray: 4 3;
}

.centroid {
    fill: #e74c3c;
    stroke: white;
    stroke-width: 2;
    r: 4;
}

.grid-label {
    font-size: 10px;
    fill: #26363c;
    text-anchor: middle;
    pointer-events: none;
    font-weight: 600;
}

.zoom-controls {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.zoom-button {
    width: 34px;
    height: 34px;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 7px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    user-select: none;
    color: var(--text);
    transition: transform 0.2s, background 0.2s, border-color 0.2s;
}

.zoom-button:hover {
    background: #f1f7f6;
    border-color: #8eabb3;
    transform: translateY(-1px);
}

.zoom-button:active {
    transform: translateY(0);
}

.tooltip {
    position: absolute;
    background: rgba(9, 20, 28, 0.88);
    color: #ffffff;
    padding: 7px 10px;
    border-radius: 6px;
    font-size: 12px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.18s;
}

.tooltip.visible {
    opacity: 1;
}

.footer {
    text-align: center;
    padding: 2px 0;
    font-size: 12px;
    color: #4d646b;
    flex-shrink: 0;
}

.footer a {
    color: #2f4d55;
    text-decoration: underline;
    text-underline-offset: 2px;
    font-weight: 700;
}

.footer a:hover {
    color: #1a3740;
}

@media (max-width: 1280px) {
    .main-layout {
        grid-template-columns: minmax(0, 1fr) 320px;
    }
}

@media (max-width: 1080px) {
    body {
        overflow: auto;
    }

    .container {
        height: auto;
        min-height: calc(100vh - 24px);
    }

    .main-layout {
        grid-template-columns: 1fr;
    }

    .control-dock {
        overflow: visible;
        padding-right: 0;
    }
}

@media (max-width: 820px) {
    .maps-container {
        grid-template-columns: 1fr;
        min-height: 740px;
    }

    .app-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .header-badge {
        align-self: flex-start;
    }
}
