:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #1f2a37;
  --sub: #5b6776;
  --line: #dfe4ea;
  --primary: #2f5d8a;
  --primary-weak: #e8f0f8;
  --accent: #e8743b;
  --care: #3f8a7f;
  --care-weak: #e3f1ef;
  --warn: #a8481a;
  --warn-weak: #fdf0e7;
  --danger: #b42318;
  --radius: 12px;
  --topbar: 56px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", "Yu Gothic", Meiryo, sans-serif;
  font-size: 15px; line-height: 1.6;
}
button, input, select, textarea { font: inherit; color: inherit; }
h2, h3 { margin: 0; }

/* ---- ボタン ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  min-height: 38px; padding: 6px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); cursor: pointer; white-space: nowrap; font-size: 14px;
}
.btn:hover { border-color: #b9c3ce; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 3px solid #9cc0e4; outline-offset: 1px; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.btn.primary:hover { background: #274f76; }
.btn.big { min-height: 50px; font-size: 16px; width: 100%; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--primary-weak); }
.btn.link { border: none; background: none; color: var(--primary); padding: 4px 6px; min-height: 0; text-decoration: underline; }
.btn.danger { color: var(--danger); }
.btn.link.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

/* ---- 上部バー ---- */
.topbar {
  position: sticky; top: 0; z-index: 20; height: var(--topbar);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 0 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.topnav { display: flex; gap: 2px; overflow-x: auto; }

/* ---- レイアウト ---- */
.app {
  display: grid; grid-template-columns: minmax(0, 1fr) 430px; gap: 16px;
  max-width: 1440px; margin: 0 auto; padding: 14px 16px 40px;
}
.panel-plan {
  position: sticky; top: calc(var(--topbar) + 14px); align-self: start;
  max-height: calc(100vh - var(--topbar) - 28px); overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px; padding-bottom: 4px;
}
.box { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
details.box > summary { cursor: pointer; font-weight: 700; list-style: none; display: flex; align-items: center; justify-content: space-between; }
details.box > summary::-webkit-details-marker { display: none; }
details.box > summary::after { content: "▾"; color: var(--sub); font-size: 13px; }
details.box:not([open]) > summary::after { content: "▸"; }
details.box[open] > summary { margin-bottom: 10px; }

/* ---- ライブラリ ---- */
.lib-search input {
  width: 100%; min-height: 46px; padding: 8px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); font-size: 16px;
}
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0 8px; }
.chip {
  min-height: 36px; padding: 4px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--card); cursor: pointer; font-size: 14px;
}
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip.care[aria-pressed="true"] { background: var(--care); border-color: var(--care); }
.filters { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: end; font-size: 13px; color: var(--sub); }
.filters label { display: flex; flex-direction: column; gap: 2px; }
.filters select { min-height: 36px; padding: 4px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); color: var(--text); font-size: 14px; min-width: 110px; }
.lib-count { font-size: 13px; color: var(--sub); margin: 10px 0 8px; }
.ex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.ex-card {
  position: relative; display: flex; flex-direction: column; background: var(--card);
  border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
}
.ex-card.added { border-color: #9cc0e4; box-shadow: 0 0 0 2px var(--primary-weak); }
.ex-open { display: block; width: 100%; padding: 0; border: none; background: #fff; cursor: zoom-in; border-bottom: 1px solid var(--line); }
.ex-open svg { display: block; width: 100%; height: auto; }
.ex-info { padding: 8px 10px 4px; flex: 1; }
.ex-name { font-weight: 700; font-size: 14px; line-height: 1.45; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tag { font-size: 11.5px; padding: 0 7px; border-radius: 999px; background: #eef2f6; color: var(--sub); line-height: 1.7; }
.tag.cat { background: var(--primary-weak); color: var(--primary); }
.tag.care { background: var(--care-weak); color: var(--care); }
.tag.lv { background: none; color: #a0a9b4; padding: 0 2px; letter-spacing: -1px; }
.btn-add { margin: 6px 10px 10px; min-height: 36px; border-radius: 9px; border: 1px solid var(--primary); background: var(--card); color: var(--primary); font-weight: 600; cursor: pointer; font-size: 14px; }
.btn-add:hover { background: var(--primary-weak); }
.ex-card.added .btn-add { background: var(--primary); color: #fff; }
.warn-badge {
  position: absolute; top: 6px; left: 6px; font-size: 11.5px; font-weight: 700;
  background: var(--warn-weak); color: var(--warn); border: 1px solid #f2c3a6; border-radius: 999px; padding: 0 8px;
}

/* ---- 処方パネル ---- */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }
.form-grid label, .stack { display: flex; flex-direction: column; gap: 3px; font-size: 13px; color: var(--sub); }
.form-grid .span2 { grid-column: span 2; }
input[type=text], input[type=date], input[type=number], select, textarea {
  min-height: 40px; padding: 6px 10px; border-radius: 9px; border: 1px solid var(--line); background: #fff; color: var(--text); font-size: 15px; width: 100%;
}
textarea { resize: vertical; min-height: 0; line-height: 1.5; }
fieldset { border: none; padding: 0; margin: 12px 0 0; }
legend { font-size: 13px; color: var(--sub); margin-bottom: 4px; padding: 0; }
legend small { font-size: 11.5px; }
.seg { display: flex; flex-wrap: wrap; gap: 0; }
.seg legend { width: 100%; }
.seg label { flex: 1 1 auto; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 7px 8px; border: 1px solid var(--line); font-size: 13.5px; cursor: pointer; background: #fff; }
.seg label:first-of-type span { border-radius: 9px 0 0 9px; }
.seg label:last-of-type span { border-radius: 0 9px 9px 0; }
.seg label + label span { border-left: none; }
.seg input:checked + span { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 600; }
.seg input:focus-visible + span { outline: 3px solid #9cc0e4; }
.flags { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.flags legend { width: 100%; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: var(--primary); }

.plan-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.plan-head h2 { font-size: 16px; }
.plan-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.empty { color: var(--sub); font-size: 14px; margin: 8px 0 4px; }
.pi { border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: #fbfcfd; }
.pi-head { display: flex; align-items: center; gap: 8px; }
.pi-no { flex: none; width: 24px; height: 24px; border-radius: 50%; background: var(--primary); color: #fff; font-size: 13px; font-weight: 700; display: grid; place-items: center; }
.pi.care .pi-no { background: var(--care); }
.pi-name { flex: 1; text-align: left; border: none; background: none; padding: 0; font-weight: 700; font-size: 14.5px; cursor: pointer; line-height: 1.4; }
.pi-tools { display: flex; gap: 2px; }
.pi-tools button { width: 32px; height: 32px; border: 1px solid var(--line); background: #fff; border-radius: 8px; cursor: pointer; font-size: 14px; padding: 0; }
.pi-tools button:disabled { opacity: .35; cursor: default; }
.pi-main { display: grid; grid-template-columns: 96px 1fr; gap: 10px; margin-top: 8px; }
.pi-thumb svg { width: 96px; height: auto; display: block; background: #fff; border: 1px solid var(--line); border-radius: 8px; }
.pi-fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.fld { display: flex; flex-direction: column; font-size: 11.5px; color: var(--sub); gap: 1px; }
.fld .in { display: flex; align-items: center; gap: 3px; }
.fld input { min-height: 34px; padding: 3px 6px; text-align: right; font-size: 15px; }
.fld .u { font-size: 12px; color: var(--sub); flex: none; }
.fld.wide { grid-column: span 3; }
.fld.half { grid-column: span 3; }
.pi-extra { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
.pi-extra select, .pi-extra input { min-height: 34px; font-size: 14px; }
.pi textarea { margin-top: 6px; font-size: 14px; }
.pi .check { margin-top: 6px; font-size: 13px; }
.pi-warn { margin-top: 8px; font-size: 12.5px; color: var(--warn); background: var(--warn-weak); border-radius: 8px; padding: 6px 8px; line-height: 1.5; }
.pi-warn b { display: block; }

.opt-row { display: flex; flex-wrap: wrap; align-items: end; gap: 10px 16px; margin-top: 10px; }
.opt-row > label:first-child { display: flex; flex-direction: column; font-size: 13px; color: var(--sub); gap: 3px; }
.opt-row select { width: auto; }

.actions { position: sticky; bottom: 0; background: var(--bg); padding-top: 4px; display: flex; flex-direction: column; gap: 8px; }
.actions-sub { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.actions-sub .btn { padding: 6px 4px; font-size: 13px; }

/* ---- タブ（スマホ・タブレット縦） ---- */
.tabbar { display: none; }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 999px; background: var(--accent); color: #fff; font-size: 12px; line-height: 20px; }

/* ---- ダイアログ ---- */
.dlg { border: none; border-radius: 16px; padding: 0; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 32px); box-shadow: 0 20px 60px rgba(15, 30, 50, .25); color: var(--text); }
.dlg.wide { width: min(820px, calc(100vw - 24px)); }
.dlg::backdrop { background: rgba(20, 30, 45, .45); }
.dlg-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; background: #fff; border-bottom: 1px solid var(--line); }
.dlg-head h2 { font-size: 17px; }
.dlg-body { padding: 14px 16px 18px; }
.dlg-body h3 { font-size: 14px; margin: 16px 0 6px; color: var(--sub); }
.note { font-size: 13px; color: var(--sub); margin: 6px 0; }
.note.small { font-size: 12px; margin-top: 14px; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.list li { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; }
.list .grow { flex: 1; min-width: 0; }
.list .t { font-weight: 700; font-size: 14.5px; }
.list .s { font-size: 12.5px; color: var(--sub); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .btn { min-height: 34px; padding: 4px 10px; font-size: 13px; }
.list .none { color: var(--sub); font-size: 13.5px; border-style: dashed; }
.row-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }

/* 運動の詳細 */
.ex-detail { display: grid; grid-template-columns: 1.1fr 1fr; gap: 18px; }
.ex-detail .fig { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.ex-detail .fig svg { display: block; width: 100%; height: auto; }
.ex-detail h2 { font-size: 19px; margin-bottom: 6px; }
.ex-detail h3 { font-size: 13px; color: var(--sub); margin: 12px 0 2px; }
.ex-detail ol, .ex-detail ul { margin: 0; padding-left: 1.3em; font-size: 14.5px; }
.ex-detail .caution li::marker { color: var(--accent); }
.legend-line { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--sub); padding: 6px 10px; border-top: 1px solid var(--line); }
.legend-line i { width: 12px; height: 12px; border-radius: 3px; background: var(--accent); display: inline-block; }
.legend-line i.g { background: #33475b; opacity: .3; }
.ex-detail-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

/* プレビュー */
.dlg.preview { width: min(900px, calc(100vw - 16px)); max-height: calc(100dvh - 16px); background: #e9edf1; }
.dlg.preview .dlg-head { flex-wrap: wrap; }
.paper-wrap { overflow-x: hidden; overflow-y: auto; }
/* 用紙はA4幅のまま描き、画面の幅に合わせて縮小する（app.js の fitPreview） */
.paper-sizer { margin: 0 auto; overflow: hidden; box-shadow: 0 2px 10px rgba(0,0,0,.15); }
.paper { background: #fff; width: 210mm; padding: 12mm; transform-origin: top left; }
@media (max-width: 600px) {
  .dlg.preview .dlg-body { padding: 8px; }
  .dlg.preview .dlg-head { padding: 8px 10px; }
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); z-index: 100;
  background: #1f2a37; color: #fff; padding: 10px 18px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---- 狭い画面 ---- */
@media (max-width: 960px) {
  .app { grid-template-columns: 1fr; padding-bottom: 90px; }
  .panel-plan { position: static; max-height: none; overflow: visible; }
  body[data-tab="lib"] .panel-plan { display: none; }
  body[data-tab="plan"] .panel-lib { display: none; }
  .tabbar {
    display: grid; grid-template-columns: 1fr 1fr; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    background: var(--card); border-top: 1px solid var(--line); padding: 6px 10px calc(6px + env(safe-area-inset-bottom)); gap: 8px;
  }
  .tabbar button { min-height: 46px; border-radius: 10px; border: 1px solid var(--line); background: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
  .tabbar button[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: #fff; }
  .actions { position: static; }
  .toast { bottom: 84px; }
}
@media (max-width: 600px) {
  body { font-size: 14.5px; }
  .topbar { padding: 0 8px; }
  .brand span { font-size: 15px; }
  .topnav .btn { padding: 6px 8px; font-size: 13px; }
  .app { padding: 10px 10px 90px; }
  .ex-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .filters select { min-width: 0; width: 100%; }
  .filters { display: grid; grid-template-columns: 1fr 1fr; }
  .ex-detail { grid-template-columns: 1fr; }
  .pi-main { grid-template-columns: 72px 1fr; }
  .pi-thumb svg { width: 72px; }
}

/* =========================================================
   印刷用
   ========================================================= */
#print-root { display: none; }
.sheet { color: #111; font-size: 11pt; line-height: 1.55; }
.sheet .p-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 6mm; border-bottom: 0.6mm solid #333; padding-bottom: 2.5mm; margin-bottom: 3mm; }
.sheet .p-title { font-size: 17pt; font-weight: 700; line-height: 1.3; }
.sheet .p-name { font-size: 14pt; margin-top: 1.5mm; }
.sheet .p-name b { font-size: 16pt; display: inline-block; min-width: 50mm; border-bottom: 0.3mm solid #666; padding: 0 2mm; }
.sheet .p-right { display: flex; flex-direction: column; align-items: flex-end; gap: 1.5mm; }
.sheet .p-logo { height: 15mm; width: auto; display: block; }
.sheet .p-meta { font-size: 9.5pt; text-align: right; white-space: nowrap; line-height: 1.6; }
.sheet .p-lead { font-size: 10.5pt; margin: 0 0 3.5mm; padding: 2mm 3mm; background: #f2f5f8; border-radius: 2mm; }
.sheet .p-legend { display: flex; gap: 5mm; font-size: 8.5pt; color: #444; margin: -1.5mm 0 3mm; }
.sheet .p-legend i { display: inline-block; width: 3mm; height: 3mm; border-radius: 0.6mm; background: #e8743b; margin-right: 1mm; vertical-align: -0.3mm; }
.sheet .p-legend i.g { background: #33475b; opacity: .3; }
.sheet .p-item { display: flex; gap: 4mm; border: 0.3mm solid #b8c0c8; border-radius: 2.5mm; padding: 3mm 3.5mm; margin-bottom: 3.5mm; break-inside: avoid; page-break-inside: avoid; }
.sheet .p-fig { width: 60mm; flex: none; }
.sheet .p-fig svg { width: 100%; height: auto; display: block; }
.sheet .p-fig .lg { font-size: 8pt; color: #555; text-align: center; }
.sheet .p-body { flex: 1; min-width: 0; }
.sheet .p-body h2 { font-size: 13.5pt; line-height: 1.35; display: flex; align-items: center; gap: 2mm; margin: 0 0 1.5mm; }
.sheet .p-no { flex: none; width: 6.5mm; height: 6.5mm; border-radius: 50%; background: #2f5d8a; color: #fff; font-size: 10.5pt; display: inline-grid; place-items: center; }
.sheet .p-side { font-size: 10pt; font-weight: 700; border: 0.3mm solid #2f5d8a; color: #2f5d8a; border-radius: 1mm; padding: 0 1.5mm; margin-left: auto; white-space: nowrap; }
.sheet .p-dose { font-size: 13pt; font-weight: 700; background: #fff4ec; border-left: 1.2mm solid #e8743b; padding: 1mm 2.5mm; margin-bottom: 1.5mm; }
.sheet .p-dose small { font-size: 10pt; font-weight: 600; margin-left: 2mm; }
.sheet ol { margin: 0; padding-left: 5mm; }
.sheet .p-caution { font-size: 9.5pt; color: #333; margin-top: 1.2mm; }
.sheet .p-caution b { color: #a8481a; }
.sheet .p-note { margin-top: 1.5mm; font-size: 10.5pt; border: 0.3mm dashed #888; border-radius: 1.5mm; padding: 1mm 2mm; }
.sheet .p-memo { border: 0.3mm solid #888; border-radius: 2mm; padding: 2mm 3mm; margin: 1mm 0 3mm; font-size: 10.5pt; break-inside: avoid; }
.sheet .p-stop { border: 0.5mm solid #a8481a; border-radius: 2mm; padding: 2mm 3mm; font-size: 10pt; break-inside: avoid; page-break-inside: avoid; }
.sheet .p-stop h3 { font-size: 11pt; color: #a8481a; margin: 0 0 1mm; }
.sheet .p-stop ul { margin: 0; padding-left: 5mm; }
.sheet .p-foot { margin-top: 3mm; font-size: 9.5pt; color: #333; display: flex; justify-content: space-between; gap: 4mm; border-top: 0.3mm solid #999; padding-top: 1.5mm; }
.sheet .p-check { break-before: page; page-break-before: always; }
.sheet .p-check h2 { font-size: 14pt; margin-bottom: 1mm; }
.sheet .p-check p { font-size: 9.5pt; margin: 0 0 2mm; }
.sheet table.cal { width: 99.5%; border-collapse: collapse; table-layout: fixed; font-size: 9pt; }
.sheet table.cal th, .sheet table.cal td { border: 0.3mm solid #666; text-align: center; height: 9mm; padding: 0 0.5mm; overflow: hidden; }
.sheet table.cal th.nm, .sheet table.cal td.nm { width: 44mm; text-align: left; padding: 0 1.5mm; font-size: 8.5pt; line-height: 1.25; }
.sheet table.cal thead th { background: #eef2f6; height: 10mm; line-height: 1.2; font-weight: 600; }
.sheet table.cal .sun { color: #b42318; }
.sheet table.cal .sat { color: #1d4ed8; }
.sheet table.cal tr.cond td { height: 12mm; }
.sheet .cal-wrap + .cal-wrap { margin-top: 5mm; }

@page { size: A4; margin: 11mm 12mm 12mm; }
@media print {
  html, body { background: #fff !important; }
  body > *:not(#print-root) { display: none !important; }
  #print-root { display: block !important; }
  .sheet { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
