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

:root {
  --color-bg: #f8f9fa;
  --color-surface: #ffffff;
  --color-primary: #2d8a4e;
  --color-primary-light: #e6f4ec;
  --color-danger: #e53e3e;
  --color-muted: #9aa0a6;
  --color-text: #1a1a1a;
  --color-border: #e2e8f0;
  --radius: 10px;
  --tap-min: 48px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
}

header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #306969;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 56px;
}

header h1 {
  font-size: 1.2rem;
  font-weight: 600;
}

#nav-toggle {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.3rem;
  cursor: pointer;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

#edit-toggle {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 50%;
  transition: background 0.15s;
}

#edit-toggle.active {
  background: rgba(255,255,255,0.25);
}

#app {
  padding: 0 0 80px;
}

.hidden { display: none; }

/* ── Login ────────────────────────────────────────── */

.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
  padding: 24px;
}
.login-title {
  color: var(--color-text);
  margin-bottom: 32px;
}
.login-form {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.login-input {
  padding: 14px 16px;
  border-radius: 10px;
  border: 1px solid var(--color-border);
  font-size: 1rem;
  background: var(--color-surface);
  color: var(--color-text);
}
.login-btn {
  padding: 14px;
  border-radius: 10px;
  border: none;
  background: var(--color-primary);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
}
.login-error {
  color: #c00;
  text-align: center;
  font-size: 0.9rem;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ── Section headers ─────────────────────────────── */

.section-header {
  padding: 12px 16px 6px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--color-muted);
  background: var(--color-bg);
}

/* ── Category grouping (inline flow) ─────────────── */

.tile-flow {
  --cols: 4;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 2px;
}

/* Each category group flows as a unit: pill on top, tiles below */
.category-group {
  width: calc(
    (100% - (var(--cols) - 1) * 2px) / var(--cols) * var(--group-cols)
    + (var(--group-cols) - 1) * 2px
  );
  align-self: flex-start;
}

.category-divider {
  display: flex;
  align-items: center;
  padding: 4px 0 1px;
  gap: 6px;
  min-height: 20px;
}

.category-divider-label {
  font-size: 0.55rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #fff;
  padding: 1px 6px;
  border-radius: 3px;
  line-height: 1.4;
  max-width: calc((100% - (var(--group-cols) - 1) * 2px) / var(--group-cols));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 0;
}

.category-divider-line {
  flex: 1;
  height: 2px;
  opacity: 0.2;
  background: var(--color-muted);
}

.category-tiles {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}

/* ── Tile remove badge (edit mode) ────────────────── */

.tile {
  position: relative;
}

.tile-remove-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  background: var(--color-danger);
  color: #fff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  z-index: 2;
}

/* ── Amount chips ──────────────────────────────────── */

.amount-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.amount-chip {
  min-height: 38px;
  min-width: 52px;
  padding: 6px 14px;
  border: 1px solid var(--color-border);
  border-radius: 20px;
  background: var(--color-surface);
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

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

/* ── Add panel ─────────────────────────────────────── */

.add-panel {
  border-top: 2px solid var(--color-border);
  margin-top: 8px;
  padding-top: 4px;
}

.add-panel .section-header {
  padding-top: 8px;
}

/* ── Detail sheet actions ──────────────────────────── */

.sheet-action-link {
  background: none;
  border: none;
  color: var(--color-primary);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 8px 0;
  text-align: left;
}

.sheet-action-link:active {
  opacity: 0.7;
}

.sheet-action-danger {
  width: 100%;
  padding: 12px;
  background: none;
  color: var(--color-danger);
  border: 1px solid var(--color-danger);
  border-radius: 10px;
  font-size: 1rem;
  cursor: pointer;
  min-height: var(--tap-min);
}

/* ── Search bar ───────────────────────────────────── */

.search-wrap {
  padding: 10px 16px 6px;
  background: var(--color-bg);
}

.search-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 1rem;
  background: var(--color-surface);
  outline: none;
  min-height: var(--tap-min);
}

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

.off-list-name {
  flex: 1;
  font-size: 1rem;
}

/* Off-list item rows */
.off-list-item {
  background: var(--color-surface);
  display: flex;
  align-items: center;
  min-height: var(--tap-min);
  padding: 0 16px;
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
  font-size: 1rem;
}

.off-list-item:active {
  background: var(--color-primary-light);
}

.off-list-cat {
  flex: 1;
  font-size: 0.75rem;
  color: var(--color-muted);
  margin-left: 8px;
}

.item-edit-btn {
  min-width: 44px;
  min-height: 44px;
  background: none;
  border: none;
  color: var(--color-muted);
  font-size: 1.1rem;
  flex-shrink: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

/* ── On-list badge in off-list items ────────────── */

.off-list-item.on-list {
  opacity: 0.55;
}

.on-list-badge {
  color: var(--color-primary);
  font-size: 0.85rem;
  font-weight: 700;
  margin-right: 8px;
  flex-shrink: 0;
}

.create-hint {
  background: var(--color-surface);
  display: flex;
  align-items: center;
  min-height: var(--tap-min);
  padding: 0 16px;
  border-bottom: 1px solid var(--color-border);
  border-top: 1px dashed var(--color-border);
  cursor: pointer;
  font-size: 0.95rem;
  color: var(--color-primary);
  font-weight: 500;
}

.create-hint:active {
  background: var(--color-primary-light);
}


/* ── Tiles ────────────────────────────────────────── */

.tile {
  width: calc((100% - (var(--cols) - 1) * 2px) / var(--cols));
  aspect-ratio: 5 / 6;
  border-radius: 4px;
  border: none;
  background: #e8e8e8;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 1px;
  cursor: pointer;
  padding: 6px 4px 4px;
  text-align: center;
  color: #222;
  transition: opacity 0.12s;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.tile * {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.tile.tap-active {
  opacity: 0.8;
}

.tile-icon {
  width: 80%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  border: 5px solid color-mix(in srgb, var(--cat-color, #666) 50%, #ccc);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  color: #555;
  flex-shrink: 1;
  min-height: 0;
  margin-bottom: 3px;
}

.tile-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 50%;
}

.tile-name {
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 1.15;
  color: #222;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow-wrap: break-word;
  word-break: keep-all;
  flex-shrink: 0;
}

.tile-details {
  font-size: 0.55rem;
  color: rgba(0, 0, 0, 0.5);
  line-height: 1.1;
  min-height: 0.65em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
  flex-shrink: 0;
  margin-top: auto;
}

.tile.done {
  opacity: 0.5;
}

.tile.done .tile-name {
  text-decoration: line-through;
}

.tile.done::after {
  content: '✕';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 4rem;
  font-weight: 300;
  color: #8c5050;
  pointer-events: none;
}

/* ── Header actions ───────────────────────────────── */

.header-actions {
  display: flex;
  align-items: center;
  gap: 0;
  flex-shrink: 0;
}

.header-btn {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 50%;
  transition: background 0.15s, opacity 0.15s;
}

.header-btn:active {
  background: rgba(255,255,255,0.25);
}

.header-btn.hidden {
  display: none;
}

/* ── Empty states ─────────────────────────────────── */

.empty-hint {
  padding: 24px 16px;
  text-align: center;
  color: var(--color-muted);
  font-size: 0.9rem;
}

/* ── Nav Drawer ───────────────────────────────────── */

@keyframes slideIn {
  from { transform: translateX(-100%); }
  to   { transform: translateX(0); }
}

body.drawer-open {
  overflow: hidden;
}

.nav-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 200;
}

.side-drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: var(--color-surface);
  z-index: 201;
  display: flex;
  flex-direction: column;
  animation: slideIn 0.2s ease;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 56px;
  font-weight: 600;
  font-size: 1rem;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.drawer-close {
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--color-muted);
  cursor: pointer;
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
}

.drawer-body {
  flex-grow: 1;
  overflow-y: auto;
  padding-bottom: 24px;
}

/* ── Category list in drawer ──────────────────────── */

.drawer-section-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 12px 16px 4px;
}

.cat-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.cat-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  border-bottom: 1px solid var(--color-border);
  min-height: var(--tap-min);
}

.cat-row.dragging {
  opacity: 0.3;
}

.drag-handle {
  cursor: grab;
  color: var(--color-muted);
  font-size: 1.3rem;
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  touch-action: none;
  margin-left: -8px;
}

.cat-name {
  flex: 1;
  font-size: 1rem;
}

.cat-rename-input {
  flex: 1;
  font-size: 1rem;
  border: 1px solid var(--color-primary);
  border-radius: 4px;
  padding: 4px 8px;
  outline: none;
  width: 100%;
}

.cat-delete-btn {
  min-width: 36px;
  min-height: 36px;
  background: none;
  border: none;
  color: var(--color-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: color 0.15s, background 0.15s;
  opacity: 0.6;
}

.cat-delete-btn:hover,
.cat-delete-btn:active {
  color: var(--color-danger);
  background: rgba(229, 62, 62, 0.08);
  opacity: 1;
}

.cat-color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.1);
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.12s;
}

.cat-color-swatch:active {
  transform: scale(1.15);
}

.color-picker-popup {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 16px 12px;
  border-bottom: 1px solid var(--color-border);
}

.color-option {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.1s, border-color 0.1s;
}

.color-option:active {
  transform: scale(1.15);
}

.color-option.selected {
  border-color: var(--color-text);
}

.cat-error {
  font-size: 0.85rem;
  color: var(--color-danger);
  padding: 4px 16px 8px;
}

/* ── Bottom sheet ─────────────────────────────────── */

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

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 100;
}

.bottom-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--color-surface);
  border-radius: 16px 16px 0 0;
  padding: 16px;
  z-index: 101;
  max-height: 80vh;
  overflow-y: auto;
  animation: slideUp 0.2s ease;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 4px;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.sheet-close {
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--color-muted);
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sheet-body {
  margin: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sheet-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sheet-label {
  font-size: 0.85rem;
  color: var(--color-muted);
  font-weight: 500;
}

.sheet-input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 1rem;
  outline: none;
  background: var(--color-surface);
  min-height: var(--tap-min);
}

.sheet-input:focus {
  border-color: var(--color-primary);
}

.sheet-footer {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sheet-confirm {
  width: 100%;
  padding: 14px;
  background: var(--color-primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  min-height: var(--tap-min);
}

.sheet-confirm:active {
  opacity: 0.85;
}

.sheet-confirm-danger {
  width: 100%;
  padding: 14px;
  background: var(--color-danger);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  min-height: var(--tap-min);
}

.sheet-cancel {
  width: 100%;
  padding: 12px;
  background: none;
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  font-size: 1rem;
  cursor: pointer;
  min-height: var(--tap-min);
}

.sheet-delete {
  width: 100%;
  padding: 12px;
  background: none;
  color: var(--color-danger);
  border: 1px solid var(--color-danger);
  border-radius: 10px;
  font-size: 1rem;
  cursor: pointer;
  min-height: var(--tap-min);
}

.sheet-warning {
  color: var(--color-danger);
  font-size: 0.95rem;
  padding: 4px 0;
}

.sheet-error {
  color: var(--color-danger);
  font-size: 0.875rem;
  padding: 4px 0;
}

/* ── Icon section in edit sheet ──────────────────── */

.icon-section { text-align: center; }
.icon-preview { margin: 8px 0; }
.icon-preview img { width: 80px; height: 80px; object-fit: contain; border-radius: 50%; }
.icon-placeholder { color: #999; font-size: 0.9rem; }
.icon-actions { display: flex; gap: 8px; justify-content: center; margin: 8px 0; }
.icon-actions button {
  padding: 6px 12px; border: 1px solid #ccc; border-radius: 8px;
  background: #fff; font-size: 0.85rem; cursor: pointer;
}
.icon-actions button:disabled { opacity: 0.5; cursor: wait; }

.icon-hint-input {
  width: 100%; padding: 6px 10px; border: 1px solid #ddd; border-radius: 8px;
  font-size: 0.85rem; margin: 0 0 4px;
}
.icon-hint-input::placeholder { color: #aaa; }

.icon-alternatives-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px;
  margin: 8px 0; min-height: 0;
}
.icon-alt-thumb {
  width: 100%; aspect-ratio: 1; object-fit: contain;
  border: 2px solid transparent; border-radius: 12px; cursor: pointer;
}
.icon-alt-thumb:active { border-color: var(--color-primary); }

.icon-spinner {
  display: inline-block; width: 24px; height: 24px;
  border: 3px solid #eee; border-top-color: #666; border-radius: 50%;
  animation: spin 0.6s linear infinite; margin: 16px auto; grid-column: 1 / -1;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Drawer action button ────────────────────────── */

.drawer-action-wrap {
  padding: 8px 16px;
}

.drawer-action-btn {
  width: 100%;
  padding: 12px;
  background: var(--color-surface);
  color: var(--color-primary);
  border: 1px solid var(--color-primary);
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  min-height: var(--tap-min);
}

.drawer-action-btn:active {
  background: var(--color-primary-light);
}

.regen-progress-bar {
  padding: 12px;
  background: var(--color-primary-light);
  color: var(--color-primary);
  border-radius: 10px;
  font-size: 0.85rem;
  font-weight: 500;
  text-align: center;
  min-height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
}
