/* フォントは src/main.js の FONTS 配列から FontFace API で登録 */

* {
  box-sizing: border-box;
}

:root {
  --app-bg: #f5f5f5;
  --surface: #fff;
  --surface-alt: #f8f8f8;
  --surface-muted: #fafafa;
  --stage-shell: #2b2b2b;
  --text: #222;
  --text-strong: #222;
  --text-muted: #555;
  --text-subtle: #888;
  --border: #ddd;
  --border-soft: #eee;
  --input-border: #ccc;
  --accent: #2a6df4;
  --accent-hover: #1f57c4;
  --accent-active: #14418f;
  --accent-soft: rgba(42, 109, 244, 0.12);
  --accent-hover-soft: rgba(42, 109, 244, 0.08);
  --menu-hover: #eef3ff;
  --button-disabled: #999;
  --danger: #d33;
  --danger-hover: #b22;
  --shadow: rgba(0, 0, 0, 0.2);
  --shadow-strong: rgba(0, 0, 0, 0.4);
  --inspector-width: 320px;
  --inspector-resizer-width: 8px;
}

body.theme-dark {
  color-scheme: dark;
  --app-bg: #1e1e1e;
  --surface: #252526;
  --surface-alt: #2d2d30;
  --surface-muted: #1f1f1f;
  --stage-shell: #181818;
  --text: #cccccc;
  --text-strong: #d4d4d4;
  --text-muted: #9cdcfe;
  --text-subtle: #858585;
  --border: #3c3c3c;
  --border-soft: #333333;
  --input-border: #3c3c3c;
  --accent: #007acc;
  --accent-hover: #1177bb;
  --accent-active: #094771;
  --accent-soft: rgba(0, 122, 204, 0.22);
  --accent-hover-soft: rgba(0, 122, 204, 0.16);
  --menu-hover: #2a2d2e;
  --button-disabled: #5a5a5a;
  --danger: #c74e39;
  --danger-hover: #a64030;
  --shadow: rgba(0, 0, 0, 0.36);
  --shadow-strong: rgba(0, 0, 0, 0.64);
}

html, body {
  margin: 0;
  padding: 0;
  font-family: 'GenEiAntiquePv6', 'GenEiAntiqueNv6', system-ui, sans-serif;
  background: var(--app-bg);
  color: var(--text);
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

header {
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex: 0 0 auto;
}

header h1 {
  margin: 0;
  font-size: 18px;
}

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

.page-nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-nav {
  padding: 6px 10px;
  min-width: 36px;
}

.page-indicator {
  font-variant-numeric: tabular-nums;
  min-width: 44px;
  text-align: center;
  color: #333;
  font-size: 14px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
  font-family: inherit;
}

.btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn:disabled {
  background: var(--button-disabled);
  cursor: not-allowed;
  opacity: 0.7;
}

.btn-danger {
  background: var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: var(--danger-hover);
}

.btn.active {
  background: var(--accent-active);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
}

.btn-secondary {
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled),
.btn-secondary.active {
  background: var(--accent-active);
  color: #fff;
}

#app {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--inspector-resizer-width) var(--inspector-width);
  min-height: 0;
}

.stage-wrapper {
  position: relative;
  overflow: auto;
  background: var(--stage-shell);
  display: flex;
  align-items: safe center;
  justify-content: safe center;
  padding: 24px;
}

.stage-wrapper.dragover {
  outline: 2px dashed #fff;
  outline-offset: -12px;
}

.stage {
  position: relative;
  display: inline-block;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  line-height: 0;
  /* レイアウト寸法は main.js が cur.canvasWidth/Height をインライン style で px 指定する */
}

.context-menu {
  position: fixed;
  z-index: 1000;
  background: var(--surface);
  border: 1px solid var(--input-border);
  border-radius: 4px;
  box-shadow: 0 4px 12px var(--shadow);
  padding: 4px 0;
  min-width: 160px;
}

.context-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 8px 16px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  color: #222;
  user-select: none;
}

.context-menu-item:hover {
  background: var(--menu-hover);
}

.panel-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.panel {
  position: absolute;
  border: var(--panel-border-width, 4px) solid #000;
  box-sizing: border-box;
  background: rgba(255, 255, 255, 0.02);
  overflow: hidden;
  /* .panel-focus の mix-blend-mode: multiply を、コマ素材に対してだけ効かせるために
     コマ単位で独立した合成コンテキストを作る */
  isolation: isolate;
  cursor: pointer;
  pointer-events: auto;
}

.panel:hover {
  background: rgba(42, 109, 244, 0.08);
}

.panel.selected {
  border-color: #2a6df4;
  background: rgba(42, 109, 244, 0.12);
}

.panel.has-material {
  background: #fff;
}

.panel:not(.has-material) {
  display: flex;
  align-items: center;
  justify-content: center;
}

.panel:not(.has-material)::before {
  content: 'ドロップ / ダブルクリックで画像';
  color: #999;
  font-size: 12px;
  pointer-events: none;
  user-select: none;
}

.panel-material {
  position: absolute;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  transform-origin: center;
  max-width: none;
}

/* 集中線。コマ素材 (.panel-material) の上、レイヤー (吹き出し/テキスト) より下に重なる。
   .panel の overflow: hidden により枠外は自動でトリミングされる。
   素材の黒地は乗算合成で透過させ、線だけがコマ素材の上に乗るようにする。 */
.panel-focus {
  position: absolute;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
  transform-origin: center;
  max-width: none;
  mix-blend-mode: multiply;
}


.layer-container {
  position: absolute;
  inset: 0;
  line-height: normal;
  /* stage を覆うが、空白部分はクリックを panel-container 側に通す。
     テキストレイヤー本体は .text-layer 側で pointer-events: auto で受ける */
  pointer-events: none;
}

.text-layer {
  pointer-events: auto;
}

.text-preview-canvas {
  position: absolute;
  inset: 0;
  display: block;
  pointer-events: none;
}

.text-layer {
  position: absolute;
  min-width: 1em;
  min-height: 1em;
  color: transparent;
  cursor: move;
  white-space: pre-wrap;
  word-break: break-word;
  outline: 1px dashed transparent;
  outline-offset: -1px;
  background: transparent;
  user-select: none;
  font-family: 'GenEiAntiquePv6', sans-serif;
}

.text-layer:hover {
  outline-color: rgba(42, 109, 244, 0.5);
}

.text-layer.selected {
  outline-color: #2a6df4;
}

.text-layer.editing {
  color: #000;
  cursor: text;
  user-select: text;
  background: rgba(255, 255, 255, 0.6);
}

.text-layer.vertical {
  writing-mode: vertical-rl;
  text-orientation: upright;
}

/* 非編集時は Canvas が白枠を描くので DOM 側は透明のままにし、
   編集中だけ DOM に白背景と枠線を出して contentEditable のテキストを見せる */
.text-layer.monologue.editing {
  background: #fff;
  box-shadow: inset 0 0 0 2px #000;
}

/* 吹き出し画像(ステッカー)。テキストレイヤーとは独立して配置・拡縮できる。 */
.sticker-layer {
  position: absolute;
  pointer-events: auto;
  cursor: move;
  user-select: none;
  -webkit-user-drag: none;
  outline: 1px dashed transparent;
  outline-offset: -1px;
}

.sticker-layer:hover {
  outline-color: rgba(42, 109, 244, 0.5);
}

.sticker-layer.selected {
  outline-color: #2a6df4;
}

/* 吹き出しの四隅リサイズハンドル */
.sticker-handle {
  position: absolute;
  width: 10px;
  height: 10px;
  background: #fff;
  border: 1px solid #2a6df4;
  box-sizing: border-box;
  pointer-events: auto;
  z-index: 10;
}
.sticker-handle-nw { cursor: nwse-resize; }
.sticker-handle-ne { cursor: nesw-resize; }
.sticker-handle-sw { cursor: nesw-resize; }
.sticker-handle-se { cursor: nwse-resize; }

/* 吹き出しピッカー: サムネイル一覧から形状を選択 */
.bubble-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  max-height: 320px;
  overflow-y: auto;
  padding: 6px;
  border: 1px solid #eee;
  border-radius: 4px;
  background: #fafafa;
}

.bubble-thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 4px;
  cursor: pointer;
}

.bubble-thumb:hover {
  border-color: #2a6df4;
}

.bubble-thumb.selected {
  border-color: #2a6df4;
  background: #eaf1ff;
}

.bubble-thumb img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  pointer-events: none;
}

/* 集中線ピッカー: ピッカー枠は bubble-picker と共有しつつ、サムネは縦に長い 16:9 寄りで見せる */
.focus-picker {
  grid-template-columns: repeat(2, 1fr);
}

.focus-picker .bubble-thumb {
  aspect-ratio: 3 / 2;
  background: #333; /* 集中線は暗い背景なので薄い灰で囲う */
}

/* 「なし」サムネ。画像の代わりにテキストを表示する */
.bubble-thumb.thumb-empty {
  background: #fff;
  color: #666;
  font-size: 12px;
}

/* ピッカー上部のタブ。今は集中線 1 タブだが、将来の拡張に備えてレイアウトだけ用意 */
.picker-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.picker-tab {
  border: 1px solid #ddd;
  background: #f4f4f4;
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 4px 4px 0 0;
  cursor: pointer;
}

.picker-tab.selected {
  background: #fff;
  border-color: #2a6df4;
  color: #2a6df4;
  font-weight: 600;
}

.inspector {
  background: #fff;
  padding: 16px;
  overflow-y: auto;
  min-width: 0;
}

.inspector-resize-handle {
  position: relative;
  background: var(--surface);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  cursor: ew-resize;
  touch-action: none;
}

.inspector-resize-handle::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 40px;
  border-left: 1px solid var(--input-border);
  border-right: 1px solid var(--input-border);
  transform: translate(-50%, -50%);
  opacity: 0.8;
}

.inspector-resize-handle:hover,
.inspector-resize-handle.dragging {
  background: var(--accent-soft);
}

.inspector h2 {
  font-size: 14px;
  margin: 0 0 10px;
  color: #555;
  border-bottom: 1px solid #eee;
  padding-bottom: 6px;
}

.inspector-section {
  margin-bottom: 24px;
}

.hint {
  font-size: 12px;
  color: #888;
  margin: 8px 0 0;
}

.field {
  display: block;
  margin-bottom: 12px;
}

.field > span {
  display: block;
  font-size: 12px;
  color: #555;
  margin-bottom: 4px;
}

.field > span small {
  color: #2a6df4;
  font-weight: bold;
  margin: 0 2px;
}

.field textarea,
.field select,
.field input[type="text"],
.field input[type="number"] {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 14px;
  font-family: inherit;
}

.field textarea {
  resize: vertical;
}

.field input[type="range"] {
  width: 100%;
}

.field-row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.field-row .btn {
  flex: 1;
}

.field-row .field {
  flex: 1;
  margin-bottom: 0;
}

.memo-panel {
  position: fixed;
  top: 80px;
  left: 40px;
  width: 240px;
  height: 220px;
  min-width: 160px;
  min-height: 120px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
  display: flex;
  flex-direction: column;
  resize: both;
  overflow: hidden;
  z-index: 100;
}

.memo-panel[hidden] {
  display: none;
}

.memo-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  background: #f5f5f5;
  border-bottom: 1px solid #eee;
  padding: 4px 6px 4px 10px;
  cursor: move;
  user-select: none;
  font-size: 12px;
  font-weight: bold;
  color: #555;
  flex: 0 0 auto;
}

.memo-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.memo-close {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  padding: 0 4px;
  color: #555;
  font-family: inherit;
}

.memo-close:hover {
  color: #222;
}

.memo-body {
  flex: 1 1 auto;
  border: none;
  background: transparent;
  resize: none;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.5;
  outline: none;
  color: var(--text);
}

.help-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 760px;
  max-width: calc(100vw - 40px);
  max-height: calc(100vh - 80px);
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 6px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.28);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translate(-50%, -50%);
  z-index: 1000;
}

.help-panel[hidden] {
  display: none;
}

.help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  background: #f8f8f8;
  border-bottom: 1px solid #e5e5e5;
  padding: 14px 16px 14px 20px;
  user-select: none;
  font-size: 16px;
  font-weight: bold;
  color: var(--text);
  flex: 0 0 auto;
}

.help-body {
  padding: 20px 24px 24px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.55;
  color: #333;
}

.help-section {
  border-top: 1px solid #eee;
  padding-top: 16px;
  margin-top: 16px;
}

.help-section:first-child {
  border-top: none;
  padding-top: 0;
  margin-top: 0;
}

.help-section h3 {
  margin: 0 0 10px;
  font-size: 13px;
  color: #2a6df4;
  letter-spacing: 0;
}

.help-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.help-list li {
  display: grid;
  grid-template-columns: minmax(160px, 0.7fr) 1fr;
  gap: 16px;
  align-items: baseline;
}

.help-list span {
  color: #666;
}

.help-list strong {
  color: #222;
  font-weight: 600;
}

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

.shortcut-grid div {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.shortcut-grid span {
  margin-left: 4px;
  color: #333;
  overflow-wrap: anywhere;
}

.help-body kbd {
  min-width: 28px;
  padding: 3px 7px;
  border: 1px solid #cfcfcf;
  border-bottom-color: #aaa;
  border-radius: 4px;
  background: #fafafa;
  color: #222;
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.08);
  font-family: inherit;
  font-size: 12px;
  line-height: 1.2;
  text-align: center;
}

body.theme-dark header,
body.theme-dark .inspector,
body.theme-dark .memo-panel,
body.theme-dark .help-panel,
body.theme-dark .context-menu {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

body.theme-dark .memo-header,
body.theme-dark .help-header,
body.theme-dark .picker-tab {
  background: var(--surface-alt);
  border-color: var(--border-soft);
  color: var(--text-strong);
}

body.theme-dark .page-indicator,
body.theme-dark .context-menu-item,
body.theme-dark .memo-body,
body.theme-dark .help-body,
body.theme-dark .shortcut-grid span {
  color: var(--text);
}

body.theme-dark .inspector h2,
body.theme-dark .field > span,
body.theme-dark .memo-header,
body.theme-dark .memo-close,
body.theme-dark .help-section h3,
body.theme-dark .field > span small {
  color: var(--text-muted);
}

body.theme-dark .hint,
body.theme-dark .help-list span,
body.theme-dark .bubble-thumb.thumb-empty,
body.theme-dark .panel:not(.has-material)::before {
  color: var(--text-subtle);
}

body.theme-dark .help-list strong,
body.theme-dark .memo-close:hover {
  color: var(--text-strong);
}

body.theme-dark .inspector h2,
body.theme-dark .help-section,
body.theme-dark .memo-header,
body.theme-dark .help-header {
  border-color: var(--border-soft);
}

body.theme-dark .field textarea,
body.theme-dark .field select,
body.theme-dark .field input[type="text"],
body.theme-dark .field input[type="number"],
body.theme-dark .help-body kbd {
  background: var(--surface-muted);
  border-color: var(--input-border);
  color: var(--text);
}

body.theme-dark .bubble-picker,
body.theme-dark .bubble-thumb {
  background: var(--surface-muted);
  border-color: var(--border);
}

body.theme-dark .bubble-thumb.selected,
body.theme-dark .picker-tab.selected {
  background: var(--accent-soft);
  border-color: var(--accent);
}

body.theme-dark .panel:hover {
  background: rgba(0, 122, 204, 0.08);
}

body.theme-dark .panel.selected {
  background: rgba(0, 122, 204, 0.16);
  border-color: var(--accent);
}

body.theme-dark .bubble-thumb:hover,
body.theme-dark .text-layer:hover,
body.theme-dark .text-layer.selected,
body.theme-dark .sticker-layer:hover,
body.theme-dark .sticker-layer.selected {
  border-color: var(--accent);
  outline-color: var(--accent);
}

body.theme-dark .context-menu-item:hover {
  background: var(--menu-hover);
}

body.theme-dark .memo-panel {
  box-shadow: 0 6px 20px var(--shadow);
}

body.theme-dark .help-panel {
  box-shadow: 0 16px 48px var(--shadow-strong);
}

@media (max-width: 760px) {
  .help-panel {
    width: calc(100vw - 32px);
    max-height: calc(100vh - 48px);
  }

  .help-body {
    padding: 16px;
  }

  .help-list li,
  .shortcut-grid {
    grid-template-columns: 1fr;
  }
}
