/*
 * styles.css — お薬手帳PWA
 * Apple Human Interface Guidelines 準拠のデザイン言語:
 * システムフォント / セマンティックカラー (ライト・ダーク自動) /
 * 半透明マテリアル / 44ptタッチターゲット / 8ptグリッド / Dynamic Type (rem基盤)
 * アダプティブ: <700pt コンパクト / >=700pt 分割ビュー / >=1024pt サイドバー
 */

/* ---------- セマンティックカラー ---------- */
:root {
  --tint: #0d7a72;                    /* ティール (アクセントのみに使用) */
  --tint-press: #095c56;
  --bg: #f2f2f7;                      /* systemGroupedBackground */
  --card: #ffffff;                    /* secondarySystemGroupedBackground */
  --fill: rgba(120, 120, 128, 0.12);  /* systemFill */
  --fill-strong: rgba(120, 120, 128, 0.22);
  --label: #1c1c1e;                   /* label */
  --label-2: rgba(60, 60, 67, 0.6);   /* secondaryLabel */
  --separator: rgba(60, 60, 67, 0.29);/* separator */
  --danger: #d70015;
  --danger-fill: rgba(215, 0, 21, 0.1);
  --ok: #248a3d;
  --ok-fill: rgba(36, 138, 61, 0.12);
  --material: rgba(249, 249, 249, 0.78);
  --radius: 1rem;                     /* 16pt */
  --tabbar-h: 4rem;
}
@media (prefers-color-scheme: dark) {
  :root {
    --tint: #33bfb0;
    --tint-press: #5fd3c4;
    --bg: #000000;
    --card: #1c1c1e;
    --fill: rgba(120, 120, 128, 0.24);
    --fill-strong: rgba(120, 120, 128, 0.36);
    --label: #f2f2f7;
    --label-2: rgba(235, 235, 245, 0.6);
    --separator: rgba(84, 84, 88, 0.65);
    --danger: #ff6961;
    --danger-fill: rgba(255, 105, 97, 0.16);
    --ok: #30d158;
    --ok-fill: rgba(48, 209, 88, 0.16);
    --material: rgba(30, 30, 32, 0.72);
  }
}
@media (prefers-contrast: more) {
  :root {
    --label-2: rgba(60, 60, 67, 0.88);
    --separator: rgba(60, 60, 67, 0.7);
    --material: rgba(249, 249, 249, 0.94);
  }
}
@media (prefers-contrast: more) and (prefers-color-scheme: dark) {
  :root {
    --label-2: rgba(235, 235, 245, 0.9);
    --separator: rgba(235, 235, 245, 0.75);
    --material: rgba(30, 30, 32, 0.94);
  }
}

/* ---------- ベース ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--label);
  line-height: 1.5;
  padding-bottom: calc(var(--tabbar-h) + 2rem + env(safe-area-inset-bottom));
}
:focus-visible { outline: 2px solid var(--tint); outline-offset: 2px; }

/* ---------- ヘッダー (スティッキー・半透明マテリアル) ---------- */
header {
  position: sticky; top: 0; z-index: 20;
  background: var(--material);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 0.5px solid var(--separator);
  padding-top: calc(0.5rem + env(safe-area-inset-top));
  padding-bottom: 0.5rem;
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}
header h1 {
  font-size: 1.75rem; font-weight: 700; letter-spacing: 0.01em;
  margin: 0; display: flex; align-items: center; gap: 0.5rem;
}
header h1 .app-icon { width: 1.75rem; height: 1.75rem; color: var(--tint); flex: none; }
#swStatus { font-size: 0.75rem; color: var(--label-2); white-space: nowrap; }
#swStatus.ready { color: var(--ok); font-weight: 600; }

/* ---------- レイアウト / ビュー ---------- */
main {
  max-width: 45rem; margin: 0 auto; width: 100%;
  padding: 1rem max(1rem, env(safe-area-inset-right)) 1rem max(1rem, env(safe-area-inset-left));
}
.view { display: none; }
.view.active { display: block; }
@media (prefers-reduced-motion: no-preference) {
  .view.active { animation: view-in 0.18s ease-out; }
  @keyframes view-in { from { opacity: 0; } to { opacity: 1; } }
}

/* ---------- カード (Inset Grouped 風) ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}
@media (prefers-contrast: more) {
  .card { border: 1px solid var(--separator); }
}
h2 { font-size: 1.375rem; font-weight: 700; margin: 0.5rem 0 0.75rem; }
h3 { font-size: 1.125rem; font-weight: 600; margin: 1rem 0 0.5rem; }
p.note { font-size: 0.8125rem; color: var(--label-2); line-height: 1.6; }

/* ---------- 記録リスト (グルーピングされた行) ---------- */
#recordList {
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 1rem;
}
#recordList:empty { display: none; }
.record-card {
  margin: 0; border-radius: 0; padding: 0.75rem 1rem;
  border-bottom: 0.5px solid var(--separator);
  cursor: pointer;
}
.record-card:last-child { border-bottom: none; }
@media (prefers-contrast: more) {
  #recordList { border: 1px solid var(--separator); }
  .record-card { border-bottom-width: 1px; }
}
.record-card:active { background: var(--fill); }
.record-card.selected { background: var(--fill); }
.record-date { font-size: 0.8125rem; color: var(--label-2); }
.record-pharmacy { font-size: 1.0625rem; font-weight: 600; margin: 0.125rem 0; }
.record-drugs { font-size: 0.875rem; color: var(--label-2); line-height: 1.5; }
.source-badge {
  font-size: 0.6875rem; border: 1px solid var(--separator); color: var(--label-2);
  border-radius: 999px; padding: 0.125rem 0.5rem; margin-left: 0.375rem; vertical-align: 0.0625rem;
}
.empty { text-align: center; color: var(--label-2); padding: 2rem 1rem; font-size: 0.9375rem; line-height: 1.7; }

/* ---------- ボタン (iOSスタイル) ---------- */
button, .btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 2.75rem;                 /* 44pt */
  padding: 0.625rem 1.125rem;
  border-radius: 0.75rem;
  border: none;
  background: var(--tint);
  color: #fff;
  font-size: 1.0625rem; font-weight: 600;
  font-family: inherit;
  cursor: pointer; text-align: center; text-decoration: none;
  transition: opacity 0.15s ease, background-color 0.15s ease;
}
button:active, .btn:active { opacity: 0.65; }
button.secondary { background: var(--fill); color: var(--tint); }
button.secondary:active { background: var(--fill-strong); opacity: 1; }
button.danger { background: var(--danger-fill); color: var(--danger); }
button.small { min-height: 2.5rem; padding: 0.375rem 0.75rem; font-size: 0.9375rem; }
button:disabled { opacity: 0.4; }
.btn-row { display: flex; gap: 0.625rem; flex-wrap: wrap; margin: 0.75rem 0; }
.back-btn {
  background: none; color: var(--tint); padding-left: 0.25rem;
  font-weight: 500; min-height: 2.75rem;
}
.back-btn:active { opacity: 0.5; }

/* ---------- フォーム (グルーピング) ---------- */
label { display: block; font-size: 0.8125rem; color: var(--label-2); margin: 0.75rem 0 0.25rem; }
input[type="text"], input[type="date"], textarea, select {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.625rem 0.75rem;
  font-size: 1rem;                      /* 16pt: iOSの自動ズーム防止 */
  font-family: inherit;
  border: none; border-radius: 0.625rem;
  background: var(--fill);
  color: var(--label);
}
input[type="file"] { font-size: 0.9375rem; font-family: inherit; color: var(--label); max-width: 100%; }
textarea { min-height: 5.5rem; resize: vertical; }
.drug-row {
  background: var(--fill);
  border-radius: 0.75rem;
  padding: 0.75rem;
  margin-bottom: 0.625rem;
}
.drug-row input[type="text"] { background: var(--card); }
.drug-row .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem; }

/* ---------- 詳細 ---------- */
table.meta { width: 100%; border-collapse: collapse; font-size: 0.9375rem; }
table.meta th, table.meta td { border-bottom: 0.5px solid var(--separator); }
table.meta tr:last-child th, table.meta tr:last-child td { border-bottom: none; }
table.meta th {
  text-align: left; color: var(--label-2); font-weight: 400;
  padding: 0.5rem 0.5rem 0.5rem 0; white-space: nowrap; vertical-align: top;
}
table.meta td { padding: 0.5rem 0; }
.drug-item { border-top: 0.5px solid var(--separator); padding: 0.75rem 0; }
.drug-item:first-of-type { border-top: none; }
.drug-name { font-size: 1rem; font-weight: 600; }
.drug-sub { font-size: 0.875rem; color: var(--label-2); margin-top: 0.125rem; line-height: 1.5; }
.drug-links { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.5rem; }
.drug-links a {
  display: inline-flex; align-items: center;
  min-height: 2.25rem; padding: 0.375rem 0.875rem;
  font-size: 0.8125rem; font-weight: 500; color: var(--tint); text-decoration: none;
  background: var(--fill); border-radius: 999px;
}
.drug-links a:active { background: var(--fill-strong); }

/* ---------- スキャン / メッセージ / 診断 ---------- */
video#scanVideo { width: 100%; border-radius: var(--radius); background: #000; }
.msg { padding: 0.75rem; border-radius: 0.625rem; font-size: 0.875rem; line-height: 1.6; margin: 0.625rem 0; display: none; }
.msg.show { display: block; }
.msg.ok { background: var(--ok-fill); }
.msg.err { background: var(--danger-fill); }
.msg.info { background: var(--fill); }
.diag {
  font-size: 0.75rem; font-family: ui-monospace, "SF Mono", monospace;
  white-space: pre-wrap; word-break: break-all; max-height: 16rem; overflow: auto;
  background: var(--fill);
}

/* ---------- 詳細ペイン (ワイド時のみ表示) ---------- */
.detail-pane { display: none; }
.detail-empty { color: var(--label-2); text-align: center; padding: 2.5rem 1rem; font-size: 0.9375rem; }

/* ---------- タブバー (フローティング・コンパクト幅) ---------- */
nav.tabbar {
  position: fixed; z-index: 20;
  left: max(1rem, env(safe-area-inset-left));
  right: max(1rem, env(safe-area-inset-right));
  bottom: calc(0.75rem + env(safe-area-inset-bottom));
  height: var(--tabbar-h);
  display: flex; gap: 0.25rem;
  padding: 0.375rem;
  background: var(--material);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-radius: 999px;
  box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.18), 0 0 0 0.5px var(--separator);
}
nav.tabbar button {
  flex: 1; min-height: 0; height: 100%;
  background: none; color: var(--label-2);
  font-size: 0.6875rem; font-weight: 500;
  border-radius: 999px; padding: 0.25rem 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.125rem;
}
nav.tabbar button .ico { width: 1.5rem; height: 1.5rem; }
nav.tabbar button:active { opacity: 1; background: var(--fill); }
nav.tabbar button.active { color: var(--tint); background: var(--fill); }

/* =========================================================
   >= 700pt : 分割ビュー (インナーディスプレイ横向き等)
   ========================================================= */
@media (min-width: 700px) {
  main { max-width: 90rem; padding-top: 1.5rem; }
  .split-wrap {
    display: grid;
    grid-template-columns: 21rem minmax(0, 1fr);
    gap: 1.5rem;
    align-items: start;
  }
  .detail-pane { display: block; min-width: 0; }
  #detailPane .view { display: none; }
  #detailPane .view.pane-shown { display: block; }
  #detailPane .back-btn { display: none; }
  /* フォーム・取り込み等は読みやすい中央カラムに */
  #view-manual, #view-scan, #view-import, #view-data {
    max-width: 45rem; margin-left: auto; margin-right: auto; width: 100%;
  }
  video#scanVideo { max-height: 26rem; object-fit: cover; }
}

/* 折り目 (ヒンジ) 位置に列の境目を合わせる — 対応環境のみのプログレッシブエンハンスメント */
@media (min-width: 700px) and (horizontal-viewport-segments: 2) {
  .split-wrap {
    grid-template-columns: env(viewport-segment-width 0 0, 21rem) minmax(0, 1fr);
    column-gap: 0;
  }
  .detail-pane {
    padding-left: calc(env(viewport-segment-left 1 0, 0px) - env(viewport-segment-right 0 0, 0px));
  }
}

/* =========================================================
   >= 1024pt : 左サイドバーナビゲーション
   ========================================================= */
@media (min-width: 1024px) {
  body { padding-left: 15rem; padding-bottom: 0; }
  nav.tabbar {
    left: 0; right: auto; top: 0; bottom: 0;
    width: 15rem; height: auto;
    flex-direction: column; justify-content: flex-start; align-items: stretch;
    gap: 0.25rem;
    padding: calc(1rem + env(safe-area-inset-top)) 0.75rem calc(1rem + env(safe-area-inset-bottom));
    border-radius: 0;
    box-shadow: none;
    border-right: 0.5px solid var(--separator);
  }
  nav.tabbar button {
    flex: none; width: 100%; height: auto; min-height: 2.75rem;
    flex-direction: row; justify-content: flex-start;
    gap: 0.75rem; padding: 0.5rem 0.75rem;
    font-size: 1rem; border-radius: 0.625rem;
  }
  main { max-width: 96rem; }
}

/* ---------- モーション抑制 ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}
