:root {
  --nav: #172234;
  --nav-2: #223149;
  --accent: #245f9e;
  --accent-2: #174a7d;
  --bg: #f3f5f8;
  --panel: #ffffff;
  --line: #cfd6df;
  --text: #1b2430;
  --muted: #667386;
  --soft: #edf3f9;
  --danger: #9f2430;
  --shadow: 0 16px 34px rgba(31, 42, 57, .09);
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-family: "Yu Gothic UI", "Meiryo", "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

#loginScreen,
.login-screen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  background: var(--bg);
}

#loginScreen[hidden] {
  display: none;
}

.login-card {
  width: min(360px, calc(100vw - 32px));
  display: grid;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 24px;
  box-shadow: var(--shadow);
}

.login-card h1 {
  margin: 0 0 4px;
  font-size: 22px;
}

.login-card label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

body[data-role="viewer"] [data-view="manual"],
body[data-role="viewer"] [data-view="import"],
body[data-role="viewer"] [data-view="settings"],
body[data-role="viewer"] #manual,
body[data-role="viewer"] #import,
body[data-role="viewer"] #settings,
body[data-role="viewer"] .row-actions,
body[data-role="viewer"] #exportJson,
body[data-role="viewer"] #restoreJson {
  display: none !important;
}

body,
button,
input,
select {
  letter-spacing: 0;
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 248px;
  background: var(--nav);
  color: #fff;
  padding: 16px 14px;
  border-right: 1px solid #0d1725;
}

.brand {
  min-height: 76px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, .12);
  margin: 0 2px 14px;
  padding: 12px 10px 18px;
  color: #f7f9fc;
  font-size: 19px;
  font-weight: 800;
}

.nav {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: #d7e0ec;
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  margin: 3px 0;
  padding: 12px 14px;
  text-align: left;
}

.nav:hover {
  background: rgba(255, 255, 255, .08);
  color: #fff;
}

.nav.active {
  border-color: rgba(255, 255, 255, .18);
  background: var(--nav-2);
  color: #fff;
  box-shadow: inset 4px 0 0 #8fb7e0;
}

main {
  min-height: 100vh;
  margin-left: 248px;
}

header {
  min-height: 88px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 18px 28px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

h1 {
  margin: 0 0 5px;
  color: #152132;
  font-size: 24px;
  font-weight: 800;
}

h2 { color: #1d2b3c; }
p { margin: 0; }

header p,
.hint {
  color: var(--muted);
}

.stats {
  display: flex;
  gap: 10px;
  white-space: nowrap;
}

.stats span {
  min-width: 132px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f8fafc;
  color: #172234;
  padding: 10px 14px;
  text-align: right;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.view {
  display: none;
  padding: 20px 24px;
}

.view.active { display: block; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.entry-form { display: grid; gap: 14px; }

.entry-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 14px;
}

.entry-row.one { grid-template-columns: minmax(220px, 360px); }
.pending-panel { margin-top: 14px; }

.pending-panel h2 {
  margin: 0 0 10px;
  font-size: 16px;
}

label {
  display: grid;
  gap: 6px;
  color: #526174;
  font-size: 13px;
  font-weight: 800;
}

input,
select {
  width: 100%;
  min-height: 38px;
  border: 1px solid #b8c2cf;
  border-radius: 5px;
  background: #fff;
  color: #111827;
  font: inherit;
  padding: 8px 10px;
}

input[type="file"] { padding: 5px 8px; }
input::placeholder { color: #8a95a4; }

input:hover,
select:hover {
  border-color: #96a3b4;
}

input:focus,
select:focus {
  outline: 2px solid rgba(36, 95, 158, .18);
  border-color: var(--accent);
}

input.invalid-input {
  border-color: var(--danger);
  background: #fff5f5;
}

.wide { grid-column: span 3; }

.actions,
.toolbar,
.export-box {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.actions { align-items: end; }
.toolbar {
  margin-bottom: 14px;
  row-gap: 10px;
}

button {
  min-height: 38px;
  border: 1px solid #aeb8c5;
  border-radius: 5px;
  background: #fff;
  color: #243245;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
  padding: 8px 14px;
}

button:hover {
  border-color: #8794a5;
  background: #f4f7fb;
}

button:active { transform: translateY(1px); }

button.primary,
.import-tab.active,
.report-tab.active,
.report-level-tab.active,
.chart-tab.active {
  border-color: var(--accent-2);
  background: var(--accent);
  color: #fff;
}

button.primary:hover,
.import-tab.active:hover,
.report-tab.active:hover,
.report-level-tab.active:hover,
.chart-tab.active:hover {
  background: var(--accent-2);
}

button.danger,
.danger {
  border-color: var(--danger);
  background: var(--danger);
  color: #fff;
}

button.danger:hover,
.danger:hover {
  background: #84202b;
}

button.active-filter {
  border-color: #9fb8d5;
  background: #e8f1fb;
  color: #174a7d;
}

button:disabled {
  opacity: .5;
  cursor: not-allowed;
  transform: none;
}

.import-tabs,
.report-tabs,
.report-level-tabs,
.chart-toolbar,
.year-shortcuts {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.filters {
  align-items: end;
  padding-bottom: 2px;
}

.filters input { max-width: 280px; }

.filters input[type="month"] {
  max-width: 190px;
}

.filters select {
  flex: 1 1 260px;
  min-width: 220px;
}

.amount-input {
  max-width: 110px;
  text-align: right;
}

.busy {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(243, 245, 248, .72);
}

.busy[hidden] { display: none; }

.busy-box {
  width: min(360px, calc(100vw - 36px));
  border: 1px solid #b9c5d3;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 45px rgba(23, 34, 52, .18);
  padding: 16px;
  color: #1f3d61;
  font-weight: 800;
}

.progress {
  height: 7px;
  margin-top: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e4e9f0;
}

.progress span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .18s ease;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  background: rgba(17, 27, 42, .42);
}

.modal-backdrop[hidden] { display: none; }

.modal-card {
  width: min(480px, calc(100vw - 34px));
  border: 1px solid #9fb0c4;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 22px 64px rgba(17, 27, 42, .26);
  padding: 22px;
}

.modal-card h2 {
  margin: 0 0 12px;
  color: #172234;
  font-size: 20px;
}

.modal-card p {
  color: #35465c;
  line-height: 1.7;
  white-space: pre-wrap;
}

.modal-card input { margin-top: 12px; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.year-shortcuts { margin: -2px 0 14px; }

.year-shortcuts button {
  min-width: 64px;
  padding: 7px 11px;
}

.year-shortcuts button.carryover-confirmed {
  border-color: #315d86;
  background: #315d86;
  color: #fff;
}

.year-shortcuts button.carryover-review {
  border-color: #d09a34;
  background: #fff8e9;
  color: #725012;
}

.year-shortcuts button.carryover-active {
  box-shadow: 0 0 0 2px rgba(36, 95, 158, .22);
}

.hint {
  margin: 8px 0 12px;
  font-size: 12px;
}

.notice {
  border: 1px solid #c8d4e2;
  border-left: 4px solid var(--accent);
  border-radius: 5px;
  background: #f7fafc;
  color: #31465e;
  margin-bottom: 12px;
  padding: 9px 12px;
}

.notice.error {
  border-color: #e5b1b6;
  border-left-color: var(--danger);
  background: #fff6f6;
  color: #8d1f29;
}

.bulk-panel,
.chart-panel,
.fiscal-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
}

.bulk-panel {
  margin-bottom: 12px;
  padding: 14px;
}

.bulk-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(160px, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}

.table-wrap {
  border: 1px solid var(--line);
  border-radius: 6px;
  max-height: calc(100vh - 190px);
  overflow: auto;
  background: #fff;
}

.table-wrap.small { max-height: 360px; }

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: #fff;
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #334155;
  color: #fff;
  font-weight: 800;
}

th,
td {
  border-right: 1px solid #d7dde5;
  border-bottom: 1px solid #d7dde5;
  padding: 7px 9px;
  white-space: nowrap;
}

#ledgerTable,
#flowTable {
  font-size: 14px;
}

#ledgerTable th,
#ledgerTable td,
#flowTable th,
#flowTable td {
  padding: 8px 10px;
}

#ledgerTable th,
#flowTable th {
  font-size: 13px;
}

th:last-child,
td:last-child {
  border-right: 0;
}

tbody tr:nth-child(even) { background: #f8fafc; }
tbody tr:hover { background: #eaf2fb; }

.sort-header {
  width: 100%;
  min-height: 0;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
  padding: 0;
  text-align: inherit;
}

.sort-header:hover { background: transparent; }

tbody tr.bad-row { background: #ffe8ea; }
tbody tr.bad-row:hover { background: #ffdadd; }

td.bad-cell {
  background: #ffd1d6 !important;
  color: #8d1f29;
  font-weight: 800;
}

tbody tr.expense-row { background: #fff; }
tbody tr.expense-row:hover { background: #eef5fc; }
tbody tr.income-row { background: #fff9e8; }
tbody tr.income-row:hover { background: #fff2cf; }
tbody tr.transfer-row { background: #edf6ff; }
tbody tr.transfer-row:hover { background: #dfefff; }
tbody tr.exchange-row { background: #eaf3fb; }
tbody tr.exchange-row:hover { background: #ddecf8; }

tbody tr.group-row { background: #d6edf1; font-weight: 900; }
tbody tr.subgroup-row { background: #e5eaf2; font-weight: 800; }
tbody tr.detail-row { background: #fff9db; font-weight: 700; }
tbody tr.rmb-reference-row { background: #fff; font-weight: 400; }
tbody tr.total-row { background: #e9f1fb; font-weight: 900; }
tbody tr.negative td { color: #c11f2f; }

td.negative-cell,
td.negative-cell button {
  color: #c11f2f;
}

.amount {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.amount button {
  min-height: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 0;
  text-align: right;
  font: inherit;
  font-weight: 600;
}

.amount button:hover {
  background: transparent;
  color: var(--accent-2);
  text-decoration: underline;
}

.row-actions {
  display: flex;
  gap: 6px;
}

.row-actions button {
  min-height: 30px;
  padding: 4px 9px;
}

.check {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
}

.check input { width: auto; }

.pager {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: -2px 0 12px;
  color: var(--muted);
  font-weight: 800;
}

.pager button {
  min-height: 32px;
  padding: 5px 12px;
}

.report-period {
  flex-basis: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: end;
}

#reports .toolbar input[type="month"] {
  width: 136px;
  padding: 8px 10px;
}

.rate-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.rate-field input,
.rate-field select {
  width: 88px;
}

.report-title {
  color: #172234;
  font-size: 18px;
  font-weight: 900;
  margin: 2px 0 10px;
}

.report-wrap { max-height: calc(100vh - 160px); }

.report-wrap table {
  width: auto;
  font-size: 12px;
}

.report-wrap th,
.report-wrap td {
  padding: 3px 5px;
  line-height: 1.25;
}

.report-wrap th:first-child,
.report-wrap td:first-child {
  width: 190px;
  min-width: 190px;
  max-width: 190px;
}

.report-wrap th:not(:first-child),
.report-wrap td:not(:first-child) {
  width: 105px;
  min-width: 105px;
  max-width: 105px;
}

.report-wrap tbody tr:nth-child(even) { background: inherit; }
.report-wrap .amount { color: #17395e; font-weight: 500; }
.report-wrap .group-row td { background: #d6edf1; }
.report-wrap .subgroup-row td { background: #e5eaf2; }
.report-wrap .detail-row td { background: #fff9db; }
.report-wrap .rmb-reference-row td { background: #fff; }
.report-wrap .total-row td { background: #e9f1fb; }
.report-wrap .group-row td:first-child,
.report-wrap .total-row td:first-child { padding-left: 6px; }
.report-wrap .subgroup-row td:first-child { padding-left: 18px; }
.report-wrap .detail-row td:first-child { padding-left: 30px; }
.report-wrap .rmb-reference-row td:first-child { padding-left: 42px; }
.report-wrap .group-row .amount { font-size: 14px; }
.report-wrap .subgroup-row .amount { font-size: 13px; }
.report-wrap .detail-row .amount { font-size: 12px; }
.report-wrap .rmb-reference-row .amount {
  color: #4b5b6f;
  font-size: 12px;
  font-weight: 400;
}
.report-wrap .total-row .amount {
  font-size: 14px;
  font-weight: 800;
}

.report-name-col,
.carryover-name-col { width: 190px; }
.report-amount-col,
.carryover-amount-col { width: 105px; }

.carryover-wrap table {
  table-layout: fixed;
  width: auto;
  min-width: 780px;
}

.carryover-wrap th,
.carryover-wrap td {
  padding: 5px 6px;
}

.carryover-wrap th:first-child,
.carryover-wrap td:first-child {
  width: 190px;
  min-width: 190px;
  max-width: 190px;
}

.carryover-wrap th:not(:first-child),
.carryover-wrap td:not(:first-child) {
  width: 105px;
  min-width: 105px;
  max-width: 105px;
}

.carryover-overview-year span,
.carryover-overview-year small {
  display: block;
}

.carryover-overview-year small {
  margin-top: 2px;
  color: #dbe5f2;
  font-size: 11px;
  font-weight: 500;
}

.carryover-confirmed {
  background: #e8f2fb;
  color: #1f4d78;
}

#flowTable {
  table-layout: fixed;
  min-width: 1080px;
}

#flowTable td {
  overflow: hidden;
  text-overflow: ellipsis;
}

.flow-select-col { width: 54px; }
.flow-action-col { width: 116px; }
.flow-type-col { width: 88px; }
.flow-date-col { width: 112px; }
.flow-name-col { width: 170px; }
.flow-summary-col { width: 330px; }
.flow-amount-col { width: 140px; }

.chart-panel {
  margin-top: 14px;
  padding: 12px;
}

.chart-toolbar { margin-bottom: 10px; }
.chart-body { overflow-x: auto; }

.chart-body svg {
  display: block;
  width: 100%;
  min-width: 760px;
  height: 300px;
}

.chart-grid line { stroke: #d8e2ee; }

.chart-grid text,
.chart-month {
  fill: #4f6176;
  font-size: 12px;
}

.chart-legend {
  display: flex;
  gap: 14px;
  margin-bottom: 4px;
  color: #243245;
  font-size: 13px;
}

.chart-legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.chart-legend i {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
}

.report-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
}

.report-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.report-card h3 {
  margin: 0;
  background: var(--soft);
  border-bottom: 1px solid var(--line);
  padding: 10px 12px;
  font-size: 15px;
}

.report-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid #e1e6ee;
  padding: 8px 12px;
}

.report-row button {
  min-height: 0;
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 0;
  text-align: right;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1.35fr 1fr;
  gap: 18px;
}

.settings-grid h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.settings-wide,
.danger-box {
  grid-column: 1 / -1;
}

.fiscal-card {
  display: grid;
  grid-template-columns: 1fr 140px;
  align-items: end;
  gap: 14px;
  margin-bottom: 14px;
  padding: 16px;
}

.fiscal-card h2 { margin-bottom: 5px; }

.fiscal-card p {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.fiscal-card strong {
  color: #174a7d;
  font-size: 13px;
}

.fiscal-card label { color: #38495d; }

.danger-box {
  border-top: 1px solid #d9e0e8;
  padding-top: 16px;
}

.total {
  background: #e9f1fb;
  font-weight: 800;
}

.print-brand {
  color: #5f6f82;
  font-size: 12px;
  font-weight: 800;
}

.print-main-title {
  color: #172234;
  font-size: 20px;
  font-weight: 900;
  margin-top: 2px;
}

.print-meta {
  border-top: 1px solid #aeb8c5;
  color: #435266;
  font-size: 12px;
  font-weight: 700;
  margin-top: 8px;
  padding-top: 6px;
}

@media print {
  @page { size: A4 landscape; margin: 4mm; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .sidebar,
  header,
  .toolbar,
  .row-actions,
  .year-shortcuts,
  #manual,
  #import,
  #ledger,
  #flow,
  #export,
  #settings {
    display: none !important;
  }
  .year-shortcuts { display: none !important; }
  body { background: #fff; }
  main { margin: 0; }
  .view { display: none !important; padding: 0; }
  body:not([data-print-view]) .view.active,
  body[data-print-view="reports"] #reports,
  body[data-print-view="carryover"] #carryover { display: block !important; }
  body[data-print-view="reports"] #reports,
  body[data-print-view="carryover"] #carryover {
    width: max-content;
    margin: 0 auto;
    zoom: var(--print-scale, .72);
  }
  .panel {
    border: 0;
    box-shadow: none;
    padding: 0;
  }
  #reportStatus,
  #carryoverStatus { display: none; }
  .report-title {
    margin: 0 0 5px;
    text-align: center;
  }
  .print-brand { font-size: 14px; }
  .print-main-title { font-size: 24px; line-height: 1.15; }
  .print-meta {
    font-size: 13px;
    margin-top: 3px;
    padding-top: 3px;
    text-align: center;
  }
  .report-wrap,
  .carryover-wrap {
    max-height: none;
    overflow: visible;
    border: 0;
  }
  .report-wrap table,
  .carryover-wrap table {
    width: auto;
    font-size: 9px;
  }
  .report-wrap th,
  .report-wrap td,
  .carryover-wrap th,
  .carryover-wrap td {
    padding: 1px 3px;
  }
  .report-wrap th:first-child,
  .report-wrap td:first-child,
  .carryover-wrap th:first-child,
  .carryover-wrap td:first-child {
    width: 150px;
    min-width: 150px;
    max-width: 150px;
  }
  .report-wrap th:not(:first-child),
  .report-wrap td:not(:first-child),
  .carryover-wrap th:not(:first-child),
  .carryover-wrap td:not(:first-child) {
    width: 78px;
    min-width: 78px;
    max-width: 78px;
  }
}

@media (max-width: 900px) {
  .sidebar {
    position: static;
    width: auto;
    padding: 14px;
  }
  .brand {
    min-height: 0;
    margin-bottom: 12px;
  }
  .nav {
    display: inline-flex;
    width: auto;
    margin-right: 6px;
    margin-bottom: 6px;
  }
  main { margin-left: 0; }
  header {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
  }
  .view { padding: 14px; }
  .entry-row,
  .entry-row.one,
  .bulk-grid,
  .settings-grid,
  .fiscal-card {
    grid-template-columns: 1fr;
  }
  .wide { grid-column: auto; }
  .stats,
  .report-period {
    width: 100%;
  }
  .stats span { flex: 1; }
}
