@layer base {
  *, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }
}

body {
  background: #ddd;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* ── SEO Header ── */
.seo-header {
  max-width: 960px;
  margin: 24px auto 16px;
  padding: 0 16px;
  line-height: 1.8;
}

.seo-header h1 {
  font-size: 1.8rem;
  margin: 0 0 10px;
  line-height: 1.4;
}

.seo-lead {
  margin: 0 0 12px;
  color: #333;
}

.tool-note {
  background: #f7faf7;
  border-left: 4px solid #7bae7f;
  padding: 12px 14px;
  margin: 14px 0;
  font-size: 0.95rem;
}

/* ── Tool Panel ── */
.tool-panel {
  position: sticky;
  top: 12px;
  z-index: 100;
  max-width: 680px;
  margin: 20px auto 16px;
  padding: 14px 18px 16px;
  background: #f8faf8;
  color: #333;
  border: 1px solid #d8e4d8;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
}

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

.tool-panel.collapsed .tool-panel-head {
  margin-bottom: 0;
}

.tool-panel.collapsed .tool-panel-row,
.tool-panel.collapsed .tool-button-row {
  display: none;
}

.panel-toggle-btn {
  margin-left: auto;
  padding: 2px 8px;
  border: 1px solid #c8d8c8;
  border-radius: 4px;
  background: transparent;
  color: #777;
  font-size: 11px;
  cursor: pointer;
  line-height: 1;
  flex-shrink: 0;
}

.panel-toggle-btn:hover {
  background: #e8f0e8;
  color: #444;
}

.toolbar-title {
  font-size: 16px;
  font-weight: 700;
  color: #333;
}

.char-count {
  min-width: 72px;
  padding: 5px 10px;
  background: #fff;
  border: 1px solid #d0ddd0;
  border-radius: 999px;
  color: #333;
  font-size: 13px;
  text-align: center;
  white-space: nowrap;
}

.remaining-count {
  font-size: 13px;
  color: #666;
  white-space: nowrap;
}

.tool-panel-row {
  margin-bottom: 12px;
}

.size-select-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #333;
}

#gridSizeSelect {
  flex: 1;
  max-width: 360px;
  padding: 7px 9px;
  border: 1px solid #b8c8b8;
  border-radius: 6px;
  background: #fff;
  color: #333;
  font-size: 13px;
  cursor: pointer;
}

#pasteTextBtn {
  background: #eaf5ea;
  border-color: #7bae7f;
  font-weight: 700;
}

#pasteTextBtn:hover {
  background: #d4edda;
}

.tool-button-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tool-button-row button {
  padding: 8px 10px;
  border: 1px solid #9db89d;
  border-radius: 6px;
  background: #fff;
  color: #333;
  font-size: 13px;
  cursor: pointer;
}

.tool-button-row button:hover {
  background: #eef7ee;
}

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

#pdfBtn {
  background: #eaf5ea;
  border-color: #7bae7f;
  font-weight: 700;
}

#clearBtn {
  margin-left: auto;
  background: #fef2f2;
  border-color: #fca5a5;
  color: #dc2626;
}

#clearBtn:hover {
  background: #fee2e2;
}

#pdfBtn:hover:not(:disabled) {
  background: #dceddc;
}

/* ── Help ── */
#helpDetails {
  max-width: 680px;
  margin: 0 auto 16px;
  background: #f0f4f0;
  border: 1px solid #c8d8c8;
  border-radius: 8px;
  font-size: 13px;
  color: #333;
  overflow: hidden;
}

#helpDetails summary {
  padding: 10px 14px;
  cursor: pointer;
  font-weight: bold;
  color: #555;
  list-style: none;
  display: flex;
  align-items: center;
  user-select: none;
}

#helpDetails summary::-webkit-details-marker {
  display: none;
}

#helpDetails summary::after {
  content: '▼';
  margin-left: auto;
  padding: 2px 8px;
  border: 1px solid #c8d8c8;
  border-radius: 4px;
  background: transparent;
  color: #777;
  font-size: 11px;
  line-height: 1;
}

#helpDetails[open] summary::after {
  content: '▲';
}

.help-body {
  padding: 4px 14px 16px;
}

.help-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 8px 24px;
}

.help-item {
  display: flex;
  gap: 6px;
}

.help-item dt {
  font-weight: bold;
  white-space: nowrap;
  min-width: 80px;
  color: #444;
}

.help-item dd {
  color: #555;
  line-height: 1.5;
}

/* ── Pages container ── */
#pagesContainer {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 40px 20px;
  gap: 48px;
  cursor: text;
  min-height: calc(100vh - 50px);
  background: #ddd;
}

/* ── ページ（サイズはJSでinline設定） ── */
.genkou-page {
  background: #fff;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  position: relative;
  flex-shrink: 0;
  overflow: hidden;
  margin-inline: auto;
}

/* ── ページ番号 ── */
.page-number {
  position: absolute;
  bottom: 8mm;
  left: 50%;
  transform: translateX(-50%);
  font-size: 10px;
  color: #999;
  font-family: sans-serif;
}

/* ── 原稿用紙グリッド ── */
.genkou-grid {
  position: absolute;
  display: block;
  isolation: isolate;
}

/* SVG罫線 */
.genkou-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
  overflow: visible;
}

/* 入力セル */
.genkou-cell {
  position: absolute;
  z-index: 1;
  box-sizing: border-box;
  border: none;
  background: transparent;

  font-family: 'Yu Mincho', 'Hiragino Mincho ProN', 'Noto Serif JP', serif;
  writing-mode: vertical-rl;
  text-orientation: upright;

  cursor: text;
  user-select: none;
}

.cell-char {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  writing-mode: horizontal-tb;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* カーソル: セル上部に横線 */
.genkou-cell.cursor::before {
  content: '';
  position: absolute;
  top: 0.5mm;
  left: 12%;
  width: 76%;
  height: 2px;
  writing-mode: horizontal-tb;
  background: #4f46e5;
  animation: blink-cursor 1s step-end infinite;
}

@keyframes blink-cursor {
  0%, 100% {
    opacity: 1;
  }

  50% {
    opacity: 0;
  }
}

/* ── Hidden textarea ── */
#hiddenInput {
  position: fixed;
  top: 50%;
  left: 50%;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
  resize: none;
  border: none;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-overlay.hidden {
  display: none;
}

.modal-box {
  background: #fff;
  border-radius: 8px;
  width: 520px;
  max-width: 90vw;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.modal-title {
  font-size: 16px;
  font-weight: bold;
}

.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: #666;
  line-height: 1;
  padding: 0 4px;
}

.modal-list {
  overflow-y: auto;
  flex: 1;
}

.modal-item {
  padding: 12px 20px;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.modal-item:last-child {
  border-bottom: none;
}

.modal-item-preview {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

.modal-item-meta {
  font-size: 11px;
  color: #888;
  margin-top: 3px;
}

.modal-item-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.empty-message {
  padding: 40px;
  text-align: center;
  color: #999;
  font-size: 14px;
}

.btn {
  padding: 6px 14px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  font-size: 13px;
}

.btn:hover {
  background: #f5f5f5;
}

.btn-load {
  background: #4f46e5;
  color: #fff;
  border-color: #4f46e5;
}

.btn-load:hover {
  background: #4338ca;
}

.btn-delete {
  background: #ef4444;
  color: #fff;
  border-color: #ef4444;
}

.btn-delete:hover {
  background: #dc2626;
}

/* ── Paste modal ── */
.paste-modal-box {
  width: 560px;
}

.paste-modal-body {
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.paste-modal-note {
  font-size: 13px;
  color: #555;
  margin: 0;
}

#bulkTextInput {
  width: 100%;
  height: 220px;
  padding: 10px 12px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1.7;
  resize: vertical;
  font-family: inherit;
}

.paste-modal-warning {
  font-size: 12px;
  color: #e53e3e;
  margin: 0;
}

.btn-apply {
  background: #4a8f4f;
  color: #fff;
  border-color: #4a8f4f;
}

.btn-apply:hover {
  background: #3d7a42;
}

/* ── SEO Content ── */
.seo-content {
  max-width: 960px;
  margin: 32px auto;
  padding: 0 16px 40px;
  line-height: 1.8;
}

.seo-content h2 {
  margin-top: 32px;
  font-size: 1.4rem;
  border-bottom: 2px solid #e5e5e5;
  padding-bottom: 6px;
}

.seo-content h3 {
  margin-top: 24px;
  font-size: 1.15rem;
}

.seo-content p {
  margin: 0 0 12px;
}

.seo-content ul {
  padding-left: 1.4em;
  margin: 0 0 12px;
}

.related-links {
  padding: 8px 14px;
  border-left: 3px solid #9db89d;
  margin: 0 0 12px;
  list-style: none;
}

.related-links li {
  margin-bottom: 4px;
}

.related-links a::before {
  content: '→ ';
  color: #7bae7f;
}

/* ── Responsive ── */
@media (max-width: 700px) {
  .seo-header {
    margin: 18px auto 12px;
  }

  .seo-header h1 {
    font-size: 1.45rem;
  }

  .tool-panel {
    top: 8px;
    margin: 14px 12px 14px;
    padding: 14px;
  }

  .tool-panel-head {
    align-items: center;
  }

  .toolbar-title {
    font-size: 15px;
  }

  .size-select-label {
    display: block;
  }

  #gridSizeSelect {
    width: 100%;
    max-width: none;
    margin-top: 6px;
  }

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

  #helpDetails {
    margin: 0 12px 14px;
  }

  #pagesContainer {
    padding: 28px 12px;
    gap: 36px;
  }
}

/* ── Print ── */
/* @page サイズはJSで動的注入するためここでは指定しない */
@media print {
  #header,
  #footer,
  .seo-header,
  #toolbar,
  #helpDetails,
  .seo-content {
    display: none !important;
  }

  body {
    background: #fff;
    margin: 0;
    padding: 0;
  }

  main {
    display: block;
    margin: 0;
    padding: 0;
  }

  #pagesContainer {
    padding: 0;
    gap: 0;
    background: #fff;
    min-height: 0;
  }

  .genkou-page {
    box-shadow: none;
    page-break-after: always;
  }

  .genkou-page:last-child {
    page-break-after: avoid;
  }

  .genkou-cell.cursor::before {
    display: none;
  }
}