:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --line: #e3e6ea;
  --text: #1d2430;
  --muted: #6b7380;
  --accent: #2f6fd6;
  --accent-soft: #e8f0fc;
  --danger: #d23b3b;
  --green: #1f8a4c;
  --green-soft: #dff3e6;
  --chat-bg: rgba(246, 247, 249, 0.92);
  --radius: 12px;
  font-family: "Noto Sans JP", "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; }
button { font: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12px; }

.app { max-width: 1400px; margin: 0 auto; padding: 16px; min-height: 100vh; display: flex; flex-direction: column; gap: 12px; }

.topbar, .bottombar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 18px; margin: 0; }
.brand .sub { color: var(--muted); font-size: 12px; }
.nav, .actions { display: flex; align-items: center; gap: 8px; }

.ico { width: 20px; height: 20px; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; vertical-align: -4px; margin-right: 6px; }
.brand .ico { width: 24px; height: 24px; margin: 0; }

.pill { background: #eceef1; border-radius: 999px; padding: 3px 12px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.pill.green { background: var(--green-soft); color: var(--green); }
.pill.blue { background: var(--accent-soft); color: var(--accent); }
.pill.red { background: #fde6e6; color: var(--danger); }

.ghost { background: transparent; border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; color: var(--text); }
.ghost:hover { background: #fff; }
.ghost.small { font-size: 12px; }
.primary { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 8px 16px; font-weight: 600; }
.primary:hover { filter: brightness(1.08); }
.primary:disabled { opacity: .5; cursor: default; }
.primary.big { padding: 12px 28px; font-size: 16px; }

.grid { display: grid; grid-template-columns: 55fr 45fr; gap: 16px; flex: 1; min-height: 0; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head h2 { font-size: 17px; margin: 0; display: flex; align-items: center; }
.side { display: flex; flex-direction: column; max-height: calc(100vh - 130px); min-width: 0; }
.side-body { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 12px; overflow-y: auto; }

/* 診察室 */
.left-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.room { display: flex; flex-direction: column; min-height: 640px; flex: 1; }
.room .card-head h2 { font-size: 14px; }
.stage { position: relative; flex: 1; border-radius: 10px; overflow: hidden; background: #ddd; min-height: 560px; }

/* ---------- ページ内ウィンドウ(windows.js) ---------- */
.win { position: fixed; display: flex; flex-direction: column; overflow: hidden; margin: 0; }
[data-win-handle] { cursor: move; user-select: none; touch-action: none; }
/* 最小化: タイトルバーだけ残す */
.win.min { height: auto !important; }
.win.min > :not([data-win-handle]):not(.win-rz) { display: none !important; }
/* 縁・角のつかみ(見えないが、ホバーでカーソルが変わる) */
.win-rz { position: absolute; z-index: 5; }
.win-rz-n, .win-rz-s { left: 12px; right: 12px; height: 8px; cursor: ns-resize; }
.win-rz-e, .win-rz-w { top: 12px; bottom: 12px; width: 8px; cursor: ew-resize; }
.win-rz-n { top: 0; } .win-rz-s { bottom: 0; } .win-rz-e { right: 0; } .win-rz-w { left: 0; }
.win-rz-ne, .win-rz-nw, .win-rz-se, .win-rz-sw { width: 14px; height: 14px; }
.win-rz-ne { top: 0; right: 0; cursor: nesw-resize; } .win-rz-sw { bottom: 0; left: 0; cursor: nesw-resize; }
.win-rz-nw { top: 0; left: 0; cursor: nwse-resize; } .win-rz-se { bottom: 0; right: 0; cursor: nwse-resize; }
.win.min .win-rz-n, .win.min .win-rz-s, .win.min .win-rz-ne, .win.min .win-rz-nw, .win.min .win-rz-se, .win.min .win-rz-sw { display: none; }
body.win-dragging, body.win-dragging * { user-select: none !important; }
body.win-dragging iframe { pointer-events: none; }
.win-btn { background: none; border: 0; border-radius: 6px; padding: 3px; display: flex; }
.win-btn:hover { background: #e3e7ec; }
.win-btn svg { width: 18px; height: 18px; fill: none; stroke: var(--text); stroke-width: 2; stroke-linecap: round; }
.win-tools { display: flex; align-items: center; gap: 6px; }

/* ウィンドウのときだけ出す部品(タイトルバー・最小化ボタン) */
.win-only { display: none; }
.win .win-only { display: flex; }
/* 診察室・右の列がウィンドウになると本体の格子は空くので、大きさだけ残して初期配置の基準にする */
.grid.win-host { height: calc(100vh - 130px); }

/* 診察室ウィンドウ: 見出しがタイトルバー。サイズを縮めたら画像とチャットも一緒に縮む */
.room.win { min-height: 0; box-shadow: 0 6px 20px rgba(20, 26, 36, .12); }
.room.win .stage { min-height: 0; }
.room.win.min .card-head { margin-bottom: 0; }

/* 右の列(過去カルテ・実施済み検査・問診票)のウィンドウ: 3枚まとめて1つ。中身はウィンドウ内でスクロール */
.side.win { max-height: none; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 6px 20px rgba(20, 26, 36, .12); gap: 0; }
.side-bar { align-items: center; justify-content: space-between; padding: 6px 8px 6px 14px; background: #f3f5f8; border-bottom: 1px solid var(--line);
  font-size: 13px; font-weight: 700; flex-shrink: 0; }
.side.win.min .side-bar { border-bottom: 0; }
.side.win .side-body { padding: 10px; }

/* 本日のカルテ記載ウィンドウ */
.note-win { background: var(--card); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 12px 32px rgba(20, 26, 36, .22); }
.note-bar { display: flex; align-items: center; gap: 10px; padding: 8px 10px 8px 14px; background: #f3f5f8; border-bottom: 1px solid var(--line); }
.note-bar h2 { font-size: 14px; margin: 0; display: flex; align-items: center; white-space: nowrap; }
.note-bar #noteStatus { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-tools { display: flex; align-items: center; gap: 6px; }
.note-body { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 8px 10px 10px; }
#chartNote { flex: 1; min-height: 0; width: 100%; font: inherit; font-size: 13px; line-height: 1.6; border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; resize: none; color: var(--text); background: #fff; }
#chartNote:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
#chartNote[readonly] { background: #f6f7f9; }
.note-hint { margin: 4px 0 0; font-size: 11px; }
.note-win.min #soapBtn { display: none; }
.note-win.min .note-bar { border-bottom: 0; }
.portrait { position: absolute; inset: 0; }
.portrait img, .portrait svg { width: 100%; height: 100%; object-fit: cover; display: block; }
.portrait img { position: absolute; inset: 0; }
.portrait img.fill { filter: blur(24px) brightness(.9); transform: scale(1.15); }
/* 横幅いっぱい(cover)だと顔のドアップになるので、高さ基準で少し引いて上寄せ。下はチャット欄に隠れる */
.portrait img.photo { width: auto; height: 125%; left: 50%; right: auto; transform: translateX(-50%); object-fit: initial; }
.gen-badge { position: absolute; top: 12px; right: 12px; background: rgba(20,26,36,.6); color: #fff; border-radius: 8px; padding: 4px 10px; font-size: 12px; }
.patient-tag {position: absolute; top: 12px; left: 12px; background: rgba(255,255,255,.85); border-radius: 8px; padding: 4px 10px; font-size: 13px; font-weight: 600; }
.patient-tag:empty { display: none; }

/* チャットは画像の上に重ねるだけ(白い帯は敷かない)。下端だけ薄く暗くして入力欄を読みやすく */
/* 診察室の全面に重ねる。上は患者名タグの下まで使う */
.chat { position: absolute; inset: 0; padding: 52px 10px 10px; display: flex; flex-direction: column; gap: 6px;
  background: linear-gradient(to top, rgba(10, 14, 20, .35), rgba(10, 14, 20, 0) 40%); pointer-events: none; }
.chat > * { pointer-events: auto; }
.chat-log { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding: 20px 2px 2px;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 24px); mask-image: linear-gradient(to bottom, transparent 0, #000 24px);
  scrollbar-width: thin; scrollbar-color: rgba(255,255,255,.5) transparent; }
/* メッセージが少ないうちは下に寄せる(justify-content: flex-end だとスクロールが壊れるので margin で) */
.chat-log > :first-child { margin-top: auto; }
/* スクロールする縦並びの中でバブルが縮んで重ならないように */
.chat-log > * { flex-shrink: 0; }
.msg { max-width: 85%; padding: 8px 12px; border-radius: 10px; line-height: 1.55; }
.chat-log .msg { max-width: min(68%, 460px); }
.msg .who { display: block; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.msg.patient { background: var(--chat-bg); border: 1px solid var(--line); align-self: flex-start; font-size: 15px; }
.msg.doctor { background: var(--accent-soft); align-self: flex-end; }
.stage .msg { box-shadow: 0 2px 8px rgba(0, 0, 0, .12); backdrop-filter: blur(6px); }
.stage .msg.patient { background: rgba(255, 255, 255, .9); border-color: transparent; }
.stage .msg.doctor { background: rgba(232, 240, 252, .92); }
.msg.system { background: #fff8e6; border: 1px solid #f1e2b5; align-self: stretch; max-width: 100%; font-size: 13px; }
.chat-log .msg.system { max-width: min(80%, 520px); align-self: flex-start; }
.msg.system table { width: 100%; border-collapse: collapse; margin-top: 4px; }
.msg.system td { padding: 2px 4px; border-bottom: 1px solid #f1e2b5; }
.msg.typing { color: var(--muted); font-style: italic; }

.modes { display: flex; gap: 6px; }
.modes button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 3px 12px; font-size: 12px; color: var(--muted); }
.modes button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chat-input { display: flex; align-items: center; gap: 8px; background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 6px 8px; }
.chat-input { align-items: flex-end; }
.chat-input input, .chat-input textarea { flex: 1; border: 0; outline: 0; font: inherit; font-size: 14px; padding: 4px; background: transparent; }
/* 1行から始めて、折り返し・改行に合わせて最大4行まで伸ばす。対応ブラウザは CSS だけで伸び、それ以外は JS(fitTextarea)で伸ばす */
.chat-input textarea { resize: none; line-height: 1.5; overflow-y: auto; display: block; white-space: pre-wrap; overflow-wrap: anywhere;
  field-sizing: content; min-height: calc(1.5em + 8px); max-height: calc(1.5em * 4 + 8px); }
.icon-btn { background: none; border: 0; padding: 4px; display: flex; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn.send svg { stroke: var(--accent); }
.icon-btn.rec svg { stroke: var(--danger); }

/* 右側 */
.body { font-size: 13px; line-height: 1.6; }
.meta { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.body h3 { font-size: 13px; margin: 12px 0 4px; padding-top: 10px; border-top: 1px solid var(--line); }
.body h3:first-child { border-top: 0; padding-top: 0; margin-top: 0; }
.body ul { margin: 0; padding-left: 18px; }
.tag-list { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0; }
.tag-list span { background: #f0f2f5; border-radius: 6px; padding: 1px 8px; font-size: 12px; }
table.labs { width: 100%; border-collapse: collapse; font-size: 12px; }
table.labs th { text-align: left; color: var(--muted); font-weight: 500; padding: 4px 6px; border-bottom: 1px solid var(--line); }
table.labs td { padding: 6px; border-bottom: 1px solid #f0f1f3; }
table.labs td:first-child { color: #8a3b3b; }
table.labs td.now { font-weight: 700; }
/* セットの見出し行と、基準範囲外の値 */
table.labs tr.panel-row td { background: #f3f5f8; color: var(--muted); font-size: 11px; font-weight: 700; padding: 3px 6px; }
table.labs td.flag-H { color: var(--danger); }
table.labs td.flag-L { color: var(--accent); }
table.labs td.prev.flag-H, table.labs td.prev.flag-L { opacity: .75; }
table.labs .hl { font-size: 10px; margin-left: 2px; }
table.labs.reports { margin-top: 10px; }
table.labs.reports td { vertical-align: top; }
table.labs.reports td.now { font-weight: 600; }
td.up { color: var(--danger); }
td.down { color: var(--accent); }
.vitals { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 8px; font-size: 13px; }
.vitals b { font-weight: 600; }
.empty { color: var(--muted); font-size: 13px; }
dl.q { display: grid; grid-template-columns: 7em 1fr; gap: 4px 10px; margin: 0; }
dl.q dt { color: var(--muted); }
dl.q dd { margin: 0; }

/* それ以前の受診記録 */
details.visit { border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; margin: 4px 0; background: #fafbfc; }
details.visit summary { cursor: pointer; font-size: 12px; font-weight: 600; }
details.visit[open] { background: #fff; }

/* 検査歴: 回数が多いと横スクロール。項目の列は固定 */
.table-scroll { overflow-x: auto; max-width: 100%; }
table.labs.history { white-space: nowrap; }
table.labs.history th, table.labs.history td { padding: 4px 8px; }
table.labs.history th:first-child, table.labs.history td:first-child { position: sticky; left: 0; background: var(--card); z-index: 1; }
table.labs.history tr.panel-row td:first-child { position: sticky; left: 0; background: #f3f5f8; }

/* 前医のカルテ記載: S/O/A/P ごとの改行をそのまま見せる */
.body p.soap { white-space: pre-wrap; margin: 6px 0; line-height: 1.7; }

/* 薬剤名 → KEGG MEDICUS へのリンク */
a.drug-link { color: inherit; text-decoration: underline dotted var(--accent); text-underline-offset: 3px; }
a.drug-link:hover { color: var(--accent); text-decoration-style: solid; }

/* 履歴 */
.day { border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.day-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
table.day-cases td:first-child { color: var(--muted); }
table.day-cases tr.clickable { cursor: pointer; }
table.day-cases tr.clickable:hover td { background: var(--accent-soft); }

/* モーダル */
.overlay { position: fixed; inset: 0; background: rgba(20, 26, 36, .45); display: flex; flex-direction: column; align-items: center; justify-content: center; z-index: 50; padding: 16px; }
.modal { background: #fff; border-radius: 14px; padding: 24px; max-width: 560px; width: 100%; max-height: 90vh; overflow-y: auto; }
.modal.wide { max-width: 860px; }
.modal.start h2 { margin-top: 0; }
.modal.start ul { padding-left: 20px; line-height: 1.7; color: #3b4350; }
.plan-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.plan-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 600; }
.plan-form textarea { font: inherit; font-size: 13px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; resize: vertical; color: var(--text); }
.form-actions { grid-column: 1 / -1; display: flex; justify-content: flex-end; align-items: center; gap: 12px; }
.plan-form textarea[readonly] { background: #f6f7f9; }
.plan-form { align-content: start; }
.disposition { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.disposition .disp-label { font-weight: 700; font-size: 13px; margin-right: 4px; }
.plan-form .disposition label { display: inline-block; cursor: pointer; }
.disposition input { position: absolute; opacity: 0; pointer-events: none; }
.disposition label span { display: inline-block; border: 1px solid var(--line); border-radius: 999px; padding: 6px 18px; font-weight: 600; font-size: 14px; color: var(--muted); background: #fff; }
.disposition input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.disposition input[value="admit"]:checked + span { background: var(--danger); border-color: var(--danger); }
.disposition input[value="close"]:checked + span { background: var(--green); border-color: var(--green); }
.disposition input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.disposition input:disabled + span { cursor: default; opacity: .85; }
.disp-empty { grid-column: 1 / -1; margin: 0; }
.modal.wide { max-width: 1180px; }
.plan-layout { display: grid; grid-template-columns: 1fr 360px; gap: 16px; align-items: stretch; }
/* 「.mentor」は指導医の吹き出しのクラスなので、パネル側は別名にする */
.mentor-panel { display: flex; flex-direction: column; gap: 8px; background: #f6f8fb; border: 1px solid var(--line); border-radius: 10px; padding: 10px; min-height: 360px; }
.mentor-head { display: flex; flex-direction: column; gap: 2px; }
.mentor-log { flex: 1; max-height: 420px; min-height: 200px; }
.mentor-log .msg { max-width: 92%; }
.msg.mentor {background: #fff; border: 1px solid var(--line); align-self: flex-start; }
.msg.resident { background: var(--accent-soft); align-self: flex-end; }
.msg { white-space: pre-wrap; }
.revision-note { background: #fff8e6; border: 1px solid #f1e2b5; border-radius: 8px; padding: 6px 10px; font-size: 12px; }
.revisions { background: #fff8e6; border: 1px solid #f1e2b5; border-radius: 10px; padding: 8px 14px; margin-top: 8px; font-size: 13px; }
.revisions ul { margin: 4px 0 0; }

.eval .score { font-size: 40px; font-weight: 700; color: var(--accent); }
.eval h3 { font-size: 14px; margin: 16px 0 6px; }
.eval ul { margin: 0; padding-left: 20px; line-height: 1.65; }
.eval .model { background: #f6f8fb; border-radius: 10px; padding: 12px 14px; }
.eval .model dt { font-weight: 600; font-size: 12px; color: var(--muted); margin-top: 6px; }
.eval .model dd { margin: 0; white-space: pre-wrap; }
/* 覚えるべき判断のマーカー */
mark.key { background: linear-gradient(transparent 50%, #ffe27a 50%); color: inherit; font-weight: 700; padding: 0 2px; }
/* 指摘の濃淡: 臨床判断は目立たせ、記載の指摘は控えめに */
.eval ul.fb { list-style: none; padding-left: 0; }
.eval ul.fb li { padding: 5px 8px 5px 10px; margin: 3px 0; border-left: 3px solid transparent; border-radius: 4px; }
.eval ul.fb li.lv-core { border-left-color: var(--danger); background: #fff5f4; }
.eval ul.fb li.lv-normal { border-left-color: var(--line); }
.eval ul.fb li.lv-minor { color: var(--muted); font-size: 12px; padding-top: 2px; padding-bottom: 2px; }
.lv-badge { display: inline-block; font-size: 10px; font-weight: 700; border-radius: 4px; padding: 0 5px; margin-right: 6px; vertical-align: 1px; }
.lv-badge.core { background: var(--danger); color: #fff; }
.lv-badge.minor { background: #e6e9ee; color: var(--muted); }
.eval .sub-h { font-size: 12px; font-weight: 600; color: var(--muted); margin: 6px 0 2px; }
.eval .model-chart { background: #f6f8fb; border-radius: 10px; padding: 8px 14px; margin-top: 8px; }
.eval .model-chart summary { cursor: pointer; font-weight: 600; font-size: 13px; }
.eval .model-chart pre { white-space: pre-wrap; font: inherit; font-size: 13px; line-height: 1.6; margin: 8px 0 0; }
.eval .tp { background: var(--green-soft); border-radius: 10px; padding: 10px 14px; margin-top: 14px; }

#loading p { color: #fff; margin-top: 14px; }
.spinner { width: 42px; height: 42px; border: 4px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 900px) {
  .grid { grid-template-columns: 1fr; }
  .side { max-height: none; }
  .plan-form { grid-template-columns: 1fr; }
  .plan-layout { grid-template-columns: 1fr; }
  .brand .sub { display: none; }
}
