/* ベスト8 — モダンCSS(ネスト / :has() / oklch / @starting-style)前提。2024年以降の主要ブラウザ対応 */

* { box-sizing: border-box; margin: 0; padding: 0; }

/* クラスの display 指定が UA の [hidden] より強いため、明示的に打ち消す */
[hidden] { display: none !important; }

:root {
  --bg: oklch(0.975 0.004 250);
  --panel: oklch(1 0 0);
  --line: oklch(0.9 0.008 250);
  --text: oklch(0.25 0.02 260);
  --muted: oklch(0.55 0.02 260);
  /* ロゴの蜂蜜色。塗り・枠線用 */
  --accent: #d4a537;
  /* 明地に載る文字用。--accent は白背景で可読性が足りないため濃くする */
  --accent-text: oklch(0.52 0.11 80);
  --accent-soft: color-mix(in oklch, var(--accent) 16%, white);
  /* ブランドの墨色。主ボタンの地色 */
  --ink: #1c2230;
  --primary-text: #e5c05c;
  --danger: oklch(0.55 0.19 25);
}

/* ポケカ版は白・黒を土台に、明快な青と黄を小さく効かせる。 */
:root[data-game="pokemon"] {
  --bg: #fff;
  --panel: #fff;
  --line: #c9c9c5;
  --text: #222220;
  --muted: #686863;
  --accent: #4aa6d8;
  --accent-text: #197fae;
  --accent-soft: #e3f4fb;
  --ink: #262522;
  --primary-text: #fff;
  --danger: #d93a50;
  --pokemon-yellow: #f2ce36;
}

body {
  font-family: "Hiragino Sans", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.7;
}

/* 見出しは盤面（＝書き出し画像）が兼ねるため、h1 は読み上げ専用にする */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

/* ───── カード一括切替: 未登録時も位置を保ち、ボタン側を無効化する ───── */
.bulk-bar {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.bulk-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;

  & + .bulk-group {
    border-inline-start: 1px solid var(--line);
    padding-inline-start: 18px;
  }
}

.label { font-size: 0.76rem; color: var(--muted); margin-inline-end: 4px; }

.handle-input {
  flex: 0 1 200px;
  width: 200px;
  max-width: 100%;
  min-width: 0;
  font: inherit;
  font-size: 0.85rem;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel);

  &:focus { outline: none; border-color: var(--accent); }
  &::placeholder { color: color-mix(in oklch, var(--muted) 70%, transparent); }
}

/* ───── 盤面 = プレイマット(書き出しと同じ背景を敷く) ───── */
#board {
  --mat: linear-gradient(170deg, oklch(0.32 0.03 260), oklch(0.24 0.03 260));
  /* 書き出し画像と同じ比率にして、そのまま完成見本として見せる。
     内部の配置は src/layout.js の計算を JS が %で流し込む */
  aspect-ratio: var(--board-ar, 1);
  container-type: size;
  position: relative;
  overflow: hidden; /* ぼかしのはみ出しをクリップ */
  border-radius: 20px;
  box-shadow: 0 16px 48px oklch(0.2 0.02 260 / 0.25);

  /* マットは擬似要素に敷き、ぼかしをかけられるようにする。
     縁のにじみ対策で外側へ広げてから blur する */
  &::before {
    content: "";
    position: absolute;
    /* ぼかしの分だけ外へ広げて、縁がにじんで白く抜けるのを防ぐ */
    inset: calc(-2 * var(--mat-blur, 0px) - 4px);
    background: var(--mat) #fff;
    filter: blur(var(--mat-blur, 0px));
    z-index: 0;
  }

  & > * { position: relative; z-index: 1; }

  /* 画像マット（白地×透過）のときは案内表示を暗色に切り替える */
  &.light {
    /* 明るいマットの上では、薄い金だと飛ぶので濃い金にする */
    & .cell.empty, & .icon-cell .spot {
      border-color: color-mix(in oklch, var(--accent-text) 60%, transparent);
      background: oklch(0 0 0 / 0.04);
    }
    & .cell.empty:hover, & .icon-cell:hover .spot, & .icon-cell.dropping .spot {
      border-color: var(--accent-text);
      background: oklch(0 0 0 / 0.08);
    }
    & .plus { color: color-mix(in oklch, var(--accent-text) 80%, transparent); }
    & .hint { color: oklch(0 0 0 / 0.5); }

    /* 明るいマットでは文字を墨色に反転し、白のにじみで浮かせる */
    & #boardTitle, & #boardCredit {
      color: #262b38;
      text-shadow: 0 0 0.4em oklch(1 0 0 / 0.65);
    }
    & #boardTitle .bt-mark { color: #97701a; }
    & #boardTitle .bt-logo .bt-logo-gold { fill: #b8891f; }
    & .icon-cell .icon-handle {
      color: #262b38;
      text-shadow: 0 0 0.5em oklch(1 0 0 / 0.65);
    }
  }
}

:root[data-game="pokemon"] #board {
  border: 2px solid #262522;
  border-radius: 16px;
  box-shadow: 0 8px 0 rgb(38 37 34 / 0.16);

  & #boardTitle, & #boardCredit {
    text-shadow: none;
  }
}

:root[data-game="pokemon"] #board.light {
  & #boardTitle, & #boardCredit { color: #222220; }
  & #boardTitle .bt-mark { color: #222220; }
  & #boardTitle .bt-logo { color: #222220; }
  & #boardTitle .bt-logo .bt-logo-gold { fill: var(--pokemon-yellow); }
  & .cell.empty, & .icon-cell .spot {
    border: 2px solid rgb(34 34 32 / 0.78);
    background: #fff;
  }
  & .cell.empty:hover, & .icon-cell:hover .spot, & .icon-cell.dropping .spot {
    border-color: #222220;
    background: var(--pokemon-yellow);
  }
  & .plus { color: #222220; }
  & .hint { color: rgb(34 34 32 / 0.7); }
}

#grid {
  position: absolute;
  inset-inline-start: var(--grid-x, 0);
  inset-block-start: var(--grid-y, 0);
  width: var(--grid-w, 100%);
  height: var(--grid-h, 100%);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  column-gap: var(--grid-gap-x, 2%);
  row-gap: var(--grid-gap-y, 2%);
}

/* 書き出し画像と同じ位置に出す見出しとクレジット */
#boardTitle, #boardCredit {
  position: absolute;
  inset-inline: 4%;
  text-align: center;
  color: #fff;
  text-shadow: 0 0 0.4em oklch(0.2 0.02 260 / 0.55);
  pointer-events: none;
}

/* ブランドロックアップ: ロゴ + ワードマーク + 版ラベル + タグライン。
   書き出し(src/export.js)と同じ構成・比率で描く */
#boardTitle {
  inset-block-start: 0;
  height: var(--header-h, 9.5%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1cqh;

  & .bt-lockup {
    display: flex;
    align-items: center;
    gap: 1.2cqh;
  }
  & .bt-logo {
    width: 3.1cqh;
    height: 3.1cqh;
    /* Cinzel の大文字は行ボックス内で視覚中心が上寄りなので光学補正 */
    transform: translateY(-0.3cqh);
    & .bt-logo-gold { fill: #e2b64f; }
  }
  & .bt-mark {
    font-family: "Cinzel", serif;
    font-weight: 700;
    font-size: 3.5cqh;
    line-height: 1;
    letter-spacing: 0.08em;
    color: #e2b64f;
  }
  :root[data-game="pokemon"] & .bt-mark {
    isolation: isolate;
    position: relative;
    font-family: "Dela Gothic One", "Hiragino Kaku Gothic ProN", sans-serif;
    font-weight: 400;
    letter-spacing: 0.025em;
    color: #222220;

    /* 外側から黒の細線→白の太線→黒文字。白背景でも色面でも輪郭を保つ。 */
    &::before, &::after {
      content: attr(data-text);
      position: absolute;
      inset: 0;
      color: transparent;
      paint-order: stroke fill;
      pointer-events: none;
    }
    &::before {
      z-index: -2;
      -webkit-text-stroke: 0.44cqh #222220;
    }
    &::after {
      z-index: -1;
      -webkit-text-stroke: 0.28cqh #fff;
    }
  }
  & .bt-edition {
    display: inline-grid;
    place-content: center;
    min-height: 2cqh;
    padding: 0.2cqh 0.7cqh 0.1cqh;
    margin-inline-start: 0.2cqh;
    border: 0.12cqh solid currentColor;
    border-radius: 999px;
    font-size: 1.15cqh;
    font-weight: 700;
    line-height: 1;
    letter-spacing: 0.12em;
    text-indent: 0.12em;
    opacity: 0.9;
    /* 小さいカプセルは幾何学中心だと下へ沈んで見える */
    transform: translateY(-0.3cqh);
  }
  & .bt-tag {
    font-size: 1.3cqh;
    font-weight: 500;
    line-height: 1;
    letter-spacing: 0.34em;
    text-indent: 0.34em; /* 字間が末尾に付くぶんの視覚補正 */
    opacity: 0.92;
  }
}

#boardCredit {
  inset-block-end: 0;
  height: var(--footer-h, 4.5%);
  display: grid;
  place-content: center;
  font-size: 1.5cqh;
  opacity: 0.85;
  text-wrap: nowrap;
}

.cell {
  position: relative;
  aspect-ratio: 63 / 88;
  border: 0;
  overflow: hidden;
  font: inherit;
  color: inherit;
  background: transparent;
  /* 中の文字をセルの大きさに合わせて出し分けるための基準 */
  container-type: inline-size;

  /* Scryfall JPG は角丸の外側が白塗り(実測 rgb255)のため、
     画像自身の角丸(幅の約4.7%)よりわずかに深くクリップして白を切り落とす */
  border-radius: 5.5% / 4%;

  &.empty {
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: 1.5px dashed color-mix(in oklch, var(--accent) 55%, transparent);
    background: oklch(1 0 0 / 0.06);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;

    &:hover { background: oklch(1 0 0 / 0.14); border-color: var(--accent); }

    & .plus {
      font-size: clamp(0.9rem, 22cqw, 1.4rem);
      line-height: 1;
      color: color-mix(in oklch, var(--accent) 85%, transparent);
    }
    & .hint { font-size: 0.7rem; color: oklch(1 0 0 / 0.6); text-wrap: nowrap; }
  }

  &.filled {
    cursor: grab;
    /* スキャンの裁ち落とし差で地が覗いても目立たないよう内側は黒 */
    background: #0c0d10;
    box-shadow: 0 4px 14px oklch(0 0 0 / 0.35);
    /* D&D 安定化: スクロール・テキスト選択・ネイティブ画像ドラッグと競合させない */
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;

    &:active { cursor: grabbing; }

    & img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
      pointer-events: none;
      -webkit-user-drag: none;
    }

    /* Alpha版はカード本体の角丸が深い。スキャン外側の白を少し深く落とす */
    &.alpha-corners { border-radius: 8% / 5.7%; }

    /* 右上: カードを外すボタン（ホバーで表示。タッチ端末では常時） */
    & .cell-x {
      position: absolute;
      inset-block-start: 5px;
      inset-inline-end: 5px;
      width: 24px;
      height: 24px;
      padding: 0;
      display: grid;
      place-content: center;
      border: 0;
      border-radius: 50%;
      font-size: 0.95rem;
      line-height: 1;
      color: oklch(1 0 0 / 0.92);
      background: oklch(0.2 0.02 260 / 0.7);
      backdrop-filter: blur(4px);
      cursor: pointer;
      opacity: 0;
      transition: opacity 0.15s, background 0.15s;

      &:hover { background: var(--danger); }
    }
    &:hover .cell-x, &:focus-within .cell-x { opacity: 1; }
  }

  &.dragging { opacity: 0.3; }
  &.dropover { outline: 3px solid var(--accent); outline-offset: -3px; }

  /* 画像未提供の版: 実物の白紙カード風にして「バグではない」ことを伝える */
  & .noimg {
    position: relative;
    display: grid;
    place-content: center;
    gap: 4px;
    height: 100%;
    padding: 14%;
    text-align: center;
    background: linear-gradient(160deg, #f8f5ee, #ebe4d2);
    color: #6d6653;

    & .noimg-name { font-size: 0.82rem; font-weight: 600; line-height: 1.4; }
    & small { font-size: 0.66rem; color: #a49d8c; }

    &::before {
      content: "";
      position: absolute;
      inset: 5% 4.5%;
      border: 1px solid #d8d0bd;
      border-radius: 6px;
      pointer-events: none;
    }
  }
}

.cell-actions {
  position: absolute;
  inset: auto 0 0 0;
  display: flex;
  background: oklch(0.2 0.02 260 / 0.78);
  backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.15s;

  .cell.filled:hover &, .cell.filled:focus-within & { opacity: 1; }

  & button {
    flex: 1;
    font-size: 0.72rem;
    padding-block: 7px;
    border: 0;
    background: none;
    color: #fff;
    cursor: pointer;

    &:hover { background: oklch(1 0 0 / 0.15); }
  }
}
@media (hover: none) { .cell-actions, .cell.filled .cell-x { opacity: 1; } }

/* セルが狭いときは説明文を省き、＋だけにする（折り返しで潰れるのを防ぐ） */
@container (max-width: 84px) {
  .cell.empty .hint, .icon-cell .spot .hint { display: none; }
}

/* ───── 中央: 円形のアイコンスポット(カード型にしない) ───── */
.icon-cell {
  position: relative;
  aspect-ratio: 63 / 88;
  /* grid+aspect-ratio+containment の組では cq 単位の解決が崩れるため flex にする */
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  cursor: pointer;
  container-type: inline-size;

  /* Xユーザー名: 書き出し(src/export.js)と同じ位置・比率で敷く */
  & .icon-handle {
    position: absolute;
    inset-inline: -25%;
    /* 書き出し(export.js)の @名 と同じ高さ（文字中心がセル高の86.3%） */
    inset-block-start: 83.3%;
    text-align: center;
    font-size: 8.5cqw;
    font-weight: 600;
    line-height: 1;
    color: #fff;
    text-shadow: 0 0 0.5em oklch(0.2 0.02 260 / 0.55);
    white-space: nowrap;
    pointer-events: none;
  }

  & .spot {
    /* 書き出し画像の円（セル幅の84%）と同じ大きさにする。
       グリッドの auto トラック内では%幅が解決できないため、絶対配置で中央に置く
       （絶対配置なら包含ブロック=セルに対して%が解決される） */
    position: absolute;
    inset-inline-start: 50%;
    inset-block-start: 50%;
    translate: -50% -50%;
    width: 84%;
    aspect-ratio: 1;
    border-radius: 50%;
    /* 中身がはみ出しても円を保つ */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    border: 1.5px dashed color-mix(in oklch, var(--accent) 55%, transparent);
    background: oklch(1 0 0 / 0.07);
    transition: background 0.15s, border-color 0.15s;

    & .plus {
      font-size: clamp(0.8rem, 20cqw, 1.3rem);
      line-height: 1;
      color: color-mix(in oklch, var(--accent) 85%, transparent);
    }
    & .hint { font-size: 0.62rem; color: oklch(1 0 0 / 0.6); text-wrap: nowrap; }
  }

  &:hover .spot, &.dropping .spot {
    background: oklch(1 0 0 / 0.16);
    border-color: var(--accent);
  }

  & .icon-img {
    position: absolute;
    inset-inline-start: 50%;
    inset-block-start: 50%;
    translate: -50% -50%;
    width: 84%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 50%;
    /* 透過背景はプレイマットに馴染むよう枠も円形の影も付けない */
    border: 0;
    box-shadow: none;
    background: transparent;
  }

  &.icon-bg-white .icon-img { background: #fff; }
  &.icon-frame-gold .icon-img {
    border: 3px solid #e2b64f;
    box-shadow: 0 4px 16px oklch(0 0 0 / 0.4);
  }
}

.ghost {
  position: fixed;
  z-index: 50;
  aspect-ratio: 63 / 88;
  border-radius: 5.5% / 4%;
  overflow: hidden;
  pointer-events: none;
  opacity: 0.9;
  background: #0c0d10;
  box-shadow: 0 16px 40px oklch(0 0 0 / 0.4);

  & img { width: 100%; height: 100%; object-fit: cover; }
  & .cell-actions, & .cell-x { display: none; }
}

.drag-hint {
  font-size: 0.72rem;
  color: var(--muted);
  text-align: center;
  padding-block: 8px;
  /* カードが2枚以上あるときだけ意味を持つ案内 */
  visibility: hidden;
}
body:has(#grid .cell.filled) .drag-hint { visibility: visible; }

/* ───── ツール ───── */
.tools { padding-block-start: 10px; }
.tool-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding-block: 5px;
}

button, .export-actions a {
  font: inherit;
  font-size: 0.8rem;
  padding: 7px 13px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: inherit;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, color 0.15s;

  &:hover { border-color: var(--accent); color: var(--accent-text); }
  &:disabled { opacity: 0.5; cursor: default; }
}

/* 最終アクション: ブランドの墨色地に金文字（金地×黒文字は違和感があるため反転） */
button.primary, .export-actions a {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--primary-text);
  font-weight: 600;

  &:hover {
    background: oklch(0.32 0.035 265);
    border-color: oklch(0.32 0.035 265);
    color: #edcd74;
  }
}

:root[data-game="pokemon"] button.primary,
:root[data-game="pokemon"] .export-actions a {
  box-shadow: none;

  &:hover {
    background: #11110f;
    border-color: #11110f;
    color: #fff;
  }
}

:root[data-game="pokemon"] .tools {
  margin-block-start: 8px;
  padding-block-start: 16px;
}

:root[data-game="pokemon"] .tools > #exportBtn {
  box-shadow: none;
}

:root[data-game="pokemon"] .seg.active {
  border-color: #262522;
  background: #262522;
  color: #fff;
  box-shadow: none;
}

:root[data-game="pokemon"] dialog .dialog-head {
  border-block-end: 2px solid var(--accent);
  margin-block-end: 12px;
}

/* 「画像を作る」は工程の締め。単独行・全幅で他のボタンと格を分ける */
.tools > #exportBtn {
  display: block;
  width: 100%;
  margin-block-start: 16px;
  padding-block: 13px;
  font-size: 0.95rem;
  letter-spacing: 0.18em;
  text-indent: 0.18em;
  border-radius: 12px;
  box-shadow: 0 6px 18px oklch(0.25 0.03 260 / 0.25);
}

.seg.active {
  border-color: var(--accent);
  color: var(--accent-text);
  background: var(--accent-soft);
  font-weight: 600;
}

/* プレイマットのスウォッチ */
#mats { display: flex; flex: 1 1 320px; flex-wrap: wrap; gap: 6px; }
#iconBackgrounds, #iconFrames { display: flex; gap: 6px; }
.mat {
  width: 34px;
  height: 26px;
  padding: 0;
  border-radius: 7px;
  border: 2px solid transparent;
  background-size: cover;
  background-position: center;

  &.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
}

:root[data-game="pokemon"] .mat.active {
  border-color: #262522;
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px #262522;
}

:root[data-game="pokemon"] .mat.mat-none.active {
  border-color: #262522;
  background: #262522 !important;
  color: #fff;
  box-shadow: none;
}

:root[data-game="pokemon"] .mat.mat-type {
  width: 36px;
  height: 28px;
  font-size: 0.68rem;
  font-weight: 800;
}

.mat.mat-none {
  width: auto;
  min-width: 48px;
  padding-inline: 9px;
  border-color: var(--line);
  color: var(--muted);
  font-size: 0.72rem;
}

/* 画像マットの調整パネル: スライダー5本を折り返しに任せず、2列グリッドに整列する */
.mat-adjust {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px 24px;
  margin-block: 6px 4px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);

  & .mat-adjust-actions {
    grid-column: 1 / -1;
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    padding-block-start: 2px;
  }
}
@media (max-width: 480px) {
  .mat-adjust { grid-template-columns: 1fr; gap: 8px; }
}

.dim-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.76rem;
  color: var(--muted);

  & .dim-label { inline-size: 3.2em; flex-shrink: 0; }
  & input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); }

  /* はみ出しがなく動かしても効かない軸は、無効であることを見た目でも示す */
  &:has(input:disabled) { opacity: 0.4; }
}

#status { min-height: 1.5em; font-size: 0.8rem; color: var(--accent-text); }

/* ───── スポンサー枠（募集時は案内、契約後はロゴとリンクへ差し替える） ───── */
.sponsor-slot {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  position: relative;
  inset-inline-start: 50%;
  width: min(375px, 100vw);
  aspect-ratio: 6 / 1;
  margin-block-end: 14px;
  padding: 8px 14px;
  border: 1px solid color-mix(in oklch, var(--accent) 34%, var(--line));
  border-radius: 9px;
  background: var(--panel);
  transform: translateX(-50%);

  & .sponsor-slot-label {
    display: none;
    color: var(--muted);
    font-size: 0.62rem;
    font-weight: 600;
    letter-spacing: 0.13em;
  }

  & .sponsor-slot-copy {
    display: flex;
    min-width: 0;
    flex-direction: column;
    line-height: 1.35;

    & strong { font-size: 0.84rem; color: var(--ink); }
    & small {
      overflow: hidden;
      color: var(--muted);
      font-size: 0.62rem;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  & .sponsor-slot-note {
    color: var(--accent-text);
    font-size: 0.72rem;
    font-weight: 600;
    white-space: nowrap;
  }
}

@media (max-width: 480px) {
  .sponsor-slot {
    border-inline: 0;
    border-radius: 0;
  }
}

/* ───── ダイアログ ───── */
dialog {
  border: 0;
  border-radius: 16px;
  padding: 0;
  width: min(560px, calc(100vw - 32px));
  max-height: 84dvh;
  /* 画面上部寄りに固定表示（内容の増減で位置が跳ねないように） */
  margin: 6dvh auto auto;
  box-shadow: 0 24px 80px oklch(0 0 0 / 0.3);
  opacity: 0;
  translate: 0 12px;
  transition: opacity 0.18s, translate 0.18s, overlay 0.18s allow-discrete, display 0.18s allow-discrete;

  &[open] {
    opacity: 1;
    translate: 0 0;

    @starting-style { opacity: 0; translate: 0 12px; }
  }

  &::backdrop { background: oklch(0.15 0.02 260 / 0.55); }
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 20px 10px;

  & h2 { font-size: 0.98rem; }
  & button { padding: 4px 11px; border-radius: 999px; }
}

.dialog-body {
  padding: 0 20px 20px;
  overflow-y: auto;
  max-height: calc(84dvh - 58px);
  scrollbar-gutter: stable;
  overscroll-behavior: contain;
}

.import-help {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.6;
}

#deckImportInput {
  box-sizing: border-box;
  width: 100%;
  min-height: 250px;
  resize: vertical;
  padding: 12px 14px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  font: 0.78rem/1.65 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: inherit;
  background: var(--panel);

  &:focus { outline: none; border-color: var(--accent); }
}

#deckImportStatus {
  min-height: 1.5em;
  margin-block: 8px;
  color: var(--accent-text);
  font-size: 0.78rem;
}

.import-actions { display: flex; justify-content: flex-end; }

.search-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin-block-end: 10px;

  & input {
    flex: 1;
    min-width: 0;
    font: inherit;
    padding: 10px 14px;
    border: 1.5px solid var(--line);
    border-radius: 10px;

    &:focus { outline: none; border-color: var(--accent); }
  }

  & button {
    flex: 0 0 auto;
    padding-inline: 13px;
    font-size: 0.78rem;
  }
}

.upload-card-picker {
  position: relative;
  display: grid;
  place-items: center;
  width: min(172px, 62%);
  aspect-ratio: 63 / 88;
  margin: 2px auto 18px;
  padding: 0;
  overflow: hidden;
  border: 1.5px dashed color-mix(in oklch, var(--accent) 65%, var(--line));
  border-radius: 10px;
  background: color-mix(in oklch, var(--accent-soft) 42%, var(--panel));
  color: var(--text);

  &:hover, &.dropping {
    border-color: var(--accent-text);
    background: var(--accent-soft);
  }

  & img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  & #uploadCardPlaceholder {
    display: grid;
    gap: 4px;
    padding: 16px;

    & strong { font-size: 0.84rem; }
    & small { color: var(--muted); font-size: 0.68rem; }
  }

  &.has-image::after {
    content: "画像を変更";
    position: absolute;
    inset: auto 0 0;
    padding: 5px;
    background: oklch(0.18 0.02 260 / 0.78);
    color: white;
    font-size: 0.68rem;
  }
}

.upload-card-fields {
  display: grid;
  gap: 11px;

  & label {
    display: grid;
    gap: 5px;

    & > span { font-size: 0.76rem; font-weight: 600; }
    & small { color: var(--muted); font-size: 0.66rem; font-weight: 400; }
  }

  & input {
    width: 100%;
    min-width: 0;
    padding: 9px 12px;
    border: 1.5px solid var(--line);
    border-radius: 9px;
    background: var(--panel);
    color: inherit;
    font: inherit;
    font-size: 0.85rem;

    &:focus { outline: none; border-color: var(--accent); }
  }
}

.upload-card-help {
  margin-block-start: 10px;
  color: var(--muted);
  font-size: 0.68rem;
  line-height: 1.5;
}

#uploadCardStatus {
  min-height: 1.5em;
  margin-block: 5px;
  color: var(--accent-text);
  font-size: 0.76rem;
}

.upload-card-actions { display: flex; justify-content: flex-end; }

.sort-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-block-end: 10px;

  & .seg { font-size: 0.74rem; padding: 5px 11px; }
}

#suggest {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;

  &:has(.chip) { margin-block-end: 10px; }
}
.chip { font-size: 0.78rem; padding: 5px 12px; border-radius: 999px; }

.result {
  display: flex;
  align-items: center;
  gap: 8px;
  border-block-start: 1px solid var(--line);
}

button.result-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  text-align: start;
  padding: 8px 6px;
  border: 0;
  border-radius: 8px;
  background: none;
  cursor: pointer;

  &:hover { background: var(--accent-soft); color: inherit; }

  & img, & .thumb-empty { width: 44px; border-radius: 4px; flex-shrink: 0; }
  & .thumb-empty { aspect-ratio: 63 / 88; }

  & .result-info {
    flex: 1;
    min-width: 0;

    & strong { font-size: 0.88rem; display: block; text-wrap: balance; }
    & small { color: var(--muted); font-size: 0.74rem; }
  }

  & .result-go {
    flex-shrink: 0;
    font-size: 0.76rem;
    color: var(--accent-text);
  }
}

.result-add {
  flex-shrink: 0;
  font-size: 0.78rem;
  padding: 6px 14px;
}

#cardDialogBody {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  gap: 10px;
}
.print {
  padding: 0 0 6px;
  overflow: hidden;
  text-align: center;

  & img { width: 100%; display: block; margin-block-end: 4px; }
  & small { font-size: 0.7rem; color: var(--muted); line-height: 1.4; display: block; padding-inline: 4px; }
}
/* 画像が存在しない版のブランク: 実物の白紙カード風（アイボリー地+内枠） */
.thumb-empty {
  position: relative;
  aspect-ratio: 63 / 88;
  background: linear-gradient(160deg, #f8f5ee, #ebe4d2);
  border: 1px solid #d2cab6;
  border-radius: 8% / 5.5%;

  /* 内枠（印刷枠の名残） */
  &::before {
    content: "";
    position: absolute;
    inset: 6.5% 5.5%;
    border: 1px solid #d8d0bd;
    border-radius: 4px;
    pointer-events: none;
  }
}

.thumb-empty.tall {
  width: 100%;
  display: grid;
  place-content: center;
  font-size: 0.7rem;
  color: #a09a8b;
  text-align: center;
  margin-block-end: 4px;
}

#exportPreview {
  display: block;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
}
#exportMeta {
  margin-block: 3px 18px;
  color: color-mix(in oklch, var(--muted) 72%, transparent);
  font-size: 0.7rem;
  font-weight: 400;
  line-height: 1.4;
  text-align: end;
}
.share-message {
  margin-block: 14px 20px;

  & .share-message-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-block-end: 7px;

    & label { font-size: 0.82rem; font-weight: 600; }
    & button { padding: 5px 11px; font-size: 0.74rem; }
  }

  & textarea {
    display: block;
    width: 100%;
    min-height: 190px;
    resize: vertical;
    padding: 11px 13px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--panel);
    color: inherit;
    font: 0.78rem/1.65 ui-monospace, SFMono-Regular, Menlo, monospace;

    &:focus { outline: none; border-color: var(--accent); }
  }

  & > p {
    margin-block-start: 5px;
    color: var(--muted);
    font-size: 0.68rem;
  }
}
.export-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;

  & a { text-decoration: none; }
}

.muted { color: var(--muted); font-size: 0.85rem; padding-block: 8px; }
.error { color: var(--danger); font-size: 0.85rem; padding-block: 8px; }

footer {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 20px 40px;
  font-size: 0.68rem;
  color: var(--muted);
  line-height: 1.9;
  text-wrap: pretty;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
