/* ============================================================
   TOKENS
   ============================================================ */
:root {
    --brand: #db4437;
    --brand-dark: #c5372c;
    --brand-tint: #fce8e6;
    --ink: #202124;
    --muted: #5f6368;
    --faint: #80868b;
    --line: #e8eaed;
    --field-line: #dadce0;
    --hover: #f1f3f4;
    --surface: #ffffff;
    --ocean: #728bb3;
    --sidebar-w: 400px;
    --shadow-1: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);
    --shadow-2: 0 2px 6px 2px rgba(60, 64, 67, 0.15), 0 1px 2px rgba(60, 64, 67, 0.3);
    --shadow-3: 0 8px 28px rgba(32, 33, 36, 0.28);
    --ease: cubic-bezier(0.4, 0, 0.2, 1);
    --font: 'Roboto', system-ui, -apple-system, 'Segoe UI', Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    display: flex;
    overflow: hidden;
    font-family: var(--font);
    color: var(--ink);
    background: var(--ocean);
    -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }

svg { fill: currentColor; flex-shrink: 0; }

.hidden { display: none !important; }

:focus-visible {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
}

/* ============================================================
   MAP
   ============================================================ */
#map {
    flex: 1;
    min-width: 0;
    height: 100vh;
    height: 100dvh;
    position: relative;
    z-index: 1;
    background: var(--ocean);
    outline: none;
}

.leaflet-container { font-family: var(--font); background: var(--ocean); }

.leaflet-bar {
    border: none !important;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: var(--shadow-1) !important;
}
.leaflet-bar a,
.leaflet-bar a:hover {
    border-bottom-color: var(--line);
    color: var(--muted);
}
.leaflet-bar a:hover { background: var(--hover); color: var(--ink); }
.leaflet-touch .leaflet-bar a { width: 40px; height: 40px; line-height: 40px; }

.leaflet-tooltip.territory-tooltip {
    padding: 6px 10px;
    border: none;
    border-radius: 8px;
    background: var(--surface);
    color: var(--ink);
    font-size: 13px;
    font-weight: 500;
    box-shadow: var(--shadow-2);
}
.leaflet-tooltip.territory-tooltip::before { display: none; }

/* ============================================================
   SIDEBAR
   Flex item in-flow: map menyusut otomatis saat sidebar dibuka.
   Konten memakai lebar tetap agar tidak reflow saat animasi width.
   ============================================================ */
#sidebar {
    position: relative;
    z-index: 999;
    flex-shrink: 0;
    width: var(--sidebar-w);
    height: 100vh;
    height: 100dvh;
    background: var(--surface);
    box-shadow: var(--shadow-2);
    overflow: hidden;
    transition: width 0.3s var(--ease), box-shadow 0.3s var(--ease);
}

#sidebar > div {
    width: var(--sidebar-w);
    height: 100%;
}

#sidebar.closed {
    width: 0;
    box-shadow: none;
}

#sidebarListView {
    display: flex;
    flex-direction: column;
}

/* ----- Top bar ----- */
#sidebarTopBar {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    gap: 4px;
    height: 56px;
    padding: 0 8px;
    flex-shrink: 0;
    background: var(--brand);
    color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#sidebarTopBar button {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: #fff;
    cursor: pointer;
    transition: background 0.15s var(--ease);
}
#sidebarTopBar button:hover { background: rgba(255, 255, 255, 0.18); }
#sidebarTopBar button:focus-visible { outline-color: #fff; }
#sidebarTopBar svg { width: 24px; height: 24px; }
#sidebarTitle {
    flex: 1;
    min-width: 0;
    margin: 0 4px;
    font-size: 17px;
    font-weight: 500;
    letter-spacing: 0.1px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ----- Search ----- */
#sidebarSearchRow {
    position: relative;
    display: flex;
    flex-shrink: 0;
    padding: 10px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
}
#sidebarSearchRow svg {
    position: absolute;
    top: 50%;
    left: 24px;
    width: 20px;
    height: 20px;
    transform: translateY(-50%);
    color: var(--faint);
    pointer-events: none;
}
#sidebarSearchInput {
    width: 100%;
    height: 40px;
    padding: 0 14px 0 42px;
    border: 1px solid transparent;
    border-radius: 20px;
    background: var(--hover);
    color: var(--ink);
    font: 400 14px var(--font);
    outline: none;
    transition: background 0.15s var(--ease), border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
#sidebarSearchInput::placeholder { color: var(--faint); }
#sidebarSearchInput:focus {
    background: var(--surface);
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-tint);
}

/* ----- Info panel ----- */
#sidebarInfoPanel {
    flex-shrink: 0;
    background: #fafafa;
    border-bottom: 1px solid var(--line);
}
#sidebarInfoHeader { display: none; }
#sidebarInfoBody { padding: 12px 16px; }
#sidebarInfoDescription {
    margin: 0;
    font-size: 13px;
    line-height: 1.5;
    color: var(--muted);
}
#sidebarInfoPanel:has(#sidebarInfoDescription:empty) { display: none; }

/* ----- List header ----- */
#sidebarListHeader {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    padding: 12px 16px 6px;
}
.list-label {
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.2px;
    color: var(--muted);
}
.chip-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 30px;
    padding: 0 12px 0 8px;
    border: 1px solid var(--field-line);
    border-radius: 15px;
    background: var(--surface);
    color: var(--brand);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s var(--ease), border-color 0.15s var(--ease);
}
.chip-btn svg { width: 18px; height: 18px; }
.chip-btn:hover { background: var(--brand-tint); border-color: var(--brand); }

/* ----- Region list ----- */
#regionListContainer {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding-bottom: 32px;
    scrollbar-width: thin;
    scrollbar-color: #c4c7c5 transparent;
}
#regionListContainer::-webkit-scrollbar { width: 8px; }
#regionListContainer::-webkit-scrollbar-thumb {
    background: #c4c7c5;
    border-radius: 4px;
    border: 2px solid var(--surface);
}

.region-list-empty {
    margin: 0;
    padding: 24px 20px;
    font-size: 14px;
    line-height: 1.5;
    color: var(--faint);
}

.region-block { border-bottom: 1px solid var(--line); }

.region-title-row {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 48px;
    padding: 4px 8px 4px 16px;
    cursor: pointer;
    user-select: none;
    transition: background 0.12s var(--ease);
}
.region-title-row:hover { background: #f8f9fa; }

.region-checkbox {
    width: 18px;
    height: 18px;
    margin: 0 4px 0 0;
    accent-color: var(--brand);
    cursor: pointer;
    flex-shrink: 0;
}
.region-name {
    flex: 1;
    min-width: 0;
    font-size: 14px;
    font-weight: 500;
    color: var(--ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.icon-btn {
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    transition: background 0.15s var(--ease), color 0.15s var(--ease);
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn.danger:hover { background: var(--brand-tint); color: var(--brand); }
.region-chevron svg { width: 22px; height: 22px; transition: transform 0.2s var(--ease); }
.region-block.is-collapsed .region-chevron svg { transform: rotate(-90deg); }

.gang-list { padding-bottom: 8px; }
.gang-list.collapsed { display: none; }

.gang-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 9px 16px 9px 46px;
    font-size: 14px;
    font-weight: 400;
    color: var(--ink);
    cursor: pointer;
    transition: background 0.12s var(--ease);
}
.gang-item > span:last-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.gang-item:hover { background: var(--hover); }
.gang-item.is-active { background: var(--brand-tint); font-weight: 500; }
.gang-item.hidden { display: none; }
.gang-item-empty { color: var(--faint); cursor: default; font-style: italic; }
.gang-item-empty:hover { background: transparent; }
.gang-item-more { color: var(--brand); font-weight: 500; }

.gang-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.18);
    transition: transform 0.15s var(--ease);
}
.gang-item:hover .gang-dot { transform: scale(1.3); }

/* ============================================================
   DETAIL VIEW
   ============================================================ */
#sidebarDetailView {
    display: block;
    overflow-y: auto;
    scrollbar-width: thin;
}

#gangHero {
    position: relative;
    height: 168px;
    overflow: hidden;
    background: linear-gradient(135deg, var(--hero-color, var(--brand)), #202124 140%);
}
#gangHero::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.28), transparent 55%);
    pointer-events: none;
}
#gangImage {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}
#gangImage[hidden] { display: none; }

#gangHeader { position: relative; z-index: 1; }
#gangNameBox {
    display: flex;
    align-items: center;
    gap: 4px;
    min-height: 60px;
    padding: 0 16px 0 8px;
    background: var(--brand);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#closeSidebar {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: #fff;
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s var(--ease);
}
#closeSidebar:hover { background: rgba(255, 255, 255, 0.18); }
#closeSidebar:focus-visible { outline-color: #fff; }
#closeSidebar svg { width: 24px; height: 24px; }
#gangName {
    flex: 1;
    min-width: 0;
    margin: 0 0 0 4px;
    font-size: 22px;
    font-weight: 500;
    line-height: 1.2;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: left;
}

#gangdescription .header-description {
    display: block;
    padding: 18px 20px 6px;
    font-size: 12px;
    font-weight: 500;
    letter-spacing: 0.2px;
    color: var(--muted);
}
#gangDescription {
    margin: 0;
    padding: 0 20px 18px;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.65;
    color: var(--ink);
    overflow-wrap: anywhere;
    white-space: pre-line;
}

.edit-territory-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: 40px;
    margin: 0 20px 28px;
    padding: 0 18px 0 14px;
    border: 1px solid var(--field-line);
    border-radius: 20px;
    background: var(--surface);
    color: var(--brand);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s var(--ease), border-color 0.15s var(--ease);
}
.edit-territory-btn svg { width: 18px; height: 18px; }
.edit-territory-btn:hover { background: var(--brand-tint); border-color: var(--brand); }

/* ============================================================
   SIDEBAR FLOAT TOGGLE
   ============================================================ */
.sidebar-toggle-float {
    position: fixed;
    top: 50%;
    left: var(--sidebar-w);
    z-index: 1001;
    display: grid;
    place-items: center;
    width: 24px;
    height: 56px;
    padding: 0;
    border: none;
    border-radius: 0 10px 10px 0;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
    box-shadow: 2px 1px 6px rgba(60, 64, 67, 0.3);
    transform: translateY(-50%);
    transition: left 0.3s var(--ease), background 0.15s var(--ease), color 0.15s var(--ease);
}
.sidebar-toggle-float svg { width: 22px; height: 22px; transition: transform 0.3s var(--ease); }
.sidebar-toggle-float:hover { background: var(--hover); color: var(--ink); }
#sidebar.closed + .sidebar-toggle-float { left: 0; }
#sidebar.closed + .sidebar-toggle-float svg { transform: rotate(180deg); }

/* ============================================================
   AUTH
   ============================================================ */
#authToggle {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px;
    border-radius: 24px;
    background: var(--surface);
    box-shadow: var(--shadow-1);
}
#userStatus {
    max-width: 200px;
    padding: 0 4px 0 12px;
    font-size: 13px;
    font-weight: 500;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
#userStatus:empty { display: none; }

.auth-btn {
    height: 36px;
    padding: 0 18px;
    border: none;
    border-radius: 18px;
    background: var(--brand);
    color: #fff;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s var(--ease);
}
.auth-btn:hover { background: var(--brand-dark); }
.auth-btn-danger { background: var(--hover); color: var(--ink); }
.auth-btn-danger:hover { background: #e3e5e8; }

/* ============================================================
   MODAL
   ============================================================ */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 10000;
    justify-content: center;
    align-items: center;
    padding: 16px;
    background: rgba(32, 33, 36, 0.55);
}
.modal.show { display: flex; }

.modal-content {
    display: flex;
    flex-direction: column;
    gap: 14px;
    width: min(440px, 100%);
    max-height: calc(100dvh - 32px);
    overflow-y: auto;
    padding: 24px;
    border-radius: 16px;
    background: var(--surface);
    box-shadow: var(--shadow-3);
}
.modal-content.modal-sm { width: min(360px, 100%); }
.modal.show .modal-content { animation: modal-in 0.2s var(--ease); }
.modal-content h2 {
    margin: 0 0 2px;
    font-size: 20px;
    font-weight: 500;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12px; font-weight: 500; color: var(--muted); }
.field-row { display: flex; gap: 12px; }
.field-grow { flex: 1; min-width: 0; }

.modal-content input,
.modal-content textarea,
.modal-content select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--field-line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--ink);
    font: 400 14px var(--font);
    outline: none;
    transition: border-color 0.15s var(--ease), box-shadow 0.15s var(--ease);
}
.modal-content input:focus,
.modal-content textarea:focus,
.modal-content select:focus {
    border-color: var(--brand);
    box-shadow: 0 0 0 3px var(--brand-tint);
}
.modal-content textarea { min-height: 110px; resize: vertical; line-height: 1.5; }
.modal-content input[type='color'] { width: 64px; height: 40px; padding: 3px; cursor: pointer; }
.modal-content input[type='file'] { padding: 7px 10px; font-size: 13px; }

#loginError {
    padding: 10px 12px;
    border-radius: 8px;
    background: var(--brand-tint);
    color: #a50e0e;
    font-size: 13px;
    line-height: 1.4;
}

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.btn-primary,
.btn-secondary,
.btn-danger {
    height: 40px;
    padding: 0 20px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s var(--ease), opacity 0.15s var(--ease);
}
.btn-primary { border: none; background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); }
.btn-primary:disabled { opacity: 0.6; cursor: progress; }
.btn-secondary { border: 1px solid var(--field-line); background: var(--surface); color: var(--muted); }
.btn-secondary:hover { background: var(--hover); }
.btn-danger { border: 1px solid var(--field-line); background: var(--surface); color: var(--brand); }
.btn-danger:hover { background: var(--brand-tint); border-color: var(--brand); }
.btn-danger:disabled { opacity: 0.6; cursor: progress; }

/* ============================================================
   TOAST
   ============================================================ */
#toastRegion {
    position: fixed;
    left: 50%;
    bottom: 24px;
    z-index: 15000;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    width: min(420px, calc(100vw - 32px));
    transform: translateX(-50%);
    pointer-events: none;
}
.toast {
    width: 100%;
    padding: 12px 16px;
    border-left: 4px solid var(--faint);
    border-radius: 8px;
    background: #323232;
    color: #fff;
    font-size: 14px;
    line-height: 1.4;
    box-shadow: var(--shadow-3);
    animation: toast-in 0.22s var(--ease);
    transition: opacity 0.2s var(--ease), transform 0.2s var(--ease);
}
.toast-success { border-left-color: #34a853; }
.toast-error { border-left-color: var(--brand); }
.toast.is-leaving { opacity: 0; transform: translateY(8px); }

/* ============================================================
   SPLASH
   ============================================================ */
#splash {
    position: fixed;
    inset: 0;
    z-index: 20000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    overflow: hidden;
    background: var(--ocean);
    transition: opacity 0.5s var(--ease), visibility 0s linear 0.5s;
}
/* Latar splash: gambar peta yang diburamkan, dengan lapisan terang agar logo tetap terbaca */
#splash::before {
    content: '';
    position: absolute;
    inset: -48px;
    background: url('assets/gta-map-blur.jpg') center / cover no-repeat;
    filter: blur(14px);
}
#splash::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.38);
}
#splash > * { position: relative; z-index: 1; }
#splashLogo {
    width: min(460px, 72vw);
    height: auto;
    animation: splash-in 0.7s var(--ease) both;
    transition: transform 0.5s var(--ease);
}
.splash-bar {
    position: relative;
    width: 168px;
    height: 3px;
    overflow: hidden;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.6);
}
.splash-bar span {
    position: absolute;
    inset: 0 auto 0 0;
    width: 40%;
    border-radius: 2px;
    background: var(--brand);
    animation: splash-bar 1.1s var(--ease) infinite;
}
.splash-text { margin: 0; font-size: 13px; font-weight: 500; color: var(--ink); }
#splash.is-leaving { opacity: 0; visibility: hidden; pointer-events: none; }
#splash.is-leaving #splashLogo { transform: scale(1.05); }

/* ============================================================
   KEYFRAMES
   ============================================================ */
@keyframes splash-in {
    from { opacity: 0; transform: scale(0.94); }
    to   { opacity: 1; transform: scale(1); }
}
@keyframes splash-bar {
    0%   { transform: translateX(-100%); }
    100% { transform: translateX(250%); }
}
@keyframes modal-in {
    from { opacity: 0; transform: translateY(10px) scale(0.98); }
    to   { opacity: 1; transform: none; }
}
@keyframes toast-in {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: none; }
}

/* ============================================================
   RESPONSIVE: bottom sheet pada layar sempit
   ============================================================ */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 54vh;
        height: 54dvh;
        border-radius: 16px 16px 0 0;
        transition: transform 0.3s var(--ease);
    }
    #sidebar > div { width: 100%; }
    #sidebar.closed { width: 100%; transform: translateY(100%); }
    #sidebarTopBar { border-radius: 16px 16px 0 0; }
    #gangNameBox { box-shadow: none; }
    #gangHero { height: 120px; }

    .sidebar-toggle-float {
        top: auto;
        left: 50%;
        bottom: 54vh;
        bottom: 54dvh;
        width: 56px;
        height: 24px;
        border-radius: 10px 10px 0 0;
        transform: translateX(-50%);
        transition: bottom 0.3s var(--ease), background 0.15s var(--ease);
    }
    .sidebar-toggle-float svg { transform: rotate(-90deg); }
    #sidebar.closed + .sidebar-toggle-float { left: 50%; bottom: 0; }
    #sidebar.closed + .sidebar-toggle-float svg { transform: rotate(90deg); }

    #userStatus { display: none; }
    .leaflet-control-zoom { display: none; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }
    .splash-bar span { width: 100%; }
}


/* ============================================================
   PIN GAMBAR DI PETA
   ============================================================ */
.leaflet-marker-icon.map-pin-icon { background: none; border: none; }
.map-pin { position: relative; width: 40px; height: 40px; cursor: pointer; }
.map-pin-badge {
    width: 40px;
    height: 40px;
    padding: 3px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 4px rgba(32, 33, 36, 0.45);
    transform: scale(var(--pin-scale, 1));
    transition: transform 0.15s var(--ease);
}
.map-pin:hover .map-pin-badge,
.map-pin.is-selected .map-pin-badge { transform: scale(calc(var(--pin-scale, 1) * 1.14)); }
.map-pin.is-selected .map-pin-badge { box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.35), 0 1px 4px rgba(32, 33, 36, 0.45); }
.map-pin-core {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--pin-color, #1a73e8);
    color: #fff;
}
.map-pin-core img { width: 100%; height: 100%; padding: 8%; object-fit: contain; display: block; user-select: none; -webkit-user-drag: none; }
.map-pin-core svg { width: 60%; height: 60%; }
