:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --text: #1c2630;
  --muted: #5b6875;
  --line: #d8dee4;
  --primary: #1f5f8b;
  --primary-ink: #ffffff;
  --primary-soft: #e3eef6;
  --ok: #1e7b4f;
  --ok-soft: #e2f3ea;
  --warn: #9a5b00;
  --warn-soft: #fff1d6;
  --danger: #b3261e;
  --danger-soft: #fde7e5;
  --focus: #f0b400;
  --radius: 10px;
  --tap: 44px;
  --font: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "BIZ UDPGothic", "BIZ UDPゴシック", "Meiryo", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- 上部バー ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--primary); color: var(--primary-ink);
}
.brand { color: inherit; text-decoration: none; font-weight: 700; white-space: nowrap; }
.topbar-mid { flex: 1; min-width: 0; }
.topbar-mid select {
  width: 100%; max-width: 420px; min-height: 40px; border-radius: 8px; border: 0; padding: 0 10px;
  background: rgba(255,255,255,.15); color: #fff;
}
.topbar-mid select option { color: var(--text); }
.topbar-right { position: relative; display: flex; align-items: center; gap: 8px; }
.pill {
  border: 0; border-radius: 999px; padding: 4px 12px; min-height: 32px; font-size: 13px;
  background: rgba(255,255,255,.18); color: #fff; white-space: nowrap; cursor: pointer;
}
.pill.online { background: #2e8b57; }
.pill.offline { background: #8a1c1c; }
.pill.warn { background: var(--focus); color: #1c2630; font-weight: 700; }
.menu-btn { border: 1px solid rgba(255,255,255,.5); background: transparent; color: #fff; border-radius: 8px; min-height: 36px; padding: 0 12px; cursor: pointer; }
.menu {
  position: absolute; right: 0; top: calc(100% + 8px); min-width: 220px;
  background: var(--surface); color: var(--text); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.18); padding: 6px; display: flex; flex-direction: column;
}
.menu-user { padding: 8px 10px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-item { display: block; text-align: left; padding: 10px; border-radius: 8px; border: 0; background: none; color: var(--text); text-decoration: none; cursor: pointer; min-height: var(--tap); }
.menu-item:hover { background: var(--surface-2); }

.banner { padding: 10px 16px; background: var(--warn-soft); color: var(--warn); border-bottom: 1px solid #f0d9a8; font-size: 14px; }
.banner button { margin-left: 8px; }

/* ---------- 画面 ---------- */
.view { max-width: 1080px; margin: 0 auto; padding: 16px; padding-bottom: 120px; outline: none; }
h1 { font-size: 22px; margin: 4px 0 12px; }
h2 { font-size: 18px; margin: 20px 0 10px; }
h3 { font-size: 16px; margin: 12px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.grow { flex: 1; }
.spacer { height: 12px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.card-title { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; margin: 0 0 8px; font-size: 17px; font-weight: 700; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 16px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); text-decoration: none; font-weight: 600;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); border-color: #e7b3ae; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

.chip { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); white-space: nowrap; }
.chip.blue { background: var(--primary-soft); border-color: #bcd4e6; color: var(--primary); }
.chip.ok { background: var(--ok-soft); border-color: #b9dfc8; color: var(--ok); }
.chip.warn { background: var(--warn-soft); border-color: #f0d9a8; color: var(--warn); }
.chip.danger { background: var(--danger-soft); border-color: #efc1bc; color: var(--danger); }

input[type=text], input[type=search], input[type=date], input[type=number], input[type=email], select, textarea {
  width: 100%; min-height: var(--tap); padding: 8px 10px; border: 1px solid #b9c3cc; border-radius: 8px; background: #fff;
}
textarea { resize: vertical; min-height: 64px; line-height: 1.5; }
input:disabled, textarea:disabled, select:disabled { background: var(--surface-2); color: var(--text); }
label.field-label { display: block; font-weight: 700; margin-bottom: 6px; }

/* ---------- 利用者一覧 ---------- */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar > * { flex: 1 1 200px; }
.toolbar .btn { flex: 0 0 auto; }
.client-list { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
.client-item {
  display: block; text-align: left; width: 100%; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px; cursor: pointer; color: inherit; text-decoration: none;
}
.client-item:hover { border-color: var(--primary); }
.client-name { font-size: 18px; font-weight: 700; }
.client-kana { font-size: 12px; color: var(--muted); }
.client-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; font-size: 13px; color: var(--muted); }

/* ---------- 利用者詳細 ---------- */
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 6px 16px; }
.kv div { font-size: 14px; }
.kv b { display: block; font-size: 12px; color: var(--muted); font-weight: 600; }
.src { font-size: 10px; margin-left: 4px; vertical-align: 1px; }
.form-buttons { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.form-buttons .btn { justify-content: flex-start; text-align: left; min-height: 52px; }
.doc-row { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); flex-wrap: wrap; }
.doc-row:first-child { border-top: 0; }
.doc-row input[type=checkbox] { width: 24px; height: 24px; }
.doc-title { font-weight: 700; }
.bundle-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; padding: 10px 16px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line); box-shadow: 0 -4px 12px rgba(0,0,0,.08);
  display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap;
}

/* ---------- 入力画面 ---------- */
.editor-head {
  position: sticky; top: 52px; z-index: 10; background: var(--bg); padding: 8px 0 6px; margin: -8px 0 8px;
  border-bottom: 1px solid var(--line);
}
.editor-head h1 { font-size: 18px; margin: 0; }
.save-state { font-size: 13px; color: var(--muted); }
.save-state.pending { color: var(--warn); font-weight: 700; }
.save-state.error { color: var(--danger); font-weight: 700; }
.tabs { display: flex; gap: 6px; overflow-x: auto; padding: 6px 0 2px; -webkit-overflow-scrolling: touch; scrollbar-width: thin; }
.tab { flex: 0 0 auto; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; min-height: 38px; cursor: pointer; font-size: 14px; white-space: nowrap; }
.tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.tab.missing::after { content: "●"; color: var(--danger); margin-left: 4px; font-size: 10px; vertical-align: 2px; }
.tab.active.missing::after { color: #ffd2cf; }

.section { scroll-margin-top: 150px; }
.field { padding: 12px 0; border-top: 1px dashed var(--line); }
.field:first-of-type { border-top: 0; }
.field.missing > .field-label::before { content: "未記入 "; color: var(--danger); font-size: 12px; }
.fields-grid { display: flex; flex-wrap: wrap; gap: 0 16px; }
.fields-grid > .field { flex: 1 1 100%; }
.fields-grid > .field.w-half { flex: 1 1 calc(50% - 16px); min-width: 220px; }
.fields-grid > .field.w-third { flex: 1 1 calc(33% - 16px); min-width: 160px; }
.help { font-size: 13px; color: var(--muted); margin: 4px 0 0; }
.help-toggle { border: 0; background: none; color: var(--primary); cursor: pointer; font-size: 13px; padding: 0 4px; }

.choices { display: flex; flex-wrap: wrap; gap: 8px; }
.choice {
  min-height: var(--tap); padding: 8px 14px; border-radius: 10px; border: 1.5px solid #b9c3cc; background: #fff;
  cursor: pointer; text-align: left; line-height: 1.35; font-size: 15px; touch-action: manipulation;
}
.choice[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 700; }
.choice[aria-pressed="true"]::before { content: "✓ "; }
.choice:disabled { cursor: default; }
.note { margin-top: 8px; }
.note-label { font-size: 13px; color: var(--muted); margin-bottom: 2px; }
.phrases { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.phrase { border: 1px dashed #9fb3c4; background: var(--surface-2); border-radius: 999px; padding: 4px 10px; font-size: 13px; cursor: pointer; min-height: 32px; }
.counter { font-size: 12px; color: var(--muted); text-align: right; }
.counter.over { color: var(--warn); font-weight: 700; }
.unit-wrap { display: flex; align-items: center; gap: 6px; }
.unit-wrap input { flex: 1; }

.repeat-item { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px 12px; margin-bottom: 10px; background: var(--surface-2); }
.repeat-head { display: flex; justify-content: space-between; align-items: center; font-weight: 700; }

.matrix { width: 100%; border-collapse: collapse; }
.matrix th, .matrix td { border-bottom: 1px solid var(--line); padding: 6px 4px; text-align: left; vertical-align: middle; }
.matrix th { font-size: 13px; color: var(--muted); font-weight: 600; }
.matrix td.cell { width: 1%; white-space: nowrap; }
.matrix .choice { min-width: 44px; padding: 8px 10px; font-size: 14px; }
.matrix .row-label { font-size: 15px; }
.matrix .other-input { margin-top: 4px; min-height: 38px; }
@media (max-width: 600px) {
  .matrix thead { display: none; }
  .matrix tr { display: block; padding: 8px 0; border-bottom: 1px solid var(--line); }
  .matrix td { display: inline-block; border: 0; padding: 2px 4px 2px 0; }
  .matrix td.row-label-cell { display: block; }
}

.sig-box { border: 1px dashed #9fb3c4; border-radius: var(--radius); background: #fff; min-height: 90px; display: flex; align-items: center; justify-content: center; padding: 8px; }
.sig-box img { max-width: 100%; max-height: 120px; }
.sig-pad-wrap { display: flex; flex-direction: column; gap: 10px; }
.sig-canvas { width: 100%; height: 46vh; min-height: 220px; max-height: 420px; border: 2px solid var(--primary); border-radius: var(--radius); background: #fff; touch-action: none; }

.result-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.result-table td, .result-table th { border: 1px solid var(--line); padding: 6px 8px; }
.result-table .hit { color: var(--danger); font-weight: 700; }

.finalized-note { background: var(--ok-soft); color: var(--ok); border: 1px solid #b9dfc8; border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; }
.alert { background: var(--warn-soft); border: 1px solid #f0d9a8; color: #6b4200; border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; font-size: 14px; }
.alert.danger { background: var(--danger-soft); border-color: #efc1bc; color: var(--danger); }
.alert table { border-collapse: collapse; margin-top: 6px; }
.alert td, .alert th { padding: 2px 8px; border-bottom: 1px solid rgba(0,0,0,.08); text-align: left; }

/* ---------- 管理 ---------- */
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 14px; background: var(--surface); }
.data-table th, .data-table td { border-bottom: 1px solid var(--line); padding: 8px; text-align: left; vertical-align: top; }
.data-table th { background: var(--surface-2); font-weight: 700; white-space: nowrap; }

/* ---------- その他 ---------- */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 50;
  background: #22303c; color: #fff; padding: 10px 18px; border-radius: 999px; max-width: calc(100% - 32px);
  box-shadow: 0 6px 20px rgba(0,0,0,.25); font-size: 14px;
}
.dialog { border: 0; border-radius: 14px; padding: 0; width: min(560px, calc(100% - 24px)); max-height: calc(100% - 24px); }
.dialog.wide { width: min(900px, calc(100% - 16px)); }
.dialog::backdrop { background: rgba(10, 20, 30, .45); }
.dialog-body { padding: 18px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 12px 18px 18px; }
.login-box { max-width: 460px; margin: 10vh auto; text-align: center; }
.empty { text-align: center; color: var(--muted); padding: 32px 8px; }
.loading { text-align: center; color: var(--muted); padding: 40px; }

@media (max-width: 600px) {
  .view { padding: 12px; }
  .topbar { position: static; flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .brand { font-size: 15px; flex: 1; }
  .topbar-mid { order: 3; flex-basis: 100%; }
  .topbar-mid select { max-width: none; }
  .fields-grid > .field.w-half, .fields-grid > .field.w-third { flex-basis: 100%; }
  .editor-head { top: 0; }
  .section { scroll-margin-top: 130px; }
}
@media print { .topbar, .bundle-bar, .editor-head .row { display: none; } }
