:root {
    --primary-color: #007bff;
    --secondary-color: #f8f9fa;
    --text-color: #333;
    --border-color: #dee2e6;
    --danger-color: #dc3545;
    --success-color: #28a745;
    --warning-color: #ffc107;
    --shadow-color: rgba(0, 0, 0, 0.1);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    flex-wrap: wrap;
    position: relative;
    padding: 0 2rem;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: #f4f7f6;
    color: var(--text-color);
    line-height: 1.6;
}

.name-setting-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding-left: 0;
    margin-bottom: 1rem;
}

.name-input {
    border: 1px solid transparent;
    border-radius: 4px;
    padding: 0.5rem;
    font-size: 1.2rem;
    font-weight: bold;
    width: 250px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.name-input:hover, .name-input:focus {
    border-color: var(--border-color);
    box-shadow: 0 0 5px rgba(0, 123, 255, 0.2);
}

.name-setting-container span {
    font-size: 1.2rem;
    font-weight: bold;
}

#app {
    max-width: 100%;
    width: 100%;
    margin: 0;
    padding: 1rem 2rem;
    background-color: #fff;
    border-radius: 0;
    box-shadow: 0 4px 12px var(--shadow-color);
    min-height: 100vh;
}

header {
    text-align: center;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 1rem;
    margin-bottom: 2rem;
}

header h1 {
    color: var(--primary-color);
    margin-bottom: 0.5rem;
    flex-grow: 1;
}

.header-container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    position: relative;
    padding: 0 2rem;
}

.header-buttons-right {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    display: flex;
    gap: 0.5rem;
}

.refresh-btn {
    background-color: var(--danger-color);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
}

.refresh-btn:hover {
    background-color: #c82333;
}

.help-btn {
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    cursor: pointer;
    transition: background-color 0.2s;
}

.help-btn:hover {
    background-color: #0056b3;
}

.settings-section {
    margin-top: 2rem;
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: center;
    gap: 1rem;
    padding: 1.5rem;
    background-color: var(--secondary-color);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.settings-section .form-group {
    flex: 1 1 200px;
    max-width: 300px;
}

.settings-section button {
    padding: 0.6rem 1.2rem;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    height: calc(0.6rem * 2 + 1rem + 2px); /* Align with other inputs */
}

.settings-section button:hover {
    background-color: #0056b3;
}

.year-selector {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1rem;
}

.year-selector button {
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 50%;
    width: 30px;
    height: 30px;
    font-size: 1rem;
    cursor: pointer;
    transition: background-color 0.2s;
}

.year-selector button:hover {
    background-color: #0056b3;
}

.year-selector h2 {
    color: var(--text-color);
    font-size: 1.8rem;
    min-width: 120px;
    text-align: center;
}

main > section {
    margin-bottom: 2.5rem;
}

.data-table-section .table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1rem;
}

.data-table-section .table-header-title {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.data-table-section .table-header h3 {
    margin-bottom: 0;
}

.header-buttons {
    display: flex;
    gap: 0.5rem;
}

.header-buttons button, .annual-view-btn {
    padding: 0.6rem 1.2rem;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s, box-shadow 0.2s;
}

.upload-download-section {
    position: relative;
    display: inline-block;
}

.upload-download-section button {
    padding: 0;
    color: inherit;
    border: none;
    border-radius: 0;
    font-size: inherit;
    font-weight: inherit;
    cursor: pointer;
    transition: none;
}

.upload-download-section button:first-of-type {
    background-color: transparent;
}

.upload-download-section button:first-of-type:hover {
    background-color: transparent;
}

.upload-download-section button:last-of-type {
    background-color: transparent;
}

.upload-download-section button:last-of-type:hover {
    background-color: transparent;
}

.data-management-btn {
    background-color: var(--success-color);
    padding: 0.6rem 1.2rem;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s;
}

.data-management-btn:hover {
    background-color: #218838;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    border-radius: 4px;
    overflow: hidden;
    right: 0;
}

.dropdown-content.show {
    display: block;
}

.dropdown-content button {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.95rem;
}

.dropdown-content button:hover {
    background-color: #f1f1f1;
}

.fiscal-month-setting {
    margin-top: 2rem;
    padding: 1.5rem;
    background-color: var(--secondary-color);
    border-radius: 8px;
    border: 1px solid var(--border-color);
    display: flex;
    justify-content: center;
    align-items: flex-end;
    gap: 1rem;
}

.fiscal-month-setting .form-group {
    flex: 0 1 200px;
}

.fiscal-month-setting button {
    padding: 0.6rem 1.2rem;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    height: calc(0.6rem * 2 + 1rem + 2px);
}

.fiscal-month-setting button:hover {
    background-color: #0056b3;
}

.annual-view-btn {
    background-color: var(--success-color);
}

.annual-view-btn:hover {
    background-color: #218838;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

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

.pdf-btn:hover {
    background-color: #0056b3;
}

.print-btn {
    background-color: var(--warning-color);
    color: var(--text-color);
}

.print-btn:hover {
    background-color: #e0a800;
}

.tabs {
    display: flex;
    flex-wrap: wrap;
    border-bottom: 2px solid var(--border-color);
    margin-bottom: 1rem;
}

.tab {
    padding: 0.8rem 1.2rem;
    cursor: pointer;
    border: none;
    background-color: transparent;
    font-size: 1rem;
    position: relative;
    color: #555;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px; /* Align with the container's border */
}

.tab:first-of-type {
    border-top-left-radius: 4px;
}

.tab:last-of-type {
    border-top-right-radius: 4px;
}

.tab:hover {
    background-color: var(--secondary-color);
}

.tab.active {
    color: var(--primary-color);
    font-weight: 600;
    border-bottom-color: var(--primary-color);
}

h3 {
    font-size: 1.4rem;
    color: var(--primary-color);
    margin-bottom: 1rem;
    border-left: 4px solid var(--primary-color);
    padding-left: 0.8rem;
}

#add-entry-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

#add-entry-form .property-name-group {
    grid-column: 1 / -1;
}

.form-row-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
    align-items: flex-end;
}

.form-group-pair {
    display: flex;
    gap: 0.5rem; /* Reduced gap for paired items */
    align-items: flex-end;
}

.form-group-pair .form-group {
    flex: 1;
    min-width: 0;
}

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

.form-group label {
    margin-bottom: 0.3rem;
    font-weight: 600;
}

.form-group input {
    padding: 0.6rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
}

.form-group select {
    padding: 0.6rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 1rem;
    background-color: white;
}

#add-entry-form button[type="submit"] {
    padding: 0.6rem;
    background-color: var(--success-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    height: auto; /* Allow height to adjust */
    width: 100%;
}

#add-entry-form button[type="submit"]:hover {
    background-color: #218838;
}

.summary-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    text-align: center;
    background-color: var(--secondary-color);
    padding: 1.5rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.summary-grid p:first-child {
    font-size: 1.1rem;
    font-weight: 600;
    color: #555;
}

.summary-grid p:last-child {
    font-size: 2rem;
    font-weight: bold;
    color: var(--primary-color);
}

.summary-section h3 {
    cursor: pointer;
    transition: color 0.2s;
}

.summary-section h3:hover {
    color: #0056b3;
}

.summary-section h3.no-click {
    cursor: default;
}

.summary-section h3.no-click:hover {
    color: var(--primary-color);
}

.monthly-summary-section h3 {
    cursor: pointer;
    transition: color 0.2s;
    display: inline-block;
}

.monthly-summary-section #monthly-summary-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 1rem;
    background-color: var(--secondary-color);
    padding: 1rem;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.monthly-item {
    padding: 0.8rem;
    text-align: center;
    border-radius: 4px;
    background-color: #fff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.monthly-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.monthly-item.selected {
    border: 2px solid var(--primary-color);
    box-shadow: 0 0 10px rgba(0, 123, 255, 0.3);
}

.monthly-item .month {
    font-weight: 600;
    margin-bottom: 0.3rem;
}

.monthly-item .amount {
    color: var(--primary-color);
    font-weight: bold;
    font-size: 1.1rem;
}

.table-container {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

th, td {
    padding: 0.8rem 1rem;
    border-bottom: 1px solid var(--border-color);
    vertical-align: middle;
}

thead {
    background-color: var(--secondary-color);
}

th {
    font-weight: 600;
}

table th:first-child,
table td:first-child {
    width: 80px;
    white-space: nowrap;
}

tr:hover {
    background-color: #f1f1f1;
}

.action-buttons {
    display: flex;
    gap: 0.5rem;
}

.edit-btn {
    background-color: var(--warning-color);
    color: var(--text-color);
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.edit-btn:hover {
    background-color: #e0a800;
}

.delete-btn {
    background-color: var(--danger-color);
    color: white;
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.delete-btn:hover {
    background-color: #c82333;
}

/* Login Styles */
.login-container {
    max-width: 400px;
    margin: 5rem auto;
    padding: 2rem;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    text-align: center;
}

.login-container h1 {
    margin-bottom: 1.5rem;
    color: var(--primary-color);
}

.login-container .form-group {
    text-align: left;
    margin-bottom: 1.5rem;
}

.login-container button {
    width: 100%;
    padding: 0.8rem;
    font-size: 1.1rem;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.login-container button:hover {
    background-color: #0056b3;
}

.error-message {
    color: var(--danger-color);
    margin-bottom: 1rem;
}

/* Modal Styles */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-content {
    background: white;
    padding: 2rem;
    border-radius: 8px;
    width: 90%;
    max-width: 600px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    position: relative;
    z-index: 1001;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 1rem;
    margin-bottom: 1.5rem;
}

.modal-header h3 {
    margin: 0;
    border: none;
    padding: 0;
}

.close-button {
    background: none;
    border: none;
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    color: #aaa;
}

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

#edit-entry-form {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}

#edit-entry-form .form-group {
    grid-column: span 1;
}

#edit-entry-form .form-group:nth-of-type(2),
#edit-entry-form .form-group:nth-of-type(3),
#edit-entry-form .form-group:nth-of-type(4),
#edit-entry-form .form-group:nth-of-type(5) {
    grid-column: span 1;
}

#edit-entry-form button[type="submit"] {
    grid-column: 2 / -1;
    justify-self: end;
    padding: 0.7rem 1.5rem;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 1rem;
    cursor: pointer;
}

#edit-entry-form button[type="submit"]:hover {
    background-color: #0056b3;
}

.help-modal-content {
    text-align: left;
    max-height: 80vh;
    overflow-y: auto;
}

.help-modal-content h4 {
    color: var(--primary-color);
    font-size: 1.2rem;
    margin-top: 1.5rem;
    margin-bottom: 0.8rem;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
}

.help-modal-content ul {
    list-style-position: inside;
    padding-left: 0.5rem;
}

.help-modal-content li {
    margin-bottom: 0.6rem;
    line-height: 1.7;
}

.help-modal-content code {
    background-color: #e9ecef;
    padding: 2px 5px;
    border-radius: 4px;
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
}

.help-modal-content strong {
    font-weight: 600;
}

.help-modal-content .warning {
    background-color: #fff3cd;
    border-left: 4px solid #ffeeba;
    padding: 0.8rem 1.2rem;
    border-radius: 4px;
    margin: 1rem 0;
}

/* Print Styles */
.print-only {
    display: none;
}

@media print {
    body * {
        visibility: hidden;
    }

    #app {
        visibility: hidden;
    }

    .print-container, .print-container * {
        visibility: visible;
    }

    .print-container {
        position: absolute;
        left: 20px;
        top: 20px;
        width: calc(100% - 40px);
    }
    
    .print-container h2 {
        font-size: 1.5rem;
        color: #000;
        margin-bottom: 1rem;
        border-bottom: 2px solid #ccc;
        padding-bottom: 0.5rem;
    }

    .print-container table {
        width: 100%;
        border-collapse: collapse;
        font-size: 8pt; /* Smaller font for printing */
        table-layout: fixed;
    }
    
    .print-container th, .print-container td {
        border: 1px solid #ccc;
        padding: 4px 5px;
        color: #000;
        overflow-wrap: break-word;
    }

    .print-container thead {
        background-color: #eee;
    }
    
    .print-container th:nth-child(1), .print-container td:nth-child(1) { width: 10%; } /* 日付 */
    .print-container th:nth-child(2), .print-container td:nth-child(2) { width: 30%; } /* 物件名 */
    .print-container th:nth-child(3), .print-container td:nth-child(3) { width: 15%; } /* 仲介料 */
    .print-container th:nth-child(4), .print-container td:nth-child(4) { width: 15%; } /* 広告料日付 */
    .print-container th:nth-child(5), .print-container td:nth-child(5) { width: 15%; } /* 広告料 */
    .print-container th:nth-child(6), .print-container td:nth-child(6) { width: 15%; } /* 歩合額 */

    .print-container .action-buttons, .print-container .edit-btn, .print-container .delete-btn {
        display: none;
    }
    
    @page {
        size: A4;
        margin: 1cm;
    }
}

/* Responsive */
@media (max-width: 768px) {
    .data-table-section .table-header {
        flex-direction: column;
        align-items: stretch;
    }

    .header-buttons {
        justify-content: flex-start;
    }
}

@media (max-width: 600px) {
    #app {
        padding: 0.5rem;
        margin: 1rem auto;
    }

    #add-entry-form {
        grid-template-columns: 1fr;
    }

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

    .monthly-summary-section #monthly-summary-container {
        grid-template-columns: 1fr 1fr;
    }

    #edit-entry-form {
        grid-template-columns: 1fr;
    }

    #edit-entry-form button[type="submit"] {
        grid-column: 1 / -1;
        width: 100%;
    }

    .header-container {
        padding: 0;
    }
    
    .help-btn {
        position: static;
        transform: none;
        margin-top: 1rem;
        width: 100%;
    }
}