/* ============================================
   CSS Variables
   ============================================ */

:root {
    /* Background */
    --bg-main: #ffffff;
    --bg-sidebar: #fafafa;
    --bg-card: #ffffff;
    --bg-hover: #f5f5f7;
    --bg-active: #e8f0fe;

    /* Text */
    --text: #1d1d1f;
    --text-secondary: #6e6e73;
    --text-muted: #aeaeb2;

    /* Borders */
    --border: #e5e5e5;
    --border-light: #f0f0f0;

    /* Status Colors */
    --green: #34c759;
    --yellow: #ffcc00;
    --orange: #ff9500;
    --red: #ff3b30;
    --gray: #8e8e93;

    /* Accent */
    --accent: #007aff;

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);

    /* Sizing */
    --sidebar-width: 260px;
}

/* ============================================
   Reset & Base
   ============================================ */

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg-main);
    -webkit-font-smoothing: antialiased;
}

/* ============================================
   App Layout
   ============================================ */

.app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ============================================
   Sidebar
   ============================================ */

.sidebar {
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

/* Auth Button */
.auth-container {
    padding: 16px 16px 0;
}

.auth-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 10px;
    background: var(--bg-main);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
}

.auth-btn:hover {
    background: var(--bg-hover);
    border-color: var(--text-muted);
}

.auth-btn.signed-in {
    background: var(--bg-active, var(--bg-hover));
    border-color: var(--accent, #4285f4);
    color: var(--accent, #4285f4);
}

.auth-btn svg {
    width: 16px;
    height: 16px;
}

/* Heatmap Button */
.heatmap-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 16px 16px 12px;
    padding: 10px;
    background: var(--bg-main);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    color: var(--text);
    cursor: pointer;
    transition: all 0.15s;
}

.heatmap-btn:hover {
    background: var(--bg-hover);
    border-color: var(--text-muted);
}

.heatmap-btn.active {
    background: var(--text);
    color: white;
    border-color: var(--text);
}

.heatmap-btn svg {
    width: 16px;
    height: 16px;
}

/* Toggle */
.sidebar-toggle {
    margin: 0 16px 16px;
    background: var(--border-light);
    border-radius: 8px;
    padding: 3px;
    display: flex;
    position: relative;
}

.toggle-btn {
    flex: 1;
    padding: 8px 12px;
    border: none;
    background: transparent;
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 6px;
    position: relative;
    z-index: 2;
    transition: color 0.2s;
}

.toggle-btn.active {
    color: var(--text);
}

.toggle-slider {
    position: absolute;
    top: 3px;
    left: 3px;
    width: calc(50% - 3px);
    height: calc(100% - 6px);
    background: var(--bg-main);
    border-radius: 6px;
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s ease;
    z-index: 1;
}

.toggle-slider.right {
    transform: translateX(100%);
}

/* Filter Dropdowns */
.sidebar-filters {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 0 16px 12px;
}

.filter-select {
    width: 100%;
    padding: 8px 32px 8px 12px;
    font-size: 13px;
    font-family: inherit;
    color: var(--text);
    background: var(--bg-main);
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236e6e73' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    transition: border-color 0.15s;
}

.filter-select:hover {
    border-color: var(--text-muted);
}

.filter-select:focus {
    outline: none;
    border-color: var(--accent);
}

/* Sidebar List */
.sidebar-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 8px 16px;
}

.sidebar-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.1s;
    margin-bottom: 2px;
}

.sidebar-item:hover {
    background: var(--bg-hover);
}

.sidebar-item.active {
    background: var(--bg-active);
}

.sidebar-item-flag {
    width: 24px;
    height: 18px;
    object-fit: cover;
    border-radius: 2px;
    flex-shrink: 0;
}

.sidebar-item-icon {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.sidebar-item-name {
    flex: 1;
    font-weight: 500;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-item-status {
    display: flex;
    gap: 3px;
}

.status-dot-mini {
    width: 6px;
    height: 6px;
    border-radius: 50%;
}

.status-dot-mini.green { background: var(--green); }
.status-dot-mini.yellow { background: var(--yellow); }
.status-dot-mini.orange { background: var(--orange); }
.status-dot-mini.red { background: var(--red); }
.status-dot-mini.gray { background: var(--gray); }

/* ============================================
   Main Content
   ============================================ */

.main {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-main);
}

/* States */
.state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    padding: 40px;
}

.state-loading .spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 16px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.state-error .btn-retry {
    margin-top: 16px;
    padding: 10px 20px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
}

.empty-icon {
    font-size: 48px;
    margin-bottom: 16px;
    opacity: 0.5;
}

/* ============================================
   Detail Panel
   ============================================ */

.detail-panel {
    padding: 32px;
    max-width: 900px;
    margin: 0 auto;
}

.detail-header {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 32px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--border-light);
}

.detail-flag {
    width: 56px;
    height: 42px;
    object-fit: cover;
    border-radius: 4px;
    box-shadow: var(--shadow-sm);
}

.detail-icon {
    width: 56px;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    background: var(--bg-hover);
    border-radius: 12px;
}

.detail-title {
    font-size: 24px;
    font-weight: 600;
    letter-spacing: -0.02em;
    margin-bottom: 4px;
}

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

/* ============================================
   Project/Country Cards
   ============================================ */

.detail-cards {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

.project-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 14px;
    transition: box-shadow 0.15s;
}

.project-card:hover {
    box-shadow: var(--shadow-md);
}

.card-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 10px;
}

.card-title-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-flag {
    width: 24px;
    height: 18px;
    object-fit: cover;
    border-radius: 2px;
}

.card-icon {
    font-size: 16px;
}

.card-title {
    font-size: 14px;
    font-weight: 600;
}

.card-status {
    display: flex;
    align-items: center;
    gap: 8px;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.status-dot.green { background: var(--green); }
.status-dot.yellow { background: var(--yellow); }
.status-dot.orange { background: var(--orange); }
.status-dot.red { background: var(--red); }
.status-dot.gray { background: var(--gray); }

.status-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
}

/* Card Content */
.card-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 12px;
}

.card-field {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.card-field.full-width {
    grid-column: 1 / -1;
}

.field-label {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.field-value {
    font-size: 13px;
    color: var(--text);
    line-height: 1.4;
}

.field-value.empty {
    color: var(--text-muted);
    font-style: italic;
}

/* History */
.card-history {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border-light);
}

.history-title {
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin-bottom: 6px;
}

.history-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.history-item {
    display: flex;
    gap: 8px;
    font-size: 12px;
}

.history-date {
    color: var(--text-muted);
    flex-shrink: 0;
    width: 90px;
    font-size: 11px;
}

.history-text {
    color: var(--text);
    line-height: 1.3;
}

.history-hidden {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-top: 4px;
}

.history-expand {
    display: inline-flex;
    align-items: center;
    margin-top: 6px;
    padding: 0;
    background: none;
    border: none;
    color: var(--accent);
    font-size: 11px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    transition: opacity 0.15s;
}

.history-expand:hover {
    opacity: 0.7;
}

/* Parent Info */
.card-parent {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    color: var(--text-muted);
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border-light);
}

.card-parent-flag {
    width: 18px;
    height: 13px;
    object-fit: cover;
    border-radius: 2px;
}

/* History Section */
.card-history-section {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--border-light);
}

/* Mosaic Tiles */
.mosaic-tile {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 20px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.15s;
}

.mosaic-tile:hover {
    background: var(--bg-hover);
    border-color: var(--text-muted);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.mosaic-flag {
    width: 48px;
    height: 36px;
    object-fit: cover;
    border-radius: 4px;
    flex-shrink: 0;
}

.mosaic-icon {
    font-size: 28px;
    flex-shrink: 0;
    width: 48px;
    text-align: center;
}

.mosaic-info {
    flex: 1;
    min-width: 0;
}

.mosaic-name {
    display: block;
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 2px;
}

.mosaic-meta {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
}

.mosaic-status {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

/* Links */
.card-links {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid var(--border-light);
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.link-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 8px;
    background: var(--bg-hover);
    border-radius: 4px;
    color: var(--text);
    text-decoration: none;
    font-size: 11px;
    font-weight: 500;
    transition: background 0.15s;
}

.link-item:hover {
    background: var(--border);
}

.link-item svg {
    width: 14px;
    height: 14px;
    color: var(--text-muted);
}

/* ============================================
   Heatmap
   ============================================ */

.heatmap-panel {
    padding: 24px;
    max-width: 1000px;
    margin: 0 auto;
}

.heatmap-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
}

.heatmap-section {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
}

.heatmap-section-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    background: var(--bg-hover);
    border-bottom: 1px solid var(--border-light);
}

.heatmap-section-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.heatmap-section-dot.green { background: var(--green); }
.heatmap-section-dot.yellow { background: var(--yellow); }
.heatmap-section-dot.orange { background: var(--orange); }
.heatmap-section-dot.red { background: var(--red); }
.heatmap-section-dot.gray { background: var(--gray); }

.heatmap-section-title {
    flex: 1;
    font-weight: 600;
    font-size: 14px;
}

.heatmap-section-count {
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 500;
}

.heatmap-section-list {
    padding: 8px;
}

.heatmap-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.1s;
}

.heatmap-item:hover {
    background: var(--bg-hover);
}

.heatmap-item-flag {
    width: 22px;
    height: 16px;
    object-fit: cover;
    border-radius: 2px;
    flex-shrink: 0;
}

.heatmap-item-icon {
    font-size: 14px;
    flex-shrink: 0;
}

.heatmap-item-name {
    flex: 1;
    font-size: 13px;
    font-weight: 500;
}

.heatmap-item-meta {
    font-size: 12px;
    color: var(--text-muted);
}

.heatmap-empty {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}

/* ============================================
   Responsive
   ============================================ */

@media (max-width: 768px) {
    :root {
        --sidebar-width: 100%;
    }

    .app {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        max-height: 40vh;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

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

    .detail-panel {
        padding: 20px;
    }

    .card-content {
        grid-template-columns: 1fr;
    }
}

/* ============================================
   Utility
   ============================================ */

.hidden {
    display: none !important;
}
