/* #9 カラーピッカー — Design System「Sandbox No.12」のトークンと部品で組む。
   トークンは No.12 の tokens.json（基準に合わせて調整済みの値）。テーマは <html data-theme> が優先、無ければ OS 設定。 */

/* ============ トークン ============ */
:root {
  --bg: #f7f7f8; --surface: #ffffff; --surface-2: #f1f2f4; --border: #e3e5e8; --border-strong: #858a97;
  --text: #17181c; --muted: #5c6170; --faint: #6b7080;
  --accent: #4f46e5; --accent-fg: #ffffff; --accent-weak: #eef0fe; --accent-weak-fg: #4338ca;
  --err: #b91c1c; --err-bg: #fef2f2; --warn: #b45309; --warn-bg: #fffbeb; --info: #2563eb; --info-bg: #eff6ff;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 1px 3px rgba(16,24,40,.06);
  --pill-shadow: 0 1px 2px rgba(16,24,40,.12);
  --checker-a: #c9ccd2; --checker-b: #9ea3ad;
  color-scheme: light;

  --font-sans: Inter, "Noto Sans JP", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, monospace;

  --radius-xs: 3px; --radius-sm: 4px; --radius-tip: 6px; --radius-control: 7px; --radius-seg: 8px; --radius-pane: 10px;
  --control-height: 32px; --icon-size: 16px; --content-width: 1120px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d0e11; --surface: #15171c; --surface-2: #1c1f26; --border: #2a2e37; --border-strong: #666d7b;
    --text: #e7e9ee; --muted: #a1a7b5; --faint: #858b9b;
    --accent: #818cf8; --accent-fg: #0d0e11; --accent-weak: #22244a; --accent-weak-fg: #c7d2fe;
    --err: #f87171; --err-bg: #2a1416; --warn: #fbbf24; --warn-bg: #2a2110; --info: #93c5fd; --info-bg: #142033;
    --shadow: none; --pill-shadow: 0 1px 2px rgba(0,0,0,.5);
    --checker-a: #3a3f4a; --checker-b: #262a32;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d0e11; --surface: #15171c; --surface-2: #1c1f26; --border: #2a2e37; --border-strong: #666d7b;
  --text: #e7e9ee; --muted: #a1a7b5; --faint: #858b9b;
  --accent: #818cf8; --accent-fg: #0d0e11; --accent-weak: #22244a; --accent-weak-fg: #c7d2fe;
  --err: #f87171; --err-bg: #2a1416; --warn: #fbbf24; --warn-bg: #2a2110; --info: #93c5fd; --info-bg: #142033;
  --shadow: none; --pill-shadow: 0 1px 2px rgba(0,0,0,.5);
  --checker-a: #3a3f4a; --checker-b: #262a32;
  color-scheme: dark;
}

/* ============ 基本 ============ */
* , *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); font: 14px/1.5 var(--font-sans); }
h1, h2, p { margin: 0; }
[hidden] { display: none !important; }
svg { width: var(--icon-size); height: var(--icon-size); stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
button, select, input, textarea { font: inherit; color: inherit; margin: 0; }
::selection { background: var(--accent-weak); color: var(--text); }

/* ============ 部品（No.12 components/bundle.css から、このページで使う分） ============ */

/* ---- header ---- */
header.app { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface); }
.logo { display: flex; align-items: center; gap: 10px; }
.logo-mark { width: 28px; height: 28px; border-radius: var(--radius-control); background: var(--accent); color: var(--accent-fg); display: grid; place-items: center; font: 600 13px var(--font-mono); }
.logo-name { font-size: 15px; font-weight: 600; line-height: 1.5; }
.spacer { flex: 1; }
.hint { font-size: 12px; color: var(--muted); }

/* ---- pane ---- */
.pane { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-pane); box-shadow: var(--shadow); min-width: 0; }
.pane-head { display: flex; align-items: center; gap: 8px; min-height: 45px; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--border); }
.pane-title { font-size: 12px; font-weight: 600; line-height: 1.5; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); white-space: nowrap; }
.pane-meta { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: 12px/1.5 var(--font-mono); color: var(--muted); }
.pane-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.pane-body { padding: 14px; }

/* ---- button ---- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: var(--control-height); padding: 0 12px; border-radius: var(--radius-control); border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; font-weight: 500; font-size: 13px; line-height: 1; white-space: nowrap; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.icon { width: var(--control-height); padding: 0; flex: 0 0 auto; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:disabled:hover { background: var(--surface); filter: none; }
.btn.primary:disabled:hover { background: var(--accent); }

/* ---- text input ---- */
.in { height: var(--control-height); width: 100%; padding: 0 10px; border-radius: var(--radius-control); border: 1px solid var(--border-strong); background: var(--surface); font-size: 14px; transition: border-color .12s; }
.in--hex { font: 13px var(--font-mono); font-variant-numeric: tabular-nums; }
.in::placeholder { color: var(--faint); }
.in:hover { border-color: var(--muted); }
.in:disabled { background: var(--surface-2); border-color: var(--border); color: var(--faint); cursor: not-allowed; }
.in.is-invalid { border-color: var(--err); background-color: var(--err-bg); }

/* ---- select ---- */
select { height: var(--control-height); width: 100%; padding: 0 28px 0 10px; border-radius: var(--radius-control); border: 1px solid var(--border-strong); background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238b91a1' stroke-width='2'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 8px center / 14px; appearance: none; cursor: pointer; font-size: 13px; color: var(--text); }
select:hover { border-color: var(--muted); }

/* ---- checkbox ---- */
.cb { appearance: none; width: 16px; height: 16px; margin: 0; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: var(--surface); display: inline-grid; place-content: center; cursor: pointer; flex: 0 0 auto; }
.cb:hover { border-color: var(--muted); }
.cb:checked { background: var(--accent); border-color: var(--accent); }
.cb:checked::after { content: ""; width: 9px; height: 5px; border-left: 2px solid var(--accent-fg); border-bottom: 2px solid var(--accent-fg); transform: translateY(-1px) rotate(-45deg); }
.cb:disabled { opacity: .45; cursor: not-allowed; }
.cbl { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; user-select: none; }
.cbl:has(.cb:disabled) { color: var(--faint); cursor: not-allowed; }

/* ---- segmented ---- */
.seg { display: inline-flex; padding: 2px; gap: 2px; border-radius: var(--radius-seg); background: var(--surface-2); border: 1px solid var(--border); }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 12px; border-radius: var(--radius-tip); font-size: 13px; font-weight: 500; color: var(--muted); white-space: nowrap; }
.seg label:hover span { color: var(--text); }
.seg input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--pill-shadow); }

/* ---- focus ---- */
.btn:focus-visible, .in:focus-visible, select:focus-visible, .cb:focus-visible, input[type=range]:focus-visible,
.seg input:focus-visible + span, .tip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.in.is-invalid:focus-visible { outline-color: var(--err); }

/* ---- tooltip（反転配色） ---- */
.tip { position: relative; }
.tip::after { content: attr(data-tip); position: absolute; left: 50%; bottom: calc(100% + 8px); transform: translate(-50%, 4px); padding: 4px 8px; border-radius: var(--radius-tip); background: var(--text); color: var(--bg); font: 500 12px/1.4 var(--font-sans); white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity .12s, transform .12s; transition-delay: 0s; z-index: 5; }
.tip[data-pos="bottom"]::after { bottom: auto; top: calc(100% + 8px); transform: translate(-50%, -4px); }
.tip:hover::after, .tip:focus-visible::after { opacity: 1; transform: translate(-50%, 0); transition-delay: .4s; }
.tip:focus-visible::after { transition-delay: 0s; }
.tip[data-align="end"]::after { left: auto; right: 0; transform: translate(0, -4px); }
.tip[data-align="end"]:hover::after, .tip[data-align="end"]:focus-visible::after { transform: none; }
.tip:disabled::after { display: none; }

/* ---- message（JS が文言だけを入れる器。空なら畳む） ---- */
.msg { display: flex; align-items: flex-start; gap: 8px; min-height: var(--control-height); padding: 7px 12px; border-radius: var(--radius-control); font-size: 12px; line-height: 1.5; }
.msg:empty { display: none; }
.msg.err { color: var(--err); background: var(--err-bg); }
.msg.warn { color: var(--warn); background: var(--warn-bg); }
.msg.info { color: var(--info); background: var(--info-bg); }
.msg[data-icon]::before { content: ""; flex: 0 0 auto; width: 16px; height: 16px; margin-top: 1px; background: currentColor; -webkit-mask: var(--msg-icon) center / 16px 16px no-repeat; mask: var(--msg-icon) center / 16px 16px no-repeat; }
.msg[data-icon="info"] { --msg-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cpath d='M12 16v-4M12 8h.01'/%3E%3C/svg%3E"); }
.msg[data-icon="warn"] { --msg-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M10.3 3.9 1.8 18a2 2 0 0 0 1.7 3h17a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0z'/%3E%3Cpath d='M12 9v4M12 17h.01'/%3E%3C/svg%3E"); }

/* ---- field ---- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-label { font-size: 13px; color: var(--muted); white-space: nowrap; }

/* ============ カラーピッカー ============ */
/* PC は左に画像、右に色のペイン。860px 以下は縦積み。 */

.cp-noscript { margin: 24px 20px 0; }

.cp-main {
  max-width: var(--content-width);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  align-items: start;
  gap: 20px;
  padding: 24px 20px 64px;
}

.cp-col { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.cp-stack { display: flex; flex-direction: column; gap: 14px; }

/* ---- 画像 ---- */

.cp-image-body { display: flex; flex-direction: column; gap: 12px; }

.cp-drop {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 300px;
  max-height: 62vh;
  background: var(--surface-2);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-control);
  cursor: pointer;
  overflow: hidden;
  transition: border-color .12s, background-color .12s;
}
.cp-drop:hover { border-color: var(--muted); }
.cp-drop.is-over { border-color: var(--accent); border-style: solid; background: var(--accent-weak); }
.cp-drop.has-image { border-style: solid; border-color: var(--border); cursor: crosshair; }
.cp-drop:has(.cp-file:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

/* input は見えなくするが label のクリックで開くようにする。 */
.cp-file { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.cp-canvas {
  display: block;
  max-width: 100%;
  max-height: 62vh;
  object-fit: contain;
  cursor: crosshair;
  touch-action: none;
  /* 透明部分が見えるように市松模様を敷く。 */
  background-color: var(--checker-b);
  background-image:
    linear-gradient(45deg, var(--checker-a) 25%, transparent 25%, transparent 75%, var(--checker-a) 75%),
    linear-gradient(45deg, var(--checker-a) 25%, transparent 25%, transparent 75%, var(--checker-a) 75%);
  background-size: 16px 16px;
  background-position: 0 0, 8px 8px;
}

.cp-drop__icon { display: grid; place-items: center; width: 40px; height: 40px; border-radius: var(--radius-control); background: var(--surface); border: 1px solid var(--border); color: var(--muted); }
.cp-drop.has-image .cp-drop__icon { display: none; }
.cp-drop__hint { font-size: 13px; font-weight: 500; color: var(--text); text-align: center; padding: 0 12px; }

.cp-drop.has-image .cp-drop__hint {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 4px 8px;
  border-radius: var(--radius-tip);
  background: var(--text);
  color: var(--bg);
  font-size: 12px;
  font-weight: 500;
  line-height: 1.4;
}

.cp-sample { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }

.cp-hover {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: var(--control-height);
  font: 13px var(--font-mono);
  color: var(--text);
  visibility: hidden;
}
.cp-hover.is-on { visibility: visible; }
.cp-hover__sw { width: 20px; height: 20px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); }

/* ---- チップ（頻出色・パレット） ---- */

.cp-chips { display: flex; flex-wrap: wrap; gap: 10px 16px; min-height: var(--control-height); }
.cp-chips--list { flex-direction: column; flex-wrap: nowrap; gap: 8px; }
.cp-chips--list:has(.cp-chip) { min-height: 0; }

.cp-chips__empty { align-self: center; font-size: 13px; color: var(--faint); }

.cp-chip { display: inline-flex; align-items: center; gap: 8px; font: 12px/1.5 var(--font-mono); color: var(--text); }
.cp-chips--list .cp-chip { display: flex; width: 100%; }

/* 色見本は押すと現在色になる。 */
.cp-chip__sw {
  flex: 0 0 auto;
  width: var(--control-height);
  height: var(--control-height);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 0;
  cursor: pointer;
}
.cp-chip__sw:hover { border-color: var(--text); }
.cp-chip__sw:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.cp-chip__hex { font-variant-numeric: tabular-nums; }
.cp-chips--list .cp-chip__hex { width: 4.8em; flex: 0 0 auto; }
.cp-chip__ratio { color: var(--muted); }

.cp-chip__name { flex: 1 1 auto; min-width: 0; font-size: 13px; }

.cp-chip__x:hover { color: var(--err); }

/* ---- 現在色 ---- */

.cp-current { display: flex; gap: 14px; align-items: stretch; }

.cp-current__sw {
  flex: 0 0 auto;
  width: 88px;
  height: 88px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 0;
  cursor: pointer;
}
.cp-current__sw:hover { border-color: var(--text); }

.cp-current__values { display: flex; flex-direction: column; justify-content: center; gap: 4px; min-width: 0; font: 12px/1.6 var(--font-mono); color: var(--muted); }
.cp-current__hex { display: flex; align-items: center; gap: 8px; margin-bottom: 2px; }
.cp-current__hex .in { width: 112px; }

.cp-hsv { display: flex; flex-direction: column; gap: 10px; }
.cp-hsv__row {
  display: grid;
  grid-template-columns: 1.2em minmax(0, 1fr) 3.4em;
  align-items: center;
  gap: 12px;
  font: 500 13px var(--font-mono);
  color: var(--muted);
}

.cp-hsv__row output,
.cp-blend-opacity__row output { font: 13px var(--font-mono); font-variant-numeric: tabular-nums; text-align: right; color: var(--text); }

/* スライダーは背景を JS で色相・彩度・明度の帯にするので、溝の枠とつまみだけ描く。 */
.cp-hsv__row input[type="range"],
.cp-blend-opacity__row input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 14px;
  margin: 0;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.cp-hsv__row input[type="range"]::-webkit-slider-thumb,
.cp-blend-opacity__row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 22px;
  background: var(--surface);
  border: 2px solid var(--text);
  border-radius: var(--radius-sm);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
  cursor: pointer;
}
.cp-hsv__row input[type="range"]::-moz-range-thumb,
.cp-blend-opacity__row input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 22px;
  box-sizing: border-box;
  background: var(--surface);
  border: 2px solid var(--text);
  border-radius: var(--radius-sm);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
  cursor: pointer;
}

/* ---- 合成 ---- */

.cp-blend-top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 16px; }

.cp-blend-controls { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 16px; }
.cp-blend-controls .field:first-child { width: 150px; }
.cp-blend-opacity { flex: 1 1 160px; }
.cp-blend-opacity__row { display: grid; grid-template-columns: minmax(0, 1fr) 3.4em; align-items: center; gap: 12px; height: var(--control-height); }
.cp-blend-opacity__row input[type="range"] { background: linear-gradient(to right, var(--surface-2), var(--muted)); }

.cp-slots { display: flex; flex-direction: column; gap: 8px; }
.cp-slot {
  display: grid;
  grid-template-columns: var(--control-height) auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
}
.cp-slot__name { white-space: nowrap; }
.cp-slot__sw {
  width: var(--control-height);
  height: var(--control-height);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-control);
  padding: 0;
  cursor: pointer;
}
.cp-slot__sw:hover { border-color: var(--text); }

/* ============ 狭い画面 ============ */

@media (max-width: 860px) {
  header.app { padding-left: 16px; padding-right: 16px; }
  .hint { display: none; }
  .cp-main { grid-template-columns: minmax(0, 1fr); padding: 16px 16px 48px; gap: 16px; }
  .cp-col { gap: 16px; }
}

/* タッチ向けに操作部品を 44px に広げる。 */
@media (max-width: 640px) {
  :root, :root[data-theme] { --control-height: 44px; }
  .in, select { font-size: 16px; }
  .in--hex { font-size: 15px; }
  .seg span { height: 38px; padding: 0 14px; }
  .cb { width: 22px; height: 22px; }
  .cb:checked::after { width: 11px; height: 6px; }
  .cbl { min-height: 44px; font-size: 14px; }

  .cp-drop { min-height: 200px; max-height: 50vh; }
  .cp-canvas { max-height: 50vh; }
  .cp-hover { margin-left: 0; width: 100%; height: 24px; }
  .cp-current__sw { width: 76px; height: 76px; }

  .cp-slot { grid-template-columns: var(--control-height) auto minmax(0, 1fr); }
  .cp-slot__take { grid-column: 2 / -1; justify-self: start; }

  .cp-hsv__row input[type="range"],
  .cp-blend-opacity__row input[type="range"] { height: 22px; }
  .cp-hsv__row input[type="range"]::-webkit-slider-thumb,
  .cp-blend-opacity__row input[type="range"]::-webkit-slider-thumb { width: 16px; height: 32px; }
  .cp-hsv__row input[type="range"]::-moz-range-thumb,
  .cp-blend-opacity__row input[type="range"]::-moz-range-thumb { width: 16px; height: 32px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; transition-delay: 0s !important; animation-duration: 0s !important; }
}
