/* ============================================================
   見積・請求 AI ツール  base styles
   ============================================================ */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo, sans-serif;
  background: #f1f5f9;
  color: #0f172a;
  line-height: 1.6;
}
:root {
  --brand: #4f46e5;
  --brand-d: #4338ca;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f1f5f9;
  --card: #ffffff;
  --ok: #059669;
  --warn: #d97706;
  --danger: #dc2626;
}
a { color: var(--brand); }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: 15px; }

/* ---------- layout ---------- */
.app-header {
  position: sticky; top: 0; z-index: 50;
  background: rgba(255,255,255,.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.app-header-inner {
  max-width: 1240px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.logo { font-weight: 800; font-size: 18px; color: var(--ink); display:flex; align-items:center; gap:8px; }
.logo .dot { width: 22px; height: 22px; border-radius: 7px; background: linear-gradient(135deg,#6366f1,#a855f7); display:inline-block; }
.nav { display: flex; gap: 4px; margin-left: auto; flex-wrap: wrap; }
.nav button {
  border: 0; background: transparent; padding: 8px 12px; border-radius: 9px;
  font-size: 14px; font-weight: 600; color: var(--muted);
}
.nav button.active, .nav button:hover { background: #eef2ff; color: var(--brand); }

main.container { max-width: 1240px; margin: 0 auto; padding: 18px 16px 80px; }
.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  padding: 18px; box-shadow: 0 1px 2px rgba(15,23,42,.04);
}
.page-title { font-size: 22px; font-weight: 800; margin: 4px 0 16px; display:flex; align-items:center; gap:10px; }
.page-title small { font-size: 13px; font-weight: 600; color: var(--muted); }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 9px 14px; border-radius: 10px; font-weight: 700; font-size: 14px;
  display: inline-flex; align-items: center; gap: 7px; transition: .15s;
}
.btn:hover { border-color: #cbd5e1; background: #f8fafc; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-d); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: #f1f5f9; color: var(--ink); }
.btn.sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.danger { color: var(--danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* ---------- 印影 ---------- */
.seal-thumb { width: 56px; height: 56px; flex: 0 0 56px; display: flex; align-items: center; justify-content: center; }
.seal-thumb svg { width: 100%; height: 100%; }
.seal-thumb.lg { width: 130px; height: 130px; flex: 0 0 130px; }
.doc-seal { pointer-events: none; }
.doc-seal svg { width: 100%; height: 100%; display: block; }

/* ---------- forms ---------- */
.field { margin-bottom: 12px; }
.field label { display:block; font-size: 12.5px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.field input, .field textarea, .field select {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.field input:focus, .field textarea:focus, .field select:focus { outline: 2px solid #c7d2fe; border-color: var(--brand); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
@media (max-width: 720px) { .grid2, .grid3 { grid-template-columns: 1fr; } }

/* ---------- dashboard ---------- */
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
@media (max-width: 720px) { .stat-row { grid-template-columns: repeat(2, 1fr); } }
.stat { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; }
.stat .k { font-size: 12px; font-weight: 700; color: var(--muted); }
.stat .v { font-size: 24px; font-weight: 800; margin-top: 2px; }

/* ---------- chat layout ---------- */
.chat-wrap { display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1.05fr); gap: 16px; align-items: start; }
@media (max-width: 980px) { .chat-wrap { grid-template-columns: 1fr; } }
.chat-panel { display: flex; flex-direction: column; height: min(76vh, 760px); }
.chat-scroll { flex: 1; overflow-y: auto; padding: 6px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 88%; padding: 10px 13px; border-radius: 14px; font-size: 14.5px; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--brand); color: #fff; border-bottom-right-radius: 4px; }
.msg.ai { align-self: flex-start; background: #f1f5f9; color: var(--ink); border-bottom-left-radius: 4px; }
.msg.sys { align-self: center; background: #fffbeb; border: 1px solid #fde68a; color: #92400e; font-size: 13px; }
.chat-input { display: flex; gap: 8px; padding-top: 12px; border-top: 1px solid var(--line); margin-top: 8px; }
.chat-input textarea { flex: 1; resize: none; height: 46px; padding: 11px 12px; border: 1px solid var(--line); border-radius: 12px; }
.chat-input textarea:focus { outline: 2px solid #c7d2fe; border-color: var(--brand); }
.quick-chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip { border: 1px dashed #cbd5e1; background: #f8fafc; color: var(--muted); border-radius: 999px; padding: 5px 11px; font-size: 12.5px; font-weight: 600; }
.chip:hover { border-color: var(--brand); color: var(--brand); background: #eef2ff; }

/* ---------- template picker ---------- */
.tpl-picker { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; margin: 6px 0 12px; }
@media (max-width: 720px) { .tpl-picker { grid-template-columns: repeat(3, 1fr); } }
.tpl-opt { border: 2px solid var(--line); border-radius: 10px; overflow: hidden; cursor: pointer; text-align: center; background:#fff; padding:0; }
.tpl-opt .sw { height: 34px; display:flex; }
.tpl-opt .sw i { flex:1; }
.tpl-opt .nm { font-size: 11px; font-weight: 700; padding: 4px 2px; color: var(--muted); }
.tpl-opt.sel { border-color: var(--brand); }
.tpl-opt.sel .nm { color: var(--brand); }

/* ---------- list rows ---------- */
.row-list { display: flex; flex-direction: column; gap: 8px; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: #fff; border: 1px solid var(--line); border-radius: 12px;
}
.row:hover { border-color: #c7d2fe; }
.row .grow { flex: 1; min-width: 0; }
.row .t { font-weight: 700; font-size: 15px; }
.row .s { font-size: 12.5px; color: var(--muted); }
.badge { font-size: 11.5px; font-weight: 800; padding: 3px 9px; border-radius: 999px; white-space: nowrap; }
.badge.quote { background: #eef2ff; color: #4338ca; }
.badge.invoice { background: #ecfdf5; color: #047857; }
.badge.draft { background: #f1f5f9; color: #475569; }
.badge.sent { background: #e0f2fe; color: #0369a1; }
.badge.paid { background: #dcfce7; color: #15803d; }
.badge.void { background: #fef2f2; color: #b91c1c; }

/* ---------- modal ---------- */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 100; display: none; align-items: center; justify-content: center; padding: 16px; }
.modal-bg.open { display: flex; }
.modal { background: #fff; border-radius: 16px; max-width: 640px; width: 100%; max-height: 90vh; overflow-y: auto; padding: 20px; }
.modal h3 { margin: 0 0 14px; font-size: 18px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.spinner { display:inline-block; width:14px; height:14px; border:2px solid #fff6; border-top-color:#fff; border-radius:50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #0f172a; color:#fff; padding: 11px 18px; border-radius: 12px; z-index: 200; font-size: 14px; opacity: 0; pointer-events:none; transition:.25s; }
.toast.show { opacity: 1; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.empty { text-align: center; color: var(--muted); padding: 40px 10px; font-size: 14px; }

/* ---------- 合言葉ロック画面 ---------- */
.lock-bg {
  position: fixed; inset: 0; z-index: 500;
  background: linear-gradient(135deg, #eef2ff 0%, #f8fafc 55%, #faf5ff 100%);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.lock-card {
  background: #fff; border: 1px solid var(--line); border-radius: 20px;
  padding: 34px 28px; max-width: 400px; width: 100%; text-align: center;
  box-shadow: 0 18px 50px rgba(79,70,229,.14);
}
.lock-logo { width: 54px; height: 54px; margin: 0 auto 14px; border-radius: 16px; background: linear-gradient(135deg,#6366f1,#a855f7); display:flex; align-items:center; justify-content:center; }
.lock-logo span { width: 22px; height: 22px; border-radius: 7px; background: #fff; }
.lock-card h2 { margin: 0 0 6px; font-size: 19px; }
.lock-card p { margin: 0 0 18px; font-size: 13.5px; color: var(--muted); }
.lock-card input { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; font-size: 16px; text-align: center; letter-spacing: .06em; margin-bottom: 12px; }
.lock-card input:focus { outline: 2px solid #c7d2fe; border-color: var(--brand); }
.lock-card .btn { width: 100%; justify-content: center; padding: 12px; font-size: 15px; }
.lock-err { color: var(--danger); font-size: 13px; margin: -4px 0 12px; font-weight: 600; }
.lock-hint { font-size: 11.5px; color: var(--muted); margin-top: 14px; }

/* ============================================================
   DOCUMENT SHEET  (A4 実寸プレビュー / 印刷対応)
   ============================================================ */
.doc-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 4px; }

/* A4 = 210mm x 297mm。プレビューは 96dpi 換算 794 x 1123px */
.doc {
  --accent: #1d4ed8;
  --accent-soft: #eef2ff;
  --ink: #0f172a;
  --line: #e2e8f0;
  width: 794px;
  min-height: 1123px;
  background: #fff; color: #111827;
  padding: 52px 56px 44px;
  margin: 0 auto;
  box-shadow: 0 6px 24px rgba(15,23,42,.14);
  position: relative;
  font-size: 12.5px;
  line-height: 1.62;
  display: flex; flex-direction: column;
}
@media (max-width: 880px) {
  .doc { width: 100%; min-height: auto; padding: 26px 20px; font-size: 12px; box-shadow: none; border: 1px solid #e2e8f0; border-radius: 12px; }
}

/* ---- ヘッダー ---- */
.doc .topbar { position: absolute; top: 0; left: 0; right: 0; height: 8px; background: var(--accent); }
.doc .head { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; padding-top: 6px; }
.doc h1.doc-title { font-size: 32px; margin: 0; letter-spacing: .16em; font-weight: 800; color: var(--accent); line-height: 1.1; }
.doc .doc-sub { font-size: 10.5px; letter-spacing: .42em; color: #9ca3af; margin-top: 6px; text-transform: uppercase; }
.doc .head-meta { text-align: right; font-size: 12px; line-height: 1.9; }
.doc .head-meta .lbl { color: #6b7280; }
.doc .head-meta b { font-weight: 700; }

/* ---- 宛先 + 発行元 ---- */
.doc .meta-block { margin-top: 24px; display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; flex-wrap: wrap; }
.doc .client { flex: 1 1 320px; min-width: 0; }
.doc .client .co { font-size: 20px; font-weight: 800; border-bottom: 1.5px solid var(--accent); display: inline-block; padding-bottom: 3px; line-height: 1.35; }
.doc .client .honor { font-size: 13px; margin-left: 8px; color: #374151; }
.doc .client .sub { font-size: 12.5px; color: #4b5563; margin-top: 6px; }
.doc .client .lead { margin-top: 10px; font-size: 12.5px; color: #111827; }
.doc .issuer-box { text-align: right; font-size: 11.5px; line-height: 1.75; color: #374151; flex: 0 1 250px; }
.doc .issuer-box .co { font-size: 15px; font-weight: 800; color: #111827; }
.doc .issuer-box .reg { font-size: 10.5px; color: #6b7280; }

/* ---- 金額バナー（サンプルのネイビー帯） ---- */
.doc .amount-banner {
  margin-top: 22px; background: var(--accent); color: #fff;
  border-radius: 4px; padding: 20px 26px;
  display: flex; justify-content: space-between; align-items: center; gap: 16px;
}
.doc .amount-banner .k { font-size: 13px; font-weight: 700; letter-spacing: .04em; }
.doc .amount-banner .k small { display:block; font-weight:500; font-size: 11px; opacity: .82; margin-top: 2px; letter-spacing: 0; }
.doc .amount-banner .v { font-size: 34px; font-weight: 800; letter-spacing: .01em; white-space: nowrap; }
.doc .amount-banner .v small { font-size: 15px; font-weight: 700; opacity: .9; margin-left: 4px; }

/* ---- 案内・お詫びボックス ---- */
.doc .notice {
  margin-top: 16px; background: #fbf7ee; border: 1px solid #efe3c8; border-radius: 4px;
  padding: 16px 20px; font-size: 12px; color: #4b4234; line-height: 1.85;
}
.doc .notice .nt { font-weight: 800; color: #8a6d1b; display: block; margin-bottom: 6px; font-size: 12.5px; }

/* ---- 明細テーブル ---- */
.doc .sec-title { font-weight: 800; font-size: 13px; margin: 26px 0 8px; color: #111827; display: flex; align-items: center; gap: 8px; }
.doc .sec-title::before { content: ""; width: 4px; height: 15px; background: var(--accent); border-radius: 2px; display: inline-block; }
.doc table.items { width: 100%; border-collapse: collapse; }
.doc table.items th, .doc table.items td { padding: 10px 12px; text-align: left; vertical-align: top; }
.doc table.items td.num, .doc table.items th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.doc table.items thead th { font-size: 11.5px; font-weight: 700; background: #f3f4f6; color: #374151; border-top: 1.5px solid #d1d5db; border-bottom: 1px solid #e5e7eb; }
.doc table.items tbody tr { border-bottom: 1px solid #eef0f2; }
.doc .item-name { font-weight: 700; }
.doc .item-detail { font-size: 11px; color: #6b7280; margin-top: 2px; }

/* ---- 合計ブロック ---- */
.doc .totals-wrap { display: flex; justify-content: flex-end; margin-top: 4px; }
.doc .totals { width: 320px; }
.doc .totals .tr { display: flex; justify-content: space-between; padding: 7px 12px; font-size: 12.5px; border-bottom: 1px solid #eef0f2; }
.doc .totals .tr .v { font-variant-numeric: tabular-nums; }
.doc .totals .tr.grand { border-top: 2px solid var(--accent); border-bottom: 0; font-weight: 800; font-size: 18px; padding-top: 11px; color: var(--accent); }
.doc .totals .tr.sub .v { font-weight: 700; }

/* ---- 支払スケジュール ---- */
.doc table.schedule { width: 100%; border-collapse: collapse; margin-top: 4px; }
.doc table.schedule th, .doc table.schedule td { padding: 9px 12px; text-align: left; font-size: 12px; border-bottom: 1px solid #eef0f2; }
.doc table.schedule thead th { background: #f3f4f6; font-size: 11.5px; color: #374151; }
.doc table.schedule td.num { text-align: right; font-variant-numeric: tabular-nums; }
.doc table.schedule tbody tr:nth-child(even) { background: #fafbfc; }

/* ---- 振込先（枠付き） ---- */
.doc .bank-box { margin-top: 20px; border: 1px solid #d1d5db; border-radius: 4px; padding: 16px 20px; }
.doc .bank-box .bt { font-weight: 800; font-size: 12.5px; margin-bottom: 10px; color: #111827; }
.doc .bank-box .brow { display: flex; font-size: 12px; padding: 4px 0; }
.doc .bank-box .brow .bk { width: 130px; color: #6b7280; flex: 0 0 130px; }
.doc .bank-box .brow .bv { font-weight: 600; color: #111827; }

/* ---- 発行元（押印） ---- */
.doc .issuer-foot { text-align: right; margin-top: auto; padding-top: 28px; position: relative; }
.doc .issuer-foot .co { font-size: 15px; font-weight: 800; }
.doc .doc-seal { margin-left: auto; }

/* ---- フッター注記 ---- */
.doc .footline { border-top: 1px solid #e5e7eb; margin-top: 18px; padding-top: 10px; font-size: 10.5px; color: #9ca3af; text-align: center; }

/* ============ テンプレ別アクセント ============ */
.doc.tpl-wamodern { font-family: "Yu Mincho", "Hiragino Mincho ProN", serif; --accent: #7f1d1d; --accent-soft: #fdf2f2; }
.doc.tpl-wamodern h1.doc-title { letter-spacing: .22em; border-bottom: 3px double #7f1d1d; padding-bottom: 8px; }
.doc.tpl-minimal { --accent: #111827; --accent-soft: #f5f5f5; }
.doc.tpl-minimal h1.doc-title { font-weight: 300; letter-spacing: .3em; }
.doc.tpl-natural { --accent: #047857; --accent-soft: #ecfdf5; }
.doc.tpl-dark { --accent: #0ea5e9; --accent-soft: #1e293b; background: #0f172a; color: #e2e8f0; }
.doc.tpl-dark .client .co, .doc.tpl-dark .issuer-box .co, .doc.tpl-dark .issuer-foot .co { color: #f8fafc; }
.doc.tpl-dark .client .co { border-color: #0ea5e9; }
.doc.tpl-dark table.items thead th { background: #1e293b; color: #7dd3fc; border-color: #334155; }
.doc.tpl-dark table.items tbody tr { border-bottom-color: #1e293b; }
.doc.tpl-dark .item-detail, .doc.tpl-dark .client .sub, .doc.tpl-dark .issuer-box { color: #94a3b8; }
.doc.tpl-dark .totals .tr { border-bottom-color: #1e293b; }
.doc.tpl-dark .notice { background: #1e293b; border-color: #334155; color: #cbd5e1; }
.doc.tpl-dark .notice .nt { color: #7dd3fc; }
.doc.tpl-dark .bank-box { border-color: #334155; }
.doc.tpl-dark .footline { border-color: #1e293b; color: #64748b; }
.doc.tpl-gold { font-family: "Yu Mincho", "Hiragino Mincho ProN", serif; --accent: #b8860b; --accent-soft: #f5ecd0; }
.doc.tpl-gold .amount-banner { background: linear-gradient(120deg, #b8860b, #d4a72c); }

/* ============================================================
   印刷（A4 にピタッと収める）
   ============================================================ */
@page { size: A4; margin: 0; }

@media print {
  html, body { background: #fff !important; margin: 0 !important; padding: 0 !important; }
  body * { visibility: hidden !important; }
  /* 印刷対象（プレビュー / チャットプレビュー / 設定サンプル）だけ見せる */
  #print-area, #print-area * { visibility: visible !important; }
  #print-area {
    position: absolute !important; left: 0 !important; top: 0 !important;
    width: 210mm !important; min-height: 297mm !important;
    box-shadow: none !important; border: 0 !important; border-radius: 0 !important;
    padding: 14mm 15mm !important; margin: 0 !important;
    font-size: 11.5px !important;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  #print-area .topbar { left: 0 !important; right: 0 !important; }
  .no-print, .app-header, .nav, .modal-head, .modal-foot { display: none !important; }
  .modal-bg { position: static !important; background: none !important; display: block !important; padding: 0 !important; }
  .modal { max-width: none !important; width: auto !important; box-shadow: none !important; padding: 0 !important; border-radius: 0 !important; }
  .doc-scroll { overflow: visible !important; }
  /* 改ページ制御 */
  .doc table.items tr, .doc table.schedule tr { break-inside: avoid; page-break-inside: avoid; }
  .doc .amount-banner, .doc .notice, .doc .bank-box, .doc .issuer-foot { break-inside: avoid; page-break-inside: avoid; }
  .no-print-keep { display: none !important; }
}
