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

:root {
    --header-h: 52px;
    --primary: #2c3e50;
    --accent: #3498db;
    --danger: #e74c3c;
    --bg: #ecf0f4;
    --border: #dde1e7;
    --shadow: 0 2px 12px rgba(0,0,0,0.15);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    background: var(--bg);
    height: 100vh;
    overflow: hidden;
    max-width: 100%;
    color: var(--primary);
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 100vw;
    overflow: hidden;
}

/* ── Header ─────────────────────────────────────────── */
header {
    flex-shrink: 0;
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    padding: 6px 12px;
    gap: 6px 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
    position: relative;
    z-index: 20;
}

header h1 {
    font-size: 1.05em;
    font-weight: 600;
    white-space: nowrap;
}

.header-maps {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px 6px;
    flex: 1;
    min-width: 0;
}

.header-actions {
    display: flex;
    gap: 8px;
}

.map-select {
    position: relative;
}

.map-select-btn {
    min-width: 150px;
    max-width: 18rem;
    padding: 5px 10px;
    font-size: 0.88em;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    text-align: left;
}

#mapSelectLabel {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.map-select-caret {
    font-size: 0.8em;
    opacity: 0.7;
    flex-shrink: 0;
}

.map-select-dropdown {
    position: fixed;
    max-width: 18rem;
    background: white;
    border-radius: 8px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    overflow: hidden;
    z-index: 200;
}

.map-select-dropdown.hidden { display: none; }

.map-select-option {
    padding: 9px 16px;
    cursor: pointer;
    font-size: 0.9em;
    color: var(--primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background 0.1s;
}

.map-select-option:hover { background: #f0f4f8; }

.map-select-option.active {
    font-weight: 600;
    color: var(--accent);
}

button {
    padding: 5px 12px;
    border-radius: 6px;
    border: 1px solid rgba(255,255,255,0.3);
    background: rgba(255,255,255,0.12);
    color: white;
    cursor: pointer;
    font-size: 0.85em;
    transition: background 0.15s;
    white-space: nowrap;
}

button:hover { background: rgba(255,255,255,0.22); }

button.mode-btn.active {
    background: var(--accent);
    border-color: var(--accent);
}

/* ── Canvas ──────────────────────────────────────────── */
#canvas-container {
    flex: 1;
    min-height: 0;
    width: 100%;
    position: relative;
    overflow: hidden;
}

#canvas {
    width: 100%;
    height: 100%;
    display: block;
    cursor: default;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.person-group { cursor: pointer; }
.rel-group { cursor: pointer; }

#canvas.panning {
    cursor: grab;
}

/* ── Relationship picker ─────────────────────────────── */
#rel-picker {
    position: fixed;
    z-index: 200;
    background: white;
    border-radius: 8px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    overflow: hidden;
    min-width: 190px;
}

#rel-picker.hidden { display: none; }

.picker-title {
    padding: 8px 14px;
    font-size: 0.78em;
    font-weight: 700;
    text-transform: uppercase;
    color: #999;
    letter-spacing: 0.05em;
    border-bottom: 1px solid var(--border);
}

.rel-option {
    padding: 9px 16px;
    cursor: pointer;
    font-size: 0.9em;
    font-family: monospace;
    transition: background 0.1s;
}

.rel-option:hover { background: var(--accent); color: white; }

.rel-option.cancel {
    font-family: inherit;
    border-top: 1px solid var(--border);
    color: #aaa;
}

.rel-option.cancel:hover { background: #f5f5f5; color: #666; }

/* ── Add Party Panel ─────────────────────────────────── */
.add-party-input-row {
    display: flex;
    gap: 6px;
    padding: 6px 10px 8px;
}

.add-party-input-row input {
    flex: 1;
    padding: 5px 8px;
    border: 1px solid var(--border);
    border-radius: 5px;
    font-size: 0.88em;
    font-family: inherit;
    color: var(--primary);
    outline: none;
}

.add-party-input-row input:focus { border-color: var(--accent); }

.add-party-btn {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
    padding: 5px 10px;
    font-size: 1em;
    font-weight: bold;
}

.add-party-btn:hover { background: #2980b9; }

.add-party-list {
    max-height: 100px;
    overflow-y: auto;
    border-top: 1px solid var(--border);
    font-size: 0.82em;
    color: #666;
}

.add-party-list.hidden { display: none; }

.add-party-list-item {
    padding: 4px 14px;
    border-bottom: 1px solid #f0f0f0;
}

.add-party-list-item:last-child { border-bottom: none; }

#add-party-panel { min-width: 220px; }
.add-party-close-btn { color: #888; font-size: 0.82em; }

/* ── Context menus ──────────────────────────────────── */
.ctx-menu {
    position: fixed;
    z-index: 200;
    background: white;
    border-radius: 8px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    overflow: hidden;
    min-width: 160px;
    cursor: default;
    user-select: none;
}

.ctx-menu.hidden { display: none; }

.ctx-label {
    padding: 8px 14px;
    font-size: 0.78em;
    font-weight: 700;
    text-transform: uppercase;
    color: #999;
    letter-spacing: 0.04em;
}

.ctx-label-edit {
    cursor: pointer;
}

.ctx-label-edit:hover {
    color: var(--accent);
}

.ctx-menu hr {
    border: none;
    border-top: 1px solid var(--border);
}

.ctx-option {
    padding: 9px 16px;
    cursor: pointer;
    font-size: 0.9em;
    transition: background 0.1s;
}

.ctx-option:hover { background: #f0f4f8; }

.ctx-option.ctx-delete { color: var(--danger); }
.ctx-option.ctx-delete:hover { background: #fef0f0; }

.ctx-sublabel {
    padding: 6px 14px 2px;
    font-size: 0.72em;
    font-weight: 600;
    text-transform: uppercase;
    color: #aaa;
    letter-spacing: 0.05em;
}

.ctx-levels {
    display: flex;
    gap: 6px;
    padding: 4px 14px 8px;
}

.ctx-level-btn {
    flex: 1;
    padding: 5px 0;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: white;
    color: var(--primary);
    cursor: pointer;
    font-size: 0.85em;
    font-weight: 600;
    transition: background 0.1s;
}

.ctx-level-btn:hover { background: #f0f4f8; }

.ctx-level-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* ── Legend ─────────────────────────────────────────── */
#legend {
    position: fixed;
    bottom: 16px;
    left: 16px;
    background: white;
    border-radius: 8px;
    border: 1px solid var(--border);
    padding: 10px 14px;
    font-size: 0.8em;
    box-shadow: var(--shadow);
    z-index: 10;
    pointer-events: none;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 5px;
    color: #444;
}

.legend-item:last-child { margin-bottom: 0; }

/* ── Hint ────────────────────────────────────────────── */
#hint {
    position: absolute;
    bottom: 16px;
    right: 16px;
    background: rgba(44, 62, 80, 0.82);
    color: white;
    border-radius: 6px;
    padding: 7px 12px;
    font-size: 0.8em;
    pointer-events: none;
    transition: opacity 0.2s;
}

/* ── Mobile ─────────────────────────────────────────── */
@media (max-width: 600px) {
    header h1 { font-size: 0.9em; }
    button { padding: 5px 8px; font-size: 0.8em; }
    .map-select-btn { min-width: 100px; font-size: 0.82em; }
    #legend { font-size: 0.75em; padding: 8px 10px; }
    #hint {
        bottom: auto;
        top: 8px;
        right: 8px;
        font-size: 0.72em;
        padding: 5px 9px;
        max-width: 42vw;
        white-space: normal;
        line-height: 1.4;
    }
}
