:root {
  --pw: 100mm;
  --ph: 148mm;
  --bg: #e9eef2;
  --panel: #fbfcfd;
  --ink: #23292f;
  --muted: #56616c;
  --line: #cfd8e0;
  --accent: #1f5a8a;
  --accent-soft: #e2edf6;
  --focus: #8db6d8;
  --danger: #b3262d;
  --warn-bg: #fff4d6;
  --warn-line: #e3c46a;
}

* { box-sizing: border-box; }
html { overflow: hidden; }
html, body {
  margin: 0;
  height: 100%;
  font-family: "Yu Gothic UI", "Meiryo UI", "Hiragino Sans", sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
}
body { display: flex; overflow: hidden; }

/* ---------- 入力パネル ---------- */
.panel {
  width: clamp(400px, 30vw, 480px);
  flex-shrink: 0;
  height: 100%;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 14px 18px 32px;
}
@media (max-width: 1100px) { .panel { width: 350px; padding: 12px 14px 24px; } }

.app-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.app-head .link { margin-left: auto; }
.ver { color: var(--muted); font-size: 12px; }
h1 { margin: 0; font-size: 22px; letter-spacing: .08em; }
h2 { margin: 0 0 10px; font-size: 16px; color: var(--accent); }

.ptabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin-bottom: 12px; border-bottom: 2px solid var(--accent); }
.ptabs button { border-radius: 6px 6px 0 0; border-bottom: none; min-height: 40px; font-size: 15px; font-weight: bold; background: #f1f4f7; }
.ptabs button.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.pane.hidden { display: none; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; min-width: 0; }
.field > span { font-size: 13px; color: var(--muted); }
.field em { font-style: normal; color: var(--accent); }
.field .row { margin: 0; flex-wrap: nowrap; }
input[type=text], input[type=search], input[type=number], select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 7px 9px;
  font-size: 15px;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
  min-height: 36px;
}
input::placeholder { color: #a7b0b8; }
input[type=range] { width: 100%; accent-color: var(--accent); }
input:focus-visible, select:focus-visible, button:focus-visible, summary:focus-visible, .seg input:focus-visible + span {
  outline: 3px solid var(--focus); outline-offset: 1px;
}
input[type=radio], input[type=checkbox] { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); flex-shrink: 0; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.grid2 .field { justify-content: flex-end; }
.row { display: flex; gap: 6px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.row > input, .row > select { flex: 1; min-width: 0; }
.row.tight button { padding: 5px 9px; font-size: 13px; }
.row.end { justify-content: space-between; margin: 14px 0 0; }
.note { font-size: 13px; color: var(--muted); margin: 0 0 8px; line-height: 1.6; }
.note:empty { display: none; }
.check { display: flex; align-items: center; gap: 8px; margin: 4px 0 10px; cursor: pointer; }

button {
  font-family: inherit; font-size: 14px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 5px; padding: 6px 12px; cursor: pointer; min-height: 36px;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.link { border: none; background: none; color: var(--muted); text-decoration: underline; padding: 4px 0; font-size: 13px; min-height: 0; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: bold; }
button.primary:hover:not(:disabled) { filter: brightness(1.1); }
button.danger { color: var(--danger); border-color: #e3b3b6; margin-left: auto; }
button small { font-weight: normal; opacity: .85; font-size: 12px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 10px; }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; margin: 0; pointer-events: none; }
.seg span { display: flex; align-items: center; justify-content: center; min-height: 40px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.seg input:checked + span { border: 2px solid var(--accent); background: var(--accent-soft); font-weight: bold; }
.seg.hidden, #customSize.hidden, #senderZipRow.hidden { display: none; }

/* 住所録 */
.clist { list-style: none; margin: 0 0 12px; padding: 0; max-height: 34vh; overflow-y: auto; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.clist:empty::after { content: '宛先はまだありません。「＋ 宛先を追加」から登録してください。'; display: block; padding: 14px; color: var(--muted); font-size: 13px; }
.clist li { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-bottom: 1px solid #edf1f4; cursor: pointer; }
.clist li:last-child { border-bottom: none; }
.clist li.sel { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.clist li .who { flex: 1; min-width: 0; }
.clist li .nm { font-weight: bold; }
.clist li .ad { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.clist li .grp { font-size: 11px; color: var(--accent); border: 1px solid var(--accent-soft); border-radius: 8px; padding: 0 6px; white-space: nowrap; }
.clist li .bad { color: var(--danger); font-size: 11px; white-space: nowrap; }
.editor, .sender-editor { border: 1px solid var(--line); border-radius: 8px; padding: 12px; background: #fff; margin-bottom: 10px; }
.editor.hidden { display: none; }
.joint { display: grid; grid-template-columns: 1fr 1fr 90px 32px; gap: 6px; align-items: end; margin-bottom: 6px; }
.joint button { min-height: 36px; padding: 0; }
.zipf input { max-width: 12em; }
.backup { justify-content: space-between; }

details.adv { border-top: 1px solid var(--line); padding: 4px 0; margin-top: 6px; }
details.adv > summary { cursor: pointer; list-style: none; padding: 10px 0; font-size: 16px; font-weight: bold; display: flex; gap: 8px; }
details.adv > summary::-webkit-details-marker { display: none; }
details.adv > summary::before { content: '▸'; color: var(--accent); transition: transform .15s; }
details.adv[open] > summary::before { transform: rotate(90deg); }
#testPrintBtn { width: 100%; margin-bottom: 10px; min-height: 40px; }

/* ---------- プレビュー ---------- */
.preview { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; overflow: hidden; padding: 12px 24px 0; }
.warnings { align-self: stretch; display: flex; flex-direction: column; gap: 4px; margin-bottom: 6px; }
.warnings:empty { display: none; }
.warn-item { display: flex; align-items: center; gap: 10px; background: var(--warn-bg); border: 1px solid var(--warn-line); border-radius: 5px; padding: 6px 10px; font-size: 14px; }
.warn-item span { flex: 1; }
.pnav { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.pnav #navInfo { min-width: 12em; text-align: center; font-weight: bold; }
.side-seg { display: inline-flex; gap: 4px; margin-left: 12px; }
.side-seg.hidden { display: none; }
.side-seg label { position: relative; }
.side-seg input { position: absolute; inset: 0; opacity: 0; margin: 0; pointer-events: none; width: 100%; height: 100%; }
.side-seg span { display: block; padding: 5px 12px; border: 1px solid var(--line); border-radius: 14px; background: #fff; cursor: pointer; font-size: 13px; }
.side-seg input:checked + span { background: var(--accent); color: #fff; border-color: var(--accent); }
.paper-holder { position: relative; flex-shrink: 0; margin: auto 0; }
.paper {
  position: absolute; top: 0; left: 0;
  width: var(--pw); height: var(--ph);
  background: #fff; box-shadow: 0 6px 24px rgba(0,0,0,.18);
  transform-origin: 0 0; overflow: hidden;
}
.content { position: absolute; inset: 0; color: #000; }
.content .col { position: absolute; writing-mode: vertical-rl; text-orientation: upright; line-height: 1; white-space: nowrap; }
.content .col .tcy { text-combine-upright: all; }
.content .hl { position: absolute; white-space: nowrap; line-height: 1.25; }
.content .hblock { position: absolute; transform: translateX(-50%); white-space: nowrap; line-height: 1.4; }
.content .zipd, .content .zipt { letter-spacing: .02em; }
.guides { position: absolute; inset: 0; pointer-events: none; }
.guides.hidden { display: none; }
.guides .g { position: absolute; }
.guides .zipbox { border: 0.3mm solid rgba(214, 64, 64, .75); }
.guides .fold { border-left: 0.3mm dashed rgba(120,120,120,.6); }
.guides .seam { border-left: 0.3mm dashed rgba(120,120,120,.45); }
.test-sheet { position: absolute; inset: 0; display: none; }
.test-sheet svg { width: 100%; height: 100%; display: block; }
html.testing .test-sheet { display: block; }
html.testing .content { display: none; }

.bottom-bar { align-self: stretch; display: flex; align-items: center; flex-wrap: wrap; gap: 8px 14px; padding: 12px 0 16px; }
.bar-group { display: flex; align-items: center; gap: 6px; }
.bar-group.end { margin-left: auto; gap: 8px; }
.bottom-bar button { padding: 8px 14px; font-size: 15px; min-height: 42px; }
.bottom-bar .primary { padding: 8px 24px; font-size: 16px; min-width: 150px; }
#printBack.hidden { display: none; }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%) translateY(10px);
  background: #23292f; color: #fff; padding: 10px 18px; border-radius: 6px; font-size: 15px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; max-width: 80vw;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: #a3222a; }
.toast small { display: block; opacity: .8; font-size: 12px; margin-top: 2px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 30; }
.modal.hidden { display: none; }
.modal-body { background: var(--panel); border-radius: 8px; padding: 20px 24px; width: min(540px, 92vw); box-shadow: 0 10px 40px rgba(0,0,0,.25); line-height: 1.7; max-height: 90vh; overflow-y: auto; }
.modal-body ol { padding-left: 1.4em; margin: 0 0 8px; }
.big-steps li { font-size: 16px; margin-bottom: 8px; }
.pdf-actions { display: flex; flex-direction: column; gap: 10px; margin: 12px 0; }
.pdf-actions button, .pdf-actions .btn { min-height: 48px; font-size: 17px; }
.btn { display: flex; align-items: center; justify-content: center; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--ink); text-decoration: none; }
.pdf-actions [hidden] { display: none; }

/* 郵便番号から住所 */
.zip-suggest:empty { display: none; }
.zip-suggest { margin: -4px 0 10px; padding: 8px 10px; border: 1px solid var(--accent-soft); border-left: 4px solid var(--accent); border-radius: 6px; background: #f6fafd; }
.zip-suggest .note { margin: 0 0 6px; }
.zip-suggest button { margin: 0 6px 6px 0; }
.zip-choices { display: flex; flex-direction: column; gap: 4px; max-height: 180px; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-right: 4px; }
.zip-choices button { margin: 0; text-align: left; font-size: 14px; }
.collapse-btn { display: none; }

/* ---------- ウェブ版 ---------- */
html.web .printer-only, html.web #dialogPrintBtn, html.web #pdfBtn { display: none !important; }

/* ---------- スマートフォン ---------- */
@media (max-width: 820px) {
  html, body { overflow: visible; height: auto; }
  body { flex-direction: column; font-size: 16px; padding-bottom: calc(84px + env(safe-area-inset-bottom)); -webkit-text-size-adjust: 100%; }
  .panel { width: auto; height: auto; overflow: visible; border-right: none; padding: 12px max(14px, env(safe-area-inset-left)) 24px max(14px, env(safe-area-inset-right)); }
  .preview { order: -1; position: sticky; top: 0; z-index: 5; height: auto; overflow: visible; padding: calc(6px + env(safe-area-inset-top)) 10px 8px; background: var(--bg); box-shadow: 0 4px 12px rgba(0,0,0,.10); }
  .paper-holder { margin: 0 auto; }
  .pnav { margin-bottom: 4px; gap: 6px; }
  .pnav button { min-height: 36px; padding: 2px 10px; }
  .bottom-bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; background: var(--panel); border-top: 1px solid var(--line); padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); flex-wrap: nowrap; gap: 6px; }
  .bar-group.end { flex: 1; }
  .bottom-bar button { min-height: 50px; padding: 4px 10px; font-size: 13px; }
  .bottom-bar .primary { flex: 1; font-size: 16px; min-width: 0; }
  input[type=text], input[type=search], input[type=number], select { font-size: 16px; min-height: 44px; }
  button { min-height: 44px; }
  .clist { max-height: 40vh; }
  .clist li { padding: 10px; }
  /* 入力欄を広く：1列に並べ、連名は2段に */
  .editor .grid2, .sender-editor .grid2, #pane-paper .grid2 { grid-template-columns: 1fr; }
  .editor, .sender-editor { padding: 12px 10px; }
  .joint { grid-template-columns: 1fr 1fr; border-top: 1px dashed var(--line); padding-top: 8px; }
  .joint .field:nth-child(3) { grid-column: 1; }
  .joint button { grid-column: 2; align-self: end; min-height: 44px; }
  .zipf input { max-width: none; }
  /* プレビュー：たたむボタンと、たたんだときの表示 */
  .collapse-btn { display: inline-block; min-height: 34px; padding: 2px 10px; font-size: 13px; }
  .pnav { flex-wrap: wrap; justify-content: center; }
  .pnav #navInfo { min-width: 0; flex: 1; }
  .side-seg { margin-left: 0; }
  body.preview-collapsed .paper-holder, body.preview-collapsed .warnings, body.typing .warnings { display: none; }
  .toast { bottom: calc(100px + env(safe-area-inset-bottom)) !important; max-width: 92vw; text-align: center; }
}

/* ---------- 印刷 ---------- */
@media print {
  html, body { width: var(--pw); height: var(--ph); overflow: hidden; background: #fff; }
  html[data-orient="90"], html[data-orient="90"] body, html[data-orient="270"], html[data-orient="270"] body { width: var(--ph); height: var(--pw); }
  .panel, .warnings, .pnav, .bottom-bar, .modal, .guides, .toast { display: none !important; }
  .preview { padding: 0; display: block; }
  .paper-holder { width: auto !important; height: auto !important; margin: 0; }
  .paper { transform: none !important; box-shadow: none; }
  html[data-orient="180"] .paper { transform: translate(var(--pw), var(--ph)) rotate(180deg) !important; }
  html[data-orient="90"] .paper { transform: translateX(var(--ph)) rotate(90deg) !important; }
  html[data-orient="270"] .paper { transform: translateY(var(--pw)) rotate(-90deg) !important; }
}
