body {
    font-family: 'Noto Sans JP', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 20px;
    background-color: #f0f2f5; /* Lighter, softer grey */
    color: #333;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

#app {
    max-width: 1400px; /* Limit max width for very large screens */
    margin: 0 auto;
    background-color: #fff;
    padding: 25px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
    box-sizing: border-box;
    border-radius: 12px; /* Softer radius */
    border: 1px solid #e1e4e8;
}

#controls-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap; /* Allow wrapping on smaller screens */
    gap: 15px; /* Space between rows if they wrap */
}

#header-info {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    background-color: #e9ecef; /* Neutral grey */
    font-size: 1em; /* Slightly larger */
    font-weight: 500;
    border-radius: 8px; /* Softer radius */
    gap: 10px; /* Space between elements */
    border: 1px solid #dee2e6;
}

#header-info .spacer {
    width: 20px;
}

#targetYearDisplay {
    font-weight: 700;
    font-size: 1.2em; /* More prominent */
    padding: 0 5px;
    min-width: 50px;
    text-align: center;
    color: #0056b3; /* Darker blue */
}

.year-nav-btn {
    background: #fff;
    border: 1px solid #ced4da;
    color: #495057;
    border-radius: 50%;
    width: 28px; /* Slightly larger */
    height: 28px; /* Slightly larger */
    cursor: pointer;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

.year-nav-btn:hover {
    background-color: #007bff;
    color: white;
    border-color: #007bff;
}

.year-text {
    margin-left: -4px; /* Move "年度" closer to the number */
}

#targetYearInput, #fiscalMonthInput {
    padding: 6px 10px; /* More padding */
    border: 1px solid #ced4da; /* Consistent border */
    border-radius: 6px; /* Softer radius */
    font-size: 1em;
    background-color: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}

#targetYearInput:focus, #fiscalMonthInput:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

#fiscalMonthInput {
    width: 60px;
}

#file-operations {
    display: flex;
    align-items: center;
    gap: 10px; /* Space between buttons */
}

.control-button {
    padding: 8px 16px;
    font-size: 0.9em;
    cursor: pointer;
    background-color: #fff; /* White background */
    color: #495057; /* Dark text */
    border: 1px solid #ced4da; /* Standard border */
    border-radius: 6px;
    font-weight: 500;
    white-space: nowrap;
    transition: all 0.2s ease-in-out;
    display: flex;
    align-items: center;
    gap: 6px; /* Space between icon and text */
}

.control-button:disabled {
    background-color: #e9ecef;
    color: #6c757d;
    cursor: not-allowed;
    border-color: #dee2e6;
}

.control-button:not(:disabled):hover {
    background-color: #f8f9fa;
    border-color: #adb5bd;
}

#helpButton {
    background-color: #17a2b8; /* Teal */
    color: white;
    border-color: #17a2b8;
}

#helpButton:hover {
    background-color: #138496;
    border-color: #117a8b;
}

#saveButton {
    background-color: #28a745; /* Green for save */
    color: white;
    border-color: #28a745;
}

#saveButton:hover {
    background-color: #218838;
    border-color: #1e7e34;
}

#undoButton, #redoButton {
    background-color: #6c757d; /* Grey */
    color: white;
    border-color: #6c757d;
}

#undoButton:hover, #redoButton:hover {
    background-color: #5a6268;
    border-color: #545b62;
}

#updateFiscalMonthBtn {
    background-color: #007bff; /* Blue for update */
    color: white;
    border-color: #007bff;
}

#updateFiscalMonthBtn:hover {
    background-color: #0069d9;
    border-color: #0062cc;
}

#updateButton {
    background-color: #0366d6; /* GitHub blue */
    margin-left: 10px;
}

#updateButton:hover {
    background-color: #005cc5;
}

#loadButton {
    background-color: #6f42c1; /* GitHub purple */
}

#loadButton:hover {
    background-color: #5832a0;
}

#tabs {
    margin-bottom: 0;
    border-bottom: 1px solid #dee2e6; /* Lighter underline */
    display: flex;
    align-items: flex-end;
}

#addTabBtn {
    margin-left: 8px;
    padding: 2px 10px;
    font-size: 1.4em;
    font-weight: bold;
    border-radius: 6px;
    line-height: 1.2;
    height: fit-content;
    margin-bottom: 2px;
    background-color: #f0f2f5;
    border: 1px dashed #ced4da;
    color: #495057;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
}

#addTabBtn:hover {
    background-color: #e9ecef;
    border-color: #007bff;
    color: #007bff;
}

.tab-button {
    padding: 12px 20px; /* More padding */
    cursor: pointer;
    border: 1px solid transparent;
    border-bottom: none;
    background-color: transparent;
    font-size: 1em;
    margin-right: 2px;
    border-radius: 8px 8px 0 0; /* Rounded top corners */
    position: relative;
    bottom: -1px;
    font-weight: 500;
    color: #495057; /* Subtler text color */
    transition: all 0.2s;
}

.tab-button .remove-tab-btn {
    position: absolute;
    top: 2px;
    right: 2px;
    font-size: 0.8em;
    color: #dc3545;
    opacity: 0;
    transition: opacity 0.2s;
    background: #fff;
    border-radius: 50%;
    padding: 2px;
    line-height: 1;
}

.tab-button:hover .remove-tab-btn {
    opacity: 0.6;
}

.tab-button .remove-tab-btn:hover {
    opacity: 1;
    color: #c82333;
}

.tab-button.active {
    background-color: #fff;
    color: #0056b3; /* Active color */
    border-color: #dee2e6 #dee2e6 #fff; /* Creates the "cutout" effect */
    font-weight: 700;
}

.tab-button[data-tab="tabA"].active { 
    border-top: 4px solid #007bff; 
}
.tab-button[data-tab="tabB"].active { 
    border-top: 4px solid #17a2b8;
}
.tab-button[data-tab="tabOfficeRent"].active { 
    border-top: 4px solid #ffc107;
}

.tab-button[contenteditable="true"]:focus {
    outline: 2px solid #007bff;
    background-color: #e7f1ff;
}

.tab-button:not(.active):hover {
    background-color: #f6f8fa;
    color: #24292e;
}

.tab-pane {
    display: none; /* Hide panes by default */
    animation: fadeIn 0.5s;
    margin-bottom: 25px; /* Reduced space between tables */
    padding-top: 25px; /* Space between tabs and table */
}

#tabContent #tabHelp {
    display: none; /* This is no longer used, ensure it's hidden */
}

#memo-section {
    margin-top: 25px;
    padding: 20px;
    border-radius: 8px;
    background-color: #f8f9fa;
    border: 1px solid #dee2e6;
}

.memo-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

#memo-section label {
    margin-bottom: 0;
    font-weight: 700;
    font-size: 1.1em;
    color: #343a40;
}

#memo-input {
    width: 100%;
    box-sizing: border-box;
    padding: 12px;
    border: 1px solid #ced4da;
    border-radius: 6px;
    font-family: inherit;
    font-size: 1em;
    resize: vertical;
    min-height: 80px;
    transition: border-color 0.2s, box-shadow 0.2s;
    background-color: #fff;
}

#memo-input:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

#bottom-controls {
    margin-top: 20px;
    display: flex;
    justify-content: flex-start;
}

#fiscal-month-control {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background-color: #e9ecef;
    font-size: 0.9em;
    font-weight: 500;
    border-radius: 8px;
    border: 1px solid #dee2e6;
}

.tab-pane h2 {
    display: none; /* Hide headers as they are now in the tabs */
    font-size: 1.4em;
    color: #333;
    padding-bottom: 10px;
    border-bottom: 2px solid #217346; /* Excel green */
    margin-bottom: 15px;
}

.tab-pane.active {
    display: block; /* Show only the active pane */
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

.table-wrapper {
    overflow-x: auto;
    width: 100%;
    border: 1px solid #dee2e6;
    border-radius: 8px;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 0;
    font-size: 0.9em;
    border: none; /* Removed border as it's on the wrapper */
    border-radius: 8px;
    overflow: hidden; /* To apply border-radius to cells */
}

th, td {
    border: 1px solid #dee2e6;
    padding: 10px 12px; /* Increased padding */
    text-align: left;
    position: relative; /* Needed for fill handle positioning */
    white-space: nowrap;
}

thead th {
    background-color: #f8f9fa; /* Lighter header */
    text-align: center;
    font-weight: 600; /* Bolder font */
    color: #495057;
    white-space: nowrap; /* Prevent month headers from wrapping */
    position: sticky;
    top: 0;
    z-index: 2;
}

/* Zebra striping for readability */
tbody tr:nth-of-type(even) {
    background-color: #f8f9fa;
}

tbody tr:hover {
    background-color: #e9ecef;
}

/* Row header cell (first column) */
tbody th.row-header {
    background-color: #f8f9fa;
    min-width: 150px; /* Wider for horizontal text */
    text-align: left; /* Align text to the left */
    vertical-align: middle;
    padding-left: 15px; /* Add some padding */
    font-weight: 500;
    position: sticky;
    left: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    gap: 8px;
}

.row-drag-handle {
    cursor: grab;
    color: #adb5bd;
    font-size: 1.1em;
    opacity: 0.5;
    transition: opacity 0.2s;
}

.row-header:hover .row-drag-handle {
    opacity: 1;
}

.row-drag-handle:active {
    cursor: grabbing;
}

tbody tr:nth-of-type(even) th.row-header {
    background-color: #f1f3f5;
}

.vertical-text {
    writing-mode: vertical-rl; /* Vertical text, right to left flow for Japanese */
    text-orientation: mixed;   /* Keeps characters upright */
    white-space: nowrap;       /* Prevent text from wrapping within the vertical cell */
    padding: 5px 0; /* Adjust padding for vertical text */
    margin: auto; /* Helps centering if the container is larger */
    display: inline-block; /* Essential for proper rendering in some browsers */
}

td[contenteditable="true"] {
    background-color: transparent; /* Let the tr background show through */
    transition: background-color 0.2s, box-shadow 0.2s;
}

td[contenteditable="true"]:focus {
    background-color: #e7f1ff; /* Light blue when cell is focused for editing */
    outline: none;
    box-shadow: inset 0 0 0 2px #007bff; /* Blue inner shadow on focus */
}

#fill-handle {
    position: absolute;
    width: 8px;
    height: 8px;
    background-color: #007bff;
    border: 1px solid white;
    cursor: crosshair;
    z-index: 10;
}

td.fill-drag-over {
    background-color: #cfe2ff !important;
    border: 1px dashed #007bff;
}

td.calculated {
    background-color: #e9ecef;
    font-weight: bold;
    color: #212529;
}

/* Total column styling */
td.total-col {
    font-weight: 700;
    background-color: #e2eafc; /* Distinct background for totals */
    border-left: 2px solid #a9bcf5; /* Separator */
}
thead th:last-child {
    border-left: 2px solid #a9bcf5;
}

.summary-row th.row-header,
.summary-row td {
    background-color: #d1e7dd; /* A light green for emphasis */
    font-weight: 700;
    color: #0f5132; /* Darker green text */
    border-top: 2px solid #a3cfbb; /* Separator line */
}

.summary-row[data-row="差引支給額"] th.row-header,
.summary-row[data-row="差引支給額"] td {
    background-color: #f8d7da; /* Light red */
    color: #721c24; /* Darker red text */
    border-top: 2px solid #f5c6cb;
}

.summary-row[data-row="差引支給額"] td {
    color: #dc3545; /* Red text for the numbers */
    text-align: center;
}

/* Styles for new features */
td.date-cell {
    padding: 0;
}

td.date-cell input {
    width: 100%;
    height: 100%;
    border: none;
    background-color: transparent;
    padding: 10px 12px; /* Match cell padding */
    font-family: inherit;
    font-size: inherit;
    color: inherit;
    box-sizing: border-box;
    text-align: center;
}

td.date-cell input:focus {
    outline: none;
    background-color: #e7f1ff; 
}

.add-row-cell {
    text-align: center;
    padding: 8px;
    background-color: #fff;
}

.add-row-btn {
    width: 100%;
    padding: 8px;
    font-size: 0.9em;
    cursor: pointer;
    background-color: #f8f9fa;
    color: #007bff;
    border: 1px dashed #a0caff;
    border-radius: 6px;
    font-weight: 500;
    transition: background-color 0.2s, border-color 0.2s;
}

.add-row-btn:hover {
    background-color: #e7f1ff;
    border-color: #007bff;
}

.add-row-btn .fa-plus {
    margin-right: 5px;
}

.remove-row-btn {
    cursor: pointer;
    color: #dc3545; /* Bootstrap red for delete */
    margin-left: 8px;
    font-size: 0.9em;
    opacity: 0.5;
    transition: opacity 0.2s;
}

.row-header:hover .remove-row-btn {
    opacity: 1;
}

.remove-row-btn:hover {
    color: #c82333;
}

/* Modal Styles */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1000; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgba(0,0,0,0.5); /* Black w/ opacity */
    animation: fadeIn 0.3s;
}

.modal-content {
    background-color: #fefefe;
    margin: 10% auto; /* Move up a bit */
    padding: 25px 35px; /* More padding */
    border: 1px solid #d1d5da;
    width: 80%;
    max-width: 500px; /* Slightly wider */
    border-radius: 12px; /* Softer radius */
    box-shadow: 0 5px 15px rgba(0,0,0,0.3);
    position: relative;
    animation: slideIn 0.3s;
}

#helpModal .modal-content {
    max-width: 800px; /* Wider for help content */
}

@keyframes slideIn {
    from { transform: translateY(-30px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.close-button {
    color: #6c757d;
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 32px;
    font-weight: bold;
}

.close-button:hover,
.close-button:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-content h2 {
    margin-top: 0;
    margin-bottom: 25px;
    font-size: 1.6em;
    color: #343a40;
    border-bottom: 1px solid #e1e4e8;
    padding-bottom: 15px;
}

.modal-body {
    padding-bottom: 20px;
}

.modal-body label {
    display: block;
    margin-bottom: 8px;
    margin-top: 15px;
    font-weight: 600;
    color: #495057;
}

.modal-body input[type="text"] {
    width: 100%;
    padding: 12px;
    border: 1px solid #ced4da;
    border-radius: 6px;
    box-sizing: border-box;
    font-size: 1em;
}

.modal-body input[type="text"]:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

#rowTypeSelector {
    margin-top: 15px;
}

#rowTypeSelector label {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    font-weight: normal;
    cursor: pointer;
    padding: 10px;
    border-radius: 6px;
    border: 1px solid #eee;
    transition: background-color 0.2s;
}

#payroll-row-types, #rent-row-types {
    display: none; /* Hide both by default */
}

#rowTypeSelector label:has(input:checked),
#tabTypeSelector label:has(input:checked) {
    background-color: #e7f1ff;
    border-color: #a0caff;
}

#rowTypeSelector input[type="radio"],
#tabTypeSelector input[type="radio"] {
    margin-right: 12px;
    cursor: pointer;
}

.modal-footer {
    text-align: right;
    padding-top: 15px;
    border-top: 1px solid #e1e4e8;
}

.row-header span.custom-row-type {
    font-size: 0.8em;
    margin-left: 5px;
    color: #586069;
    font-weight: normal;
}

.row-header span.type-addition {
    color: #28a745; /* Green */
}

.row-header span.type-deduction {
    color: #dc3545; /* Red */
}

/* Help Tab Styles */
#tabHelp {
    padding: 10px 20px;
    line-height: 1.7;
    font-size: 0.95em;
}

#tabHelp h2 {
    display: block; /* Override display:none from .tab-pane h2 */
    font-size: 1.8em;
    color: #333;
    padding-bottom: 15px;
    border-bottom: 2px solid #007bff;
    margin-bottom: 25px;
}

#tabHelp h3 {
    font-size: 1.3em;
    color: #0056b3;
    margin-top: 30px;
    margin-bottom: 15px;
    padding-left: 10px;
    border-left: 4px solid #007bff;
}

#tabHelp ul {
    list-style-type: none;
    padding-left: 20px;
}

#tabHelp li {
    margin-bottom: 10px;
}

#tabHelp ul ul {
    margin-top: 8px;
    padding-left: 25px;
}

#tabHelp ul ul li::before {
    content: "–"; /* en dash */
    margin-right: 8px;
    color: #6c757d;
}

#tabHelp strong {
    font-weight: 600;
    color: #343a40;
}

#tabHelp code {
    background-color: #e9ecef;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: Consolas, 'Courier New', monospace;
    font-size: 0.9em;
}

/* Help Content Styles (inside modal) */
#helpContent {
    padding: 10px 20px;
    line-height: 1.7;
    font-size: 0.95em;
    max-height: 70vh;
    overflow-y: auto;
}

#helpContent h2 {
    display: block;
    font-size: 1.8em;
    color: #333;
    padding-bottom: 15px;
    border-bottom: 2px solid #007bff;
    margin-bottom: 25px;
}

#helpContent h3 {
    font-size: 1.3em;
    color: #0056b3;
    margin-top: 30px;
    margin-bottom: 15px;
    padding-left: 10px;
    border-left: 4px solid #007bff;
}

#helpContent ul {
    list-style-type: none;
    padding-left: 20px;
}

#helpContent li {
    margin-bottom: 10px;
}

#helpContent ul ul {
    margin-top: 8px;
    padding-left: 25px;
}

#helpContent ul ul li::before {
    content: "–"; /* en dash */
    margin-right: 8px;
    color: #6c757d;
}

#helpContent strong {
    font-weight: 600;
    color: #343a40;
}

#helpContent code {
    background-color: #e9ecef;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: Consolas, 'Courier New', monospace;
    font-size: 0.9em;
}