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

:root {
    --primary: #48536B;
    --primary-hover: #202632;
    --danger: #861D13;
    --danger-hover: #5C160D;
    --white: #ffffff;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-500: #6b7280;
    --gray-700: #374151;
    --gray-900: #111827;
    --radius: 8px;
}

[x-cloak] {
    display: none !important;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--gray-50);
    color: var(--gray-900);
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 1rem;
}

/* Forms */
.form-group {
    margin-bottom: 0.75rem;
}

.form-group label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
    color: var(--gray-700);
}

input[type="text"],
input[type="email"],
input[type="password"],
textarea {
    width: 100%;
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 1rem;
    background: var(--white);
    transition: border-color 0.15s;
}

input:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

textarea {
    resize: vertical;
}

.form-row {
    display: flex;
    flex-direction: column;
    gap: 0;
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 0.5rem;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.625rem 1rem;
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s, border-color 0.15s;
    white-space: nowrap;
}

.btn-primary {
    background: var(--primary);
    color: var(--white);
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-outline {
    background: var(--white);
    border-color: var(--gray-300);
    color: var(--gray-700);
}

.btn-outline:hover {
    background: var(--gray-100);
}

.btn-danger {
    background: var(--danger);
    color: var(--white);
}

.btn-danger:hover {
    background: var(--danger-hover);
}

.btn-sm {
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem;
}

.btn-full {
    width: 100%;
}

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    font-size: 0.875rem;
}

.alert-error {
    background: #fef2f2;
    color: var(--danger);
    border: 1px solid #fecaca;
}

/* Login */
.login-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 80vh;
}

.login-container h1 {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    color: var(--gray-900);
}

.login-form {
    width: 100%;
    max-width: 360px;
    background: var(--white);
    padding: 1.5rem;
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* Top bar */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--gray-200);
}

.topbar h1 {
    font-size: 1.25rem;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.user-label {
    font-size: 0.8rem;
    color: var(--gray-500);
}

/* Search */
.search-form {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.search-form input[type="text"] {
    flex: 1;
}

.new-contact-container {
    width: fit-content;
    margin: 0 auto 1rem auto;
    width: 100%;
    max-width: 500px;
}

.new-contact-container a {
    width: 100%;
}

.form-view-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.form-view-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
    margin: 0;
}

.contact-form-wrapper {
    margin-bottom: 1rem;
}

.contact-form {
    background: var(--white);
    padding: 1rem;
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    margin-top: 0.75rem;
}

/* Contacts list */
.contacts-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.contact-card {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    overflow: hidden;
}

.contact-card-link {
    text-decoration: none;
    color: inherit;
    display: block;
    transition: box-shadow 0.15s;
}

.contact-card-link:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.inline-form {
    display: inline;
}

.contact-body {
    padding: 0.75rem 1rem;
    display: flex;
    gap: 0.75rem;
    align-items: flex-start;
}

.contact-photo {
    width: 128px;
    height: 128px;
    border-radius: var(--radius);
    object-fit: cover;
    flex-shrink: 0;
}

.contact-photo-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gray-200);
    color: var(--gray-500);
    font-size: 1.125rem;
    font-weight: 600;
}

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

.contact-detail {
    font-size: 0.875rem;
    margin-bottom: 0.2rem;
}

.contact-detail a {
    color: var(--primary);
    text-decoration: none;
}

.contact-detail a:hover {
    text-decoration: underline;
}

.muted {
    color: var(--gray-500);
}

.empty-state {
    text-align: center;
    color: var(--gray-500);
    padding: 3rem 1rem;
}

/* Photo upload */
.photo-upload {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.photo-preview {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--gray-200);
}

.photo-help {
    font-size: 0.75rem;
    color: var(--gray-500);
}

.photo-remove-label {
    font-size: 0.8rem;
    color: var(--gray-500);
    display: flex;
    align-items: center;
    gap: 0.35rem;
    cursor: pointer;
}

input[type="file"] {
    font-size: 0.875rem;
    color: var(--gray-700);
}

/* Contact view */
.contact-view {
    background: var(--white);
    padding: 1rem;
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.contact-view-header {
    display: flex;
    align-items: center;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
    padding: 1.5rem 0;
    border-bottom: 1px solid var(--gray-200);
}

.contact-view-header h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0 0 0.25rem 0;
}

.contact-view-header .muted {
    font-size: 1rem;
}

.contact-view-photo {
    width: 128px;
    height: 128px;
    border-radius: var(--radius);
    object-fit: cover;
    flex-shrink: 0;
}

.contact-view-photo.contact-photo-placeholder {
    font-size: 2.5rem;
}

.contact-view-details {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.contact-view-row {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    font-size: 0.875rem;
}

.contact-view-label {
    font-weight: 600;
    color: var(--gray-500);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.contact-view-row a {
    color: var(--primary);
    text-decoration: none;
}

.contact-view-row a:hover {
    text-decoration: underline;
}

.contact-view-attachments {
    margin-bottom: 1rem;
}

.contact-view-attachments h3 {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--gray-700);
    margin-bottom: 0.5rem;
}

.contact-view-actions {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--gray-200);
}

/* Attachments */
.attachments-section {
    margin-top: 0.75rem;
    margin-bottom: 0.75rem;
}

.attachments-section > label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--gray-700);
}

.attachments-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.attachment-item {
    position: relative;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    overflow: hidden;
    background: var(--gray-50);
}

.attachment-thumb {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    display: block;
}

.attachment-file {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1;
    background: var(--gray-100);
    text-decoration: none;
    color: var(--gray-700);
}

.attachment-file-icon {
    font-size: 2rem;
    line-height: 1;
}

.attachment-file-ext {
    font-size: 0.7rem;
    font-weight: 600;
    margin-top: 0.25rem;
    color: var(--gray-500);
}

.attachment-name {
    padding: 0.25rem 0.5rem;
    font-size: 0.7rem;
    color: var(--gray-700);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.attachment-delete {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    width: 1.5rem;
    height: 1.5rem;
    border-radius: 50%;
    border: none;
    background: rgba(0, 0, 0, 0.5);
    color: var(--white);
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.attachment-delete:hover {
    background: var(--danger);
}

/* Lightbox */
.lightbox-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.lightbox-img {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    border-radius: var(--radius);
}

.lightbox-close {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.15);
    color: var(--white);
    font-size: 1.5rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lightbox-close:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Desktop */
@media (min-width: 640px) {
    .form-row {
        flex-direction: row;
        gap: 1rem;
    }

    .form-row .form-group {
        flex: 1;
    }

    .container {
        padding: 1.5rem;
    }

    .attachments-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}
