/* ===================================================================
   旅のスタンプ帳 — スマホ縦持ち前提。紙のスタンプ帳の手ざわりを目標にする。
   =================================================================== */

:root {
  --paper:   #f7f3e8;   /* 台紙のクリーム色 */
  --card:    #fffdf7;
  --ink:     #2d2a24;
  --sub:     #857f72;
  --line:    #e2dccb;
  --accent:  #c0392b;   /* 朱肉の赤 */
  --accent-d:#a5301f;
  --danger:  #b3382c;
  --radius:  14px;
  --safe-b:  env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #1e1c19; --card: #262320; --ink: #ece7dc;
    --sub: #9b948a; --line: #3a352e;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* hidden 属性を常に効かせる。
   #fx や .row のように display を指定した規則があると、
   ブラウザ既定の [hidden]{display:none} が負けて隠れなくなるため。 */
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", sans-serif;
  background: var(--paper);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: contain;
}

/* ---------- 上部バー ---------- */
#topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px;
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
#topTitle { flex: 1; font-size: 16px; margin: 0; font-weight: 700; letter-spacing: .04em; }
.icon-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--card); color: var(--ink);
  font-size: 18px; line-height: 1; cursor: pointer;
}
#backBtn { font-size: 24px; padding-bottom: 3px; }

/* ---------- ビュー ---------- */
#main { padding: 14px 14px calc(28px + var(--safe-b)); max-width: 720px; margin: 0 auto; }
.view { display: none; }
.view.on { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } }

.hint { font-size: 13px; color: var(--sub); line-height: 1.6; margin: 0 0 12px; }
.section-title { font-size: 13px; color: var(--sub); margin: 24px 0 10px; font-weight: 600; }
.empty-note { font-size: 13px; color: var(--sub); line-height: 1.9; text-align: center; margin: 40px 0; }

/* ---------- ホーム ---------- */
.stats {
  display: flex; gap: 10px; margin-bottom: 18px;
}
.stat {
  flex: 1; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 10px; text-align: center;
}
.stat b { display: block; font-size: 26px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11px; color: var(--sub); }

.capture-actions { text-align: center; }
.big-btn {
  width: 100%; border: none; border-radius: 20px; cursor: pointer;
  background: linear-gradient(160deg, var(--accent), var(--accent-d));
  color: #fff; font-family: inherit; font-size: 19px; font-weight: 700;
  padding: 26px 16px 20px; letter-spacing: .06em;
  box-shadow: 0 6px 18px rgba(160, 45, 30, .28);
  display: flex; flex-direction: column; align-items: center; gap: 4px;
}
.big-btn:active { transform: scale(.985); box-shadow: 0 3px 10px rgba(160,45,30,.3); }
.big-btn-icon { font-size: 34px; line-height: 1; opacity: .9; }
.big-btn small { font-size: 11px; font-weight: 400; opacity: .8; letter-spacing: .1em; }
.text-btn {
  margin-top: 12px; background: none; border: none; cursor: pointer;
  color: var(--sub); font: inherit; font-size: 13px; text-decoration: underline;
  text-underline-offset: 4px; padding: 6px;
}

/* ---------- グリッド ---------- */
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.grid img, .book img { width: 100%; height: 100%; object-fit: contain; }
.cell {
  aspect-ratio: 1; background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 6px; cursor: pointer; overflow: hidden;
}
.cell:active { transform: scale(.97); }

/* 台紙ビュー */
.book {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 0;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden;
}
.book .slot {
  aspect-ratio: 1; padding: 12px; cursor: pointer;
  border-right: 1px dashed var(--line); border-bottom: 1px dashed var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
}
.book .slot:nth-child(2n) { border-right: none; }
.book .slot img { flex: 1; min-height: 0; }
.book .slot em { font-style: normal; font-size: 10px; color: var(--sub); text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

.filters { display: flex; gap: 7px; overflow-x: auto; padding-bottom: 12px; margin-bottom: 4px; }
.filters button {
  flex-shrink: 0; font: inherit; font-size: 12px; padding: 6px 13px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--card); color: var(--sub); cursor: pointer;
}
.filters button.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- 4隅合わせ ---------- */
.stage { position: relative; touch-action: none; line-height: 0; margin-bottom: 14px; }
.stage canvas { width: 100%; height: auto; border-radius: 10px; display: block; }
#cropQuad { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#cropQuad polygon { fill: rgba(192,57,43,.12); stroke: var(--accent); stroke-width: 2; }
.handle {
  position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  touch-action: none; cursor: grab;
}
.handle::after {
  content: ''; position: absolute; inset: 14px;
  background: var(--accent); border: 2px solid #fff; border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
}

/* 拡大鏡 — 指で隠れる部分を見えるようにする */
#loupe {
  position: absolute; width: 116px; height: 116px; margin: -58px 0 0 -58px;
  border-radius: 50%; overflow: hidden; pointer-events: none; z-index: 30;
  border: 3px solid #fff; box-shadow: 0 3px 14px rgba(0,0,0,.35);
  background: #fff;
}
#loupe canvas { width: 100%; height: 100%; display: block; }

/* ---------- 透過が分かる市松模様 ---------- */
.checker {
  background-color: #fff;
  background-image:
    linear-gradient(45deg, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%),
    linear-gradient(45deg, #ddd 25%, transparent 25%, transparent 75%, #ddd 75%);
  background-size: 18px 18px; background-position: 0 0, 9px 9px;
  border-radius: 10px; overflow: hidden;
}
#adjustWrap canvas, .meta-preview canvas, .detail-stage canvas {
  width: 100%; height: auto; display: block;
}
#adjustWrap.picking canvas { cursor: crosshair; }
.meta-preview { max-width: 190px; margin: 0 auto 18px; }
.detail-stage { margin-bottom: 16px; }

/* ---------- タブ ---------- */
.tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.tabs button {
  flex: 1; font: inherit; font-size: 13px; padding: 9px 4px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--sub); border-radius: 10px;
}
.tabs button.on { background: var(--ink); border-color: var(--ink); color: var(--paper); font-weight: 600; }

/* ---------- ボタン列 ---------- */
.btn-row { display: flex; gap: 9px; margin-top: 14px; }
.btn-row.bottom { margin-top: 22px; }
button:not(.icon-btn):not(.big-btn):not(.text-btn) {
  font: inherit; font-size: 14px; padding: 12px 16px; border-radius: 11px;
  border: 1px solid var(--line); background: var(--card); color: var(--ink); cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.danger  { background: none; border-color: var(--danger); color: var(--danger); }
button.wide { flex: 1; padding: 15px; font-size: 16px; }
button:disabled { opacity: .45; }

/* ---------- つまみ ---------- */
.controls { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-top: 12px; }
.row { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.row:last-child { margin-bottom: 0; }
.row label { font-size: 12px; width: 96px; flex-shrink: 0; color: var(--sub); }
.row input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); }
.row .val { font-size: 12px; color: var(--sub); width: 40px; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- 入力欄 ---------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 12px; color: var(--sub); margin-bottom: 6px; }
.field label small { font-weight: 400; opacity: .75; margin-left: 6px; }
.field input, .field textarea {
  width: 100%; font: inherit; font-size: 16px; padding: 12px;
  border: 1px solid var(--line); border-radius: 11px;
  background: var(--card); color: var(--ink); resize: vertical;
}
.field input:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.tag-suggest { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag-suggest button { font-size: 12px !important; padding: 5px 11px !important; border-radius: 999px !important; color: var(--sub) !important; }

.auto-info {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 16px; font-size: 13px; line-height: 1.9; color: var(--sub);
}
.auto-info b { color: var(--ink); font-weight: 600; }

/* ---------- 詳細 ---------- */
#detailTitle { font-size: 19px; margin: 0 0 12px; }
.detail-list { margin: 0; font-size: 13px; }
.detail-list div { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.detail-list dt { width: 74px; flex-shrink: 0; color: var(--sub); margin: 0; }
.detail-list dd { margin: 0; flex: 1; line-height: 1.7; }
.detail-list a { color: var(--accent); }

/* ---------- 押印アニメーション ---------- */
#fx {
  position: fixed; inset: 0; z-index: 100; display: flex;
  align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--paper) 82%, transparent);
  backdrop-filter: blur(3px);
}
#fxCanvas {
  max-width: 62vw; max-height: 46vh; width: auto; height: auto;
  animation: slam .42s cubic-bezier(.3, 1.7, .5, 1) forwards;
}
@keyframes slam {
  0%   { transform: scale(2.1) rotate(-9deg); opacity: 0; }
  55%  { transform: scale(.94) rotate(1.5deg); opacity: 1; }
  75%  { transform: scale(1.03) rotate(-.5deg); }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}
#fxRing {
  position: absolute; width: 40vw; height: 40vw; border-radius: 50%;
  border: 3px solid var(--accent); opacity: 0;
  animation: ripple .7s ease-out .18s forwards;
}
@keyframes ripple {
  0%   { transform: scale(.5); opacity: .55; }
  100% { transform: scale(1.9); opacity: 0; }
}

/* ---------- トースト ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(28px + var(--safe-b)); transform: translateX(-50%);
  background: var(--ink); color: var(--paper); font-size: 13px;
  padding: 11px 20px; border-radius: 999px; z-index: 200;
  animation: toastIn .2s ease; box-shadow: 0 4px 16px rgba(0,0,0,.2); max-width: 86vw; text-align: center;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- 処理中 ---------- */
.busy { position: relative; pointer-events: none; opacity: .6; }
