:root {
  --c-bg: #f3f5f8;
  --c-surface: #ffffff;
  --c-text: #1d2430;
  --c-muted: #6b7482;
  --c-border: #d6dbe3;
  --c-primary: #1f5fa8;
  --c-primary-text: #ffffff;
  --c-primary-soft: #e6eef8;
  --c-danger: #c0392b;
  --c-danger-soft: #fdecea;
  --c-quote: #1f5fa8;
  --c-invoice: #1e8a5a;
  --c-overlay: rgba(20, 26, 36, 0.45);
  --radius: 10px;
  --tab-h: 56px;
  --bar-h: 116px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", Meiryo, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--c-bg);
  color: var(--c-text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
}

/* フォーム部品は16px(iOSのフォーカス時ズーム防止) */
input, select, textarea, button { font: inherit; font-size: 16px; }
input, select, textarea {
  width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--c-border);
  border-radius: 8px;
  background: var(--c-surface);
  color: var(--c-text);
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--c-primary); outline-offset: 0; border-color: var(--c-primary); }

/* 省略表示(Grid/Flexの子でも縮むよう min-width:0) */
.ellipsis { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.muted { color: var(--c-muted); font-size: 13px; margin: 4px 0; }
.error-text { color: var(--c-danger); font-size: 14px; margin: 6px 0; min-height: 1em; }

/* ---------- ボタン ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 8px 14px;
  border: 1px solid var(--c-border); border-radius: 8px;
  background: var(--c-surface); color: var(--c-text);
  cursor: pointer; white-space: nowrap;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--c-primary); border-color: var(--c-primary); color: var(--c-primary-text); }
.btn-outline { border-color: var(--c-primary); color: var(--c-primary); }
.btn-danger { border-color: var(--c-danger); color: var(--c-danger); }
.btn-small { min-height: 38px; padding: 6px 12px; font-size: 14px; }
.btn-block { display: flex; width: 100%; }
.btn-text { background: none; border: 0; color: var(--c-primary); padding: 8px; cursor: pointer; font-size: 14px; white-space: nowrap; }

/* ---------- ログイン ---------- */
.login { padding: 48px 16px; }
.login-card {
  max-width: 400px; margin: 0 auto; padding: 24px 20px;
  background: var(--c-surface); border-radius: var(--radius); box-shadow: 0 2px 10px rgba(0,0,0,.06);
}
.login-title { font-size: 22px; margin: 0 0 8px; }
.login-lead { margin: 0 0 16px; color: var(--c-muted); }

/* ---------- 本体レイアウト ---------- */
.app-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; background: var(--c-primary); color: var(--c-primary-text);
}
.app-header-title { flex: 1 1 auto; font-weight: 600; }
.app-header .btn-text { color: var(--c-primary-text); }

.main {
  max-width: 640px; margin: 0 auto; padding: 12px 16px;
  padding-bottom: calc(var(--tab-h) + var(--safe-b) + 24px);
}
body.with-bar .main { padding-bottom: calc(var(--tab-h) + var(--bar-h) + var(--safe-b) + 24px); }

.panel { background: var(--c-surface); border-radius: var(--radius); padding: 16px; }

.field { display: flex; flex-direction: column; gap: 4px; margin: 12px 0; min-width: 0; }
.field-label { font-size: 13px; color: var(--c-muted); }
.field-meta { font-size: 13px; color: var(--c-muted); min-height: 1em; overflow-wrap: anywhere; }
.field-meta.error { color: var(--c-danger); }
.field-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px; }
.field-row .field { margin: 12px 0 0; }

.section-title { font-size: 15px; margin: 20px 0 8px; }

.form-head { display: flex; align-items: center; gap: 8px; }
.doc-status { flex: 1 1 auto; font-size: 14px; color: var(--c-muted); }

.segment { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); margin: 12px 0; border: 1px solid var(--c-primary); border-radius: 8px; overflow: hidden; }
.segment-btn { min-height: 42px; border: 0; background: var(--c-surface); color: var(--c-primary); cursor: pointer; min-width: 0; }
.segment-btn + .segment-btn { border-left: 1px solid var(--c-primary); }
.segment-btn.active { background: var(--c-primary); color: var(--c-primary-text); font-weight: 600; }

.notice { border-radius: var(--radius); padding: 12px; margin: 12px 0; }
.notice p { margin: 0 0 8px; white-space: pre-line; overflow-wrap: anywhere; }
.notice-error { background: var(--c-danger-soft); color: var(--c-danger); }

/* ---------- 明細カード ---------- */
.rows { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.row-card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: 10px 12px; min-width: 0; }
.row-card .field { margin: 8px 0; }
.row-head { display: flex; align-items: center; gap: 8px; }
.row-no { flex: 1 1 auto; font-size: 13px; color: var(--c-muted); }
.row-detail { font-size: 13px; color: var(--c-muted); }
.row-nums { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.row-amount { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding-top: 6px; border-top: 1px dashed var(--c-border); }
.row-amount-value { font-size: 17px; font-weight: 600; }

/* ---------- 合計バー・下部タブ(固定) ---------- */
.total-bar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tab-h) + var(--safe-b));
  height: var(--bar-h);
  padding: 8px 16px; background: var(--c-surface); border-top: 1px solid var(--c-border);
  box-shadow: 0 -2px 8px rgba(0,0,0,.05); z-index: 20;
}
.totals { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; max-width: 640px; margin: 0 auto; }
.total-cell { display: flex; flex-direction: column; min-width: 0; }
.total-label { font-size: 12px; color: var(--c-muted); }
.total-value { font-size: 15px; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.total-grand .total-value { font-size: 18px; color: var(--c-primary); }
.bar-actions { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; max-width: 640px; margin: 6px auto 0; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
  height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--c-surface); border-top: 1px solid var(--c-border); z-index: 21;
}
.tab-btn { border: 0; background: none; color: var(--c-muted); cursor: pointer; min-width: 0; font-size: 14px; }
.tab-btn.active { color: var(--c-primary); font-weight: 700; box-shadow: inset 0 3px 0 var(--c-primary); }

/* ---------- 一覧カード(履歴・マスタ) ---------- */
.toolbar { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.toolbar-row { display: flex; gap: 8px; align-items: center; }
.toolbar-row select { flex: 1 1 0; }
.toolbar-row .btn { flex: 0 0 auto; }
.card-list { display: flex; flex-direction: column; gap: 10px; }
.card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius); padding: 10px 12px; min-width: 0; }
.card.inactive { opacity: .5; }
.card-tap { cursor: pointer; }
.card-line { display: flex; align-items: center; gap: 8px; min-width: 0; }
.card-main { flex: 1 1 auto; font-weight: 600; }
.card-sub { flex: 1 1 auto; font-size: 13px; color: var(--c-muted); }
.card-side { flex: 0 0 auto; font-size: 13px; color: var(--c-muted); }
.card-total { flex: 0 0 auto; font-weight: 600; }
.card-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.badge { flex: 0 0 auto; font-size: 12px; padding: 1px 8px; border-radius: 999px; color: #fff; }
.badge-quote { background: var(--c-quote); }
.badge-invoice { background: var(--c-invoice); }
.badge-draft { background: var(--c-muted); }
.badge-off { background: var(--c-muted); }
.empty { text-align: center; color: var(--c-muted); padding: 24px 0; }

/* ---------- ボトムシート ---------- */
.sheet-overlay { position: fixed; inset: 0; background: var(--c-overlay); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 640px; max-height: 88%;
  display: flex; flex-direction: column;
  background: var(--c-surface); border-radius: 14px 14px 0 0;
  padding-bottom: var(--safe-b);
}
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--c-border); }
.sheet-title { flex: 1 1 auto; font-size: 16px; margin: 0; }
.sheet-body { overflow-y: auto; padding: 4px 16px 16px; -webkit-overflow-scrolling: touch; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }

/* ---------- トースト・ローディング ---------- */
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(var(--tab-h) + var(--safe-b) + 12px);
  max-width: 560px; margin: 0 auto;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-radius: 8px; background: #2a313c; color: #fff; z-index: 60;
}
body.with-bar .toast { bottom: calc(var(--tab-h) + var(--bar-h) + var(--safe-b) + 12px); }
.toast-text { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.toast-action { flex: 0 0 auto; background: none; border: 0; color: #9cc7ff; font-weight: 700; cursor: pointer; padding: 4px; }

.loading { position: fixed; inset: 0; background: rgba(255,255,255,.55); z-index: 50; display: flex; align-items: center; justify-content: center; }
.spinner { width: 40px; height: 40px; border: 4px solid var(--c-primary-soft); border-top-color: var(--c-primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
