*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --bg: #f6f8fb;
  --panel: #ffffff;
  --ink: #283445;
  --muted: #667386;
  --line: #dbe3ee;
  --blue: #407fed;
  --blue-deep: #1d2d75;
  --blue-soft: #edf4ff;
  --mint: #1fc49d;
  --shadow: 0 18px 42px rgba(40, 52, 69, 0.1);
}

html {
  min-width: 320px;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic", Meiryo, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  height: 100vh;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  padding: 20px;
  overflow: hidden;
}

.workspace,
.control-panel,
.meta-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.workspace {
  min-width: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
}

.topbar {
  min-height: 88px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.kicker,
h1,
h2,
p {
  margin: 0;
}

.kicker {
  color: var(--blue);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

h1 {
  margin-top: 3px;
  color: var(--blue-deep);
  font-size: 28px;
  line-height: 1.2;
}

h2 {
  color: var(--blue-deep);
  font-size: 14px;
  line-height: 1.4;
}

.file-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

#imageInput {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.icon-button,
.preset-button,
.segment,
.preview-mode,
.sharpen-option {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 14px;
  font-weight: 700;
}

.icon-button.primary {
  background: var(--blue);
  border-color: var(--blue);
  color: #fff;
}

.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.icon-button.active {
  background: var(--blue-soft);
  border-color: var(--blue);
  color: var(--blue-deep);
}

.sponsor-slot {
  padding: 14px 18px 0;
}

.sponsor-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.sponsor-banner {
  min-height: 78px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 12px;
  overflow: hidden;
  background: linear-gradient(135deg, #ffffff, #eef5ff);
  border: 1px dashed #b7c8e2;
  border-radius: 8px;
  color: var(--ink);
  text-decoration: none;
}

/* 掲載中の枠。バナー画像を枠いっぱいに置く。
   地色をバナー本体と同じ紺にしてあるので、横に余白が出ても1枚の帯に見える */
.sponsor-banner.is-filled {
  display: block;
  padding: 0;
  overflow: hidden;
  background: #1d2d3d;
  border: 1px solid #16222e;
}

/* 1.91:1 のバナーをそのまま出す。別の縦横比が来ても max-height で高さが暴れないようにし、
   はみ出す分は contain で中央に収める(余白は地色と同じ紺なので目立たない) */
.sponsor-banner.is-filled img {
  display: block;
  width: 100%;
  height: auto;
  max-height: 180px;
  object-fit: contain;
}

.sponsor-banner.is-filled:hover {
  opacity: 0.88;
}

.sponsor-label {
  min-height: 28px;
  display: inline-grid;
  place-items: center;
  padding: 4px 9px;
  background: var(--blue-soft);
  border: 1px solid #cfe0fb;
  border-radius: 8px;
  color: var(--blue);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.sponsor-banner strong {
  min-width: 0;
  color: var(--blue-deep);
  overflow-wrap: anywhere;
}

.sponsor-size {
  grid-column: 2;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.stage {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  gap: 16px;
  padding: 18px;
}

/* 使い方ガイドへの文字リンク。画面幅に応じてPC版・スマホ版を出し分ける */
.help-links {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px 20px;
  font-size: 13px;
}

.help-links a {
  color: var(--blue-deep);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.help-links a:hover {
  opacity: 0.72;
}

/* 広い画面ではPC版のガイドだけ出す */
.help-sp {
  display: none;
}

.sponsor-slot + .stage {
  padding-top: 10px;
}

.compare-row {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
}

body.hide-origin .compare-row {
  grid-template-columns: minmax(0, 1fr);
}

body.hide-origin .origin-panel {
  display: none;
}

.origin-panel {
  position: relative;
  min-height: 360px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: #f9fbfe;
  border: 1px solid var(--line);
  border-radius: 8px;
}

/* 枠の中に絶対配置して中央寄せ。こうすると枠の高さを基準に縮んでくれる */
#originImage {
  display: none;
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: calc(100% - 12px);
  max-height: calc(100% - 12px);
  width: auto;
  height: auto;
  box-shadow: var(--shadow);
}

body.has-image #originImage {
  display: block;
}

body.has-image .origin-placeholder {
  display: none;
}

.origin-placeholder {
  position: absolute;
  inset: 18px;
  display: grid;
  place-content: center;
  border: 1px dashed #aebacf;
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

/* 小窓(スマホ)のときだけ出る閉じるボタン */
.origin-close {
  display: none;
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 3;
  width: 28px;
  height: 28px;
  padding: 0;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.94);
  color: var(--muted);
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
}

.panel-caption {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 2;
  padding: 3px 10px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.panel-caption.accent {
  background: var(--blue-soft);
  border-color: #cfe0fb;
  color: var(--blue-deep);
}

.preview-panel {
  position: relative;
  min-height: 360px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    linear-gradient(45deg, #edf1f6 25%, transparent 25%),
    linear-gradient(-45deg, #edf1f6 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #edf1f6 75%),
    linear-gradient(-45deg, transparent 75%, #edf1f6 75%);
  background-color: #f9fbfe;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
}

canvas {
  display: block;
  /* 枠の中に絶対配置して中央寄せ。枠からはみ出して切れないようにする */
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  background: #fff;
  box-shadow: var(--shadow);
  touch-action: none;
}

body.has-image canvas {
  cursor: grab;
}

body.has-image.is-panning canvas {
  cursor: grabbing;
}

.empty-state {
  position: absolute;
  inset: 18px;
  display: grid;
  place-content: center;
  gap: 4px;
  border: 1px dashed #aebacf;
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.empty-state strong {
  color: var(--blue-deep);
  font-size: 18px;
}

.meta-panel {
  padding: 14px 16px;
}

dl {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin: 0;
}

dt {
  color: var(--muted);
  font-size: 12px;
}

dd {
  margin: 2px 0 0;
  overflow-wrap: anywhere;
  font-weight: 700;
}

.control-panel {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 18px;
  min-height: 0;
  overflow: auto;
}

.producer-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.producer-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.producer-brand {
  display: inline-flex;
  align-items: center;
  color: var(--blue-deep);
  font-weight: 700;
  text-decoration: none;
}

.producer-logo-frame {
  width: 220px;
  height: 72px;
  display: block;
  overflow: hidden;
  position: relative;
  flex: 0 0 auto;
}

.producer-logo {
  position: absolute;
  left: 50%;
  top: 50%;
  display: block;
  width: 220px;
  height: auto;
  max-width: none;
  max-height: none;
  transform: translate(-50%, -50%);
}

.control-group {
  display: grid;
  gap: 10px;
}

.control-group.two-cols {
  grid-template-columns: 1fr 1fr;
}

.two-cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.preset-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.preset-button,
.segment,
.preview-mode,
.sharpen-option {
  padding: 8px 10px;
  font-weight: 700;
}

.preset-button.active,
.segment.active,
.preview-mode.active,
.sharpen-option.active {
  background: var(--blue-soft);
  border-color: var(--blue);
  color: var(--blue-deep);
}

.custom-preset {
  border-style: dashed;
}

.custom-size-panel {
  padding: 12px;
  background: #f9fbfe;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.batch-panel,
.filename-panel {
  padding: 12px;
  background: #f9fbfe;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.batch-grid {
  display: grid;
  gap: 8px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.sharpen-control {
  grid-template-columns: 0.7fr 1.1fr 1.4fr;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

output {
  justify-self: end;
  color: var(--ink);
  font-size: 12px;
}

.group-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.mini-button {
  min-height: 30px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--blue);
  font-size: 12px;
  font-weight: 700;
}

input[type="number"],
input[type="text"],
select {
  width: 100%;
  min-height: 42px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}

.filename-preview {
  min-height: 28px;
  padding: 7px 9px;
  overflow-wrap: anywhere;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 12px;
}

input[type="range"] {
  accent-color: var(--blue);
}

.check-row {
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: center;
}

.check-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--mint);
}

body.dragging .preview-panel {
  border-color: var(--blue);
}

body.has-image .empty-state {
  display: none;
}

@media (max-width: 980px) {
  /* 画面を上下2段に固定する。
     上段=写真(元の写真と仕上がりの両方)は動かさない、下段=設定だけがスクロールする。
     こうしないと、設定をいじっている間に写真が画面外へ行って見比べられない。 */
  .app-shell {
    height: 100vh;
    height: 100dvh;
    /* iOSのSafariは 100vh が画面より大きくなることがあるので、実測値(script.js)を優先する */
    height: var(--app-height, 100dvh);
    grid-template-columns: 1fr;
    grid-template-rows: minmax(240px, 62dvh) minmax(0, 1fr);
    gap: 12px;
    overflow: hidden;
  }

  /* 右がはみ出して切れるのを防ぐ。
     グリッド/フレックスの子は既定が min-width:auto で「中身より小さくならない」ため、
     親が390pxまで縮んでも中の箱は434pxのまま残り、app-shellのoverflow:hiddenで
     右45pxが切り落とされていた(iPhone標準幅390pxで実測 2026-08-10)。
     横スクロールも効かないので、書き出しボタンの文字と仕上がりプレビューが見えなくなる。 */
  .workspace > *,
  .stage > *,
  .compare-row > *,
  .topbar > * {
    min-width: 0;
  }

  /* ボタンが一列に収まらない幅では折り返す(切り落とすより折り返すほうがまし) */
  .file-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .producer-panel {
    order: 99;
  }

  .topbar {
    min-height: 0;
    padding: 10px 12px;
  }

  .kicker {
    display: none;
  }

  h1 {
    margin-top: 0;
    font-size: 19px;
  }

  .icon-button {
    min-height: 38px;
    padding: 6px 10px;
    font-size: 13px;
  }

  /* スポンサー枠は横並びのまま。入りきらない分は横スクロール */
  .sponsor-slot {
    padding: 10px 12px 0;
  }

  .sponsor-grid {
    grid-template-columns: none;
    grid-auto-flow: column;
    grid-auto-columns: minmax(148px, 1fr);
    overflow-x: auto;
    padding-bottom: 4px;
  }

  .sponsor-banner {
    min-height: 50px;
    grid-template-columns: minmax(0, 1fr);
    gap: 2px;
    padding: 8px 10px;
  }

  .sponsor-label {
    display: none;
  }

  /* 写真の高さを優先。掲載中のバナーもこの高さまでに抑える */
  .sponsor-banner.is-filled img {
    max-height: 84px;
  }

  .sponsor-size {
    grid-column: auto;
    font-size: 10px;
  }

  .stage {
    padding: 10px 12px 12px;
    gap: 8px;
  }

  .sponsor-slot + .stage {
    padding-top: 8px;
  }

  /* 画像情報は場所を取るので隠す(出力サイズは設定側で分かる) */
  .meta-panel {
    display: none;
  }

  /* 狭い画面ではスマホ版のガイドと、ホーム画面への入れ方を出す */
  .help-pc {
    display: none;
  }

  .help-sp {
    display: inline;
  }

  .help-links {
    gap: 4px 14px;
    font-size: 12px;
  }

  /* 仕上がりは縦の高さで大きさが決まるので、横に並べても小さくならない。
     縦積みにすると元の写真が細長い帯になって比べられないため、スマホでも左右に並べる */
  .compare-row {
    grid-template-columns: minmax(0, 0.75fr) minmax(0, 1fr);
    gap: 8px;
  }

  .origin-panel {
    min-height: 0;
  }

  #originImage {
    cursor: zoom-in;
  }

  .preview-panel {
    min-height: 0;
  }

  /* 元の写真をタップしたら全画面で確認できる */
  .origin-panel.is-large {
    position: fixed;
    inset: 0;
    z-index: 80;
    height: auto;
    background: rgba(255, 255, 255, 0.98);
    border-radius: 0;
  }

  .origin-panel.is-large #originImage {
    cursor: zoom-out;
  }

  .origin-close {
    display: grid;
  }

  .panel-caption {
    top: 6px;
    left: 6px;
    padding: 2px 7px;
    font-size: 10px;
  }
}

@media (max-width: 640px) {
  .app-shell {
    padding: 12px;
    gap: 10px;
  }

  /* タイトルとボタンで2段。ボタンは横一列のまま */
  .topbar {
    flex-wrap: wrap;
    padding: 6px 12px 8px;
    gap: 4px;
  }

  h1 {
    font-size: 16px;
  }

  .file-actions {
    width: 100%;
    justify-content: space-between;
  }

  /* 幅が足りないので「元の写真と並べる」は記号だけにする */
  #compareToggle .button-text {
    display: none;
  }

  .icon-button {
    min-height: 36px;
  }

  /* 写真に高さを回すため、スポンサー枠は名前だけの1行にする */
  .sponsor-banner {
    min-height: 40px;
    padding: 6px 10px;
  }

  .sponsor-size {
    display: none;
  }

  .sponsor-grid {
    grid-auto-columns: minmax(130px, 1fr);
  }

  .control-group.two-cols,
  .two-cols {
    grid-template-columns: 1fr;
  }
}

/* 指定サイズの見出し横に出す補足。入力するだけで反映されることを伝える */
.group-hint {
  color: var(--muted, #5b6676);
  font-size: 12px;
  white-space: nowrap;
}

/* 保存したことを知らせる短い通知。数秒で自動的に消える */
.save-toast {
  position: fixed;
  /* left:50% + translateX だと収まり幅が画面の半分になり不要な折り返しが出る。
     左右を留めて margin:auto で中央に置く */
  left: 16px;
  right: 16px;
  bottom: 24px;
  width: fit-content;
  margin: 0 auto;
  transform: translateY(8px);
  z-index: 60;
  padding: 12px 20px;
  border-radius: 999px;
  background: #17202a;
  color: #fff;
  font-size: 15px;
  font-weight: 650;
  line-height: 1.4;
  text-align: center;
  box-shadow: 0 6px 20px rgba(15, 25, 45, .28);
  opacity: 0;
  transition: opacity .18s ease, transform .18s ease;
  pointer-events: none;
}

.save-toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (prefers-reduced-motion: reduce) {
  .save-toast {
    transition: none;
  }
}

/* 使い方とよくある質問（検索で見つけた人にも何のページか伝わるように） */
.about { max-width: 860px; margin: 0 auto; padding: 28px 20px 48px; line-height: 1.8; }
.about h2 { font-size: 1.2rem; color: var(--blue-deep); margin: 28px 0 10px; }
.about h2:first-child { margin-top: 0; }
.about ul { padding-left: 1.3rem; margin: 0 0 12px; }
.about details { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; margin-bottom: 8px; }
.about summary { cursor: pointer; font-weight: 700; }
.about details p { margin: 6px 0 0; color: var(--muted); }
.about-more { margin-top: 20px; color: var(--muted); }
.about a { color: var(--blue); font-weight: 700; }
