:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #d9dde3;
  --text: #1f2933;
  --muted: #5f6b7a;
  --accent: #1d4ed8;
  --accent-soft: #e8efff;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --radius: 10px;
  font-family: "Yu Gothic UI", "Meiryo UI", "Meiryo", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; display: flex; flex-direction: column; }
button, input { font: inherit; }

.topbar { display: flex; align-items: baseline; gap: 16px; padding: 14px 24px 8px; background: var(--panel); }
.topbar h1 { margin: 0; font-size: 22px; letter-spacing: 0.05em; }
.safe-note { margin: 0; color: var(--muted); font-size: 14px; }
.about-open { margin-left: auto; align-self: center; min-height: 36px; padding: 0 14px; font-size: 14px; }
.tagline { margin-top: 18px; color: var(--muted); font-size: 13px; line-height: 1.7; }
.tagline p, .about-lead p { margin: 0; }

.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 8px 24px 14px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.sep { width: 1px; height: 28px; background: var(--line); margin: 0 6px; }

button {
  min-height: 42px; padding: 0 18px; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; color: var(--text); font-size: 15px; cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
button:focus-visible, input:focus-visible, .card:focus-visible { outline: 3px solid #93b4ff; outline-offset: 2px; }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: #1e40af; color: #fff; }
button.big { min-height: 48px; width: 100%; font-size: 16px; }

/* お知らせは画面の下に重ねて出す（出たり消えたりしてもページの位置がずれない） */
.notices {
  position: fixed; left: 24px; bottom: 24px; z-index: 20; width: min(560px, calc(100vw - 48px));
  display: flex; flex-direction: column; gap: 6px;
}
.notice {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 8px;
  background: var(--accent-soft); font-size: 15px; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15); border: 1px solid #c7d6fb;
}
.notice.error { border-color: #f3b8b1; }
.notice.error { background: var(--danger-soft); color: var(--danger); }
.notice span { flex: 1; }
.notice button { min-height: 32px; padding: 0 10px; background: transparent; border: none; color: inherit; font-size: 18px; }

.layout { flex: 1; display: grid; grid-template-columns: 1fr 300px; gap: 20px; padding: 16px 24px 24px; min-height: 0; }
.workbench {
  position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; min-height: 420px; overflow: auto;
}

.empty { position: absolute; inset: 16px; display: flex; }
.empty[hidden] { display: none; }
.empty-box {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  border: 3px dashed #b8c2d0; border-radius: var(--radius); background: #fafbfc; text-align: center;
}
.empty-title { margin: 0; font-size: 26px; font-weight: 700; }
.empty-sub { margin: 0 0 16px; color: var(--muted); font-size: 16px; }
.empty-box button.big { width: auto; padding: 0 32px; }

.legend { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-bottom: 14px; font-size: 14px; }
.legend[hidden] { display: none; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-dot { width: 14px; height: 14px; border-radius: 4px; background: var(--c); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 16px; user-select: none; }

.card {
  position: relative; display: flex; flex-direction: column; border: 2px solid var(--line); border-radius: var(--radius);
  background: #fff; overflow: hidden; cursor: grab; touch-action: none;
}
.card:hover { border-color: #9fb3d1; }
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.card.dragging { opacity: 0.35; }
.card.drop-before::before, .card.drop-after::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 5px; background: var(--accent); border-radius: 3px; z-index: 2;
}
.card.drop-before::before { left: 0; }
.card.drop-after::after { right: 0; }
.card-band {
  padding: 5px 8px; font-size: 13px; color: #fff; background: var(--c);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card-thumb { height: 190px; display: flex; align-items: center; justify-content: center; background: #eef1f5; padding: 8px; }
.card-thumb canvas { max-width: 100%; max-height: 100%; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); background: #fff; }
.card-thumb .loading { color: var(--muted); font-size: 13px; }
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 6px 8px; font-size: 13px; color: var(--muted); }
.card-num { font-size: 18px; font-weight: 700; color: var(--text); }
.card-tools { display: flex; gap: 4px; padding: 0 8px 8px; }
.card-tools button { flex: 1; min-height: 34px; padding: 0; font-size: 18px; }
.card-check {
  position: absolute; top: 34px; right: 8px; width: 28px; height: 28px; border-radius: 50%;
  background: var(--accent); color: #fff; font-weight: 700; display: none; align-items: center; justify-content: center;
}
.card.selected .card-check { display: flex; }

.savepanel {
  display: flex; flex-direction: column; gap: 12px; align-self: start; position: sticky; top: 16px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px;
}
.savepanel h2 { margin: 0; font-size: 18px; }
.status { margin: 0; color: var(--muted); font-size: 14px; min-height: 20px; }
.status.busy { color: var(--accent); font-weight: 700; }
.group { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--line); border-radius: 8px; padding: 12px; margin: 0; }
.group legend, .group > label { font-size: 14px; color: var(--muted); padding: 0 4px; }
.group input[type="text"] { min-height: 42px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 16px; }
.radio { display: flex; align-items: center; gap: 8px; font-size: 15px; cursor: pointer; }
.radio input { width: 18px; height: 18px; }
.hint { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.6; }

.drop-overlay {
  position: fixed; inset: 0; background: rgba(29, 78, 216, 0.12); border: 4px dashed var(--accent);
  display: flex; align-items: center; justify-content: center; z-index: 10; pointer-events: none;
}
.drop-overlay[hidden] { display: none; }
.drop-overlay p { background: #fff; padding: 16px 28px; border-radius: var(--radius); font-size: 22px; font-weight: 700; color: var(--accent); }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .savepanel { position: static; }
}

/* ---------------------------------------------------------------- 編集画面（書き込みと印鑑） */
.card-tools button[data-act="edit"] { font-size: 17px; }
body.editing { overflow: hidden; }
body.editing .notices { bottom: 96px; }
.editor {
  position: fixed; inset: 0; z-index: 15; display: grid; grid-template-rows: auto 1fr auto; background: var(--bg);
}
.editor[hidden] { display: none; }
.ico { display: inline-block; min-width: 1.2em; margin-right: 6px; font-weight: 700; text-align: center; }
.ico-marker { color: #e0c200; }
.ico-white { color: #fff; text-shadow: 0 0 1px #556, 0 0 1px #556; }
.ed-top {
  display: flex; flex-direction: column; gap: 8px; padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.ed-tools, .ed-options { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.ed-tools button { padding: 0 14px; }
.editor button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 700; }
.opt-label { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.ed-options select { min-height: 38px; font: inherit; font-size: 15px; padding: 0 8px; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.swatch { padding: 0 12px; min-height: 38px; }
.dot { display: inline-block; width: 14px; height: 14px; border-radius: 50%; margin-right: 6px; vertical-align: -2px; }
.dot.black { background: #111; }
.dot.red { background: #d92020; }
.dot.blue { background: #1d4ed8; }

.ed-body { display: grid; grid-template-columns: 1fr 280px; min-height: 0; }
.ed-stage { overflow: auto; padding: 24px; background: #e4e7ec; display: flex; }
.ed-page { position: relative; margin: auto; flex: none; background: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25); }
.ed-page canvas#ed-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.ed-layer { position: absolute; inset: 0; touch-action: none; cursor: crosshair; }
.ed-layer[data-tool="select"] { cursor: default; }
.ed-layer[data-tool="text"] { cursor: text; }
.ed-layer[data-tool="stamp"] { cursor: copy; }
.ed-anns { position: absolute; inset: 0; }
.ed-ann { position: absolute; }
.ed-ann canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.ed-layer[data-tool="select"] .ed-ann { cursor: move; }
.ed-ann.selected { outline: 2px dashed var(--accent); outline-offset: 2px; }
.ed-ann.ed-preview { pointer-events: none; }
.ed-ann.hidden-while-editing { visibility: hidden; }
.ed-handle {
  position: absolute; width: 14px; height: 14px; background: #fff; border: 2px solid var(--accent); border-radius: 3px; z-index: 2;
}
.ed-handle.nw { left: -9px; top: -9px; cursor: nwse-resize; }
.ed-handle.ne { right: -9px; top: -9px; cursor: nesw-resize; }
.ed-handle.sw { left: -9px; bottom: -9px; cursor: nesw-resize; }
.ed-handle.se { right: -9px; bottom: -9px; cursor: nwse-resize; }
.ed-ann-del {
  position: absolute; top: -34px; right: -10px; min-height: 26px; width: 26px; padding: 0; border-radius: 50%;
  background: var(--danger); border-color: var(--danger); color: #fff; font-size: 16px; line-height: 1; z-index: 3;
}
.ed-ann-del:hover:not(:disabled) { background: #8f1c13; color: #fff; border-color: #8f1c13; }
.ed-textarea {
  position: absolute; margin: 0; border: 1px dashed var(--accent); background: rgba(255, 255, 255, 0.6); outline: none;
  resize: none; overflow: hidden; white-space: pre; box-sizing: content-box; z-index: 4;
}

.ed-side {
  display: flex; flex-direction: column; gap: 10px; padding: 16px; background: var(--panel); border-left: 1px solid var(--line); overflow: auto;
}
.ed-side h2 { margin: 0; font-size: 18px; }
.stamp-list { display: flex; flex-direction: column; gap: 6px; }
.stamp-item { display: flex; align-items: stretch; gap: 4px; }
.stamp-pick { flex: 1; display: flex; align-items: center; gap: 10px; padding: 4px 10px; min-height: 60px; text-align: left; }
.stamp-thumb { width: 52px; height: 52px; flex: none; }
.stamp-label { font-size: 14px; line-height: 1.4; }
.stamp-del { min-height: 0; width: 36px; padding: 0; font-size: 18px; color: var(--muted); }
.stamp-make { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; }
.stamp-make button { text-align: left; }
.ico-round { color: #d0312d; }

.ed-bottom {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 10px 20px; background: var(--panel); border-top: 1px solid var(--line);
}
.ed-nav, .ed-zoom { display: flex; align-items: center; gap: 6px; }
.ed-pageno { min-width: 92px; text-align: center; font-weight: 700; }
.ed-zoom-label { min-width: 52px; text-align: center; color: var(--muted); }
.ed-zoom button { padding: 0 12px; }
.ed-hint { flex: 1; min-width: 200px; margin: 0; color: var(--muted); font-size: 14px; }
#ed-save { padding: 0 22px; }

.stamp-dialog { border: none; border-radius: var(--radius); padding: 22px; width: min(640px, calc(100vw - 32px)); box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3); }
.stamp-dialog::backdrop { background: rgba(15, 23, 42, 0.45); }
.stamp-dialog h2 { margin: 0 0 14px; font-size: 20px; }
.sd-body { display: grid; grid-template-columns: 1fr 210px; gap: 18px; }
.sd-fields { display: flex; flex-direction: column; gap: 12px; }
.sd-fields label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.sd-fields label[hidden] { display: none; }
.sd-fields label.radio { flex-direction: row; color: var(--text); }
.sd-fields input[type="text"], .sd-fields input[type="number"], .sd-fields input[type="date"] {
  min-height: 42px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; font: inherit; font-size: 16px; color: var(--text);
}
.sd-range { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.sd-range input { flex: 1; }
.sd-preview { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.sd-canvas {
  width: 200px; height: 200px; border: 1px solid var(--line); border-radius: 8px;
  background-color: #fff; background-image: conic-gradient(#eef0f3 25%, #fff 0 50%, #eef0f3 0 75%, #fff 0); background-size: 16px 16px;
}
.sd-error { min-height: 20px; margin: 10px 0 0; color: var(--danger); font-size: 14px; }
.sd-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.about-lead { margin: 0 0 14px; color: var(--accent); font-weight: 700; line-height: 1.7; }
.about-body { font-size: 15px; line-height: 1.8; }
.about-body p { margin: 0 0 12px; }
.about-body h3 { margin: 16px 0 6px; font-size: 15px; }
.about-warranty { padding: 10px 12px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-weight: 700; }

@media (max-width: 860px) {
  .ed-body { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .ed-side { border-left: none; border-top: 1px solid var(--line); max-height: 40vh; }
  .sd-body { grid-template-columns: 1fr; }
}
