:root {
  --bg: #f7f7f4;
  --ink: #171717;
  --muted: #666;
  --grid: #111;
  --header: #eef4e5;
  --editable: #e9e9e9;
  --readonly: #fff;
  --button-top: #f7f7f7;
  --button-bottom: #c9c9c9;
  --good: #0a7a55;
  --bad: #b42318;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  color: var(--ink);
  font-family: "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", sans-serif;
  font-size: 16px;
  letter-spacing: 0;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.page {
  width: min(1540px, 100%);
  margin: 0 auto;
  padding: 10px 10px 28px;
}

.page-head {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.date-line {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 0.95rem;
}

h1 {
  margin: 0;
  font-size: 1.2rem;
  line-height: 1.2;
}

.legend {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.86rem;
  white-space: nowrap;
}

.legend-swatch {
  width: 18px;
  height: 18px;
  border: 1px solid var(--grid);
  background: var(--editable);
}

.sheet-layout {
  display: grid;
  grid-template-columns: 210px minmax(560px, 1fr) 470px;
  grid-template-areas:
    "account holdings rebalance"
    "summary status orders"
    ". status orders"
    "tools tools tools";
  gap: 48px 72px;
  align-items: start;
}

.sheet-block {
  min-width: 0;
}

.area-account {
  grid-area: account;
}

.area-holdings {
  grid-area: holdings;
}

.area-rebalance {
  grid-area: rebalance;
}

.area-summary {
  grid-area: summary;
}

.area-status {
  grid-area: status;
}

.area-orders {
  grid-area: orders;
}

.sheet-title {
  border: 1px solid var(--grid);
  border-bottom: 0;
  background: var(--header);
  padding: 2px 6px;
  text-align: center;
  font-size: 1rem;
  line-height: 1.3;
}

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--readonly);
}

th,
td {
  height: 26px;
  border: 0;
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  padding: 2px 6px;
  text-align: center;
  vertical-align: middle;
  line-height: 1.25;
  white-space: nowrap;
}

tr > :first-child {
  border-left: 1px solid var(--grid);
}

thead tr:first-child > *,
tbody tr:first-child > * {
  border-top: 1px solid var(--grid);
}

th {
  background: var(--readonly);
  font-weight: 400;
}

.holdings-table th:first-child,
.holdings-table td:first-child,
.rebalance-table th:first-child,
.rebalance-table td:first-child,
.status-table th:first-child,
.status-table td:first-child,
.order-table th:first-child,
.order-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 2;
  box-shadow: none;
}

.holdings-table th:first-child,
.rebalance-table th:first-child,
.status-table th:first-child,
.order-table th:first-child {
  z-index: 3;
  background: var(--readonly);
}

.holdings-table td:first-child {
  background: var(--editable);
}

.rebalance-table td:first-child,
.status-table td:first-child,
.order-table td:first-child {
  background: var(--readonly);
}

td.label {
  text-align: center;
}

td.name-cell,
th.name-cell {
  min-width: 220px;
}

.number-cell {
  text-align: right;
}

.editable-cell {
  background: var(--editable);
}

.result-cell {
  background: var(--readonly);
}

.cell-input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  text-align: inherit;
  line-height: 1.2;
}

.cell-input[type="number"] {
  text-align: right;
}

.cell-input:focus {
  box-shadow: inset 0 0 0 2px #2563eb;
  background: #f6fbff;
}

.account-table td:first-child {
  width: 92px;
}

.account-table td:last-child {
  width: 120px;
}

.summary-table td:first-child {
  width: 98px;
}

.summary-table td:last-child {
  width: 112px;
}

.holdings-table th:nth-child(2),
.holdings-table td:nth-child(2),
.holdings-table th:nth-child(3),
.holdings-table td:nth-child(3),
.holdings-table th:nth-child(5),
.holdings-table td:nth-child(5),
.holdings-table th:nth-child(6),
.holdings-table td:nth-child(6) {
  min-width: 82px;
}

.holdings-table th:nth-child(4),
.holdings-table td:nth-child(4) {
  min-width: 96px;
}

.holdings-table th:nth-child(7),
.holdings-table td:nth-child(7) {
  min-width: 70px;
}

.rebalance-table th:nth-child(2),
.rebalance-table td:nth-child(2),
.rebalance-table th:nth-child(3),
.rebalance-table td:nth-child(3),
.rebalance-table th:nth-child(4),
.rebalance-table td:nth-child(4),
.order-table th:nth-child(2),
.order-table td:nth-child(2),
.order-table th:nth-child(3),
.order-table td:nth-child(3),
.order-table th:nth-child(4),
.order-table td:nth-child(4) {
  min-width: 92px;
}

.status-table th,
.status-table td {
  min-width: 80px;
}

.status-table .name-cell {
  min-width: 220px;
}

.action-row {
  display: flex;
  justify-content: end;
  gap: 6px;
  margin-bottom: 2px;
}

.excel-button {
  min-width: 84px;
  min-height: 27px;
  border: 1px solid #9f9f9f;
  border-radius: 2px;
  background: linear-gradient(var(--button-top), var(--button-bottom));
  color: #111;
  box-shadow: inset 1px 1px 0 #fff, inset -1px -1px 0 #777;
}

.excel-button:active {
  background: linear-gradient(var(--button-bottom), var(--button-top));
}

.action-cell {
  text-align: center;
}

.row-button {
  min-width: 48px;
  min-height: 23px;
  border: 1px solid #9f9f9f;
  border-radius: 2px;
  background: linear-gradient(var(--button-top), var(--button-bottom));
  color: #111;
  box-shadow: inset 1px 1px 0 #fff, inset -1px -1px 0 #777;
}

.positive {
  color: var(--ink);
}

.negative {
  color: var(--ink);
}

.total-label {
  text-align: center;
}

.remaining-row td {
  font-weight: 700;
}

.notice {
  grid-column: 1 / -1;
  border: 1px solid #b42318;
  background: #fff4f2;
  padding: 8px 10px;
}

.notice h2 {
  margin: 0 0 4px;
  font-size: 1rem;
}

.notice ul {
  margin: 0;
  padding-left: 20px;
}

.data-tools {
  grid-area: tools;
  border: 1px solid #c9c9c9;
  background: #fff;
  padding: 8px;
}

.data-tools summary {
  cursor: pointer;
  color: var(--muted);
}

.config-row {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr);
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}

.config-row label {
  color: var(--muted);
}

.config-input {
  width: 100%;
  min-width: 0;
  border: 1px solid #aaa;
  background: var(--editable);
  padding: 5px 7px;
}

.data-tools textarea {
  width: 100%;
  min-height: 150px;
  margin-top: 8px;
  border: 1px solid #aaa;
  padding: 8px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.86rem;
}

.file-input {
  display: none;
}

.tools-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

@media (max-width: 1180px) {
  .sheet-layout {
    grid-template-columns: 1fr;
    grid-template-areas: none;
    gap: 18px;
  }

  .area-account,
  .area-holdings,
  .area-rebalance,
  .area-summary,
  .area-status,
  .area-orders {
    grid-area: auto;
  }

  .area-summary {
    order: 1;
  }

  .area-status {
    order: 2;
  }

  .area-account {
    order: 3;
  }

  .area-holdings {
    order: 4;
  }

  .area-rebalance {
    order: 5;
  }

  .area-orders {
    order: 6;
  }

  .data-tools {
    order: 7;
    grid-area: auto;
  }

  .sheet-block {
    width: 100%;
  }
}

@media (max-width: 720px) {
  .page {
    padding: 8px 6px 24px;
  }

  .page-head {
    position: sticky;
    top: 0;
    z-index: 5;
    align-items: center;
    padding: 6px 0;
    background: var(--bg);
  }

  h1 {
    font-size: 1.05rem;
  }

  body {
    font-size: 15px;
  }

  th,
  td {
    height: 30px;
    padding: 3px 5px;
  }

  .sheet-title {
    font-size: 0.96rem;
  }
}
