/* レイアウトA「暗室」。iPhone：写真を全面・下にプリセット帯／Mac（900px〜）：右に 320px パネル */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text); font-family: var(--font);
  font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%; overflow: hidden; overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:disabled { cursor: default; }
[hidden] { display: none !important; }

.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* ---------- 写真エリア ---------- */
.stage { position: relative; flex: 1 1 auto; min-height: 0; background: #000; overflow: hidden; }
#view { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
.empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; padding: 80px 24px; text-align: center; color: var(--sub); border: 1px dashed transparent; margin: 12px;
  transition: border-color .15s;
}
.empty-main { font-size: 16px; font-weight: 300; letter-spacing: .12em; color: var(--text); }
.empty-sub { font-size: 12.5px; max-width: 24em; }
.app.is-drop .stage { box-shadow: inset 0 0 0 2px var(--amber); }
.app.is-drop .empty { border-color: var(--amber); }
.msg {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 3;
  max-width: calc(100% - 32px); padding: 8px 16px; border-radius: 999px; font-size: 12.5px;
  background: rgba(23, 25, 28, .92); border: 1px solid var(--line); color: var(--text); text-align: center;
}
.msg.is-warn { border-color: var(--warn); }

/* ---------- 上のバー ---------- */
.bar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 2; pointer-events: none;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(var(--sat) + 10px) calc(var(--sar) + 14px) 26px calc(var(--sal) + 14px);
  background: linear-gradient(rgba(8, 9, 10, .7), transparent);
}
.bar > * { pointer-events: auto; }
.title { color: var(--sub); letter-spacing: .3em; font-weight: 300; font-size: 13px; text-shadow: 0 1px 3px rgba(0, 0, 0, .6); }
.bar-group { display: flex; gap: 6px; align-items: center; }
.btn {
  border: 1px solid var(--line); background: var(--raised); color: var(--text); border-radius: 999px;
  padding: 6px 14px; min-height: 32px; font-size: 13px; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--sub); }
.btn:focus-visible, .chip:focus-visible, .range:focus-visible, summary:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }
.btn:disabled { opacity: .4; }
.btn-main { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }
.btn-main:disabled { opacity: .35; }
.btn-ghost { border: 0; background: none; padding: 6px 4px; }

/* ---------- 下のプリセット帯・強さ ---------- */
.side { flex: 0 0 auto; background: var(--bg); }
.dock { padding: 10px 0 calc(var(--sab) + 12px); }
.strip { display: flex; gap: 10px; overflow-x: auto; padding: 4px calc(var(--sar) + 14px) 10px calc(var(--sal) + 14px); scrollbar-width: none; }
.strip::-webkit-scrollbar { display: none; }
.chip { flex: 0 0 62px; background: none; border: 0; text-align: center; color: var(--sub); min-width: 0; }
.chip .th { display: block; aspect-ratio: 1; border-radius: 6px; background: var(--raised); border: 1px solid var(--line); }
.chip-name { display: block; margin-top: 5px; font-size: 11px; letter-spacing: .08em; white-space: nowrap; }
.chip .th { overflow: hidden; }
.chip .th canvas { display: block; width: 100%; height: 100%; object-fit: cover; }
.chip:disabled .chip-name, .chip.is-off .chip-name { opacity: .55; }
.chip { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
.chip-name { overflow: hidden; text-overflow: ellipsis; }
/* 自作と組込みの区切り（控えめな縦線）と「＋」チップ */
.chip-sep { flex: 0 0 1px; align-self: flex-start; height: 62px; margin: 0 2px; background: var(--line); }
.chip-sep[hidden] { display: none; }
.chip-add .th { background: none; border: 1px dashed var(--sub); position: relative; }
.chip-add .th::before, .chip-add .th::after { content: ""; position: absolute; left: 50%; top: 50%; background: var(--sub); }
.chip-add .th::before { width: 18px; height: 1px; margin: -.5px 0 0 -9px; }
.chip-add .th::after { width: 1px; height: 18px; margin: -9px 0 0 -.5px; }
.chip-add:hover .th { border-color: var(--text); }
.chip-add:hover .th::before, .chip-add:hover .th::after { background: var(--text); }
.chip.is-active { color: var(--text); }
.chip.is-active .th { border-color: var(--amber); box-shadow: 0 0 0 1px var(--amber); }
.dock-tools { display: flex; align-items: center; gap: 8px; padding: 4px calc(var(--sar) + 14px) 0 calc(var(--sal) + 14px); }
.adj.dock-strength { flex: 1 1 auto; min-width: 0; grid-template-columns: 2.4em 1fr 2.6em; gap: 8px; }
.btn-icon { min-width: 36px; padding: 6px 10px; font-size: 17px; line-height: 1; }
.sheet-tools { display: flex; align-items: center; gap: 6px; }

/* ---------- スライダー共通 ---------- */
.adj { display: grid; grid-template-columns: 5.6em 1fr 3.4em; align-items: center; gap: 10px; min-height: 30px; }
.lbl { color: var(--sub); font-size: 12.5px; white-space: nowrap; }
.val { text-align: right; font-size: 12.5px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.range {
  -webkit-appearance: none; appearance: none; width: 100%; height: 28px; margin: 0; background: transparent;
  --a: 0%; --b: 0%; touch-action: pan-y; cursor: pointer;
}
.range:disabled { opacity: .45; cursor: default; }
.range::-webkit-slider-runnable-track {
  height: 28px; border-radius: 2px;
  background: linear-gradient(to right, var(--line) 0 var(--a), var(--sub) var(--a) var(--b), var(--line) var(--b) 100%) center / 100% 2px no-repeat;
}
.range::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; margin-top: 6px; border-radius: 50%; background: var(--amber); border: 0; box-shadow: 0 0 0 3px rgba(14, 15, 17, .8); }
.range::-moz-range-track { height: 2px; background: linear-gradient(to right, var(--line) 0 var(--a), var(--sub) var(--a) var(--b), var(--line) var(--b) 100%); }
.range::-moz-range-thumb { width: 16px; height: 16px; border-radius: 50%; background: var(--amber); border: 0; box-shadow: 0 0 0 3px rgba(14, 15, 17, .8); }

/* ---------- 調整パネル（iPhone：下から出るシート） ---------- */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; max-height: 72vh; max-height: 72dvh;
  display: flex; flex-direction: column; background: var(--surface); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
  transform: translateY(100%); visibility: hidden; transition: transform .25s ease, visibility 0s .25s;
}
.sheet.is-open { transform: none; visibility: visible; transition: transform .25s ease; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px calc(var(--sar) + 18px) 6px calc(var(--sal) + 18px); flex: 0 0 auto; }
.panel-h { color: var(--sub); letter-spacing: .2em; font-size: 12px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 calc(var(--sar) + 18px) calc(var(--sab) + 20px) calc(var(--sal) + 18px); }

.grp { border-top: 1px solid var(--line); }
.grp > summary { list-style: none; cursor: pointer; padding: 11px 0; font-size: 13px; letter-spacing: .1em; display: flex; align-items: center; gap: 8px; }
.grp > summary::-webkit-details-marker { display: none; }
.grp > summary::before { content: "▸"; color: var(--sub); font-size: 10px; transition: transform .15s; }
.grp[open] > summary::before { transform: rotate(90deg); }
.grp-body { padding: 0 0 12px; display: flex; flex-direction: column; gap: 2px; }
.band { padding: 6px 0 4px; }
.band-h { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text); margin-bottom: 2px; }
.swatch { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }

/* 調整中（iPhone 幅）：シートを重ねず下に並べ、写真は残りの高さに縮めて全体を見せる */
@media (max-width: 899px) {
  .app.is-adjusting .dock { display: none; }
  .app.is-adjusting .sheet {
    position: static; transform: none; visibility: visible; transition: none;
    height: 48vh; height: 48dvh; max-height: none;
  }
  .app.is-adjusting .bar { background: none; }
  /* 上のボタンの下から写真を始める */
  .app.is-adjusting #view { top: calc(var(--sat) + 52px); height: calc(100% - var(--sat) - 52px); }
}

/* ---------- Mac 幅：右に 320px のパネル ---------- */
@media (min-width: 900px) {
  .app { flex-direction: row; }
  .side {
    flex: 0 0 var(--panel-w); width: var(--panel-w); min-height: 0; display: flex; flex-direction: column;
    background: var(--surface); border-left: 1px solid var(--line); padding-top: var(--sat);
  }
  .dock { flex: 0 0 auto; padding: 16px 0 12px; }
  .strip { display: grid; grid-template-columns: repeat(5, 1fr); gap: 12px 8px; overflow: visible; padding: 0 18px 14px; }
  .chip { flex: none; }
  .chip-sep { grid-column: 1 / -1; width: auto; height: 1px; margin: 2px 0; }
  .dock-tools { padding: 0 18px; }
  .sheet-tools .js-undo, .sheet-tools .js-redo, .sheet-tools .js-reset { display: none; }
  .sheet {
    position: static; transform: none; visibility: visible; max-height: none; flex: 1 1 auto; min-height: 0;
    border: 0; border-top: 1px solid var(--line); border-radius: 0; transition: none;
  }
  .sheet-close, .btn-adjust { display: none; }
  .sheet-head, .sheet-body { padding-left: 18px; padding-right: 18px; }
  .sheet-body { padding-bottom: 20px; }
}

/* ---------- 比較（長押し・分割） ---------- */
.stage { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.stage.is-split { touch-action: none; cursor: ew-resize; }
.btn[aria-pressed="true"] { border-color: var(--amber); color: var(--amber); }
.cmp-overlay { position: absolute; z-index: 1; pointer-events: none; overflow: hidden; --cmp-top: 8px; --cmp-x: 50%; }
.cmp-label {
  position: absolute; top: var(--cmp-top); padding: 2px 9px; border-radius: 999px; font-size: 11px; letter-spacing: .1em;
  background: rgba(14, 15, 17, .62); color: var(--text); border: 1px solid rgba(232, 230, 225, .16);
}
.cmp-left { left: 10px; }
.cmp-right { right: 10px; }
.cmp-line { position: absolute; top: 0; bottom: 0; left: var(--cmp-x); width: 1px; margin-left: -.5px; background: rgba(232, 230, 225, .85); box-shadow: 0 0 0 .5px rgba(0, 0, 0, .35); }
.cmp-handle {
  position: absolute; top: 50%; left: 50%; width: 26px; height: 26px; margin: -13px 0 0 -13px; border-radius: 50%;
  background: rgba(14, 15, 17, .6); border: 1.5px solid var(--text); box-shadow: 0 0 0 3px rgba(14, 15, 17, .35);
}

/* ---------- 書き出し画面 ---------- */
.xsheet {
  position: fixed; inset: auto 0 0 0; margin: 0; width: 100%; max-width: none; max-height: 90dvh; overflow: auto;
  padding: 0; color: var(--text); background: var(--surface); border: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
}
.xsheet::backdrop { background: rgba(0, 0, 0, .55); }
.xs-body { padding: 18px calc(var(--sar) + 18px) calc(var(--sab) + 18px) calc(var(--sal) + 18px); display: flex; flex-direction: column; gap: 12px; }
.xs-title { font-size: 12px; font-weight: 400; letter-spacing: .2em; color: var(--sub); }
.seg { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 2px; gap: 2px; }
.seg-btn { border: 0; background: none; border-radius: 8px; padding: 8px 2px; min-height: 40px; font-size: 12.5px; color: var(--sub); white-space: nowrap; }
.seg-btn[aria-checked="true"] { background: var(--raised); color: var(--text); box-shadow: inset 0 0 0 1px var(--amber); }
.seg-btn:disabled { opacity: .5; }
.seg-btn:focus-visible { outline: 2px solid var(--amber); outline-offset: 1px; }
.xs-size { font-size: 13px; font-variant-numeric: tabular-nums; }
.xs-note { font-size: 11.5px; color: var(--sub); margin-top: -6px; }
.xs-status { font-size: 12.5px; min-height: 1.5em; color: var(--text); }
.xs-status.is-warn { color: var(--warn); }
.xs-text { font-size: 14px; line-height: 1.6; overflow-wrap: anywhere; }
.xs-input {
  width: 100%; min-height: 44px; padding: 8px 12px; font: inherit; font-size: 16px; /* 16px：iPhone の自動拡大を防ぐ */
  color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
}
.xs-input:focus-visible { outline: 2px solid var(--amber); outline-offset: 1px; }
.xs-stack { flex-direction: column; align-items: stretch; }
.xs-stack .btn { width: 100%; }
.xs-actions.xs-stack .btn-ghost { margin-right: 0; }
.xs-actions { display: flex; justify-content: flex-end; align-items: center; gap: 8px; }
.xs-actions .btn-ghost { margin-right: auto; color: var(--sub); }
.xs-actions .btn { min-height: 40px; padding-left: 18px; padding-right: 18px; }
.xs-actions .btn-ghost { padding-left: 4px; padding-right: 4px; }
@media (min-width: 900px) {
  .xsheet { inset: 0; margin: auto; width: 360px; border: 1px solid var(--line); border-radius: 16px; }
  .xs-body { padding: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet.is-open, .empty { transition: none; }
  .xsheet, .xsheet::backdrop { animation: none; transition: none; }
}
/* 小さい画面：ボタン 5 つを 1 行に収める */
@media (max-width: 480px) {
  .title { display: none; }
  .bar-group { width: 100%; justify-content: space-between; gap: 4px; }
  .btn { padding: 6px 10px; }
}

/* 自作プリセットのダイアログ：空の注意欄は詰める／削除は警告色 */
.xs-status.is-warn:empty { display: none; }
.btn-main.btn-danger { background: var(--warn); border-color: var(--warn); color: var(--text); }

/* お手本の範囲を選ぶ画面 */
.ref-note { margin-top: -4px; }
.ref-wrap { display: flex; justify-content: center; background: #000; border-radius: 10px; overflow: hidden; }
.ref-canvas { display: block; max-width: 100%; max-height: 58dvh; width: auto; height: auto; touch-action: none; cursor: crosshair; }
@media (min-width: 900px) {
  .xsheet.ref-sheet { width: 560px; }
}
.ref-sheet .xs-actions { flex-wrap: wrap; gap: 6px; }
.ref-sheet .xs-actions .btn:not(.btn-ghost) { padding-left: 14px; padding-right: 14px; }

/* 追加3：自作の絵を選ぶ列・プリセットの分類見出し */
.xs-icons { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.xs-icon { padding: 0; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: none; aspect-ratio: 1; }
.xs-icon canvas { display: block; width: 100%; height: 100%; }
.xs-icon[aria-checked="true"] { border-color: var(--amber); box-shadow: 0 0 0 1px var(--amber); }
.xs-icon:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }
.chip-group { flex: 0 0 auto; align-self: flex-start; writing-mode: vertical-rl; font-size: 10px; letter-spacing: .25em; color: var(--sub); height: 62px; padding: 0 2px; border-left: 1px solid var(--line); padding-left: 6px; }
@media (min-width: 900px) {
  .chip-group { grid-column: 1 / -1; writing-mode: horizontal-tb; height: auto; border-left: 0; padding: 6px 0 0; border-top: 1px solid var(--line); margin-top: 2px; }
}
.chip-sep + .chip-group { border-left: 0; padding-left: 2px; }
@media (min-width: 900px) {
  /* 追加3：プリセットが24種に増えたので、帯だけスクロール（強さ・調整が押し出されないように） */
  .strip { max-height: 42vh; overflow-y: auto; overscroll-behavior: contain; padding-top: 2px; }
}
/* 強さを1行まるごと使い、↶ ↷ とリセットはその下の行に（iPhone・Mac 共通。↷ を足して横が狭くなったため） */
.dock-tools { flex-wrap: wrap; row-gap: 6px; gap: 6px; }
.adj.dock-strength { order: -1; flex-basis: 100%; }
.dock-tools .js-reset { margin-left: auto; }

/* 入口画面（合言葉） */
.gate {
  position: fixed; inset: 0; z-index: 1000; background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: calc(24px + var(--sat)) calc(24px + var(--sar)) calc(24px + var(--sab)) calc(24px + var(--sal));
  overflow-y: auto;
}
.gate[hidden] { display: none; }
.gate-box { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 24px; }
.gate-title { margin: 0; display: flex; align-items: center; gap: 10px; font-size: 18px; font-weight: 600; letter-spacing: .08em; color: var(--text); }
.gate-dots { display: inline-flex; }
.gate-dots i { width: 14px; height: 14px; border-radius: 50%; display: block; }
.gate-dots i:first-child { background: var(--amber); }
.gate-dots i:last-child { background: var(--teal); margin-left: -4px; }
#gate-form { display: flex; flex-direction: column; gap: 10px; }
.gate-label { font-size: 12px; color: var(--sub); }
.gate-input {
  width: 100%; box-sizing: border-box; min-height: 44px; padding: 8px 14px; font: inherit; font-size: 16px;
  color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
}
.gate-input:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }
#gate-go { min-height: 44px; font-size: 16px; margin-top: 4px; }
.gate-msg { margin: 0; min-height: 1.5em; font-size: 13px; color: var(--sub); }
.gate-msg.is-warn { color: var(--warn); }
.gate-note { margin: 0; font-size: 11px; color: var(--sub); }
.gate.is-busy .gate-input, .gate.is-busy .btn { opacity: .4; }
/* ログイン状態を確かめている間は入力欄を見せない（入れている人に一瞬出さない） */
.gate.is-checking #gate-form { visibility: hidden; }
/* パスワード保存（キーチェーン）用のユーザー名欄。見えない所に置く（hidden だと Safari が組にしないことがある） */
.gate-user { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }
