:root {
  --primary:        #5b50f0;
  --primary-bg:     #f0eeff;
  --mandatory:      #e04040;
  --mandatory-bg:   #fff0f0;
  --text:           #111827;
  --muted:          #6b7280;
  --border:         #e5e7eb;
  --bg:             #f5f5fb;
  --card:           #ffffff;
  --shadow:         0 1px 4px rgba(91,80,240,.1);
  --radius:         12px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

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

#login-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  text-align: center;
}

.login-card .logo { font-size: 60px; display: block; margin-bottom: 8px; }

.login-card h1 {
  font-size: 28px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 4px;
}

.login-card > p {
  color: var(--muted);
  margin-bottom: 28px;
  font-size: 15px;
}

#login-form { display: flex; flex-direction: column; gap: 10px; }

#login-form input,
#login-form button {
  width: 100%;
  padding: 14px 16px;
  border-radius: var(--radius);
  font-size: 16px;
}

#login-form input {
  border: 2px solid var(--border);
  outline: none;
  transition: border-color .2s;
}
#login-form input:focus { border-color: var(--primary); }

#login-form button {
  background: var(--primary);
  color: #fff;
  border: none;
  font-weight: 700;
  cursor: pointer;
}

.error { color: var(--mandatory); font-size: 13px; margin-top: 10px; }

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

#main-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--card);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.app-title { font-size: 19px; font-weight: 700; color: var(--primary); }

.main-nav {
  display: flex;
  background: var(--bg);
  border-radius: 8px;
  padding: 2px;
  gap: 2px;
}

.nav-btn {
  padding: 5px 12px;
  border: none;
  border-radius: 6px;
  background: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: all .15s;
}

.nav-btn.active {
  background: var(--card);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}

#logout-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
}

/* ── Budget bar ─────────────────────────────────────── */

#budget-bar {
  display: flex;
  align-items: center;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  gap: 16px;
  flex-shrink: 0;
}

.budget-cell { flex: 1; display: flex; flex-direction: column; gap: 1px; }
.budget-sep  { width: 1px; height: 32px; background: var(--border); }

.budget-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  font-weight: 600;
}

.budget-amount {
  font-size: 17px;
  font-weight: 700;
  min-height: 24px;
}
.budget-amount.mandatory { color: var(--mandatory); }
.budget-amount.wishlist  { color: var(--primary); }

/* ── Tabs ───────────────────────────────────────────── */

/* ── List ───────────────────────────────────────────── */

main {
  flex: 1;
  overflow-y: auto;
  padding: 14px 14px;
  padding-bottom: max(88px, calc(80px + env(safe-area-inset-bottom)));
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-section { display: flex; flex-direction: column; gap: 9px; }

.section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  padding: 4px 2px;
}

.item-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 56px 24px;
  line-height: 1.7;
  font-size: 15px;
}

/* ── Item Card ──────────────────────────────────────── */

.item-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 11px 12px;
  box-shadow: var(--shadow);
  user-select: none;
}

.drag-handle-ph {
  width: 28px;
  flex-shrink: 0;
}

.drag-handle {
  color: #ccc;
  font-size: 20px;
  cursor: grab;
  touch-action: none;
  flex-shrink: 0;
  line-height: 1;
  padding: 2px 4px;
}
.drag-handle:active { cursor: grabbing; }

.item-img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
  background: var(--bg);
}

.item-img-ph {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  background: var(--bg);
  flex-shrink: 0;
}

.item-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.item-name-row {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
}

.item-name {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  flex-shrink: 1;
  min-width: 0;
}

.item-price {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
  color: var(--muted);
}

.item-price-tbd {
  font-size: 12px;
  color: var(--border);
  font-weight: 400;
}

.item-due {
  font-size: 11px;
  font-weight: 700;
  color: var(--mandatory);
}

.item-notes {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.item-link {
  font-size: 12px;
  color: var(--primary);
  text-decoration: none;
}

.item-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.qty-btn {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  color: var(--text);
  flex-shrink: 0;
}
.qty-btn:active { background: var(--border); }

.qty-val {
  font-size: 13px;
  font-weight: 600;
  min-width: 16px;
  text-align: center;
}

.edit-btn, .trash-btn, .unpurchase-btn {
  background: none;
  border: none;
  font-size: 15px;
  cursor: pointer;
  padding: 3px;
  line-height: 1;
  opacity: .5;
}
.edit-btn:hover, .trash-btn:hover, .unpurchase-btn:hover { opacity: 1; }

.purchase-btn {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: none;
  border: 1.5px solid var(--border);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: border-color .15s, color .15s;
}
.purchase-btn:hover { border-color: #2a9d60; color: #2a9d60; }

/* ── Purchased screen ───────────────────────────────── */

#purchased-screen {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.p-summary-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.p-summary-total {
  font-size: 32px;
  font-weight: 700;
  color: #2a9d60;
  line-height: 1;
  margin-bottom: 4px;
}

.p-summary-label {
  font-size: 13px;
  color: var(--muted);
}

.table-wrap {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

#purchased-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

#purchased-table th {
  text-align: left;
  padding: 10px 14px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  border-bottom: 1.5px solid var(--border);
}

#purchased-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

#purchased-table tr:last-child td { border-bottom: none; }

.td-name   { font-weight: 500; }
.td-qty    { color: var(--muted); font-weight: 400; font-size: 12px; }
.td-date   { color: var(--muted); font-size: 13px; white-space: nowrap; }
.td-total  { font-weight: 700; white-space: nowrap; text-align: right; }
.td-actions { text-align: right; white-space: nowrap; }
.td-empty  { text-align: center; color: var(--muted); padding: 32px !important; }

/* ── FAB ────────────────────────────────────────────── */

.fab {
  position: fixed;
  bottom: max(24px, calc(20px + env(safe-area-inset-bottom)));
  right: 22px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  border: none;
  font-size: 30px;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(91,80,240,.38);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  line-height: 1;
}

/* ── Modal ──────────────────────────────────────────── */

#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.46);
  z-index: 50;
  display: flex;
  align-items: flex-end;
}

#modal {
  width: 100%;
  max-height: 92dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--card);
  border-radius: 20px 20px 0 0;
  padding: 20px 20px;
  padding-bottom: max(20px, calc(16px + env(safe-area-inset-bottom)));
  display: flex;
  flex-direction: column;
  gap: 13px;
  animation: slideUp .25s cubic-bezier(.34,1.56,.64,1);
}

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

@media (min-width: 600px) {
  #modal-overlay { align-items: center; justify-content: center; }
  #modal { border-radius: 20px; max-width: 480px; max-height: 88dvh; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h2 { font-size: 17px; font-weight: 700; }

#modal-close {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--bg);
  border: none;
  font-size: 13px;
  cursor: pointer;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
}

.url-row { display: flex; gap: 8px; }
.url-row input { flex: 1; }

#fetch-btn {
  padding: 0 16px;
  height: 44px;
  background: var(--primary-bg);
  color: var(--primary);
  border: none;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
#fetch-btn:disabled { opacity: .5; cursor: default; }

#fetch-status {
  font-size: 13px;
  padding: 8px 12px;
  border-radius: 8px;
}
.fetch-loading { background: #eef2ff; color: #4060c0; }
.fetch-success { background: #f0fff6; color: #207050; }
.fetch-error   { background: var(--mandatory-bg); color: var(--mandatory); }

#img-preview-wrap {
  border-radius: 10px;
  overflow: hidden;
  background: var(--bg);
  max-height: 170px;
}
#img-preview { width: 100%; height: 170px; object-fit: cover; display: block; }

.field-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 6px;
}

/* Form inputs */
#modal input[type=text],
#modal input[type=url],
#modal input[type=number],
#modal input[type=date],
#modal textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  font-family: inherit;
  outline: none;
  background: #fff;
  transition: border-color .18s;
  -webkit-appearance: none;
}
#modal input:focus,
#modal textarea:focus { border-color: var(--primary); }

#due-date-row { display: flex; flex-direction: column; }

.price-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 0 14px;
  transition: border-color .18s;
  background: #fff;
}
.price-wrap:focus-within { border-color: var(--primary); }
.currency-sym { color: var(--muted); font-weight: 600; flex-shrink: 0; }
.price-wrap input { border: none !important; padding: 12px 0 !important; background: transparent; }

.cat-toggle { display: flex; gap: 8px; }

.cat-btn {
  flex: 1;
  padding: 10px 8px;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: #fff;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: all .15s;
}
.cat-btn.active[data-cat=wishlist]   { border-color: var(--primary);   background: var(--primary-bg);   color: var(--primary);   }
.cat-btn.active[data-cat=mandatory]  { border-color: var(--mandatory); background: var(--mandatory-bg); color: var(--mandatory); }

#modal textarea { resize: none; }

.modal-footer { display: flex; gap: 10px; }

.btn-primary {
  flex: 1;
  padding: 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.btn-danger {
  padding: 14px 18px;
  background: var(--mandatory-bg);
  color: var(--mandatory);
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* ── Small screen card layout ───────────────────────── */

@media (max-width: 500px) {
  .item-card {
    flex-wrap: wrap;
  }
  .item-controls {
    flex-basis: 100%;
    /* indent to align under item-body: handle(28) + gap(10) + image(50) + gap(10) */
    padding-left: 98px;
    justify-content: flex-start;
  }
}

/* ── Sortable feedback ──────────────────────────────── */
.sortable-ghost   { opacity: .25; }
.sortable-chosen  { box-shadow: 0 4px 20px rgba(91,80,240,.2); }
