/* ------------------------------------------------------------------
   併売ツール — モバイルファースト（iPhone の Safari / ホーム画面アプリ想定）
   ------------------------------------------------------------------ */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #dfe1e6;
  --text: #16181d;
  --text-dim: #6b7280;
  --accent: #d2002e;
  --accent-soft: #ffe9ed;
  --ok: #0a7d3f;
  --warn: #a35b00;
  --err: #c02121;
  --radius: 14px;
  --tap: 44px; /* iOS のヒットターゲット下限 */
  --header-h: 52px;
  --tabbar-h: 56px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1116;
    --surface: #181b22;
    --surface-2: #21252e;
    --border: #2c313c;
    --text: #e8eaed;
    --text-dim: #9aa1ad;
    --accent: #ff4d6a;
    --accent-soft: #3a1620;
    --ok: #3ecf7a;
    --warn: #e0a33a;
    --err: #ff6b6b;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  /* ホーム画面アプリでの引っぱり更新・ラバーバンドを抑える */
  overscroll-behavior-y: none;
  touch-action: manipulation;
}

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

/* iOS は 16px 未満の入力欄でフォーカス時に自動ズームするので下限を決める */
input, select, textarea { font-size: 16px; }

/* ---------- レイアウト ---------- */

.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  height: calc(var(--header-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 14px 0;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}

.header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: .01em;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.main {
  flex: 1;
  padding: 12px 14px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
}

.tabbar button {
  border: 0;
  background: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 11px;
  color: var(--text-dim);
  cursor: pointer;
  position: relative;
}
.tabbar button .glyph { font-size: 19px; line-height: 1; }
.tabbar button[aria-selected="true"] { color: var(--accent); font-weight: 600; }
.tabbar .badge {
  position: absolute;
  top: 6px;
  margin-left: 22px;
  min-width: 17px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 17px;
}

/* ---------- 汎用パーツ ---------- */

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

.section-title {
  margin: 20px 0 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .06em;
  color: var(--text-dim);
}
.section-title:first-child { margin-top: 4px; }

.btn {
  min-height: var(--tap);
  padding: 0 16px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--err); border-color: color-mix(in srgb, var(--err) 40%, var(--border)); }
.btn.sm { min-height: 34px; padding: 0 11px; font-size: 13px; border-radius: 9px; }
.btn:disabled { opacity: .45; cursor: default; }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.tight { gap: 6px; }
.spacer { flex: 1; }

.field { margin-bottom: 12px; }
.field > label {
  display: block;
  margin-bottom: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}
.field input, .field select, .field textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface-2);
  -webkit-appearance: none;
  appearance: none;
}
.field textarea { min-height: 120px; resize: vertical; line-height: 1.55; }
.field select {
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
                    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 34px;
}
.field .hint { margin-top: 4px; font-size: 11.5px; color: var(--text-dim); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; -webkit-overflow-scrolling: touch; }
.chip {
  flex: 0 0 auto;
  min-height: 34px;
  padding: 0 13px;
  border-radius: 17px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 13px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--text-dim);
  white-space: nowrap;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.listed { color: var(--ok); background: color-mix(in srgb, var(--ok) 13%, transparent); }
.pill.queued { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); }
.pill.error, .pill.failed { color: var(--err); background: color-mix(in srgb, var(--err) 13%, transparent); }
.pill.sold { color: #fff; background: var(--accent); }

.empty { padding: 48px 16px; text-align: center; color: var(--text-dim); }
.empty .big { font-size: 34px; margin-bottom: 8px; }

/* ---------- 商品一覧 ---------- */

.item-card {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-bottom: 10px;
  cursor: pointer;
}
.item-card.selected { border-color: var(--accent); background: var(--accent-soft); }
.item-card .thumb {
  width: 72px; height: 72px;
  flex: 0 0 auto;
  border-radius: 10px;
  object-fit: cover;
  background: var(--surface-2);
}
.item-card .body { min-width: 0; flex: 1; }
.item-card .title {
  font-weight: 600;
  font-size: 14px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.item-card .meta { margin-top: 4px; font-size: 12px; color: var(--text-dim); }
.item-card .badges { margin-top: 6px; display: flex; gap: 4px; flex-wrap: wrap; }
.item-card .check {
  width: 24px; height: 24px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: grid;
  place-items: center;
  font-size: 13px;
  color: transparent;
  align-self: center;
}
.item-card.selected .check { background: var(--accent); border-color: var(--accent); color: #fff; }

.bulkbar {
  position: fixed;
  left: 10px; right: 10px;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 10px);
  z-index: 25;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0,0,0,.18);
  padding: 10px;
  display: flex;
  gap: 8px;
  align-items: center;
  max-width: 880px;
  margin: 0 auto;
}
.bulkbar .count { font-weight: 700; font-size: 14px; padding-left: 4px; }

/* ---------- 詳細シート ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  animation: sheet-in .22s ease;
}
@keyframes sheet-in { from { transform: translateY(3%); opacity: 0; } }

.sheet-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.sheet-header .title { font-weight: 700; flex: 1; font-size: 15px; }
.sheet-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 14px calc(env(safe-area-inset-bottom) + 90px);
}
.sheet-footer {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  padding: 10px 14px calc(env(safe-area-inset-bottom) + 10px);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.sheet-footer .btn { flex: 1; }

.photostrip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
  margin-bottom: 12px;
}
.photostrip img, .photostrip .add {
  width: 96px; height: 96px;
  flex: 0 0 auto;
  border-radius: 12px;
  object-fit: cover;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.photostrip .add {
  display: grid;
  place-items: center;
  font-size: 26px;
  color: var(--text-dim);
  cursor: pointer;
}

table.matrix { width: 100%; border-collapse: collapse; font-size: 13px; }
table.matrix th, table.matrix td { padding: 8px 6px; text-align: right; border-bottom: 1px solid var(--border); }
table.matrix th:first-child, table.matrix td:first-child { text-align: left; }
table.matrix th { font-size: 11px; color: var(--text-dim); font-weight: 600; }
table.matrix tbody tr:last-child td { border-bottom: 0; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- 一覧表 ---------- */

.tablewrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

table.ledger {
  border-collapse: collapse;
  font-size: 12.5px;
  white-space: nowrap;
  min-width: 100%;
}
table.ledger th, table.ledger td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
table.ledger thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 700;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}
table.ledger thead th .arrow { opacity: .5; font-size: 9px; }
table.ledger tbody tr { cursor: pointer; }
table.ledger tbody tr:hover { background: var(--surface-2); }
table.ledger tbody tr:last-child td { border-bottom: 0; }
table.ledger td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.ledger td.name {
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 600;
  white-space: nowrap;
}
table.ledger .muted { color: var(--text-dim); }
table.ledger .pos { color: var(--ok); font-weight: 700; }
table.ledger .neg { color: var(--err); font-weight: 700; }

/* 先頭2列（SKU・商品名）は横スクロールしても残す */
@media (min-width: 700px) {
  table.ledger th:nth-child(1), table.ledger td:nth-child(1) { position: sticky; left: 0; background: var(--surface); z-index: 1; }
  table.ledger th:nth-child(2), table.ledger td:nth-child(2) { position: sticky; left: 96px; background: var(--surface); z-index: 1; }
  table.ledger thead th:nth-child(1), table.ledger thead th:nth-child(2) { background: var(--surface-2); z-index: 2; }
  table.ledger tbody tr:hover td:nth-child(1), table.ledger tbody tr:hover td:nth-child(2) { background: var(--surface-2); }
}

/* ---------- 集計カード ---------- */

.stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
}
.stat .k { font-size: 11px; color: var(--text-dim); font-weight: 600; }
.stat .v { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat.accent .v { color: var(--accent); }
.stat.ok .v { color: var(--ok); }
@media (min-width: 560px) { .stats { grid-template-columns: repeat(4, 1fr); } }

/* ---------- 在庫 ---------- */

.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button {
  width: 42px; height: 42px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 19px; font-weight: 700;
  cursor: pointer;
}
.stepper .qty {
  min-width: 62px;
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stepper .qty.low { color: var(--err); }

.history { font-size: 12px; }
.history div {
  display: flex;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  color: var(--text-dim);
}
.history div:last-child { border-bottom: 0; }
.history .d { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 34px; }
.history .d.plus { color: var(--ok); }
.history .d.minus { color: var(--err); }

/* ---------- 売上 ---------- */

.sale {
  display: flex;
  gap: 10px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 11px;
  margin-bottom: 8px;
  cursor: pointer;
}
.sale.selected { border-color: var(--accent); background: var(--accent-soft); }
.sale .body { flex: 1; min-width: 0; }
.sale .t { font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sale .s { font-size: 11.5px; color: var(--text-dim); margin-top: 2px; }
.sale .amt { text-align: right; font-variant-numeric: tabular-nums; }
.sale .amt .net { font-weight: 700; font-size: 14px; }
.sale .amt .profit { font-size: 11px; color: var(--text-dim); }
.pill.pending { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); }
.pill.requested { color: #0b62c4; background: color-mix(in srgb, #0b62c4 14%, transparent); }
.pill.paid { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }

.note { padding: 9px 11px; border-radius: 10px; font-size: 12.5px; margin-bottom: 8px; }
.note.warn { background: color-mix(in srgb, var(--warn) 13%, transparent); color: var(--warn); }
.note.err  { background: color-mix(in srgb, var(--err) 12%, transparent); color: var(--err); }
.note.info { background: var(--surface-2); color: var(--text-dim); }

/* ---------- タスク ---------- */

.task {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 11px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 8px;
}
.task .body { flex: 1; min-width: 0; }
.task .body .t { font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.task .body .s { font-size: 11.5px; color: var(--text-dim); margin-top: 2px; }

/* ---------- トースト ---------- */

.toasts {
  position: fixed;
  left: 12px; right: 12px;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: #22262f;
  color: #fff;
  padding: 11px 14px;
  border-radius: 12px;
  font-size: 13.5px;
  box-shadow: 0 8px 24px rgba(0,0,0,.28);
  animation: toast-in .2s ease;
}
.toast.err { background: var(--err); }
.toast.ok { background: var(--ok); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* ---------- デスクトップ ---------- */

@media (min-width: 760px) {
  .main { padding-bottom: 40px; padding-top: 18px; }
  .tabbar {
    position: sticky;
    top: calc(var(--header-h) + env(safe-area-inset-top));
    bottom: auto;
    max-width: 900px;
    margin: 0 auto;
    grid-template-columns: repeat(5, minmax(0, 150px));
    justify-content: start;
    height: 46px;
    border-top: 0;
    border-bottom: 1px solid var(--border);
    background: transparent;
    backdrop-filter: none;
    padding: 0;
  }
  .tabbar button { flex-direction: row; gap: 7px; font-size: 13.5px; }
  .tabbar button .glyph { font-size: 15px; }
  .tabbar .badge { position: static; margin-left: 0; }
  .bulkbar { bottom: 18px; }
  .toasts { bottom: 18px; left: auto; right: 18px; width: 340px; }
  .items-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
  .sheet {
    inset: 5vh auto 5vh 50%;
    transform: translateX(-50%);
    width: min(720px, 92vw);
    border-radius: 18px;
    overflow: hidden;
    border: 1px solid var(--border);
    box-shadow: 0 24px 70px rgba(0, 0, 0, .3);
  }
  @keyframes sheet-in { from { transform: translateX(-50%) translateY(2%); opacity: 0; } }
}
