/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: #f5f5f5;
    color: #333;
    line-height: 1.6;
}

.hidden {
    display: none !important;
}

/* Login Screen */
#login-screen {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-container {
    background: white;
    padding: 3rem;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
    text-align: center;
    max-width: 400px;
    width: 90%;
}

.login-container h1 {
    margin-bottom: 1rem;
    color: #333;
}

.login-container p {
    margin-bottom: 2rem;
    color: #666;
}

/* Main App Layout */
#main-app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-header {
    background: #667eea;
    color: white;
    padding: 1rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.app-header h1 {
    font-size: 1.5rem;
}

.app-title-clickable {
    cursor: pointer;
    transition: opacity 0.2s;
    user-select: none;
}

.app-title-clickable:hover {
    opacity: 0.8;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.user-menu-container {
    position: relative;
}

.user-name-clickable {
    cursor: pointer;
    font-weight: 500;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    transition: background-color 0.2s;
    position: relative;
    display: inline-block;
}

.user-name-clickable:hover {
    background-color: #f0f0f0;
}

.user-menu-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    background: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    min-width: 200px;
    z-index: 1000;
    margin-top: 0.5rem;
    overflow: visible;
}

.user-menu-item {
    display: block;
    padding: 0.75rem 1rem;
    color: #333;
    text-decoration: none;
    transition: background-color 0.2s;
}

.user-menu-item:hover {
    background-color: #f8f9fa;
}

.admin-submenu {
    position: absolute;
    top: 0;
    background: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    min-width: 180px;
    z-index: 1001;
    /* Position will be set dynamically by JavaScript */
}

.notifications-badge-menu {
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    background: #dc3545;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 0.7em;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
}

.notifications-badge-user-name {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    background: #dc3545;
    border-radius: 50%;
    width: 12px;
    height: 12px;
    border: 2px solid white;
    z-index: 10;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.app-nav {
    background: white;
    padding: 0.5rem 2rem;
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.nav-btn {
    padding: 0.75rem 1.5rem;
    border: none;
    background: transparent;
    color: #666;
    cursor: pointer;
    border-radius: 5px;
    transition: all 0.2s;
    white-space: nowrap;
}

.nav-btn:hover {
    background: #f0f0f0;
}

.nav-btn.active {
    background: #667eea;
    color: white;
}

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

.view {
    display: none;
}

.view.active {
    display: block;
}

.view h2 {
    margin-bottom: 1.5rem;
    color: #333;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    transition: all 0.2s;
    font-weight: 500;
}

.btn-primary {
    background: #667eea;
    color: white;
}

.btn-primary:hover {
    background: #5568d3;
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover {
    background: #5a6268;
}

.btn-danger {
    background: #dc3545;
    color: white;
}

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

/* Cards Grid */
.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 1.5rem;
    margin-top: 1rem;
}

.card {
    background: white;
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s, box-shadow 0.2s;
    position: relative;
    overflow: hidden;
    cursor: grab;
    /* Prevent iOS/Android long-press from triggering text selection or the
       contextual callout, which steals the gesture from our touch-based
       drag & drop handler and makes card reordering impossible on parts of
       the card that contain text. */
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

/* -webkit-touch-callout is NOT inherited; if a <span> or nested element
   inside a .card doesn't have it set, long-pressing on that specific child
   triggers the OS text-selection / callout menu, which hijacks the gesture
   and breaks drag-to-reorder on mobile. Mirror user-select to be safe. */
.card,
.card * {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card.dragging {
    opacity: 0.5;
    cursor: grabbing;
}

.card:active {
    cursor: grabbing;
}

/* Overlay for cards with logo backgrounds to ensure text readability */
.card[style*="background-image"]::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.85);
    z-index: 0;
}

.card[style*="background-image"] > * {
    position: relative;
    z-index: 1;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: #333;
}

.card-balance {
    font-size: 1.5rem;
    font-weight: 700;
    color: #667eea;
    margin: 0.5rem 0;
}

.card-meta {
    color: #666;
    font-size: 0.9rem;
}

/* List Container */
.list-container {
    margin-top: 1rem;
}

.list-item {
    background: white;
    padding: 1rem 1.5rem;
    margin-bottom: 0.5rem;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    overflow: hidden;
}

.list-item-content {
    flex: 1;
}

.profit {
    color: #2e7d32;
}

.loss {
    color: #d32f2f;
}

.list-item-title {
    font-weight: 600;
    color: #333;
    margin-bottom: 0.25rem;
}

.list-item-meta {
    color: #666;
    font-size: 0.9rem;
}

.list-item-actions {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
}

.list-item-actions::-webkit-scrollbar {
    height: 4px;
}

.list-item-actions::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 2px;
}

.list-item-actions::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 2px;
}

.list-item-actions::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Forms */
.form-container {
    background: white;
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    max-width: 600px;
}

.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    color: #333;
    font-weight: 500;
}

.form-group input,
.form-group select,
.form-group textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #ddd;
    border-radius: 5px;
    font-size: 1rem;
    font-family: inherit;
}

.form-group textarea {
    resize: vertical;
    min-height: 100px;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

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

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

.checkbox-group input[type="checkbox"] {
    width: auto;
}

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

.modal-overlay:has(.modal-content.fullscreen) {
    padding: 0;
    background: #000;
    touch-action: none;
    -webkit-overflow-scrolling: none;
}

body.modal-fullscreen-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    touch-action: none;
}

.modal-content {
    background: white;
    border-radius: 8px;
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    position: relative;
    padding: 2rem;
}

.modal-content.fullscreen {
    max-width: 100vw;
    max-height: 100vh;
    width: 100vw;
    height: 100vh;
    padding: 0;
    margin: 0;
    overflow: hidden;
    border-radius: 0;
}

.modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: rgba(255, 255, 255, 0.9);
    border: none;
    font-size: 2rem;
    cursor: pointer;
    color: #333;
    line-height: 1;
    padding: 0.25rem 0.5rem;
    min-width: 44px;
    min-height: 44px;
    width: auto;
    border-radius: 4px;
    z-index: 1001;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.modal-content.fullscreen .modal-close {
    background: rgba(255, 255, 255, 0.95);
    color: #000;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

/* Summary Container */
.summary-container {
    background: white;
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    margin-bottom: 2rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.summary-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px; /* Ensure table doesn't shrink too much on mobile */
}

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

.summary-table th {
    font-weight: 600;
    color: #333;
    background: #f8f9fa;
}

/* Responsive */
@media (max-width: 768px) {
    .app-nav {
        padding: 0.5rem 1rem;
    }

    .app-content {
        padding: 1rem;
    }

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

    .app-header {
        flex-direction: column;
        gap: 1rem;
        text-align: center;
    }

    /* Ensure summary table is scrollable on mobile and extends to edges */
    .summary-container {
        padding: 1rem;
        margin-left: calc(-1rem - 1px);
        margin-right: calc(-1rem - 1px);
        width: calc(100% + 2rem + 2px);
        border-radius: 0;
        box-shadow: none;
        border-top: 1px solid #eee;
        border-bottom: 1px solid #eee;
        border-left: none;
        border-right: none;
    }

    .summary-table {
        min-width: 500px; /* Smaller min-width on mobile */
    }

    /* Ensure list items don't overflow on mobile */
    .list-item {
        flex-wrap: wrap;
        padding: 1rem;
    }

    .list-item-content {
        min-width: 0;
        flex: 1 1 100%;
        margin-bottom: 0.5rem;
    }

    .list-item-actions {
        flex: 1 1 100%;
        justify-content: flex-start;
        padding-top: 0.5rem;
        border-top: 1px solid #f0f0f0;
        margin-top: 0.5rem;
        margin-left: -1rem;
        margin-right: -1rem;
        padding-left: 1rem;
        padding-right: 1rem;
        width: calc(100% + 2rem);
    }

    .list-item-actions .btn {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Ensure list items also extend to edges on mobile */
    .list-item {
        margin-left: -1rem;
        margin-right: -1rem;
        width: calc(100% + 2rem);
        border-radius: 0;
        border-left: none;
        border-right: none;
    }
}

/* Loading Spinner */
.loading {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 3px solid rgba(102, 126, 234, 0.3);
    border-radius: 50%;
    border-top-color: #667eea;
    animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

