/* ============================================================
   Subkarte Note スタイル
   - iPad Safari 横持ちを基準に、PCブラウザでも崩れない
   - タッチ操作前提: ボタンは44px以上、余白広め
   ============================================================ */

:root {
  --primary: #0e7490;        /* メインカラー（テール系） */
  --primary-dark: #155e75;
  --primary-pale: #ecfeff;
  --bg: #eef2f6;             /* 画面背景 */
  --paper: #ffffff;          /* 用紙 */
  --text: #1f2937;
  --text-sub: #6b7280;
  --border: #d6dee6;
  --danger: #dc2626;
  --warn-bg: #fef9c3;
  --warn-border: #facc15;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  /* 画面全体のバウンススクロールを抑える（キャンバス操作の邪魔になるため） */
  overscroll-behavior: none;
}

.view[hidden] { display: none !important; }

/* ---------- 共通部品 ---------- */

.btn {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation; /* ダブルタップズームを防ぐ */
}
.btn:active { opacity: 0.7; }
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-primary:active { background: var(--primary-dark); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--primary-dark); }
.btn-block { width: 100%; }
.btn-small { min-height: 36px; padding: 0 12px; font-size: 13px; }
.btn[disabled] { opacity: 0.4; cursor: default; }

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sub);
  margin-bottom: 6px;
}
.field .req {
  font-style: normal;
  color: var(--danger);
  font-size: 11px;
  margin-left: 6px;
}
input[type="text"], input[type="email"], input[type="password"], input[type="search"] {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  font-size: 16px; /* iOSの自動ズームを防ぐため16px以上 */
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
}
input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.form-error {
  color: var(--danger);
  font-size: 13px;
  margin: 8px 0;
}

/* トースト通知 */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 100;
  max-width: 90vw;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
}
.toast.toast-error { background: var(--danger); }

/* ---------- ログイン画面 ---------- */

.view-login {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-box {
  width: 100%;
  max-width: 380px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 28px;
  box-shadow: 0 8px 30px rgba(15, 76, 92, 0.08);
}
.app-title {
  margin: 0;
  font-size: 26px;
  color: var(--primary-dark);
  letter-spacing: 0.02em;
  text-align: center;
}
.app-subtitle {
  margin: 4px 0 24px;
  text-align: center;
  color: var(--text-sub);
  font-size: 13px;
}
.login-note {
  margin: 16px 0 0;
  font-size: 12px;
  color: var(--text-sub);
  text-align: center;
}
.config-warning {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 13px;
  margin-bottom: 16px;
}

/* ---------- アプリ共通ヘッダー ---------- */

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 0 16px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.header-title {
  margin: 0;
  font-size: 18px;
  color: var(--primary-dark);
}
.header-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.staff-name { font-size: 13px; color: var(--text-sub); }

/* ---------- 患者一覧 ---------- */

.patients-main {
  max-width: 860px;
  margin: 0 auto;
  padding: 20px 16px 60px;
}
.patients-toolbar {
  display: flex;
  gap: 10px;
}
.patients-toolbar input[type="search"] { flex: 1; }
.patients-toolbar .btn { white-space: nowrap; }

.list-status {
  font-size: 12px;
  color: var(--text-sub);
  margin: 14px 2px 6px;
}
.patient-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.patient-list:empty { display: none; }
.patient-list li {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid #edf1f5;
}
.patient-list li:last-child { border-bottom: none; }
.patient-row {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 8px 14px;
  border: none;
  background: transparent;
  cursor: pointer;
  text-align: left;
  font-size: 15px;
  min-width: 0;
}
.patient-row:active { background: var(--primary-pale); }
.patient-no-chip {
  flex-shrink: 0;
  background: var(--primary-pale);
  color: var(--primary-dark);
  font-weight: 700;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
}
.patient-row .p-name { font-weight: 600; }
.patient-row .p-kana, .patient-row .p-date {
  color: var(--text-sub);
  font-size: 12px;
}
.patient-row .p-date { margin-left: auto; padding-left: 8px; }
.patient-edit-btn {
  border: none;
  border-left: 1px solid #edf1f5;
  background: transparent;
  color: var(--primary-dark);
  font-size: 13px;
  padding: 0 16px;
  cursor: pointer;
}
.patient-edit-btn:active { background: var(--primary-pale); }

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

.dialog {
  border: none;
  border-radius: 16px;
  padding: 24px;
  width: min(420px, 92vw);
  box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
.dialog::backdrop { background: rgba(15, 23, 42, 0.4); }
.dialog h2 { margin: 0 0 18px; font-size: 18px; }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

/* 用紙（テンプレート）選択 */
.dialog-wide { width: min(760px, 94vw); max-height: 90dvh; overflow-y: auto; }
.tpl-group-title {
  margin: 14px 0 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-sub);
}
.tpl-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.tpl-btn {
  border: 2px solid var(--border);
  border-radius: 10px;
  background: #fff;
  padding: 8px 8px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  touch-action: manipulation;
}
.tpl-btn:active { border-color: var(--primary); background: var(--primary-pale); }
.tpl-btn img,
.tpl-btn .tpl-blank {
  width: 100%;
  aspect-ratio: 2100 / 2970;
  object-fit: cover;
  border: 1px solid #eef2f6;
  border-radius: 4px;
  background: #fff;
}
.tpl-btn .tpl-blank {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-sub);
  font-size: 13px;
  border-style: dashed;
}
.tpl-btn span { font-size: 13px; font-weight: 600; color: var(--text); }
@media (max-width: 640px) {
  .tpl-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- サブカルテ画面 ---------- */

.view-karte {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}
.karte-header { flex-shrink: 0; }
.karte-patient-info {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
}
.karte-patient-name { font-weight: 700; font-size: 16px; white-space: nowrap; }
.karte-patient-kana {
  color: var(--text-sub);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.save-status { font-size: 12px; color: var(--text-sub); white-space: nowrap; }
.save-status.saving { color: #b45309; }
.save-status.saved { color: #047857; }
.save-status.save-error { color: var(--danger); font-weight: 700; }

/* 閲覧のみバナー */
.lock-banner {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--warn-bg);
  border-bottom: 1px solid var(--warn-border);
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
}

/* ページタブ */
.page-tabs-row {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 0 8px;
  overflow-x: auto;
}
.page-tabs { display: flex; }
.page-tab {
  min-height: 44px;
  padding: 0 18px;
  border: none;
  background: transparent;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-sub);
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
}
.page-tab.active {
  color: var(--primary-dark);
  border-bottom-color: var(--primary);
}
.btn-page-add { white-space: nowrap; font-size: 13px; }

/* ツールバー */
.karte-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.toolbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tool-group { display: flex; align-items: center; gap: 6px; }
.tool-group-right { margin-left: auto; }
.tool-btn {
  min-width: 44px;
  min-height: 44px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: #f3f6f9;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
}
.tool-btn.active {
  border-color: var(--primary);
  background: var(--primary-pale);
}
.tool-btn[disabled] { opacity: 0.4; cursor: default; }
.color-btn::before {
  content: "";
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--c);
}
.width-btn .dot {
  width: var(--d);
  height: var(--d);
  border-radius: 50%;
  background: #374151;
}
.tool-btn-labeled {
  padding: 0 14px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.finger-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-sub);
  white-space: nowrap;
  min-height: 44px;
  cursor: pointer;
}
.finger-toggle input { width: 20px; height: 20px; }

/* 用紙（キャンバス） */
.canvas-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px;
}
.paper-wrap {
  max-width: 1050px;
  margin: 0 auto;
}
.karte-canvas {
  display: block;
  width: 100%;
  /* A4縦の比率。高さはJS側でも同じ比率で計算する */
  aspect-ratio: 2100 / 2970;
  background: var(--paper);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 2px 10px rgba(15, 76, 92, 0.08);
  /* ペンモード時: 指スワイプはブラウザのスクロールに任せる（JSで切替） */
  touch-action: pan-x pan-y pinch-zoom;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.karte-canvas.eraser-cursor { cursor: cell; }
.karte-canvas.readonly { pointer-events: none; }

/* 閲覧モード時はツールバーを無効化 */
.toolbar.disabled { opacity: 0.45; pointer-events: none; }

/* 狭い画面（縦持ちiPad/スマホ）での調整 */
@media (max-width: 640px) {
  .karte-patient-kana { display: none; }
  .patients-toolbar { flex-direction: column; }
}
