:root {
    --bg-color: linear-gradient(135deg, #e0e7ff 0%, #f4f4f9 100%);
    --container-bg: #ffffff;
    --text-color: #1f2937;
    --header-color: #1a3c87;
    --border-color: #e5e7eb;
    --input-bg: #f9fafb;
    --btn-bg: linear-gradient(90deg, #007bff, #005bb5);
    --btn-hover-bg: linear-gradient(90deg, #005bb5, #003087);
    --table-header-bg: linear-gradient(90deg, #007bff, #005bb5);
    --table-even-bg: #f9fafb;
    --table-hover-bg: #e0f2fe;
    --error-bg: #fef2f2;
    --error-text: #b91c1c;
    --result-bg: #f9f9f9;
    --result-text: #1f2937; /* Added for result text in light mode */
    --footer-text: #4b5563;
    --card-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.dark-mode {
    --bg-color: linear-gradient(135deg, #1e3a8a 0%, #1e293b 100%);
    --container-bg: #2d3748;
    --text-color: #e2e8f0;
    --header-color: #93c5fd;
    --border-color: #4b5563;
    --input-bg: #374151;
    --btn-bg: linear-gradient(90deg, #60a5fa, #3b82f6);
    --btn-hover-bg: linear-gradient(90deg, #3b82f6, #1e40af);
    --table-header-bg: linear-gradient(90deg, #60a5fa, #3b82f6);
    --table-even-bg: #374151;
    --table-hover-bg: #4b5563;
    --error-bg: #4b1c1c;
    --error-text: #f9a8d4;
    --result-bg: #2d3748;
    --result-text: #e2e8f0; /* Adjusted for result text in dark mode */
    --card-shadow: 0 2px 4px rgba(255, 255, 255, 0.1);
}

body {
    font-family: 'Segoe UI', Arial, sans-serif;
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
    background: var(--bg-color);
    min-height: 100vh;
    color: var(--text-color);
    box-sizing: border-box;
}

.container {
    background-color: var(--container-bg);
    padding: 20px;
    border-radius: 12px;
    box-shadow: var(--card-shadow);
    transition: transform 0.3s ease;
    max-width: 100%;
    box-sizing: border-box;
}

.container:hover {
    transform: translateY(-5px);
}

h1 {
    text-align: center;
    color: var(--header-color);
    font-size: 1.8rem;
    margin-bottom: 20px;
    position: relative;
}

h1::after {
    content: '';
    width: 60px;
    height: 4px;
    background: #007bff;
    position: absolute;
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: 2px;
}

.global-search-container {
    margin-bottom: 20px;
    position: relative;
    max-width: 100%;
}

#global-search {
    width: 100%;
    padding: 12px 12px 12px 40px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 1rem;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    background: var(--input-bg);
    box-sizing: border-box;
    max-width: 100%;
}

#global-search:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
}

.global-search-container i {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #6b7280;
}

.theme-toggle {
    position: absolute;
    top: 20px;
    right: 20px;
}

#theme-toggle-btn {
    padding: 8px;
    background: var(--btn-bg);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    transition: background 0.3s ease;
    display: flex;
    align-items: center;
    gap: 8px;
}

#theme-toggle-btn:hover {
    background: var(--btn-hover-bg);
}

.tabs {
    display: flex;
    margin-bottom: 20px;
    border-bottom: 2px solid var(--border-color);
    flex-wrap: nowrap; /* Ensure tabs stay in one row */
    justify-content: space-between; /* Distribute tabs evenly */
    gap: 5px; /* Reduced gap for tighter spacing */
}

.tab-btn {
    flex: 1; /* Equal width for all tabs */
    padding: 10px 10px; /* Adjusted padding for balance */
    background: var(--input-bg);
    border: none;
    cursor: pointer;
    border-radius: 8px 8px 0 0;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-color);
    transition: all 0.3s ease;
    text-align: center;
    white-space: normal; /* Allow text to wrap */
    word-wrap: break-word; /* Break long words if necessary */
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 60px; /* Minimum height to ensure alignment */
    line-height: 1.2; /* Adjust line height for wrapped text */
}

.tab-btn.active {
    background: var(--btn-bg);
    color: white;
}

.tab-btn:hover {
    background: var(--btn-hover-bg);
    color: white;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
    animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.form-group {
    margin-bottom: 15px;
    position: relative;
    max-width: 100%;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: var(--header-color);
    font-size: 0.95rem;
}

.form-group label i {
    margin-right: 8px;
    color: #007bff;
}

.form-control {
    width: 100%;
    padding: 10px 10px 10px 40px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-size: 0.95rem;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    background: var(--input-bg);
    box-sizing: border-box;
    max-width: 100%;
}

.form-control:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 8px rgba(0, 123, 255, 0.3);
}

.form-group i {
    position: absolute;
    left: 12px;
    top: 38px;
    color: #6b7280;
}

.btn {
    padding: 10px 25px;
    background: var(--btn-bg);
    color: #fff;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 600;
    transition: background 0.3s ease, transform 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    max-width: 200px;
}

.btn i {
    font-size: 1.1rem;
}

.btn:hover {
    background: var(--btn-hover-bg);
    transform: translateY(-2px);
}

.btn:disabled {
    background: #cccccc;
    cursor: not-allowed;
    transform: none;
}

.spinner {
    border: 4px solid #f3f3f3;
    border-top: 4px solid #007bff;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    animation: spin 1s linear infinite;
    margin: 15px auto;
    display: none;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.error-message {
    background-color: var(--error-bg);
    color: var(--error-text);
    padding: 10px;
    border-radius: 8px;
    margin-top: 15px;
    display: none;
    font-size: 0.9rem;
    border-left: 4px solid var(--error-text);
}

.result-box {
    margin-top: 20px;
    padding: 15px;
    background: var(--result-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    display: none;
    font-size: 0.9rem;
    color: var(--result-text); /* Use result-text variable */
    box-shadow: var(--card-shadow);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.result-box p {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: var(--container-bg); /* Match container background for contrast */
    border-radius: 4px;
    color: var(--result-text); /* Ensure text is readable */
}

.result-box i {
    color: #007bff;
    font-size: 1rem;
    min-width: 20px;
}

.population-area {
    margin-top: 20px;
    padding: 15px;
    background: var(--result-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--card-shadow);
    display: flex;
    flex-direction: column;
    gap: 10px;
    color: var(--result-text); /* Ensure text is readable */
}

.population-area h3 {
    margin-bottom: 10px;
    color: var(--header-color);
    font-size: 1.2rem;
    padding: 8px;
    background: var(--container-bg); /* Match container background */
    border-radius: 4px;
}

.population-area h3 i {
    margin-right: 8px;
    color: #007bff;
}

.population-area .info-card {
    flex: 1 1 150px;
    padding: 8px;
    background: var(--container-bg); /* Match container background */
    border: 1px solid var(--border-color);
    border-radius: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    color: var(--result-text); /* Ensure text is readable */
}

.population-area .info-card i {
    color: #007bff;
    font-size: 1rem;
    min-width: 20px;
}

.population-area canvas {
    max-width: 100%;
    margin-top: 10px;
    flex: 1 1 100%;
    max-height: 200px;
    border-radius: 4px;
    background: var(--container-bg); /* Match container background */
    padding: 10px;
    box-sizing: border-box;
}

.statistics {
    margin-top: 20px;
    padding: 15px;
    background: var(--result-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--card-shadow);
    display: flex;
    flex-direction: column;
    gap: 10px;
    color: var(--result-text); /* Ensure text is readable */
}

.statistics h3 {
    margin-bottom: 10px;
    color: var(--header-color);
    font-size: 1.2rem;
    padding: 8px;
    background: var(--container-bg); /* Match container background */
    border-radius: 4px;
}

.statistics h3 i {
    margin-right: 8px;
    color: #007bff;
}

.statistics .info-card {
    flex: 1 1 150px;
    padding: 8px;
    background: var(--container-bg); /* Match container background */
    border: 1px solid var(--border-color);
    border-radius: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    color: var(--result-text); /* Ensure text is readable */
}

.statistics .info-card i {
    color: #007bff;
    font-size: 1rem;
    min-width: 20px;
}

.units-list {
    margin-top: 20px;
    overflow-x: auto;
    max-width: 100%;
    -webkit-overflow-scrolling: touch;
}

#units-table-body table, #old-units-table-body table, #province-units-table-body table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    min-width: 300px;
}

.units-list th, .units-list td {
    border: 1px solid var(--border-color);
    padding: 10px;
    text-align: left;
    white-space: nowrap;
    font-size: 0.9rem;
}

.units-list th {
    background: var(--table-header-bg);
    color: #fff;
    position: sticky;
    top: 0;
    font-weight: 600;
}

.units-list tr:nth-child(even) {
    background-color: var(--table-even-bg);
}

.units-list tr:hover {
    background-color: var(--table-hover-bg);
    transition: background-color 0.2s ease;
}

.units-list::-webkit-scrollbar {
    height: 8px;
}

.units-list::-webkit-scrollbar-track {
    background: var(--input-bg);
    border-radius: 5px;
}

.units-list::-webkit-scrollbar-thumb {
    background: #007bff;
    border-radius: 5px;
}

.units-list::-webkit-scrollbar-thumb:hover {
    background: #005bb5;
}

.leader-info {
    margin-top: 20px;
    padding: 15px;
    background: var(--result-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--card-shadow);
    font-size: 0.9rem;
    color: var(--result-text); /* Ensure text is readable */
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.leader-info p {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: var(--container-bg); /* Match container background */
    border-radius: 4px;
    color: var(--result-text); /* Ensure text is readable */
}

.leader-info i {
    margin-right: 8px;
    color: #007bff;
    font-size: 1rem;
    min-width: 20px;
}

.result-title {
    font-size: 1.2rem;
    font-weight: bold;
    background-color: #007bff;
    color: #fff;
    padding: 8px 12px;
    border-radius: 4px;
    display: inline-block;
    margin-bottom: 10px;
}

@media (max-width: 600px) {
    body {
        padding: 10px;
    }
    .container {
        padding: 15px;
    }
    h1 {
        font-size: 1.5rem;
    }
    .tab-btn {
        font-size: 0.9rem;
        padding: 8px 10px;
        min-height: 50px; /* Adjust minimum height for mobile */
    }
    .form-control, #global-search {
        font-size: 0.9rem;
        padding: 8px 8px 8px 35px;
    }
    .result-title {
        font-size: 1.1rem;
        padding: 6px 10px;
    }
    .form-group i {
        top: 36px;
        left: 10px;
    }
    .btn {
        font-size: 0.9rem;
        padding: 8px 15px;
        max-width: 150px;
    }
    .result-box, .population-area, .statistics, .leader-info {
        padding: 10px;
    }
    .result-box p, .population-area .info-card, .statistics .info-card, .leader-info p {
        font-size: 0.85rem;
        padding: 6px;
    }
    .population-area h3, .statistics h3 {
        font-size: 1.1rem;
        padding: 6px;
    }
    .units-list th, .units-list td {
        font-size: 0.85rem;
        padding: 8px;
    }
    .theme-toggle {
        top: 15px;
        right: 15px;
    }
    #theme-toggle-btn {
        padding: 6px;
        font-size: 0.9rem;
    }
}

@media (max-width: 320px) {
    .tab-btn {
        font-size: 0.85rem;
        padding: 6px 8px;
        min-height: 40px; /* Further adjust for very small screens */
    }
    .form-control, #global-search {
        font-size: 0.85rem;
        padding: 6px 6px 6px 30px;
    }
    .form-group i {
        top: 34px;
        left: 8px;
    }
    .result-title {
        font-size: 1.1rem;
        padding: 6px 10px;
    }
    .btn {
        font-size: 0.85rem;
        padding: 6px 15px;
        max-width: 120px;
    }
    .result-box p, .population-area .info-card, .statistics .info-card, .leader-info p {
        font-size: 0.8rem;
        padding: 4px;
    }
    .population-area h3, .statistics h3 {
        font-size: 1rem;
        padding: 4px;
    }
    .population-area canvas {
        max-height: 150px;
    }
}
