:root {
  --c-bg: #f4f5f7;
  --c-surface: #ffffff;
  --c-text: #1f2430;
  --c-text-dim: #6b7280;
  --c-primary: #2563eb;
  --c-primary-dark: #1d4ed8;
  --c-danger: #dc2626;
  --c-border: #e2e5ea;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.12);
  --appbar-h: 52px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
  background: var(--c-bg);
  color: var(--c-text);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.appbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--appbar-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px;
  background: var(--c-primary); color: #fff;
  padding-top: env(safe-area-inset-top);
}
.appbar-title { color: #fff; text-decoration: none; font-weight: 700; font-size: 18px; }
.appbar-action { color: #fff; text-decoration: none; font-size: 22px; padding: 6px; }

#view { padding: 14px; max-width: 720px; margin: 0 auto; padding-bottom: 90px; }

h2 { font-size: 17px; margin: 6px 0 12px; }
.dim { color: var(--c-text-dim); font-size: 13px; }
.note { color: var(--c-text-dim); font-size: 12px; line-height: 1.6; margin: 8px 0; }

button, .btn {
  appearance: none; border: none; border-radius: var(--radius);
  font: inherit; cursor: pointer; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 16px; font-size: 15px; font-weight: 600;
  background: var(--c-surface); color: var(--c-text);
  box-shadow: var(--shadow);
  min-height: 46px;
}
button:disabled { opacity: .45; cursor: default; }
.btn-primary { background: var(--c-primary); color: #fff; }
.btn-danger { color: var(--c-danger); }
.btn-block { display: flex; width: 100%; margin: 10px 0; }
.btn-big { padding: 18px 16px; font-size: 17px; }
.btn-ghost { background: transparent; box-shadow: none; color: var(--c-primary); }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 15;
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--c-primary); color: #fff; font-size: 30px; font-weight: 400;
  box-shadow: 0 4px 12px rgba(37,99,235,.45);
}

/* ---------- home ---------- */
.doc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.doc-card {
  background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; cursor: pointer; position: relative;
}
.doc-card .thumbwrap {
  height: 150px; background: #eceef1;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.doc-card img { width: 100%; height: 100%; object-fit: cover; }
.doc-card .thumbwrap .placeholder { font-size: 40px; color: #c3c8d0; }
.doc-meta { padding: 8px 10px; }
.doc-title { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-sub { font-size: 11px; color: var(--c-text-dim); margin-top: 2px; }
.doc-menu-btn {
  position: absolute; top: 4px; right: 4px; min-height: 0;
  padding: 4px 10px; border-radius: 8px; background: rgba(255,255,255,.9); font-size: 16px;
}
.empty-state { text-align: center; color: var(--c-text-dim); padding: 60px 20px; }
.empty-state .icon { font-size: 56px; margin-bottom: 12px; }

/* ---------- capture ---------- */
.capture-actions { margin-top: 30px; }
.progress-banner {
  background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; text-align: center; font-weight: 600; margin-bottom: 12px;
}

/* ---------- corners ---------- */
.corners-stage {
  position: relative; background: #22262e; border-radius: var(--radius);
  overflow: hidden; touch-action: none; user-select: none;
  display: flex; align-items: center; justify-content: center;
}
.corners-stage canvas { display: block; max-width: 100%; }
.corner-handle {
  position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  border-radius: 50%; touch-action: none;
  display: flex; align-items: center; justify-content: center;
}
.corner-handle::after {
  content: ""; width: 18px; height: 18px; border-radius: 50%;
  background: var(--c-primary); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.5);
}
.toolbar { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.toolbar > * { flex: 1; min-width: 120px; }

/* ---------- editor ---------- */
.page-stage {
  background: #22262e; border-radius: var(--radius); overflow: hidden;
  display: flex; align-items: center; justify-content: center; min-height: 300px;
}
.page-stage img, .page-stage canvas { max-width: 100%; max-height: 62vh; display: block; }
.thumb-strip {
  display: flex; gap: 8px; overflow-x: auto; padding: 10px 2px; align-items: center;
}
.thumb-strip .pthumb {
  flex: 0 0 auto; width: 56px; height: 72px; border-radius: 8px; overflow: hidden;
  border: 2px solid transparent; background: #eceef1; position: relative;
}
.thumb-strip .pthumb.active { border-color: var(--c-primary); }
.thumb-strip .pthumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb-strip .pthumb .pnum {
  position: absolute; left: 2px; bottom: 2px; background: rgba(0,0,0,.55); color: #fff;
  font-size: 10px; border-radius: 4px; padding: 0 4px;
}
.thumb-strip .pthumb-add {
  flex: 0 0 auto; width: 56px; height: 72px; border-radius: 8px;
  border: 2px dashed #c3c8d0; background: none; color: var(--c-text-dim); font-size: 24px;
  box-shadow: none; min-height: 0;
}
.filter-row { display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px; }
.filter-row button {
  flex: 0 0 auto; padding: 8px 14px; min-height: 0; font-size: 13px; border-radius: 20px;
}
.filter-row button.active { background: var(--c-primary); color: #fff; }
.editor-title { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.editor-title h2 { margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- signature ---------- */
.sig-canvas-wrap {
  background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; touch-action: none;
}
.sig-canvas-wrap canvas { display: block; width: 100%; height: 220px; }
.sig-list { display: flex; gap: 10px; overflow-x: auto; padding: 8px 2px; }
.sig-item {
  flex: 0 0 auto; height: 60px; padding: 4px; background: var(--c-surface);
  border-radius: 8px; box-shadow: var(--shadow); border: 2px solid transparent; position: relative;
}
.sig-item.active { border-color: var(--c-primary); }
.sig-item img { height: 100%; }
.place-stage {
  position: relative; background: #22262e; border-radius: var(--radius); overflow: hidden;
  touch-action: none; user-select: none;
  display: flex; align-items: center; justify-content: center;
}
.place-stage canvas { display: block; max-width: 100%; max-height: 60vh; }
.slider-row { display: flex; align-items: center; gap: 10px; margin: 10px 0; }
.slider-row label { font-size: 13px; color: var(--c-text-dim); width: 52px; }
.slider-row input[type=range] { flex: 1; }

/* ---------- export / settings ---------- */
.card {
  background: var(--c-surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; margin-bottom: 12px;
}
.card h3 { margin: 0 0 8px; font-size: 15px; }
.kv { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; }
.kv .k { color: var(--c-text-dim); }
textarea.ocr-out {
  width: 100%; min-height: 160px; border: 1px solid var(--c-border); border-radius: 8px;
  font: 13px/1.6 inherit; padding: 8px;
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%); z-index: 100;
  background: #1f2430; color: #fff; border-radius: 10px; padding: 12px 16px;
  font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,.35);
  display: flex; gap: 12px; align-items: center; max-width: 90vw;
}
/* display:flex が hidden属性のUAスタイルに勝ってしまうため明示的に隠す */
.toast[hidden] { display: none; }
.toast button {
  background: none; box-shadow: none; color: #7cb0ff; padding: 4px 8px; min-height: 0;
}

/* ---------- action sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--c-surface); border-radius: 16px 16px 0 0;
  width: 100%; max-width: 480px;
  padding: 10px 14px calc(14px + env(safe-area-inset-bottom));
}
.sheet .sheet-title {
  text-align: center; font-size: 13px; color: var(--c-text-dim); padding: 6px 0 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sheet button { display: flex; width: 100%; margin: 8px 0; }

.spinner {
  display: inline-block; width: 18px; height: 18px;
  border: 3px solid rgba(0,0,0,.15); border-top-color: var(--c-primary);
  border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -4px;
}
@keyframes spin { to { transform: rotate(360deg); } }
