:root {
  --c-primary: #2b7a78;
  --c-primary-d: #205653;
  --c-accent: #3aafa9;
  --c-bg: #f4f6f8;
  --c-card: #ffffff;
  --c-border: #d9e0e3;
  --c-text: #1d2d2c;
  --c-muted: #6b7b7a;
  --c-danger: #c0392b;
  --radius: 10px;
  --gap: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN",
    "Hiragino Sans", "Noto Sans JP", Meiryo, sans-serif;
  color: var(--c-text);
  background: var(--c-bg);
  font-size: 16px;
  line-height: 1.5;
}

/* sticky-footer (flexbox) パターン。position:fixed は iframe 内で崩れるため不使用 */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.app-header {
  background: var(--c-primary);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 10px 12px;
  gap: 8px;
}
.app-header h1 { font-size: 18px; margin: 0; font-weight: 600; }
.header-user {
  margin-left: auto; font-size: 12px; color: #fff;
  white-space: nowrap; max-width: 50%; overflow: hidden; text-overflow: ellipsis;
  background: rgba(255,255,255,.18); border: none; border-radius: 999px;
  padding: 5px 12px; cursor: pointer; font-family: inherit; display: none;
  -webkit-tap-highlight-color: transparent; transition: background .15s;
}
.header-user.show { display: inline-block; }
.header-user:active { background: rgba(255,255,255,.32); }
.header-back {
  background: transparent; border: none; color: #fff;
  font-size: 28px; line-height: 1; width: 32px; height: 32px;
  cursor: pointer; padding: 0; display: none;
}
.header-back.show { display: block; }

.app-nav {
  display: flex;
  background: var(--c-primary-d);
}
.app-nav a {
  flex: 1; text-align: center; color: #cfe7e5; text-decoration: none;
  padding: 10px 4px; font-size: 14px; border-bottom: 3px solid transparent;
}
.app-nav a.active { color: #fff; border-bottom-color: var(--c-accent); font-weight: 600; }

.app-view {
  flex: 1;
  padding: var(--gap);
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
}

.app-footer {
  text-align: center;
  color: var(--c-muted);
  font-size: 11px;
  padding: 8px;
}

.loading { text-align: center; color: var(--c-muted); padding: 40px 0; }

/* ---- card / section ---- */
.card {
  background: var(--c-card);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: var(--gap);
}
.card h2 { font-size: 16px; margin: 0 0 10px; }

/* アカウントのプロフィールカード */
.profile-card { text-align: center; padding: 22px 14px; }
.profile-avatar {
  width: 64px; height: 64px; border-radius: 50%;
  background: var(--c-primary); color: #fff;
  font-size: 30px; line-height: 64px; margin: 0 auto 10px;
}
.profile-name { font-size: 20px; font-weight: 700; }
.profile-role { font-size: 13px; color: var(--c-muted); margin-top: 2px; }
.card h3 { font-size: 14px; margin: 16px 0 8px; color: var(--c-primary-d); }
.muted { color: var(--c-muted); font-size: 13px; }

.summary-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
}
.summary-grid .stat {
  background: var(--c-card); border: 1px solid var(--c-border);
  border-radius: var(--radius); padding: 12px 8px; text-align: center;
}
.summary-grid .stat .num { font-size: 24px; font-weight: 700; color: var(--c-primary); }
.summary-grid .stat .lbl { font-size: 11px; color: var(--c-muted); }

/* ---- 往診カレンダー ---- */
.cal-card { padding: 12px; }
.cal-head { display: flex; align-items: center; justify-content: center; gap: 14px; margin-bottom: 8px; }
.cal-head b { font-size: 15px; }
.cal-nav {
  border: 1px solid var(--c-border); background: var(--c-card); color: var(--c-primary);
  width: 32px; height: 32px; border-radius: 8px; font-size: 18px; line-height: 1; cursor: pointer;
}
.cal-nav:active { background: var(--c-bg); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
.cal-dow { text-align: center; font-size: 11px; color: var(--c-muted); padding: 2px 0; }
.cal-dow.sun { color: var(--c-danger); }
.cal-dow.sat { color: var(--c-primary); }
.cal-cell {
  position: relative; min-height: 38px; border-radius: 8px; padding: 3px 0 0;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  font-size: 13px; color: var(--c-text);
}
.cal-cell.empty { visibility: hidden; }
.cal-cell.has { background: var(--c-bg); cursor: pointer; }
.cal-cell.today { outline: 2px solid var(--c-accent); }
.cal-cell.sel { background: var(--c-accent); color: #fff; }
.cal-dots { display: flex; gap: 2px; margin-top: 2px; min-height: 8px; }
.cal-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; vertical-align: middle; }
.cal-dot.plan { background: var(--c-primary); }
.cal-dot.done { background: #e0a93b; }
.cal-dot.over { background: var(--c-danger); }
.cal-legend { font-size: 11px; color: var(--c-muted); margin-top: 8px; text-align: center; }
.cal-legend .cal-dot { margin-right: 3px; }
.cal-day { margin-top: 6px; }
.cal-day-head { font-size: 13px; font-weight: 700; color: var(--c-primary-d); padding: 6px 2px 2px; }
.cal-day .li-main .cal-dot { margin-right: 6px; }

/* ---- 訪問予定カード ---- */
.plan-list { margin-bottom: 8px; }
.plan-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 4px; border-bottom: 1px solid var(--c-border); font-size: 14px;
}
.plan-row.over { color: var(--c-danger); }
.plan-add { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.plan-add input { flex: 1; padding: 8px; border: 1px solid var(--c-border); border-radius: 8px; }
.plan-bulk { margin-top: 12px; border-top: 1px solid var(--c-border); padding-top: 8px; }
.plan-bulk summary { cursor: pointer; font-size: 13px; color: var(--c-primary); }
.plan-bulk-row { margin: 8px 0; font-size: 14px; }
.plan-bulk-row input, .plan-bulk-row select { padding: 6px; border: 1px solid var(--c-border); border-radius: 8px; }

/* ---- list ---- */
.list-item {
  background: var(--c-card); border: 1px solid var(--c-border);
  border-radius: var(--radius); padding: 12px 14px; margin-bottom: 8px;
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer; text-decoration: none; color: inherit;
}
.list-item { transition: background .12s, transform .08s; }
.list-item:active { background: #eef5f4; transform: scale(.985); }
.list-item .li-main { font-weight: 600; }
.list-item .li-sub { font-size: 12px; color: var(--c-muted); }
.list-item .chevron { color: var(--c-muted); font-size: 20px; }

.badge {
  display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 999px;
  margin-left: 6px;
}
.badge.todo { background: #fdecea; color: var(--c-danger); }
.badge.done { background: #e8f5e9; color: #2e7d32; }
.badge.gray { background: #eceff1; color: var(--c-muted); }
.badge.karte { background: var(--c-primary); color: #fff; font-weight: 700; margin-left: 0; margin-right: 4px; }

/* 要対応ブロック（ホーム） */
.todo-block { margin-bottom: 12px; }
.todo-block:last-child { margin-bottom: 0; }
.todo-head { font-weight: 600; font-size: 13px; margin: 4px 0; }

/* 月切替バー（書類ハブ） */
.month-bar { display: flex; align-items: center; justify-content: space-between; }
.month-bar b { font-size: 16px; }

/* 書類チップ（①②③…） */
.docchip {
  display: inline-block; min-width: 18px; text-align: center;
  font-size: 12px; padding: 0 4px; border-radius: 4px; margin-right: 2px;
  background: #fdecea; color: var(--c-danger);
}
.docchip.done { background: #e8f5e9; color: #2e7d32; }

/* カルテ番号の見出し（患者ダッシュボード上部） */
.karte-no {
  display: inline-block;
  background: #e7f3f2; color: var(--c-primary-d);
  border: 1px solid var(--c-accent); border-radius: 8px;
  padding: 4px 12px; font-size: 14px; margin-bottom: 8px;
}
.karte-no b { font-size: 20px; margin-left: 4px; }

/* ---- buttons ---- */
.btn {
  display: inline-block; width: 100%; text-align: center;
  background: var(--c-primary); color: #fff; border: none;
  border-radius: var(--radius); padding: 13px; font-size: 16px;
  cursor: pointer; text-decoration: none; margin-bottom: 8px;
  transition: background .12s, transform .08s;
}
.btn:active { background: var(--c-primary-d); transform: scale(.985); }
.btn.secondary { background: #fff; color: var(--c-primary); border: 1px solid var(--c-primary); }
.btn.danger { background: var(--c-danger); }
.btn.ghost { background: transparent; color: var(--c-primary); }
.btn.small { width: auto; padding: 8px 14px; font-size: 14px; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { margin-bottom: 0; }

.menu-btn {
  display: flex; align-items: center; gap: 10px;
  background: var(--c-card); border: 1px solid var(--c-border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 8px;
  cursor: pointer; text-decoration: none; color: inherit; font-size: 16px;
}
.menu-btn .ico { font-size: 22px; }
.menu-btn { transition: background .12s, transform .08s; }
.menu-btn:active { background: #eef5f4; transform: scale(.985); }

/* ---- forms ---- */
.field { margin-bottom: 14px; }
.field > label { display: block; font-size: 13px; color: var(--c-primary-d); margin-bottom: 5px; font-weight: 600; }
.field .req { color: var(--c-danger); margin-left: 3px; }
.field input[type=text], .field input[type=tel], .field input[type=date],
.field input[type=time], .field input[type=number], .field select, .field textarea {
  width: 100%; padding: 10px; border: 1px solid var(--c-border);
  border-radius: 8px; font-size: 16px; background: #fff; font-family: inherit;
}
.field textarea { min-height: 72px; resize: vertical; }
.field .hint { font-size: 11px; color: var(--c-muted); margin-top: 3px; }

.radio-group, .check-group { display: flex; flex-wrap: wrap; gap: 6px; }
.radio-group label, .check-group label {
  border: 1px solid var(--c-border); border-radius: 999px; padding: 7px 14px;
  font-size: 14px; cursor: pointer; background: #fff; user-select: none;
}
.radio-group input, .check-group input { display: none; }
.radio-group input:checked + span,
.radio-group label:has(input:checked),
.check-group label:has(input:checked) {
  /* fallback handled by JS class .sel */
}
.radio-group label.sel, .check-group label.sel {
  background: var(--c-accent); color: #fff; border-color: var(--c-accent);
}

/* tri-state group (あり/なし/分からない) compact */
.tri-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.tri-row .tri-label { flex: 1; font-size: 14px; }
.tri-row .tri-opts { display: flex; gap: 4px; }
.tri-row .tri-opts label {
  border: 1px solid var(--c-border); border-radius: 6px; padding: 5px 9px;
  font-size: 12px; cursor: pointer; background: #fff;
}
.tri-row .tri-opts label.sel { background: var(--c-accent); color: #fff; border-color: var(--c-accent); }
.tri-row .tri-opts input { display: none; }

.form-section {
  border-top: 1px solid var(--c-border);
  margin-top: 18px; padding-top: 14px;
}
.form-section:first-child { border-top: none; margin-top: 0; padding-top: 0; }
.form-section > .sec-title {
  font-size: 15px; font-weight: 700; color: var(--c-primary);
  margin-bottom: 12px;
}
.subfield { padding-left: 12px; border-left: 2px solid var(--c-border); margin: 8px 0; }

/* ---- tooth chart ---- */
.tooth-chart { margin: 10px 0; }
.tooth-legend { font-size: 11px; color: var(--c-muted); margin-bottom: 8px; line-height: 1.7; }
.tooth-row { display: flex; justify-content: center; gap: 3px; margin-bottom: 4px; flex-wrap: nowrap; }
.tooth {
  width: 30px; height: 36px; border: 1px solid var(--c-border); border-radius: 5px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 9px; cursor: pointer; background: #fff; padding: 1px;
}
.tooth .t-num { color: var(--c-muted); font-size: 8px; }
.tooth .t-state { font-weight: 700; font-size: 11px; min-height: 12px; }
.tooth.set { background: #fff4e0; border-color: #e6a23c; }
.tooth-mid { border-left: 2px dashed var(--c-muted); }

/* ---- bottom sheet ---- */
.sheet-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; z-index: 1000;
}
.sheet {
  background: #fff; width: 100%; border-radius: 16px 16px 0 0;
  padding: 16px; max-height: 80vh; overflow-y: auto;
  animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet h3 { margin: 0 0 12px; font-size: 16px; }
/* メッセージ */
.nav-badge {
  display: inline-block; min-width: 16px; height: 16px; line-height: 16px;
  padding: 0 4px; margin-left: 3px; border-radius: 8px; background: var(--c-danger, #e34);
  color: #fff; font-size: 10px; text-align: center; vertical-align: top;
}
.li-time { font-size: 11px; color: var(--c-muted, #999); white-space: nowrap; align-self: center; }
.msg-list { max-height: 60vh; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.msg-row { max-width: 80%; }
.msg-row.mine { align-self: flex-end; text-align: right; }
.msg-row.other { align-self: flex-start; }
.msg-from { font-size: 11px; color: var(--c-muted, #999); margin-bottom: 2px; }
.msg-bubble {
  display: inline-block; padding: 8px 12px; border-radius: 14px;
  background: #eee; text-align: left; word-break: break-word; white-space: pre-wrap;
}
.msg-row.mine .msg-bubble { background: var(--c-primary, #2a7); color: #fff; }
.msg-time { font-size: 10px; color: var(--c-muted, #999); margin-top: 2px; }
.msg-compose { display: flex; gap: 8px; align-items: stretch; }
.msg-compose textarea {
  flex: 1 1 auto; min-width: 0; resize: vertical; margin: 0;
  padding: 10px; border: 1px solid var(--c-border, #ccc); border-radius: 8px;
  font-size: 15px; font-family: inherit;
}
.msg-compose .btn { width: auto; flex: 0 0 auto; align-self: stretch; margin: 0; padding: 10px 18px; }
.recip { display: block; padding: 8px 4px; border-bottom: 1px solid #eee; cursor: pointer; }

/* 要対応の折りたたみ */
.todo-card > summary { cursor: pointer; font-size: 15px; list-style: revert; }
.todo-card > summary .badge.todo { margin-left: 6px; }
.todo-card[open] > summary { margin-bottom: 8px; }
/* 登録ダイアログはログイン画面(#authGate z-index:9999)より前面に */
#regDialog { z-index: 10000; }
.reg-fixed {
  padding: 10px 12px; background: var(--c-bg, #f5f5f5); border-radius: 8px;
  font-size: 14px; color: var(--c-text, #333);
}
.sheet-opt {
  padding: 13px; border: 1px solid var(--c-border); border-radius: 8px;
  margin-bottom: 6px; cursor: pointer; font-size: 15px;
}
.sheet-opt:active, .sheet-opt.sel { background: var(--c-accent); color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; z-index: 2000; opacity: 0; transition: opacity .2s;
}
.toast.show { opacity: 1; }

.inline-warn { background: #fff8e1; border: 1px solid #ffe082; color: #8a6d00;
  padding: 10px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }

/* ===========================================================
   印刷書類ビュー（書式 ①〜⑤ の再現）
   画面では拡大縮小して表示、印刷時は A4 にフィット
   =========================================================== */
.doc-toolbar {
  display: flex; gap: 8px; margin-bottom: 12px;
}
.doc-page {
  background: #fff; color: #000; margin: 0 auto 16px;
  width: 210mm; min-height: 297mm; padding: 12mm;
  box-shadow: 0 0 6px rgba(0,0,0,.2);
  font-size: 10.5pt; line-height: 1.45;
}
.doc-scale { transform-origin: top left; }
#docArea { overflow-x: hidden; }
.doc-page * { box-sizing: border-box; }
.doc-title { text-align: center; font-size: 14pt; font-weight: 700; margin: 0 0 2mm; }
.doc-subtitle { text-align: center; font-size: 10pt; margin: -1mm 0 3mm; }
.doc-page table { border-collapse: collapse; width: 100%; }
.doc-page td, .doc-page th { border: 1px solid #000; padding: 2px 4px; vertical-align: top; font-size: 9.5pt; }
.doc-page .noborder, .doc-page .noborder td { border: none; }
.doc-page .cb { font-family: monospace; }
.doc-row { display: flex; justify-content: space-between; align-items: flex-end; }
.doc-label-line { border-bottom: 1px solid #000; flex: 1; min-height: 1.4em; }
.doc-small { font-size: 8.5pt; }
.doc-section-label { font-size: 9.5pt; margin: 2mm 0 1mm; }
.doc-checks { font-size: 9.5pt; }
.doc-checks span { margin-right: 10px; white-space: nowrap; }

@page { margin: 0; }
@media print {
  .app-header, .app-nav, .app-footer, .doc-toolbar, .doc-meta, #sheetRoot { display: none !important; }
  .app-view { padding: 0; max-width: none; }
  body { background: #fff; }
  .doc-page { box-shadow: none; margin: 0 !important; width: auto; min-height: auto; padding: 10mm;
              page-break-after: always; transform: none !important; }
  .doc-page:last-child { page-break-after: auto; }
  .doc-scale { transform: none !important; }
  #docArea { overflow: visible; }
  /* 長い書類が複数ページにまたがる時、欄の途中では切らない（見やすさ優先） */
  .doc-page tr, .doc-page .doc-checks { page-break-inside: avoid; }
  .doc-page .doc-section-label { page-break-inside: avoid; page-break-after: avoid; }
}

/* ===== パスキー認証ゲート ===== */
#authGate {
  position: fixed; inset: 0; z-index: 9999;
  background: var(--c-bg);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.auth-box {
  width: 100%; max-width: 360px;
  background: #fff; border-radius: 14px; padding: 28px 22px;
  box-shadow: 0 8px 30px rgba(0,0,0,.12); text-align: center;
}
.auth-box h1 { font-size: 20px; color: var(--c-primary-d); margin: 0 0 6px; }
.auth-sub { font-size: 13px; color: var(--c-muted); margin: 0 0 16px; }
.auth-msg { min-height: 18px; font-size: 13px; margin-bottom: 10px; white-space: pre-line; line-height: 1.5; }
.auth-msg.err { color: var(--c-danger); }
.auth-box details { text-align: left; margin-top: 14px; border-top: 1px solid #eee; padding-top: 10px; }
.auth-box summary { cursor: pointer; font-size: 13px; color: var(--c-primary); }
.auth-box details input {
  width: 100%; box-sizing: border-box; margin: 10px 0; padding: 10px;
  border: 1px solid #ccc; border-radius: 8px; font-size: 15px;
}
.cred-row { display: flex; justify-content: space-between; align-items: center;
  gap: 8px; padding: 6px 0; border-bottom: 1px solid #f0f0f0; font-size: 13px; }
