/* メールテンプレート作成ツール 固有スタイル */

:root {
    /* ツール固有の色。ダークモード側は最下部で明示的に上書きする */
    --mt-danger: #b91c1c;
    --mt-danger-border: #fca5a5;
    --mt-danger-bg: #fef2f2;
    --mt-warn: #92400e;
    --mt-warn-bg: #fffbeb;
    --mt-warn-border: #fcd34d;
    /* フォーカスリング・入力欄の枠は WCAG 1.4.11（非テキスト 3:1）を満たす濃さにする */
    --mt-focus: #1d4ed8;
    --mt-muted-surface: #f1f3f5;
    --mt-field-border: #868e96;
    --mt-token-bg: #dbeafe;
    --mt-token-border: #93c5fd;
    --mt-token-active-bg: #bfdbfe;
    /* プルダウンの矢印。ダークモード側で線色を差し替える */
    --mt-select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' stroke='%236c757d' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

/* display: flex / grid を指定した要素でも hidden 属性を確実に効かせる */
[hidden] {
    display: none !important;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ========== 保存不可の警告 ========== */
.storage-warning {
    background: var(--mt-warn-bg);
    border: 1px solid var(--mt-warn-border);
    border-radius: var(--border-radius);
    color: var(--mt-warn);
    padding: 0.75rem 1rem;
    margin: 0 0 1.5rem;
    font-size: 0.9rem;
    font-weight: 600;
}

/* ========== タブ ========== */
.tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    border-bottom: 1px solid var(--secondary-color);
    margin-bottom: 1.5rem;
}

.tab-btn {
    background: transparent;
    border: none;
    border-bottom: 3px solid transparent;
    padding: 0.7rem 1rem;
    min-height: 44px;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--gray-text-color);
    cursor: pointer;
    transition: color 0.15s ease, border-color 0.15s ease;
}

.tab-btn:hover { color: var(--primary-color); }

.tab-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.tab-btn:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}

.tab-panel[hidden] { display: none; }
.tab-panel:focus { outline: none; }

.subtabs {
    margin: 1rem 0 0.75rem;
}

/* ========== フォーム共通 ========== */
.form-step {
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    padding: 1rem 1.25rem 1.25rem;
    margin: 0 0 1.25rem;
    background: var(--background-color);
}

.form-step legend {
    font-weight: 700;
    color: var(--primary-color);
    padding: 0 0.5rem;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.step-num {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.6rem;
    height: 1.6rem;
    border-radius: 50%;
    background: var(--primary-color);
    color: var(--text-color-light);
    font-size: 0.85rem;
    font-weight: 700;
}

.field-block {
    margin-bottom: 1rem;
}

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

.field-block > label,
.pseudo-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-color);
    margin-bottom: 0.25rem;
}

.req {
    color: var(--mt-danger);
    font-weight: 700;
}

.field-row {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
}

.field-row > .field-block {
    flex: 1 1 220px;
    min-width: 0;
}

/* 入力欄の共通見た目。新しい type を使うときはこの一覧と
   下の :hover / :focus-visible にも必ず追加すること */
input[type="text"],
input[type="email"],
input[type="number"],
select,
textarea {
    width: 100%;
    box-sizing: border-box;
    padding: 0.55rem 0.75rem;
    min-height: 44px;
    font-size: 1rem;
    font-family: inherit;
    border: 1px solid var(--mt-field-border);
    border-radius: var(--border-radius);
    background: var(--card-color);
    color: var(--text-color);
    transition: border-color 0.15s ease;
}

/* プルダウンはネイティブ描画をやめ、独自の矢印を右端に固定して
   「テキスト入力ではなく選択肢である」ことを一目で分かるようにする */
select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    padding-right: 2.5rem;
    background-image: var(--mt-select-arrow);
    background-repeat: no-repeat;
    background-position: right 0.9rem center;
    background-size: 12px 8px;
    line-height: 1.4;
    cursor: pointer;
    /* Firefox で選択中テキストが矢印に重ならないようにする */
    text-overflow: ellipsis;
}

select::-ms-expand {
    display: none;
}

/* ネイティブのオプションリストはダークモードを自動で追随しないので明示する */
select option {
    background-color: var(--card-color);
    color: var(--text-color);
}

input[type="text"]:hover,
input[type="email"]:hover,
input[type="number"]:hover,
select:hover,
textarea:hover {
    border-color: var(--primary-color);
}

textarea {
    resize: vertical;
    line-height: 1.6;
}

#edit-body,
#edit-body-html,
#style-custom-css {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9rem;
}

/* ========== 変数のブロック表示 ==========
   入力欄の背後に同じ文字送りの層を重ね、{{変数}} をブロックとして見せる。
   contenteditable を使わないので日本語IME・アンドゥ・貼り付けは native のまま動く。
   .field-highlight と入力欄のフォント・行間・パディング・枠幅は必ず一致させること。 */
.field-editor {
    position: relative;
}

.field-highlight,
.field-editor > input[type="text"],
.field-editor > textarea {
    box-sizing: border-box;
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--mt-field-border);
    border-radius: var(--border-radius);
    font-family: inherit;
    font-size: 1rem;
    line-height: 1.6;
    letter-spacing: normal;
    /* 単一行は折り返さない。横スクロールは JS で同期する */
    white-space: pre;
    tab-size: 4;
}

/* 本文だけ等幅・折り返しあり */
.field-editor.is-multiline .field-highlight,
.field-editor.is-multiline > textarea {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9rem;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    word-break: break-word;
}

.field-highlight {
    position: absolute;
    inset: 0;
    margin: 0;
    overflow: hidden;
    border-color: transparent;
    background: var(--card-color);
    color: transparent;
    pointer-events: none;
}

.field-editor > input[type="text"],
.field-editor > textarea {
    position: relative;
    background: transparent;
}

.field-editor > textarea {
    resize: vertical;
}

/* 変数ブロック。背景は下の層、文字は入力欄側が描くので色が二重にならない */
.field-highlight .var-token {
    border-radius: 4px;
    background: var(--mt-token-bg);
    box-shadow: 0 0 0 1px var(--mt-token-border);
}

.field-highlight .var-token.is-undeclared {
    background: var(--mt-warn-bg);
    box-shadow: 0 0 0 1px var(--mt-warn-border);
}

.field-highlight .var-token.is-active {
    background: var(--mt-token-active-bg);
    box-shadow: 0 0 0 2px var(--primary-color);
}

/* ========== 変数のフロートメニュー ==========
   どの入力欄からも同じメニューを使うため、位置の基準はフォームに置く。
   z-index スケール: 入力欄の層 0 / フロート 20 / ダイアログ backdrop（native）/ トースト 1000 */
#editor-form {
    position: relative;
}

.var-float {
    position: absolute;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem;
    border: 1px solid var(--mt-field-border);
    border-radius: var(--border-radius);
    background: var(--card-color);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
    white-space: nowrap;
}

.var-float-label {
    padding: 0 0.5rem;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--primary-color);
}

.var-float-btn {
    min-height: 44px;
    padding: 0 0.85rem;
    border: none;
    border-radius: calc(var(--border-radius) - 2px);
    background: transparent;
    color: var(--text-color);
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.var-float-btn:hover {
    background: var(--mt-muted-surface);
}

.var-float-btn:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: -2px;
}

.var-float-btn.is-danger {
    color: var(--mt-danger);
}

/* 座標計測専用の隠し要素。スタイルは実行時に textarea から写す */
.measure-mirror {
    position: absolute;
    top: 0;
    left: -9999px;
    visibility: hidden;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    word-break: break-word;
    pointer-events: none;
}

input[type="color"] {
    width: 100%;
    max-width: 8rem;
    min-height: 44px;
    padding: 0.2rem;
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    background: var(--card-color);
    cursor: pointer;
}

input[type="checkbox"] {
    width: 20px;
    height: 20px;
    margin: 0;
    accent-color: var(--primary-color);
    cursor: pointer;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 1px;
}

input.is-readonly {
    background: var(--mt-muted-surface);
    color: var(--gray-text-color);
    font-weight: 600;
}

.field-hint {
    margin: 0.35rem 0 0;
    font-size: 0.8rem;
    color: var(--gray-text-color);
    line-height: 1.6;
}

.field-hint code,
.seo-content code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    background: var(--background-color);
    border: 1px solid var(--secondary-color);
    border-radius: 4px;
    padding: 0.05rem 0.3rem;
    font-size: 0.85em;
}

.field-error {
    color: var(--mt-danger);
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0.35rem 0 0;
}

.field-warn {
    color: var(--mt-warn);
    background: var(--mt-warn-bg);
    border: 1px solid var(--mt-warn-border);
    border-radius: var(--border-radius);
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0.5rem 0 0;
    padding: 0.5rem 0.7rem;
    line-height: 1.6;
}

.empty-note {
    color: var(--gray-text-color);
    font-size: 0.9rem;
    margin: 0.5rem 0 0;
}

/* ラジオチップ */
.radio-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.25rem;
}

.radio-chip {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0.9rem;
    min-height: 44px;
    border: 1px solid var(--secondary-color);
    border-radius: 999px;
    background: var(--card-color);
    cursor: pointer;
    font-size: 0.9rem;
    user-select: none;
    transition: background-color 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.radio-chip input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
}

.radio-chip span {
    position: relative;
    z-index: 1;
    pointer-events: none;
}

.radio-chip:has(input:checked) {
    background: var(--primary-color);
    color: var(--text-color-light);
    border-color: var(--primary-color);
}

/* 色以外でも選択状態が分かるよう font-weight も変える */
.radio-chip:has(input:checked) span {
    font-weight: 700;
}

.radio-chip:focus-within {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}

.override-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.5rem;
    min-height: 44px;
}

.override-row label {
    font-size: 0.875rem;
    color: var(--text-color);
    cursor: pointer;
    margin: 0;
}

.auto-hint {
    font-size: 0.8rem;
    color: var(--gray-text-color);
}

/* ========== ボタン ========== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    min-height: 44px;
    padding: 0.6rem 1.1rem;
    border-radius: var(--border-radius);
    border: 1px solid transparent;
    font-size: 0.95rem;
    font-family: inherit;
    font-weight: 700;
    cursor: pointer;
    text-align: center;
    transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.btn:active { transform: translateY(1px); }

.btn:focus-visible {
    outline: 3px solid var(--mt-focus);
    outline-offset: 2px;
}

.btn-primary {
    background: var(--primary-color);
    color: var(--text-color-light);
}
.btn-primary:hover { background: var(--primary-color-dark); }

.btn-ghost {
    background: var(--card-color);
    color: var(--primary-color);
    border-color: var(--secondary-color);
}
.btn-ghost:hover {
    background: var(--background-color);
    border-color: var(--primary-color);
}

.btn-danger {
    background: var(--card-color);
    color: var(--mt-danger);
    border-color: var(--mt-danger-border);
}
.btn-danger:hover {
    background: var(--mt-danger-bg);
    border-color: var(--mt-danger);
}

.btn-sm {
    min-height: 44px;
    padding: 0.4rem 0.8rem;
    font-size: 0.85rem;
}

/* aria-disabled による無効表現（フォーカスは残す） */
.btn[aria-disabled="true"],
.btn.is-disabled {
    opacity: 0.5;
    cursor: not-allowed;
}
.btn[aria-disabled="true"]:active,
.btn.is-disabled:active { transform: none; }

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    background: var(--card-color);
    color: var(--text-color);
    font-size: 1.1rem;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}
.btn-icon:hover:not(:disabled) {
    background: var(--background-color);
    border-color: var(--primary-color);
}
.btn-icon:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
.btn-icon:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}
.btn-icon-danger { color: var(--mt-danger); }
.btn-icon-danger:hover:not(:disabled) {
    background: var(--mt-danger-bg);
    border-color: var(--mt-danger);
}

.action-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    margin-top: 1rem;
}

.action-note {
    margin: 0.75rem 0 0;
    padding: 0.6rem 0.8rem;
    background: var(--mt-warn-bg);
    border: 1px solid var(--mt-warn-border);
    border-radius: var(--border-radius);
    color: var(--mt-warn);
    font-size: 0.85rem;
    font-weight: 600;
    line-height: 1.6;
}

.dirty-note {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--mt-warn);
}

/* ========== 空状態 ========== */
.empty-state p {
    color: var(--gray-text-color);
}

/* ========== プレビュー ========== */
.preview-headers {
    margin: 0 0 1rem;
    padding: 0.75rem 1rem;
    background: var(--card-color);
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
}

.preview-header-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.75rem;
    padding: 0.25rem 0;
}

.preview-header-row dt {
    flex: 0 0 6rem;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--gray-text-color);
}

.preview-header-row dd {
    flex: 1 1 240px;
    margin: 0;
    min-width: 0;
    word-break: break-word;
    font-size: 0.95rem;
}

.pv-body-text {
    margin: 0;
    padding: 1rem;
    background: var(--card-color);
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    white-space: pre-wrap;
    word-break: break-word;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9rem;
    line-height: 1.7;
    color: var(--text-color);
    max-height: 26rem;
    overflow: auto;
}

.pv-source {
    font-size: 0.8rem;
    color: var(--gray-text-color);
}

/* iframe の中身は実際のメール表示に近づけるため常に白背景 */
.pv-frame {
    display: block;
    width: 100%;
    min-height: 240px;
    height: 320px;
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    background: #ffffff;
}

/* ========== テンプレート一覧 ========== */
.tpl-list {
    list-style: none;
    padding: 0;
    margin: 1rem 0 0;
}

.tpl-item {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 0.75rem;
    padding: 0.6rem 0.85rem;
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    background: var(--background-color);
    margin-bottom: 0.5rem;
}

.tpl-item-name {
    flex: 1 1 12rem;
    min-width: 0;
    font-weight: 700;
    word-break: break-word;
}

.tpl-item-meta {
    flex: 0 0 auto;
    font-size: 0.8rem;
    color: var(--gray-text-color);
}

.tpl-item-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-left: auto;
}

/* ========== 変数チップ ========== */
.chip-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem;
    margin-bottom: 0.6rem;
}

.chip-row-label {
    font-size: 0.8rem;
    color: var(--gray-text-color);
    font-weight: 600;
}

.chip {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 0.35rem 0.8rem;
    border: 1px solid var(--secondary-color);
    border-radius: 999px;
    background: var(--card-color);
    color: var(--primary-color);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}
.chip:hover {
    background: var(--background-color);
    border-color: var(--primary-color);
}
.chip:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}

.chip-add {
    border-style: dashed;
}

/* ========== 本文ツールバー ========== */
/* 中身のある行が1つも無いときは枠ごと出さない。
   [hidden] を除外しないと、テキスト入力時に隠している Markdown 行だけで枠が出てしまう */
.body-toolbar:has(.chip-row:not(:empty):not([hidden])) {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    margin-bottom: 0.6rem;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--mt-field-border);
    border-radius: var(--border-radius);
    background: var(--mt-muted-surface);
}

.body-toolbar .chip-row {
    margin-bottom: 0;
}

/* ========== Markdown ツールバー ========== */
.md-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    min-height: 44px;
    padding: 0.35rem 0.8rem;
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    background: var(--card-color);
    color: var(--text-color);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease;
}
.md-btn:hover {
    background: var(--background-color);
    border-color: var(--primary-color);
}
.md-btn:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}
.md-btn strong,
.md-btn em {
    color: var(--primary-color);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

/* ========== Markdown プレビュー / リッチテキスト編集面 ==========
   書きながら仕上がりが分かるよう、両者はまったく同じ組版規則を共有する */
.md-preview,
.rich-body {
    padding: 1rem;
    background: var(--card-color);
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    color: var(--text-color);
    font-size: 0.95rem;
    line-height: 1.7;
    word-break: break-word;
    max-height: 26rem;
    overflow: auto;
}
.md-preview > :first-child,
.rich-body > :first-child { margin-top: 0; }
.md-preview > :last-child,
.rich-body > :last-child { margin-bottom: 0; }
.md-preview p,
.rich-body p { margin: 0 0 1em; }
.md-preview h1,
.md-preview h2,
.md-preview h3,
.rich-body h1,
.rich-body h2,
.rich-body h3 {
    margin: 1.2em 0 0.5em;
    line-height: 1.3;
    font-weight: 700;
    color: var(--text-color);
}
.md-preview h1,
.rich-body h1 { font-size: 1.4rem; }
.md-preview h2,
.rich-body h2 { font-size: 1.2rem; }
.md-preview h3,
.rich-body h3 { font-size: 1.05rem; }
.md-preview ul,
.md-preview ol,
.rich-body ul,
.rich-body ol {
    margin: 0 0 1em;
    padding-left: 1.5em;
}
.md-preview li,
.rich-body li { margin: 0 0 0.25em; }
.md-preview blockquote,
.rich-body blockquote {
    margin: 0 0 1em;
    padding: 0.25em 0 0.25em 1em;
    border-left: 3px solid var(--secondary-color);
    color: var(--gray-text-color);
}
.md-preview hr,
.rich-body hr {
    border: 0;
    border-top: 1px solid var(--secondary-color);
    margin: 1.5em 0;
}
.md-preview a,
.rich-body a { color: var(--primary-color); }
.md-preview code,
.rich-body code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.85em;
    background: var(--mt-muted-surface);
    padding: 0.1em 0.3em;
    border-radius: 3px;
}
.md-preview pre,
.rich-body pre {
    margin: 0 0 1em;
    padding: 0.75em 1em;
    background: var(--mt-muted-surface);
    border-radius: var(--border-radius);
    overflow-x: auto;
}
.md-preview pre code,
.rich-body pre code {
    background: none;
    padding: 0;
}

/* ========== リッチテキスト編集面の固有分 ========== */
.rich-body {
    min-height: 16rem;
    border-color: var(--mt-field-border);
    resize: vertical;
}
.rich-body:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}
/* 空のときだけ入力例を出す。contenteditable には placeholder が無いため */
.rich-body:empty::before {
    content: attr(data-placeholder);
    color: var(--gray-text-color);
    pointer-events: none;
}

/* ========== 変数化ダイアログ ========== */
.var-dialog {
    /* padding を含めた幅で収めないと狭い画面でビューポートをはみ出す */
    box-sizing: border-box;
    width: min(30rem, calc(100vw - 2rem));
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
    padding: 1.25rem 1.5rem;
    border: 1px solid var(--mt-field-border);
    border-radius: var(--border-radius);
    background: var(--card-color);
    color: var(--text-color);
}

.var-dialog::backdrop {
    background: rgba(0, 0, 0, 0.5);
}

.var-dialog h2 {
    margin-top: 0;
}

.var-dialog .field-block {
    margin-bottom: 1rem;
}

.var-dialog-actions {
    margin-top: 1.25rem;
    margin-bottom: 0;
}

/* 種類に応じて出す追加設定 */
.var-dialog-sub {
    margin-bottom: 1rem;
    padding: 0.85rem 1rem 0.1rem;
    border: 1px solid var(--mt-field-border);
    border-radius: var(--border-radius);
    background: var(--mt-muted-surface);
}

.var-dialog-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
    gap: 0 0.75rem;
}

.var-dialog-sub .field-block {
    margin-bottom: 0.75rem;
}

/* 欄ごとの型制限に反している変数の一覧 */
.var-type-warn {
    margin-top: 1rem;
    padding: 0.75rem 1rem;
    border: 1px solid var(--mt-danger-border);
    border-radius: var(--border-radius);
    background: var(--mt-danger-bg);
    color: var(--mt-danger);
    font-size: 0.9rem;
}

.var-type-warn p {
    margin: 0.35rem 0;
}

.field-hint-guess {
    color: var(--primary-color);
    font-weight: 600;
}

.checkbox-label {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    min-height: 44px;
    cursor: pointer;
}

/* ========== 変数編集行 ========== */
.var-row {
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    background: var(--card-color);
    padding: 0.75rem 1rem 1rem;
    margin-bottom: 0.75rem;
}

.var-row-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--secondary-color);
}

.var-row-title {
    flex: 1 1 10rem;
    min-width: 0;
    font-weight: 700;
    font-size: 0.9rem;
    color: var(--primary-color);
    word-break: break-word;
}

.var-row-actions {
    display: flex;
    gap: 0.4rem;
    margin-left: auto;
}

.var-row-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 0.75rem 1rem;
}

.var-row-grid .field-block { margin-bottom: 0; }

.checkbox-block {
    display: flex;
    align-items: flex-end;
}

/* ========== HTML 出力設定 ========== */
#html-settings > summary,
.nested-details > summary {
    cursor: pointer;
    font-weight: 700;
    color: var(--primary-color);
    padding: 0.6rem 0;
    min-height: 44px;
    display: flex;
    align-items: center;
}

#html-settings > summary:focus-visible,
.nested-details > summary:focus-visible {
    outline: 2px solid var(--mt-focus);
    outline-offset: 2px;
}

.details-body {
    padding-top: 0.5rem;
}

.nested-details {
    border: 1px solid var(--secondary-color);
    border-radius: var(--border-radius);
    padding: 0 0.85rem 0.5rem;
    margin: 1rem 0;
    background: var(--background-color);
}

.style-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.75rem 1rem;
    margin: 1rem 0;
}

.style-grid .field-block { margin-bottom: 0; }

/* ========== テンプレート管理 ========== */
.data-stats {
    margin: 0 0 0.5rem;
    color: var(--gray-text-color);
    font-size: 0.9rem;
}

/* 一覧とエディタは排他表示。戻る導線を本文の先頭に置く */
.editor-back-row {
    margin-bottom: 0.75rem;
}

.import-choice {
    margin-top: 1rem;
    padding: 0.85rem 1rem;
    border: 1px solid var(--primary-color);
    border-radius: var(--border-radius);
    background: var(--background-color);
}

.import-choice-title {
    margin: 0;
    font-size: 0.9rem;
    font-weight: 600;
}

/* 破壊的操作は畳んでおき、開いたときだけ危険色の枠を見せる */
.danger-details {
    margin-top: 1.75rem;
}

.danger-details > summary {
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--mt-danger);
}

.danger-zone {
    margin-top: 0.5rem;
    padding: 1rem;
    border: 1px solid var(--mt-danger-border);
    border-radius: var(--border-radius);
    background: var(--mt-danger-bg);
}

.danger-zone .field-hint {
    color: var(--mt-danger);
}

/* ========== SEO コンテンツ ========== */
.feature-list {
    margin: 0.5rem 0 1rem;
    padding-left: 1.2rem;
}
.feature-list li { margin-bottom: 0.4rem; }

/* ========== トースト ========== */
.toast {
    position: fixed;
    left: 50%;
    bottom: 2rem;
    transform: translateX(-50%);
    max-width: min(90vw, 30rem);
    background: var(--primary-color-dark);
    color: var(--text-color-light);
    padding: 0.7rem 1.2rem;
    border-radius: 999px;
    font-weight: 600;
    font-size: 0.9rem;
    text-align: center;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
}

.toast.is-visible {
    opacity: 1;
    transform: translateX(-50%) translateY(-4px);
}

/* 取り消しボタン付きのトースト。親が pointer-events: none でも
   子で auto に戻せば押せる（トースト全体はクリックを遮らないままにする） */
.toast:has(.toast-action) {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
}

.toast-action {
    flex: none;
    min-height: 32px;
    padding: 0.25rem 0.9rem;
    border: 1px solid rgba(255, 255, 255, 0.6);
    border-radius: 999px;
    background: transparent;
    color: var(--text-color-light);
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    pointer-events: auto;
    transition: background-color 0.15s ease;
}

.toast-action:hover {
    background: rgba(255, 255, 255, 0.18);
}

.toast-action:focus-visible {
    outline: 2px solid #ffffff;
    outline-offset: 2px;
}

/* ========== レスポンシブ ========== */
@media (max-width: 600px) {
    .form-step {
        padding: 1rem 0.85rem 1.1rem;
    }
    .tab-btn {
        padding: 0.6rem 0.7rem;
        font-size: 0.9rem;
    }
    .var-row-grid,
    .style-grid {
        grid-template-columns: 1fr;
    }
    .field-row {
        flex-direction: column;
        gap: 0;
    }
    .action-row .btn {
        flex: 1 1 100%;
    }
    .tpl-item-actions {
        margin-left: 0;
        width: 100%;
    }
    .tpl-item-actions .btn {
        flex: 1 1 auto;
    }
    .preview-header-row dt {
        flex: 0 0 100%;
    }
}

/* ========== ダークモード ========== */
@media (prefers-color-scheme: dark) {
    :root {
        --mt-danger: #fca5a5;
        --mt-danger-border: #ef4444;
        --mt-danger-bg: rgba(127, 29, 29, 0.3);
        --mt-warn: #fcd34d;
        --mt-warn-bg: rgba(120, 53, 15, 0.35);
        --mt-warn-border: #b45309;
        --mt-focus: #60a5fa;
        --mt-muted-surface: #1f2937;
        --mt-field-border: #7b8695;
        --mt-token-bg: #2c4a7c;
        --mt-token-border: #4a6fd4;
        --mt-token-active-bg: #3b5f9e;
        /* 矢印の線色を明るくしないと暗い面でほぼ見えない */
        --mt-select-arrow: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' stroke='%23a0aec0' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
        /* 暗い面の上では --primary-color（#4a6fd4）が 2.5:1 しか出ないため、
           文字色として使う箇所だけ明るい青に置き換えてコントラスト 4.5:1 以上を確保する */
        --mt-accent-text: #93c5fd;
    }

    .btn-ghost,
    .btn-ghost:hover,
    .chip,
    .chip:hover,
    .tab-btn:hover,
    .tab-btn.active,
    .var-row-title,
    .form-step legend,
    #html-settings > summary,
    .nested-details > summary {
        color: var(--mt-accent-text);
    }

    .tab-btn.active { border-bottom-color: var(--mt-accent-text); }

    .chip,
    .field-hint-guess,
    .var-dialog h2,
    .var-float-label,
    .md-btn strong,
    .md-btn em,
    .md-preview a,
    .rich-body a {
        color: var(--mt-accent-text);
    }

    .var-dialog::backdrop {
        background: rgba(0, 0, 0, 0.65);
    }

    .btn-danger { background: var(--card-color); }
    .btn-danger:hover { background: var(--mt-danger-bg); }
}

/* ========== モーション低減 ========== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }
    .btn:active { transform: none; }
}
