/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { overflow-x: hidden; } /* 水平スクロール防止（iOS Safari対応） */

:root {
  --color-surface: #FAFAF8;
  --color-ink: #1A1A1A;
  --color-grid: #C8C8C0;
  --color-signal: #D4764A;
  --font-sans: "Noto Sans JP", "Hiragino Kaku Gothic ProN", sans-serif;
  --font-latin: "Inter", sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", monospace;
}

body {
  font-family: var(--font-sans);
  background: var(--color-surface);
  color: var(--color-ink);
  line-height: 1.9;
  font-feature-settings: "palt";
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: repeating-linear-gradient(
    to bottom,
    transparent,
    transparent 7px,
    rgba(200, 200, 192, 0.06) 7px,
    rgba(200, 200, 192, 0.06) 8px
  );
  pointer-events: none;
  z-index: 0;
}

.container { max-width: 1400px; margin: 0 auto; padding: 0 24px; position: relative; }

/* === Header === */
.site-header {
  background: rgba(250, 250, 248, 0.95);
  backdrop-filter: blur(4px);
  border-bottom: 1px solid rgba(200, 200, 192, 0.4);
  padding: 16px 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: var(--color-ink);
}

.logo-symbol {
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: 700;
  color: var(--color-signal);
}

.logo-name {
  font-family: var(--font-latin);
  font-size: 15px;
  font-weight: 600;
}

.logo-divider {
  color: var(--color-grid);
  font-size: 15px;
}

.logo-tool {
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 400;
  color: var(--color-grid);
}

.header-back {
  font-family: var(--font-latin);
  font-size: 13px;
  color: var(--color-grid);
  text-decoration: none;
  white-space: nowrap;
}

.header-back:hover {
  color: var(--color-signal);
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 3px;
}

/* === Layout === */
.main-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 24px;
  padding-top: 32px;
  padding-bottom: 64px;
  align-items: start;
  position: relative;
}

@media (max-width: 1024px) {
  /* minmax(0,1fr): グリッドアイテムがコンテナ幅を超えないよう強制 */
  .main-layout { grid-template-columns: minmax(0, 1fr); }
  .preview-panel {
    position: static;
    top: auto;
  }
}

.editor-panel, .preview-panel {
  border: 1px solid var(--color-grid);
  padding: 24px;
  background: var(--color-surface);
  min-width: 0; /* グリッドアイテムのmin-width:auto問題を防ぐ */
}

.preview-panel {
  position: sticky;
  top: 80px;
}

.section-title {
  font-family: var(--font-latin);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.15em;
  color: var(--color-grid);
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(200, 200, 192, 0.4);
}

/* === Form === */
.form-group {
  margin-bottom: 20px;
  border: 1px solid var(--color-grid);
  padding: 16px;
}

fieldset.form-group {
  border: 1px solid var(--color-grid);
  min-width: 0; /* fieldsetのブラウザデフォルトmin-width:min-contentを上書き */
}

fieldset.form-group legend {
  font-weight: 700;
  font-size: 11px;
  padding: 0 8px;
  color: var(--color-ink);
  font-family: var(--font-latin);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.form-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.form-row .form-field { flex: 1; min-width: 140px; }

.form-field {
  margin-bottom: 12px;
}
.form-field label {
  display: block;
  font-size: 11px;
  font-weight: 500;
  margin-bottom: 4px;
  color: var(--color-grid);
  font-family: var(--font-latin);
  letter-spacing: 0.05em;
}
.form-field input,
.form-field textarea,
.form-field select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--color-grid);
  border-radius: 0;
  font-size: 14px;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-ink);
  transition: border-color 150ms ease-out;
}
.form-field input:focus,
.form-field textarea:focus,
.form-field select:focus {
  outline: none;
  border-color: var(--color-signal);
}

/* === Line Items === */
.line-item {
  display: grid;
  grid-template-columns: minmax(0, 2fr) 80px 100px 60px 100px 36px;
  gap: 8px;
  align-items: end;
  margin-bottom: 8px;
}
.line-item-header {
  display: grid;
  grid-template-columns: minmax(0, 2fr) 80px 100px 60px 100px 36px;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 11px;
  color: var(--color-grid);
  font-weight: 500;
  font-family: var(--font-latin);
  letter-spacing: 0.05em;
}
.line-item input, .line-item select {
  padding: 6px 8px;
  border: 1px solid var(--color-grid);
  border-radius: 0;
  font-size: 13px;
  font-family: inherit;
  background: var(--color-surface);
  color: var(--color-ink);
  transition: border-color 150ms ease-out;
}
.line-item input:focus, .line-item select:focus {
  outline: none;
  border-color: var(--color-signal);
}
.remove-line-btn {
  background: none;
  border: 1px solid var(--color-grid);
  color: var(--color-grid);
  border-radius: 0;
  cursor: pointer;
  font-size: 14px;
  padding: 6px;
  font-family: var(--font-mono);
  transition: border-color 150ms ease-out, color 150ms ease-out;
}
.remove-line-btn:hover {
  border-color: #B85A5A;
  color: #B85A5A;
}

@media (max-width: 640px) {
  /* 明細行: 3カラムグリッドに変更（品目は全幅スパン） */
  /* minmax(0,1fr) でグリッドアイテムのintrinsic min-widthを0に強制 */
  .line-item {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 1fr);
  }
  .line-item input[data-field="description"] {
    grid-column: 1 / -1;
  }
  .line-item .line-subtotal {
    grid-column: span 2;
  }
  /* line-item内の入力要素: min-width:0 + width:100%でグリッドセル内に収める */
  .line-item input,
  .line-item select,
  .line-item button {
    min-width: 0;
    width: 100%;
  }
  .line-item button {
    width: auto; /* 削除ボタンは幅を自動に（全幅不要） */
    min-width: 36px;
  }
  /* ヘッダーラベル行はモバイルでは非表示（placeholderで代替） */
  .line-item-header {
    display: none;
  }
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border: 1px solid transparent;
  border-radius: 2px;
  font-size: 14px;
  font-family: var(--font-sans);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 150ms ease-out, border-color 150ms ease-out, color 150ms ease-out;
}
.btn-primary {
  background: var(--color-signal);
  color: white;
  border-color: var(--color-signal);
}
.btn-primary:hover {
  background: #1e47e0;
  border-color: #1e47e0;
}
.btn-secondary {
  background: var(--color-surface);
  color: var(--color-ink);
  border-color: var(--color-grid);
}
.btn-secondary:hover {
  border-color: var(--color-signal);
  color: var(--color-signal);
}

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 8px;
}

/* === Invoice Preview (Paper) === */
.invoice-paper {
  background: white;
  border: 1px solid var(--color-grid);
  padding: 40px;
  font-size: 11px;
  line-height: 1.5;
  min-height: 800px;
}

.inv-header {
  text-align: center;
  margin-bottom: 24px;
}
.inv-title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 4px;
  border-bottom: 3px double var(--color-ink);
  display: inline-block;
  padding-bottom: 4px;
  margin-bottom: 8px;
}
.inv-number {
  font-size: 10px;
  color: var(--color-grid);
  font-family: var(--font-mono);
}

.inv-parties {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 20px;
}
.inv-buyer, .inv-seller { flex: 1; }
.inv-buyer-name {
  font-size: 16px;
  font-weight: 700;
  border-bottom: 1px solid var(--color-ink);
  padding-bottom: 2px;
  margin-bottom: 4px;
}
.inv-buyer-name::after { content: ' 御中'; font-weight: 400; font-size: 12px; }
.inv-buyer-dept { font-size: 10px; color: var(--color-grid); }

.inv-seller {
  text-align: right;
  font-size: 10px;
}
.inv-seller-name { font-size: 13px; font-weight: 700; margin-bottom: 2px; }
.inv-registration {
  display: inline-block;
  background: transparent;
  border: 1px solid var(--color-grid);
  padding: 2px 8px;
  border-radius: 0;
  font-size: 9px;
  margin-top: 4px;
  color: var(--color-ink);
  font-family: var(--font-mono);
}

.inv-meta {
  display: flex;
  justify-content: flex-end;
  gap: 20px;
  margin-bottom: 16px;
  font-size: 10px;
}
.inv-meta-item span { font-weight: 700; }

/* Table */
.inv-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}
.inv-table th {
  background: var(--color-ink);
  color: var(--color-surface);
  padding: 6px 8px;
  text-align: left;
  font-size: 10px;
  font-weight: 500;
}
.inv-table th:nth-child(n+2) { text-align: right; }
.inv-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--color-grid);
  font-size: 10px;
}
.inv-table td:nth-child(n+2) { text-align: right; }
.inv-table tbody tr:nth-child(even) { background: rgba(200, 200, 192, 0.08); }

/* Totals */
.inv-totals {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 20px;
}
.inv-totals-table { width: 260px; }
.inv-totals-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 10px;
  border-bottom: 1px solid var(--color-grid);
}
.inv-totals-row.total {
  font-size: 14px;
  font-weight: 700;
  border-bottom: 3px double var(--color-ink);
  padding: 8px 0;
}

/* Bank & Notes */
.inv-bank {
  background: transparent;
  border: 1px solid var(--color-grid);
  padding: 10px;
  margin-bottom: 12px;
  font-size: 10px;
}
.inv-bank-title { font-weight: 700; margin-bottom: 4px; }

.inv-notes {
  font-size: 9px;
  color: var(--color-grid);
  white-space: pre-wrap;
}

/* === Footer === */
.site-footer {
  background: var(--color-ink);
  color: var(--color-grid);
  padding: 32px 0;
  font-size: 12px;
  font-family: var(--font-latin);
  position: relative;
}

.footer-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

.footer-copy { color: rgba(200, 200, 192, 0.5); }

.footer-link {
  color: var(--color-grid);
  text-decoration: none;
}
.footer-link:hover {
  color: var(--color-surface);
  text-decoration: underline;
}

.footer-brand {
  font-family: var(--font-mono);
  font-size: 40px;
  color: rgba(200, 200, 192, 0.1);
  position: absolute;
  right: 24px;
  bottom: 8px;
  line-height: 1;
  pointer-events: none;
}

.footer-disclaimer {
  margin-top: 16px;
  font-size: 11px;
  color: rgba(200, 200, 192, 0.4);
  max-width: 640px;
  line-height: 1.7;
}

/* === Page Heading === */
.page-heading {
  border-bottom: 1px solid rgba(200, 200, 192, 0.3);
  padding-top: 32px;
  padding-bottom: 24px;
  margin-bottom: 0;
}

.page-title {
  font-family: var(--font-sans);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--color-ink);
  margin-bottom: 4px;
}

.page-subtitle {
  font-family: var(--font-latin);
  font-size: 13px;
  color: var(--color-grid);
}

/* === QA Fixes === */

/* 小計フィールド（インラインスタイル廃止） */
.line-subtotal {
  background: rgba(200, 200, 192, 0.1) !important;
  text-align: right;
  color: var(--color-ink);
}

/* 削除ボタン: 1件のみ時 */
.remove-line-btn--disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* 削除不可フィードバック（赤フラッシュ） */
.remove-line-btn--flash {
  border-color: #B85A5A !important;
  color: #B85A5A !important;
}

/* focus-visible: キーボード操作時のフォーカスリング */
.form-field input:focus-visible,
.form-field textarea:focus-visible,
.form-field select:focus-visible,
.line-item input:focus-visible,
.line-item select:focus-visible {
  outline: 2px solid var(--color-signal);
  outline-offset: 1px;
}

/* 削除ボタン タッチターゲット拡大（44px最小） */
.remove-line-btn {
  min-width: 36px;
  min-height: 36px;
}

@media (hover: none) {
  .remove-line-btn {
    min-width: 44px;
    min-height: 44px;
  }
}

/* === Invoice Preview Additions === */

/* 登録番号 未入力警告 */
.inv-registration--missing {
  border-color: #B85A5A;
  color: #B85A5A;
}

/* 軽減税率マーク */
.inv-reduced-mark {
  font-size: 8px;
  vertical-align: super;
  margin-left: 1px;
}

/* 軽減税率注記（明細テーブル下） */
.inv-reduced-note {
  font-size: 9px;
  color: #555;
  margin-bottom: 8px;
  text-align: right;
}

/* 請求先住所 */
.inv-buyer-address {
  font-size: 10px;
  margin-top: 2px;
}

/* 「下記の通りご請求〜」テキスト */
.inv-regards {
  margin-top: 12px;
  font-size: 10px;
}

/* 振込先詳細（改行保持） */
.inv-bank-detail {
  white-space: pre-wrap;
}

/* ご請求金額欄（上部の強調合計） */
.inv-totals--featured {
  margin-bottom: 16px;
}

/* ============================================================
   Responsive: Mobile / Tablet
   ============================================================ */

/* --- R-1: ヘッダー (≤640px) --- */
@media (max-width: 640px) {
  .header-inner {
    flex-wrap: wrap;
    gap: 8px;
  }
  .logo {
    flex-wrap: wrap;
    row-gap: 2px;
  }
  .logo-symbol { font-size: 14px; }
  .logo-name   { font-size: 13px; }
  .logo-divider { display: none; }
  .logo-tool { font-size: 11px; }
  .header-back { font-size: 12px; }
}

/* --- R-2: Container padding + パネルpadding削減 (≤640px / ≤360px) --- */
@media (max-width: 640px) {
  .container { padding: 0 12px; }
  .main-layout { padding-bottom: 40px; }
  /* パネルpadding: 24px → 16px */
  .editor-panel,
  .preview-panel {
    padding: 16px;
  }
  /* プレビューは横スクロール可能に（テーブルが272px未満に収まらない場合） */
  .preview-panel {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}
@media (max-width: 425px) {
  .container { padding: 0 8px; }
  /* さらに削減: 16px → 12px */
  .editor-panel,
  .preview-panel {
    padding: 12px;
  }
}

/* --- R-4 / R-5: form-row を縦積み (≤640px) --- */
@media (max-width: 640px) {
  .form-row {
    flex-direction: column;
  }
  .form-row .form-field {
    min-width: auto;
    flex: auto;
  }
}

/* --- R-6: アクションボタン全幅 (≤640px) --- */
@media (max-width: 640px) {
  .actions {
    flex-direction: column;
  }
  .actions .btn {
    width: 100%;
    justify-content: center;
  }
}

/* --- R-8: invoice-paper パディング削減 (≤640px / ≤425px) --- */
@media (max-width: 640px) {
  .invoice-paper {
    padding: 20px;
    min-height: auto;
  }
}
@media (max-width: 425px) {
  .invoice-paper {
    padding: 12px;
  }
}

/* --- R-9: 請求先・差出人 縦積み (≤640px) --- */
@media (max-width: 640px) {
  .inv-parties {
    flex-direction: column;
    gap: 12px;
  }
  .inv-seller {
    text-align: left;
  }
  /* 長いテキストの折り返し強制（住所・登録番号・メール等） */
  .inv-seller,
  .inv-buyer-address,
  .inv-registration,
  .inv-notes {
    word-break: break-word;
    overflow-wrap: break-word;
  }
}

/* --- R-10: inv-table フォントサイズ縮小 (≤640px / ≤425px) --- */
@media (max-width: 640px) {
  .inv-table {
    font-size: 9px;
  }
  .inv-table th,
  .inv-table td {
    padding: 4px 5px;
  }
}
@media (max-width: 425px) {
  .inv-table {
    font-size: 8px;
  }
  .inv-table th,
  .inv-table td {
    padding: 3px 4px;
  }
}

/* --- R-11: inv-totals 全幅 (≤640px) --- */
@media (max-width: 640px) {
  .inv-totals {
    justify-content: flex-start;
  }
  .inv-totals-table {
    width: 100%;
  }
}

/* --- R-12: inv-meta 縦積み (≤640px) --- */
@media (max-width: 640px) {
  .inv-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    justify-content: flex-start;
  }
}

/* --- R-13: inv-title フォントサイズ縮小 (≤640px / ≤425px) --- */
@media (max-width: 640px) {
  .inv-title {
    font-size: 18px;
    letter-spacing: 2px;
  }
}
@media (max-width: 425px) {
  .inv-title {
    font-size: 16px;
    letter-spacing: 1px;
  }
}

/* --- R-14: フッター縦積み (≤640px) --- */
@media (max-width: 640px) {
  .footer-inner {
    flex-direction: column;
    gap: 8px;
  }
  .footer-brand {
    right: 12px;
    bottom: 4px;
    font-size: 30px;
  }
  .footer-disclaimer {
    font-size: 10px;
  }
}

/* === PDF Capture Mode ===
   PDF出力時にスマホでもPC版レイアウトを強制する。
   .pdf-mode をプレビューパネルに付与して、レスポンシブ上書きをリセット。 */
.pdf-mode.preview-panel {
  padding: 24px;
  overflow-x: visible;
}
.pdf-mode .invoice-paper {
  padding: 40px;
  min-height: 800px;
}
.pdf-mode .inv-title {
  font-size: 22px;
  letter-spacing: 4px;
}
.pdf-mode .inv-parties {
  flex-direction: row;
  gap: 20px;
}
.pdf-mode .inv-seller {
  text-align: right;
}
.pdf-mode .inv-meta {
  flex-direction: row;
  gap: 20px;
  justify-content: flex-end;
  align-items: center;
}
.pdf-mode .inv-table {
  font-size: 10px;
}
.pdf-mode .inv-table th,
.pdf-mode .inv-table td {
  padding: 6px 8px;
}
.pdf-mode .inv-totals {
  justify-content: flex-end;
}
.pdf-mode .inv-totals-table {
  width: 260px;
}
