:root {
  --bg: #faf7f3;          /* 生成り */
  --fg: #2a2424;          /* 墨 */
  --muted: #8a7d77;       /* 温かみのあるグレー */
  --brand: #c2185b;       /* 千波ピンク（維持） */
  --brand-dark: #8e0036;
  --border: #ece2dc;      /* 淡い暖色境界 */
  --accent: #6b4f8e;      /* 京紫 */
  --ok: #15803d;
  --err: #b91c1c;
  --warn: #b45309;
  --radius: 8px;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; color: var(--fg); }
html { background: var(--bg); }
body {
  font-family: "Hiragino Sans", "Meiryo", "Segoe UI", system-ui, sans-serif;
  font-size: 14px; line-height: 1.6;
  min-height: 100vh;
  background:
    linear-gradient(180deg, #faf7f3 0%, #f5ede0 60%, #efe4d2 100%);
  background-attachment: fixed;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.appbar {
  display: flex; align-items: center; gap: 20px;
  background: linear-gradient(180deg, #ffffff 0%, #fbf7f1 100%);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px; position: sticky; top: 0; z-index: 10;
  box-shadow: 0 2px 14px rgba(107, 79, 142, .06);
}
.appbar::after {
  content: ''; position: absolute; left: 0; right: 0; bottom: -1px; height: 2px;
  background: linear-gradient(90deg, transparent 0%, var(--brand) 25%, var(--accent) 75%, transparent 100%);
  opacity: .35; pointer-events: none;
}
.appbar .brand { font-weight: bold; color: var(--brand); font-size: 16px; letter-spacing: .02em; }
.appbar nav { display: flex; gap: 16px; margin-left: 8px; }
.appbar nav a { color: var(--fg); position: relative; padding: 4px 2px; transition: color .15s; }
.appbar nav a:hover { color: var(--brand); text-decoration: none; }
.appbar .user { margin-left: auto; display: flex; gap: 10px; align-items: center; color: var(--muted); }
.appbar .user button.link { background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; font: inherit; }
.appbar .user button.link:hover { color: var(--brand); text-decoration: underline; }

.container { max-width: 1100px; margin: 24px auto; padding: 0 20px; }
h1 {
  font-size: 22px; margin: 0 0 18px;
  padding-bottom: 10px; position: relative;
  letter-spacing: .02em;
}
h1::after {
  content: ''; position: absolute; left: 0; bottom: 0;
  width: 56px; height: 3px; border-radius: 2px;
  background: linear-gradient(90deg, var(--brand) 0%, var(--accent) 100%);
}
h2 { font-size: 16px; margin: 24px 0 10px; color: #4a3838; }

.flash { padding: 10px 16px; margin: 10px 0; border-radius: var(--radius); }
.flash.info    { background: #dbeafe; color: #1e40af; }
.flash.success { background: #dcfce7; color: #166534; }
.flash.error   { background: #fee2e2; color: #991b1b; }
.flash.warn    { background: #fef3c7; color: #854d0e; }

.login-box {
  max-width: 380px; margin: 100px auto; padding: 28px;
  background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 2px 10px rgba(0,0,0,.05);
}
.login-box h1 { color: var(--brand); text-align: center; margin-bottom: 20px; }
.login-box label { display: block; margin: 12px 0; }
.login-box input { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 4px; font-size: 14px; }
.login-box button { width: 100%; padding: 10px; margin-top: 12px; background: var(--brand); color: #fff; border: 0; border-radius: 4px; cursor: pointer; font-size: 14px; }
.login-box button:hover { background: var(--brand-dark); }
.login-box .error { color: var(--err); background: #fee2e2; padding: 8px 12px; border-radius: 4px; }

.lock-box {
  background: #fffbeb;
  border: 2px solid #f59e0b;
  border-radius: 10px;
  padding: 18px 20px;
  margin-bottom: 16px;
}
.lock-title { font-size: 18px; font-weight: 700; color: #b45309; margin-bottom: 10px; }
.lock-remain { font-size: 15px; margin-bottom: 8px; }
.lock-remain strong { font-size: 22px; color: #b45309; margin: 0 4px; }
.lock-reason { font-size: 13px; color: #78350f; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat {
  background: linear-gradient(180deg, #ffffff 0%, #fbf7f1 100%);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: 0 1px 3px rgba(107, 79, 142, .04);
  transition: box-shadow .15s, transform .12s;
}
.stat:hover { box-shadow: 0 4px 14px rgba(107, 79, 142, .1); transform: translateY(-1px); }
.stat .label { color: var(--muted); font-size: 12px; letter-spacing: .03em; }
.stat .num { font-size: 22px; font-weight: 600; color: #1a1212; }

.table { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); }
.table th, .table td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { background: #fafafa; font-weight: 600; font-size: 13px; }
.table tr:last-child td { border-bottom: 0; }

/* Sticky thead — th sticks right under the sticky .appbar. */
.appbar { height: 52px; padding-top: 0; padding-bottom: 0; }   /* fixed-height appbar so sticky offset is reliable */
.table thead th {
  position: sticky;
  top: 52px;                 /* match .appbar height exactly */
  z-index: 3;
  background: #fafafa;
  box-shadow: inset 0 -1px 0 var(--border);
}

.btn {
  display: inline-block; padding: 6px 14px;
  border: 1px solid var(--border); background: #fff;
  color: var(--fg); border-radius: 6px; cursor: pointer;
  font-size: 13px; text-decoration: none;
  transition: background .12s, border-color .12s, box-shadow .12s, transform .08s;
}
.btn:disabled, .btn[disabled] { background: #e5e7eb !important; color: #9ca3af !important; border-color: #d1d5db !important; cursor: not-allowed !important; opacity: 1 !important; box-shadow: none !important; }
.btn:disabled:hover, .btn[disabled]:hover { background: #e5e7eb !important; color: #9ca3af !important; }
.btn:hover { background: #fbf5ee; border-color: #d8c5be; text-decoration: none; }
.btn.primary {
  background: linear-gradient(180deg, var(--brand) 0%, var(--brand-dark) 100%);
  color: #fff; border-color: var(--brand-dark);
  box-shadow: 0 1px 3px rgba(194, 24, 91, .25);
}
.btn.primary:hover { background: linear-gradient(180deg, var(--brand-dark) 0%, #6e0029 100%); box-shadow: 0 3px 10px rgba(194, 24, 91, .35); transform: translateY(-1px); }
.btn.primary:active { transform: translateY(0); }
.btn.danger { color: var(--err); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; border-color: #fca5a5; }

.muted { color: var(--muted); }

.filter { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.filter input, .filter select { padding: 6px 10px; border: 1px solid var(--border); border-radius: 4px; font-size: 13px; background: #fff; }
.filter input[type=text] { min-width: 200px; }

.form { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; max-width: 640px; }
.form label { display: block; margin: 10px 0; font-size: 13px; color: var(--muted); }
.form label > input, .form label > select, .form label > textarea { display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 4px; font-size: 14px; color: var(--fg); background: #fff; }
.form label > input[readonly] { background: #f8f9fa; color: var(--muted); }
.form-actions { display: flex; gap: 8px; margin-top: 16px; }

.meta-kv { display: grid; grid-template-columns: 120px 1fr; gap: 4px 12px; font-size: 12px; margin: 10px 0; padding: 10px; background: #fafafa; border-radius: 4px; }
.meta-kv dt { color: var(--muted); }
.meta-kv dd { margin: 0; }

/* Inline meta editor table on the campaign show page */
.meta-edit-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 14px; }
.meta-edit-table th { text-align: left; vertical-align: middle; width: 200px; color: var(--muted); font-weight: normal; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.meta-edit-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.meta-edit-table tr:last-child th, .meta-edit-table tr:last-child td { border-bottom: 0; }
.meta-edit-table input[type=text], .meta-edit-table input[type=email], .meta-edit-table input[type=datetime-local], .meta-edit-table select { width: 100%; max-width: 420px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; box-sizing: border-box; }
.meta-edit-table input:disabled, .meta-edit-table select:disabled { background: #f3f4f6; color: var(--muted); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.status-active       { background: #dcfce7; color: #166534; }
.badge.status-unsubscribed { background: #fee2e2; color: #991b1b; }
.badge.status-bounced      { background: #fef3c7; color: #854d0e; }
.badge.status-complained   { background: #fce7f3; color: #9d174d; }
.badge.status-suppressed   { background: #e5e7eb; color: #4b5563; }

.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin: 20px 0; }

/* Big variants for send-related screens (elderly users, larger touch targets) */
.btn.big { font-size: 16px; padding: 10px 22px; min-height: 44px; }
.form.big label { font-size: 15px; }
.form.big label > input, .form.big label > select, .form.big label > textarea { font-size: 15px; padding: 10px 12px; }
.form.big fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 14px 18px; margin: 14px 0; background: #fafbfc; }
.form.big fieldset > legend { padding: 0 8px; color: var(--muted); font-size: 13px; }
.form.big .form-actions { gap: 12px; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border); }

.big-card { background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 24px; }
/* Keep inline forms (CSRF wrappers) from disturbing button row alignment */
.big-card form { margin: 0; vertical-align: middle; }
.big-card .btn { vertical-align: middle; }
/* Operation button rows: flex-aligned regardless of mix of <a> / <form><button> */
.op-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.op-actions form { display: inline-flex; margin: 0; }
.op-actions .btn { margin: 0; }

.progress { width: 100%; height: 10px; background: #e5e7eb; border-radius: 5px; overflow: hidden; }
.progress.big { height: 18px; border-radius: 9px; }
.progress-bar { height: 100%; background: linear-gradient(90deg, var(--brand) 0%, var(--brand-dark) 100%); transition: width .3s; }

.badge.campaign-draft     { background: #e5e7eb; color: #4b5563; }
.badge.campaign-queued    { background: #dbeafe; color: #1e40af; }
.badge.campaign-sending   { background: #fef3c7; color: #854d0e; }
.badge.campaign-paused    { background: #ede9fe; color: #6d28d9; }
.badge.campaign-sent      { background: #dcfce7; color: #166534; }
.badge.campaign-cancelled { background: #fee2e2; color: #991b1b; }
.badge.campaign-failed    { background: #fee2e2; color: #991b1b; }

.kpi { background: #fafbfc; border: 1px solid var(--border); border-radius: 8px; padding: 12px 16px; }
.kpi-label { font-size: 12px; color: var(--muted); }
.kpi-num { font-size: 18px; font-weight: 600; margin-top: 4px; }

/* Reaction timeline (per-campaign analytics) */
.timeline-chart { background: #fff; border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; }
.timeline-legend { display: flex; gap: 16px; align-items: center; font-size: 12px; margin-bottom: 10px; }
.timeline-legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }
.timeline-legend .sw-open  { background: #f59e0b; }
.timeline-legend .sw-click { background: #c2185b; }
.timeline-svg { width: 100%; height: auto; display: block; }

/* 3-bucket grouping (配信可/停止/無効) */
.group-filter { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.group-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 18px; border: 1px solid var(--border);
  border-radius: 999px; background: #fff; color: var(--fg); font-size: 14px;
  text-decoration: none;
}
.group-pill .num { font-weight: 700; }
.group-pill:hover { background: #f3f4f6; text-decoration: none; }
.group-pill.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.group-pill.group-active.active   { background: #15803d; border-color: #15803d; }
.group-pill.group-stopped.active  { background: #b45309; border-color: #b45309; }
.group-pill.group-invalid.active  { background: #b91c1c; border-color: #b91c1c; }

.stat-link { text-decoration: none; color: var(--fg); display: block; transition: transform .1s; }
.stat-link:hover { transform: translateY(-1px); text-decoration: none; box-shadow: 0 2px 8px rgba(0,0,0,.06); }
.stat.group-active .label  { color: #15803d; }
.stat.group-stopped .label { color: #b45309; }
.stat.group-invalid .label { color: #b91c1c; }

/* Dashboard sections */
.dash-section { margin-top: 24px; }
.dash-section-title {
  font-size: 16px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px;
}
.dash-dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  flex-shrink: 0;
}
.dash-dot-sending  { background: #f59e0b; box-shadow: 0 0 0 4px rgba(245,158,11,.2); animation: dash-pulse 2s ease-in-out infinite; }
.dash-dot-queued   { background: #2563eb; }
.dash-dot-draft    { background: #9ca3af; }
.dash-dot-sent     { background: #15803d; }
.dash-dot-activity { background: #9ca3af; }
@keyframes dash-pulse { 0%,100%{box-shadow:0 0 0 4px rgba(245,158,11,.2);} 50%{box-shadow:0 0 0 8px rgba(245,158,11,0);} }

/* ----- Modal (subscriber edit) ----- */
.sub-modal { padding: 0; border: 0; border-radius: 12px; max-width: 680px; width: 94vw; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.sub-modal::backdrop { background: rgba(0,0,0,.45); backdrop-filter: blur(2px); }
.sub-modal-form { display: flex; flex-direction: column; max-height: 88vh; }
.sub-modal-form > header { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); background: #fafafa; border-radius: 12px 12px 0 0; }
.sub-modal-form > header h2 { font-size: 17px; margin: 0; flex: 1; }
.modal-close { background: none; border: 0; font-size: 22px; cursor: pointer; color: var(--muted); padding: 4px 10px; line-height: 1; }
.modal-close:hover { color: var(--fg); }
.sub-modal-body { padding: 24px 28px; overflow-y: auto; }
.sub-modal-body label { display: block; width: 100%; font-size: 15px; color: var(--muted); margin: 18px 0 8px; font-weight: 500; }
.sub-modal-body label:first-child { margin-top: 4px; }
.sub-modal-body input, .sub-modal-body select {
  display: block; width: 100%;
  padding: 16px 18px;
  border: 2px solid var(--border); border-radius: 8px;
  font-size: 20px;        /* large enough for elderly + touch */
  color: var(--fg); background: #fff;
  min-height: 60px;
  box-sizing: border-box;
  font-family: inherit;
}
.sub-modal-body input:focus, .sub-modal-body select:focus { border-color: var(--brand); outline: none; box-shadow: 0 0 0 3px rgba(194,24,91,.15); }
.sub-modal-body input[readonly] { background: #f5f6f8; color: var(--muted); cursor: not-allowed; border-color: #e5e7eb; }
.sub-modal-body select:disabled { background: #f5f6f8; color: var(--muted); }
.sub-modal-actions { display: flex; gap: 12px; padding: 18px 26px; border-top: 1px solid var(--border); background: #fafafa; border-radius: 0 0 12px 12px; }
.sub-modal-actions .btn.big { font-size: 16px; padding: 14px 22px; min-height: 52px; }
.sub-modal-actions .btn.danger { margin-left: auto; }

.btn-link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font: inherit; }
.btn-link:hover { text-decoration: underline; }

/* Floating bulk action bar — fixed so it stays visible on any scroll position */
.bulk-bar {
  position: fixed;
  left: 20px; right: 20px; bottom: 16px;
  max-width: 1060px;   /* matches .container max-width 1100 - 40px */
  margin: 0 auto;
  z-index: 20;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 18px;
  background: #fff;
  border: 2px solid var(--brand);
  border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0,0,0,.18);
}
.bulk-bar-count { font-size: 16px; }
.bulk-bar-count strong { color: var(--brand); font-size: 22px; margin: 0 4px; }
.bulk-check, #bulk-select-all { width: 18px; height: 18px; cursor: pointer; }

/* Reserve bottom space so table content isn't hidden under the fixed bulk-bar */
body.has-bulk-bar .container { padding-bottom: 100px; }

/* ----- System pages ----- */
.sys-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-top: 16px; }
.sys-card { display: block; padding: 22px; background: #fff; border: 1px solid var(--border); border-radius: 12px; text-decoration: none; color: var(--fg); transition: transform .12s, box-shadow .12s; }
.sys-card:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(0,0,0,.08); text-decoration: none; }
.sys-icon { font-size: 32px; margin-bottom: 10px; }
.sys-title { font-size: 18px; font-weight: 700; margin-bottom: 6px; }
.sys-desc { color: var(--muted); font-size: 13px; }

.log-pre { background: #1e1e2e; color: #cdd6f4; padding: 14px 16px; border-radius: 6px; font-size: 12px; line-height: 1.55; overflow-x: auto; max-height: 480px; overflow-y: auto; white-space: pre; font-family: Consolas, Menlo, monospace; }

/* ----- Template editor split view ----- */
/* Break out of the 1100px .container so editor+preview+link-checker can use full viewport */
.tpl-edit {
  margin-left: calc(-50vw + 50% + 20px);
  margin-right: calc(-50vw + 50% + 20px);
  max-width: calc(100vw - 40px);
}
.tpl-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 14px; }
.tpl-split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr); gap: 16px; align-items: start; }
.tpl-pane-links { display: none; }   /* shown only on wide viewports */
@media (min-width: 1400px) {
  .tpl-split { grid-template-columns: minmax(0, 1fr) minmax(0, 1.6fr) minmax(280px, 320px); }
  .tpl-pane-links { display: flex; flex-direction: column; }
}
@media (max-width: 1100px) {
  .tpl-split { grid-template-columns: 1fr; }
  .tpl-pane-preview { position: static !important; max-height: 600px; }
  .tpl-edit { margin-left: 0; margin-right: 0; max-width: none; }
}
.tpl-pane { background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 18px; }
.tpl-pane-edit label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.tpl-pane-edit label:first-of-type { margin-top: 0; }
.tpl-pane-edit input, .tpl-pane-edit textarea { display: block; width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; box-sizing: border-box; font-family: Consolas, Menlo, monospace; }
.tpl-pane-edit input { font-family: inherit; }
.tpl-pane-edit textarea { font-size: 12px; line-height: 1.5; }
.tpl-actions { display: flex; gap: 10px; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.tpl-actions .btn.danger { margin-left: auto; }

.tpl-pane-preview {
  position: sticky; top: 68px;          /* below appbar */
  display: flex; flex-direction: column;
  height: calc(100vh - 90px);            /* fill viewport */
  padding: 14px;
}
.tpl-preview-toolbar { display: flex; align-items: baseline; gap: 10px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.tpl-preview-subject { font-size: 13px; padding: 8px 4px; color: var(--muted); border-bottom: 1px solid var(--border); }
.tpl-preview-subject strong { color: var(--fg); margin-left: 4px; }
.tpl-preview-iframe, #tpl-preview-iframe, #cmp-preview-iframe { flex: 1; width: 100%; border: 0; background: #fff; margin-top: 6px; }

/* Rich/Source mode switch */
.tpl-body-wrap { margin: 12px 0 4px; }
.tpl-body-head { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.tpl-body-head label { margin: 0 !important; color: var(--muted); font-size: 13px; }
.tpl-mode-switch { margin-left: auto; display: inline-flex; background: #f3f4f6; padding: 3px; border-radius: 8px; gap: 2px; }
.tpl-mode-btn { background: transparent; border: 0; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 12px; color: var(--muted); }
.tpl-mode-btn.is-active { background: #fff; color: var(--fg); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.tpl-mode-btn:hover:not(.is-active) { color: var(--fg); }

.tpl-rich-toolbar {
  display: flex; flex-wrap: wrap; gap: 4px;
  padding: 8px; background: #fafafa; border: 1px solid var(--border); border-bottom: 0; border-radius: 8px 8px 0 0;
}
.tpl-rich-toolbar button {
  background: #fff; border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; font-size: 13px; cursor: pointer; color: var(--fg);
}
.tpl-rich-toolbar button:hover { background: #f3f4f6; }
.tpl-rich-toolbar .sep { display: inline-block; width: 1px; background: var(--border); margin: 2px 4px; }

.tpl-rich-iframe {
  width: 100%; min-height: 480px; border: 1px solid var(--border); border-radius: 0 0 8px 8px; background: #fff;
}

/* Toolbar swatch-indicator (color preview inside button) */
.tpl-rich-toolbar .swatch-indicator {
  display: inline-block; padding: 0 6px; border-radius: 3px; border: 1px solid rgba(0,0,0,.15);
  color: #fff; font-weight: 700; font-size: 11px; margin-right: 2px;
}

/* Toolbar popovers */
.tpl-pane-edit { position: relative; }
.tpl-pop {
  position: absolute; z-index: 30;
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; min-width: 180px;
  box-shadow: 0 8px 20px rgba(0,0,0,.1);
}
.tpl-pop-wide { min-width: 260px; }
.tpl-pop-title { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.tpl-swatches { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; }
.tpl-swatch { width: 20px; height: 20px; border: 1px solid rgba(0,0,0,.15); border-radius: 4px; cursor: pointer; padding: 0; }
.tpl-swatch:hover { transform: scale(1.15); }
.mini-btn { padding: 5px 10px; border: 1px solid var(--border); background: #fff; border-radius: 4px; cursor: pointer; font-size: 12px; }
.mini-btn:hover { background: #f3f4f6; }

/* Image tabs + upload drop */
.tpl-image-tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.tpl-image-tab { background: #f3f4f6; border: 0; padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 12px; color: var(--muted); }
.tpl-image-tab.is-active { background: var(--brand); color: #fff; }
.tpl-upload-drop {
  display: block; padding: 18px; text-align: center;
  border: 2px dashed var(--border); border-radius: 8px; cursor: pointer;
  color: var(--muted); font-size: 13px; background: #fafafa;
}
.tpl-upload-drop:hover, .tpl-upload-drop.is-drag { border-color: var(--brand); background: #fff; }

/* Image selection popup over iframe */
.tpl-imgpop {
  background: #1f2937; color: #fff; padding: 4px; border-radius: 6px;
  display: flex; gap: 2px; box-shadow: 0 4px 14px rgba(0,0,0,.3);
  font-size: 12px;
}
.tpl-imgpop button { background: transparent; border: 0; color: #fff; padding: 6px 10px; cursor: pointer; border-radius: 4px; font-size: 12px; }
.tpl-imgpop button:hover { background: rgba(255,255,255,.1); }
.tpl-imgpop button.danger { color: #fecaca; }
.tpl-imgpop .sep { width: 1px; background: rgba(255,255,255,.2); margin: 0 2px; }

/* Link checker panel */
.tpl-pane-links {
  position: sticky; top: 68px;
  height: calc(100vh - 90px);
  padding: 14px;
  overflow: hidden;
}
.tpl-links-list { overflow-y: auto; flex: 1; margin-top: 8px; }
.link-item { border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin-bottom: 10px; background: #fff; cursor: pointer; transition: box-shadow .15s, border-color .15s; }
.link-item:hover { border-color: var(--brand); box-shadow: 0 2px 8px rgba(194,24,91,.08); }
.link-item--other { padding: 6px 10px; background: #fafafa; }
.link-item--other .link-label { font-size: 11px; font-weight: 500; color: var(--muted); }
.link-item--other .link-url { margin-top: 2px; }
.link-item.has-err { background: #fef2f2; border-color: #fecaca; }
.link-item--other.has-err .link-label { color: var(--err); font-weight: 700; }
.link-row-top { display: flex; gap: 6px; align-items: baseline; }
.link-num { font-size: 11px; color: var(--muted); font-weight: 700; flex-shrink: 0; }
.link-label { font-size: 12px; color: var(--fg); font-weight: 600; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.link-arrow { font-size: 10px; color: var(--muted); margin: 4px 0 2px; }
.link-title { font-size: 13px; font-weight: 600; margin-bottom: 6px; line-height: 1.4; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.link-title.muted { color: var(--muted); font-weight: normal; }
.link-url { font-size: 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 6px; color: var(--muted); }
.link-url a { color: var(--accent); }
.link-thumb {
  width: 100%; height: 170px; overflow: hidden; background: #f3f4f6; border-radius: 4px; position: relative;
  display: flex; align-items: center; justify-content: center;
}
.link-thumb-inner { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
.link-thumb-inner img { max-width: 100%; max-height: 100%; object-fit: contain; }
.no-thumb { color: var(--muted); font-size: 11px; }
.link-status { margin-top: 8px; font-size: 12px; padding: 4px 8px; border-radius: 4px; display: inline-block; }
.link-status[data-st="pending"] { background: #f3f4f6; color: var(--muted); }
.link-status[data-st="ok"]      { background: #dcfce7; color: #15803d; font-weight: 600; }
.link-status[data-st="err"]     { background: #fee2e2; color: #b91c1c; font-weight: 700; }
.link-status[data-st="warn"]    { background: #fef3c7; color: #854d0e; }
.error-box { max-width: 500px; margin: 60px auto; text-align: center; }
.error-box h1 { color: var(--err); }

/* Image gallery (/system/images + in-editor tab) */
.gal-grid { display:grid; grid-template-columns:repeat(auto-fill, minmax(180px, 1fr)); gap:14px; }
.gal-item { background:#fff; border:1px solid var(--border); border-radius:8px; padding:10px; display:flex; flex-direction:column; gap:8px; }
.gal-thumb { display:flex; align-items:center; justify-content:center; height:140px; background:#f4f4f5; border-radius:6px; overflow:hidden; }
.gal-thumb img { max-width:100%; max-height:140px; display:block; }
.gal-meta .gal-name { font-size:12px; font-family:ui-monospace, SFMono-Regular, Menlo, monospace; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.gal-actions { display:flex; gap:6px; justify-content:space-between; }
.btn.mini { padding:4px 10px; font-size:12px; }
.btn.mini.danger { background:#fee2e2; color:#b91c1c; border-color:#fca5a5; }

/* In-editor mini gallery (image popover) */
.tpl-gallery-grid { display:grid; grid-template-columns:repeat(3, 1fr); gap:6px; max-height:260px; overflow-y:auto; padding:4px; }
.tpl-gallery-grid .gal-pick { cursor:pointer; border:2px solid transparent; border-radius:4px; overflow:hidden; background:#f4f4f5; height:80px; display:flex; align-items:center; justify-content:center; transition:border-color 0.1s; }
.tpl-gallery-grid .gal-pick:hover { border-color:var(--brand); }
.tpl-gallery-grid .gal-pick img { max-width:100%; max-height:76px; }
.tpl-gallery-empty { padding:30px; text-align:center; color:#888; font-size:12px; }

/* ===========================================================
   Mobile (≤ 640px) — 見切れ防止・基本レイアウト調整
   =========================================================== */
@media (max-width: 640px) {
  body { font-size: 13px; }

  /* Container: 左右パディングを縮める */
  .container { margin: 12px auto; padding: 0 12px; }

  /* Appbar: brand ＋ nav が横スクロールできるように、高さは維持 */
  .appbar { padding: 10px 12px; gap: 12px; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .appbar .brand { font-size: 14px; flex-shrink: 0; }
  .appbar nav { gap: 10px; flex-shrink: 0; }
  .appbar nav a { font-size: 13px; white-space: nowrap; }
  .appbar .user { gap: 6px; flex-shrink: 0; font-size: 12px; }

  h1 { font-size: 18px; }
  h2 { font-size: 15px; }

  /* Stats grid: もう一段細かい */
  .stats { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
  .stat { padding: 10px 12px; }
  .stat .num { font-size: 18px; }

  /* Tables: カード形式に切り替え。各行をカードとして縦スタック */
  .table { background: transparent; border: 0; max-width: 100%; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; box-sizing: border-box; }
  .table tr {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 8px;
    padding: 10px 12px 8px;
    position: relative;
  }
  .table td {
    border-bottom: 0 !important;
    padding: 3px 0;
    font-size: 13px;
    text-align: left;
  }
  /* チェックボックス td（最初の列）をカード右上に配置 */
  .table tr > td:first-child:has(input[type=checkbox]) {
    position: absolute;
    top: 8px;
    right: 8px;
    width: auto;
    padding: 0;
  }
  /* ID 列など .muted のみの td はインライン化＆フォント縮小 */
  .table td.muted { font-size: 11px; padding: 2px 0; }
  /* 入力フォーム系のセル幅を素直に */
  .table td input, .table td select { max-width: 100%; box-sizing: border-box; }

  /* Filter: 入力欄を全幅に */
  .filter { gap: 6px; }
  .filter input, .filter select { font-size: 13px; padding: 8px 10px; }
  .filter input[type=text] { min-width: 0; flex: 1 1 100%; }

  /* Form: max-width 解除して全幅 */
  .form { padding: 14px; max-width: none; }
  .form-actions { flex-wrap: wrap; gap: 6px; }

  /* meta-kv: 縦並びに */
  .meta-kv { grid-template-columns: 1fr; gap: 2px; padding: 8px 10px; }
  .meta-kv dt { font-size: 11px; margin-top: 4px; }

  /* meta-edit-table: th 幅固定を解除、行ごと縦スタック */
  .meta-edit-table, .meta-edit-table tbody, .meta-edit-table tr,
  .meta-edit-table th, .meta-edit-table td { display: block; width: 100%; }
  .meta-edit-table th { padding: 8px 4px 2px; border-bottom: 0; white-space: normal; font-size: 12px; }
  .meta-edit-table td { padding: 0 4px 10px; border-bottom: 1px solid var(--border); }
  .meta-edit-table input[type=text], .meta-edit-table input[type=email],
  .meta-edit-table input[type=datetime-local], .meta-edit-table select { max-width: none; }

  /* Bulk-bar: 全幅に近づける、ボタン折り返し */
  .bulk-bar { left: 8px; right: 8px; bottom: 8px; padding: 10px 12px; gap: 8px; }
  .bulk-bar-count { font-size: 13px; flex-basis: 100%; }
  .bulk-bar-count strong { font-size: 18px; }
  .bulk-bar .btn { padding: 8px 12px; font-size: 13px; }
  body.has-bulk-bar .container { padding-bottom: 140px; }

  /* Group filter pills: 折り返し前提でサイズ縮小 */
  .group-pill { padding: 8px 12px; font-size: 13px; }

  /* System grid: 1列 */
  .sys-grid { grid-template-columns: 1fr; gap: 10px; }
  .sys-card { padding: 16px; }
  .sys-icon { font-size: 24px; margin-bottom: 6px; }
  .sys-title { font-size: 15px; }

  /* Campaign show: KPI 横スクロール代わりに 2列折り返し */
  .kpi { padding: 10px 12px; }
  .kpi-num { font-size: 16px; }

  /* Sub-modal: 全画面寄り */
  .sub-modal { width: 96vw; max-width: 96vw; max-height: 92vh; }
  .sub-modal-form { max-height: 92vh; }
  .sub-modal-body { padding: 16px 18px; }
  .sub-modal-body input, .sub-modal-body select { font-size: 17px; padding: 12px 14px; min-height: 52px; }
  .sub-modal-actions { padding: 14px 18px; gap: 8px; flex-wrap: wrap; }
  .sub-modal-actions .btn.big { font-size: 14px; padding: 12px 16px; min-height: 48px; }
  .sub-modal-actions .btn.danger { margin-left: 0; }

  /* Big form/buttons (送信系): モバイルでも視認は維持しつつパディング縮小 */
  .btn.big { font-size: 15px; padding: 10px 16px; }
  .form.big { padding: 14px; }
  .form.big fieldset { padding: 12px; }

  /* Big card */
  .big-card { padding: 16px; }
  .op-actions { gap: 6px; }

  /* Template editor: モバイルではプレビュー高さ縮小、ツールバー折り返し */
  .tpl-pane { padding: 12px; }
  .tpl-rich-toolbar { padding: 6px; }
  .tpl-rich-toolbar button { padding: 5px 8px; font-size: 12px; }
  .tpl-pane-preview { max-height: 480px; }
  .tpl-actions { gap: 6px; }
  .tpl-actions .btn.danger { margin-left: 0; }

  /* Image gallery: 2列 */
  .gal-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
  .gal-thumb { height: 110px; }
  .gal-thumb img { max-height: 110px; }

  /* Log pre: フォントサイズ更に縮小 */
  .log-pre { font-size: 11px; padding: 10px; }

  /* Login box: 上余白詰めて全幅近く */
  .login-box { margin: 40px 16px; padding: 20px; }
}

/* ===========================================================
   Tablet narrow (641-820px) — 中間層調整
   =========================================================== */
@media (min-width: 641px) and (max-width: 820px) {
  .container { padding: 0 16px; }
  .filter input[type=text] { min-width: 160px; }
  .meta-edit-table th { width: 140px; }
}
