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

:root {
    --primary-color: #2563eb;
    --primary-dark: #1e40af;
    --secondary-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --bg-color: #f8fafc;
    --surface-color: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --border-color: #e2e8f0;
    --person-a-color: #28a745;
    --person-b-color: #ffc107;
    --shared-color: #6c757d;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    padding-bottom: env(safe-area-inset-bottom);
    display: grid;
    grid-template-rows: 3.5rem auto 4rem;
    grid-template-areas: 'header'
                        'main'
                        'nav';
    min-height: 100vh;
}

header {
    grid-area: header;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--primary-color);
    color: white;
    box-shadow: var(--shadow-md);
    position: sticky;
    top: 0;
    z-index: 100;
}
header h1 {
    font-size: 1.25rem;
}

nav {
    grid-area: nav;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: var(--surface-color);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    z-index: 50;
}
nav ul {
    display: flex;
    justify-content: space-around;
    list-style-type: none;
}
nav ul li a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    padding: 0.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    transition: color 0.2s;
    text-decoration: none;
    font-size: 0.75rem;
}
nav .active {
    color: var(--primary-color);
}
.nav-icon {
    display: inline-grid;
    font-size: 1.25rem;
}

main {
    grid-area: main;
    /* padding-bottom: calc(80px + env(safe-area-inset-bottom)); */
}
.content-container {
    display: none;
    animation: fadeIn 0.3s;
    background-color: var(--surface-color);
    padding: 1.5rem 0.5rem;
    min-height: calc(100vh - 7.5rem);
}
.content-container.active {
    display: block;
}
.card-header {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
    text-align: center;
}
.image-upload-area {
    border: 2px dashed var(--border-color);
    border-radius: 0.5rem;
    padding: 2rem;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
}
.image-upload-area:hover {
    border-color: var(--primary-color);
    background-color: rgba(37, 99, 235, 0.05);
}
.btn-row {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    row-gap: 0.5rem;
    column-gap: 1rem;
    margin-top: 1.5rem;
}
.btn {
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    /* display: inline-flex; */
    /* align-items: center; */
    /* gap: 0.5rem; */
    /* justify-content: center; */
    flex-grow: 1;
}
#captureSection .btn {
    max-width: 12rem;
}
.btn-primary {
    background-color: var(--primary-color);
    border: 2px solid var(--primary-color);
    color: white;
}
.btn-outline {
    background-color: transparent;
    border: 2px solid var(--primary-color);
    color: var(--primary-color);
}
#cropContainer {
    position: relative;
    max-width: 100%;
    margin: 1rem 0;
}
.bon-layout {
    display: grid;
    gap: 1rem;
}
.bon-layout > div {
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 1rem;
}
.image-preview {
    position: sticky;
    top: 80px;
    max-height: calc(100vh - 100px);
    overflow: auto;
}
.image-preview img {
    width: 100%;
    border-radius: 0.25rem;
}
.image-preview h3 {
    margin-bottom: 1rem;
    font-size: 1rem;
}
.bon-editor {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}
.field-group {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.field-group label {
    font-weight: 500;
    font-size: 0.875rem;
    width: 10rem;
}
.field-group input, .field-group select {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 0.25rem;
    font-size: 0.9rem;
}
.person-btn-compact {
    padding: 4px 10px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.85rem;
    border-radius: 0.25rem;
    transition: all 0.2s;
    grid-row: 1 / 2;
}
.item-row {
    border: 1px solid var(--border-color);
    border-radius: 0.25rem;
    margin-bottom: 0.5rem;
    padding: 0.5rem;
    background: var(--bg-color);
}
.item-row > div {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    margin-bottom: 0.5rem;
}
.item-description {
    flex: 1;
}
.item-description input,
.item-field input {
    width: 100%;
    padding: 0.3rem;
    border: 1px solid var(--border-color);
    border-radius: 0.25rem;
    font-size: 0.9rem;
}
.item-row-header button {
    padding: 0.3rem 0.5rem;
    border: none;
    border-radius: 0.25rem;
    cursor: pointer;
    font-size: 0.85rem;
}
.item-row-header button.danger {
    padding: 0.2rem;
    display: inline-grid;
}
.item-row-header button.danger svg-icon {
    width: 20px !important;
}
.person-toggle-compact {
    display: grid;
    gap: 2px;
    background: var(--border-color);
    border-radius: 0.25rem;
    border: 2px solid var(--border-color)
}
.item-row-header .person-btn-compact.a {
    grid-column: 1 / 2;
}
.item-row-header .person-btn-compact.b {
    grid-column: 2 / 3;
}
.person-btn-compact.shared {
    grid-column: 1 / 3;
}
.active-shared .person-btn-compact.shared {
    background: var(--shared-color);
    color: white;
}
.item-row-header .person-toggle-compact.active-shared .person-btn-compact {
    color: white;
}
.active-a .person-btn-compact.a {
    background: var(--person-a-color);
    color: white;
}
.active-b .person-btn-compact.b {
    background: var(--person-b-color);
}
.item-field {
    display: flex;
    gap: 0.1rem;
    align-items: center;
}
.item-field input,
.item-field p,
.item-field label {
    font-size: 0.85rem;
}
.item-field.gesamtpreis {
    display: none;
}
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type=number] {
    -moz-appearance:textfield;
}

#overviewSection.receiptsSection {
    max-width: 55rem;
    margin-inline: auto;
}
.filter-bar {
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 1rem;
    margin-bottom: 1rem;
}
.filter-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}
.form-label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    font-size: 0.875rem;
}
.form-input, .form-select {
    width: 100%;
    padding: 0.3rem;
    border: 1px solid var(--border-color);
    border-radius: 0.25rem;
    font-size: 0.825rem;
    font-family: inherit;
    height: 28px;
}
#daterange {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 0.5rem;
}
#date button {
    background: none;
    border: none;
    cursor: pointer;
    outline: inherit;
    padding: 0.4em 0.8em;
    font-weight: 600;
    font-size: 0.875em;
    border-radius: 2px;
}
#date button[aria-pressed=mixed] {
    background: rgba(37, 99, 235, 0.1);
    color: var(--primary-color);
}
#date button[aria-pressed=true] {
    background-color: var(--primary-color);
    color: white;
}
#date p {
    line-height: 1.6rem;
}
#datepicker {
    display: flex;
    justify-content: space-evenly;
    width: 100%;
}
#datepicker input {
    width: 6.6rem;
}
.settings-list,
.receipt-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}
.settings-list {
    gap: 0.5rem;
}
.receipt-item {
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 1rem;
    transition: all 0.2s;
}
.receipt-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}
.receipt-item-store {
    font-weight: 600;
}
.receipt-item-amount {
    font-weight: 700;
    color: var(--primary-dark);
    font-size: 1.125rem;
}
.receipt-item-meta {
    display: flex;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-secondary);
    flex-wrap: wrap;
}
.receipt-item-meta span {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}
.receipt-item-actions {
    display: flex;
    gap: 0.5rem;
}
.receipt-item svg-icon {
    width: 18px !important;
}
.receipt-item-actions button {
    background-color: transparent;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    color: var(--primary-color);
    display: flex;
    gap: 0.3rem;
    align-items: center;
}
.receipt-item-actions button span {
    display: none;
    color: inherit;
}
.receipt-preview {
    /* display: none; */
    margin-top: 0.75rem;
    padding: 0 0.5rem;
    background-color: var(--bg-color);
    border-radius: 0.5rem;
    border: 1px solid var(--border-color);
}
.preview-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.3rem 0;
    border-bottom: 1px solid var(--border-color);
    gap: 1rem;
    max-width: calc(100vw - 4.25rem);
}
.preview-item:last-child {
    border-bottom: none;
}
.preview-item-desc {
    flex: 1;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}
.preview-item-badges {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.badge {
    display: inline-flex;
    align-items: center;
    padding: 0.175rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
}
.badge-shared {
    background-color: rgba(108, 117, 125, 0.2);
    color: #4b5563;
}
.badge-a {
    background-color: rgba(255, 193, 7, 0.2);
    color: #d97706;
}
.badge-b {
    background-color: rgba(40, 167, 69, 0.2);
    color: #15803d;
}
.badge-account {
    background-color: rgba(16, 185, 129, 0.1);
    color: var(--secondary-color);
    display: none;
}
#receiptList, .card-header.receipts, 
#statsDisplay, .card-header.statistics {
    display: none;
}
.receiptsSection #receiptList {
    display: flex;
}
.receiptsSection .card-header.receipts,
.statisticsSection #statsDisplay,
.statisticsSection .card-header.statistics {
    display: block;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(155px, 1fr));
    gap: 0.5rem;
    margin-bottom: 2rem;
}
.stat-card {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    color: white;
    padding: 1.5rem;
    border-radius: 0.5rem;
    box-shadow: var(--shadow-md);
}
.stat-label {
    font-size: 0.875rem;
    opacity: 0.9;
    margin-bottom: 0.5rem;
}
.stat-value {
    font-size: 1.75rem;
    font-weight: 700;
}
#statsDisplay .content-container {
    display: block;
    min-height: unset;
}
#statsDisplay .content-container h3 {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
}
.account-breakdown {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.account-bar {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 3px 3px 3px 10px;
    background: #f8f9fa;
    border-radius: 6px;
}
.account-name {
    font-size: 0.875rem;
    font-weight: 600;
    color: #666;
}
.bar-container {
    flex: 1;
    height: 30px;
    border-radius: 6px;
    overflow: hidden;
    position: relative;
}
.bar-fill {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: 10px;
    color: var(--primary-color);
    font-weight: 600;
    font-size: 0.82rem;
    transition: width 0.3s ease;
    position: relative;
}
.bar-amount-outside {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--primary-color);
    font-weight: 600;
    font-size: 0.82rem;
}

#balanceSection {
    max-width: 72rem;
    margin-inline: auto;
}
#balanceDisplay {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(292px, 1fr));
    gap: 1rem;
    margin-bottom: 1rem;
}
.balance-section {
    border: 2px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 1.25rem;
}
.balance-header {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--border-color);
}
.balance-row {
    display: flex;
    justify-content: space-between;
    margin: 0.5rem 0;
}
.balance-row.total {
    font-weight: 600;
    font-size: 1.125rem;
    border-top: 2px solid var(--border-color);
    margin-bottom: 0;
    padding-top: 0.5rem;
}
.balance-value {
    font-weight: 600;
}
.balance-positive {
    color: var(--secondary-color);
}
.balance-negative {
    color: var(--danger-color);
}

.settings-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.375rem 0.75rem;
    background-color: var(--bg-color);
    border-radius: 0.25rem;
    border: 1px solid var(--border-color);
}
.icon-btn {
    display: inline-grid;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.18rem;
    border-radius: 0.25rem;
    color: var(--text-secondary);
}
.btn-full {
    width: 100%;
    max-width: unset !important;
}
svg-icon {
    width: 24px;
    aspect-ratio: 1;
    display: inline-grid;
}

.reverseButton {
    order: -1;
    width: 100%;
}

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

.spinner {
    border: 3px solid var(--border-color);
    border-top: 3px solid var(--primary-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin: 2rem auto;
}

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

#settlementModal,
#loginScreen {
    border: none;
    border-radius: 12px;
    background: var(--bg-color);
    padding: 2rem;
    width: 320px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5);
    margin: auto;
}
#settlementModal {
    width: fit-content;
    z-index: 100;
}

#loginScreen::backdrop {
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
}

#loginScreen h2 {
    color: var(--text-primary);
    margin-bottom: 1.5rem;
    text-align: center;
}

#loginScreen input {
    width: 100%;
    padding: .75rem;
    margin-bottom: 1rem;
    /* background: #0f3460; */
    /* border: none; */
    border-radius: 0.25rem;
    /* color: #e0e0e0; */
    font-size: 1rem;
    box-sizing: border-box;
    border: 1px solid var(--border-color);
}

#loginScreen button[type="submit"] {
    width: 100%;
    padding: .75rem;
    background: var(--primary-color);
    border: none;
    border-radius: 8px;
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
}

#loginError {
    color: #e05c5c;
    margin-top: .75rem;
    text-align: center;
    font-size: .9rem;
    min-height: 1.2em;
    display: none;
}

@media (max-width: 768px) {
    .bar-fill {
        width: 100% !important;
    }
}
@media (min-width: 768px) {
    body {
        grid-template-rows: 3.5rem auto;
        grid-template-columns: 250px auto;
        grid-template-areas: 'header header'
                            'nav main';
    }

    nav {
        position: unset;
        border-right: 1px solid var(--border-color);
        border-top: unset;
        padding: 1rem;
    }
    nav ul {
        justify-content: unset;
        box-shadow: unset;
        flex-direction: column;
        gap: 0.5rem;
        overflow-y: auto;
    }
    nav ul li a {
        border-radius: 0.25rem;
        flex-direction: row;
        gap: 1rem;
        padding: 0.6rem 1rem;
        transition: background-color 0.2s;
        font-size: 1rem;
    }
    nav .active {
        background-color: rgba(37, 99, 235, 0.1);
    }

    main {
        padding: 1rem;
    }
    .content-container {
        border-radius: 0.5rem;
        /* margin-bottom: 1rem; */
        box-shadow: var(--shadow-sm);
        border: 1px solid var(--border-color);
        min-height: unset;
        padding: 1.5rem;
    }
    .btn {
        max-width: 12rem;
    }
    .bon-layout {
        grid-template-columns: auto 400px;
        grid-template-rows: auto auto;
    }
    .bon-layout .image-preview {
        grid-column: 2/3;
        grid-row: 1/3;
    }

    .form-label {
        padding-top: 0.7rem;
    }
    .receipt-item-actions button {
        /* background: buttonface; */
        padding: 0.2rem 0.8rem;
        border-radius: 0.25rem;
        border: 2px solid var(--primary-color);
    }
    .receipt-item-actions button span {
        display: block;
    }
    .badge-account {
        display: block;
    }
    .receipt-preview {
        padding: 0.5rem 1rem;
    }
    .preview-item {
        max-width: unset;
        padding: 0.5rem 0;
    }

    .stats-grid {
        gap: 1rem;
    }

    #statsDisplay .content-container {
        padding: 1rem;
        margin-bottom: 1rem;
    }
    .account-bar {
        gap: 0.5rem;
    }
    .account-name {
        min-width: 150px;
        font-weight: unset;
    }
    .bar-container {
        background: buttonface;
        min-width: 80px;
    }
    .bar-fill {
        background: linear-gradient(135deg, var(--primary-color), var(--primary-dark));
    }
    .bar-amount-outside {
        color: #333;
    }
    .bar-amount-inside {
        color: white;
    }
    .balance-section p {
        line-height: 3rem;
    }

    .onlyMobile {
        display: none;
    }
    #cropSection {
        min-height: calc(100vh - 5.5rem);
    }
    .reverseButton {
        order: unset;
        width: unset;
    }
    .receipt-item-meta {
        gap: 1rem;
    }
    .item-field.gesamtpreis {
        display: unset;
    }
}
