:root {
  color-scheme: light;
  --bg: #f7f8f5;
  --panel: #ffffff;
  --ink: #17201a;
  --muted: #687268;
  --line: #dfe5dc;
  --accent: #1f7a4d;
  --accent-dark: #125936;
  --danger: #b73838;
  --warm: #e8b84a;
  --blue: #337b9a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Arial, Helvetica, sans-serif;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.login-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
}

.login-brand {
  display: flex;
  justify-content: center;
  padding: 0 0 6px;
}

.login-brand img {
  display: block;
  width: min(260px, 76vw);
  max-height: 82px;
  height: auto;
  object-fit: contain;
}

.login-card button {
  width: 100%;
}

button[hidden] {
  display: none;
}

.login-error {
  background: #fff0f0;
  border: 1px solid #efb6b6;
  border-radius: 8px;
  color: var(--danger);
  padding: 10px 12px;
}

.topbar {
  position: relative;
  z-index: 5;
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 8px 12px;
}

.brand {
  display: flex;
  align-items: center;
  grid-column: 1;
  grid-row: 1;
  min-width: 0;
  height: 44px;
}

.brand img {
  display: block;
  width: 154px;
  max-height: 44px;
  height: auto;
  object-fit: contain;
  transform: translateY(2px);
}

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
}

.tabs-header {
  align-items: center;
  grid-column: 2;
  grid-row: 1;
}

.tabs-bottom {
  display: none;
}

.logout-link {
  position: static;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  min-height: 24px;
  color: var(--muted);
  font-size: 0.75rem;
  text-decoration: none;
  white-space: nowrap;
  line-height: 1;
  grid-column: 4;
  grid-row: 1;
  justify-self: end;
}

.logout-link:hover {
  color: var(--danger);
  text-decoration: underline;
}

.tabs a,
.button,
button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  font: inherit;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.tabs a {
  flex: 1;
  max-width: 180px;
  min-height: 40px;
  background: transparent;
  color: var(--muted);
}

.tabs a.active {
  background: #e5f1e9;
  color: var(--accent-dark);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.ghost {
  background: #edf1ea;
  color: var(--ink);
}

.danger {
  background: var(--danger);
  color: #fff;
}

.danger.ghost {
  background: #fae7e5;
  color: var(--danger);
}

.shell {
  width: min(980px, 100%);
  margin: 0 auto;
  padding: 18px 14px 72px;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.detail-nav {
  display: flex;
  align-items: center;
  gap: 6px;
}

.detail-nav .icon-btn {
  font-size: 1.7rem;
  line-height: 1;
}

.shopping-head {
  margin-top: 22px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.7rem;
}

h2 {
  font-size: 1.2rem;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.metrics article,
.list-row,
.notice,
.import-box,
dialog form {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.metrics article {
  padding: 16px;
}

.metrics span {
  display: block;
  font-size: 2rem;
  font-weight: 800;
}

.metrics p,
.muted {
  color: var(--muted);
}

.best-price {
  display: block;
  color: var(--accent-dark);
  font-size: 0.82rem;
  font-weight: 700;
  margin-top: 1px;
}

.compact {
  margin-bottom: 16px;
}

.compact article {
  padding: 12px;
}

.compact span {
  font-size: 1.35rem;
}

input,
select {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

select {
  cursor: pointer;
}

.search-control {
  position: relative;
  min-width: 0;
}

.search-control input {
  padding-right: 48px;
}

.clear-input {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  width: 38px;
  min-height: 38px;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  padding: 0;
  font-size: 1.35rem;
}

.clear-input:hover {
  background: #edf1ea;
  color: var(--ink);
}

.section-search {
  margin-bottom: 14px;
}

.segmented-control {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.segmented-control button {
  min-height: 36px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  padding: 0 12px;
}

.segmented-control button.active {
  background: #e5f1e9;
  color: var(--accent-dark);
}

.items-sort-toggle {
  margin: -4px 0 12px;
}

.quick-add,
.import-box,
.purchase-grid,
.item-form,
.detail-item-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  margin-bottom: 14px;
}

.item-form {
  grid-template-columns: 1fr auto;
}

.item-form[hidden] {
  display: none;
}

.detail-item-form {
  grid-template-columns: 1.2fr 1fr 0.8fr 1fr auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.purchase-grid {
  grid-template-columns: repeat(6, 1fr);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.purchase-grid button {
  grid-column: span 1;
}

.list {
  display: grid;
  gap: 7px;
}

.category-group {
  display: grid;
  gap: 5px;
}

.category-group h3 {
  margin: 6px 2px 0;
  color: var(--accent-dark);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.category-items {
  display: grid;
  gap: 6px;
}

.list-row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 12px;
}

.active-list-row {
  padding: 7px 9px;
}

.search-result-row {
  padding: 10px 12px;
}

.active-list-actions {
  flex-wrap: nowrap;
}

.active-list-row strong {
  font-size: 0.96rem;
}

.active-list-row .muted {
  font-size: 0.82rem;
}

.search-result-actions {
  flex-wrap: nowrap;
}

.list-row strong {
  display: block;
  font-size: 1.05rem;
}

.row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.square-btn {
  width: 44px;
  padding: 0;
  font-size: 1.4rem;
  line-height: 1;
}

.icon-btn {
  width: 44px;
  min-height: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  text-decoration: none;
}

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

.item-row {
  grid-template-columns: 1fr auto;
  align-items: center;
}

.compact-item-row {
  padding: 8px 10px;
  gap: 8px;
}

.item-summary {
  min-width: 0;
}

.item-summary strong {
  overflow-wrap: anywhere;
}

.item-meta {
  margin-top: 2px;
  font-size: 0.9rem;
}

.item-actions {
  align-content: start;
  flex-wrap: nowrap;
}

.action-menu {
  position: relative;
  width: 44px;
}

.action-menu summary {
  width: 44px;
  min-height: 44px;
  border-radius: 8px;
  background: #edf1ea;
  color: var(--ink);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  font-weight: 800;
  list-style: none;
}

.action-menu summary::-webkit-details-marker {
  display: none;
}

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 10;
  min-width: 150px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 30px rgba(23, 32, 26, 0.14);
  overflow: hidden;
}

.menu-panel a,
.menu-panel button {
  width: 100%;
  min-height: 44px;
  border-radius: 0;
  background: #fff;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: flex-start;
  padding: 10px 16px;
  text-decoration: none;
  gap: 8px;
}

.menu-panel button {
  font-weight: 700;
}

.menu-panel .menu-danger {
  color: var(--danger);
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  border-radius: 999px;
  padding: 0 10px;
  background: #edf1ea;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.notice {
  display: none;
  margin: 12px 0;
  padding: 12px;
}

.notice.visible {
  display: block;
}

.notice.error {
  display: block;
  border-color: #efb6b6;
  background: #fff0f0;
}

.preview-list {
  display: grid;
  gap: 6px;
  max-height: 320px;
  overflow: auto;
  margin: 12px 0;
}

.preview-list div {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
}

.wide {
  width: 100%;
}

dialog {
  width: min(520px, calc(100% - 24px));
  border: 0;
  border-radius: 10px;
  padding: 0;
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.35);
}

dialog form {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 38px;
  gap: 10px;
  padding: 16px;
}

.dialog-nav {
  position: static;
  width: 38px;
  min-height: 38px;
  border-radius: 8px;
  background: rgba(237, 241, 234, 0.94);
  color: var(--accent-dark);
  font-size: 1.7rem;
  line-height: 1;
}

.dialog-nav-prev {
  grid-column: 1;
  grid-row: 1;
}

.dialog-nav-next {
  grid-column: 3;
  grid-row: 1;
}

.dialog-nav:disabled {
  opacity: 0.24;
}

#purchase-title {
  grid-column: 2;
  grid-row: 1;
  align-self: center;
  padding: 0;
  text-align: center;
}

#purchase-form > input,
#purchase-form > label,
#purchase-form > .dialog-actions {
  grid-column: 1 / -1;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%) translateY(20px);
  max-width: calc(100% - 28px);
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  opacity: 0;
  padding: 12px 14px;
  transition: 160ms ease;
  pointer-events: none;
}

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

@media (max-width: 900px) {
  .topbar {
    grid-template-columns: minmax(0, 1fr) max-content;
    gap: 0;
    padding: 0;
    padding-top: env(safe-area-inset-top);
  }

  .login-card {
    padding: 16px;
  }

  .login-brand img {
    width: min(210px, 70vw);
    max-height: 66px;
  }

  .brand {
    padding: 7px 10px 6px;
    grid-column: 1;
    grid-row: 1;
    height: auto;
  }

  .brand img {
    width: min(118px, 36vw);
    max-height: 36px;
    transform: none;
  }

  .logout-link {
    position: absolute;
    top: 6px;
    right: 8px;
    font-size: 0.7rem;
    grid-column: auto;
    grid-row: auto;
  }

  .tabs {
    justify-content: center;
    gap: 8px;
  }

  .tabs-header {
    display: none;
  }

  .tabs-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    display: flex;
    grid-column: auto;
    grid-row: auto;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.96);
    border-top: 1px solid var(--line);
    box-shadow: 0 -8px 24px rgba(23, 32, 26, 0.08);
    backdrop-filter: blur(10px);
  }

  .tabs a {
    border-radius: 8px;
  }

  .shell {
    padding-bottom: calc(82px + env(safe-area-inset-bottom));
  }

  .metrics,
  .purchase-grid,
  .quick-add,
  .import-box,
  .item-form,
  .detail-item-form {
    grid-template-columns: 1fr;
  }

  .list-row {
    grid-template-columns: 1fr;
  }

  .active-list-row {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px;
    padding: 7px 8px;
  }

  .search-result-row {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 10px;
  }

  .active-list-actions {
    display: flex;
    width: auto;
  }

  .search-result-actions {
    display: flex;
    width: auto;
  }

  .active-list-actions .icon-btn {
    width: 34px;
    min-height: 34px;
    border-radius: 7px;
  }

  .active-list-actions .icon-btn svg {
    width: 17px;
    height: 17px;
  }

  .search-result-actions .square-btn,
  .search-result-actions .icon-btn {
    width: 42px;
    min-height: 42px;
  }

  .compact-item-row {
    grid-template-columns: 1fr auto;
  }

  .row-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    justify-content: stretch;
  }

  .item-actions {
    display: flex;
    width: auto;
  }

  .row-actions button,
  .row-actions .button {
    width: 100%;
  }

  .active-list-actions button {
    width: 34px;
  }

  .search-result-actions button,
  .search-result-actions .icon-btn {
    width: 42px;
  }

  .action-menu summary {
    width: 100%;
  }

  .item-actions button,
  .item-actions .action-menu,
  .item-actions .action-menu summary {
    width: 44px;
  }

  .dialog-nav {
    width: 38px;
    min-height: 38px;
    font-size: 1.7rem;
  }

  #purchase-title {
    text-align: center;
  }
}
