/* === Reset & Base === */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    margin: 0;
    padding: 0;
    background: #fafafa;
    color: #222;
    line-height: 1.5;
}

/* === Header === */
header {
    background: #fff;
    border-bottom: 1px solid #ddd;
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
}

header h1 {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

/* === Main Content === */
main {
    padding: 1rem;
    max-width: 1400px;
    margin: 0 auto;
}

/* === Buttons === */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    background: #fff;
    border: 1px solid #333;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.875rem;
    text-decoration: none;
    color: #333;
    transition: background 0.15s;
}

.btn:hover {
    background: #f0f0f0;
}

.btn-primary {
    background: #333;
    color: #fff;
}

.btn-primary:hover {
    background: #555;
}

.btn-small {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
}

.btn-danger {
    border-color: #c00;
    color: #c00;
}

.btn-danger:hover {
    background: #fee;
}

.link-btn {
    color: #06c;
    text-decoration: none;
    font-size: 0.875rem;
}

.link-btn:hover {
    text-decoration: underline;
}

/* === Filters === */
.filters {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    align-items: center;
}

.filter-group {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.filter-group label {
    font-size: 0.875rem;
    font-weight: 500;
}

.filter-group select,
.filter-group input {
    padding: 0.5rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 0.875rem;
}

.search-group input {
    min-width: 200px;
}

/* === Table View (Desktop) === */
.table-view {
    display: block;
    overflow-x: auto;
}

.card-view {
    display: none;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    font-size: 0.875rem;
}

th, td {
    padding: 0.5rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid #eee;
}

th {
    background: #f5f5f5;
    font-weight: 600;
    white-space: nowrap;
}

tbody tr:hover {
    background: #fafafa;
}

.title-cell {
    max-width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Inline edit fields in table */
.inline-edit {
    padding: 0.25rem 0.5rem;
    border: 1px solid #ddd;
    border-radius: 3px;
    font-size: 0.8rem;
    background: #fff;
}

.inline-edit:focus {
    border-color: #666;
    outline: none;
}

td select.inline-edit {
    max-width: 140px;
}

td input[type="text"].inline-edit {
    width: 120px;
}

td input[type="number"].inline-edit {
    width: 70px;
}

td input[type="date"].inline-edit {
    width: 130px;
}

.saving-indicator {
    display: none;
    font-size: 0.7rem;
    color: #666;
    margin-left: 0.25rem;
}

.saving-indicator.error {
    color: #c00;
}

/* === Card View (Mobile) === */
@media (max-width: 819px) {
    .table-view {
        display: none;
    }

    .card-view {
        display: block;
    }
}

.record-card {
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 6px;
    margin-bottom: 1rem;
    padding: 1rem;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 0.75rem;
    gap: 0.5rem;
}

.card-title {
    font-weight: 600;
    font-size: 1rem;
    flex: 1;
}

.card-body {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.card-field {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-field label {
    font-weight: 500;
    font-size: 0.8rem;
    min-width: 80px;
    color: #555;
}

.card-field .inline-edit {
    flex: 1;
}

.card-row {
    display: flex;
    gap: 1rem;
    font-size: 0.875rem;
    flex-wrap: wrap;
}

.card-row span {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.inline-small {
    width: 80px !important;
}

.card-footer {
    margin-top: 0.75rem;
    padding-top: 0.75rem;
    border-top: 1px solid #eee;
}

/* === Modal === */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    justify-content: center;
    align-items: flex-start;
    padding: 2rem 1rem;
    overflow-y: auto;
    z-index: 1000;
}

.modal-content {
    background: #fff;
    border-radius: 8px;
    max-width: 600px;
    width: 100%;
    max-height: calc(100vh - 4rem);
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid #eee;
}

.modal-header h2 {
    margin: 0;
    font-size: 1.25rem;
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #666;
    padding: 0;
    line-height: 1;
}

.modal-close:hover {
    color: #333;
}

/* === Forms === */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
    padding: 1rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.form-group label {
    font-size: 0.8rem;
    font-weight: 500;
    color: #555;
}

.form-group input,
.form-group select,
.form-group textarea {
    padding: 0.5rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 0.875rem;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    border-color: #666;
    outline: none;
}

.form-actions {
    padding: 1rem;
    border-top: 1px solid #eee;
    display: flex;
    gap: 1rem;
    align-items: center;
}

.form-meta {
    padding: 0.5rem 1rem;
    font-size: 0.75rem;
    color: #888;
    display: flex;
    gap: 1.5rem;
    flex-wrap: wrap;
}

/* === Record Page === */
.record-page {
    max-width: 900px;
}

.section {
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 6px;
    margin-bottom: 1.5rem;
    padding: 1rem;
}

.section h2 {
    margin: 0 0 1rem 0;
    font-size: 1.125rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid #eee;
}

/* === Notes === */
.note-item {
    padding: 0.75rem;
    background: #f9f9f9;
    border-radius: 4px;
    margin-bottom: 0.75rem;
}

.note-meta {
    font-size: 0.75rem;
    color: #888;
    margin-bottom: 0.25rem;
}

.note-text {
    font-size: 0.875rem;
    white-space: pre-wrap;
}

.add-note-form {
    margin-top: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.add-note-form textarea {
    padding: 0.5rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 0.875rem;
    resize: vertical;
}

/* === Attachments === */
.attachment-item {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 1rem;
    padding: 0.75rem;
    background: #f9f9f9;
    border-radius: 4px;
    margin-bottom: 0.75rem;
    flex-wrap: wrap;
}

.attachment-info {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
    flex: 1;
}

.attachment-name {
    font-weight: 500;
    font-size: 0.875rem;
    word-break: break-all;
}

.attachment-meta {
    font-size: 0.75rem;
    color: #666;
}

.attachment-note {
    font-size: 0.8rem;
    color: #555;
    font-style: italic;
}

.attachment-actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
}

.upload-form {
    margin-top: 1rem;
}

.upload-row {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
}

.upload-row input[type="file"] {
    flex: 1;
    min-width: 150px;
}

.upload-row input[type="text"] {
    flex: 1;
    min-width: 150px;
    padding: 0.5rem;
    border: 1px solid #ccc;
    border-radius: 4px;
}

.upload-hint {
    font-size: 0.75rem;
    color: #888;
    margin-top: 0.5rem;
}

/* === Status Messages === */
.status-msg {
    font-size: 0.8rem;
    display: none;
}

.status-msg.success {
    color: #090;
}

.status-msg.error {
    color: #c00;
}

/* === Utility === */
.loading {
    color: #888;
    font-style: italic;
}

.empty {
    color: #888;
    text-align: center;
    padding: 2rem;
}

.error {
    color: #c00;
}

/* === Responsive Adjustments === */
@media (max-width: 600px) {
    header {
        flex-direction: column;
        align-items: flex-start;
    }

    .filters {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-group {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-group label {
        margin-bottom: 0.25rem;
    }

    .search-group {
        flex-direction: row;
    }

    .search-group input {
        min-width: 0;
        flex: 1;
    }

    .form-grid {
        grid-template-columns: 1fr;
    }

    .upload-row {
        flex-direction: column;
        align-items: stretch;
    }

    .upload-row input[type="file"],
    .upload-row input[type="text"],
    .upload-row button {
        width: 100%;
    }

    .attachment-item {
        flex-direction: column;
    }

    .attachment-actions {
        width: 100%;
        justify-content: flex-start;
    }
}
