:root {
    --ink: #1c1c1e;
    --ink-soft: #4a4d52;
    --muted: #6b6f76;
    --line: #e2e3e6;
    --line-strong: #c9ccd1;
    --bg: #f5f5f3;
    --surface: #ffffff;
    --surface-soft: #f3f4f6;
    --danger: #b3261e;
    --danger-soft: #fdecea;
    --ok: #1d6b3f;
    --ok-soft: #e7f4ec;
    --info: #1f4f8f;
    --info-soft: #e8f0fb;
    --warn: #8a5a00;
    --warn-soft: #fbf1dc;
    --radius: 8px;
}

* { box-sizing: border-box; }

html { font-size: 15px; }

body {
    margin: 0;
    font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "BIZ UDPGothic", "Yu Gothic UI", "Meiryo", sans-serif;
    line-height: 1.6;
    color: var(--ink);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}

a { color: inherit; }

h1 {
    margin: 0;
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: 0.02em;
}

/* ---- 上部バー ---- */

.topbar {
    display: flex;
    align-items: center;
    gap: 28px;
    padding: 0 24px;
    height: 56px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand {
    font-weight: 700;
    font-size: 1.05rem;
    letter-spacing: 0.04em;
    text-decoration: none;
}
.brand span {
    font-weight: 400;
    color: var(--muted);
    margin-left: 2px;
}

.topbar nav {
    display: flex;
    gap: 4px;
    height: 100%;
}
.topbar nav a {
    display: flex;
    align-items: center;
    padding: 0 12px;
    text-decoration: none;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}
.topbar nav a:hover { color: var(--ink); }
.topbar nav a.active {
    color: var(--ink);
    border-bottom-color: var(--ink);
    font-weight: 600;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-left: auto;
}
.topbar-actions form { margin: 0; }

/* ---- レイアウト ---- */

.container {
    max-width: 1240px;
    margin: 0 auto;
    padding: 28px 24px 64px;
}
.container-wide { max-width: 1480px; }
.container-narrow { max-width: 680px; }

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

.back {
    display: inline-block;
    margin-bottom: 4px;
    font-size: 0.85rem;
    color: var(--muted);
    text-decoration: none;
}
.back::before { content: "← "; }
.back:hover { color: var(--ink); }

.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 20px;
}

.section-title {
    margin: 24px 0 10px;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    color: var(--ink-soft);
}
.card > .section-title:first-child { margin-top: 0; }

/* ---- ボタン ---- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 36px;
    padding: 0 14px;
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 0.9rem;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
}
.btn:hover { background: var(--surface-soft); }
.btn:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
    outline: 2px solid var(--info);
    outline-offset: 1px;
}

.btn-primary {
    background: var(--ink);
    border-color: var(--ink);
    color: #fff;
    font-weight: 600;
}
.btn-primary:hover { background: #000; }

.btn-quiet {
    border-color: transparent;
    background: transparent;
    color: var(--muted);
}
.btn-quiet:hover { color: var(--ink); }
.btn-quiet[aria-disabled="true"] { opacity: 0.4; pointer-events: none; }

.btn-danger {
    border-color: #e5b5b1;
    color: var(--danger);
}
.btn-danger:hover { background: var(--danger-soft); }

.btn-block { width: 100%; }
.btn-small { height: 28px; padding: 0 8px; font-size: 0.8rem; }

/* ---- フォーム ---- */

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}
.field > span {
    font-size: 0.8rem;
    color: var(--ink-soft);
}
.field small {
    margin-left: 6px;
    font-size: 0.75rem;
    color: var(--muted);
}
.field-link {
    margin-left: 8px;
    font-size: 0.75rem;
    color: var(--info);
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="search"],
input[type="number"],
input[type="date"],
select,
textarea {
    width: 100%;
    height: 36px;
    padding: 0 10px;
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    background: var(--surface);
    color: var(--ink);
    font: inherit;
    font-size: 0.93rem;
}
textarea {
    height: auto;
    min-height: 36px;
    padding: 7px 10px;
    line-height: 1.5;
    resize: vertical;
}
input[type="number"] { text-align: right; }

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px 16px;
}
.field-span { grid-column: 1 / -1; }

.form-actions {
    display: flex;
    gap: 8px;
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid var(--line);
}

.check {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
    color: var(--ink-soft);
    cursor: pointer;
}

/* ---- お知らせ ---- */

.flash {
    margin-bottom: 16px;
    padding: 10px 14px;
    border-radius: 6px;
    font-size: 0.9rem;
}
.flash ul { margin: 0; padding-left: 18px; }
.flash-ok { background: var(--ok-soft); color: var(--ok); }
.flash-error { background: var(--danger-soft); color: var(--danger); }

/* ---- ログイン ---- */

.login {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 24px;
}
.login .flash { width: 100%; max-width: 360px; }
.login-card {
    width: 100%;
    max-width: 360px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 28px;
}
.login-card h1 { margin-bottom: 4px; }

/* ---- 一覧 ---- */

.stat {
    display: flex;
    align-items: baseline;
    gap: 10px;
}
.stat-label { font-size: 0.8rem; color: var(--muted); }
.stat-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 0.8rem; color: var(--muted); }

.filters {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 12px 14px;
    margin-bottom: 16px;
    padding: 16px 20px;
}
.filters .field { width: 150px; }
.filters .field-grow { flex: 1 1 200px; width: auto; }
.filters .field:has(.range) { width: 300px; }
.range input { flex: 1; min-width: 0; }
.range {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--muted);
}
.filters-actions { display: flex; gap: 4px; }

.table-wrap { padding: 0; overflow-x: auto; }

.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.93rem;
}
.table th {
    padding: 10px 16px;
    text-align: left;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--muted);
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}
.table td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #fafafa; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .subject { color: var(--ink-soft); min-width: 180px; }
.table .partner { white-space: nowrap; }
.nowrap { white-space: nowrap; font-variant-numeric: tabular-nums; }

.row-link { font-weight: 600; text-decoration: none; white-space: nowrap; }
.row-link:hover { text-decoration: underline; }
.doc-type {
    display: inline-block;
    margin-right: 6px;
    font-size: 0.75rem;
    font-weight: 400;
    color: var(--muted);
}

.overdue { color: var(--danger); }
.overdue-note { margin-left: 4px; font-size: 0.75rem; }

.badge {
    display: inline-block;
    padding: 1px 9px;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    white-space: nowrap;
    vertical-align: middle;
}
.badge-draft { background: var(--surface-soft); color: var(--ink-soft); }
.badge-issued { background: var(--info-soft); color: var(--info); }
.badge-sent { background: var(--warn-soft); color: var(--warn); }
.badge-paid { background: var(--ok-soft); color: var(--ok); }
.badge-cancelled { background: var(--surface-soft); color: var(--muted); text-decoration: line-through; }
h1 .badge { margin-left: 8px; }

.empty {
    padding: 48px 20px;
    text-align: center;
    color: var(--muted);
}

.pager {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    margin-top: 16px;
    font-size: 0.85rem;
    color: var(--muted);
}

/* ---- 編集画面 ---- */

.editor {
    display: grid;
    grid-template-columns: minmax(480px, 1fr) minmax(420px, 1fr);
    gap: 20px;
    align-items: start;
}

.lines {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 12px;
}
.line {
    display: grid;
    grid-template-columns: 76px 64px 1fr 116px;
    gap: 8px 10px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: #fcfcfb;
}
.line-description { grid-column: 1 / -1; }
.line-options {
    grid-column: 1 / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.field-notes { margin-top: 20px; }

.editor-preview,
.detail-main {
    position: sticky;
    top: 76px;
    height: calc(100vh - 96px);
    display: flex;
    flex-direction: column;
}
.preview-head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.12em;
    color: var(--ink-soft);
}
.preview-state { font-weight: 400; letter-spacing: 0; color: var(--muted); }
.preview-frame {
    flex: 1;
    width: 100%;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}
.preview-message {
    padding: 16px 20px;
    margin-bottom: 8px;
    border: 1px dashed var(--line-strong);
    border-radius: var(--radius);
    color: var(--muted);
    font-size: 0.9rem;
}
.preview-message p { margin: 0; }
[hidden] { display: none !important; }

/* ---- 詳細画面 ---- */

.detail {
    display: grid;
    grid-template-columns: 340px 1fr;
    gap: 20px;
    align-items: start;
}
.detail-side {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.facts {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 8px 16px;
    margin: 0;
    font-size: 0.93rem;
}
.facts dt { color: var(--muted); font-size: 0.8rem; padding-top: 2px; white-space: nowrap; }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.facts-amount { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.actions form { margin: 0; }
.inline-form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}
.inline-form .field { flex: 1; }
.danger-zone {
    margin-top: 6px !important;
    padding-top: 14px;
    border-top: 1px solid var(--line);
}
.danger-zone-standalone { margin-top: 20px; text-align: right; }

.history {
    list-style: none;
    margin: 0;
    padding: 0;
    font-size: 0.88rem;
}
.history li {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 5px 0;
}
.history-meta { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }

/* ---- 確認ダイアログ ---- */

.dialog {
    width: min(420px, calc(100vw - 32px));
    padding: 24px;
    border: 0;
    border-radius: 10px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.dialog p { margin: 0 0 20px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---- 狭い画面 ---- */

@media (max-width: 960px) {
    .editor, .detail { grid-template-columns: 1fr; }
    .editor-preview, .detail-main { position: static; height: 80vh; }
    .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px; gap: 8px 16px; }
    .topbar nav { height: 40px; }
}

@media (max-width: 560px) {
    .container { padding: 20px 14px 48px; }
    .form-grid { grid-template-columns: 1fr; }
    .line { grid-template-columns: 1fr 1fr; }
    .filters .field, .filters .field:has(.range) { width: 100%; }
    .page-head { flex-direction: column; align-items: flex-start; }
}
