:root {
    /* Apple System Colors - Light Mode (Default) */
    --app-bg: #F2F2F7;
    /* System Grouped Background */
    --card-bg: #FFFFFF;
    /* Secondary System Grouped Background */
    --text-primary: #000000;
    --text-secondary: #8E8E93;
    /* System Gray */
    --text-tertiary: #C7C7CC;
    /* System Gray 4 */

    /* Brand Colors */
    --accent-blue: #007AFF;
    /* System Blue */
    --accent-red: #FF3B30;
    /* System Red */
    --accent-green: #34C759;
    /* System Green */

    /* UI Elements */
    --separator: #C6C6C8;
    /* System Separator */
    --separator-non-opaque: rgba(60, 60, 67, 0.36);
    --fill-tertiary: rgba(118, 118, 128, 0.12);
    /* System Fill */
    --glass-bg: rgba(255, 255, 255, 0.75);

    /* Metrics */
    --radius-l: 22px;
    /* Large cards */
    --radius-m: 14px;
    /* Buttons/Inputs */
    --radius-s: 10px;
    /* Small elements */

    /* Shadows */
    --shadow-soft: 0 4px 24px rgba(0, 0, 0, 0.06);
    --shadow-deep: 0 12px 48px rgba(0, 0, 0, 0.12);

    /* Safe Area */
    --safe-top: env(safe-area-inset-top, 20px);
    --safe-bottom: env(safe-area-inset-bottom, 20px);

    /* Legacy mapping */
    --divider: var(--separator-non-opaque);
    --btn-red: var(--accent-red);
    --btn-gray: var(--fill-tertiary);
    --info-panel-bottom: calc(20px + var(--safe-bottom));
}

@media (prefers-color-scheme: dark) {
    :root {
        --app-bg: #1C1C1E;
        --card-bg: #2C2C2E;
        --text-primary: #FFFFFF;
        --text-secondary: #EBEBF5;
        --text-tertiary: #EBEBF599;

        --separator: #38383A;
        --separator-non-opaque: rgba(84, 84, 88, 0.65);
        --fill-tertiary: rgba(118, 118, 128, 0.24);
        --glass-bg: rgba(44, 44, 46, 0.85);

        --shadow-soft: 0 4px 24px rgba(0, 0, 0, 0.4);
        --shadow-deep: 0 12px 48px rgba(0, 0, 0, 0.6);
    }
}

/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
}

body {
    background-color: var(--app-bg);
    color: var(--text-primary);
    width: 100%;
    height: 100dvh;
    /* Dynamic Viewport Height */
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    /* iOS Momentum Scrolling */
    -webkit-overflow-scrolling: touch;
}

/* Map Container */
#map {
    width: 100%;
    height: 100%;
    z-index: 1;
    /* Ensure map is behind everything */
}

/* 
   ---------------------------
   Info Panel ("Dynamic Island" Style)
   ---------------------------
*/
.info-panel-container {
    position: absolute;
    bottom: var(--info-panel-bottom);
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    width: auto;
    max-width: 90vw;
    transition: transform 0.4s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.3s ease;
    pointer-events: none;
}

.info-panel-container.hidden {
    transform: translate(-50%, 150%);
    opacity: 0;
    pointer-events: none;
}

.info-panel-glass {
    display: flex;
    align-items: center;
    background: var(--glass-bg);
    backdrop-filter: blur(25px) saturate(180%);
    -webkit-backdrop-filter: blur(25px) saturate(180%);
    padding: 6px;
    border-radius: 100px;
    /* Pill shape */
    box-shadow: var(--shadow-deep);
    border: 1px solid var(--separator-non-opaque);
    pointer-events: auto;
    /* CRITICAL: Enables clicks inside the pointer-events: none container */
}

.legend-item,
.kb-btn-icon {
    background: transparent;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border-radius: 100px;
    cursor: pointer;
    transition: background-color 0.2s ease;
    text-decoration: none;
    color: var(--text-primary);
}

.legend-item:active,
.kb-btn-icon:active {
    background-color: var(--fill-tertiary);
    transform: scale(0.96);
}

.legend-icon {
    font-size: 20px;
    margin-right: 8px;
}

.legend-label {
    font-size: 15px;
    font-weight: 600;
    margin-right: 8px;
    letter-spacing: -0.2px;
}

.legend-badge {
    background-color: var(--accent-blue);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    padding: 0 8px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
}

.legend-badge.badge-red {
    background-color: var(--accent-red);
}

.panel-divider {
    width: 1px;
    height: 24px;
    background-color: var(--separator-non-opaque);
    margin: 0 2px;
}

.kb-btn-icon {
    padding: 10px 14px;
    color: var(--accent-blue);
}

/* 
   ---------------------------
   Center Pin
   ---------------------------
*/
#center-pin-new {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 500;
    pointer-events: none;
    display: none;
    /* Hidden by default */
}

#center-pin-new.visible {
    display: block;
}

/* 
   ---------------------------
   Lists (Rows)
   ---------------------------
*/
.list-row {
    background-color: var(--card-bg);
    padding: 12px 16px;
    border-radius: var(--radius-m);
    display: flex;
    align-items: center;
    gap: 16px;
    transition: transform 0.1s, background-color 0.2s;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.02);
    cursor: pointer;
}

.list-row:active {
    background-color: var(--fill-tertiary);
    transform: scale(0.98);
}

.list-icon {
    font-size: 24px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--fill-tertiary);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.list-content {
    flex: 1;
    overflow: hidden;
}

.list-title {
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.list-time {
    font-size: 14px;
    color: var(--text-secondary);
}

/* 
   ---------------------------
   PWA Standalone Overrides
   ---------------------------
*/
/* 
   ---------------------------
   PWA Standalone Overrides (Removed for Universal Layout)
   ---------------------------
*/
/* 
@media all and (display-mode: standalone) {
    body {
        overscroll-behavior-y: none;
    }
} 
*/

.center-pin-svg path {
    fill: var(--accent-blue);
    stroke: #fff;
    stroke-width: 2px;
}

.center-pin-svg circle {
    fill: #fff;
}

.center-pin-svg {
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
}

/* 
   ---------------------------
   Bottom Sheet (iOS Modal)
   ---------------------------
*/
.bottom-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: var(--app-bg);
    border-radius: var(--radius-l) var(--radius-l) 0 0;
    box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.2);
    z-index: 5000;
    transform: translateY(110%);
    transition: transform 0.5s cubic-bezier(0.32, 0.72, 0, 1);
    display: flex;
    flex-direction: column;
    max-height: 94dvh;
    padding-bottom: calc(var(--safe-bottom) + 20px);
    /* Add extra padding for bottom buttons */
    pointer-events: auto;
    /* Ensure sheet itself captures clicks */
}

.bottom-sheet.visible {
    transform: translateY(0);
}

.sheet-handle-bar {
    width: 100%;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    cursor: grab;
}

.sheet-handle {
    width: 36px;
    height: 5px;
    background-color: var(--separator-non-opaque);
    border-radius: 3px;
}

.sheet-view-container {
    position: relative;
    width: 100%;
    flex: 1;
    overflow: hidden;
    min-height: 50dvh;
}

.sheet-view {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding: 0 16px min(80px, calc(var(--safe-bottom) + 40px)) 16px;
    display: block;
    background: transparent;
    transition: transform 0.4s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.3s;
}

#view-detail {
    display: flex;
    flex-direction: column;
}

/* Transitions */
.view-active {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
}

.view-left {
    transform: translateX(-30%);
    opacity: 0;
    pointer-events: none;
}

.view-right {
    transform: translateX(100%);
    /* Slide in from right fully */
    opacity: 1;
    /* Keep opacity to verify animation */
    pointer-events: none;
    box-shadow: -10px 0 30px rgba(0, 0, 0, 0.1);
    /* Subtle depth */
}

/* Header Areas */
.sheet-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0;
    height: 56px;
    position: sticky;
    top: 0;
    z-index: 10;
    padding: 0;
    background: var(--app-bg);
    border-bottom: 0.5px solid var(--separator);
    flex-shrink: 0;
}

.sheet-title {
    font-size: 17px;
    font-weight: 600;
    letter-spacing: -0.4px;
    color: var(--text-primary);
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    pointer-events: none;
    white-space: nowrap;
    z-index: 1;
}

/* On very small screens or dense headers, give title some room */
@media (max-width: 360px) {
    .sheet-title {
        font-size: 15px;
    }
}

.sheet-title.large {
    position: static;
    transform: none;
    font-size: 34px;
    font-weight: 700;
    margin-bottom: 8px;
    width: 100%;
}

.sheet-close-btn,
.back-btn {
    background: var(--fill-tertiary);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.back-btn {
    width: auto;
    padding: 0 12px 0 8px;
    border-radius: 100px;
    color: var(--accent-blue);
    background: transparent;
    font-size: 17px;
    font-weight: 400;
}

.back-btn:active,
.sheet-close-btn:active {
    opacity: 0.5;
}

/* 
   ---------------------------
   Lists (Inset Grouped)
   ---------------------------
*/
#list-container,
#kb-container {
    display: flex;
    flex-direction: column;
    gap: 0;
    background-color: var(--card-bg);
    border-radius: var(--radius-m);
    overflow: hidden;
    margin-bottom: 20px;
}

.list-row,
.kb-row-ios {
    background-color: var(--card-bg);
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: background-color 0.2s;
    cursor: pointer;
    position: relative;
}

/* Separator line for all but last item */
.list-row:not(:last-child)::after,
.kb-row-ios:not(:last-child)::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 60px;
    /* Aligned with text start */
    right: 0;
    height: 1px;
    background-color: var(--separator-non-opaque);
}

.kb-row-ios:not(:last-child)::after {
    left: 16px;
}

/* 
   ---------------------------
   KB Search Bar
   ---------------------------
*/
.ios-search-container {
    padding: 8px 0 8px 0;
    background: var(--app-bg);
    position: sticky;
    top: 56px;
    z-index: 9;
}

.ios-search-bar {
    background: var(--fill-tertiary);
    border-radius: 10px;
    height: 36px;
    display: flex;
    align-items: center;
    padding: 0 10px;
    gap: 6px;
}

.ios-search-icon {
    font-size: 14px;
    color: var(--text-secondary);
}

.ios-search-input {
    background: transparent;
    border: none;
    height: 100%;
    flex: 1;
    font-size: 17px;
    color: var(--text-primary);
    outline: none;
}

.ios-search-input::placeholder {
    color: var(--text-secondary);
}

/* 
   ---------------------------
   KB Row Styling
   ---------------------------
*/
.kb-row-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.kb-row-kw {
    font-size: 17px;
    font-weight: 600;
    color: var(--text-primary);
}

.kb-row-loc {
    font-size: 14px;
    color: var(--text-secondary);
}

.kb-row-arrow {
    font-size: 20px;
    color: var(--text-tertiary);
}

/* 
   ---------------------------
   Header Actions (Plus/Done)
   ---------------------------
*/
.sheet-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* Hover/Active states */
.list-row:active,
.kb-row-ios:active {
    background-color: var(--fill-tertiary);
}

/* 
   ---------------------------
   Overlays (Add/Move Mode)
   ---------------------------
*/
.add-mode-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.add-mode-overlay.visible {
    opacity: 1;
    /* Карта остаётся интерактивной — кнопки получают pointer-events отдельно */
    pointer-events: none;
}

.overlay-close-btn {
    position: fixed;
    top: max(20px, env(safe-area-inset-top));
    right: 20px;
    background: var(--glass-bg);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    color: var(--text-primary);
    border: none;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    box-shadow: var(--shadow-deep);
    cursor: pointer;
    z-index: 11000;
    pointer-events: auto;
}

.overlay-hint {
    font-size: 20px;
    font-weight: 700;
    color: white;
    text-align: center;
    padding: 0 40px;
    text-shadow: 0 2px 12px rgba(0, 0, 0, 0.8);
    position: absolute;
    top: 25%;
    width: 100%;
    pointer-events: none;
}

.overlay-actions {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    flex-direction: column;
    gap: 16px;
    align-items: center;
    padding: 20px;
    padding-bottom: max(50px, env(safe-area-inset-bottom, 30px));
    width: 100%;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.4) 0%, transparent 100%);
    pointer-events: auto;
}

/* 
   ---------------------------
   Header Buttons (Text)
   ---------------------------
*/
.sheet-close-btn {
    background: transparent;
    width: auto;
    height: auto;
    padding: 8px 12px;
    border-radius: 8px;
    color: var(--accent-blue);
    font-size: 17px;
    font-weight: 600;
    /* Done style */
}

.sheet-close-btn:active {
    opacity: 0.5;
    background: var(--fill-tertiary);
}

.back-btn {
    background: transparent;
    width: auto;
    height: auto;
    padding: 8px 0 8px 8px;
    color: var(--accent-blue);
    font-size: 17px;
    font-weight: 400;
    /* Back style */
    display: flex;
    align-items: center;
}

.back-btn::before {
    content: "‹";
    font-size: 28px;
    margin-right: 4px;
    margin-top: -4px;
    font-weight: 300;
}

/* 
   ---------------------------
   Forms & Inputs (iOS Style)
   ---------------------------
*/
.edit-form {
    padding: 0 16px;
    /* Add side padding for form */
}

.form-group {
    margin-bottom: 20px;
}

.form-label {
    margin-left: 16px;
    /* Align with input text */
    margin-bottom: 6px;
    font-size: 13px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.form-input {
    width: 100%;
    background: var(--card-bg);
    /* Grouped style often uses white on gray bg */
    border-radius: var(--radius-m);
    border: none;
    padding: 12px 16px;
    font-size: 17px;
    color: var(--text-primary);
    outline: none;
}

/* If the sheet background is white, inputs should be gray */
.sheet-view {
    background: var(--app-bg);
    /* Ensure sheet has contrast bg */
}

/* Override input background for contrast if sheet is light */
.form-input {
    background: var(--fill-tertiary);
    /* Light gray input on white/light sheet */
}

#edit-coords {
    padding: 12px 16px;
    background: var(--fill-tertiary);
    border-radius: var(--radius-m);
    font-size: 17px;
    color: var(--text-secondary);
}

/* ===========================
   Unified iOS Button (.ios-btn)
   =========================== */
.ios-btn {
    width: 100%;
    height: 50px;
    border-radius: 14px;
    border: none;
    font-size: 17px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s, transform 0.1s;
    background: var(--fill-tertiary);
    color: var(--accent-blue);
}

.ios-btn:active {
    transform: scale(0.97);
    opacity: 0.7;
}

.ios-btn.danger {
    color: var(--accent-red);
    background: rgba(255, 59, 48, 0.12);
}

.ios-btn.primary {
    background: var(--accent-blue);
    color: #fff;
}

.ios-btn-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: auto;
}

#detail-content {
    display: flex;
    flex-direction: column;
}

.ios-btn-group .ios-btn {
    border-radius: 14px;
}

/* Legacy support — old classes redirect */
.detail-actions {
    display: flex;
    flex-direction: column;
    gap: 0;
    margin-top: 24px;
    background: var(--card-bg);
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.08);
}

.detail-actions .action-btn {
    border-radius: 0;
    background: var(--card-bg);
    border-bottom: 0.5px solid var(--separator);
    box-shadow: none;
}

.detail-actions .action-btn:last-child {
    border-bottom: none;
}

.action-btn {
    flex: 1;
    height: 50px;
    border-radius: 14px;
    border: none;
    font-size: 17px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s, transform 0.1s;
}

.action-btn:active {
    opacity: 0.7;
    transform: scale(0.97);
}

.btn-secondary {
    background-color: transparent;
    color: var(--accent-blue);
}

.btn-danger {
    background-color: transparent;
    color: var(--accent-red);
}

.btn-primary {
    background-color: var(--accent-blue);
    color: #fff;
}

.btn-danger-ios {
    width: 100%;
    margin: 0;
    background-color: transparent;
    color: var(--accent-red);
    height: 52px;
    border-radius: 14px;
    border: none;
    font-size: 17px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
}

.btn-danger-ios:active {
    background-color: rgba(255, 59, 48, 0.1);
    transform: scale(0.97);
}

/* 
   ---------------------------
   Detail View Styling (Missing)
   ---------------------------
*/
.detail-card {
    background: var(--card-bg);
    border-radius: var(--radius-m);
    padding: 20px;
    margin-bottom: 20px;
    display: flex;
    flex-direction: column;
    flex: 1;
}

.detail-header-loc {
    font-size: 22px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 16px;
    line-height: 1.3;
    letter-spacing: -0.5px;
}

.msg-bubble {
    background: var(--fill-tertiary);
    padding: 12px 16px;
    border-radius: 18px 18px 18px 4px;
    margin-bottom: 4px;
    font-size: 16px;
    line-height: 1.45;
    color: var(--text-primary);
}

.msg-author {
    font-size: 13px;
    font-weight: 600;
    color: var(--accent-blue);
    margin-bottom: 4px;
    padding-left: 4px;
}

.msg-meta {
    font-size: 12px;
    color: var(--text-tertiary);
    text-align: right;
    margin-top: 2px;
    padding-right: 4px;
    margin-bottom: 16px;
}

/* Ensure empty text hints are visible */
.empty-hint {
    padding: 40px;
    text-align: center;
    color: var(--text-secondary);
    font-size: 15px;
}