/* 配色：メイン白 × サブ緑。外部フォントは使わない（システムフォントのみ） */
:root {
  --bg: #ffffff;
  --surface: #f3f5f4;
  --line: #dfe5e2;
  --text: #1f2a26;
  --muted: #6b7a74;
  --accent: #059669;
  --accent-dark: #047857;
  --accent-soft: #e7f5ef;
  --tabbar-h: 48px;
  --toolbar-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  display: grid;
  grid-template-rows: calc(var(--tabbar-h) + var(--safe-top)) minmax(0, 1fr) calc(var(--toolbar-h) + var(--safe-bottom));
  grid-template-areas: "tabbar" "main" "toolbar";
  height: 100vh;
  height: 100dvh;
}

button { font: inherit; color: inherit; }

/* ---------- タブバー ---------- */
.tabbar {
  grid-area: tabbar;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  padding: var(--safe-top) calc(8px + var(--safe-right)) 0 calc(8px + var(--safe-left));
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  min-width: 0;
}

.brand {
  flex: none;
  align-self: center;
  margin: 0 8px 0 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--accent);
  white-space: nowrap;
}

.tabs {
  display: flex;
  gap: 4px;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 0 1 auto;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 40px;
  max-width: 240px;
  min-width: 96px;
  padding: 0 4px 0 12px;
  border: 1px solid var(--line);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.tab.is-active {
  background: var(--bg);
  color: var(--text);
  box-shadow: inset 0 3px 0 var(--accent);
}
.tab-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 14px;
}
.tab-close, .tab-add {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  cursor: pointer;
  border-radius: 6px;
}
.tab-close { width: 32px; height: 32px; color: var(--muted); }
.tab-close:active { background: var(--line); }
.tab-add {
  flex: none;
  width: 40px;
  height: 40px;
  margin-bottom: 2px;
  color: var(--accent);
}
.tab-add:active { background: var(--accent-soft); }

/* ---------- 本体（連続スクロール） ---------- */
.viewer {
  grid-area: main;
  position: relative;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  /* ブラウザ標準のピンチズームを止め、独自のズーム（transform プレビュー）に任せる */
  touch-action: pan-x pan-y;
  background: #e9edeb;
  outline: none;
  /* padding を付けない：clientWidth をそのまま「画面幅」としてページ幅を割り出すため */
  padding: 0;
}

.viewer-content {
  position: relative;
  will-change: auto;
}

.page {
  position: absolute;
  background: #ffffff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  contain: strict;
}
/* 描画前のプレースホルダ：ページ番号だけ薄く出す（canvas が上に重なる） */
.page::before {
  content: attr(data-page);
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c3ccc8;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}
.page.is-error::before {
  content: "このページは表示できません";
  color: #b45309;
  font-size: 14px;
}
.page-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #ffffff;
}

/* ---------- 空のとき ---------- */
.empty {
  grid-area: main;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
  z-index: 1;
}
.empty[hidden] { display: none; }
.empty-card {
  text-align: center;
  color: var(--accent);
  max-width: 420px;
}
.empty-title { margin: 12px 0 4px; font-size: 20px; font-weight: 600; color: var(--text); }
.empty-text { margin: 0 0 20px; color: var(--muted); font-size: 15px; line-height: 1.6; }

.primary-btn {
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #ffffff;
  padding: 12px 28px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.primary-btn:active { background: var(--accent-dark); }

body.is-dragover .viewer,
body.is-dragover .empty { box-shadow: inset 0 0 0 3px var(--accent); }

/* ---------- ツールバー ---------- */
.toolbar {
  grid-area: toolbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 calc(12px + var(--safe-right)) var(--safe-bottom) calc(12px + var(--safe-left));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.tb-group { display: flex; align-items: center; gap: 4px; }

.tb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 44px;
  height: 44px;
  padding: 0 8px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
}
.tb-btn:active { background: var(--accent-soft); }
.tb-btn:disabled { color: #b9c4bf; cursor: default; background: transparent; }
.tb-btn span { font-size: 15px; font-weight: 600; white-space: nowrap; }
/* ペン・消しゴムのON表示 */
.tb-tool[aria-pressed="true"] { background: var(--accent); color: #ffffff; }
.tb-pen[aria-pressed="true"] { background: #e00000; }

/* ---------- ペン／消しゴムモード ---------- */
/* 描いている間にスクロールしないよう、ページ上のタッチ操作を止める（ページ外の余白ではスクロールできる） */
.viewer.is-pen .page,
.viewer.is-eraser .page {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.viewer.is-pen .page { cursor: crosshair; }
.viewer.is-eraser .page { cursor: cell; }

.tb-page {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  font-variant-numeric: tabular-nums;
  font-size: 16px;
}
.tb-page-input {
  width: 4.5em;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-align: center;
  font: inherit;
  font-size: 16px; /* 16px 未満だと iOS がフォーカス時に拡大する */
  color: var(--text);
  background: var(--bg);
  -moz-appearance: textfield;
}
.tb-page-input::-webkit-outer-spin-button,
.tb-page-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.tb-page-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.tb-page-total { color: var(--muted); white-space: nowrap; }
.tb-zoom {
  min-width: 3.5em;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 15px;
}

/* ---------- 読み込み中 ---------- */
.loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.7);
  z-index: 10;
}
.loading[hidden] { display: none; }
.loading-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  max-width: min(90vw, 520px);
}
.loading-card p { margin: 0; font-size: 15px; overflow-wrap: anywhere; }
.spinner {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 書き出し完了ダイアログ ---------- */
.dialog-card {
  padding: 20px 22px;
  border-radius: 14px;
  background: var(--bg);
  border: 1px solid var(--line);
  width: min(90vw, 420px);
}
.dialog-title { margin: 0 0 6px; font-size: 17px; font-weight: 600; }
.dialog-text { margin: 0 0 18px; color: var(--muted); font-size: 14px; overflow-wrap: anywhere; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; }
.secondary-btn {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  padding: 12px 20px;
  font-size: 16px;
  cursor: pointer;
}

/* 縦向きiPad（幅 〜834px）ではツールバーの文字を一部省いて1行に収める */
@media (max-width: 900px) {
  .tb-label span { display: none; }
  .tb-zoom { display: none; }
  .brand { font-size: 14px; }
}
@media (max-width: 600px) {
  .tb-tool span { display: none; }
  .brand { display: none; }
}
