/* 帳場 — 和帳簿パレット
   紙:  #F6F5F1  墨: #1C1C21  藍: #234E70  朱: #B5342C  灰: #8B8B92 */
:root {
  --paper: #F6F5F1;
  --card: #FFFFFF;
  --ink: #1C1C21;
  --ai: #234E70;
  --ai-weak: #E6EDF3;
  --shu: #B5342C;
  --shu-weak: #F7E9E7;
  --gray: #8B8B92;
  --line: #E2E1DA;
  --ok: #2E6B4F;
  --ok-weak: #E7F0EB;
  --mono: "SF Mono", ui-monospace, "Cascadia Mono", Menlo, monospace;
  --sans: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", "Yu Gothic", sans-serif;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font-family: var(--sans); font-size: 15px; line-height: 1.65;
}
a { color: var(--ai); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--ai); outline-offset: 2px; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; font-size: 19px; letter-spacing: .12em; color: var(--ink); }
.brand:hover { text-decoration: none; }
.brand-sub { font-size: 11px; font-weight: 400; letter-spacing: .05em; color: var(--gray); margin-left: 10px; }
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a { padding: 5px 11px; border-radius: 4px; color: var(--ink); font-size: 14px; }
.nav a:hover { background: var(--ai-weak); text-decoration: none; }
.nav a.on { background: var(--ai); color: #fff; }
.user { margin-left: auto; display: flex; gap: 12px; align-items: center; font-size: 13px; color: var(--gray); }
.user a { color: var(--gray); }

.page { max-width: 1060px; margin: 0 auto; padding: 22px 20px 80px; }

h1 { font-size: 21px; margin: 4px 0 18px; letter-spacing: .04em; }
h2 { font-size: 16px; margin: 26px 0 10px; letter-spacing: .04em; }
.muted { color: var(--gray); font-size: 13px; }

/* ---- flash ---- */
.flash { padding: 9px 14px; border-radius: 5px; margin: 0 0 14px; font-size: 14px; }
.flash-ok { background: var(--ok-weak); color: var(--ok); }
.flash-error { background: var(--shu-weak); color: var(--shu); }

/* ---- cards / stats ---- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; margin-bottom: 22px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 6px; padding: 14px 16px; }
.card .label { font-size: 12px; color: var(--gray); letter-spacing: .08em; }
.card .value { font-family: var(--mono); font-size: 26px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.card .value.bill { color: var(--ai); }
.card .value.alert { color: var(--shu); }
.card .sub { font-size: 12px; color: var(--gray); margin-top: 2px; }

/* ---- tables ---- */
.tablewrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: 6px; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--gray); font-weight: 600; letter-spacing: .06em; white-space: nowrap; background: #FBFBF8; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
td.nowrap { white-space: nowrap; }
tr.transfer td { color: var(--gray); background: #F2F1EB; }
tr.transfer .ed-view { opacity: .8; }
tfoot td { font-weight: 700; background: #FBFBF8; }

/* ---- stamps (状態印) ---- */
.stamp {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; border-radius: 50%;
  font-size: 12px; font-weight: 700; letter-spacing: 0;
  transform: rotate(-6deg); user-select: none; flex: none;
}
.stamp-settled { border: 2px solid var(--shu); color: var(--shu); background: transparent; }
.stamp-approved { border: 2px solid var(--ai); color: var(--ai); }
.stamp-unconfirmed { border: 2px dashed var(--gray); color: var(--gray); transform: none; }

/* ---- badges ---- */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; white-space: nowrap; }
.badge-half { background: var(--ai-weak); color: var(--ai); }
.badge-ken { background: #E7F0EB; color: #2E6B4F; }
.badge-wife { background: #F3EAF4; color: #7A4B85; }
.badge-custom { background: #FDF3E3; color: #9A6A1B; }
.badge-transfer { background: #EEEEEA; color: var(--gray); text-decoration: line-through; }
.badge-unclassified { background: var(--shu-weak); color: var(--shu); }
.badge-src { background: transparent; border: 1px solid var(--line); color: var(--gray); }

/* ---- forms ---- */
form.stack { display: grid; gap: 12px; max-width: 460px; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--gray); }
input[type=text], input[type=password], input[type=number], input[type=date],
input[type=month], input[type=file], select, textarea {
  font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 5px;
  background: var(--card); color: var(--ink); width: 100%;
}
input[type=number] { font-family: var(--mono); }
textarea { resize: vertical; }
.btn {
  display: inline-block; font: inherit; font-weight: 600; cursor: pointer;
  padding: 8px 18px; border-radius: 5px; border: 1px solid var(--ai);
  background: var(--ai); color: #fff;
}
.btn:hover { filter: brightness(1.1); }
.btn-ghost { background: transparent; color: var(--ai); }
.btn-danger { background: transparent; border-color: var(--shu); color: var(--shu); }
.btn-sm { padding: 3px 10px; font-size: 13px; font-weight: 500; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* filter bar */
.filterbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: end; margin-bottom: 14px; }
.filterbar label.field { min-width: 110px; flex: 0 1 auto; }
.filterbar input, .filterbar select { width: auto; min-width: 110px; }

/* confirm rows */
.confirm-row td { border-bottom: 1px solid var(--line); }
.confirm-row .inline-controls { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.confirm-row select, .confirm-row input[type=text] { width: auto; padding: 4px 8px; font-size: 13px; }
.rulebox { font-size: 12px; color: var(--gray); display: flex; gap: 6px; align-items: center; margin-top: 4px; }
.rulebox input[type=text] { min-width: 140px; }

/* settlement */
.ledger { background: var(--card); border: 1px solid var(--line); border-radius: 6px; max-width: 560px; }
.ledger .row { display: flex; justify-content: space-between; padding: 10px 18px; border-bottom: 1px solid var(--line); }
.ledger .row:last-child { border-bottom: 0; }
.ledger .row .k { color: var(--gray); font-size: 14px; }
.ledger .row .v { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.ledger .row.total { background: var(--ai-weak); font-weight: 700; }
.ledger .row.total .k { color: var(--ai); }
.ledger .row.total .v { color: var(--ai); font-size: 19px; }
.summary-text { width: 100%; max-width: 560px; font-family: var(--mono); font-size: 13px; margin-top: 10px; }

/* bars (analysis) */
.bar-row { display: grid; grid-template-columns: 110px 1fr 90px; gap: 10px; align-items: center; padding: 5px 0; }
.bar-row .name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: #ECEBE5; border-radius: 3px; height: 16px; }
.bar-fill { background: var(--ai); height: 100%; border-radius: 3px; min-width: 2px; }
.bar-row .amt { font-family: var(--mono); font-size: 13px; text-align: right; font-variant-numeric: tabular-nums; }
.trend { display: flex; gap: 6px; align-items: flex-end; height: 140px; padding: 8px 0 0; }
.trend .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 0; }
.trend .tbar { width: 70%; background: var(--ai); border-radius: 3px 3px 0 0; min-height: 2px; }
.trend .tlabel { font-size: 10px; color: var(--gray); white-space: nowrap; }

/* login */
.auth { max-width: 380px; margin: 60px auto; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 28px; }
.auth h1 { text-align: center; letter-spacing: .3em; margin-bottom: 20px; }

@media (max-width: 640px) {
  body { font-size: 14px; }
  .topbar { padding: 8px 12px; gap: 10px; }
  .brand-sub { display: none; }
  .user span { display: none; }
  .page { padding: 14px 12px 60px; }
  th, td { padding: 7px 8px; }
  .bar-row { grid-template-columns: 80px 1fr 78px; }
  .hide-sm { display: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .flash { animation: slidein .18s ease-out; }
  @keyframes slidein { from { opacity: 0; transform: translateY(-4px); } }
}

/* ---- tag toggle (按分タグ選択) ---- */
.tagset { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.tagset input { position: absolute; opacity: 0; width: 0; height: 0; }
.tagset label {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; line-height: 1.55; white-space: nowrap; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: #AAAAB0;
  user-select: none; transition: none;
}
.tagset label:hover { border-color: var(--gray); color: var(--ink); }
.tagset input:checked + label { font-weight: 700; }
.tagset input:checked + label.tag-half { background: var(--ai-weak); color: var(--ai); border-color: #BCD0E0; }
.tagset input:checked + label.tag-ken { background: #E7F0EB; color: var(--ok); border-color: #C3DCCF; }
.tagset input:checked + label.tag-wife { background: #F3EAF4; color: #7A4B85; border-color: #DFCBE3; }
.tagset input:checked + label.tag-transfer { background: #EEEEEA; color: var(--gray); border-color: #D7D7CE; text-decoration: line-through; }
.tagset input:focus-visible + label { outline: 2px solid var(--ai); outline-offset: 1px; }
td.cell-split { white-space: nowrap; }
td.cell-cat select { padding: 3px 8px; font-size: 12.5px; min-width: 116px; }

/* 保存トースト */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 7px 18px; border-radius: 999px;
  font-size: 13px; opacity: 0; pointer-events: none; z-index: 50;
  transition: opacity .18s ease-out;
}
.toast.on { opacity: .92; }
.toast.err { background: var(--shu); }

/* 精算ステータス */
.paystamp { display: inline-flex; align-items: center; justify-content: center; padding: 4px 12px; border-radius: 999px; font-size: 13px; font-weight: 700; }
.paystamp-paid { border: 2px solid var(--shu); color: var(--shu); transform: rotate(-3deg); }
.paystamp-unpaid { border: 2px dashed var(--gray); color: var(--gray); }

/* 設定: ルール行内編集(1060pxに収める) */
.rule-inline th, .rule-inline td { padding: 6px 8px; }
.rule-inline input, .rule-inline select { padding: 3px 6px; font-size: 12.5px; width: 100%; }
.rule-inline .w-pri { width: 50px; }
.rule-inline .w-amt { width: 68px; }
.rule-inline td.range { white-space: nowrap; }
.rule-inline td.range input { display: inline-block; width: 68px; }
.rule-inline .btn-sm { padding: 3px 9px; font-size: 12.5px; }

/* ---- 取引一覧: 行内編集をコンパクトに(1行に収める) ---- */
.page-wide { max-width: 1460px; }
.txn-table th, .txn-table td { padding: 4px 6px; white-space: nowrap; }
.txn-table .stamp { width: 25px; height: 25px; font-size: 11px; }
.txn-table input, .txn-table select {
  padding: 2px 6px; font-size: 12.5px; border-radius: 4px; width: auto;
}
.txn-table input.i-date { width: 122px; font-family: var(--mono); }
.txn-table input.i-desc { width: 152px; }
.txn-table input.i-amt { width: 86px; text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.txn-table input.i-memo { width: 104px; }
.txn-table select.i-payer { width: 68px; }
.txn-table select.i-cat { width: 100px; }
.txn-table select.i-src { width: 90px; }
.txn-table .tagset { flex-wrap: nowrap; }
.txn-table .actions-cell { display: flex; gap: 4px; }
.txn-table .actions-cell .btn { padding: 3px 8px; font-size: 12px; }
.txn-table tr.rulerow td { background: #FBFBF8; white-space: normal; }
.txn-table tr.rulerow .rulebox { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding: 2px 0; }
.txn-table tr.rulerow input[type=text] { width: 240px; }

.btn-rule { background: transparent; border-color: var(--ok); color: var(--ok); }
.btn-rule:hover { background: var(--ok-weak); filter: none; }
.btn-rule.on { background: var(--ok); color: #fff; border-color: var(--ok); }

/* 入力フォームの按分タグ */
.tagpick { display: inline-flex; gap: 5px; flex-wrap: wrap; }
.tagpick input { position: absolute; opacity: 0; width: 0; height: 0; }
.tagpick label {
  display: inline-block; padding: 3px 12px; border-radius: 999px; font-size: 13px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: #AAAAB0; user-select: none;
}
.tagpick label:hover { border-color: var(--gray); color: var(--ink); }
.tagpick input:checked + label { font-weight: 700; }
.tagpick input:checked + label.t-unclassified { background: #F1F0EA; color: #5A5A60; border-color: #D7D7CE; }
.tagpick input:checked + label.t-half { background: var(--ai-weak); color: var(--ai); border-color: #BCD0E0; }
.tagpick input:checked + label.t-ken { background: #E7F0EB; color: var(--ok); border-color: #C3DCCF; }
.tagpick input:checked + label.t-wife { background: #F3EAF4; color: #7A4B85; border-color: #DFCBE3; }
.tagpick input:checked + label.t-transfer { background: #EEEEEA; color: var(--gray); border-color: #D7D7CE; text-decoration: line-through; }
.tagpick input:focus-visible + label { outline: 2px solid var(--ai); outline-offset: 1px; }

/* ---- クリックで編集に変わるセル ---- */
.txn-table td.ed { cursor: pointer; }
.txn-table td.ed .ed-view {
  display: inline-block; min-width: 22px; min-height: 19px; padding: 1px 5px;
  border-radius: 4px; border: 1px solid transparent;
}
.txn-table td.ed:hover .ed-view { background: #EEEDE6; border-color: #DEDDD4; }
.txn-table td.locked { color: var(--gray); }
.txn-table td.locked .ed-view { padding: 1px 5px; }
.txn-table td.editing { padding: 2px 4px; }
.txn-table td.saving .ed-view { opacity: .5; }

/* ルール反映で更新された行のハイライト */
.txn-table tr.applied td { background: #EAF1F7; }
.txn-table tr.applied td:first-child { box-shadow: inset 3px 0 0 var(--ai); }
.applied-mark { color: var(--ai); font-weight: 700; margin-left: 3px; font-size: 12px; }

/* ---- 分析: 積み上げ棒 ---- */
.stack-chart { display: flex; gap: 8px; align-items: flex-end; height: 250px; padding: 6px 0; }
.stack-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; min-width: 0; gap: 3px; }
.stack-total { font-size: 10px; color: var(--gray); font-family: var(--mono); }
.stack-bar { width: 76%; display: flex; flex-direction: column-reverse; border-radius: 3px 3px 0 0; overflow: hidden; min-height: 2px; }
.stack-seg { min-height: 1px; }
.stack-label { font-size: 10px; color: var(--gray); white-space: nowrap; }

.legend { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 10px; font-size: 12px; color: var(--gray); }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.legend-dot { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* ---- 分析: 発散型の増減バー ---- */
.diverge-row { display: grid; grid-template-columns: 92px 1fr 96px 190px; gap: 10px; align-items: center; padding: 4px 0; }
.diverge-row .name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.diverge-track { position: relative; height: 16px; background: #F1F0EA; border-radius: 3px; }
.diverge-axis { position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: #CFCEC5; }
.diverge-bar { position: absolute; top: 2px; bottom: 2px; border-radius: 2px; }
.diverge-bar.up { background: var(--shu); }
.diverge-bar.down { background: var(--ok); }
.diverge-row .amt { font-family: var(--mono); font-size: 13px; text-align: right; font-variant-numeric: tabular-nums; }
.diverge-row .amt.up { color: var(--shu); }
.diverge-row .amt.down { color: var(--ok); }
.diverge-row .sub-amt { font-size: 11px; white-space: nowrap; }

/* ---- 分析: 折れ線 ---- */
.linechart { width: 100%; height: 200px; display: block; }
.axis-labels { display: flex; justify-content: space-between; font-size: 10px; color: var(--gray); margin-top: 2px; }
.axis-labels span { flex: 1; text-align: center; }

.tag-fixed { display: inline-block; margin-left: 5px; padding: 0 4px; border-radius: 3px;
  background: var(--ai-weak); color: var(--ai); font-size: 10px; vertical-align: middle; }
.split-track { display: flex; overflow: hidden; }
.split-track .bar-fill { border-radius: 0; }

@media (max-width: 640px) {
  .diverge-row { grid-template-columns: 74px 1fr 84px; }
  .diverge-row .sub-amt { display: none; }
  .stack-chart { height: 200px; }
}

.flash-demo { background: #FDF3E3; color: #7A5312; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
