:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #eff6ff;
  --success: #16a34a;
  --success-light: #f0fdf4;
  --warning: #d97706;
  --warning-light: #fffbeb;
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --bg: #f1f5f9;
  --card-bg: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-secondary: #64748b;
  --header-height: 60px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

/* ===== HEADER ===== */
header {
  background: var(--primary);
  color: white;
  height: var(--header-height);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.header-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  height: 100%;
  display: flex;
  align-items: center;
  gap: 16px;
}
header h1 { font-size: 18px; font-weight: 700; }
.upload-status { font-size: 13px; opacity: 0.85; }
.app-version { font-size: 11px; opacity: 0.6; white-space: nowrap; }

/* ===== UPLOAD BAR ===== */
.upload-bar {
  background: white;
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  gap: 12px;
  max-width: 1400px;
  margin: 0 auto;
}
.upload-zone {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border: 2px dashed var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  min-width: 220px;
  background: var(--bg);
  position: relative;
}
.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--primary);
  background: var(--primary-light);
}
.upload-zone.loaded {
  border-color: var(--success);
  background: var(--success-light);
  border-style: solid;
}
.upload-icon { font-size: 20px; }
.upload-label { font-size: 13px; line-height: 1.3; }
.upload-label small { color: var(--text-secondary); }
.upload-badge {
  margin-left: auto;
  background: var(--success);
  color: white;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
}
.upload-badge.secondary { background: var(--primary); }

/* ===== TABS ===== */
.tabs {
  background: white;
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  display: flex;
  gap: 4px;
  position: sticky;
  top: var(--header-height);
  z-index: 90;
}
.tab {
  padding: 12px 18px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  border-bottom: 3px solid transparent;
  margin-bottom: -1px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.tab:hover:not(.disabled) { color: var(--primary); }
.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}
.tab.disabled { opacity: 0.45; cursor: default; }
.soon {
  font-size: 10px;
  background: var(--bg);
  color: var(--text-secondary);
  padding: 1px 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.badge-count {
  background: var(--danger);
  color: white;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

/* ===== TAB CONTENT ===== */
.tab-content { display: none; padding: 20px; max-width: 1400px; margin: 0 auto; }
.tab-content.active { display: block; }

/* ===== TOOLBAR ===== */
.toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

/* ===== MONTH SELECTOR ===== */
.month-selector {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ===== VIEW TOGGLE ===== */
.view-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.view-btn {
  padding: 6px 16px;
  border: none;
  background: white;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all 0.15s;
}
.view-btn + .view-btn {
  border-left: 1px solid var(--border);
}
.view-btn.active {
  background: var(--primary);
  color: white;
}
.view-btn:not(.active):hover {
  background: var(--primary-light);
  color: var(--primary);
}
.month-btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: white;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all 0.2s;
}
.month-btn:hover { border-color: var(--primary); color: var(--primary); }
.month-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* ===== SUMMARY CARDS ===== */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
@media (max-width: 1000px) {
  .summary-cards { grid-template-columns: repeat(3, 1fr); }
}
.card {
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  text-align: center;
}
.card.profit {
  background: var(--success-light);
  border-color: #86efac;
}
.card-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.card-value {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}
.card.profit .card-value { color: var(--success); }

/* ===== TABLE ===== */
.table-wrap {
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
thead {
  background: #f8fafc;
}
th {
  padding: 10px 12px;
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
th.num, td.num { text-align: right; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--primary); }
th.sort-asc::after { content: " ↑"; color: var(--primary); }
th.sort-desc::after { content: " ↓"; color: var(--primary); }
td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fafc; }
.td-name {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.td-sku {
  font-family: monospace;
  font-size: 11px;
  color: var(--text-secondary);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.profit-pos { color: var(--success); font-weight: 600; }
.profit-neg { color: var(--danger); font-weight: 600; }

/* クリック可能セル */
.td-copy { cursor: pointer; }
.td-copy:hover { background: #f8fafc !important; }

/* テキストポップアップ */
.text-popup {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.text-popup-inner {
  background: white;
  border-radius: 12px;
  padding: 20px;
  max-width: 480px;
  width: 100%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}
.text-popup-body {
  font-size: 15px;
  line-height: 1.6;
  word-break: break-all;
  margin-bottom: 16px;
  color: var(--text);
}
.text-popup-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}
.text-popup-copy {
  padding: 8px 18px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}
.text-popup-copy:hover { background: var(--primary-dark); }
.text-popup-close {
  padding: 8px 18px;
  background: var(--bg);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
}

/* ===== SOURCE BADGES ===== */
.source-badge {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}
.src-pricer  { background: #dbeafe; color: #1d4ed8; }
.src-manual  { background: #fed7aa; color: #c2410c; }
.src-unknown { background: #fee2e2; color: #b91c1c; }

/* ===== RATE COLOR ===== */
.rate-high { color: var(--success); font-weight: 600; }
.rate-mid  { color: var(--warning); font-weight: 600; }
.rate-low  { color: var(--danger); font-weight: 600; }

/* ===== MISSING SECTION ===== */
.section-header { margin-bottom: 16px; }
.section-header h2 { font-size: 16px; margin-bottom: 6px; }
.section-desc { color: var(--text-secondary); font-size: 13px; }

.cost-input-wrap { display: flex; gap: 8px; align-items: center; }
.cost-input {
  width: 110px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  text-align: right;
}
.cost-input:focus { outline: none; border-color: var(--primary); }
.save-btn {
  padding: 6px 14px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.2s;
}
.save-btn:hover { background: var(--primary-dark); }
.save-btn:disabled { background: #94a3b8; cursor: default; }
.saved-label {
  color: var(--success);
  font-size: 12px;
  font-weight: 600;
}

/* ===== EMPTY STATE ===== */
.empty-state {
  padding: 60px 20px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 15px;
}

/* ===== DASHBOARD ===== */
.dash-section {
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 20px;
  margin-bottom: 16px;
}
.dash-section-header {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.dash-section-header .dash-section-title {
  margin-bottom: 0;
  flex-shrink: 0;
}
.dash-section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
}
.dash-cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 12px;
}
@media (max-width: 1000px) {
  .dash-cards { grid-template-columns: repeat(3, 1fr); }
}
.dash-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  text-align: center;
}
.dash-card.highlight {
  background: var(--success-light);
  border-color: #86efac;
}
.dash-card-label {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.dash-card-value {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.dash-card-value.large { font-size: 20px; }
.dash-card.highlight .dash-card-value { color: var(--success); }

.chart-wrap {
  overflow-x: auto;
}
.profit-chart {
  min-width: 400px;
  width: 100%;
}
.profit-chart svg {
  width: 100%;
  height: 220px;
  display: block;
}

.dash-alert {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--warning-light);
  border: 1px solid var(--warning);
  border-radius: 10px;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  z-index: 200;
  font-size: 14px;
  white-space: nowrap;
}
.dash-alert-icon { font-size: 18px; }
.dash-alert-btn {
  padding: 5px 14px;
  background: var(--warning);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}

/* ===== CASHFLOW ===== */
.cf-row {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.cf-panel {
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 20px;
  margin-bottom: 16px;
}
.cf-row .cf-panel { margin-bottom: 0; flex: 1; min-width: 260px; }
.cf-panel-grow { flex: 2 !important; }
.cf-panel-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
}
.cf-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.cf-panel-header .cf-panel-title { margin-bottom: 0; }

.cf-balance-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.cf-yen { color: var(--text-secondary); font-size: 15px; }
.cf-balance-input {
  width: 160px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 16px;
  font-weight: 700;
  text-align: right;
}
.cf-balance-input:focus { outline: none; border-color: var(--primary); }
.cf-balance-date { font-size: 11px; color: var(--text-secondary); }
.cf-save-btn, .cf-add-btn {
  padding: 7px 14px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.2s;
}
.cf-save-btn:hover, .cf-add-btn:hover { background: var(--primary-dark); }

.cf-add-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.cf-date-input {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
}
.cf-amount-input {
  width: 130px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  text-align: right;
}
.cf-date-input:focus, .cf-amount-input:focus { outline: none; border-color: var(--primary); }

.cf-payout-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.cf-payout-item:last-of-type { border-bottom: none; }
.cf-payout-date { color: var(--text-secondary); min-width: 80px; }
.cf-payout-amt  { font-weight: 600; color: var(--success); }
.cf-del-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 4px;
}
.cf-del-btn:hover { background: var(--danger-light); color: var(--danger); }

.cf-card-name-input {
  width: 160px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 13px;
}
.cf-day-input {
  width: 60px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 13px;
  text-align: center;
}
.cf-bill-input {
  width: 110px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  font-size: 13px;
  text-align: right;
}

.cf-warning-item {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 13px;
  background: var(--danger-light);
  border: 1px solid #fca5a5;
  color: var(--danger);
  font-weight: 500;
}
.cf-warning-ok {
  background: var(--success-light);
  border-color: #86efac;
  color: var(--success);
}

.cf-event-in  { color: var(--success); font-weight: 600; }
.cf-event-out { color: var(--danger);  font-weight: 600; }
.cf-balance-danger { color: var(--danger); font-weight: 700; }
.cf-balance-warn   { color: var(--warning); font-weight: 600; }

/* ===== COMING SOON ===== */
.coming-soon {
  text-align: center;
  padding: 80px 20px;
  color: var(--text-secondary);
}
.coming-icon { font-size: 48px; margin-bottom: 12px; }
.coming-soon h2 { font-size: 20px; color: var(--text); margin-bottom: 10px; }

/* ===== TOAST ===== */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: #1e293b;
  color: white;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  z-index: 999;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2);
  animation: slide-in 0.3s ease;
}
.toast.success { background: var(--success); }
.toast.error   { background: var(--danger); }
@keyframes slide-in {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* ===== MOBILE ===== */
@media (max-width: 640px) {
  /* ヘッダー */
  .header-inner { flex-wrap: wrap; gap: 4px; padding: 0 12px; }
  header h1 { font-size: 15px; }
  .app-version { font-size: 10px; }
  .upload-status { font-size: 11px; width: 100%; }

  /* アップロードバー */
  .upload-bar { padding: 8px 12px; }
  .upload-zone { min-width: 0; flex: 1; padding: 8px 10px; }
  .upload-label { font-size: 12px; }

  /* タブ：横スクロール */
  .tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 0 12px; gap: 0; }
  .tab { padding: 10px 12px; font-size: 13px; white-space: nowrap; }

  /* コンテンツ余白 */
  .tab-content { padding: 12px; }

  /* ツールバー縦並び */
  .toolbar { flex-direction: column; align-items: stretch; gap: 8px; }
  .month-selector { flex-wrap: wrap; }
  .month-btn { font-size: 12px; padding: 5px 10px; }

  /* サマリーカード：2列 */
  .summary-cards { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .card { padding: 10px 12px; }
  .card-value { font-size: 15px; }

  /* テーブル文字小さく */
  table { font-size: 12px; }
  th, td { padding: 7px 8px; }
  .td-name { max-width: 140px; }
  .td-sku  { max-width: 100px; }

  /* ダッシュボード */
  .dash-section { padding: 12px; }
  .dash-cards { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .dash-section-header { flex-direction: column; align-items: flex-start; gap: 8px; }
  .dash-card-value.large { font-size: 16px; }

  /* キャッシュフロー */
  .cf-row { flex-direction: column; }
  .cf-panel { padding: 12px 14px; }
  .cf-balance-input { width: 130px; font-size: 14px; }
  .cf-add-row { gap: 6px; }
  .cf-date-input { width: 130px; }
  .cf-amount-input { width: 100px; }

  /* カードテーブル：スクロール */
  #cf-cards-wrap table { min-width: 520px; }
  .cf-card-name-input { width: 120px; }
  .cf-day-input  { width: 44px; }
  .cf-bill-input { width: 90px; }

  /* トースト */
  .toast { right: 12px; left: 12px; bottom: 16px; text-align: center; }

  /* アラート */
  .dash-alert { white-space: normal; width: calc(100% - 24px); }
}
