:root {
  --bg: #f7f9f8;
  --surface: #ffffff;
  --surface-soft: #eef5f2;
  --text: #17211f;
  --muted: #697774;
  --line: #d6dfdc;
  --line-strong: #879691;
  --teal: #176b63;
  --teal-dark: #0d4741;
  --gold: #b8862e;
  --danger: #b9423f;
  --shadow: 0 18px 45px rgba(25, 43, 39, 0.08);
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button,
input {
  font: inherit;
}

.app-shell {
  width: min(1440px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 28px 0 48px;
}

.app-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 18px;
}

h1,
h2,
p,
dl {
  margin: 0;
}

h1 {
  font-size: 30px;
  line-height: 1.2;
  font-weight: 800;
}

h2 {
  font-size: 18px;
  line-height: 1.25;
  font-weight: 800;
}

.app-header p {
  margin-top: 8px;
  color: var(--muted);
  font-size: 15px;
}

.header-actions,
.panel-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.button,
.icon-button {
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 40px;
  padding: 0 15px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: 0.15s ease;
}

.button:hover,
.icon-button:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}

.button.primary {
  border-color: var(--teal);
  background: var(--teal);
  color: #fff;
  font-weight: 700;
}

.button.secondary {
  color: var(--teal-dark);
}

.icon-button {
  width: 40px;
  padding: 0;
  color: #fff;
  background: var(--teal);
  border-color: var(--teal);
  font-size: 24px;
  line-height: 1;
}

.setup-panel,
.entry-panel,
.summary-panel,
.preview-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.setup-panel {
  display: grid;
  grid-template-columns: repeat(4, minmax(130px, 1fr)) minmax(320px, 2fr);
  gap: 14px;
  padding: 16px;
  margin-bottom: 18px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 8px 10px;
  color: var(--text);
  background: #fff;
}

input:focus {
  outline: 3px solid rgba(23, 107, 99, 0.16);
  border-color: var(--teal);
}

.input-with-action {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 112px;
  gap: 8px;
}

.mini-button {
  min-height: 40px;
  border: 1px solid rgba(23, 107, 99, 0.28);
  border-radius: 7px;
  padding: 0 10px;
  color: var(--teal-dark);
  background: var(--surface-soft);
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}

.mini-button:hover {
  border-color: var(--teal);
  background: #e3f0ec;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 18px;
  align-items: start;
}

.entry-panel,
.summary-panel,
.preview-panel {
  padding: 16px;
}

.panel-title-row {
  justify-content: space-between;
  margin-bottom: 12px;
}

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

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  color: var(--teal-dark);
  background: var(--surface-soft);
  font-size: 13px;
  text-align: left;
  border: 1px solid var(--line);
  padding: 9px 8px;
  white-space: nowrap;
}

td {
  border: 1px solid var(--line);
  padding: 5px;
}

.entry-table input {
  min-height: 34px;
  border: 0;
  border-radius: 5px;
  padding: 5px 6px;
  background: transparent;
}

.entry-table input:focus {
  background: #fff;
}

.entry-table th:nth-child(1) {
  width: 120px;
}

.entry-table th:nth-child(2) {
  width: 135px;
}

.entry-table th:nth-child(4),
.entry-table th:nth-child(5) {
  width: 95px;
}

.entry-table th:nth-child(6) {
  width: 120px;
}

.entry-table th:nth-child(7) {
  width: 44px;
}

.delete-row {
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 6px;
  color: var(--danger);
  background: #fff2f1;
  cursor: pointer;
}

.category-dialog {
  width: min(560px, calc(100vw - 28px));
  border: 0;
  border-radius: 10px;
  padding: 0;
  box-shadow: 0 30px 80px rgba(18, 33, 30, 0.24);
}

.category-dialog::backdrop {
  background: rgba(18, 33, 30, 0.28);
}

.dialog-card {
  display: grid;
  gap: 14px;
  padding: 18px;
  background: var(--surface);
}

.dialog-title-row,
.category-add-row,
.category-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.dialog-title-row {
  justify-content: space-between;
}

.category-add-row input,
.category-row input {
  flex: 1;
}

.category-manager-list {
  display: grid;
  gap: 8px;
  max-height: min(56vh, 520px);
  overflow: auto;
  padding-right: 4px;
}

.category-row {
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfb;
}

.metrics {
  display: grid;
  gap: 10px;
  margin: 14px 0;
}

.metrics div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  background: var(--surface-soft);
  border-radius: 8px;
}

.metrics dt {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.metrics dd {
  font-size: 19px;
  font-weight: 800;
  color: var(--teal-dark);
}

.ledger-list {
  display: grid;
  gap: 8px;
}

.ledger-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--line);
  padding: 8px 0;
  font-size: 14px;
}

.ledger-item strong {
  color: var(--gold);
}

.preview-panel {
  margin-top: 18px;
}

#autosaveStatus {
  color: var(--muted);
  font-size: 13px;
}

.report-paper {
  overflow-x: auto;
  background: #fbfbfa;
  border: 1px solid var(--line);
  padding: 18px;
}

.report-table {
  min-width: 820px;
  background: #fff;
  border: 2px solid #222;
}

.report-table caption {
  caption-side: top;
  padding: 0 0 8px;
  font-size: 20px;
  font-weight: 800;
  text-align: left;
}

.report-table th,
.report-table td {
  border: 1px solid #222;
  padding: 7px 8px;
  color: #111;
  background: #fff;
  font-size: 14px;
}

.report-table th {
  text-align: center;
  font-weight: 800;
}

.report-table .number {
  text-align: right;
}

.report-table .total-row td {
  font-weight: 800;
}

.report-gap td {
  height: 18px;
  border-left-color: transparent;
  border-right-color: transparent;
}

@media (max-width: 980px) {
  .app-shell {
    width: min(100vw - 20px, 760px);
    padding-top: 18px;
  }

  .app-header,
  .workspace {
    grid-template-columns: 1fr;
    display: grid;
  }

  .header-actions {
    flex-wrap: wrap;
  }

  .setup-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .wide-field {
    grid-column: 1 / -1;
  }
}

@media (max-width: 560px) {
  .setup-panel {
    grid-template-columns: 1fr;
  }

  h1 {
    font-size: 24px;
  }

  .button {
    flex: 1;
  }
}
