/* 債権管理 — 帳簿をイメージした落ち着いた配色 */
:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --surface-2: #faf9f6;
  --ink: #1c2230;
  --muted: #5f6675;
  --line: #e3dfd6;
  --line-strong: #cfc9bc;
  --navy: #1f2f52;
  --navy-2: #2c4170;
  --navy-soft: #e7ebf4;
  --gold: #c9a24f;
  --red: #b0372d;
  --red-soft: #fbeceb;
  --amber: #93650a;
  --amber-soft: #fcf1d8;
  --green: #2a7149;
  --green-soft: #e4f2ea;
  --gray: #676b74;
  --gray-soft: #ecebe8;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(28, 34, 48, 0.06), 0 6px 20px rgba(28, 34, 48, 0.05);
  color: var(--ink);
  background: var(--bg);
  font-family: "Noto Sans JP", "Hiragino Sans", "Yu Gothic UI", Meiryo, system-ui, sans-serif;
  line-height: 1.6;
  font-synthesis: none;
}

* { box-sizing: border-box; }
html { min-width: 320px; }
body { margin: 0; min-height: 100vh; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.eyebrow { font-size: 0.72rem; letter-spacing: 0.18em; color: var(--gold); font-weight: 700; }

:focus-visible { outline: 3px solid rgba(44, 65, 112, 0.45); outline-offset: 2px; }

/* ---------- ボタン ---------- */
.button {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4em;
  min-height: 40px; padding: 0 16px; border-radius: 9px;
  border: 1px solid var(--line-strong); background: var(--surface);
  font-weight: 600; cursor: pointer; white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}
.button:hover { background: var(--surface-2); border-color: var(--muted); }
.button.primary { background: var(--navy); border-color: var(--navy); color: #fff; }
.button.primary:hover { background: var(--navy-2); }
.button.danger { color: var(--red); border-color: #e3b9b4; }
.button.danger:hover { background: var(--red-soft); }
.button.ghost { background: transparent; }
.button.ghost-light { background: transparent; color: #fff; border-color: rgba(255,255,255,0.35); }
.button.ghost-light:hover { background: rgba(255,255,255,0.1); }
.button.small { min-height: 32px; padding: 0 12px; font-size: 0.85rem; }
.button.wide { width: 100%; }
.button:disabled { opacity: 0.55; cursor: not-allowed; }
.icon-button {
  width: 36px; height: 36px; border: 0; border-radius: 8px; background: transparent;
  font-size: 1.4rem; line-height: 1; cursor: pointer; color: var(--muted);
}
.icon-button:hover { background: var(--gray-soft); }
.link-button {
  border: 0; background: none; padding: 0; color: var(--navy-2); font-weight: 600;
  cursor: pointer; text-align: left; text-decoration: underline; text-underline-offset: 3px;
}

/* ---------- ログイン ---------- */
.login-view {
  min-height: 100vh; display: grid; place-items: center; padding: 24px 16px;
  background:
    linear-gradient(160deg, rgba(31, 47, 82, 0.92), rgba(31, 47, 82, 0.78)),
    repeating-linear-gradient(0deg, transparent 0 31px, rgba(255,255,255,0.05) 31px 32px),
    var(--navy);
}
.login-card {
  width: min(420px, 100%); background: var(--surface); border-radius: 16px;
  padding: 32px 28px; display: grid; gap: 16px; box-shadow: 0 24px 60px rgba(0,0,0,0.25);
  border-top: 4px solid var(--gold);
}
.login-card h1 { font-size: 1.8rem; letter-spacing: 0.04em; }

/* ---------- ヘッダー・タブ ---------- */
.app-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 24px; background: var(--navy); color: #fff;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 38px; height: 38px; border-radius: 9px; display: grid; place-items: center;
  background: rgba(255,255,255,0.08); border: 1px solid var(--gold); color: var(--gold);
  font-weight: 700; font-size: 1.15rem;
}
.brand strong { display: block; font-size: 1.05rem; letter-spacing: 0.06em; }
.brand div span { display: block; font-size: 0.66rem; letter-spacing: 0.2em; color: rgba(255,255,255,0.6); }
.session { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.session-name { font-weight: 600; }
.role-badge {
  font-size: 0.74rem; padding: 2px 10px; border-radius: 999px;
  background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.25);
}
.role-badge.admin { border-color: var(--gold); color: var(--gold); }

.tabs {
  display: flex; gap: 4px; padding: 0 24px; background: var(--surface);
  border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none;
}
.tab {
  border: 0; background: none; padding: 14px 14px 12px; cursor: pointer; white-space: nowrap;
  color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent;
}
.tab:hover { color: var(--ink); }
.tab.active { color: var(--navy); border-bottom-color: var(--navy); }

.app-main { max-width: 1200px; margin: 0 auto; padding: 24px; }
.panel { display: grid; gap: 18px; }
/* 表の幅でグリッドが横に広がらないようにする */
.panel > *, .dialog-body > * { min-width: 0; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 1.45rem; }

/* ---------- カード・指標 ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 16px 18px 4px; flex-wrap: wrap; }
.card-head h2 { font-size: 1.02rem; }

.metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 1100px) { .metrics { grid-template-columns: repeat(6, minmax(0, 1fr)); } }
.metric {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); display: grid; gap: 2px;
}
.metric .label { font-size: 0.8rem; color: var(--muted); font-weight: 600; }
.metric .value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: 0.01em; }
.metric .sub { font-size: 0.76rem; color: var(--muted); }
.metric.hero { background: var(--navy); border-color: var(--navy); color: #fff; }
.metric.hero .label, .metric.hero .sub { color: rgba(255,255,255,0.7); }
.metric.hero .value { color: #fff; }
.positive { color: var(--green); }
.negative { color: var(--red); }

.status-strip { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.status-tile {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  cursor: pointer; font-weight: 600; text-align: left;
}
.status-tile:hover { border-color: var(--line-strong); background: var(--surface-2); }
.status-tile strong { font-size: 1.2rem; font-variant-numeric: tabular-nums; }

/* ---------- ステータスバッジ ---------- */
.badge {
  display: inline-block; padding: 1px 10px; border-radius: 999px; font-size: 0.78rem;
  font-weight: 700; white-space: nowrap; border: 1px solid transparent;
}
.badge.unpaid { background: var(--red-soft); color: var(--red); border-color: #efc9c4; }
.badge.partial { background: var(--amber-soft); color: var(--amber); border-color: #eedaa6; }
.badge.paid { background: var(--green-soft); color: var(--green); border-color: #bfdfcb; }
.badge.written_off { background: var(--gray-soft); color: var(--gray); border-color: #d9d7d2; }
.badge.role { background: var(--navy-soft); color: var(--navy); }
.badge.inactive { background: var(--gray-soft); color: var(--gray); }

/* ---------- ツールバー ---------- */
.toolbar { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.search {
  flex: 1 1 260px; min-height: 40px; padding: 0 14px; border-radius: 9px;
  border: 1px solid var(--line-strong); background: var(--surface);
}
.segmented { display: flex; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--line-strong); border-radius: 9px; padding: 3px; gap: 2px; }
.segmented button {
  border: 0; background: none; padding: 6px 12px; border-radius: 7px; cursor: pointer;
  font-weight: 600; color: var(--muted); font-size: 0.88rem;
}
.segmented button.active { background: var(--navy); color: #fff; }

/* ---------- 表 ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { padding: 10px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { font-size: 0.76rem; color: var(--muted); font-weight: 700; letter-spacing: 0.04em; background: var(--surface-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: #f7f5f0; }
td.name { font-weight: 600; }
td .sub { display: block; font-size: 0.78rem; color: var(--muted); font-weight: 400; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .button + .button { margin-left: 6px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
tfoot td { font-weight: 700; background: var(--surface-2); border-top: 1px solid var(--line-strong); }

/* ---------- フォーム ---------- */
.field { display: grid; gap: 6px; }
.field > span { font-size: 0.84rem; font-weight: 700; color: var(--ink); }
.field em { font-style: normal; font-size: 0.7rem; color: var(--red); margin-left: 4px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; min-height: 40px; padding: 8px 12px; border: 1px solid var(--line-strong);
  border-radius: 9px; background: var(--surface);
}
.field textarea { resize: vertical; }
.field.checkbox { display: flex; align-items: center; gap: 10px; align-self: end; min-height: 40px; }
.field.checkbox input { width: 18px; height: 18px; min-height: 0; }
.hint { color: var(--muted); font-size: 0.76rem; }
.form-error { color: var(--red); background: var(--red-soft); border-radius: 8px; padding: 8px 12px; font-size: 0.88rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.span-2 { grid-column: 1 / -1; }
.inline-form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; padding: 16px 18px; }
.inline-form .field { flex: 1 1 200px; }
.preview {
  align-self: end; min-height: 40px; display: flex; flex-direction: column; justify-content: center;
  font-size: 0.82rem; color: var(--muted); background: var(--surface-2); border: 1px dashed var(--line-strong);
  border-radius: 9px; padding: 6px 12px;
}
.preview strong { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- ダイアログ ---------- */
.dialog {
  width: min(600px, calc(100vw - 32px)); max-height: calc(100vh - 48px); padding: 0;
  border: 0; border-radius: 14px; box-shadow: 0 30px 80px rgba(0,0,0,0.3); color: var(--ink);
}
.dialog.wide { width: min(820px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(20, 26, 40, 0.5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { font-size: 1.1rem; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.dialog-body { padding: 18px 20px; display: grid; gap: 14px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px; border-top: 1px solid var(--line); background: var(--surface-2); flex-wrap: wrap; }
.dialog-foot .spacer { flex: 1; }

.detail-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.detail-grid .metric { box-shadow: none; padding: 10px 12px; }
.detail-grid .metric .value { font-size: 1.1rem; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 0.9rem; }
.facts dt { color: var(--muted); font-weight: 600; }
.facts dd { margin: 0; white-space: pre-wrap; word-break: break-word; }
.progress-row { display: grid; gap: 6px; }
.progress-row .labels { display: flex; justify-content: space-between; font-size: 0.8rem; color: var(--muted); }
progress { width: 100%; height: 10px; appearance: none; border: 0; border-radius: 999px; overflow: hidden; background: var(--gray-soft); }
progress::-webkit-progress-bar { background: var(--gray-soft); border-radius: 999px; }
progress::-webkit-progress-value { background: var(--green); border-radius: 999px; }
progress::-moz-progress-bar { background: var(--green); border-radius: 999px; }
.section-title { font-size: 0.95rem; font-weight: 700; }
.payment-form { display: grid; grid-template-columns: 1.2fr 1fr 1.6fr auto; gap: 10px; align-items: end; padding: 14px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; }
.amount-with-button { display: flex; gap: 6px; }
.amount-with-button input { flex: 1; min-width: 0; }
.notice { padding: 10px 14px; border-radius: 9px; font-size: 0.88rem; background: var(--gray-soft); color: var(--gray); }
.notice.success { background: var(--green-soft); color: var(--green); }
.warning-box { padding: 10px 14px; border-radius: 9px; background: var(--amber-soft); color: var(--amber); font-size: 0.88rem; }
.key-box { display: flex; gap: 10px; align-items: center; padding: 12px; background: var(--surface-2); border: 1px solid var(--line-strong); border-radius: 9px; }
.key-box code { flex: 1; word-break: break-all; font-size: 0.9rem; }

.diff { display: grid; gap: 2px; font-size: 0.82rem; }
.diff .from { color: var(--muted); text-decoration: line-through; }
.diff .to { color: var(--ink); font-weight: 600; }

/* ---------- トースト ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25); font-weight: 600; max-width: calc(100vw - 32px);
}
.toast.error { background: var(--red); }

/* ---------- スマートフォン ---------- */
@media (max-width: 720px) {
  .app-header { padding: 10px 16px; }
  .brand div span { display: none; }
  .tabs { padding: 0 8px; }
  .app-main { padding: 16px; }
  .form-grid { grid-template-columns: 1fr; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .status-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric { padding: 12px; }
  .metric .value { overflow-wrap: anywhere; }
  .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .payment-form { grid-template-columns: 1fr; }
  .metric .value { font-size: 1.15rem; }
  th, td { padding: 9px 10px; }
  .session-name { display: none; }
}

@media print {
  .app-header, .tabs, .toolbar, .button, .segmented { display: none !important; }
  body, .card, .metric { background: #fff; box-shadow: none; }
}
