/*
 * Pixsmith スタイルシート
 * レイアウト方針（DESIGN.md参照）：
 * - PC（960px以上）：左ツールバー・中央キャンバス・右設定パネルの3カラム
 * - スマートフォン・タブレット（960px未満）：上ヘッダー・中央キャンバス・下部固定ツールバー
 */

:root {
  --color-bg: #f4f4f5;
  --color-surface: #ffffff;
  --color-border: #d4d4d8;
  --color-text: #27272a;
  --color-text-muted: #71717a;
  --color-accent: #4f46e5;
  --header-height: 48px;
  --toolbar-size: 64px;
  /* タップターゲットの最小サイズ（アクセシビリティ要件） */
  --tap-target: 44px;
}

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

html,
body {
  height: 100%;
}

body {
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", "Noto Sans JP", sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
  display: flex;
  flex-direction: column;
  /* キャンバス操作中にページ全体がスクロールしないよう固定 */
  overflow: hidden;
}

/* ヘッダー */

.app-header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.app-logo {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.header-nav {
  display: flex;
  gap: 8px;
}

.header-link {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 0 12px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--color-text-muted);
  text-decoration: none;
}

.header-link:hover,
.header-link:focus-visible {
  color: var(--color-accent);
  background: var(--color-bg);
}

/* メインレイアウト（PC：3カラム） */

.app-main {
  flex: 1;
  display: grid;
  grid-template-columns: var(--toolbar-size) 1fr 280px;
  min-height: 0;
}

/* ツールバー */

.toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 12px 8px;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
}

/* タップターゲット44px以上を確保したツールボタン */
.tool-button {
  width: var(--tap-target);
  min-height: var(--tap-target);
  padding: 4px;
  font-size: 11px;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
}

.tool-button:hover {
  border-color: var(--color-accent);
}

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

/* 選択中のツールを強調する */
.tool-button[aria-pressed="true"] {
  color: #ffffff;
  background: var(--color-accent);
  border-color: var(--color-accent);
}

.color-field {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* 黒・白をワンタップで塗り色にできる固定スウォッチ */
.preset-color {
  width: var(--tap-target);
  height: var(--tap-target);
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
}

.preset-color:hover {
  border-color: var(--color-accent);
}

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

#preset-black {
  background: #000000;
}

#preset-white {
  background: #ffffff;
}

.color-field input[type="color"] {
  width: var(--tap-target);
  height: var(--tap-target);
  padding: 2px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-bg);
  cursor: pointer;
}

/* スクリーンリーダー専用のテキスト（視覚上は非表示） */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* キャンバス領域 */

.canvas-area {
  position: relative;
  display: flex;
  padding: 24px;
  overflow: auto;
  /* ブラウザ既定のスクロール・ピンチズームを無効化し、Pointer Eventsで扱う */
  touch-action: none;
}

.canvas-stack {
  position: relative;
  /* 板が領域より小さいときは中央寄せ、大きいときはスクロールで全体を見られるようにする */
  margin: auto;
  flex-shrink: 0;
}

.canvas-stack canvas {
  display: block;
  /* iOS Safariの長押しメニュー・テキスト選択を抑止 */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#ui-canvas {
  position: absolute;
  inset: 0;
  /* UI表示専用。操作はメインcanvasで受ける */
  pointer-events: none;
}

#reference-image {
  position: absolute;
  top: 0;
  left: 0;
  /* 下絵は表示のみ。クリック・タッチは下のピクセル板へ通す */
  pointer-events: none;
}

/* 設定パネル */

.settings-panel {
  padding: 16px;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  overflow-y: auto;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.panel-title {
  font-size: 14px;
  font-weight: 700;
}

.panel-header .panel-close {
  width: auto;
  flex-shrink: 0;
  min-height: 36px;
  padding: 4px 16px;
}

.panel-header .panel-title {
  flex: 1;
  white-space: nowrap;
}

/* モバイル専用の要素はPCでは表示しない */
.mobile-only {
  display: none;
}

.panel-section {
  margin-bottom: 20px;
}

.section-title {
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--color-border);
}

.field {
  margin-bottom: 12px;
}

.field label {
  display: block;
  font-size: 12px;
  color: var(--color-text-muted);
  margin-bottom: 4px;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-row output {
  min-width: 2em;
  font-size: 13px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* スライダーはタッチで操作しやすい高さを確保する */
.field input[type="range"] {
  flex: 1;
  min-height: var(--tap-target);
  accent-color: var(--color-accent);
}

.field input[type="number"] {
  width: 100%;
  min-height: 36px;
  padding: 4px 8px;
  font-size: 14px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
}

.field select {
  width: 100%;
  min-height: 36px;
  padding: 4px 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 6px;
}

/* スライダーと並べる数値入力は固定幅にして、スライダー側へ幅を譲る */
.field .field-row input[type="number"] {
  width: 72px;
  flex-shrink: 0;
  text-align: right;
}

/* 設定パネル内のカラーピッカーは横幅いっぱいに広げてタップしやすくする */
.field input[type="color"] {
  width: 100%;
  height: 36px;
  padding: 2px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-surface);
  cursor: pointer;
}

/* チェックボックス付きラベル。タップしやすい高さを確保する */
.check-field {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  font-size: 13px;
  color: var(--color-text);
  cursor: pointer;
}

.check-field input[type="checkbox"],
.check-field input[type="radio"] {
  width: 18px;
  height: 18px;
  accent-color: var(--color-accent);
}

/* 色履歴のスウォッチ */
.swatch-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.swatch {
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  cursor: pointer;
}

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

/* 行・列の追加/削除ボタン群 */
.field-label {
  display: block;
  font-size: 12px;
  color: var(--color-text-muted);
  margin-bottom: 4px;
}

.edge-controls {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}

.edge-row {
  display: flex;
  align-items: center;
  gap: 4px;
}

.edge-name {
  font-size: 12px;
  color: var(--color-text);
  min-width: 1.5em;
}

.edge-button {
  width: 40px;
  height: 40px;
  font-size: 16px;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
}

.edge-button:hover {
  border-color: var(--color-accent);
}

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

.section-note {
  font-size: 11px;
  line-height: 1.5;
  color: var(--color-text-muted);
  margin-bottom: 8px;
}

/* 設定パネル内の操作ボタン */
.action-button {
  width: 100%;
  min-height: var(--tap-target);
  padding: 8px 12px;
  font-size: 13px;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
}

.action-button:hover {
  border-color: var(--color-accent);
}

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

.action-button[aria-pressed="true"],
.action-button.action-primary {
  color: #ffffff;
  background: var(--color-accent);
  border-color: var(--color-accent);
}

.action-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ドラッグ&ドロップの受け入れ中はキャンバス領域を強調する */
.canvas-area.is-drop-target {
  outline: 2px dashed var(--color-accent);
  outline-offset: -8px;
}

/* テキストカード：入力した文字をますに転写するオーバーレイ */
.text-card {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
  /* 入力・ドラッグともブラウザ既定のタッチ動作に任せる */
  touch-action: auto;
}

.text-card-header {
  position: absolute;
  bottom: 100%;
  left: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px;
  margin-bottom: 8px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  white-space: nowrap;
}

/* カードをつかんで動かすためのつまみ */
.text-card-grip {
  padding: 0 8px;
  font-size: 18px;
  color: var(--color-text-muted);
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.text-card-button {
  min-width: 36px;
  height: 36px;
  padding: 0 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--color-text);
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  cursor: pointer;
}

.text-card-button:hover {
  border-color: var(--color-accent);
}

.text-card-button:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

.text-card-primary {
  color: #ffffff;
  background: var(--color-accent);
  border-color: var(--color-accent);
}

.text-card-header input[type="color"] {
  width: 36px;
  height: 36px;
  padding: 2px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-surface);
  cursor: pointer;
}

/* 入力欄。転写結果と近い見た目になるよう太字・行間1で表示する */
.text-card-input {
  font-weight: 700;
  line-height: 1;
  white-space: pre;
  min-width: 0.5em;
  min-height: 1em;
  outline: 2px dashed var(--color-accent);
  outline-offset: 2px;
  cursor: text;
}

/* 下絵をつかめる状態（移動モード）とつかんでいる最中のカーソル表示 */
.canvas-area.is-move-mode {
  cursor: grab;
}

.canvas-area.is-dragging-reference {
  cursor: grabbing;
}


/* スマートフォン・タブレット（960px未満）：下部固定ツールバー */

@media (max-width: 959px) {
  .app-main {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }

  .toolbar {
    /* キャンバスの下に固定表示する横並びツールバー */
    order: 2;
    flex-direction: row;
    justify-content: center;
    min-height: var(--toolbar-size);
    padding: 8px 12px;
    border-right: none;
    border-top: 1px solid var(--color-border);
    /* ホームバーと重ならないようセーフエリアを確保 */
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  .canvas-area {
    order: 1;
  }

  /* モバイル用の要素を表示する */
  .mobile-only {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* ツールバーはボタンが多いとき横スクロールできるようにする */
  .toolbar {
    overflow-x: auto;
    justify-content: flex-start;
  }

  /* 設定パネルは下から出るボトムシート。開いてもキャンバスの一部が見える高さに抑える */
  .settings-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    max-height: 60vh;
    border-left: none;
    border-top: 1px solid var(--color-border);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.15);
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    transform: translateY(100%);
    /* 閉じている間は操作もフォーカスも受けない。visibilityはスライドアウト後に切り替える */
    visibility: hidden;
    transition: transform 0.25s ease, visibility 0s linear 0.25s;
  }

  .settings-panel.is-open {
    transform: translateY(0);
    visibility: visible;
    transition: transform 0.25s ease;
  }

  /* iOS Safariが入力フォーカス時に自動ズームしないよう16px以上にする */
  .field input[type="number"],
  .field select {
    font-size: 16px;
  }
}
