/* ===== Choppin Design System ===== */
/* Inspired by Dieter Rams & Teenage Engineering */

:root {
  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 16px;
  --space-lg: 24px;
  --space-xl: 32px;
  --space-2xl: 48px;

  /* Typography */
  --font: -apple-system, 'SF Pro Display', 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', monospace;
  --text-xs: 11px;
  --text-sm: 13px;
  --text-base: 15px;
  --text-md: 17px;
  --text-lg: 20px;
  --text-xl: 24px;
  --text-2xl: 32px;

  /* Radius */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 999px;

  /* Transitions */
  --t-fast: 120ms ease;
  --t-base: 200ms ease;
  --t-slow: 350ms ease;

  /* Accent colors */
  --lime:    #A8E63D;
  --lime-dk: #7DB82A;
  --orange:  #FF7A2F;
  --pink:    #F03E8E;
  --cyan:    #00C8D4;

  /* Light Mode */
  --bg:          #F5F5F2;
  --bg-card:     #FFFFFF;
  --bg-elevated: #FAFAF8;
  --bg-input:    #EFEFEC;
  --bg-hover:    #E8E8E4;
  --border:      #E0E0DA;
  --border-mid:  #CACAC4;
  --text-primary:   #1A1A18;
  --text-secondary: #6B6B64;
  --text-tertiary:  #9E9E97;
  --text-on-accent: #1A1A18;
  --accent:      var(--lime);
  --accent-text: var(--lime-dk);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.10);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:          #1E1E1C;
    --bg-card:     #282826;
    --bg-elevated: #303030;
    --bg-input:    #323230;
    --bg-hover:    #3A3A38;
    --border:      #383836;
    --border-mid:  #4A4A48;
    --text-primary:   #F0F0EC;
    --text-secondary: #9A9A94;
    --text-tertiary:  #6A6A64;
    --text-on-accent: #1A1A18;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
  }
}

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

html {
  height: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font);
  font-size: var(--text-base);
  color: var(--text-primary);
  background: var(--bg);
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  /* Prevent iOS bounce scroll */
  position: fixed;
  width: 100%;
}

/* ===== App Shell ===== */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 430px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* ===== Header ===== */
.app-header {
  flex-shrink: 0;
  padding: env(safe-area-inset-top, 0) var(--space-md) 0;
  background: var(--bg);
  z-index: 10;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
}

.app-wordmark {
  font-size: var(--text-lg);
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--text-primary);
}

.app-wordmark em {
  font-style: normal;
  color: var(--accent-text);
}


.header-actions {
  display: flex;
  gap: var(--space-sm);
}

/* ===== Tab Bar ===== */
.tab-bar {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  padding: 0 var(--space-md);
}

.tab-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-tertiary);
  background: none;
  border: none;
  cursor: pointer;
  position: relative;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  transition: color var(--t-base);
  -webkit-tap-highlight-color: transparent;
}

.tab-btn svg { transition: color var(--t-base); }

.tab-btn.active {
  color: var(--accent-text);
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 20%;
  right: 20%;
  height: 2px;
  background: var(--accent);
  border-radius: 2px 2px 0 0;
}

/* ===== Tab Content ===== */
.tab-content {
  flex: 1;
  overflow: hidden;
  position: relative;
}

.tab-panel {
  display: none;
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.tab-panel.active { display: block; }
.tab-panel.fade-in { animation: fadeIn 150ms ease; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== Section Headers ===== */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-md) var(--space-xs);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}

.section-title {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

/* ===== Cards ===== */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  margin: 0 var(--space-md) var(--space-md);
  overflow: hidden;
}

.card + .card { margin-top: 0; }

/* ===== List Items ===== */
.list-item {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: 12px var(--space-md);
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  transition: background var(--t-fast);
  min-height: 52px;
  position: relative;
}

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

.list-item:active { background: var(--bg-hover); }

/* ===== Checkboxes ===== */
.check-box {
  width: 24px;
  height: 24px;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border-mid);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--t-base);
  background: none;
  -webkit-tap-highlight-color: transparent;
}

.check-box.checked {
  background: var(--accent);
  border-color: var(--accent);
}

.check-box.checked svg { display: block; }
.check-box svg { display: none; width: 14px; height: 14px; }

/* ===== Item Text ===== */
.item-name {
  flex: 1;
  font-size: var(--text-base);
  color: var(--text-primary);
  line-height: 1.3;
}

.item-name.checked {
  color: var(--text-tertiary);
  text-decoration: line-through;
}

.item-qty {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  min-width: 28px;
  text-align: right;
}

/* ===== Not-Found Badge ===== */
.not-found-badge {
  font-size: 14px;
  margin-left: var(--space-xs);
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
  padding: 10px var(--space-md);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all var(--t-base);
  -webkit-tap-highlight-color: transparent;
  letter-spacing: 0.2px;
}

.btn-primary {
  background: var(--accent);
  color: var(--text-on-accent);
}
.btn-primary:active { filter: brightness(0.92); }

.btn-ghost {
  background: none;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-ghost:active { background: var(--bg-hover); }

.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: var(--radius-md);
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--t-fast);
  -webkit-tap-highlight-color: transparent;
}
.btn-icon:active { background: var(--bg-hover); }

.btn-accent-sm {
  background: var(--bg-input);
  color: var(--text-secondary);
  font-size: var(--text-xs);
  padding: 5px 10px;
  border-radius: var(--radius-full);
  border: none;
  cursor: pointer;
  font-weight: 600;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  transition: all var(--t-fast);
  -webkit-tap-highlight-color: transparent;
}
.btn-accent-sm:active { background: var(--bg-hover); }

/* ===== Input Fields ===== */
.input {
  background: var(--bg-input);
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 10px var(--space-md);
  font-size: 16px; /* min 16px prevents iOS auto-zoom */
  font-family: var(--font);
  color: var(--text-primary);
  width: 100%;
  outline: none;
  transition: all var(--t-base);
  -webkit-appearance: none;
}

.input:focus {
  border-color: var(--accent);
  background: var(--bg-card);
}

.input::placeholder { color: var(--text-tertiary); }

/* ===== Inline Edit ===== */
.inline-edit {
  background: none;
  border: none;
  outline: none;
  font: inherit;
  color: inherit;
  width: 100%;
  cursor: default;
  border-radius: var(--radius-sm);
  padding: 2px 4px;
  margin: -2px -4px;
  transition: background var(--t-fast);
  font-size: 16px; /* prevent iOS zoom */
}

.inline-edit:focus {
  cursor: text;
  background: var(--bg-input);
}

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

.autocomplete-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  z-index: 100;
  overflow: hidden;
  max-height: 220px;
  overflow-y: auto;
}

.autocomplete-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px var(--space-md);
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: var(--text-base);
  transition: background var(--t-fast);
}

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

.autocomplete-section {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
}

/* ===== Market Selector ===== */
.market-selector {
  display: flex;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  -webkit-overflow-scrolling: touch;
}

.market-selector::-webkit-scrollbar { display: none; }

.market-chip {
  flex-shrink: 0;
  padding: 7px 14px;
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: 600;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--t-base);
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
  letter-spacing: 0.1px;
}

.market-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-on-accent);
}

/* ===== Section Groups ===== */
.section-group {
  margin-bottom: var(--space-sm);
}

.section-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px var(--space-md) 4px;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.section-group-title {
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-tertiary);
}

.section-collapse-icon {
  color: var(--text-tertiary);
  transition: transform var(--t-base);
  flex-shrink: 0;
}

.section-collapse-icon.collapsed { transform: rotate(-90deg); }

.section-group-body {
  overflow: hidden;
  transition: none;
}

.section-group-body.collapsed { display: none; }

/* ===== Not Found Section ===== */
.not-found-section .section-group-title { color: var(--orange); }
.not-found-section .card { border-color: rgba(255,122,47,0.3); }

/* ===== Add Item Row ===== */
.add-item-row {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: 10px var(--space-md);
  border-top: 1px solid var(--border);
}

.add-item-input-wrap {
  flex: 1;
  position: relative;
}

/* ===== Drag Handle ===== */
.drag-handle {
  width: 20px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: center;
  cursor: grab;
  flex-shrink: 0;
  padding: 4px 0;
}

.drag-handle span {
  display: block;
  width: 14px;
  height: 2px;
  background: var(--border-mid);
  border-radius: 2px;
}

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

/* ===== Quantity Control ===== */
.qty-control {
  display: flex;
  align-items: center;
  gap: 4px;
}

.qty-btn {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  border: none;
  color: var(--text-secondary);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: 300;
  transition: background var(--t-fast);
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

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

.qty-value {
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  min-width: 24px;
  text-align: center;
  font-weight: 600;
}

/* ===== Markets Tab ===== */
.market-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin: 0 var(--space-md) var(--space-md);
  overflow: hidden;
}

.market-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md);
  border-bottom: 1px solid var(--border);
}

.market-card-name {
  font-size: var(--text-md);
  font-weight: 700;
  letter-spacing: -0.2px;
}

.market-card-actions {
  display: flex;
  gap: var(--space-xs);
}

.section-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 4px 10px 4px var(--space-sm);
  background: var(--bg-input);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  color: var(--text-secondary);
  border: none;
  cursor: default;
  margin: 3px;
}

.sections-container {
  padding: var(--space-sm) var(--space-md);
}

/* ===== Smart Tab ===== */
.smart-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin: 0 var(--space-md) var(--space-md);
  overflow: hidden;
}

.suggestion-item {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: 12px var(--space-md);
  border-bottom: 1px solid var(--border);
}

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

.suggestion-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.suggestion-dot.due { background: var(--orange); }
.suggestion-dot.soon { background: var(--lime); }
.suggestion-dot.later { background: var(--border-mid); }

.suggestion-text { flex: 1; }
.suggestion-name { font-size: var(--text-base); font-weight: 500; }
.suggestion-meta { font-size: var(--text-xs); color: var(--text-tertiary); margin-top: 2px; }

.suggestion-add {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-md);
  background: var(--bg-input);
  border: none;
  color: var(--accent-text);
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: 300;
  transition: background var(--t-fast);
  -webkit-tap-highlight-color: transparent;
}

.suggestion-add:active { background: var(--bg-hover); }

/* ===== Auth Screen ===== */
#auth-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-xl);
  z-index: 200;
}

.auth-logo {
  font-size: 40px;
  font-weight: 800;
  letter-spacing: -1px;
  margin-bottom: var(--space-2xl);
}

.auth-logo span { color: var(--accent-text); }

.auth-form {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.auth-form .input {
  font-size: var(--text-md);
  padding: 14px var(--space-md);
}

.auth-form .btn-primary {
  font-size: var(--text-md);
  padding: 14px;
}

.auth-switch {
  text-align: center;
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin-top: var(--space-sm);
}

.auth-switch a {
  color: var(--accent-text);
  font-weight: 600;
  text-decoration: none;
}

/* ===== Modals ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 300;
  display: flex;
  align-items: flex-end;
  animation: overlayIn var(--t-base);
}

@media (prefers-color-scheme: dark) {
  .modal-overlay { background: rgba(0,0,0,0.6); }
}

@keyframes overlayIn {
  from { background: transparent; }
  to   { background: rgba(0,0,0,0.4); }
}

.modal-sheet {
  width: 100%;
  background: var(--bg-card);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  padding: var(--space-sm) var(--space-md) calc(var(--space-xl) + env(safe-area-inset-bottom, 0px));
  animation: sheetIn var(--t-slow) cubic-bezier(0.32, 0.72, 0, 1);
  max-height: 85dvh;
  overflow-y: auto;
}

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

.modal-handle {
  width: 36px;
  height: 4px;
  background: var(--border-mid);
  border-radius: 2px;
  margin: 0 auto var(--space-lg);
}

.modal-title {
  font-size: var(--text-lg);
  font-weight: 700;
  margin-bottom: var(--space-lg);
  letter-spacing: -0.3px;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
  margin-top: var(--space-md);
}

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  padding: var(--space-2xl) var(--space-xl);
  text-align: center;
}

.empty-icon {
  font-size: 40px;
  opacity: 0.4;
}

.empty-title {
  font-size: var(--text-md);
  font-weight: 600;
  color: var(--text-secondary);
}

.empty-sub {
  font-size: var(--text-sm);
  color: var(--text-tertiary);
  max-width: 240px;
  line-height: 1.5;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 90px);
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--text-primary);
  color: var(--bg);
  padding: 10px 20px;
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: 500;
  z-index: 400;
  opacity: 0;
  transition: all var(--t-base);
  white-space: nowrap;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===== Bottom Safe Area Padding ===== */
.safe-bottom {
  height: calc(env(safe-area-inset-bottom, 0px) + var(--space-lg));
}

/* ===== Accent color variants ===== */
[data-accent="orange"] { --accent: var(--orange); --accent-text: var(--orange); }
[data-accent="pink"]   { --accent: var(--pink);   --accent-text: var(--pink); }
[data-accent="cyan"]   { --accent: var(--cyan);   --accent-text: var(--cyan); }
[data-accent="lime"]   { --accent: var(--lime);   --accent-text: var(--lime-dk); }

/* ===== Swipe-to-delete ===== */
.swipeable {
  overflow: hidden;
  position: relative;
}

.swipe-action {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  padding: 0 var(--space-lg);
  background: var(--pink);
  color: white;
  font-size: var(--text-sm);
  font-weight: 600;
  transform: translateX(100%);
  transition: transform var(--t-base);
}

/* ===== Divider ===== */
.divider {
  height: 1px;
  background: var(--border);
  margin: var(--space-sm) var(--space-md);
}

/* ===== Toggle collapse all ===== */
.collapse-toggle {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 6px 12px;
  border-radius: var(--radius-full);
  background: var(--bg-input);
  border: none;
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  cursor: pointer;
  transition: background var(--t-fast);
  -webkit-tap-highlight-color: transparent;
}

.collapse-toggle:active { background: var(--bg-hover); }
.collapse-toggle.active-toggle { background: var(--bg-input); color: var(--accent-text); }

/* ===== Page padding ===== */
.page-pad {
  padding-top: var(--space-sm);
  padding-bottom: var(--space-xl);
}

/* ===== Dot separator ===== */
.dot-sep {
  display: inline-block;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--text-tertiary);
  vertical-align: middle;
  margin: 0 5px;
}

/* ===== Dragging state ===== */
.dragging {
  opacity: 0.5;
  background: var(--bg-hover) !important;
}

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

/* ===== Skeleton loader ===== */
.skeleton {
  background: linear-gradient(90deg, var(--bg-input) 25%, var(--bg-hover) 50%, var(--bg-input) 75%);
  background-size: 200% 100%;
  animation: skeleton 1.2s infinite;
  border-radius: var(--radius-sm);
}

@keyframes skeleton {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
