/* SPDX-License-Identifier: MIT */

/* Colours follow the system setting.
   Every text pair meets WCAG AA (4.5:1) and field borders 3:1, in both themes. */
:root {
  color-scheme: light dark;
  --bg: light-dark(#f3f5f8, #0f1216);
  --surface: light-dark(#ffffff, #171b21);
  --field: light-dark(#ffffff, #1f242c);
  --text: light-dark(#111827, #e6e9ee);
  --muted: light-dark(#4b5563, #a7b0bd);
  --placeholder: light-dark(#5d6574, #939daa);
  --border: light-dark(#e2e6ec, #2a3038);
  --field-border: light-dark(#7b8597, #6b7686);
  --primary: light-dark(#1d4ed8, #8ab4ff);
  --primary-hover: light-dark(#1e40af, #b3ceff);
  --primary-soft: light-dark(#e8eefd, #1c2a45);
  --on-primary: light-dark(#ffffff, #0b1220);
  --danger: light-dark(#b91c1c, #ff8f87);
  --danger-soft: light-dark(#fdecec, #3a1d1d);
  --hover: light-dark(#eef1f5, #232931);
  --notice-bg: light-dark(#fef3c7, #3d2e05);
  --notice-text: light-dark(#713f12, #fde68a);
  --shadow: light-dark(rgb(15 23 42 / 0.06), rgb(0 0 0 / 0.4));
  --radius: 14px;
  --radius-small: 10px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Elements with their own display value would otherwise ignore the hidden attribute. */
[hidden] {
  display: none !important;
}

body {
  min-height: 100dvh;
  margin: 0;
  padding: 0.75rem clamp(0.75rem, 4vw, 1.5rem) 1.5rem;
  background: var(--bg);
  color: var(--text);
  font:
    1rem/1.5 system-ui,
    sans-serif;
}

.site-header,
main,
.site-footer {
  max-width: 36rem;
  margin-inline: auto;
}

a {
  color: var(--primary);
  text-underline-offset: 0.15em;
}

a:hover {
  color: var(--primary-hover);
}

button {
  color: inherit;
  font: inherit;
  cursor: pointer;
}

:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Icons */

.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.icon {
  flex: none;
  width: 1.25rem;
  height: 1.25rem;
}

.icon-wrap {
  display: contents;
}

/* Header */

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-block: 0 0.75rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0;
  font-size: clamp(1.2rem, 1rem + 1vw, 1.5rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.brand img {
  flex: none;
  width: 2rem;
  height: 2rem;
  /* The logo is a full square; rounded, it sits like an app icon next to the name. */
  border-radius: 0.45rem;
}

.header-actions {
  display: flex;
  flex: none;
  gap: 0.25rem;
}

/* Buttons */

.icon-button {
  display: inline-grid;
  place-items: center;
  min-width: 2.75rem;
  min-height: 2.75rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  line-height: 1;
}

.icon-button:hover {
  background: var(--hover);
  color: var(--text);
}

.avatar-button {
  overflow: hidden;
}

.avatar {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  object-fit: cover;
}

.primary-button,
.add-button,
.secondary-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 2.75rem;
  padding: 0.6rem 1rem;
  border-radius: var(--radius-small);
  font-weight: 600;
  text-decoration: none;
}

.primary-button {
  width: 100%;
  border: 0;
  background: var(--primary);
  color: var(--on-primary);
}

.primary-button:hover {
  background: var(--primary-hover);
  color: var(--on-primary);
}

.add-button {
  width: 100%;
  min-height: 3rem;
  border: 0;
  background: var(--primary-soft);
  color: var(--primary);
}

.add-button:hover {
  background: var(--primary);
  color: var(--on-primary);
}

.secondary-button {
  border: 1px solid var(--field-border);
  background: transparent;
  color: var(--primary);
}

.secondary-button:hover {
  background: var(--hover);
}

.text-button {
  min-width: 2.75rem;
  min-height: 2.75rem;
  margin-inline-end: -0.5rem;
  padding: 0 0.5rem;
  border: 0;
  border-radius: var(--radius-small);
  background: transparent;
  color: var(--primary);
  font-weight: 600;
}

.text-button:hover {
  background: var(--hover);
}

/* Converter card */

.card {
  display: grid;
  gap: 0.75rem;
  padding: clamp(0.75rem, 3vw, 1.25rem);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow:
    0 1px 2px var(--shadow),
    0 8px 24px var(--shadow);
}

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-height: 2.75rem;
}

.list-header h2 {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.875rem;
}

/* On a first visit the rate date arrives after the first paint; keep its line reserved. */
.rates-info {
  min-block-size: 1lh;
  text-align: center;
}

.notice {
  margin: 0;
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius-small);
  background: var(--notice-bg);
  color: var(--notice-text);
  font-size: 0.9rem;
}

.currency-list {
  display: grid;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Converting: name on the left, amount on the right. Editing: remove, name, handle. */
.currency-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(9.5rem, 1.4fr);
  align-items: center;
  gap: 0.75rem;
  /* The same height in both modes, so switching doesn't make the list jump. */
  min-height: 4rem;
  padding-block: 0.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.currency-row:last-child {
  border-bottom: 0;
}

.remove-button,
.drag-handle {
  display: none;
}

.is-editing .currency-row {
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 0.25rem;
}

.is-editing .amount-field {
  display: none;
}

.is-editing :is(.remove-button, .drag-handle) {
  display: inline-grid;
}

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

.remove-button:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

.drag-handle {
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.currency-row.dragging {
  position: relative;
  z-index: 1;
  border-radius: var(--radius-small);
  box-shadow: 0 8px 24px var(--shadow);
}

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

body:has(.dragging) {
  user-select: none;
}

.move-menu,
.undo-toast,
.update-toast {
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 8px 24px var(--shadow);
}

.move-menu {
  padding: 0.25rem;
}

.move-menu:popover-open {
  display: grid;
}

.move-menu button {
  min-height: 2.75rem;
  padding: 0 1rem;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: start;
}

.move-menu button:hover:enabled {
  background: var(--hover);
}

.move-menu button:disabled {
  color: var(--muted);
  cursor: default;
}

/* Below the handle and ending at its edge; above it when there is no room below. */
@supports (position-area: block-end) {
  .move-menu {
    inset: auto;
    margin: 0.25rem 0;
    position-area: block-end span-inline-start;
    position-try-fallbacks: flip-block;
  }
}

/* No anchor positioning: the script places the menu. */
@supports not (position-area: block-end) {
  .move-menu {
    inset: auto;
    margin: 0;
  }
}

.undo-toast {
  inset: auto 0 max(1rem, env(safe-area-inset-bottom));
  width: min(34rem, 100% - 1.5rem);
  margin: 0 auto;
  padding: 0.25rem 0.5rem 0.25rem 1rem;
}

.undo-toast:popover-open {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.undo-toast p {
  flex: 1;
  min-width: 0;
  margin: 0;
}

/* At the top: the undo toast and the on-screen keyboard take the bottom. */
.update-toast {
  inset: max(1rem, env(safe-area-inset-top)) 0 auto;
  width: min(34rem, 100% - 1.5rem);
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
  margin: 0 auto;
  padding: 0.25rem 0.5rem 0.25rem 1rem;
}

.update-header {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.update-header p {
  flex: 1;
  min-width: 0;
  margin: 0;
}

.whats-new summary {
  padding-block: 0.6rem;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
}

.whats-new ul {
  display: grid;
  gap: 0.5rem;
  margin: 0 0 0.75rem;
  padding: 0 0.5rem 0 0;
  list-style: none;
}

.whats-new a {
  display: inline-block;
  margin-bottom: 0.75rem;
}

/* Room to scroll everything above the toast, so it never covers the last controls. */
body:has(.undo-toast:popover-open) {
  padding-bottom: 5rem;
}

.currency-label {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
  cursor: pointer;
}

.is-editing .currency-label {
  cursor: default;
}

.flag {
  font-size: 1.6rem;
}

/* Flag emoji are wider than tall and the coin icons are square: a fixed box keeps the
   text next to them in one line. */
:is(.flag, .result-flag) {
  display: inline-grid;
  flex: none;
  place-items: center;
  width: 1.25em;
  height: 1em;
  line-height: 1;
}

/* Bitcoin and metal icons, sized like the flag emoji next to them. */
.coin {
  display: block;
  width: 1em;
  height: 1em;
}

.currency-text {
  display: grid;
  min-width: 0;
  line-height: 1.25;
}

.code {
  font-size: 1.05rem;
  font-weight: 700;
}

.name {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.8125rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.amount-field {
  display: flex;
  align-items: center;
  min-width: 0;
  min-height: 3rem;
  border: 1px solid var(--field-border);
  border-radius: var(--radius-small);
  background: var(--field);
}

/* The row whose amount drives the others. */
.is-base .amount-field {
  border-color: var(--primary);
  box-shadow: inset 0 0 0 1px var(--primary);
  background: var(--primary-soft);
}

.amount-field:focus-within {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

.symbol {
  padding-inline-start: 0.75rem;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}

.amount {
  flex: 1;
  width: 100%;
  min-width: 0;
  padding: 0.5rem 0.75rem;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 1.25rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: end;
  /* Only for amounts too long even at the smallest size fitAmount allows. */
  text-overflow: ellipsis;
}

/* The surrounding field shows the focus ring. */
.amount:focus-visible {
  outline: none;
}

.amount::placeholder,
#currency-search::placeholder,
#bunker-input::placeholder {
  color: var(--placeholder);
  opacity: 1;
}

/* Dialogs */

dialog {
  width: min(30rem, 100% - 1.5rem);
  max-height: min(42rem, 100dvh - 1.5rem);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px var(--shadow);
}

dialog:modal {
  overscroll-behavior: contain;
}

dialog::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgb(0 0 0 / 0.55);
}

/* Top-aligned, so the results stay above an on-screen keyboard. */
#add-dialog:modal {
  margin-block: min(8dvh, 4rem) auto;
}

.dialog-body {
  display: grid;
  gap: 0.75rem;
  padding: 1rem 1.25rem 1.25rem;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-inline-end: -0.5rem;
}

.dialog-header h2 {
  margin: 0;
  font-size: 1.25rem;
}

.search-label {
  font-weight: 600;
}

#currency-search,
#bunker-input {
  width: 100%;
  min-height: 2.75rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--field-border);
  border-radius: var(--radius-small);
  background: var(--field);
  color: var(--text);
  font: inherit;
}

#currency-search {
  font-size: 1.05rem;
}

#currency-search:focus-visible,
#bunker-input:focus-visible {
  outline-offset: 0;
}

.search-results {
  display: grid;
  gap: 0.125rem;
  max-height: min(24rem, 50dvh);
  margin: 0 -0.5rem;
  padding: 0.25rem 0.5rem;
  overflow-y: auto;
  list-style: none;
}

.result {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  min-height: 2.75rem;
  padding: 0.5rem 0.75rem;
  border: 0;
  border-radius: var(--radius-small);
  background: transparent;
  text-align: start;
}

.result:hover:not([aria-disabled="true"]) {
  background: var(--hover);
}

/* Already in the list: shown, but there is nothing to add. */
.result[aria-disabled="true"] {
  cursor: default;
}

.result[aria-disabled="true"] .result-code {
  color: var(--muted);
}

/* Inside the scrolling list an outer ring would be clipped. */
.result:focus-visible {
  outline-offset: -3px;
}

.result-flag {
  font-size: 1.4rem;
}

.result-code {
  flex: none;
  min-width: 3.25rem;
  font-weight: 700;
}

.result-name {
  min-width: 0;
  color: var(--muted);
}

/* The country that matched, and whether the currency is already in the list. */
.result-details {
  display: block;
  font-size: 0.875rem;
}

@media (prefers-reduced-motion: no-preference) {
  dialog[open] {
    opacity: 1;
    translate: 0;

    @starting-style {
      opacity: 0;
      translate: 0 0.75rem;
    }
  }

  dialog {
    opacity: 0;
    translate: 0 0.75rem;
    transition:
      opacity 0.18s ease-out,
      translate 0.18s ease-out,
      display 0.18s allow-discrete,
      overlay 0.18s allow-discrete;
  }

  dialog[open]::backdrop {
    background: rgb(0 0 0 / 0.55);

    @starting-style {
      background: rgb(0 0 0 / 0);
    }
  }

  dialog::backdrop {
    background: rgb(0 0 0 / 0);
    transition:
      background-color 0.18s ease-out,
      display 0.18s allow-discrete,
      overlay 0.18s allow-discrete;
  }
}

/* Account dialog (nostr/account.js). */
.account-section p,
.account-section h3 {
  margin: 0 0 0.75rem;
}

.account-section h3 {
  margin-top: 1.25rem;
  font-size: 1.05rem;
}

.sign-in {
  display: grid;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.option-button {
  display: grid;
  min-height: 2.75rem;
  padding: 0.7rem 0.9rem;
  border: 1px solid var(--field-border);
  border-radius: var(--radius-small);
  background: var(--field);
  text-align: start;
}

.option-button:hover {
  background: var(--hover);
}

.option-button[aria-expanded="true"] {
  border-color: var(--primary);
  box-shadow: inset 0 0 0 1px var(--primary);
}

.option-label {
  font-weight: 600;
}

.option-hint {
  color: var(--muted);
  font-size: 0.875rem;
}

.signer-connect {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0.75rem;
  justify-items: center;
  margin-bottom: 0.75rem;
  text-align: center;
}

.signer-connect .hint {
  margin: 0;
}

/* Dark modules on white in both themes: scanners expect that. */
.qr {
  width: min(14rem, 100%);
  height: auto;
  aspect-ratio: 1;
  border-radius: var(--radius-small);
  background: #fff;
}

.qr path {
  fill: #000;
}

.bunker-form {
  display: grid;
  gap: 0.25rem;
  width: 100%;
  text-align: start;
}

.bunker-row {
  display: flex;
  gap: 0.5rem;
}

#bunker-input {
  flex: 1;
  min-width: 0;
}

.profile {
  display: flex;
  gap: 1rem;
  align-items: center;
  margin-bottom: 1rem;
}

.profile-avatar {
  display: grid;
  flex: none;
  place-items: center;
  width: 4rem;
  height: 4rem;
  overflow: hidden;
  border-radius: 50%;
  background: var(--hover);
  color: var(--muted);
}

.profile-avatar .icon {
  width: 2rem;
  height: 2rem;
}

.profile-picture {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-text {
  display: grid;
  min-width: 0;
}

.profile-name {
  font-size: 1.15rem;
  font-weight: 700;
}

.profile-npub {
  overflow: hidden;
  color: var(--muted);
  font-size: 0.875rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#account-status:empty {
  display: none;
}

/* About and footer */

.about {
  margin-top: 2rem;
}

.about h2 {
  margin: 0 0 0.5rem;
  font-size: 1.15rem;
}

.about p,
.about ul {
  margin: 0 0 0.75rem;
}

.about ul {
  padding-inline-start: 1.25rem;
  color: var(--muted);
}

.about li + li {
  margin-top: 0.25rem;
}

.site-footer {
  margin-top: 2rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.875rem;
}

.site-footer p {
  margin: 0 0 0.5rem;
}

.not-found {
  max-width: 36rem;
  margin: 4rem auto;
  text-align: center;
}

/* Very narrow phones: the amount gets its own line so long numbers stay readable. */
@media (max-width: 22rem) {
  .currency-row {
    grid-template-columns: minmax(0, 1fr);
    gap: 0.4rem;
  }
}

@media (prefers-reduced-motion: no-preference) {
  body,
  .card,
  .amount-field,
  button {
    transition:
      background-color 0.2s ease,
      border-color 0.2s ease,
      color 0.2s ease;
  }
}

/* Windows high contrast and similar: keep borders the theme colours would hide. */
@media (forced-colors: active) {
  .is-base .amount-field {
    border-width: 3px;
  }

  .icon-button,
  .text-button,
  .add-button {
    border: 1px solid ButtonText;
  }
}
