:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --line: #d8dde5;
  --text: #172033;
  --muted: #657086;
  --accent: #16734a;
  --danger: #a83232;
  --manual: #fff7d7;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior-y: contain;
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  min-height: 36px;
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover {
  border-color: var(--accent);
}

button:disabled {
  cursor: wait;
  opacity: 0.72;
}

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

.icon-button {
  width: 36px;
  min-width: 36px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  padding: 0;
}

.icon-button svg {
  width: 18px;
  height: 18px;
  stroke-width: 2;
}

.edit-icon-close {
  display: none;
}

.product-row.is-editing .edit-icon-open {
  display: none;
}

.product-row.is-editing .edit-icon-close {
  display: block;
}

input {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 9px;
  background: #fff;
}

label,
.label {
  display: grid;
  gap: 3px;
  color: var(--muted);
  font-size: 12px;
}

.page {
  width: min(1180px, calc(100vw - 24px));
  margin: 0 auto;
  padding: 20px 0 28px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
}

h2 {
  font-size: 16px;
}

.add-form,
.exchange-form,
.refresh-all-form {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(260px, 2fr) minmax(120px, 160px) auto;
  gap: 10px;
  align-items: end;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 16px;
}

.add-form {
  grid-template-columns: minmax(180px, 1fr) minmax(190px, 1.2fr) minmax(190px, 1.2fr) minmax(110px, 140px) minmax(110px, 140px) 36px;
  align-items: center;
}

.add-submit {
  justify-self: end;
}

.add-details-toggle {
  display: none;
}

.add-name-wrap {
  position: relative;
}

.autocomplete-list {
  position: absolute;
  z-index: 20;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  display: grid;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: 0 8px 20px rgb(23 32 51 / 12%);
}

.autocomplete-list[hidden] {
  display: none;
}

.autocomplete-option {
  width: 100%;
  min-height: 34px;
  border: 0;
  border-radius: 0;
  padding: 7px 9px;
  text-align: left;
  white-space: normal;
}

.autocomplete-option + .autocomplete-option {
  border-top: 1px solid var(--line);
}

.autocomplete-option:hover,
.autocomplete-option:focus {
  background: #f1f5f3;
}

.bottom-actions {
  display: flex;
  align-items: stretch;
  gap: 10px;
  margin-top: 18px;
}

.exchange-form {
  grid-template-columns: minmax(180px, 260px) auto;
  margin-bottom: 0;
}

.refresh-all-form {
  grid-template-columns: auto;
  align-items: end;
  margin-bottom: 0;
}

.refresh-status {
  display: none;
  align-items: center;
  gap: 7px;
}

.refresh-all-form.htmx-request .refresh-label {
  display: none;
}

.refresh-all-form.htmx-request .refresh-status {
  display: inline-flex;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

.pull-refresh-indicator {
  position: fixed;
  z-index: 50;
  top: -42px;
  left: 50%;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--accent);
  box-shadow: 0 8px 20px rgb(23 32 51 / 14%);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 0);
  transition: opacity 120ms ease, transform 160ms ease;
}

.pull-refresh-indicator.is-visible {
  opacity: 1;
}

.pull-refresh-indicator.is-ready {
  color: var(--text);
}

.notice {
  border: 1px solid #a9d6bf;
  background: #edf8f2;
  color: #185d3e;
  border-radius: 6px;
  padding: 9px 10px;
  margin-bottom: 12px;
}

.notice.error {
  border-color: #e2b2b2;
  background: #fff1f1;
  color: #842727;
}

.error-detail-list {
  display: grid;
  gap: 5px;
  margin: 8px 0 0;
  padding-left: 18px;
}

.error-detail-list li {
  overflow-wrap: anywhere;
}

.error-detail-list span {
  color: #a04444;
  font-weight: 700;
}

.error-detail-list a {
  color: inherit;
}

.list-section {
  margin-top: 16px;
}

.section-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  color: var(--muted);
  margin-bottom: 8px;
}

.product-list {
  display: grid;
  gap: 8px;
}

.product-row {
  display: grid;
  grid-template-columns: 24px 32px minmax(0, 1fr) minmax(190px, max-content) auto;
  gap: 8px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  animation: row-appear 170ms ease-out;
  transition: opacity 140ms ease, transform 140ms ease;
}

.sortable-list.is-sorting {
  touch-action: none;
}

.drag-handle {
  width: 24px;
  min-width: 24px;
  height: 36px;
  display: inline-grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0;
  cursor: grab;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

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

.is-dragging-product,
.is-dragging-product * {
  cursor: grabbing;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.drag-handle svg {
  width: 18px;
  height: 18px;
}

.drag-active {
  opacity: 0.86;
  transform: scale(0.995);
  box-shadow: 0 8px 22px rgb(23 32 51 / 14%);
}

.product-row.inactive {
  opacity: 0.68;
}

.product-row.row-moving-out {
  opacity: 0;
  transform: translateY(4px) scale(0.99);
}

.product-row.inactive .name-field input {
  text-decoration: line-through;
}

.toggle-cell {
  display: grid;
  place-items: center;
}

.toggle-cell input {
  width: 20px;
  min-height: 20px;
}

.edit-form {
  display: contents;
}

.name-field {
  grid-column: 3;
  min-width: 0;
}

.price-stack {
  grid-column: 4;
}

.row-controls {
  grid-column: 5;
}

.product-row.is-editing .edit-form {
  grid-column: 3 / 5;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(190px, max-content);
  gap: 8px;
  align-items: center;
}

.product-row.is-editing .name-field {
  grid-column: 1;
}

.product-row.is-editing .price-stack {
  grid-column: 2;
}

.link-field,
.link-sk-field,
.manual-field,
.manual-eur-field,
.row-actions {
  display: none;
}

.product-row.is-editing .link-field,
.product-row.is-editing .link-sk-field,
.product-row.is-editing .manual-field,
.product-row.is-editing .manual-eur-field {
  display: grid;
  grid-column: 1 / -1;
}

.row-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
}

.row-actions {
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
  min-width: 80px;
}

.product-row.is-editing .row-actions {
  display: flex;
}

.row-action {
  margin: 0;
  display: contents;
}

.price-stack {
  display: grid;
  gap: 2px;
  min-width: 190px;
  justify-self: end;
  padding: 5px 6px;
  text-align: right;
}

.price-line {
  display: flex;
  align-items: baseline;
  justify-content: flex-end;
  gap: 4px;
  white-space: nowrap;
}

.source-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.15;
}

.source-label {
  min-width: 10px;
  text-align: center;
}

.price-line strong {
  color: var(--text);
  font-size: 14px;
  line-height: 1.15;
  text-align: right;
  white-space: nowrap;
  font-weight: 500;
}

.converted-price {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.15;
  white-space: nowrap;
  min-width: 0;
}

.price-line.cheaper .source-label,
.price-line.cheaper strong,
.price-line.cheaper .converted-price {
  font-weight: 800;
}

.price-line.cheaper .source-label,
.price-line.cheaper .converted-price {
  color: var(--text);
}

.price-line.manual-active {
  margin: 0 -3px;
  padding: 1px 3px;
}

.empty {
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 12px;
}

@keyframes row-appear {
  from {
    opacity: 0;
    transform: translateY(-4px) scale(0.995);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .product-row {
    animation: none;
    transition: none;
  }

  .spinner {
    animation: none;
  }
}

@media (max-width: 900px) {
  body {
    font-size: 14px;
  }

  .page {
    width: min(100vw - 12px, 1180px);
    padding: 10px 0 18px;
  }

  h1 {
    font-size: 20px;
  }

  h2 {
    font-size: 14px;
  }

  .topbar {
    margin-bottom: 10px;
  }

  .add-form,
  .exchange-form,
  .refresh-all-form,
  .bottom-actions {
    grid-template-columns: 1fr;
  }

  .add-form {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(74px, 96px) minmax(74px, 96px) 36px;
  }

  .add-name-wrap {
    grid-column: 1 / 5;
  }

  .add-details-toggle {
    display: inline-grid;
    grid-column: 5;
    justify-self: end;
  }

  .add-details-toggle svg {
    transition: transform 140ms ease;
  }

  .add-form.is-expanded .add-details-toggle svg {
    transform: rotate(180deg);
  }

  .add-link-field {
    grid-column: 1;
  }

  .add-link-sk-field {
    grid-column: 2;
  }

  .add-manual-field {
    grid-column: 3;
  }

  .add-manual-eur-field {
    grid-column: 4;
  }

  .add-submit {
    grid-column: 5;
  }

  .add-form:not(.is-expanded) .add-link-field,
  .add-form:not(.is-expanded) .add-link-sk-field,
  .add-form:not(.is-expanded) .add-manual-field,
  .add-form:not(.is-expanded) .add-manual-eur-field,
  .add-form:not(.is-expanded) .add-submit {
    display: none;
  }

  .add-form,
  .exchange-form,
  .refresh-all-form {
    gap: 8px;
    padding: 9px;
    margin-bottom: 10px;
  }

  .bottom-actions {
    display: grid;
    gap: 8px;
    margin-top: 12px;
  }

  .list-section {
    margin-top: 12px;
  }

  .section-heading {
    margin-bottom: 6px;
  }

  .product-list {
    gap: 2px;
  }

  .product-row {
    grid-template-columns: 22px 22px minmax(0, 1fr) minmax(118px, max-content) 36px;
    gap: 5px;
    align-items: center;
    border: 0;
    border-radius: 0;
    padding: 4px 2px;
  }

  .drag-handle {
    width: 22px;
    min-width: 22px;
    height: 34px;
  }

  .drag-handle svg {
    width: 17px;
    height: 17px;
  }

  .product-row.is-editing {
    align-items: start;
  }

  .toggle-cell {
    place-items: center;
  }

  .product-row.is-editing .toggle-cell {
    align-self: start;
    padding-top: 8px;
  }

  .toggle-cell input {
    width: 18px;
    min-height: 18px;
  }

  .product-row.is-editing .edit-form {
    grid-column: 3 / 5;
    grid-template-columns: minmax(0, 1fr);
    gap: 5px;
    align-items: center;
  }

  .product-row.is-editing .name-field,
  .product-row.is-editing .price-stack {
    grid-column: 1;
  }

  .name-field input {
    min-height: 34px;
    padding: 6px 8px;
  }

  .link-field,
  .link-sk-field,
  .manual-field,
  .manual-eur-field {
    grid-column: 1 / -1;
  }

  .price-stack {
    min-width: 118px;
    padding: 2px 0;
  }

  .price-line {
    display: grid;
    grid-template-columns: auto auto max-content;
    column-gap: 3px;
    justify-items: end;
  }

  .converted-price {
    grid-column: 3;
    font-size: 10px;
  }

  .price-line strong {
    font-size: 13px;
  }

  .row-controls {
    min-width: 0;
    justify-content: flex-end;
  }

  .product-row.is-editing .row-controls {
    grid-column: 1 / -1;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .row-actions .icon-button {
    width: 36px;
    min-width: 36px;
  }

  .add-form button,
  .exchange-form button,
  .refresh-all-form button {
    width: 100%;
  }

  .add-form .add-submit {
    width: 36px;
  }
}
