:root {
  --bg: #ebe4d6;
  --bg-deep: #e3d9c8;
  --card: #fffdf9;
  --ink: #1c1915;
  --muted: #6d6256;
  --line: #e7dccb;
  --line-strong: #d9cbb6;
  --green: #146b45;
  --green-bg: #e5f4ec;
  --red: #9d3030;
  --red-bg: #f8ece8;
  --amber: #8a5a1e;
  --amber-bg: #f8f0e2;
  --gold: #a67c3d;
  --cream: #f6efe4;
  --shadow: 0 1px 2px rgba(28, 25, 21, 0.04), 0 16px 40px rgba(70, 42, 12, 0.06);
  --radius: 18px;
  color: var(--ink);
  font-family: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(880px 380px at 8% -8%, rgba(255, 252, 246, 0.9), transparent 58%),
    radial-gradient(640px 280px at 100% 0%, rgba(166, 124, 61, 0.12), transparent 50%),
    linear-gradient(180deg, #f4eee4 0%, var(--bg) 42%, var(--bg-deep) 100%);
  color: var(--ink);
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
a { color: var(--green); }
::selection { background: #ead9b4; color: var(--ink); }

.boot {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--muted);
}

#app {
  padding:
    max(18px, env(safe-area-inset-top))
    max(18px, env(safe-area-inset-right))
    calc(40px + env(safe-area-inset-bottom))
    max(18px, env(safe-area-inset-left));
}

.topbar, .banner, main, footer {
  width: min(1120px, 100%);
  margin-inline: auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 22px;
  align-items: stretch;
  position: sticky;
  top: 0;
  z-index: 4;
  padding: 6px 0 14px;
  background: #f3ece2;
  border-bottom: 1px solid transparent;
}

.brand, .bcv-card { display: flex; gap: 14px; }
.brand { align-items: flex-start; }
.mark {
  width: 46px;
  height: 46px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 14px;
  background:
    linear-gradient(160deg, #3a322a 0%, var(--ink) 55%);
  color: var(--cream);
  font-weight: 700;
  letter-spacing: -0.04em;
  box-shadow: inset 0 -8px 14px rgba(166, 124, 61, 0.28);
}
.eyebrow {
  margin: 0;
  color: var(--gold);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.16em;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 32px; line-height: 1.1; letter-spacing: -0.03em; }
h2 { font-size: 20px; letter-spacing: -0.02em; }
.lede { max-width: 460px; margin-top: 6px; color: var(--muted); }

.bcv-card {
  min-width: min(440px, 100%);
  justify-content: space-between;
  align-items: flex-end;
  padding: 16px 18px;
  background:
    linear-gradient(180deg, #fffdf9 0%, #fbf6ee 100%);
  border: 1px solid rgba(70, 42, 12, 0.08);
  border-top: 3px solid var(--gold);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.bcv-rate {
  font-family: Georgia, "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  font-size: clamp(28px, 3vw, 36px);
  line-height: 1.05;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
}
.bcv-unit, .bcv-meta, .help, .scope, footer { color: var(--muted); }
.bcv-meta { text-align: right; font-size: 13px; }
.bcv-meta p { margin-top: 2px; }
.bcv-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.bcv-actions a { font-weight: 650; }
.bcv-actions button, .ghost, .text-btn, .primary, .danger {
  border-radius: 999px;
  border: 1px solid transparent;
  padding: 8px 14px;
  min-height: 40px;
}
.bcv-actions button, .ghost {
  background: #fff;
  border-color: var(--line-strong);
}
.ghost:hover, .bcv-actions button:hover, .text-btn:hover { border-color: #c9b79a; }
.primary { background: var(--ink); color: var(--cream); border-color: var(--ink); }
.primary:hover { background: #2c261f; }
.danger { background: var(--red); color: white; }
.text-btn { background: transparent; color: var(--green); padding-inline: 8px; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid #1f7a52;
  outline-offset: 2px;
}
button:disabled { opacity: 0.6; cursor: default; }

.banner {
  margin-bottom: 14px;
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--red-bg);
  color: var(--red);
}

.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}
.kpi {
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-radius: 16px;
  padding: 14px 16px;
  border: 1px solid rgba(70, 42, 12, 0.06);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset;
}
.kpi span, .kpi em { display: block; color: var(--muted); font-style: normal; font-size: 13px; }
.kpi em { margin-top: auto; }
.kpi strong, .fit {
  display: block;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  font-variant-numeric: tabular-nums lining-nums;
}
.kpi strong {
  margin: 6px 0 8px;
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: clamp(22px, 2vw, 28px);
  font-weight: 500;
  letter-spacing: -0.03em;
  line-height: 1.15;
}
.kpi.pos { background: linear-gradient(180deg, #f7fbf8, var(--card)); }
.kpi.neg { background: linear-gradient(180deg, #fdf8f6, var(--card)); }
.kpi.pos strong { color: var(--green); }
.kpi.neg strong { color: var(--red); }
.scope { margin: 12px 2px 18px; font-size: 13px; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(300px, 0.85fr);
  gap: 16px;
  align-items: start;
}
.card {
  background: var(--card);
  border: 1px solid rgba(70, 42, 12, 0.08);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.form, .receipt, .ledger, .months { padding: 18px; }
.form-head, .ledger-head, .field-label, .form-actions, .month-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}
.form-head { margin-bottom: 14px; }

label, .field { display: block; margin-top: 14px; font-size: 13px; font-weight: 650; }
input, select {
  display: block;
  width: 100%;
  margin-top: 6px;
  min-height: 46px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: #fff;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
input:focus, select:focus {
  outline: 2px solid #1f7a52;
  outline-offset: 1px;
  border-color: transparent;
}
input[readonly] { background: #f6f0e6; color: var(--ink); }
.grid-2 { display: grid; grid-template-columns: minmax(150px, 190px) minmax(0, 1fr); gap: 12px; }
.grid-2 label { margin-top: 0; }
.help { margin-top: 6px; font-size: 12px; font-weight: 500; }
.seg {
  display: inline-flex;
  flex-wrap: wrap;
  padding: 3px;
  gap: 2px;
  background: #f3ece3;
  border-radius: 999px;
}
.seg button {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 12px;
  min-height: 36px;
  font-size: 13px;
  font-weight: 650;
}
.seg button.is-on { background: white; color: var(--ink); box-shadow: 0 1px 2px rgba(50, 30, 10, 0.1); }
.sell-field { padding-top: 4px; }
.backcalc {
  margin-top: 12px;
  padding: 12px;
  border-radius: 14px;
  background: #fbf6ef;
  border: 1px solid var(--line);
}
.backcalc summary { cursor: pointer; font-size: 13px; font-weight: 650; }
.backcalc .ghost { margin-top: 10px; }
.form-error { margin-top: 12px; color: var(--red); font-weight: 650; }
.form-actions { margin-top: 16px; }
.primary, .danger, .ghost { font-weight: 650; }
.primary { flex: 1; padding: 12px 16px; min-height: 48px; }

.receipt { position: sticky; top: 108px; }
.result {
  display: grid;
  grid-template-columns: 1.3fr 0.7fr;
  gap: 10px;
  padding: 16px;
  border-radius: 16px;
  background: var(--amber-bg);
}
.result.pos { background: var(--green-bg); }
.result.neg { background: var(--red-bg); }
.result span, .result em { color: var(--muted); font-style: normal; font-size: 13px; }
.result strong {
  display: block;
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: clamp(26px, 3vw, 32px);
  font-weight: 500;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
  overflow-wrap: anywhere;
}
.result.pos strong { color: var(--green); }
.result.neg strong { color: var(--red); }
.slip { list-style: none; margin: 14px 0 0; padding: 0; }
.slip li, .month-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-top: 1px dashed var(--line);
  font-variant-numeric: tabular-nums;
}
.slip li span { color: var(--muted); }
.slip li b { text-align: right; font-variant-numeric: tabular-nums lining-nums; }
.slip .total { font-weight: 750; }
.formula { margin-top: 12px; color: var(--muted); font-size: 12px; }
.warn { margin-top: 8px; color: var(--amber); font-size: 13px; }

.ledger, .months { margin-top: 16px; }
.ledger-head { align-items: flex-end; margin-bottom: 8px; }
.toolbar { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; align-items: center; }
.toolbar input { width: 180px; margin-top: 0; padding: 8px 12px; min-height: 40px; }
.sort-field { display: none; margin: 0; }
.toolbar .sort-field { font-size: 13px; }
.entry-cards { display: none; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 920px; }
th {
  padding: 4px 8px 8px;
  text-align: left;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
th button {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  width: 100%;
  min-height: 36px;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 700;
  letter-spacing: 0;
}
th button:hover, th button.is-sort { color: var(--ink); }
.sort-mark {
  display: inline-block;
  width: 0.8em;
  text-align: center;
  color: var(--gold);
  font-size: 11px;
}
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums lining-nums; white-space: nowrap; }
th.num button { justify-content: flex-end; }
td { padding: 11px 8px; border-top: 1px solid var(--line); vertical-align: top; }
.sub { display: block; color: var(--muted); font-size: 12px; font-weight: 500; max-width: 220px; }
td:first-child .sub { white-space: nowrap; max-width: none; }
.pill {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--amber-bg);
  color: var(--amber);
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.pill.sold { background: var(--green-bg); color: var(--green); }
.pill.part { background: #e7eef8; color: #1d4f91; }
.wallet {
  width: min(1120px, 100%);
  margin: 0 auto 14px;
  padding: 16px 18px;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-end;
}
.wallet-balance {
  margin-top: 4px;
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: clamp(32px, 6vw, 44px);
  line-height: 1.05;
  letter-spacing: -0.04em;
}
.wallet-balance span { font-size: 0.42em; letter-spacing: 0; color: var(--muted); }
.wallet .primary { flex: none; min-width: 112px; }
.sale-list { margin-top: 8px; }
.sale-list .sub { display: block; max-width: 100%; margin-top: 4px; overflow-wrap: anywhere; }
.sale-list button, td .sub button {
  margin-left: 8px;
  border: 0;
  background: transparent;
  color: var(--red);
  font-weight: 650;
  padding: 0;
}
.pos { color: var(--green); }
.neg { color: var(--red); }
.row-actions { display: flex; gap: 10px; justify-content: flex-end; white-space: nowrap; }
.row-actions button, .linkish {
  border: 0;
  background: transparent;
  color: var(--green);
  padding: 4px 0;
  min-height: 32px;
  font-weight: 650;
}
.row-actions .danger-text, .card-actions .danger-text { color: var(--red); }
.empty { padding: 28px 8px; color: var(--muted); }

.entry-card {
  padding: 14px 0;
  border-top: 1px solid var(--line);
}
.entry-card:first-child { border-top: 0; padding-top: 4px; }
.entry-card header { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.entry-card header strong { display: block; font-size: 16px; }
.entry-profit { margin-top: 10px; }
.entry-profit b {
  font-family: Georgia, "Iowan Old Style", Palatino, serif;
  font-size: 28px;
  font-weight: 500;
  letter-spacing: -0.03em;
  line-height: 1.1;
}
.entry-profit span { color: var(--muted); font-size: 13px; }
.entry-card dl {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  margin: 12px 0 0;
}
.entry-card dl > div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  min-width: 0;
  overflow: hidden;
}
.entry-card dt { flex: none; color: var(--muted); font-size: 12px; }
.entry-card dd {
  flex: 1;
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-align: right;
  font-variant-numeric: tabular-nums lining-nums;
  font-weight: 650;
}
.card-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(92px, 1fr));
  gap: 8px;
  margin-top: 12px;
}
.card-actions button {
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: #fff;
  color: var(--ink);
  font-weight: 650;
}

.month-row { border-top-style: solid; }
.month-row b { flex: 1; min-width: 0; font-variant-numeric: tabular-nums; text-align: right; }
.months h2 { margin-bottom: 4px; }

footer { margin-top: 18px; font-size: 13px; }
footer p + p { margin-top: 4px; }
code { font-family: Consolas, "Cascadia Mono", monospace; }

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: end center;
  background: rgba(28, 25, 21, 0.46);
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 20;
}
.modal-card {
  width: min(420px, 100%);
  background: var(--card);
  border-radius: 20px 20px 16px 16px;
  padding: 18px;
  box-shadow: var(--shadow);
}
.modal-card p { margin-top: 8px; color: var(--muted); }
.modal-card .form-actions { margin-top: 16px; }
.modal-card .danger, .modal-card .ghost { flex: 1; min-height: 48px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: max(22px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  max-width: min(420px, calc(100% - 32px));
  background: var(--ink);
  color: var(--cream);
  padding: 12px 16px;
  border-radius: 999px;
  text-align: center;
}
.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding:
    max(24px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
}
.login-card {
  width: min(420px, 100%);
  padding: 28px 24px 24px;
  border-top: 3px solid var(--gold);
}
.login-card h1 { margin-top: 4px; }
.login-card .lede { margin: 8px 0 4px; }
.login-card .primary { width: 100%; margin-top: 18px; }

@media (min-width: 901px) {
  .topbar { box-shadow: 0 10px 0 #f3ece2; }
}

@media (max-width: 900px) {
  #app { padding-inline: max(14px, env(safe-area-inset-left)); padding-right: max(14px, env(safe-area-inset-right)); }
  .topbar { position: static; display: block; padding-bottom: 4px; background: transparent; }
  .wallet { flex-direction: column; align-items: stretch; }
  .wallet .primary { width: 100%; }
  .lede { font-size: 14px; }
  h1 { font-size: 28px; }
  .topbar .bcv-card {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    width: 100%;
    min-width: 0;
    margin-top: 14px;
  }
  .topbar .bcv-main,
  .topbar .bcv-meta { width: 100%; min-width: 0; }
  .bcv-meta { text-align: left; margin-top: 0; }
  .bcv-actions {
    justify-content: flex-start;
    width: 100%;
    flex-wrap: wrap;
  }
  .bcv-actions button, .bcv-actions a {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    flex: 0 0 auto;
  }
  .kpis { grid-template-columns: 1fr 1fr; gap: 8px; }
  .kpi { padding: 12px; overflow: hidden; }
  .result { grid-template-columns: 1fr; }
  .kpi strong { font-size: clamp(18px, 5.2vw, 24px); }
  .workspace, .ledger-head { display: block; }
  .receipt { position: static; margin-top: 14px; }
  .grid-2 { grid-template-columns: 1fr; }
  .field-label { align-items: stretch; flex-direction: column; }
  .seg { display: flex; width: 100%; }
  .seg button { flex: 1 1 0; min-height: 44px; padding-inline: 6px; }
  input, select { font-size: 16px; }
  .form-actions { align-items: stretch; }
  .form-actions .ghost { min-height: 48px; }
  .toolbar { justify-content: stretch; }
  .sort-field { display: block; width: 100%; }
  .toolbar input, .toolbar .ghost, .sort-field select { width: 100%; }
  .toolbar .ghost { min-height: 44px; }
  .table-wrap { display: none; }
  .entry-cards { display: block; }
  .month-row { align-items: flex-start; flex-direction: column; gap: 2px; }
  .month-row b { text-align: left; }
  footer { font-size: 12px; }
}

@media (max-width: 340px) {
  h1 { font-size: 26px; }
}
