* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
}

input,
textarea {
  user-select: text;
  -webkit-user-select: text;
}

html {
  overscroll-behavior-y: none;
}

:root {
  --bg: #f5f5f7;
  --surface: #fffffffc;
  --text: #1d1d1f;
  --text-secondary: #86868b;
  --bg-secondary: #ebebef;
  --accent: #007aff;
  --accent-light: #e8f2ff;
  --danger: #ff3b30;
  --success: #34c759;
  --border: #e5e5ea;
  --divider: #efeff5;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

body {
  font-family:
    -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding-bottom: var(--safe-bottom);
  overscroll-behavior: none;
}

header {
  background: var(--surface);
  padding: 16px 20px 8px;
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid var(--border);
}

header h1 {
  font-size: 28px;
  font-weight: 700;
}

/* Tabs */
nav#tabs {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 99;
  border-bottom: 1px solid var(--border);
  padding-top: var(--safe-top);
}

.tab-row {
  display: flex;
}

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

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* Tab content */
.tab-content {
  display: none;
  padding: 12px;
}

#tab-inventory,
#tab-shopping {
  padding-bottom: 80px;
}

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

/* Search */
.search-bar {
  padding: 8px 12px;
  background: var(--bg);
  border-top: 1px solid var(--border);
}

.search-bar.hidden {
  display: none;
}

.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.search-bar input {
  width: 100%;
  padding: 10px 36px 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 16px;
  background: var(--surface);
  outline: none;
}

.search-bar input:focus {
  border-color: var(--accent);
}

.btn-clear-search {
  position: absolute;
  right: 10px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  line-height: 0;
  opacity: 0.85;
}

.btn-clear-search:active {
  opacity: 0.6;
}

.btn-clear-search.hidden {
  display: none;
}

/* Category groups */
.category-group {
  margin-bottom: 4px;
}

.category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 4px;
  cursor: pointer;
  user-select: none;
}

.category-header .left {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.category-header h2 {
  font-size: 15px;
  font-weight: 600;
}

.category-header .count {
  font-size: 13px;
  color: var(--text-secondary);
}

.category-header .chevron {
  font-size: 12px;
  color: var(--text-secondary);
  transition: transform 0.2s;
}

.category-group.collapsed .chevron {
  transform: rotate(-90deg);
}

.category-group.collapsed .card-list {
  display: none;
}

/* Items */
.card-list {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
}

.item-row {
  display: flex;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid var(--divider);
  cursor: pointer;
  transition: background 0.15s;
  gap: 12px;
}

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

.item-row:active {
  background: var(--accent-light);
  transition: none;
}

.item-row.selected {
  background: var(--accent-light);
}

.item-row.in-shopping .item-checkbox {
  opacity: 0.3;
}

.item-row.in-shopping .item-name {
  opacity: 0.3;
}

.item-checkbox {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

.item-row.selected .item-checkbox {
  background: var(--accent);
  border-color: var(--accent);
}

.item-row.selected .item-checkbox::after {
  content: "\2713";
  color: white;
  font-size: 15px;
  font-weight: 700;
}

.item-name {
  flex: 1;
  font-size: 16px;
}

.item-name[data-badge]::after {
  content: attr(data-badge);
  font-size: 12px;
  padding: 3px 6px;
  border-radius: 4px;
  background: var(--bg-secondary);
  color: var(--text);
  font-weight: 500;
  margin-left: 6px;
  vertical-align: middle;
}

/* Storage badge */
.storage-badge {
  font-size: 12px;
  padding: 3px 6px;
  border-radius: 4px;
  font-weight: 500;
  flex-shrink: 0;
}

.storage-냉장 {
  background: #e3f2fd;
  color: #1565c0;
}
.storage-냉동 {
  background: #ede7f6;
  color: #4527a0;
}

/* Storage picker */
.storage-picker {
  display: flex;
  gap: 8px;
}

.storage-pick-btn {
  flex: 1;
  padding: 10px 0;
  border: 2px solid transparent;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.storage-pick-btn.storage-실온 {
  background: #fff3e0;
  color: #e65100;
}
.storage-pick-btn.storage-냉장 {
  background: #e3f2fd;
  color: #1565c0;
}
.storage-pick-btn.storage-냉동 {
  background: #ede7f6;
  color: #4527a0;
}

.storage-pick-btn.active {
  border-color: currentColor;
}

/* Shopping list */

.shopping-completed-section {
  margin-top: 8px;
  padding-top: 4px;
  border-top: 1px solid var(--border);
}

.shopping-completed-section h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 8px 4px 4px;
}

.item-row.pending {
  opacity: 0.8;
  background: var(--accent-light);
}

.item-row.pending .shopping-item-name {
  text-decoration: line-through;
}

.item-row.pending .shopping-check {
  background: var(--accent);
  border-color: var(--accent);
}

.item-row.pending .shopping-check::after {
  content: "\2713";
  color: white;
  font-size: 14px;
  font-weight: 700;
}

.item-row.completed {
  opacity: 0.4;
}

.item-row.completed .shopping-item-name {
  text-decoration: line-through;
}

.shopping-check {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

.item-row.completed .shopping-check {
  background: var(--success);
  border-color: var(--success);
}

.item-row.completed .shopping-check::after {
  content: "\2713";
  color: white;
  font-size: 14px;
  font-weight: 700;
}

.shopping-item-name {
  flex: 1;
  font-size: 16px;
}

.shopping-item-cat {
  font-size: 12px;
  color: var(--text-secondary);
}

.shopping-empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-secondary);
  font-size: 15px;
}

/* Quick add prompt */
.quick-add-prompt {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 16px;
  text-align: center;
}

.quick-add-prompt p {
  font-size: 15px;
  color: var(--text-secondary);
  margin-bottom: 14px;
}

.shopping-match-section {
  text-align: left;
  margin-bottom: 12px;
}

.shopping-match-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 0 2px 6px;
}

.shopping-match-list {
  margin-top: 6px;
}

.btn-quick-add-shopping {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 18px;
  width: 30px;
  height: 30px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.btn-direct-add {
  width: 100%;
  padding: 11px;
  border: 1.5px dashed var(--border);
  border-radius: 10px;
  background: none;
  font-size: 15px;
  color: var(--text-secondary);
  cursor: pointer;
  margin-top: 4px;
}

.btn-direct-add:active {
  background: var(--bg);
}

.quick-add-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Floating add bar */
.floating-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: var(--surface);
  padding: 12px 20px;
  padding-bottom: calc(12px + var(--safe-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-top: 1px solid var(--border);
  max-height: calc(64px + var(--safe-bottom));
  overflow: hidden;
  transition:
    max-height 0.3s ease,
    padding 0.3s ease;
}

.floating-bar.hidden {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-top: none;
}

.floating-bar #selected-count,
.floating-bar #completed-count {
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
}

.bar-actions {
  display: flex;
  gap: 6px;
}

.btn-primary {
  padding: 10px 16px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.floating-bar button:active {
  opacity: 0.8;
}

.btn-danger {
  padding: 10px 16px;
  border: none;
  border-radius: 10px;
  background: var(--danger);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* Action bar */
.action-bar {
  padding: 16px 0;
  display: flex;
  gap: 8px;
}

.btn-secondary {
  flex: 1;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  color: var(--text);
}

.btn-secondary:active {
  background: var(--bg);
}

/* Manage */
.manage-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 12px;
}

.manage-section h2 {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 12px;
}

.form-row {
  display: flex;
  gap: 8px;
}

/* ── Shared form controls ── */
.form-row input,
.form-row select,
.quick-add-row select,
.cat-select,
.edit-item-input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  background: var(--surface);
  outline: none;
}

.form-row input,
.form-row select,
.quick-add-row select {
  flex: 1;
}

.form-row input:focus,
.form-row select:focus,
.quick-add-row select:focus,
.cat-select:focus,
.edit-item-input:focus {
  border-color: var(--accent);
}

.form-row select,
.quick-add-row select,
.cat-select {
  padding-right: 32px;
  background: var(--surface)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%2386868b' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E")
    no-repeat right 12px center;
  -webkit-appearance: none;
  appearance: none;
  opacity: 1;
  color: #000;
}

.cat-select {
  width: 100%;
}

.form-row button,
.quick-add-row button {
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.form-row button:disabled {
  opacity: 0.3;
  cursor: default;
}

.hint {
  font-size: 14px;
  color: var(--text-secondary);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: white;
  padding: 10px 16px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  z-index: 300;
  transition: opacity 0.3s;
}

.toast.hidden {
  opacity: 0;
  pointer-events: none;
}

/* Loading */
.loading {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
}

.loading.hidden {
  display: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 400;
  padding: 20px;
}

.modal {
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  max-width: 300px;
  width: 100%;
  text-align: center;
}

.modal p {
  font-size: 16px;
  margin-bottom: 16px;
}

.edit-item-input {
  width: 100%;
  margin-bottom: 16px;
}

.item-meta {
  text-align: left;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 2;
  margin-top: 12px;
  margin-bottom: 12px;
}

.item-meta-value {
  color: var(--text);
}

.btn-save {
  background: var(--accent);
  color: white;
}

.cat-pick-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 12px 0 18px;
  max-height: 50vh;
  overflow-y: auto;
}

.cat-pick-list .btn-secondary {
  flex: none;
}

.btn-edit-item {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  line-height: 1;
}

.modal .btn-row {
  display: flex;
  gap: 8px;
}

.modal .btn-row button {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.modal .btn-cancel {
  background: var(--bg);
  color: var(--text);
}

.modal .btn-delete {
  background: var(--danger);
  color: white;
}

