:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --text: #1d211e;
  --muted: #69716b;
  --line: #e2e1db;
  --accent: #2f7d4f;
  --accent-text: #ffffff;
  --ok: #2f7d4f;
  --ok-bg: #e5f2ea;
  --warn: #9a5b00;
  --warn-bg: #fff1d6;
  --warn-line: #e9a23b;
  --bad: #b3261e;
  --bad-bg: #fde7e5;
  --info: #1f5fa8;
  --info-bg: #e3eefb;
  --neutral-bg: #ecebe6;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 16px rgb(0 0 0 / 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121513;
    --surface: #1c201d;
    --text: #e8ebe8;
    --muted: #9aa39c;
    --line: #2d332f;
    --accent: #4fae76;
    --accent-text: #08130c;
    --ok: #6fcf97;
    --ok-bg: #173322;
    --warn: #ffc56b;
    --warn-bg: #3a2a0d;
    --warn-line: #d08a1f;
    --bad: #ff8a80;
    --bad-bg: #3d1614;
    --info: #8cbcf5;
    --info-bg: #142a44;
    --neutral-bg: #262b27;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  padding: 0 max(16px, env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 640px;
  margin-inline: auto;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button,
.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 14px;
  min-height: 44px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

button.danger {
  color: var(--bad);
}

button:disabled {
  opacity: 0.5;
}

.link-btn {
  border: 0;
  background: none;
  padding: 4px;
  min-height: 0;
  color: var(--accent);
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 2px;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
}

input:focus-visible,
textarea:focus-visible,
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

[hidden] {
  display: none !important;
}

.muted {
  color: var(--muted);
  font-size: 14px;
}

.empty {
  color: var(--muted);
  text-align: center;
  padding: 24px 8px;
}

/* ---- header & sync status ---- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 0 8px;
  background: var(--bg);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 22px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar h1.editable {
  cursor: pointer;
}

.icon-btn {
  width: 44px;
  padding: 0;
  border: 0;
  background: none;
}

.icon-btn svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sync-status {
  position: sticky;
  top: calc(60px + env(safe-area-inset-top));
  z-index: 4;
  width: 100%;
  justify-content: flex-start;
  text-align: left;
  border-radius: var(--radius);
  border: 1px solid transparent;
  margin-bottom: 12px;
  padding: 8px 12px;
  min-height: 40px;
  font-size: 14px;
  flex-wrap: wrap;
}

.sync-text {
  font-weight: 600;
}

.sync-detail {
  color: inherit;
  opacity: 0.8;
}

.sync-icon {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: currentColor;
  flex: none;
}

.sync-status[data-tone='ok'] {
  background: var(--ok-bg);
  color: var(--ok);
}
.sync-status[data-tone='busy'] {
  background: var(--info-bg);
  color: var(--info);
}
.sync-status[data-tone='busy'] .sync-icon {
  animation: pulse 1s ease-in-out infinite;
}
.sync-status[data-tone='warn'] {
  background: var(--warn-bg);
  color: var(--warn);
  border-color: var(--warn-line);
}
.sync-status[data-tone='bad'] {
  background: var(--bad-bg);
  color: var(--bad);
  border-color: var(--bad);
}
.sync-status[data-tone='neutral'] {
  background: var(--neutral-bg);
  color: var(--muted);
}

@keyframes pulse {
  50% {
    opacity: 0.25;
  }
}

.banner {
  background: var(--info-bg);
  color: var(--info);
  border-radius: var(--radius);
  padding: 8px 12px;
  margin-bottom: 12px;
  font-size: 14px;
}

/* ---- home ---- */

.lists {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.lists a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: inherit;
  text-decoration: none;
  font-weight: 600;
}

.lists a .name {
  flex: 1;
}

.badge {
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  padding: 2px 8px;
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid var(--warn-line);
}

.home-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 16px;
}

.whoami {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

/* ---- add form & suggestions ---- */

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

.suggestions {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-top: 8px;
  padding: 4px 0;
}

.suggestions-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 12px;
  font-size: 13px;
  color: var(--muted);
}

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

.suggestions li button {
  width: 100%;
  border: 0;
  border-radius: 0;
  background: none;
  justify-content: flex-start;
  gap: 12px;
  padding: 6px 12px;
  text-align: left;
}

.suggestions li button:active {
  background: var(--neutral-bg);
}

.suggestions .plus {
  margin-left: auto;
  color: var(--accent);
  font-size: 22px;
  line-height: 1;
}

.suggestions .new-entry {
  font-style: italic;
}

/* ---- items ---- */

.items {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px 10px 6px 4px;
  min-height: 56px;
  border-left: 4px solid transparent;
  cursor: pointer;
}

.item.pending {
  border-left: 4px dashed var(--warn-line);
}

.item.failed {
  border-left: 4px solid var(--bad);
}

.item.flash {
  animation: flash 1s;
}

@keyframes flash {
  30% {
    background: var(--warn-bg);
  }
}

.check {
  flex: none;
  width: 44px;
  height: 44px;
  border: 0;
  background: none;
  padding: 0;
}

.check span {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--muted);
  display: grid;
  place-items: center;
}

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

.check svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: var(--accent-text);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  visibility: hidden;
}

.item.checked .check svg {
  visibility: visible;
}

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

.thumb {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--neutral-bg);
}

.thumb.missing {
  display: grid;
  place-items: center;
  font-size: 10px;
  color: var(--muted);
  text-align: center;
  line-height: 1.1;
}

.item .body {
  flex: 1;
  min-width: 0;
}

.item .name {
  font-weight: 500;
  overflow-wrap: anywhere;
}

.item .qty {
  color: var(--muted);
  font-weight: 400;
  margin-left: 6px;
}

.item .note {
  color: var(--muted);
  font-size: 14px;
  overflow-wrap: anywhere;
}

.item .unsynced {
  font-size: 12px;
  font-weight: 600;
  color: var(--warn);
}

.item.failed .unsynced {
  color: var(--bad);
}

.cloud {
  flex: none;
  width: 20px;
  height: 20px;
  fill: none;
  stroke: var(--warn);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.item.failed .cloud {
  stroke: var(--bad);
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-top: 24px;
}

.section-head h2 {
  font-size: 16px;
  color: var(--muted);
  margin: 0;
}

#cart .item {
  opacity: 0.75;
}

/* ---- dialogs ---- */

dialog.sheet {
  border: 0;
  border-radius: 16px;
  padding: 20px;
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.3);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.4);
}

dialog h2 {
  margin: 0 0 12px;
  font-size: 20px;
}

dialog label {
  display: block;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 10px;
}

dialog label input,
dialog label textarea {
  margin-top: 4px;
  color: var(--text);
}

.dialog-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.spacer {
  flex: 1;
}

.photo-field {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.photo-preview img {
  width: 96px;
  height: 96px;
  border-radius: 10px;
  object-fit: cover;
  display: block;
}

.photo-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

dialog .photo-actions label.btn {
  margin: 0;
  color: var(--text);
  font-size: 16px;
}

.sync-note {
  font-size: 14px;
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--warn-bg);
  color: var(--warn);
}

.sync-note.bad {
  background: var(--bad-bg);
  color: var(--bad);
}

.photo-dialog {
  border: 0;
  padding: 0;
  background: transparent;
  max-width: 100vw;
  max-height: 100dvh;
}

.photo-dialog img {
  display: block;
  max-width: 100vw;
  max-height: 100dvh;
  object-fit: contain;
}

#sync-body h3 {
  font-size: 15px;
  margin: 16px 0 6px;
}

#sync-body ul {
  margin: 0;
  padding-left: 0;
  list-style: none;
  display: grid;
  gap: 6px;
}

#sync-body li {
  font-size: 14px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--neutral-bg);
}

#sync-body li.failed {
  background: var(--bad-bg);
}

#sync-body li .err {
  color: var(--bad);
  display: block;
}

#sync-body li .acts {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

#sync-body li .acts button {
  min-height: 36px;
  padding: 4px 12px;
}

#sync-body .headline {
  font-weight: 600;
}

/* ---- toast ---- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  display: flex;
  gap: 12px;
  align-items: center;
  box-shadow: 0 4px 20px rgb(0 0 0 / 0.25);
  z-index: 20;
  max-width: calc(100vw - 32px);
}

.toast .link-btn {
  color: var(--bg);
}
