* { box-sizing: border-box; margin: 0; padding: 0; }

/* kaitori-pop-tool と共通のデザイントークン */
:root {
  --primary: #e60012;
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #23272f;
  --muted: #7a8190;
  --border: #dde1e8;
  --ok: #1a9c4b;
  --warn: #e08a00;
  --err: #d43333;
  --appbar-h: 52px;
}

html, body { height: 100%; }
body {
  font-family: 'Noto Sans JP', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 14px; }

/* ---------- ヘッダー ---------- */
header.appbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: #fff;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  height: var(--appbar-h);
  white-space: nowrap;
  overflow-x: auto;
}
header.appbar h1 {
  font-size: 16px;
  font-weight: 900;
  color: var(--primary);
  white-space: nowrap;
}
header.appbar .spacer { flex: 1; }
header.appbar a {
  color: var(--muted);
  text-decoration: none;
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
}
header.appbar a:hover { color: var(--text); }

.totals-live {
  display: flex;
  gap: 12px;
  align-items: center;
  font-weight: 700;
  white-space: nowrap;
  background: #fdeaec;
  border-radius: 999px;
  padding: 4px 14px;
  font-size: 13px;
}
.totals-live .amount { font-size: 15px; color: var(--primary); font-weight: 900; }

main { max-width: 1500px; margin: 0 auto; padding: 16px; }
main.narrow { max-width: 1000px; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }

/* ---------- ボタン ---------- */
button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}
button:hover { background: #f2f4f7; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
button.primary:hover { background: #c50010; }
button.danger { color: var(--err); border-color: #f2c6c6; }
button.danger:hover { background: #fbe3e3; }
button.small { padding: 4px 10px; font-size: 12px; }

label.field { display: block; margin-bottom: 10px; }
label.field span.lbl {
  display: block;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 4px;
}

input[type="text"], input[type="date"], input[type="number"], select, textarea {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid #f5b5ba;
  outline-offset: 0;
  border-color: var(--primary);
}
textarea { resize: vertical; }

.header-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr;
  gap: 12px;
}
@media (max-width: 800px) {
  .header-grid { grid-template-columns: 1fr 1fr; }
}

/* ---------- 明細テーブル ----------
   見出し行はテーブル枠内スクロール + sticky で常に表示される。
   (ページ全体のstickyはoverflowラッパー内で効かないため枠内方式にする) */
.table-wrap {
  overflow: auto;
  max-height: calc(100vh - var(--appbar-h) - 150px);
  min-height: 320px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}
table.items {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  min-width: 980px;
}
table.items th {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #fafbfc;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  padding: 8px 6px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.items td {
  border-bottom: 1px solid #eef0f3;
  padding: 2px;
  vertical-align: middle;
  background: #fff;
}
table.items td input, table.items td textarea {
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
  width: 100%;
  background: transparent;
}
table.items td input:focus {
  border-color: var(--primary);
  outline: 2px solid #f5b5ba;
  background: #fff;
}
table.items td.num input { text-align: right; }
table.items td.total-cell {
  text-align: right;
  padding-right: 10px;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}
table.items tr.invalid td { background: #fbe3e3; }
/* 空白行がカテゴリ区切りとして機能している行(PDFではここに小計が入る) */
table.items tr.break-row td { background: #f7f8fa; border-bottom: 1px solid var(--muted); }
table.items tr.break-row td.total-cell { color: var(--muted); font-size: 11px; }
table.items tr.category-row td { background: #fdeaec; font-weight: 700; }
table.items tr.category-row td input { font-weight: 700; }
table.items td.rowno { text-align: center; color: var(--muted); font-size: 11px; width: 34px; }
table.items td.sel { text-align: center; width: 32px; }
table.items td.sel input[type="checkbox"],
table.items th input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; accent-color: var(--primary); }
table.items td.ops { white-space: nowrap; text-align: center; }
table.items td.ops button {
  border: none;
  background: none;
  padding: 4px 7px;
  font-size: 15px;
  color: var(--muted);
}
table.items td.ops button:hover { color: var(--text); background: #eef0f3; border-radius: 6px; }

.row-error-note { color: var(--err); font-size: 12px; font-weight: 700; margin: 6px 0; }

/* ---------- 一覧テーブル ---------- */
table.list { width: 100%; border-collapse: collapse; }
table.list th, table.list td {
  border-bottom: 1px solid var(--border);
  padding: 10px 8px;
  text-align: left;
  vertical-align: middle;
}
table.list th { font-size: 12px; color: var(--muted); background: #fafbfc; white-space: nowrap; }
table.list td { font-size: 13px; }
table.list td a { color: var(--text); font-weight: 700; text-decoration: none; }
table.list td a:hover { color: var(--primary); }
table.list td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.list tr.deleted td { color: #a6adba; background: #fafafa; }
table.list td.ops { white-space: nowrap; }

.status-note { font-size: 12px; color: var(--muted); min-width: 120px; font-weight: 700; }
.status-note.error { color: var(--err); }

/* ---------- モーダル ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 32, .5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px 12px;
  overflow-y: auto;
  z-index: 200;
}
.modal {
  background: #fff;
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 640px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25);
}
.modal h2 { margin-bottom: 14px; font-size: 16px; }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.company-row { display: flex; gap: 6px; align-items: center; padding: 3px 0; }
.company-row input { flex: 1; }

.hidden { display: none !important; }

.scroll-hint { display: none; }

/* ---------- スマホ ---------- */
@media (max-width: 700px) {
  html { --appbar-h: 108px; }
  header.appbar {
    gap: 8px;
    padding: 8px;
    height: auto;
    min-height: var(--appbar-h);
    flex-wrap: wrap;
    row-gap: 8px;
    overflow-x: visible;
    white-space: normal;
  }
  header.appbar h1 { font-size: 13px; }
  /* spacer を幅100%にして、それ以降の要素(ボタン等)を次の行に折り返す */
  header.appbar .spacer { flex: 1 1 100%; height: 0; }
  header.appbar .totals-live { order: 1; }
  main { padding: 8px; }
  .totals-live { font-size: 12px; padding: 3px 10px; }
  .totals-live .amount { font-size: 13px; }
  .table-wrap { max-height: calc(100vh - var(--appbar-h) - 120px); }

  /* 検索・絞り込みは縦に並べて全幅にし、タップしやすくする */
  #search-text, #filter-company, #sort-order {
    max-width: none !important;
    width: 100%;
  }

  /* 貼り付け説明文が長いので編集画面の横スクロール案内を表示 */
  .scroll-hint { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }

  /* Excel貼り付けの説明はスマホでは不要なので隠す */
  .paste-hint { display: none; }

  /* 入力欄の文字を16px以上にする(小さいとiPhoneが勝手に画面を拡大してしまう) */
  input[type="text"], input[type="date"], input[type="number"], select, textarea,
  table.items td input, table.items td textarea {
    font-size: 16px;
  }

  /* 指で押しやすいようにボタンを大きくする */
  button { padding: 10px 14px; min-height: 44px; font-size: 14px; }
  button.small { padding: 8px 12px; min-height: 38px; font-size: 13px; }
  table.items td.ops button { padding: 8px 10px; min-height: auto; font-size: 17px; }
  table.items td.sel input[type="checkbox"],
  table.items th input[type="checkbox"] { width: 20px; height: 20px; }

  /* 編集画面: ランク・コンディション列を少し狭めて横スクロール量を減らす */
  table.items { min-width: 860px; }
  table.items th:nth-child(4), table.items th:nth-child(5) { width: 130px !important; }

  /* 編集画面ヘッダーの保存・PDFボタンを横幅いっぱいに広げて押しやすくする */
  header.appbar button { flex: 1 1 auto; justify-content: center; }
  .status-note { min-width: 0; }

  /* 編集画面: 会社名の入力欄は横幅いっぱいにする */
  .header-grid label.field:first-child { grid-column: 1 / -1; }

  /* 一覧画面: 管理ボタン入りヘッダーを画面上部に固定せず、スクロールで隠れるようにする
     (スマホの狭い画面を一覧表示に使えるようにするため) */
  body.page-list header.appbar { position: static; }

  /* ---------- 一覧テーブルをカード表示に変える ---------- */
  table.list thead { display: none; }
  table.list, table.list tbody, table.list tr, table.list td { display: block; width: 100%; }
  table.list tr {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 10px;
    background: #fff;
  }
  table.list tr.deleted { background: #fafafa; }
  table.list td {
    border-bottom: none;
    padding: 6px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
  }
  table.list td::before {
    content: attr(data-label);
    font-size: 11px;
    color: var(--muted);
    font-weight: 700;
    flex-shrink: 0;
  }
  table.list td a { font-size: 15px; }
  table.list td.ops {
    justify-content: flex-start;
    flex-wrap: wrap;
    padding-top: 10px;
    margin-top: 4px;
    border-top: 1px solid #eef0f3;
  }
  table.list td.ops::before { display: none; }
}

/* ---------- 一括取込モーダル ---------- */
.modal.import-modal { max-width: 920px; }
.import-opt {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 14px;
  margin-bottom: 10px;
  font-size: 13px;
}
.import-opt .opt-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  min-width: 76px;
}
.import-opt label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.import-opt input[type="radio"] { width: auto; accent-color: var(--primary); }
#import-preview td { font-size: 12px; padding: 4px 6px; }
#import-preview td.num { text-align: right; font-variant-numeric: tabular-nums; }
#import-preview tr.warn td { background: #fff7e6; }
#import-preview tr.miss td { background: #fbe3e3; }
.pill { display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.pill.ok { background: #e2f4e8; color: var(--ok); }
.pill.warn { background: #fbf0dc; color: var(--warn); }
.pill.miss { background: #fbe3e3; color: var(--err); }
