:root {
  --bg: #0f1115;
  --panel: #181b22;
  --border: #2a2f3a;
  --text: #e6e8ee;
  --muted: #8a93a6;
  --accent: #4f8cff;
  --accent-fg: #fff;
  --danger: #ff6b6b;
  --success: #6ee7b7;
  --warn: #fbbf24;
  --gold: #ffd700;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Hiragino Sans", "Yu Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 14px;
}

/* ヘッダー */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
}

.app-header h1 { font-size: 16px; margin: 0; }
#app-title[contenteditable] {
  border-radius: 4px;
  padding: 2px 6px;
  outline: none;
  transition: background 0.15s;
  cursor: text;
}
#app-title[contenteditable]:hover { background: rgba(255,255,255,0.06); }
#app-title[contenteditable]:focus { background: rgba(255,255,255,0.1); box-shadow: 0 0 0 1px var(--border); }

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.env-status {
  font-size: 11px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
}
.env-status.ok { color: var(--success); }
.env-status.warn { color: var(--warn); }
.env-status.err { color: var(--danger); }

.ai-badge {
  font-size: 11px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(79,140,255,0.15);
  color: var(--accent);
  border: 1px solid rgba(79,140,255,0.3);
}

/* メインレイアウト */
.app-main {
  display: grid;
  grid-template-columns: minmax(320px, 460px) 1fr;
  gap: 12px;
  padding: 12px;
  align-items: start;
}

@media (max-width: 800px) {
  .app-main {
    grid-template-columns: 1fr;
    padding: 8px;
  }
}

.col-left, .col-right {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* パネル */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}

.panel h2 {
  font-size: 11px;
  margin: 0 0 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-weight: 600;
}

.panel-hint {
  font-size: 10px;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 400;
  color: var(--muted);
}

/* フィールド */
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}

.field:last-child { margin-bottom: 0; }

.field-label {
  font-size: 12px;
  color: var(--muted);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
}

.hint {
  font-size: 10px;
  color: #5a6478;
}

.field input, .field textarea, .field select {
  background: #0c0e13;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  min-height: 40px;
}

.field textarea { resize: vertical; }

.field input[type="color"] {
  padding: 2px 4px;
  height: 36px;
  cursor: pointer;
}

.field input[type="file"] {
  min-height: unset;
  padding: 6px 8px;
  cursor: pointer;
}

.field-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
}

.char-line {
  font-size: 11px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* フィールド行 */
.row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 8px;
}

.field.small input, .field.small select { padding: 6px 8px; }

/* アクション */
.actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

/* ボタン */
button {
  background: var(--accent);
  color: var(--accent-fg);
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  font: inherit;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  min-height: 40px;
  transition: opacity 0.15s;
}

button:hover:not(:disabled) { opacity: 0.85; }

button:disabled {
  background: #2a2f3a;
  color: var(--muted);
  cursor: not-allowed;
}

button.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

button.secondary:disabled {
  border-color: var(--border);
}

button.small {
  padding: 5px 12px;
  font-size: 12px;
  min-height: 32px;
}

button.micro {
  padding: 3px 8px;
  font-size: 11px;
  min-height: 26px;
  border-radius: 6px;
}

/* プリセット */
.preset-section { margin-bottom: 10px; }

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

.preset-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.tab {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 4px 12px;
  font-size: 12px;
  border-radius: 6px;
  min-height: 30px;
  cursor: pointer;
}

.tab.active {
  background: rgba(79,140,255,0.15);
  border-color: var(--accent);
  color: var(--accent);
}

.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  background: #1e2330;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
  min-height: 30px;
  transition: all 0.15s;
}

.chip:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.chip-active {
  background: rgba(79,140,255,0.2);
  border-color: var(--accent);
  color: var(--accent);
}

/* 排除パターン */
.exclusion-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 5px;
  margin-bottom: 8px;
}

.excl-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 9px;
  background: #1a1d26;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.excl-chip input[type="checkbox"] {
  width: 14px;
  height: 14px;
  cursor: pointer;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.excl-chip:hover { border-color: #4a5168; }

.excl-active {
  background: rgba(255,107,107,0.12);
  border-color: rgba(255,107,107,0.45);
  color: #ffaaaa;
}

.neg-preview {
  margin-top: 6px;
  padding: 6px 10px;
  background: rgba(0,0,0,0.3);
  border-radius: 6px;
  border: 1px solid var(--border);
}

/* 設定グリッド */
.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 480px) {
  .settings-grid { grid-template-columns: 1fr; }
}

.setting-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.setting-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--muted);
}

.setting-val {
  font-size: 14px;
  color: var(--text);
  font-weight: 600;
  min-width: 30px;
  text-align: right;
}

.range-input {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
  height: 20px;
}

.input-sm {
  background: #0c0e13;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font: inherit;
  width: 100%;
}

.setting-desc {
  font-size: 11px;
  color: #5a6478;
  line-height: 1.4;
}

/* 折りたたみ */
.collapsible-wrap { margin-bottom: 8px; }

.toggle-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 100%;
  text-align: left;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border-radius: 8px;
  font-size: 12px;
  min-height: 36px;
  cursor: pointer;
}

.toggle-btn:hover { border-color: #4a5168; color: var(--text); }

.toggle-arrow { font-size: 10px; }

.collapsible-body {
  padding-top: 10px;
}

/* バッジ */
.badge {
  font-size: 10px;
  padding: 2px 7px;
  background: rgba(79,140,255,0.2);
  color: var(--accent);
  border-radius: 10px;
  margin-left: 4px;
}

/* 情報ボックス */
.info-box {
  background: rgba(79,140,255,0.07);
  border: 1px solid rgba(79,140,255,0.2);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  color: #a0aec0;
  line-height: 1.6;
  margin-bottom: 10px;
}

.info-box strong { color: var(--text); }

/* チェックボックス行 */
.check-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  cursor: pointer;
}

.check-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin-top: 2px;
  flex-shrink: 0;
  cursor: pointer;
  accent-color: var(--accent);
}

/* モデル */
#model-status, #gen-status { font-size: 13px; margin-bottom: 6px; }

progress {
  width: 100%;
  height: 6px;
  margin-bottom: 8px;
}

/* キャンバス */
.canvas-wrap {
  width: 100%;
  max-width: 540px;
  margin: 0 auto 8px;
  overflow: hidden;
  border-radius: 8px;
  background: #0c0e13;
  border: 1px dashed var(--border);
  position: relative;
}

/* クロップオーバーレイ */
#crop-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.crop-mask {
  position: absolute;
  left: 0; right: 0;
  background: rgba(0,0,0,0.62);
}
#crop-band {
  position: absolute;
  left: 0; right: 0;
  box-sizing: border-box;
  border-top: 2px solid rgba(255,255,255,0.85);
  border-bottom: 2px solid rgba(255,255,255,0.85);
  cursor: ns-resize;
  pointer-events: all;
  display: flex;
  align-items: center;
  justify-content: center;
}
.crop-band-hint {
  font-size: 11px;
  color: rgba(255,255,255,0.7);
  background: rgba(0,0,0,0.35);
  padding: 2px 8px;
  border-radius: 4px;
  user-select: none;
  pointer-events: none;
}

/* クロップコントロール */
.crop-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 auto 8px;
  max-width: 540px;
}
.crop-enable-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
#crop-ratio-row {
  display: flex;
  gap: 6px;
}
.crop-ratio-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1a1d26;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.crop-ratio-btn input[type="radio"] { display: none; }
.crop-ratio-btn span { color: var(--muted); }
.crop-ratio-btn:has(input:checked) { background: rgba(79,140,255,0.2); border-color: var(--accent); }
.crop-ratio-btn:has(input:checked) span { color: var(--accent); }
.crop-lock-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  color: var(--muted);
  margin-left: auto;
}

#result {
  width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
  /* aspect-ratioは生成後にcanvas属性で決まるため固定しない */
}

/* 未生成時のプレースホルダー比率 (1:1 正方形) */
#result.empty {
  aspect-ratio: 1 / 1;
}

/* テキスト配置: 画像ロード後はドラッグ可能を示すカーソル */
#result:not(.empty) { cursor: crosshair; }
#result.dragging { cursor: grabbing; }

/* 参照画像プレビュー */
#ref-preview {
  max-width: 100%;
  max-height: 140px;
  margin-top: 6px;
  border-radius: 8px;
  display: none;
}
#ref-preview[src] { display: block; }

/* テキストオーバーレイ 9スタイル */
.style-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

@media (max-width: 480px) {
  .style-grid { grid-template-columns: repeat(3, 1fr); }
}

.style-card {
  background: #12151d;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-height: 80px;
  transition: border-color 0.15s;
}

.style-card:hover { border-color: #4a5168; }

.style-active {
  border-color: var(--accent) !important;
  background: rgba(79,140,255,0.08);
}

.style-preview {
  width: 100%;
  height: 44px;
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: linear-gradient(135deg, #1a2a4a 0%, #2d1b4a 100%);
  position: relative;
}

.sp-text {
  font-size: 15px;
  font-weight: bold;
  line-height: 1;
}

/* 各スタイルのプレビュー */
.sp-simple .sp-text {
  color: #fff;
  text-shadow: 2px 2px 6px rgba(0,0,0,0.95);
  padding-bottom: 4px;
}

.sp-darkbar {
  background: linear-gradient(135deg, #1a2a4a 0%, #2d1b4a 100%);
}
.sp-darkbar::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 55%;
  background: rgba(0,0,0,0.62);
}
.sp-darkbar .sp-text {
  color: #fff;
  position: relative;
  z-index: 1;
  padding-bottom: 4px;
}

.sp-gradient {
  background: linear-gradient(135deg, #1a2a4a 0%, #2d1b4a 100%);
}
.sp-gradient::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, transparent 30%, rgba(0,0,0,0.88));
}
.sp-gradient .sp-text {
  color: #fff;
  position: relative;
  z-index: 1;
  padding-bottom: 4px;
}

.sp-neon .sp-text {
  color: #fff;
  text-shadow: 0 0 8px #00eeff, 0 0 16px #00eeff;
  -webkit-text-stroke: 0.5px #00eeff;
  padding-bottom: 4px;
}

.sp-outline .sp-text {
  color: transparent;
  -webkit-text-stroke: 1.5px #fff;
  padding-bottom: 4px;
}

.sp-gold::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 55%;
  background: rgba(0,0,0,0.55);
}
.sp-gold .sp-text {
  background: linear-gradient(to bottom, #fff8c0, #ffd700, #b8860b);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  position: relative;
  z-index: 1;
  padding-bottom: 4px;
}

.sp-frost::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 55%;
  background: rgba(255,255,255,0.18);
  border-top: 1px solid rgba(255,255,255,0.3);
}
.sp-frost .sp-text {
  color: #fff;
  position: relative;
  z-index: 1;
  padding-bottom: 4px;
}

.sp-cinema {
  background: linear-gradient(135deg, #1a2a4a 0%, #2d1b4a 100%);
}
.sp-cinema::before {
  content: "";
  position: absolute;
  inset: 0 0 auto;
  height: 20%;
  background: rgba(0,0,0,0.92);
}
.sp-cinema::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  height: 30%;
  background: rgba(0,0,0,0.92);
}
.sp-cinema .sp-text {
  color: #fff;
  position: relative;
  z-index: 1;
  padding-bottom: 3px;
  font-size: 11px;
}

.sp-stamp {
  align-items: center;
}
.sp-stamp::after {
  content: "";
  position: absolute;
  inset: auto 12px 8px;
  height: 55%;
  background: rgba(0,0,0,0.72);
  border: 1.5px solid rgba(255,255,255,0.6);
  border-radius: 4px;
}
.sp-stamp .sp-text {
  color: #fff;
  position: relative;
  z-index: 1;
  padding-bottom: 6px;
}

.style-card-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
}

.style-card-desc {
  font-size: 10px;
  color: var(--muted);
}

/* テキスト水平アライメントボタン */
.halign-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}

.halign-btn {
  flex: 1;
  background: #1a1d26;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 12px;
  padding: 6px 0;
  cursor: pointer;
  transition: all 0.15s;
}

.halign-btn:hover { border-color: #4a5168; color: var(--text); }

.halign-active {
  background: rgba(79,140,255,0.2);
  border-color: var(--accent);
  color: var(--accent);
}

/* スタイル select + ミニプレビュー */
.style-select-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}
.style-select-row select {
  flex: 1;
  min-width: 0;
}
#style-preview-mini {
  flex-shrink: 0;
  border-radius: 4px;
  border: 1px solid var(--border);
  display: block;
}

/* 出力アスペクト比 */
.ratio-row {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}
.ratio-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1a1d26;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  padding: 6px 4px;
  transition: all 0.15s;
}
.ratio-btn input[type="radio"] { display: none; }
.ratio-btn span {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}
.ratio-btn:has(input:checked) {
  background: rgba(79,140,255,0.2);
  border-color: var(--accent);
}
.ratio-btn:has(input:checked) span { color: var(--accent); }
.ratio-btn:hover:not(:has(input:checked)) { border-color: #4a5168; }

/* 呪文プリセット select */
.spell-preset-select {
  flex: 1;
  min-width: 120px;
  background: #1a1d26;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 11px;
  padding: 4px 6px;
  cursor: pointer;
}

/* 呪文化ボタン ON 状態 */
.spell-active {
  background: rgba(255, 180, 60, 0.15) !important;
  border-color: #ffb43c !important;
  color: #ffb43c !important;
}

/* 履歴 */
.history {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 8px;
}

.history-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  cursor: pointer;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #12151d;
  transition: border-color 0.15s;
}

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

.history-item img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
}

.history-info {
  font-size: 10px;
  color: var(--muted);
  padding: 2px 6px;
  line-height: 1.3;
}

.history-ts {
  font-size: 9px;
  color: #4a5168;
  padding: 0 6px 2px;
}

.del-btn {
  margin: 0 6px 6px;
  border-color: rgba(255,107,107,0.3);
  color: var(--danger);
  font-size: 10px;
  min-height: 24px;
  padding: 2px 6px;
}

.del-btn:hover { border-color: var(--danger); }

.regen-btn {
  margin: 0 6px 4px;
  font-size: 10px;
  min-height: 24px;
  padding: 2px 6px;
  color: var(--accent);
  border-color: rgba(79,140,255,0.3);
}
.regen-btn:hover { border-color: var(--accent); }

.reuse-btn {
  margin: 0 6px 4px;
  font-size: 10px;
  min-height: 24px;
  padding: 2px 6px;
  color: #a0c8a0;
  border-color: rgba(100,180,100,0.3);
}
.reuse-btn:hover { border-color: #a0c8a0; }

/* ステータステキスト */
.status-text {
  font-size: 13px;
  margin-bottom: 6px;
}

.muted { color: var(--muted); }
.small-text { font-size: 12px; }

/* 使い方ガイド */
.guide-panel { border-left: 3px solid var(--accent); }
.guide-toggle { font-weight: 600; color: var(--accent); }
.guide-steps {
  padding-left: 18px;
  margin: 8px 0 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.guide-steps li { line-height: 1.6; }
.guide-steps p { margin: 4px 0 0; color: var(--muted); font-size: 12px; }
.guide-faq { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 8px; }
.guide-faq-title { font-weight: 600; margin-bottom: 8px; font-size: 12px; color: var(--muted); }
.guide-faq dl { display: grid; gap: 6px; }
.guide-faq dt { font-weight: 600; font-size: 12px; }
.guide-faq dd { margin-left: 0; font-size: 12px; color: var(--muted); line-height: 1.5; }

/* フッター */
.app-footer {
  text-align: center;
  padding: 12px 16px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

/* スマホ横画面 (landscape) */
@media (max-height: 520px) and (orientation: landscape) {
  .app-header {
    padding: 4px 12px;
  }
  .app-header h1 { font-size: 13px; }
  .env-status, .ai-badge { font-size: 10px; padding: 2px 8px; }

  .app-main {
    grid-template-columns: 320px 1fr;
    padding: 6px;
    gap: 6px;
  }

  .panel {
    padding: 8px 10px;
  }

  .panel h2 { margin-bottom: 6px; }

  /* 結果キャンバスを画面高に合わせて制限 */
  .canvas-wrap {
    max-height: calc(100vh - 80px);
  }

  #result {
    max-height: calc(100vh - 80px);
    width: auto;
    max-width: 100%;
    margin: 0 auto;
  }

  .col-left, .col-right { gap: 6px; }

  button { min-height: 36px; padding: 6px 12px; }
  button.small { min-height: 30px; }

  .actions { gap: 6px; }

  .settings-grid { gap: 8px; }

  /* 履歴・テキストオーバーレイは折りたたんで省スペース */
  #history-panel { display: none; }
}

/* スマホ縦向き追加調整 */
@media (max-width: 480px) {
  body { font-size: 15px; }

  .app-header { padding: 8px 12px; }
  .app-header h1 { font-size: 15px; }

  button { min-height: 44px; padding: 10px 14px; }
  button.small { min-height: 38px; }
  button.micro { min-height: 30px; }

  .tab { min-height: 36px; padding: 6px 12px; }
  .chip { min-height: 34px; }

  .exclusion-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .history {
    grid-template-columns: repeat(2, 1fr);
  }

  .panel { padding: 12px; }

  .actions { flex-direction: column; }
  .actions button { width: 100%; }
}
