/* ==================== BASE ==================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #E8533A;
  --primary-light: #fff0ed;
  --green: #22C55E;
  --red: #EF4444;
  --orange: #F97316;
  --bg: #F2F2F7;
  --card: #FFFFFF;
  --border: rgba(0,0,0,0.09);
  --text: #1A1A1A;
  --text2: #6B7280;
  --text3: #9CA3AF;
  --nav-h: 56px;
  --header-h: 52px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body { height: 100%; overflow: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Hiragino Kaku Gothic ProN',
    'YuGothic', 'Yu Gothic', sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ==================== SCREENS ==================== */
.screen { display: none; height: 100%; }
.screen.active { display: flex; flex-direction: column; }

/* ==================== AUTH ==================== */
.auth-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
  background: var(--bg);
}

.auth-logo {
  text-align: center;
  margin-bottom: 32px;
}

.auth-logo-icon {
  width: 72px; height: 72px;
  background: var(--primary);
  color: #fff;
  font-size: 36px;
  font-weight: 800;
  border-radius: 18px;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 12px;
  box-shadow: 0 4px 16px rgba(232,83,58,0.35);
}

.auth-logo-text {
  font-size: 26px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.5px;
}

.auth-logo-sub {
  font-size: 13px;
  color: var(--text2);
  margin-top: 4px;
}

.auth-card { width: 100%; max-width: 360px; padding: 20px; }

.auth-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}

.auth-tab {
  flex: 1;
  padding: 10px;
  background: none;
  border: none;
  font-size: 15px;
  color: var(--text2);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .2s, border-color .2s;
}

.auth-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

.auth-error {
  color: var(--red);
  font-size: 13px;
  text-align: center;
  margin-top: 8px;
  min-height: 18px;
}

/* ==================== HEADER ==================== */
#app-header {
  height: var(--header-h);
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  position: sticky;
  top: 0;
  z-index: 100;
  flex-shrink: 0;
}

.header-back {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 16px;
  cursor: pointer;
  padding: 8px 4px;
  min-width: 60px;
}

#header-title {
  flex: 1;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  color: var(--primary);
}

#header-actions {
  display: flex;
  gap: 8px;
  min-width: 60px;
  justify-content: flex-end;
}

.btn-header {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

/* ==================== BOTTOM NAV ==================== */
#bottom-nav {
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: #fff;
  border-top: 1px solid var(--border);
  display: flex;
  flex-shrink: 0;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  font-size: 10px;
  padding: 6px 0;
  transition: color .2s;
}

.nav-item svg {
  width: 22px; height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.nav-item.active { color: var(--primary); }

/* ==================== CONTENT AREA ==================== */
#app-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.tab-pane {
  display: none;
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 16px;
}

.tab-pane.active { display: block; }

/* ==================== CARDS ==================== */
.card {
  background: var(--card);
  border-radius: 14px;
  margin: 10px 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

/* ==================== BUTTONS ==================== */
button { cursor: pointer; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 20px;
  border-radius: 10px;
  border: none;
  font-size: 15px;
  font-weight: 600;
  transition: opacity .15s;
}

.btn:active { opacity: .7; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-danger { background: #FEE2E2; color: var(--red); }
.btn-outline { background: #fff; color: var(--primary); border: 1.5px solid var(--primary); }
.btn-block { width: 100%; }

/* ==================== FORM ==================== */
.form-input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  color: var(--text);
  background: #FAFAFA;
  outline: none;
  transition: border-color .2s;
  -webkit-appearance: none;
}

.form-input:focus { border-color: var(--primary); background: #fff; }
.form-textarea { min-height: 80px; resize: vertical; }
.form-select { background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236B7280' stroke-width='2'%3e%3cpolyline points='6 9 12 15 18 9'/%3e%3c/svg%3e"); background-repeat: no-repeat; background-position: right 12px center; background-size: 16px; padding-right: 36px; }

.form-row { margin-bottom: 14px; }
.form-label { display: block; font-size: 13px; font-weight: 600; color: var(--text2); margin-bottom: 6px; }
.required { color: var(--red); }

.form-section { padding: 16px; }

/* ==================== SUMMARY GRID ==================== */
.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  margin: 10px 14px;
}

.summary-cell {
  background: #fff;
  padding: 12px 8px;
  text-align: center;
}

.summary-cell .label {
  font-size: 11px;
  color: var(--text2);
  margin-bottom: 4px;
}

.summary-cell .value {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  word-break: break-all;
}

.summary-cell .value.green { color: var(--green); }
.summary-cell .value.red { color: var(--red); }

/* ==================== SUB TABS ==================== */
.sub-tabs {
  display: flex;
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  gap: 0;
}

.sub-tab {
  flex: 1;
  padding: 12px 4px;
  background: none;
  border: none;
  font-size: 14px;
  color: var(--text2);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .2s, border-color .2s;
}

.sub-tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ==================== MONTH NAV ==================== */
.month-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 12px 16px;
  background: #fff;
}

.month-nav button {
  background: none;
  border: none;
  font-size: 22px;
  color: var(--primary);
  padding: 4px 10px;
  line-height: 1;
}

.month-nav span {
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  min-width: 130px;
  text-align: center;
}

/* ==================== CHART ==================== */
.chart-card {
  padding: 14px 12px 10px;
}

.chart-label {
  font-size: 13px;
  color: var(--text2);
  margin-bottom: 10px;
}

#monthly-chart { width: 100%; display: block; }

.section-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 18px;
  background: #fff;
  margin: 0 14px;
  border-radius: 10px;
  margin-top: 8px;
}

.section-title { font-size: 14px; color: var(--text2); }
.section-value { font-size: 15px; font-weight: 600; }

/* ==================== YEARLY TABLE ==================== */
.yearly-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.yearly-table th, .yearly-table td {
  padding: 10px 10px;
  text-align: right;
  border-bottom: 1px solid var(--border);
}

.yearly-table th {
  color: var(--text2);
  font-weight: 600;
  font-size: 12px;
  background: #FAFAFA;
}

.yearly-table td:first-child, .yearly-table th:first-child {
  text-align: left;
}

.yearly-table .profit-cell { color: var(--green); font-weight: 600; }
.yearly-table .loss-cell { color: var(--red); font-weight: 600; }

/* ==================== RANKING ==================== */
.ranking-ctrl {
  background: #fff;
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
}

.rank-type-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.rank-type-btn {
  padding: 5px 14px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: #fff;
  font-size: 13px;
  color: var(--text2);
}

.rank-type-btn.active {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 600;
}

.month-nav-inline {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.month-nav-inline button {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--primary);
  padding: 2px 6px;
}

.month-nav-inline span {
  font-size: 14px;
  font-weight: 600;
  min-width: 80px;
  text-align: center;
}

.rank-sort-row {
  display: flex;
  gap: 6px;
}

.rank-sort {
  padding: 5px 12px;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: #fff;
  font-size: 12px;
  color: var(--text2);
}

.rank-sort.active {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 600;
}

.ranking-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.rank-num {
  font-size: 16px;
  font-weight: 700;
  color: var(--text2);
  min-width: 24px;
  text-align: center;
}

.rank-num.top3 { color: var(--primary); }

.rank-thumb {
  width: 48px; height: 48px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--bg);
  flex-shrink: 0;
}

.rank-thumb-placeholder {
  width: 48px; height: 48px;
  border-radius: 8px;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.rank-info { flex: 1; min-width: 0; }

.rank-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 3px;
}

.rank-sub {
  font-size: 12px;
  color: var(--text2);
}

.rank-right { text-align: right; flex-shrink: 0; }

.rank-main-val {
  font-size: 15px;
  font-weight: 700;
}

.rank-sub-val {
  font-size: 12px;
  color: var(--text2);
}

.rank-profit { color: var(--green); }
.rank-loss { color: var(--red); }

/* ==================== PRODUCT LIST ==================== */
.search-bar {
  padding: 10px 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.search-input {
  width: 100%;
  padding: 9px 14px 9px 36px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  background: #F4F4F5 url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%239CA3AF' stroke-width='2'%3e%3ccircle cx='11' cy='11' r='8'/%3e%3cline x1='21' y1='21' x2='16.65' y2='16.65'/%3e%3c/svg%3e") no-repeat 10px center / 16px;
  outline: none;
}

.product-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.product-row:active { background: #F9F9F9; }

.product-thumb {
  width: 56px; height: 56px;
  border-radius: 10px;
  object-fit: cover;
  background: var(--bg);
  flex-shrink: 0;
}

.product-thumb-ph {
  width: 56px; height: 56px;
  border-radius: 10px;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
}

.product-info { flex: 1; min-width: 0; }

.product-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 3px;
}

.product-sku {
  font-size: 12px;
  color: var(--text2);
  margin-bottom: 2px;
}

.product-cost {
  font-size: 12px;
  color: var(--text2);
}

.product-arrow {
  color: var(--text3);
  font-size: 18px;
}

/* ==================== PRODUCT DETAIL ==================== */
.detail-images {
  position: relative;
  background: #000;
}

.detail-img-slider {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.detail-img-slider::-webkit-scrollbar { display: none; }

.detail-img-item {
  flex: none;
  width: 100%;
  scroll-snap-align: start;
}

.detail-img-item img {
  width: 100%;
  height: 220px;
  object-fit: contain;
  background: #F0F0F0;
}

.detail-no-img {
  height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  font-size: 48px;
}

.detail-info-table { width: 100%; }

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
}

.detail-row:last-child { border-bottom: none; }

.detail-label { font-size: 14px; color: var(--text2); }
.detail-value { font-size: 14px; font-weight: 500; text-align: right; max-width: 60%; word-break: break-all; }
.detail-value.mono { font-family: 'SF Mono', monospace; font-size: 13px; }

/* ==================== IMAGE UPLOAD ==================== */
.img-upload-area {
  background: var(--bg);
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.img-preview-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.img-preview-item {
  position: relative;
  width: 70px; height: 70px;
}

.img-preview-item img {
  width: 70px; height: 70px;
  border-radius: 8px;
  object-fit: cover;
}

.img-preview-del {
  position: absolute;
  top: -6px; right: -6px;
  width: 22px; height: 22px;
  background: var(--red);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 14px;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}

.btn-add-photo {
  background: none;
  border: 1.5px dashed var(--text3);
  border-radius: 10px;
  padding: 10px;
  font-size: 13px;
  color: var(--text2);
  width: 100%;
  text-align: center;
}

/* ==================== CSV ==================== */
.csv-upload-area {
  margin: 14px;
  background: #fff;
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 30px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.csv-upload-icon { font-size: 40px; }
.csv-upload-text { font-size: 14px; color: var(--text2); line-height: 1.6; }
.csv-upload-area.drag-over { border-color: var(--primary); background: var(--primary-light); }

.section-header {
  padding: 12px 14px 6px;
}

.report-row {
  display: flex;
  align-items: center;
  padding: 12px 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.report-info { flex: 1; min-width: 0; }
.report-month { font-size: 15px; font-weight: 600; }
.report-file { font-size: 12px; color: var(--text2); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.report-count { font-size: 12px; color: var(--text3); }

.btn-del-report {
  background: none;
  border: none;
  color: var(--red);
  font-size: 22px;
  padding: 4px 8px;
}

.csv-preview-info {
  background: var(--bg);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--text2);
}

.csv-preview-table-wrap {
  overflow-x: auto;
  border-radius: 8px;
  border: 1px solid var(--border);
  max-height: 240px;
  overflow-y: auto;
}

.csv-preview-table {
  border-collapse: collapse;
  font-size: 12px;
  white-space: nowrap;
  width: 100%;
}

.csv-preview-table th, .csv-preview-table td {
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
}

.csv-preview-table th { background: #FAFAFA; color: var(--text2); }

/* ==================== INVENTORY ==================== */
.inventory-ctrl {
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.inv-summary {
  padding: 8px 14px 12px;
  display: flex;
  gap: 20px;
}

.inv-summary-item { text-align: center; }
.inv-summary-label { font-size: 11px; color: var(--text2); margin-bottom: 2px; }
.inv-summary-val { font-size: 16px; font-weight: 700; }

.inventory-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: #fff;
  border-bottom: 1px solid var(--border);
}

.inv-thumb {
  width: 44px; height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--bg);
  flex-shrink: 0;
}

.inv-thumb-ph {
  width: 44px; height: 44px;
  border-radius: 8px;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.inv-info { flex: 1; min-width: 0; }
.inv-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inv-sku-label { font-size: 11px; color: var(--text2); }
.inv-right { text-align: right; flex-shrink: 0; }
.inv-qty { font-size: 15px; font-weight: 700; }
.inv-val { font-size: 12px; color: var(--text2); }
.inv-del-btn { background: none; border: none; color: var(--red); font-size: 18px; padding: 4px; }

/* ==================== SETTINGS ==================== */
.settings-card { overflow: hidden; }
.settings-section-title { padding: 10px 14px 4px; font-size: 12px; color: var(--text2); font-weight: 600; }
.settings-row { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--border); cursor: pointer; }
.settings-row:last-child { border-bottom: none; }
.settings-label { font-size: 15px; }
.settings-value { font-size: 14px; color: var(--text2); }
.settings-arrow { color: var(--text3); font-size: 18px; }
.app-version { text-align: center; padding: 20px; font-size: 12px; color: var(--text3); }

/* ==================== MODALS ==================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.45);
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}

.modal-overlay.hidden { display: none; }

.modal-sheet {
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
  animation: slideUp .25s ease;
  /* iPhoneノッチの下に隠れないよう上部余白を確保 */
  padding-top: 0;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: #fff;
  border-radius: 20px 20px 0 0;
  flex-shrink: 0;
  min-height: 52px;
}

/* 戻る・キャンセルボタン：赤・太字・タップ領域を広く */
.modal-cancel {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 16px;
  font-weight: 600;
  padding: 8px 4px;
  min-width: 70px;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
}
.modal-cancel:active { opacity: .6; }

.modal-title { font-size: 16px; font-weight: 600; color: var(--text); flex: 1; text-align: center; }

.modal-save {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 16px;
  font-weight: 600;
  padding: 8px 4px;
  min-width: 70px;
  text-align: right;
}
.modal-save:active { opacity: .6; }

.modal-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ==================== TOAST ==================== */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30,30,30,0.9);
  color: #fff;
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 9999;
  pointer-events: none;
  white-space: nowrap;
  animation: toastIn .2s ease;
}

@keyframes toastIn { from { opacity:0; transform: translateX(-50%) translateY(10px); } }
.toast.hidden { display: none; }

/* ==================== MISC ==================== */
.hidden { display: none !important; }
.empty-state { text-align: center; padding: 40px 20px; color: var(--text2); font-size: 14px; }
.empty-icon { font-size: 40px; margin-bottom: 10px; }

.loading {
  text-align: center;
  padding: 30px;
  color: var(--text3);
  font-size: 14px;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

.badge-green { background: #DCFCE7; color: #16A34A; }
.badge-gray { background: var(--bg); color: var(--text2); }
