:root {
    --primary: #7c3aed;
    --primary-dark: #6d28d9;
    --primary-light: #ede9fe;
    --success: #16a34a;
    --danger: #dc2626;
    --warning: #f59e0b;
    --bg: #f8fafc;
    --surface: #ffffff;
    --text: #1e293b;
    --text-muted: #64748b;
    --border: #e2e8f0;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.06);
}

*, *::before, *::after { box-sizing: border-box; }

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100dvh;
}

.app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
}

.app-header {
    background: var(--primary);
    color: white;
    padding: 0.75rem 1rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.app-header h1 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
    outline: none;
}

.nav-bar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}
.nav-bar::-webkit-scrollbar { display: none; }
.nav-link {
    padding: 0.65rem 1rem;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    text-decoration: none;
    white-space: nowrap;
    border-bottom: 2px solid transparent;
    transition: all 0.15s;
}
.nav-link:hover { color: var(--text); }
.nav-link.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.app-content {
    padding: 1rem;
    max-width: 1100px;
    margin: 0 auto;
    width: 100%;
}

.page-title {
    font-size: 1.2rem;
    font-weight: 700;
    margin: 0 0 1rem;
}

.card {
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1rem;
    margin-bottom: 0.75rem;
    border: 1px solid var(--border);
}

.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.badge-active { background: #dcfce7; color: #166534; }
.badge-closed { background: #fee2e2; color: #991b1b; }
.badge-draft { background: #fef3c7; color: #92400e; }
.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-open { background: #dbeafe; color: #1e40af; }
.badge-in-progress { background: #fef3c7; color: #92400e; }
.badge-resolved { background: #dcfce7; color: #166534; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.55rem 1rem;
    border: none;
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    text-decoration: none;
    -webkit-appearance: none;
    appearance: none;
    touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-warning { background: var(--warning); color: white; }
.btn-outline {
    background: transparent;
    border: 2px solid var(--border);
    color: var(--text);
}
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.78rem; }
.btn-block { width: 100%; }
.btn-lg { padding: 0.75rem 1.25rem; font-size: 0.95rem; }

.form-group { margin-bottom: 0.85rem; }
.form-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: 0.3rem;
    color: var(--text);
}
.form-input, .form-select, .form-textarea {
    width: 100%;
    padding: 0.55rem 0.7rem;
    border: 2px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s;
    -webkit-appearance: none;
    appearance: none;
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(124,58,237,.15);
}
.form-textarea { resize: vertical; min-height: 70px; }
.form-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.7rem center;
    padding-right: 2rem;
}

.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -1rem;
    padding: 0 1rem;
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}
th {
    text-align: left;
    padding: 0.5rem 0.6rem;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-bottom: 2px solid var(--border);
    white-space: nowrap;
}
td {
    padding: 0.55rem 0.6rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
tr:last-child td { border-bottom: none; }

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.75rem;
    margin-bottom: 1rem;
}
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem;
    text-align: center;
}
.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}
.stat-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.result-bar-container { margin-bottom: 0.5rem; }
.result-bar-label {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    margin-bottom: 0.2rem;
}
.result-bar {
    height: 8px;
    background: var(--border);
    border-radius: 4px;
    overflow: hidden;
}
.result-bar-fill {
    height: 100%;
    background: var(--primary);
    border-radius: 4px;
    transition: width 0.5s ease;
}

.alert {
    padding: 0.65rem 0.85rem;
    border-radius: 8px;
    font-size: 0.85rem;
    margin-bottom: 0.75rem;
}
.alert-danger { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }

.spinner {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-center {
    display: flex;
    justify-content: center;
    padding: 3rem;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 200;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}
.modal-content {
    background: var(--surface);
    border-radius: 16px 16px 0 0;
    padding: 1.25rem;
    width: 100%;
    max-width: 500px;
    max-height: 85dvh;
    overflow-y: auto;
    animation: slideUp 0.2s ease;
}
@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}
.modal-title {
    font-size: 1.1rem;
    font-weight: 700;
    margin: 0 0 1rem;
}
.result-voter-chip {
    display: inline-block;
    padding: 0.15rem 0.4rem;
    margin: 0.1rem 0.15rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.75rem;
    white-space: nowrap;
}
.modal-section-label {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0.75rem 0 0.4rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid var(--border);
}

.blazor-error-boundary {
    background: var(--danger);
    padding: 1rem;
    color: white;
    border-radius: 8px;
    margin: 1rem;
}
.blazor-error-boundary::after { content: "An error has occurred."; }

#blazor-error-ui {
    background: var(--danger);
    color: white;
    padding: 0.5rem 1rem;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    text-align: center;
    display: none;
}
#blazor-error-ui .reload { color: white; text-decoration: underline; }
#blazor-error-ui .dismiss { color: white; cursor: pointer; margin-left: 1rem; }

.apt-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 0.35rem;
    margin-bottom: 0.5rem;
}
.apt-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.45rem 0.25rem;
    border: 2px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.15s;
    text-align: center;
    user-select: none;
    -webkit-user-select: none;
}
.apt-cell:active { transform: scale(0.96); }
.apt-cell.apt-selected {
    border-color: var(--primary);
    background: #eff6ff;
}
.apt-num {
    font-size: 0.9rem;
    font-weight: 700;
}
.apt-holder {
    font-size: 0.65rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90px;
}

.login-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 80dvh;
    padding: 2rem 1rem;
    text-align: center;
}
.login-container h2 {
    font-size: 1.5rem;
    margin: 0 0 0.5rem;
}
.login-container p {
    color: var(--text-muted);
    margin: 0 0 1.5rem;
    font-size: 0.9rem;
}
.login-box {
    width: 100%;
    max-width: 360px;
}
.btn-link {
    background: none;
    border: none;
    color: var(--primary);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0;
    text-decoration: underline;
}
.btn-lg { padding: 0.85rem 1.5rem; font-size: 1rem; }

.alert-info { background: #eff6ff; color: #1e40af; border: 1px solid #bfdbfe; }

/* Grid editing */
.grid-input, .grid-select {
    width: 100%;
    padding: 0.3rem 0.4rem;
    border: 1.5px solid var(--border);
    border-radius: 5px;
    font-size: 0.8rem;
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
    transition: border-color 0.15s;
    -webkit-appearance: none;
    appearance: none;
}
.grid-input:focus, .grid-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(124,58,237,.1);
}
.grid-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.35rem center;
    padding-right: 1.3rem;
}

/* Building visual layout */
.building-visual {
    margin-bottom: 1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.building-roof {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 8px 8px 0 0;
    text-align: center;
}
.roof-label {
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.03em;
}
.building-body {
    border: 2px solid var(--border);
    border-top: none;
    border-radius: 0 0 8px 8px;
    background: var(--surface);
    overflow: hidden;
}
.building-floor-row {
    display: flex;
    border-bottom: 1px solid var(--border);
    min-height: 3rem;
}
.building-floor-row:last-child {
    border-bottom: none;
}
.floor-label {
    width: 2.2rem;
    min-width: 2.2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--text-muted);
    background: var(--bg);
    border-right: 1px solid var(--border);
}
.entrance-floor-cell {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    padding: 0.3rem;
    align-items: center;
    justify-content: center;
    border-right: 1px dashed var(--border);
    min-width: 7rem;
}
.entrance-floor-cell:last-child {
    border-right: none;
}
.entrance-floor-cell.floor-empty {
    background: repeating-linear-gradient(
        45deg,
        transparent,
        transparent 4px,
        var(--bg) 4px,
        var(--bg) 8px
    );
}
.building-apt {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.2rem 0.4rem;
    background: #eff6ff;
    border: 1.5px solid #bfdbfe;
    border-radius: 6px;
    cursor: default;
    min-width: 3rem;
    transition: all 0.15s;
}
.building-apt:hover {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(124,58,237,.12);
}
.building-apt.apt-inactive {
    opacity: 0.45;
    background: var(--bg);
    border-color: var(--border);
}
.building-apt.apt-has-debt {
    background: #fee2e2;
    border-color: #fca5a5;
}
.standalone-apt.apt-has-debt {
    background: #fee2e2;
    border-color: #fca5a5;
}
.building-apt-num {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--primary);
    line-height: 1.1;
}
.building-apt-holder {
    font-size: 0.55rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 5rem;
    line-height: 1.1;
}
.building-apt-empty {
    font-size: 0.75rem;
    color: var(--border);
}
.entrance-labels {
    background: var(--bg);
    border-top: 2px solid var(--border);
    min-height: auto;
}
.entrance-label-cell {
    flex: 1;
    text-align: center;
    padding: 0.4rem 0.25rem;
    border-right: 1px dashed var(--border);
    min-width: 7rem;
}
.entrance-label-cell:last-child {
    border-right: none;
}
.entrance-label-name {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text);
}
.entrance-label-info {
    font-size: 0.65rem;
    color: var(--text-muted);
}
.standalone-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(5.5rem, 1fr));
    gap: 0.35rem;
}
.standalone-apt {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0.35rem 0.25rem;
    background: #fef3c7;
    border: 1.5px solid #fcd34d;
    border-radius: 6px;
    cursor: default;
    text-align: center;
}
.standalone-apt.apt-inactive {
    opacity: 0.45;
    background: var(--bg);
    border-color: var(--border);
}

@media (min-width: 768px) {
    .modal-content {
        border-radius: 16px;
        margin-bottom: 2rem;
    }
    .modal-backdrop {
        align-items: center;
    }
    .apt-grid {
        grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    }
}

/* Confirmation Dialog */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}
.modal-dialog {
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    max-width: 400px;
    width: 90%;
    overflow: hidden;
}
.modal-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
}
.modal-body {
    padding: 1rem;
}
.modal-footer {
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    background: var(--bg);
}
