:root {
  --bg: #f7f7f5;
  --card: #fff;
  --text: #222;
  --muted: #6e6e6e;
  --line: #e4e4e0;
  --primary: #222;
  --primary-ink: #fff;
  --accent: #8a7a52;
  --danger: #c0392b;
  --warn: #b7791f;
  --ok: #2f855a;
  --radius: 4px;
  font-family: "Noto Sans JP", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic UI", Meiryo, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.6; }
a { color: var(--primary); }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 20px 0 8px; }

/* ヘッダー */
.topbar { background: #fff; color: var(--text); display: flex; align-items: center; gap: 8px 24px; padding: 8px 24px 0; position: sticky; top: 0; z-index: 10; flex-wrap: wrap; border-bottom: 1px solid var(--line); box-shadow: 0 1px 4px rgba(0,0,0,.04); }
.brand { display: flex; align-items: center; gap: 14px; text-decoration: none; color: inherit; }
.brand img { height: 44px; width: auto; display: block; }
.brand-text { font-weight: 700; letter-spacing: .08em; border-left: 1px solid var(--line); padding-left: 14px; }
.userbox { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
.nav { display: flex; gap: 2px; overflow-x: auto; flex-basis: 100%; }
.nav a { color: var(--muted); text-decoration: none; padding: 10px 14px; white-space: nowrap; border-bottom: 2px solid transparent; letter-spacing: .04em; }
.nav a:hover { color: var(--text); }
.nav a.active { color: var(--text); font-weight: 700; border-bottom-color: var(--text); }
@media (max-width: 600px) { .topbar { padding: 8px 16px 0; } .brand img { height: 34px; } .brand-text { font-size: 13px; } }

/* フッター */
.site-footer { border-top: 1px solid var(--line); background: #fff; text-align: center; padding: 24px 16px; font-size: 13px; color: var(--muted); line-height: 1.8; }
.site-footer .foot-name { font-weight: 700; color: var(--text); font-size: 15px; letter-spacing: .05em; }

/* ログイン */
body.auth-mode .topbar, body.auth-mode .site-footer { display: none; }
.auth-wrap { min-height: calc(100vh - 88px); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; }
.auth-card { background: #fff; border: 1px solid var(--line); border-top: 3px solid var(--text); width: min(400px, 100%); padding: 32px; box-shadow: 0 6px 24px rgba(0,0,0,.06); }
.auth-logo { display: block; width: 200px; max-width: 70%; margin: 0 auto 8px; }
.auth-title { text-align: center; font-weight: 700; letter-spacing: .1em; padding-bottom: 16px; margin-bottom: 20px; border-bottom: 1px solid var(--line); }
.auth-card h2 { text-align: center; font-size: 15px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-card .small { font-size: 12px; }
.auth-error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 13px; }
.auth-info { background: #f2f2ef; padding: 8px 12px; font-size: 13px; }
.auth-foot { color: var(--muted); font-size: 12px; letter-spacing: .05em; }
.btn.block { width: 100%; justify-content: center; padding: 10px; }
.btn:disabled { opacity: .6; cursor: wait; }

.logo-edit { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.logo-edit img { height: 56px; max-width: 200px; object-fit: contain; border: 1px solid var(--line); padding: 4px; background: #fff; }

main { max-width: 1200px; margin: 0 auto; padding: 24px 16px 64px; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions.right { justify-content: flex-end; margin-top: 16px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid2 > .card { margin: 0; min-width: 0; overflow-x: auto; }
@media (max-width: 900px) { .grid2 { grid-template-columns: 1fr; } }

.notice { background: #fbf8ef; border: 1px solid #e3d6b0; padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ボタン */
.btn { display: inline-flex; align-items: center; gap: 4px; padding: 8px 14px; border: 1px solid #cfd6dd; background: #fff; color: var(--text); border-radius: 6px; cursor: pointer; font: inherit; text-decoration: none; white-space: nowrap; }
.btn:hover { background: #f0f3f6; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { background: #444; border-color: #444; }
.btn.danger { color: var(--danger); border-color: #e8b4ae; }
.btn.sm { padding: 3px 10px; font-size: 13px; }
.icon { border: none; background: none; cursor: pointer; color: var(--muted); padding: 2px 4px; font-size: 11px; }
.icon:hover { color: var(--text); }
.icon.remove:hover { color: var(--danger); }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.kpi-label { color: var(--muted); font-size: 12px; }
.kpi-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi-value small { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 6px; }
.kpi.warn .kpi-value { color: var(--warn); }
.kpi.danger .kpi-value { color: var(--danger); }

/* グラフ */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 220px; padding-top: 18px; }
.bar-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; min-width: 0; }
.bar { width: 70%; background: #3a3a3a; border-radius: 3px 3px 0 0; min-height: 1px; }
.bar-val { font-size: 10px; color: var(--muted); white-space: nowrap; }
.bar-label { font-size: 11px; color: var(--muted); margin-top: 4px; }

/* 一覧テーブル */
table.list { width: 100%; border-collapse: collapse; }
table.list th, table.list td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; }
table.list th { font-size: 12px; color: var(--muted); font-weight: 600; background: #fafbfc; }
table.list th.num, table.list td.num { text-align: right; }
table.list tfoot th, table.list tfoot td { font-weight: 700; border-top: 2px solid var(--line); }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f6f5f0; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn + .btn { margin-left: 4px; }
.card > table.list { margin: -8px 0; }
.card:has(> table.list) { overflow-x: auto; }

.filters { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.filters input { flex: 1; min-width: 200px; }

/* バッジ */
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; white-space: nowrap; }
.q-draft { background: #edf0f3; color: #4a5562; }
.q-sent { background: #e8edf5; color: #2c4a7a; }
.q-accepted, .i-paid { background: #e3f4ea; color: var(--ok); }
.q-lost { background: #f3e8e8; color: #8a4a4a; }
.i-unpaid { background: #fff3dc; color: var(--warn); }
.overdue { background: #fde2df; color: var(--danger); font-weight: 600; }

/* フォーム */
input, select, textarea { font: inherit; padding: 7px 9px; border: 1px solid #cfd6dd; border-radius: 6px; background: #fff; color: inherit; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid #c9c2ad; border-color: var(--primary); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field.span2 { grid-column: span 2; }
@media (max-width: 600px) { .field.span2 { grid-column: auto; } }
.inline { display: flex; gap: 6px; }
.inline select { flex: 1; }
.filters select, .page-head select { width: auto; }

/* 明細 */
.lines-wrap { overflow-x: auto; }
table.lines { width: 100%; border-collapse: collapse; min-width: 760px; }
table.lines th { font-size: 12px; color: var(--muted); text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--line); }
table.lines td { padding: 4px; border-bottom: 1px solid #f0f2f4; }
table.lines input, table.lines select { padding: 5px 7px; }
table.lines td.drag, table.lines th.drag { width: 28px; padding: 0; }
.drag .icon { display: block; line-height: 1; }
.w-qty { width: 80px; } .w-unit { width: 70px; } .w-price { width: 120px; } .w-rate { width: 90px; } .w-amt { width: 120px; }
table.lines td.amt { font-weight: 600; padding-right: 8px; }
#addLine { margin-top: 8px; }

.editor-bottom { display: grid; grid-template-columns: 1fr 320px; gap: 24px; margin-top: 20px; }
@media (max-width: 800px) { .editor-bottom { grid-template-columns: 1fr; } }
.totals table { width: 100%; border-collapse: collapse; }
.totals th { text-align: left; font-weight: 400; color: var(--muted); padding: 4px 0; }
.totals td { text-align: right; font-variant-numeric: tabular-nums; padding: 4px 0; }
.totals tr.sub th, .totals tr.sub td { font-size: 12px; }
.totals tr.line th, .totals tr.line td { border-top: 1px solid var(--line); padding-top: 8px; }
.totals tr.grand th, .totals tr.grand td { font-size: 20px; font-weight: 700; color: var(--text); border-top: 2px solid var(--text); padding-top: 8px; }

/* ダイアログ */
dialog { border: none; border-radius: 10px; padding: 0; width: min(640px, calc(100vw - 32px)); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(15, 23, 42, .45); }
.dialog-form { padding: 24px; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: #1f2933; color: #fff; padding: 10px 20px; border-radius: 6px; opacity: 0; transition: .2s; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== 印刷用帳票（A4） ===== */
.print-toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.sheet { background: #fff; width: 210mm; min-height: 297mm; margin: 0 auto; padding: 16mm 15mm; box-shadow: 0 2px 12px rgba(0,0,0,.12); color: #000; font-size: 10pt; }
@media (max-width: 860px) { .sheet { width: 100%; min-height: 0; padding: 20px; } }
.doc-title { text-align: center; font-size: 22pt; font-weight: 700; letter-spacing: .3em; border-bottom: 3px double #000; padding-bottom: 4mm; margin-bottom: 4mm; }
.doc-meta { text-align: right; font-size: 9pt; margin-bottom: 6mm; }
.doc-head { display: flex; justify-content: space-between; gap: 10mm; margin-bottom: 8mm; }
.to { flex: 1.3; }
.to-name { font-size: 15pt; font-weight: 700; border-bottom: 1px solid #000; padding-bottom: 1mm; display: inline-block; min-width: 80%; }
.lead { margin: 4mm 0 2mm; }
.subject { margin-bottom: 3mm; }
.amount-box { display: flex; align-items: baseline; gap: 4mm; border-bottom: 2px solid #000; padding: 2mm 0; }
.amount-box strong { font-size: 20pt; }
.amount-box .incl { font-size: 9pt; }
.due { margin-top: 2mm; font-size: 9.5pt; }
.from { flex: 1; font-size: 9pt; line-height: 1.7; padding-top: 2mm; }
.from-logo { display: block; height: 18mm; width: auto; max-width: 60mm; object-fit: contain; margin-bottom: 2mm; }
.from-name { font-size: 12pt; font-weight: 700; margin-bottom: 1mm; }
.regno { margin-top: 1mm; font-weight: 600; }
table.doc-lines { width: 100%; border-collapse: collapse; margin-bottom: 5mm; }
table.doc-lines th { background: #eee; border: 1px solid #555; padding: 1.8mm 2mm; font-weight: 600; font-size: 9pt; text-align: center; }
table.doc-lines td { border: 1px solid #999; padding: 1.8mm 2mm; }
table.doc-lines th:first-child { width: 50%; }
table.doc-lines tr.blank td { height: 7.5mm; }
.doc-bottom { display: flex; gap: 8mm; justify-content: space-between; align-items: flex-start; }
.doc-notes { flex: 1; font-size: 9pt; }
.doc-notes .small { font-size: 8.5pt; margin-bottom: 2mm; }
.doc-notes .label { font-weight: 700; border-bottom: 1px solid #999; margin-bottom: 1mm; }
.bank, .remarks { margin-bottom: 4mm; }
table.doc-totals { border-collapse: collapse; min-width: 70mm; }
table.doc-totals th, table.doc-totals td { border: 1px solid #999; padding: 1.5mm 3mm; font-size: 9pt; }
table.doc-totals th { background: #f4f4f4; text-align: left; font-weight: 400; }
table.doc-totals td { text-align: right; font-variant-numeric: tabular-nums; }
table.doc-totals tr.sum th, table.doc-totals tr.sum td { border-top: 2px solid #000; }
table.doc-totals tr.grand th, table.doc-totals tr.grand td { font-weight: 700; font-size: 11pt; background: #e8e8e8; }

@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .no-print { display: none !important; }
  main { padding: 0; max-width: none; }
  .sheet { box-shadow: none; margin: 0; width: 210mm; min-height: 0; }
  table.doc-lines th, table.doc-totals th, table.doc-totals tr.grand td { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

fieldset.plain { border: 0; margin: 0; padding: 0; min-width: 0; }
.btn.is-busy { opacity: .6; pointer-events: none; }
