/* =========================================================
   編集画面の見た目（印刷には出ない）
   ========================================================= */

:root {
  --bg: #f3f1ec;
  --panel: #ffffff;
  --line: #ddd8cf;
  --text: #2b2b2b;
  --muted: #7b756b;
  --accent: #8a5a2b;       /* ボタン・スイッチの色 */
  --accent-weak: #f4ebe1;
  --danger: #b23b3b;
  --warn-bg: #fff3d6;
  --warn-text: #7a5200;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  font-size: 14px;
}

button, .btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
button:hover, .btn:hover { background: var(--accent-weak); }
button:disabled { opacity: .35; cursor: default; background: #fff; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }

input[type="text"], input:not([type]), textarea, select {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 5px;
  font: inherit;
  background: #fff;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { resize: none; line-height: 1.45; display: block; }
.muted { color: var(--muted); }

/* ---------- 上部のツールバー ---------- */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.toolbar h1 { margin: 0; font-size: 18px; }
.toolbar h1 small { font-size: 12px; font-weight: 400; color: var(--muted); margin-left: 6px; }
.toolbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.save-status { color: var(--muted); font-size: 12px; min-width: 6em; text-align: right; }

/* ---------- 左右2列のレイアウト ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(560px, 1fr) auto;
  gap: 20px;
  padding: 20px;
  align-items: start;
}

.editor { display: flex; flex-direction: column; gap: 14px; }

.preview-area {
  position: sticky;
  top: 72px;
  max-height: calc(100vh - 92px);
  overflow-y: auto;
  padding: 0 8px 8px 4px;
}
.preview-scaler {
  zoom: 0.62;              /* 画面上では縮小表示。印刷時は等倍 */
}
.preview-scaler .sheet { box-shadow: 0 2px 12px rgba(0,0,0,.15); margin-bottom: 28px; cursor: pointer; }
.preview-scaler .sheet.is-current { outline: 5px solid var(--accent); outline-offset: 0; }
.preview-scaler .sheet.is-over { outline: 5px solid var(--danger); }

/* 画面が狭いときはプレビューをさらに縮小 */
@media screen and (max-width: 1250px) {
  .layout { grid-template-columns: minmax(480px, 1fr) auto; }
  .preview-scaler { zoom: 0.45; }
}

.preview-status {
  min-height: 1.6em;
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--muted);
}
.preview-status.is-warn {
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-weight: 600;
}

/* ---------- パネル（ページ設定・各セクション） ---------- */
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px;
}
.panel-title { margin: 0 0 10px; font-size: 15px; }

.field { display: grid; grid-template-columns: 11em 1fr; align-items: center; gap: 8px; margin-bottom: 8px; }
.field span { color: var(--muted); }

.hint { margin: 6px 0 0; color: var(--muted); font-size: 12.5px; line-height: 1.7; }
.hint code { background: var(--accent-weak); padding: 1px 5px; border-radius: 4px; color: var(--text); }

/* セクションの見出し行 */
.section-head { display: flex; gap: 6px; align-items: center; margin-bottom: 10px; }
.section-title-input { font-weight: 700; font-size: 15px; }
.section-head .section-title-input { flex: 1 1 auto; min-width: 0; }
.section-head .section-en-input { flex: 0 0 13em; width: 13em; font-size: 13px; }

button.icon { padding: 4px 8px; font-size: 12px; white-space: nowrap; }
button.danger { color: var(--danger); }
button.danger:hover { background: #fbe9e9; }

/* 品目の列：型ごとに列が違うので、列の幅は app.js が行ごとに指定する */
.item-header, .item-row {
  display: grid;
  gap: 6px;
  align-items: center;
}
.item-header .h-price0, .item-header .h-price1 { text-align: right; }
.item-header { font-size: 12px; color: var(--muted); padding: 0 0 4px; }

.item-list { min-height: 12px; border-radius: 6px; }

.item-row {
  position: relative;
  padding: 4px 0;
  border-radius: 6px;
}
.item-row.is-hidden input:not([type="checkbox"]) { color: #aaa; background: #f6f5f2; text-decoration: line-through; }

/* ドラッグ中：つかんだ行の「入る場所」を、色付きの点線枠で示す（中身は薄く） */
.item-row.dragging {
  background: var(--accent-weak);
  outline: 2px dashed var(--accent);
  outline-offset: -1px;
}
.item-row.dragging > * { opacity: .25; }
.handle:active { cursor: grabbing; }

.handle {
  cursor: grab;
  color: #8f877b;
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  user-select: none;
}
.handle:hover { color: var(--accent); }

/* セクションのつまみ（見出しの左端） */
.sec-handle { flex: 0 0 auto; padding: 0 2px; font-size: 20px; }
/* セクションをドラッグ中：全セクションを見出しの行だけに縮める */
.editor.sorting-sections .section-panel > :not(.section-head) { display: none; }
.editor.sorting-sections .section-panel { padding-top: 8px; padding-bottom: 8px; }
.editor.sorting-sections .section-head { margin-bottom: 0; }
.section-panel.dragging {
  background: var(--accent-weak);
  outline: 2px dashed var(--accent);
  outline-offset: -1px;
}
.section-panel.dragging > * { opacity: .25; }

input.price { text-align: right; }

/* ふりがな */
button.ruby-btn { justify-content: center; padding: 4px 4px; font-size: 12px; }
.ruby-list {
  grid-column: 3 / -1;           /* 品名の下に、横いっぱいで並べる */
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  padding: 2px 0 4px;
}
.ruby-pair { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; }
.ruby-pair input { padding: 3px 6px; font-size: 12.5px; }
.ruby-pair .ruby-base { width: 6em; }
.ruby-pair .ruby-text { width: 8em; }
.ruby-arrow { color: var(--muted); }
.ruby-caption { color: var(--muted); font-size: 12px; }
.ruby-warn { display: none; color: var(--danger); font-size: 12px; }
.ruby-pair.is-missing .ruby-base { border-color: var(--danger); background: #fdf0f0; }
.ruby-pair.is-missing .ruby-warn { display: inline; }
input.is-invalid { border-color: var(--danger); background: #fdf0f0; }
.save-status.is-error { color: var(--danger); font-weight: 600; min-width: 0; }
.price-cell { display: flex; flex-direction: column; gap: 1px; }
.tax-ex { color: var(--muted); font-size: 11.5px; text-align: right; white-space: nowrap; min-height: 1.2em; }

/* 表示／非表示スイッチ */
.switch { position: relative; display: inline-block; width: 38px; height: 22px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch span {
  position: absolute; inset: 0;
  background: #cfc9bf; border-radius: 22px; transition: background .15s;
}
.switch span::before {
  content: ""; position: absolute; left: 3px; top: 3px;
  width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::before { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.add-item { margin-top: 8px; }

/* 型C：生産者・産地（品名の下） */
.item-sub { display: flex; gap: 10px; padding: 0 0 4px; }
.item-sub label { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 12.5px; flex: 1; min-width: 0; white-space: nowrap; }
.item-sub input { padding: 3px 6px; font-size: 13px; }

/* 価格2列の型：列の名前 */
.col-names { display: flex; align-items: center; gap: 6px; margin: -4px 0 10px; font-size: 12.5px; color: var(--muted); }
.col-names input { width: 8em; padding: 3px 6px; }

/* ---------- ページのタブ ---------- */
.page-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  position: sticky;
  top: 54px;
  z-index: 5;
  padding: 8px 0;
  background: var(--bg);
}
.tab { border-radius: 999px; padding: 5px 14px; }
.tab.is-active { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.tab-no {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 1.5em; height: 1.5em; margin-right: 2px;
  border-radius: 50%; background: var(--accent-weak); color: var(--accent); font-size: 11.5px; font-weight: 700;
}
.tab.is-active .tab-no { background: #fff; }
.tab-add { border-style: dashed; color: var(--muted); }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.page-head .panel-title { margin: 0; }
.page-actions { display: flex; gap: 6px; }

/* ---------- 表紙の設定 ---------- */
.check { display: inline-flex; align-items: center; gap: 6px; margin-bottom: 10px; cursor: pointer; }
.cover-fields.is-off { opacity: .45; }
.en-fields.is-off { opacity: .45; }
.radios { display: flex; gap: 16px; }
.radios label { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.logo-box { display: flex; align-items: center; gap: 8px; }
.logo-box img { max-height: 48px; max-width: 120px; border: 1px solid var(--line); border-radius: 4px; background: #fff; }
.archive-label { font-weight: 700; margin-right: 8px; }

/* 保管庫（各セクションの下） */
.archive {
  margin-top: 12px;
  padding: 8px 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: #faf8f4;
}
.archive summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.archive[open] summary { margin-bottom: 6px; }
.archive .hint { margin: 0 0 2px; }
.archive-count { margin-left: 2px; }
.archive-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 8px;
  align-items: center;
  padding: 5px 0;
  border-top: 1px dotted var(--line);
}
.archive-price { color: var(--muted); white-space: nowrap; }
.archive-name ruby rt { font-size: .55em; }
.add-section { align-self: flex-start; }

/* ---------- 印刷時：メニュー以外はすべて隠す ---------- */
@media print {
  body { background: #fff; }
  .toolbar, .editor, .preview-status { display: none !important; }
  .layout { display: block; padding: 0; }
  .page-tabs { display: none !important; }
  .preview-area { position: static; max-height: none; overflow: visible; padding: 0; }
  .preview-scaler { zoom: 1; }
  .preview-scaler .sheet { box-shadow: none; margin: 0; outline: none !important; }
  .sheet:not(:last-child) { break-after: page; }
}

/* ---------- 原価・原価率（画面だけ。印刷には出ない） ---------- */
/* ツールバーの「原価率を表示」スイッチ（オンのときは色が付く） */
.cost-toggle { display: inline-flex; align-items: center; gap: 8px; padding: 5px 12px 5px 8px; border: 1px solid var(--line); border-radius: 999px; background: #fff; cursor: pointer; white-space: nowrap; font-weight: 600; }
.cost-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.cost-switch { position: relative; width: 34px; height: 20px; border-radius: 999px; background: #c9c4bb; transition: background .15s; flex: none; }
.cost-switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.cost-toggle:has(input:checked) { border-color: #2e6b3a; background: #eef6ef; color: #2e6b3a; }
.cost-toggle input:checked + .cost-switch { background: #2e6b3a; }
.cost-toggle input:checked + .cost-switch::after { transform: translateX(14px); }
.cost-toggle input:focus-visible + .cost-switch { outline: 2px solid var(--accent); outline-offset: 2px; }

/* 価格の下の原価率バッジ：一目で分かるよう、色付きの札にする */
.cost-rate { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; margin-top: 3px; line-height: 1.3; white-space: nowrap; }
.cost-rate .rate { display: inline-block; padding: 2px 9px; border-radius: 999px; background: #e3f1e5; color: #22582d; font-size: 13px; font-weight: 700; }
.cost-rate .rate.is-over { background: #fbe1e1; color: var(--danger); }
.cost-rate .rate.is-over::before { content: "▲ "; font-size: 10px; }
.cost-rate .rate.is-empty { background: #efede9; color: var(--muted); font-weight: 600; cursor: help; }
.cost-rate .guide { color: var(--muted); font-size: 11.5px; }
.cost-rate .guide.is-over { color: var(--danger); font-weight: 600; }
.item-cost, .sec-cost { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; padding: 4px 8px; margin: 2px 0 4px; background: #f3f6f1; border-radius: 6px; font-size: 12.5px; color: var(--muted); }
.item-cost label, .sec-cost label { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.item-cost input, .sec-cost input { width: 6.5em; padding: 3px 6px; font-size: 13px; text-align: right; }
.sec-cost { margin-bottom: 8px; flex-direction: column; align-items: stretch; gap: 6px; padding: 8px 10px; }
.sec-cost-line { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; }
.sec-cost-title { font-weight: 600; color: var(--text); }
.sec-cost-sub { font-weight: 600; color: var(--text); min-width: 7.5em; }
.sec-cost .sec-cost-sub[title] { cursor: help; text-decoration: underline dotted; }
.sec-cost .step { display: inline-flex; align-items: center; gap: 4px; padding: 2px 4px 2px 8px; background: #fff; border: 1px solid #dfe5dc; border-radius: 6px; white-space: nowrap; }
.sec-cost .step input { width: 5.5em; }
.sec-cost .step input[data-field="stepAdd"] { width: 3.5em; }
.sec-cost input.short { width: 3em; }
.sec-cost label.check input { width: auto; }
/* 原価の入力欄の下：仕入れ値を入れた時点での原価・目標・目安の価格 */
.cost-guide { flex-basis: 100%; display: flex; flex-wrap: wrap; gap: 2px 16px; color: var(--text); font-size: 12.5px; }
.cost-guide .cg-item b { font-weight: 700; }
.cost-guide .cg-empty { color: var(--muted); }
/* 容量・1杯の量の欄：セクションの標準をそのまま使っている値は、少し薄い字（書き換えると普通の字に） */
.item-cost input.is-inherited { color: #77706a; }
.item-cost .size-btn { padding: 2px 8px; font-size: 12px; }
.item-cost .size-pick { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 2px 6px; border: 1px solid #dfe5dc; border-radius: 6px; background: #fff; }
.item-cost .size-free { margin-left: 4px; }
.item-cost .size-btn.is-active { background: var(--accent, #8a5a2b); color: #fff; border-color: transparent; }
.cost-guide .cg-empty.is-invalid { color: var(--danger); font-weight: 600; }
.inline-input { display: inline-flex; align-items: center; gap: 4px; }
.field .inline-input input { width: 6em; text-align: right; }
.tab-cost { margin-left: auto; }
.sub-title { margin: 14px 0 6px; font-size: 13.5px; }
.cost-sort { display: flex; align-items: center; gap: 6px; }
.cost-sort .sub-title { margin-right: 8px; }
button.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.cost-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.cost-table th, .cost-table td { padding: 5px 6px; border-bottom: 1px solid var(--line); text-align: left; }
.cost-table th { color: var(--muted); font-weight: 600; white-space: nowrap; }
.cost-table .num { text-align: right; white-space: nowrap; }
.cost-table .is-over { color: var(--danger); font-weight: 700; }
.cost-table tr.is-hidden td { opacity: .55; }
button.link { padding: 0; border: none; background: none; color: var(--accent); text-decoration: underline; cursor: pointer; font: inherit; text-align: left; }
button.link:hover { background: none; }
.cost-table tr.is-pending .reason { color: var(--warn-text); }
/* 焼酎の種類：［芋］［麦］［米］その他［自由入力］ */
.item-kind { display: flex; padding: 0 0 4px; font-size: 12.5px; color: var(--muted); }
.item-kind .size-pick { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 4px; padding: 2px 6px; border: 1px solid #dfe5dc; border-radius: 6px; background: #fff; }
.item-kind .size-btn { padding: 2px 10px; font-size: 12px; }
.item-kind .size-btn.is-active { background: var(--accent, #8a5a2b); color: #fff; border-color: transparent; }
.item-kind .size-free { display: inline-flex; align-items: center; gap: 4px; margin-left: 4px; }
.item-kind input { width: 7em; padding: 3px 6px; font-size: 13px; }

/* =========================================================
   スマホ（画面の幅が 760px 以下）
   - PCの見た目は変えない。印刷（PDF）にも影響しない（screen だけに効く）
   - 「編集」と「プレビュー」は画面下のボタンで切り替える（js/mobile.js）
   ========================================================= */
.mobile-switch { display: none; }

@media screen and (max-width: 760px) {
  body { font-size: 15px; padding-bottom: 72px; }
  /* iPhone は 16px 未満の入力欄を押すと画面を勝手に拡大するので、入力欄は 16px にする */
  input[type="text"], input:not([type]), textarea, select,
  .item-cost input, .sec-cost input, .item-sub input, .item-kind input, .ruby-pair input, .col-names input { font-size: 16px; }
  button, .btn { padding: 8px 12px; }   /* 指で押しやすく */

  /* 上のツールバー：固定をやめ、ボタンを折り返して並べる */
  .toolbar { position: static; flex-direction: column; align-items: stretch; gap: 8px; padding: 10px 12px; }
  .toolbar h1 { font-size: 17px; }
  .toolbar-actions { gap: 6px; }
  .toolbar-actions > * { flex: 1 1 auto; justify-content: center; }
  .save-status { order: 9; flex-basis: 100%; text-align: left; min-width: 0; }

  /* 2列 → 1列。編集とプレビューのどちらか一方だけを見せる */
  .layout { display: block; padding: 10px 12px; }
  .preview-area { position: static; max-height: none; overflow: visible; padding: 0; }
  body:not(.m-preview) .preview-area { display: none; }
  body.m-preview .editor { display: none; }
  .preview-scaler { zoom: var(--m-zoom, 0.45); }
  .preview-scaler .sheet { margin-bottom: 16px; }

  /* 画面下の切り替えボタン */
  .mobile-switch {
    display: block; position: fixed; left: 12px; right: 12px; bottom: 12px; z-index: 20;
    padding: 14px; border: none; border-radius: 12px;
    background: var(--accent); color: #fff; font-size: 16px; font-weight: 700;
    box-shadow: 0 4px 14px rgba(0,0,0,.25);
  }
  .mobile-switch:hover { background: var(--accent); filter: brightness(1.1); }

  /* ページのタブ：1行で横にスクロール */
  .page-tabs { top: 0; flex-wrap: nowrap; overflow-x: auto; margin: 0 -12px; padding: 8px 12px; }
  .tab { flex: none; }
  .tab-cost { margin-left: 0; }

  .panel { padding: 12px; }
  .field { grid-template-columns: 1fr; gap: 2px; margin-bottom: 10px; }
  .page-head { flex-wrap: wrap; }
  .section-head { flex-wrap: wrap; }
  .section-head .section-title-input { flex: 1 1 0; }
  .section-head .section-en-input { flex: 1 1 100%; width: auto; order: 5; }

  /* 品目の行：横一列（表の形）をやめ、2段に組み直す
     1段目：つまみ・表示スイッチ・品名　／　2段目：ラベル・前置き・価格　／　3段目：右に寄せて ふりがな・保管・削除
     3段目以降：焼酎の種類・英語名・生産者・原価・ふりがなの欄（横いっぱい） */
  .item-header { display: none; }   /* 列の名前は、価格の欄の上に小さく出す */
  .item-row {
    display: flex !important; flex-wrap: wrap; align-items: flex-end; gap: 6px;
    padding: 10px 0; border-bottom: 1px solid var(--line); border-radius: 0;
  }
  .item-row > * { order: 2; }
  .item-row > .handle, .item-row > .switch { order: 0; align-self: center; }
  .item-row > [data-field="name"] { order: 1; flex: 1 1 calc(100% - 100px); }
  .item-row > [data-field="label"] { flex: 1 1 100%; }
  .item-row > [data-field="note"] { flex: 1 1 100%; }
  .item-row > [data-field="prefix"] { width: 3.2em; }
  .item-row > .price-cell { width: 6.5em; }
  .item-row > .price-cell::before { content: attr(data-colname); color: var(--muted); font-size: 11.5px; text-align: right; }
  .item-row { align-items: flex-start; }
  /* 幅0・高さ0の見えない区切りを横いっぱいに置いて、ボタンを必ず次の行へ送る */
  .item-row::after { content: ""; order: 3; flex-basis: 100%; height: 0; margin-top: -6px; }
  .item-row > .ruby-btn, .item-row > [data-action="archive-item"], .item-row > [data-action="del-item"] { order: 4; }
  .item-row > .ruby-btn { margin-left: auto; }
  .item-row > .item-kind, .item-row > .item-sub, .item-row > .item-cost, .item-row > .ruby-list { order: 5; flex: 1 1 100%; }
  .item-sub { flex-direction: column; gap: 6px; }

  /* 並べ替えのつまみ：指で長押ししてドラッグ。押している間に画面が流れないようにする */
  .handle { font-size: 22px; padding: 4px 2px; touch-action: none; -webkit-user-select: none; -webkit-touch-callout: none; }

  .cost-table { font-size: 12px; display: block; overflow-x: auto; }
  .archive-row { grid-template-columns: 1fr auto; }
}

/* 印刷のときは、スマホでもプレビュー（メニュー）を必ず出す */
@media print {
  .preview-area { display: block !important; }
  .mobile-switch { display: none !important; }
}

/* ---------- アプリ内の確認画面（js/dialog.js） ---------- */
.ui-dialog-back {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(30, 25, 20, .45);
}
.ui-dialog {
  width: 100%; max-width: 420px; padding: 20px;
  background: #fff; border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.ui-dialog-msg { margin: 0 0 18px; white-space: pre-wrap; line-height: 1.7; font-size: 15px; }
.ui-dialog-btns { display: flex; justify-content: flex-end; gap: 8px; }
.ui-dialog-btns button { min-width: 6em; justify-content: center; padding: 9px 16px; font-size: 15px; }
.ui-dialog-btns button.is-danger { background: var(--danger); border-color: var(--danger); }
@media print { .ui-dialog-back { display: none !important; } }
