:root {
  --teal: #0d9488;
  --teal-dark: #0f766e;
  --teal-light: #ccfbf1;
  --teal-tint: #f0fdfa;
  --green-best: #15803d;
  --green-best-bg: #dcfce7;
  --green-best-border: #86efac;

  --bg: #f4f7f6;
  --surface: #ffffff;
  --surface-2: #f8fafa;
  --line: #e2e8e6;
  --line-strong: #cbd5d2;
  --text: #0f1f1d;
  --text-soft: #51625f;
  --text-faint: #8b9a97;
  --danger: #dc2626;
  --danger-bg: #fef2f2;

  --shadow-sm: 0 1px 2px rgba(13, 80, 73, .06);
  --shadow-md: 0 6px 20px rgba(13, 80, 73, .10);
  --radius: 16px;
  --radius-sm: 11px;
  --tap: 46px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

[data-theme="dark"] {
  --teal: #2dd4bf;
  --teal-dark: #5eead4;
  --teal-light: #134e4a;
  --teal-tint: #122c2a;
  --green-best: #4ade80;
  --green-best-bg: #11331f;
  --green-best-border: #1f6b3a;

  --bg: #0a1413;
  --surface: #122220;
  --surface-2: #0f1b1a;
  --line: #213734;
  --line-strong: #2f4a46;
  --text: #e8f3f1;
  --text-soft: #a3b8b4;
  --text-faint: #6f8581;
  --danger: #f87171;
  --danger-bg: #2a1414;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .3);
  --shadow-md: 0 8px 26px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  min-height: 100dvh;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--teal);
  color: #fff;
  padding: 10px 14px;
  border-radius: 0 0 10px 0;
  z-index: 100;
}
.skip-link:focus { left: 0; }

button { font-family: inherit; }

:focus-visible {
  outline: 3px solid var(--teal);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: linear-gradient(180deg, var(--surface), var(--surface) 70%, transparent);
  border-bottom: 1px solid var(--line);
}
[data-theme="dark"] .app-header {
  background: linear-gradient(180deg, var(--surface), var(--surface) 80%, transparent);
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}
.brand-tag {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: none;
  color: #fff;
  border-radius: 12px;
  background: linear-gradient(140deg, var(--teal), var(--teal-dark));
  box-shadow: var(--shadow-sm);
}
[data-theme="dark"] .brand-tag { color: #04201d; }
.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 1.12rem;
  line-height: 1.1;
  letter-spacing: -.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-text p {
  margin: 2px 0 0;
  font-size: .78rem;
  color: var(--text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: none;
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 12px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.icon-btn:hover { border-color: var(--line-strong); }
.icon-btn:active { transform: scale(.95); }
.ic-moon { display: none; }
[data-theme="dark"] .ic-sun { display: none; }
[data-theme="dark"] .ic-moon { display: block; }

/* ---------- Layout ---------- */
.app-main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}

.section-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.section-top h2 {
  margin: 0;
  font-size: 1rem;
  letter-spacing: -.01em;
}
.hint {
  font-size: .76rem;
  color: var(--text-faint);
  white-space: nowrap;
}

/* ---------- Product rows ---------- */
.product-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.product {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 12px;
  background: var(--surface-2);
  position: relative;
  transition: border-color .2s, box-shadow .2s, background .2s;
}
.product.is-best {
  border-color: var(--green-best-border);
  background: var(--green-best-bg);
  box-shadow: 0 0 0 1px var(--green-best-border);
}

.product-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.product-name {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  font-size: .92rem;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 4px 2px;
  border-bottom: 1.5px dashed transparent;
}
.product-name::placeholder { color: var(--text-faint); font-weight: 500; }
.product-name:focus {
  outline: none;
  border-bottom-color: var(--teal);
}
.best-badge {
  display: none;
  align-items: center;
  gap: 4px;
  font-size: .7rem;
  font-weight: 700;
  color: #fff;
  background: var(--green-best);
  padding: 4px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
[data-theme="dark"] .best-badge { color: #04201d; }
.product.is-best .best-badge { display: inline-flex; }

.remove-btn {
  flex: none;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text-soft);
  border-radius: 9px;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}
.remove-btn:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-bg);
}

.fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
}

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field-full { grid-column: 1 / -1; }
.field label {
  font-size: .72rem;
  font-weight: 600;
  color: var(--text-soft);
  letter-spacing: .01em;
}

.input-wrap { position: relative; display: flex; align-items: center; }
.input-wrap .prefix,
.input-wrap .suffix {
  position: absolute;
  font-size: .82rem;
  color: var(--text-faint);
  pointer-events: none;
}
.input-wrap .prefix { left: 11px; }
.input-wrap .suffix { right: 11px; }

input[type="number"],
input[type="text"].grow,
select {
  width: 100%;
  min-width: 0;
  height: var(--tap);
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  font-size: 1rem;
  padding: 0 12px;
  transition: border-color .15s, box-shadow .15s;
  -moz-appearance: textfield;
  appearance: none;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.input-wrap.has-prefix input { padding-left: 30px; }
.input-wrap.has-suffix input { padding-right: 34px; }

input:focus, select:focus {
  outline: none;
  border-color: var(--teal);
  box-shadow: 0 0 0 3px var(--teal-light);
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2351625f' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 18px;
  padding-right: 34px;
  cursor: pointer;
}
[data-theme="dark"] select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23a3b8b4' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

.unit-price {
  grid-column: 1 / -1;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
  padding-top: 9px;
  border-top: 1px dashed var(--line-strong);
  font-size: .8rem;
  color: var(--text-soft);
  min-height: 20px;
}
.unit-price strong {
  font-size: .98rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.product.is-best .unit-price strong { color: var(--green-best); }

/* ---------- Buttons ---------- */
.list-actions {
  display: flex;
  gap: 9px;
  margin-top: 14px;
  flex-wrap: wrap;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: var(--tap);
  padding: 0 16px;
  border-radius: 11px;
  border: 1px solid transparent;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s, color .15s;
}
.btn:active { transform: scale(.97); }
.btn-small { height: 38px; padding: 0 13px; font-size: .82rem; }

.btn-soft {
  background: var(--teal-tint);
  border-color: var(--teal-light);
  color: var(--teal-dark);
  flex: 1;
  min-width: 160px;
}
.btn-soft:hover { background: var(--teal-light); }
[data-theme="dark"] .btn-soft { color: var(--teal-dark); }

.btn-ghost {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--text-soft);
}
.btn-ghost:hover { border-color: var(--text-soft); color: var(--text); }

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { filter: brightness(1.05); }

/* ---------- Result ---------- */
.result-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.result-empty {
  text-align: center;
  color: var(--text-faint);
  font-size: .88rem;
  padding: 14px 4px;
  line-height: 1.5;
}
.result-rank {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.result-rank.best {
  border-color: var(--green-best-border);
  background: var(--green-best-bg);
}
.rank-pos {
  flex: none;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  font-weight: 700;
  font-size: .85rem;
  background: var(--surface);
  color: var(--text-soft);
  border: 1px solid var(--line);
}
.result-rank.best .rank-pos {
  background: var(--green-best);
  color: #fff;
  border-color: var(--green-best);
}
[data-theme="dark"] .result-rank.best .rank-pos { color: #04201d; }
.rank-info { flex: 1; min-width: 0; }
.rank-name {
  font-weight: 600;
  font-size: .92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rank-sub {
  font-size: .76rem;
  color: var(--text-soft);
  margin-top: 1px;
}
.rank-price {
  flex: none;
  text-align: right;
  font-weight: 700;
  font-size: .96rem;
  font-variant-numeric: tabular-nums;
}
.result-rank.best .rank-price { color: var(--green-best); }
.rank-diff {
  display: block;
  font-size: .72rem;
  font-weight: 600;
  color: var(--text-faint);
  margin-top: 1px;
}
.result-rank.best .rank-diff { color: var(--green-best); }

.savings-banner {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--green-best-bg);
  border: 1px solid var(--green-best-border);
  color: var(--green-best);
  font-size: .9rem;
  font-weight: 600;
  line-height: 1.35;
}
.savings-banner svg { flex: none; }

/* ---------- History ---------- */
.save-row { margin-bottom: 12px; }
.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.history-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.hist-main { flex: 1; min-width: 0; }
.hist-title {
  font-weight: 600;
  font-size: .88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hist-meta {
  font-size: .74rem;
  color: var(--text-soft);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hist-meta .win { color: var(--green-best); font-weight: 600; }
.hist-del {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text-faint);
  border-radius: 8px;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.hist-del:hover { color: var(--danger); border-color: var(--danger); }

.empty-note {
  text-align: center;
  color: var(--text-faint);
  font-size: .85rem;
  margin: 6px 0 0;
}

.app-foot {
  text-align: center;
  padding: 4px 8px 0;
}
.app-foot p {
  margin: 0;
  font-size: .74rem;
  color: var(--text-faint);
  line-height: 1.5;
}

/* ---------- Toast ---------- */
.toast-wrap {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(18px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  pointer-events: none;
  z-index: 80;
}
.toast {
  max-width: 100%;
  background: var(--text);
  color: var(--surface);
  padding: 11px 16px;
  border-radius: 12px;
  font-size: .86rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: 8px;
  animation: toastIn .25s ease;
}
.toast.ok { background: var(--teal-dark); color: #fff; }
[data-theme="dark"] .toast.ok { color: #04201d; }
.toast.leaving { animation: toastOut .25s ease forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(10px); } }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 24, 22, .5);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 90;
  animation: fadeIn .18s ease;
}
.modal {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-md);
  animation: popIn .2s ease;
}
.modal h3 { margin: 0 0 8px; font-size: 1.05rem; }
.modal p { margin: 0 0 18px; font-size: .9rem; color: var(--text-soft); line-height: 1.5; }
.modal-actions { display: flex; gap: 9px; justify-content: flex-end; }
.modal-actions .btn { flex: 1; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: scale(1); } }

@media (max-width: 380px) {
  .brand-text p { display: none; }
  .btn-soft { min-width: 130px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    transition-duration: .001ms !important;
  }
}
