/* ===== 基本設定 ===== */
:root {
  --green: #1f4a3a;
  --line: #9fb3aa;
  --head-bg: #e8efec;
  --fill: #fff7c2;
  --text: #222;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: #eef1ef;
  color: var(--text);
  font-family: "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

button, input { font: inherit; color: inherit; }

/* ===== 操作ボタン ===== */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  max-width: 680px;
  margin: 0 auto;
  padding: 10px 0;
  background: #eef1ef;
}
.toolbar button, .add-row, .history-actions button, .history .actions button {
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
button.primary { background: var(--green); border-color: var(--green); color: #fff; }
button.armed { background: #b33 !important; border-color: #b33 !important; color: #fff !important; }
button:focus-visible { outline: 2px solid #3d8a6b; outline-offset: 2px; }

/* ===== 操作パネル（品目ボタン・設定・履歴） ===== */
.panel {
  max-width: 680px;
  margin: 12px auto;
  padding: 14px 20px;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}
.panel-title { margin: 0 0 10px; font-size: 16px; font-weight: bold; }
details.panel > summary { cursor: pointer; margin: 0; }
details.panel[open] > summary { margin-bottom: 10px; }
.hint { margin: 8px 0 0; font-size: 12px; color: #555; }

/* 品目ボタン */
.quick-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}
.quick {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-left: 5px solid var(--green);
  border-radius: 6px;
  background: #f7faf8;
  text-align: left;
  cursor: pointer;
}
.quick-buttons > .hint { grid-column: 1 / -1; margin: 0; }
.quick:hover { background: #e8efec; }
.quick:active { transform: scale(0.98); }
.quick.kind-ship { border-left-color: #c08a2e; }
.quick.kind-other { border-left-color: #888; }
.quick-name { font-weight: bold; }
.quick-price { font-size: 13px; color: #555; font-variant-numeric: tabular-nums; }

/* 品目の設定 */
.product-head, .product-row {
  display: grid;
  grid-template-columns: 76px 1fr 56px 88px 40px;
  gap: 6px;
  align-items: center;
}
.product-head { font-size: 12px; color: #555; margin-bottom: 4px; }
.product-row { margin-bottom: 6px; }
.product-row input, .product-row select {
  width: 100%;
  min-width: 0;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #fff;
  font: inherit;
}
.product-row [data-field="price"] { text-align: right; }
.product-row .del {
  height: 32px;
  border: none;
  background: none;
  color: #b33;
  font-size: 13px;
  cursor: pointer;
}

/* お客さん選択 */
.panel-title-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.panel-title-row .panel-title { margin: 0; }
.panel-title-spaced { margin-top: 18px; }
.filter {
  width: 12em;
  max-width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
  font-size: 13px;
}
.quick.customer { border-left-color: #3b6ea5; }
.quick.customer.selected { background: #e3ecf6; }

/* 宛名の下の住所（空なら画面では案内文、印刷では非表示） */
.client-address { margin: 6px 0 0; font-size: 13px; white-space: pre-line; }
[contenteditable][data-placeholder]:empty::before { content: attr(data-placeholder); color: #999; }
.exporting [contenteditable]:empty::before { content: none; }

/* お客さん名簿 */
.customer-row {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: 6px;
  padding: 10px 0;
  border-bottom: 1px solid #e3e9e6;
}
.customer-row input[type="text"] {
  width: 100%;
  min-width: 0;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
}
.customer-row .row-foot {
  grid-column: 1 / -1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}
.customer-row label { display: flex; align-items: center; gap: 4px; cursor: pointer; }
.customer-row .del { border: none; background: none; color: #b33; cursor: pointer; font-size: 13px; }
@media (max-width: 560px) {
  .customer-row { grid-template-columns: 1fr; }
}

/* バックアップ */
.file-button {
  display: inline-block;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}

.restore-confirm {
  margin-top: 8px;
  padding: 8px 16px;
  border: 1px solid var(--green);
  border-radius: 6px;
  cursor: pointer;
}

/* Googleスプレッドシート連携 */
.sheet-setting { display: flex; gap: 8px; margin-top: 10px; }
.sheet-setting input {
  flex: 1;
  min-width: 0;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
}
.sheet-setting button {
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  cursor: pointer;
}
.sheet-status { margin: 8px 0 0; font-size: 13px; }
.sheet-status.ok { color: #1f6b47; }
.sheet-status.error { color: #b33; }

/* 発行履歴 */
.history-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.table-scroll { overflow-x: auto; }
.history { font-size: 13px; }
.history th, .history td { white-space: nowrap; padding: 6px; }
.history .num { text-align: right; font-variant-numeric: tabular-nums; }
.history .empty { color: #555; text-align: center; }
.history .actions button + button { margin-left: 4px; }
.history .actions button { padding: 4px 8px; font-size: 12px; }
.history .actions .del { color: #b33; }

/* ===== 請求書本体 ===== */
.sheet {
  max-width: 680px;
  margin: 12px auto 24px;
  padding: 24px 20px;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}

h1 {
  margin: 0 0 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--green);
  font-size: 32px;
  letter-spacing: 0.05em;
}
h2 { margin: 24px 0 8px; font-size: 19px; }

.head {
  display: flex;
  gap: 16px;
  justify-content: space-between;
}
.head-left { flex: 1; min-width: 0; }
.head-right { flex: 1; min-width: 0; margin: 0; }
.head-right > div { display: flex; gap: 10px; margin-bottom: 6px; }
.head-right dt { flex: none; }
.head-right dd { margin: 0; flex: 1; min-width: 0; }

.shop { margin: 0 0 8px; font-weight: bold; }
.client { margin: 0; display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.client input { width: 9em; }

/* 編集できる文字（画面上だけ薄く点線で示す） */
[contenteditable] { outline: none; border-radius: 3px; min-height: 1.4em; }
[contenteditable]:hover { box-shadow: 0 0 0 1px #ccc; }
[contenteditable]:focus { box-shadow: 0 0 0 2px #9ec5b4; }

/* 入力欄（黄色の部分） */
input.fill, .items input {
  min-width: 0;
  padding: 4px 6px;
  border: 1px solid #e0c95a;
  border-radius: 4px;
  background: var(--fill);
}
input.fill:focus, .items input:focus { outline: 2px solid #9ec5b4; }

/* ご請求金額 */
.amount-bar {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 10px;
  margin-top: 20px;
  padding: 16px;
  border-radius: 6px;
  background: var(--green);
  color: #fff;
}
.amount-label { font-size: 15px; font-weight: bold; }
.amount-value { font-size: 30px; font-weight: bold; }

/* 明細・振込先の表 */
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: middle; }
th { background: var(--head-bg); font-weight: bold; }

.items col.c-qty { width: 22%; }
.items col.c-price { width: 22%; }
.items col.c-amount { width: 20%; }
.items col.c-del { width: 36px; }
.items td { padding: 4px; }
.items input { width: 100%; }
.items .qty { display: flex; gap: 2px; }
.items .qty input:first-child { width: 55%; text-align: right; }
.items .qty input:last-child { width: 45%; }
.items .price { display: flex; align-items: center; gap: 2px; }
.items .price input { text-align: right; }
.items .amount { text-align: right; white-space: nowrap; padding-right: 8px; }
.items .del {
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  background: none;
  color: #b33;
  font-size: 18px;
  cursor: pointer;
}
.items tfoot td {
  background: var(--green);
  color: #fff;
  font-weight: bold;
  text-align: right;
  border-color: var(--green);
}
.items tfoot span { margin-left: 16px; }
.add-row { margin-top: 8px; }

.bank th { width: 42%; font-weight: normal; }
.note { margin: 14px 0 0; }

.toolbar button:disabled { opacity: 0.6; cursor: wait; }

/* お知らせ */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 6px;
  background: #1f2a25;
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 20;
}
.toast.show { opacity: 1; }

/* ===== PDF作成中の見た目（印刷と同じにする） ===== */
html:has(body.exporting), .exporting { background: #fff; }
.exporting .sheet { box-shadow: none; }
.exporting input.fill, .exporting .items input { border: none; background: none; padding: 0; }
.exporting .client input { field-sizing: content; width: auto; min-width: 1em; }
.exporting [contenteditable] { box-shadow: none !important; }
.exporting .items tr.is-empty { display: none; }
.exporting .client-address:empty { display: none; }
.exporting .sheet .no-print { display: none !important; }

/* ===== スマホ表示 ===== */
@media (max-width: 560px) {
  body { font-size: 14px; }
  .sheet { padding: 16px; margin: 12px 0 16px; box-shadow: none; }
  .head { flex-direction: column; gap: 8px; }
  h1 { font-size: 26px; }
  .amount-value { font-size: 26px; }
  .items col.c-qty { width: 24%; }
  .items col.c-price { width: 24%; }
}

/* ===== 印刷設定 ===== */
@page { size: A4; margin: 14mm; }

@media print {
  body { background: #fff; padding: 0; }
  .no-print { display: none !important; }
  .sheet { max-width: none; margin: 0; padding: 0; box-shadow: none; }

  /* 入力欄を普通の文字に見せる */
  input.fill, .items input { border: none; background: none; padding: 0; }
  .client input { field-sizing: content; width: auto; min-width: 1em; }
  [contenteditable] { box-shadow: none !important; }

  /* 空の行は印刷しない */
  .items tr.is-empty { display: none; }
  .client-address:empty { display: none; }
  [contenteditable]:empty::before { content: none; }

  .amount-bar, .items tfoot td, th {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* ===== デモ版の案内 ===== */
.demo-banner {
  max-width: 680px;
  box-sizing: border-box;
  margin: 12px auto 0;
  padding: 10px 14px;
  border: 1px solid #e3c770;
  border-radius: 8px;
  background: #fff8e1;
  color: #5b4a12;
  font-size: 14px;
}
.demo-banner p {
  margin: 0 0 8px;
}
.demo-banner-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  align-items: center;
}
.demo-banner-actions a {
  color: inherit;
}
