:root {
  --bg: #f2f2f7;
  --panel: #ffffff;
  --text: #1c1c1e;
  --muted: #6c6c70;
  --line: #d1d1d6;
  --accent: #0d6efd;
  --accent-text: #ffffff;
  --danger: #d70015;
  --chip: #e5efff;
  --sel: #dbe8ff;
  --mark: #ff9500; /* 「選んだカードがある」目印（カテゴリーの枠・枚数）。開いているカテゴリーの青とは別の色 */
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --panel: #1c1c1e;
    --text: #f2f2f7;
    --muted: #98989d;
    --line: #3a3a3c;
    --accent: #3d8bff;
    --danger: #ff6961;
    --chip: #1f3556;
    --sel: #1f3556;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 -apple-system, BlinkMacSystemFont, "Hiragino Sans", sans-serif;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  overscroll-behavior: none; /* 画面を下に引っ張っての再読み込みで、入力中の内容が消えないようにする */
}

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* --- 共通部品 --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 19px; margin: 0; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
/* 外部のサイトへ移るリンク。誤って押さないよう、小さく控えめにして、タブから離して置く */
.kofi {
  flex: none;
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  font-size: 13px;
  color: var(--muted);
  text-decoration: none;
  white-space: nowrap;
  border: 1px solid var(--line);
  border-radius: 15px;
}
.kofi:active { opacity: 0.6; }
/* 言語の切り替え。広い画面では Ko-fi の隣に、狭い画面では 1 段目の右端に置き、タブから離す */
.lang {
  flex: none;
  min-height: 30px;
  padding: 0 10px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 15px;
  white-space: nowrap;
}
.lang:active { opacity: 0.6; }

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--sel);
}
.banner-error { color: var(--danger); font-size: 14px; }

.btn {
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
}
.btn:active { opacity: 0.6; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-danger { color: var(--danger); }
.btn-wide { flex: 1; }
.btn:disabled { opacity: 0.4; }

.muted { color: var(--muted); font-size: 14px; margin: 8px 0 0; }
.pre { white-space: pre-wrap; }
.empty { color: var(--muted); text-align: center; padding: 24px 12px; margin: 0; grid-column: 1 / -1; }
.empty.small { padding: 12px; }
.spacer { flex: 1; }

input[type="text"], input[type="number"], textarea, select {
  width: 100%;
  min-height: 44px;
  padding: 8px 10px;
  font: inherit;
  font-size: 16px; /* 16px 未満だと iOS が入力時に拡大する */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
}
textarea { resize: vertical; }

.field { display: block; margin: 0 0 14px; }
.field > span { display: block; font-size: 14px; color: var(--muted); margin-bottom: 4px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: 90vw;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  color: #fff;
  background: rgba(40, 40, 42, 0.95);
  border-radius: 12px;
  z-index: 100;
  word-break: break-word;
}
.toast-btn { font: inherit; color: #8ab8ff; background: none; border: 0; padding: 6px; min-height: 44px; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* --- 作成画面（横画面：左右 2 列、縦画面：上下） --- */
#view-builder {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* 中身（長い 1 行）で列が広がって、横にはみ出さないように */
  grid-template-rows: minmax(0, 1fr) auto;
}
@media (orientation: landscape) {
  #view-builder { grid-template-columns: minmax(0, 1fr) 340px; grid-template-rows: minmax(0, 1fr); }
}

.b-left { display: flex; flex-direction: column; min-height: 0; min-width: 0; }

/* 選択中のカード名：1 行で並べ、はみ出す分はこの行だけ横にスクロールする */
.sel-cards {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  padding: 6px 12px;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
  background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 13px;
}
.sel-cards-label { flex: none; color: var(--muted); }
.sel-cards-empty { flex: none; color: var(--muted); }
.sel-card-name {
  flex: none;
  max-width: 10em; /* 長い名前は「騎乗位（とても…」くらいで省略する */
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 3px 10px;
  color: var(--text);
  background: var(--sel);
  border: 1px solid var(--accent);
  border-radius: 12px;
}

.cards {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 12px 12px;
}
.group-head {
  width: 100%;
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding: 0 4px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: none;
  border: 0;
  text-align: left;
  -webkit-user-select: none;
  user-select: none;
}
.group-arrow { width: 1.2em; color: var(--muted); font-size: 13px; }
.group-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 10px;
}

.card {
  padding: 0;
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  text-align: center;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: pointer;
}
.card.selected { border-color: var(--accent); background: var(--sel); }
.card-img {
  height: 72px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--muted);
  font-weight: bold;
  font-size: 18px;
}
.card-img img { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.card-name {
  padding: 8px 6px;
  font-size: 14px;
  line-height: 1.25;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-wrap: anywhere;
}

.catbar {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.cat-btn {
  flex: none;
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  white-space: nowrap;
  -webkit-user-select: none;
  user-select: none;
}
.cat-btn.active { color: var(--accent-text); background: var(--accent); border-color: var(--accent); }
.cat-btn { display: inline-flex; align-items: center; gap: 6px; }
.cat-btn.has-sel { border: 3px solid var(--mark); padding: 0 14px; } /* 枠を太くした分、余白を減らして大きさをそろえる */
.cat-count {
  min-width: 20px;
  padding: 0 6px;
  font-size: 12px;
  font-weight: 700;
  line-height: 20px;
  text-align: center;
  color: #1c1c1e;
  background: var(--mark);
  border-radius: 10px;
}
.group-head.has-sel .group-sel { color: #1c1c1e; background: var(--mark); }
.group-sel {
  padding: 0 8px;
  font-size: 12px;
  font-weight: 700;
  line-height: 20px;
  border-radius: 10px;
}

.b-right {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  padding: 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
}
@media (orientation: landscape) {
  .b-right { border-top: 0; border-left: 1px solid var(--line); }
}
.panel-title { font-size: 14px; color: var(--muted); }

/* 選択中のタグ欄：見出しで開閉できる（縦画面だけ。横画面では常に開いて、見出しは押せない） */
.sel-head { display: flex; align-items: center; gap: 8px; }
.sel-toggle {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0;
  font: inherit;
  color: var(--text);
  background: none;
  border: 0;
  text-align: left;
  -webkit-user-select: none;
  user-select: none;
}
.sel-body { display: flex; flex-direction: column; gap: 8px; flex: 1; min-height: 0; }
.copy-mini { display: none; }
.b-right.collapsed .sel-body { display: none; }
.b-right.collapsed .copy-mini { display: inline-block; }
@media (orientation: landscape) {
  .sel-toggle { pointer-events: none; }
  #sel-arrow { display: none; }
  .b-right.collapsed .sel-body { display: flex; }
  .b-right.collapsed .copy-mini { display: none; }
}

.chips {
  flex: 1;
  min-height: 56px;
  max-height: 28dvh;
  overflow-y: auto;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 6px;
}
@media (orientation: landscape) {
  .chips { max-height: none; }
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-height: 40px;
  padding: 0 4px 0 12px;
  background: var(--chip);
  border-radius: 20px;
  font-size: 15px;
  -webkit-user-select: none;
  user-select: none;
}
.chip-x {
  width: 40px;
  height: 40px;
  font-size: 20px;
  color: var(--muted);
  background: none;
  border: 0;
}
.chips.reorder .chip { padding-right: 12px; outline: 2px dashed var(--accent); touch-action: none; }
.chips.reorder .chip-x { display: none; }

.output { min-height: 72px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn[aria-pressed="true"] { background: var(--sel); border-color: var(--accent); }

/* --- 管理画面 --- */
#view-manager { flex: 1; overflow-x: hidden; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; }
.m-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; margin-bottom: 12px; }
@media (orientation: landscape) {
  .m-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
.card-box { min-width: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-bottom: 12px; }
.m-grid .card-box { margin-bottom: 0; }
.box-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.box-head h2 { font-size: 17px; margin: 0; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

.m-list { list-style: none; margin: 0; padding: 0; }

/* 一覧が増えても、ページ全体が長くならないように、一覧の中だけスクロールする。
   高さは画面の半分ほどまでにして、見出しや箱の余白を触ればページ全体をスクロールできるようにする。
   一覧の端まで来たら、続けてページがスクロールする（overscroll-behavior は既定のまま）。 */
.m-grid .m-list {
  max-height: min(46dvh, 30rem);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  /* まだ下（上）に続きがあるときは、縁に影を出して知らせる */
  background:
    linear-gradient(var(--panel) 30%, transparent) top / 100% 28px no-repeat local,
    linear-gradient(transparent, var(--panel) 70%) bottom / 100% 28px no-repeat local,
    radial-gradient(farthest-side at 50% 0, rgba(0, 0, 0, 0.28), transparent) top / 100% 10px no-repeat scroll,
    radial-gradient(farthest-side at 50% 100%, rgba(0, 0, 0, 0.28), transparent) bottom / 100% 10px no-repeat scroll;
}
@media (orientation: landscape) {
  .m-grid .m-list { max-height: 55dvh; }
}
.m-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 4px 0;
  border-top: 1px solid var(--line);
}
.m-item.active { background: var(--sel); }
.m-main {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: none;
  border: 0;
  padding: 0;
}
.m-sub {
  max-width: 100%;
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.m-thumb { flex: none; width: 44px; height: 44px; border-radius: 8px; background: var(--bg); overflow: hidden; }
.m-thumb img { width: 100%; height: 100%; object-fit: cover; }
.drag-handle {
  flex: none;
  width: 44px;
  text-align: center;
  font-size: 22px;
  line-height: 44px;
  color: var(--muted);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.sortable-ghost { opacity: 0.4; }

.credit {
  margin: 4px 0 8px;
  text-align: center;
  font-size: 11px;
  color: var(--muted);
  opacity: 0.6;
}

/* --- ダイアログ --- */
dialog {
  width: min(560px, 92vw);
  max-height: 90dvh;
  padding: 16px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog h3 { margin: 0 0 12px; font-size: 18px; }
.dlg-actions { display: flex; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.thumb-box { display: flex; align-items: flex-end; gap: 12px; margin-top: 8px; }
.thumb-box img { max-width: 96px; max-height: 96px; border-radius: 8px; }

/* --- タブ --- */
.tabs { display: flex; gap: 4px; }
.tab {
  min-height: 44px;
  padding: 0 18px;
  font: inherit;
  color: var(--text);
  background: none;
  border: 0;
  border-radius: 10px;
  -webkit-user-select: none;
  user-select: none;
}
.tab.active { color: var(--accent-text); background: var(--accent); }

/* --- チップ（切り出し・画像画面で共通） --- */
.pick-list { display: flex; flex-wrap: wrap; gap: 6px; }
.mode-select { width: auto; max-width: 100%; margin-bottom: 8px; }
.pick {
  min-height: 40px;
  max-width: 100%;
  padding: 8px 12px;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  overflow-wrap: anywhere;
  -webkit-user-select: none;
  user-select: none;
  text-align: left;
}
.pick.on { background: var(--sel); border-color: var(--accent); }
.pick.done { opacity: 0.45; }

/* --- 切り出しフォーム --- */
#x-box textarea { margin-bottom: 8px; }
#x-box input[type="text"] { margin-bottom: 8px; }
.x-target { margin: 0 0 8px; }
.x-thumb { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.x-thumb img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; }
#a-list { max-height: 60dvh; overflow-y: auto; }
#a-list .m-main { padding: 6px 0; }

/* --- 画像画面 --- */
#view-image {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  gap: 12px;
  padding: 12px;
  -webkit-overflow-scrolling: touch;
}
@media (orientation: landscape) {
  #view-image { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 120px;
  padding: 16px;
  background: var(--panel);
  border: 2px dashed var(--line);
  border-radius: 12px;
}
.dropzone.over { border-color: var(--accent); background: var(--sel); }
.dropzone img { max-width: 100%; max-height: 30dvh; object-fit: contain; border-radius: 8px; }
@media (orientation: landscape) {
  .dropzone img { max-height: 60dvh; }
}
.i-result-col { min-width: 0; }
.i-result-col > #i-result,
.i-result-col > details {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 12px;
}
.i-gap { margin-top: 12px; }
.i-neg { margin: 0; color: var(--muted); overflow-wrap: anywhere; }
.i-settings { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 12px 0; font-size: 14px; }
.i-settings dt { color: var(--muted); }
.i-settings dd { margin: 0; overflow-wrap: anywhere; }
details > summary { min-height: 44px; display: flex; align-items: center; }
.raw-item { margin-bottom: 8px; }
.raw-text { margin: 4px 0 0; padding: 8px; max-height: 240px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; background: var(--bg); border-radius: 8px; }

/* 狭い画面（iPhone）では、ヘッダーの文字と余白を小さくして 1 行に収める */
@media (max-width: 430px) {
  .topbar { padding: 6px 8px; gap: 4px; }
  .topbar h1 { font-size: 16px; white-space: nowrap; }
  .tab { padding: 0 11px; }
}

/* 狭い画面（iPhone の縦）では 2 段にする：1 段目にタイトルと Ko-fi、2 段目にタブ */
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; row-gap: 12px; padding-bottom: 8px; }
  .brand { width: 100%; }
  .lang { margin-left: auto; }
  .topbar h1 { font-size: 17px; }
  .tabs { width: 100%; }
  .tab { flex: 1; padding: 0 8px; }
}

/* とくに狭い画面（320px 前後）では、1 段目（タイトル・Ko-fi・言語）が収まるように、間隔・余白・タイトルを小さくする */
@media (max-width: 360px) {
  .brand { gap: 6px; }
  .kofi, .lang { padding: 0 8px; }
  .topbar h1 { font-size: 15px; }
}

/* --- 説明画面 --- */
#view-help { flex: 1; overflow-x: hidden; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; }
.help { max-width: 720px; margin: 0 auto; }
.help-lead { margin: 0 0 12px; }
.help-sec { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; margin-bottom: 10px; }
.help-sec > summary { font-weight: 600; min-height: 48px; cursor: pointer; }
.help-sec[open] > summary { border-bottom: 1px solid var(--line); margin-bottom: 8px; }
.help-sec ol, .help-sec ul { margin: 0 0 8px; padding-left: 1.4em; }
.help-sec li { margin-bottom: 6px; overflow-wrap: anywhere; }
.help-sec p { margin: 0 0 10px; }
.help-sec .muted { margin-top: 0; }
.help-kofi { display: inline-flex; align-items: center; text-decoration: none; }
