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

:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --border: #e8e8e4;
  --text-primary: #1a1a1a;
  --text-secondary: #888880;
  --accent: #c0392b;
  --star: #e8a020;
  --chip-active: #1a1a1a;
  --chip-inactive: #f0f0ec;
  --fab-bg: #1a1a1a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 4px rgba(0,0,0,0.08);
  --shadow-panel: 0 -2px 20px rgba(0,0,0,0.10);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Yu Gothic', sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100vh;
  padding-bottom: 80px;
}

/* ===== Tab Nav ===== */
.tab-nav {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 99;
}

.tab-btn {
  flex: 1;
  padding: 12px 0;
  border: none;
  background: none;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
  font-weight: 500;
}

.tab-btn.active {
  color: var(--text-primary);
  border-bottom-color: var(--text-primary);
}

/* ===== Tab Content ===== */
.tab-content { display: none; }
.tab-content.active { display: block; }
#tab-calendar { padding-top: 12px; }

/* ===== Filter Bar ===== */
.filter-bar {
  padding: 12px 16px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.category-chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 12px;
  padding-right: 16px;
  scrollbar-width: none;
}
.category-chips::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  padding: 5px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--chip-inactive);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
  color: var(--text-secondary);
}

.chip.active {
  background: var(--chip-active);
  color: #fff;
  border-color: var(--chip-active);
}

.filter-sort-row {
  display: flex;
  gap: 8px;
  padding-bottom: 12px;
  align-items: center;
}

.period-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 12px;
  cursor: pointer;
  color: var(--text-secondary);
  white-space: nowrap;
}
.period-btn:hover { background: var(--bg); }

.sort-select {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 12px;
  color: var(--text-primary);
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23888' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 28px;
}

.period-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: #f0f0f0;
  border-radius: 20px;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.period-clear {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1;
}

/* ===== Item Grid ===== */
.item-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 8px;
}

@media (min-width: 768px) {
  .item-grid { grid-template-columns: repeat(4, 1fr); }
}
@media (min-width: 1024px) {
  .item-grid { grid-template-columns: repeat(5, 1fr); }
}

/* ===== Item Card ===== */
.item-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}
.item-card:active { transform: scale(0.97); }

.card-img-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  background: var(--bg);
}

.card-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.card-category-badge {
  position: absolute;
  top: 6px;
  left: 6px;
  background: rgba(255,255,255,0.85);
  backdrop-filter: blur(4px);
  border-radius: 20px;
  padding: 2px 7px;
  font-size: 9px;
  font-weight: 600;
  color: var(--text-primary);
  max-width: calc(100% - 12px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-img-placeholder {
  width: 100%;
  aspect-ratio: 1;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--border);
  font-size: 28px;
}

.card-body {
  padding: 8px 10px 10px;
}

.card-seller {
  font-size: 10px;
  color: var(--text-secondary);
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-name {
  font-size: 12px;
  font-weight: 500;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.card-price {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.card-date {
  font-size: 10px;
  color: var(--text-secondary);
}

.empty-state {
  grid-column: 1 / -1;
  text-align: center;
  padding: 60px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* ===== FAB ===== */
.fab {
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--fab-bg);
  color: #fff;
  border: none;
  font-size: 28px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
  z-index: 200;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s;
}
.fab:active { transform: scale(0.92); }

/* ===== Panel ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 300;
}

.panel {
  position: fixed;
  inset: 0;
  background: var(--surface);
  z-index: 400;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

.panel.open { transform: translateX(0); }

.panel-header {
  display: flex;
  align-items: center;
  padding: 16px 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
  flex-shrink: 0;
}

.back-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.panel-title {
  flex: 1;
  font-size: 16px;
  font-weight: 600;
}

.icon-btn {
  border: none;
  background: none;
  font-size: 13px;
  color: var(--accent);
  cursor: pointer;
  padding: 4px 8px;
}

.save-btn {
  border: none;
  background: var(--fab-bg);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
}

/* ===== Detail View ===== */
.detail-img-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 20px;
  background: var(--bg);
}

.detail-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.detail-img-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--border);
  font-size: 48px;
}

.detail-category-overlay {
  position: absolute;
  top: 12px;
  left: 12px;
  background: rgba(255,255,255,0.88);
  backdrop-filter: blur(4px);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
}

.detail-seller {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.detail-name {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 8px;
}

.detail-price {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 4px;
}

.detail-date {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.detail-section {
  margin-bottom: 20px;
}

.detail-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 6px;
}

.detail-value {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
}

/* Stars */
.stars {
  display: flex;
  gap: 4px;
}

.star {
  font-size: 22px;
  cursor: pointer;
  color: var(--border);
  transition: color 0.1s;
  line-height: 1;
}

.star.filled { color: var(--star); }
.star.readonly { cursor: default; font-size: 18px; }

/* Purchase button */
.purchase-btn {
  padding: 14px;
  background: var(--fab-bg);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* Detail bottom buttons */
.detail-bottom-btns {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

.edit-btn {
  flex: 0 0 auto;
  padding: 14px 20px;
  background: var(--bg);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* 購入リスト詳細の編集ボタン（add-usage-btnに合わせる） */
.purchased-edit-btns {
  margin-top: 0;
}
.purchased-edit-btns .edit-btn {
  flex: 1;
  width: 100%;
  padding: 12px;
  font-size: 13px;
  margin-top: 12px;
}

.purchase-btn {
  flex: 1;
}

/* Usage log */
.usage-log {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
}

.usage-entry {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.usage-entry-date {
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.usage-entry-text {
  font-size: 13px;
  line-height: 1.5;
}

.add-usage-btn {
  width: 100%;
  padding: 12px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  background: none;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  margin-top: 12px;
}

/* Add usage form */
.usage-form {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.usage-form textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  resize: none;
  height: 80px;
  font-family: inherit;
}

.usage-form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ===== Form ===== */
.form-field {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.form-input,
.form-textarea,
.form-select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px; /* iOS拡大防止：16px以上必須 */
  background: var(--surface);
  color: var(--text-primary);
  font-family: inherit;
  transition: border-color 0.15s;
  height: 46px;
  box-sizing: border-box;
}

/* Date wrapper - hide native iOS date UI */
.form-date-wrapper {
  position: relative;
}
.date-display {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--text-primary);
}
.date-hidden-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.img-upload-area.has-image {
  border: none;
}

.form-input:focus,
.form-textarea:focus,
.form-select:focus {
  outline: none;
  border-color: var(--text-primary);
}

.form-textarea {
  resize: none;
  height: 90px;
  line-height: 1.5;
  font-size: 16px;
}

.form-select { appearance: none; cursor: pointer; }

.form-type-toggle {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 20px;
}

.type-btn {
  flex: 1;
  padding: 8px;
  border: none;
  background: none;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s;
}

.type-btn.active {
  background: var(--surface);
  color: var(--text-primary);
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

/* Image upload */
.img-upload-area {
  width: 160px;
  aspect-ratio: 1;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  position: relative;
  background: var(--bg);
}

.img-upload-area img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.img-upload-label {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  pointer-events: none;
}

.img-upload-icon { font-size: 28px; margin-bottom: 6px; }

.img-upload-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

/* Autocomplete */
.autocomplete-wrapper {
  position: relative;
}

.autocomplete-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  z-index: 999;
  max-height: 180px;
  overflow-y: auto;
}

.autocomplete-item {
  padding: 12px 14px;
  font-size: 15px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}

.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:active { background: var(--bg); }

/* Category select with manage button */
.form-select-row {
  display: flex;
  gap: 8px;
}

.form-select-row .form-select { flex: 1; }

.manage-category-btn {
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 12px;
  cursor: pointer;
  color: var(--text-secondary);
  white-space: nowrap;
}

/* ===== Calendar ===== */
.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px 12px;
}

.cal-month-nav {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cal-month {
  font-size: 18px;
  font-weight: 700;
}

.cal-nav {
  width: 28px;
  height: 28px;
  border: none;
  background: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.cal-header-total {
  font-size: 26px;
  font-weight: 700;
  color: var(--text-primary);
}

.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  padding: 0 8px;
}

.calendar-weekdays span {
  text-align: center;
  font-size: 11px;
  color: var(--text-secondary);
  padding: 4px 0 6px;
  font-weight: 500;
}

.calendar-weekdays .cal-sun { color: #c0392b; }
.calendar-weekdays .cal-sat { color: #2980b9; }

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  padding: 0 8px 16px;
}

.cal-day {
  aspect-ratio: 0.7;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 6px;
  cursor: default;
  gap: 4px;
}

.cal-day.has-purchase { cursor: pointer; }
.cal-day.has-purchase:active { opacity: 0.7; }

.cal-day-num {
  font-size: 13px;
  font-weight: 400;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.cal-day.today {
  background: #f0f0f0;
  border-radius: 6px;
}

.cal-day.today .cal-day-num {
  font-weight: 600;
}

.cal-day.other-month .cal-day-num { color: #c0c0c0; }
.cal-day.sunday .cal-day-num { color: #c0392b; }
.cal-day.saturday .cal-day-num { color: #2980b9; }
.cal-day.holiday .cal-day-num { color: #c0392b; }
.cal-day.sunday.other-month .cal-day-num,
.cal-day.saturday.other-month .cal-day-num,
.cal-day.holiday.other-month .cal-day-num { color: #ddd; }

.cal-day-holiday {
  font-size: 8px;
  color: #c0392b;
  font-weight: 500;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: none;
}
.cal-day.holiday .cal-day-holiday { display: block; }

.cal-day-amount {
  display: none;
  font-size: 9px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border-radius: 4px;
  padding: 2px 4px;
  white-space: nowrap;
}

.cal-day.has-purchase .cal-day-amount { display: block; }

/* ===== Modals ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 500;
  display: flex;
  align-items: flex-end;
  padding: 0;
}

@media (min-width: 480px) {
  .modal-overlay { align-items: center; padding: 20px; }
}

.modal {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px 20px 32px;
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
}

@media (min-width: 480px) {
  .modal { border-radius: var(--radius); padding: 24px 20px; }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-title {
  font-size: 16px;
  font-weight: 700;
}

.modal-close {
  width: 28px;
  height: 28px;
  border: none;
  background: var(--bg);
  border-radius: 50%;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-field {
  margin-bottom: 16px;
}

.modal-field label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.modal-field input[type="date"] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.btn-primary, .btn-secondary {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary { background: var(--fab-bg); color: #fff; }
.btn-secondary { background: var(--bg); color: var(--text-secondary); }

/* Category modal list */
.category-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  transition: background 0.1s;
}

.category-item.drag-over {
  background: var(--bg);
  border-top: 2px solid var(--accent);
}

.drag-handle {
  cursor: grab;
  color: var(--text-secondary);
  font-size: 18px;
  padding: 0 4px;
  user-select: none;
  touch-action: none;
  flex-shrink: 0;
}

.drag-handle:active { cursor: grabbing; }

.category-name-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.category-name-input,
.category-edit-input {
  flex: 1;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: inherit;
}

.category-item-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.category-edit-btn {
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  padding: 4px 8px;
}

.category-delete-btn {
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 16px;
  padding: 4px 8px;
}

.modal-add-row {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}

.modal-add-row input {
  flex: 1;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-family: inherit;
}

.modal-add-row .btn-primary {
  flex: none;
  padding: 10px 16px;
}

/* Cal day list */
.cal-day-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  font-size: 14px;
}

.cal-day-item:last-child { border-bottom: none; }

.cal-day-item-name { font-weight: 500; }
.cal-day-item-detail { font-size: 12px; color: var(--text-secondary); }
.cal-day-item-price { font-weight: 600; color: var(--text-primary); }

/* ===== Crop Modal ===== */
.crop-modal-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 600;
  display: flex;
  flex-direction: column;
}

.crop-modal {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
}

.crop-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: #111;
  flex-shrink: 0;
}

.crop-modal-header button {
  border: none;
  background: none;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  padding: 4px 8px;
}

.crop-modal-header button:first-child { color: #aaa; }
.crop-modal-header span { color: #fff; font-size: 15px; font-weight: 600; }
.crop-modal-header button:last-child { color: #fff; font-weight: 700; }

.crop-stage {
  flex: 1;
  position: relative;
  overflow: hidden;
  touch-action: none;
  background: #000;
}

#cropCanvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
}

.crop-frame {
  position: absolute;
  border: 2px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.5);
  pointer-events: none;
  border-radius: 2px;
}

.crop-hint {
  text-align: center;
  color: #888;
  font-size: 12px;
  padding: 12px;
  background: #111;
  flex-shrink: 0;
}

.hidden { display: none !important; }
