/*
 * Currency — visual language.
 *
 * Aligned to Apple's Human Interface Guidelines rather than to a hairline,
 * near-monochrome aesthetic. The earlier pass took restraint too far: on a real
 * iPhone the type sat well under HIG's scale, the weights read as washed out,
 * and with no surface differentiation the elements ran together.
 *
 * Type follows the iOS Dynamic Type scale — Large Title 34, Title 2 22,
 * Headline 17 semibold, Subhead 15, Footnote 13. Nothing carrying meaning sits
 * below 13, and every control clears the 44pt minimum touch target.
 *
 * Colour follows the grouped-list model: a recessive page background with
 * content raised onto a surface, which is what separates elements on iOS rather
 * than borders. Both themes are designed, not derived — this app is used in
 * bright sun abroad and in dim restaurants, often the same day.
 */

:root {
  /* systemGroupedBackground / secondarySystemGroupedBackground */
  --bg: #f2f2f7;
  --surface: #ffffff;

  /* label / secondaryLabel / tertiaryLabel */
  --text: #000000;
  --text-secondary: rgba(60, 60, 67, 0.62);
  --text-tertiary: rgba(60, 60, 67, 0.32);

  --separator: rgba(60, 60, 67, 0.22);
  --accent: #007aff;              /* systemBlue */
  --accent-soft: rgba(0, 122, 255, 0.12);
  --press: rgba(0, 0, 0, 0.06);
  --fill: rgba(118, 118, 128, 0.12);   /* tertiarySystemFill */

  /* Flags keep more of their colour now that the palette is not monochrome,
     but still sit slightly below full saturation so they support the row
     rather than dominate it. */
  --flag-mute: saturate(0.86);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --text: #ffffff;
    --text-secondary: rgba(235, 235, 245, 0.62);
    --text-tertiary: rgba(235, 235, 245, 0.32);
    --separator: rgba(84, 84, 88, 0.7);
    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.2);
    --press: rgba(255, 255, 255, 0.08);
    --fill: rgba(118, 118, 128, 0.24);
    --flag-mute: saturate(0.8) brightness(0.94);
  }
}

* { box-sizing: border-box; }

/*
 * iOS interaction defaults.
 *
 * touch-action: manipulation disables double-tap-to-zoom on controls. Without
 * it, typing "1000" quickly reads as repeated double taps and Safari zooms the
 * page instead of entering digits.
 *
 * user-select: none stops a slightly-held tap raising the text magnifier, and
 * keeps the drag handle from selecting text mid-reorder.
 */
button, .row, .key, .tab, .edit-item, .handle, .range, .selector {
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

/* The hidden attribute must win over any display rule. Without this, an element
   styled `display: grid` or `display: flex` ignores [hidden] entirely. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  /* No page-level scroll: the board scrolls inside itself. Without this, iOS
     rubber-bands the whole document and the keypad drifts under the thumb. */
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
               Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.app {
  display: flex;
  flex-direction: column;
  /* dvh so the layout does not jump as browser chrome collapses, with a vh
     fallback: dvh needs iOS 15.4, and older versions drop the declaration
     entirely, leaving .app with auto height and a floating keypad. */
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
}

/* ---------------------------------------------------------------- header */

.header {
  padding: max(env(safe-area-inset-top), 12px) 16px 8px;
  flex-shrink: 0;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

/* Large Title, 34pt bold. */
.title {
  margin: 0;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.011em;
  line-height: 1.12;
}

.icon-button {
  appearance: none;
  border: 0;
  background: none;
  color: var(--accent);
  /* 44pt minimum touch target, per HIG. */
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: -10px;
  border-radius: 22px;
  cursor: pointer;
}

.icon-button svg { width: 25px; height: 25px; }
.icon-button:active { background: var(--press); }

/* Footnote, 13pt. Always states the ECB publication date — the app never
   implies a rate is live, since on a weekend the newest one is Friday's. */
.freshness {
  margin: 2px 0 0;
  font-size: 13px;
  font-weight: 400;
  color: var(--text-secondary);
}

.freshness .offline::before { content: " · "; }

/* ----------------------------------------------------------------- board */

.board-scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px 16px 10px;
}

/* Content is raised onto a surface. On iOS this, not a border, is what
   separates a list from the page behind it. */
.board {
  background: var(--surface);
  border-radius: 12px;
  overflow: hidden;
}

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 74px;
  padding: 10px 16px;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

/* Separators inset to the text column, as iOS grouped lists do, so the flag
   column reads as a continuous edge. */
.row + .row::before {
  content: "";
  position: absolute;
  top: 0;
  left: 70px;
  right: 0;
  height: 1px;
  background: var(--separator);
}

.row:active { background: var(--press); }

/* Circular and roughly double the previous size, matching the reference app.
   A rectangle at 28px read as a detail; at 38px round it reads as the row's
   identifying mark and is findable without reading anything. preserveAspectRatio
   is overridden to slice, so the 3:2 flag fills the circle instead of letterboxing. */
.flag {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: block;
  flex: 0 0 auto;
  filter: var(--flag-mute);
  box-shadow: inset 0 0 0 0.5px rgba(0, 0, 0, 0.18);
}

.row-labels {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

/* The currency name now leads, at Title 3 weight. It is what a traveller
   recognises; the code is confirmation. */
.row-name {
  font-size: 21px;
  font-weight: 600;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Subhead, secondary. */
.row-code {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0;
}

.row-values {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  text-align: right;
}

/* Title 2, 22pt. The figure is the reason the screen exists, so it is the
   largest thing on the row by a clear margin. */
/* Lifted: the converted figure is the answer the screen exists to give, so it
   outweighs the name beside it rather than merely matching it. */
.row-value {
  font-size: 27px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

/* The pending half of an operation, e.g. "1,200 ÷" awaiting the divisor. */
.row-pending {
  font-size: 13px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  margin-top: 1px;
}

.row.is-base { background: var(--accent-soft); }
/* Accent the name and the figure, not the code: the code is the secondary line
   now, and tinting it left the row's emphasis in the wrong place. */
.row.is-base .row-name,
.row.is-base .row-value { color: var(--accent); }
.row.is-base .row-value { font-weight: 600; }

/* Before anything is typed everything reads as zero, quietly. */
.board.is-zero .row-value { color: var(--text-tertiary); }
.board.is-zero .row.is-base .row-value { color: var(--accent); opacity: 0.5; }

/* A favorite with no current quote — BGN after Bulgaria joined the euro. */
.row.is-unavailable .row-value { color: var(--text-tertiary); }
.row.is-unavailable { cursor: default; }

/* ----------------------------------------------------------- entry sheet */

/* Slides up over the board rather than docking beneath it, so the keys can be
   large and the list is never squeezed. It animates: appearing instantly would
   lose the relationship between the tapped row and the sheet it opened. */
.keypad-sheet {
  display: flex;
  flex-direction: column;
  padding-bottom: max(env(safe-area-inset-bottom), 10px);
  transform: translateY(100%);
  transition: transform 260ms cubic-bezier(0.32, 0.72, 0, 1);
  will-change: transform;
}

.keypad-sheet.is-open { transform: translateY(0); }

.entry-currency {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.entry-labels { display: flex; flex-direction: column; min-width: 0; }
.entry-name { font-size: 21px; font-weight: 600; letter-spacing: -0.02em; }
.entry-code { font-size: 15px; font-weight: 500; color: var(--text-secondary); }

/* Takes the space above the keys and centres the amount in it, rather than
   pinning it to the top and leaving a void. The reference does the same with
   its placeholder. */
.entry-display {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 8px 20px;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
  text-align: right;
}

/* The amount being typed, as the largest thing on the screen. */
.entry-amount {
  font-size: 52px;
  font-weight: 600;
  letter-spacing: -0.035em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  overflow: hidden;
  text-overflow: ellipsis;
}

.entry-amount.is-zero { color: var(--text-tertiary); }

.entry-pending {
  font-size: 15px;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  min-height: 19px;
}


/* Magnitudes multiply what is entered. Colón and yen amounts run to five and
   six figures; without these every entry ends in four taps of zero. */
.magnitudes {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 4px 16px 4px;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
}

/* ---------------------------------------------------------------- keypad */

.keypad {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 4px 16px 0;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
}

.done-button {
  appearance: none;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 19px;
  font-weight: 600;
  min-height: 56px;
  margin: 12px auto 0;
  max-width: 560px;
  width: calc(100% - 32px);
  cursor: pointer;
}

.done-button:active { opacity: 0.85; }

/* Keys are raised onto the same surface as the board. Transparent keys read as
   loose text on a phone rather than as controls. */
.key {
  appearance: none;
  border: 0;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 30px;
  font-weight: 400;
  /* Well past the 44pt minimum. The sheet is given over entirely to the keypad
     now, so the height that the live-results list used to occupy goes into the
     targets instead — which is what was actually being complained about. */
  min-height: 66px;
  border-radius: 12px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}

/* Tinted rather than white, so the magnitude strip reads as a different kind
   of key from the digits below it. Declared after .key so it wins. */
.key-mag {
  background: var(--fill);
  color: var(--accent);
  font-size: 19px;
  font-weight: 600;
  min-height: 54px;
}

.key:active { background: var(--accent-soft); color: var(--accent); }
.key-op { color: var(--accent); font-size: 24px; }

/* Backspace is an icon, not a glyph, and sized to match the operators — as a
   text character it rendered thin and small enough to read as noise beside
   them. */
.key-quiet { color: var(--accent); display: flex; align-items: center; justify-content: center; }
.key-quiet svg { width: 27px; height: 27px; }

/* Tinted rather than filled. A solid accent bar reads as the primary action of
   the screen, and it is not — conversion is live, so equals only resolves a
   pending operation. It needs to be findable, not dominant. */
.key-equals {
  grid-column: 1 / -1;
  background: var(--accent-soft);
  color: var(--accent);
  min-height: 42px;
  font-size: 22px;
  font-weight: 600;
}

/* Accessory bar above the keys: names what is being edited and offers the way
   out. iOS numeric keyboards carry the same affordance for the same reason —
   a number pad has no return key to dismiss itself with. */
.keypad-bar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 4px 2px;
}

.keypad-hint {
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ------------------------------------------------------------------ tabs */

.tabs {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface);
  border-top: 1px solid var(--separator);
  padding-bottom: max(env(safe-area-inset-bottom), 0px);
}

.tab {
  appearance: none;
  border: 0;
  background: none;
  color: var(--text-secondary);
  font: inherit;
  /* Text-only tabs, so HIG's 10pt icon-tab label does not apply — these carry
     the whole affordance and were too faint to read as controls. */
  font-size: 20px;
  font-weight: 600;
  min-height: 52px;
  cursor: pointer;
}

.tab.is-on { color: var(--accent); font-weight: 700; }

/* ----------------------------------------------------------------- chart */

.chart-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 4px 16px 12px;
}

/* Takes all the room the other elements do not. The chart is the point of this
   screen; at a fixed height it left the lower third of the phone empty. */
.chart-wrap {
  flex: 1;
  min-height: 170px;
  /* Capped only high enough to stop the plot becoming taller than it is wide on
     a tablet, where that would exaggerate volatility. On a phone the chart is
     the shorter constraint, so this never engages and no gap appears. */
  max-height: 460px;
  background: var(--surface);
  border-radius: 12px;
  padding: 4px 0;
  margin-top: 2px;
}

/* Title 2. */
.chart-title {
  margin: 8px 0 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.chart-readout {
  margin: 2px 0 8px;
  display: flex;
  align-items: baseline;
  gap: 10px;
}

/* Large Title, matching the app title's weight class: this is the headline
   number of the screen. */
.chart-rate {
  font-size: 34px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.025em;
}

/* Deliberately uncoloured. A rising rate is good for one traveller and bad for
   another, so red and green would assert a valence the data does not carry. */
.chart-change {
  font-size: 15px;
  font-weight: 500;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

.chart {
  display: block;
  width: 100%;
  height: 100%;
}

.chart-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.chart-area { fill: var(--accent); opacity: 0.1; stroke: none; }
.chart-last, .chart-point { fill: var(--accent); }

.chart-axis {
  fill: var(--text-secondary);
  font-size: 12px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
}

.chart-empty { fill: var(--text-secondary); font-size: 15px; font-family: inherit; }

.chart-note {
  margin: 8px 2px 0;
  font-size: 13px;
  color: var(--text-secondary);
}

/* A segmented control, as iOS uses for exactly this job. */
/* Segmented control. Its track uses the lighter iOS fill rather than the
   separator colour, which rendered as a heavy grey slab. */
.range-row {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 2px;
  /* auto pushes the controls to the bottom of the view, so any space the capped
     chart does not use reads as grouping rather than as a gap. */
  margin: 14px 0 0;
  margin-top: auto;
  padding: 2px;
  background: var(--fill);
  border-radius: 9px;
}

.range {
  appearance: none;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  min-height: 32px;
  border-radius: 7px;
  cursor: pointer;
}

.range.is-on {
  background: var(--surface);
  color: var(--accent);
  font-weight: 600;
}

.selector-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 12px 0 0;
}

.selector {
  appearance: none;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  min-height: 52px;
  cursor: pointer;
}

.selector-code { font-size: 17px; font-weight: 600; }
.selector:active { background: var(--press); }
.swap { color: var(--accent); }

/* --------------------------------------------------------------- settings */

.sheet {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  z-index: 10;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: max(env(safe-area-inset-top), 12px) 16px 10px;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
}

/* Title 1. */
.sheet-title {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0.007em;
}

.text-button {
  appearance: none;
  border: 0;
  background: none;
  color: var(--accent);
  font: inherit;
  font-size: 17px;
  font-weight: 600;
  min-height: 44px;
  padding: 0 2px;
  cursor: pointer;
}

.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 16px max(env(safe-area-inset-bottom), 24px);
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
}

/* Footnote, uppercase — the iOS grouped-list section header. */
.section-label {
  margin: 20px 4px 6px;
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-secondary);
}

.attribution { margin-top: 20px; }
.attribution a { color: var(--accent); }

.section-hint {
  margin: 8px 4px 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.search {
  appearance: none;
  width: 100%;
  margin: 0 0 10px;
  padding: 11px 14px;
  border: 0;
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  /* Must stay at or above 17px: iOS zooms the page when a field below 16px
     receives focus, which throws the whole layout off mid-search. */
  font-size: 17px;
}

.search:focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.edit-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: 12px;
  overflow: hidden;
}

.edit-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 74px;
  padding: 10px 16px;
}

.edit-item + .edit-item::before {
  content: "";
  position: absolute;
  top: 0;
  left: 66px;
  right: 0;
  height: 1px;
  background: var(--separator);
}

.edit-list .edit-item.is-addable { cursor: pointer; }
.edit-item.is-addable:active { background: var(--press); }

/* Dragging is restricted to the handle so a vertical swipe still scrolls the
   list. The target is a full 44pt square — the previous one was about 25x37,
   which is why it was hard to catch — and it is inset from the card edge so it
   does not sit in iOS's back-swipe zone. */
.handle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  margin-left: -12px;
  color: var(--text-tertiary);
  cursor: grab;
  touch-action: none;
  border-radius: 10px;
}

.handle svg { width: 20px; height: 20px; }
.handle:active { background: var(--fill); cursor: grabbing; }

/* Lifted while dragging, so it is obvious which row is being carried and that
   the gesture took hold at all. */
.edit-item.is-dragging {
  background: var(--surface);
  position: relative;
  z-index: 2;
  cursor: grabbing;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18);
  border-radius: 10px;
}

.edit-item.is-dragging .handle { color: var(--accent); }

.edit-labels {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.edit-name {
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.edit-code {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Selects the base currency. A dot, not a labelled pill — the word would repeat
   down every row and shout louder than the currencies themselves. */
.base-toggle {
  appearance: none;
  width: 22px;
  height: 22px;
  flex: 0 0 auto;
  padding: 0;
  border: 1.5px solid var(--text-tertiary);
  border-radius: 50%;
  background: none;
  cursor: pointer;
  position: relative;
}

/* Expands the touch target to 44pt without moving neighbours. */
.base-toggle::after {
  content: "";
  position: absolute;
  inset: -11px;
}

.base-toggle.is-on {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: inset 0 0 0 4px var(--surface);
}

.remove, .add-mark {
  appearance: none;
  border: 0;
  background: none;
  color: var(--text-tertiary);
  font: inherit;
  font-size: 19px;
  min-width: 44px;
  min-height: 44px;
  margin-right: -10px;
  cursor: pointer;
  line-height: 1;
}

.add-mark { color: var(--accent); font-size: 22px; }

.edit-item.is-unavailable .edit-code,
.edit-item.is-unavailable .edit-name { color: var(--text-tertiary); }


.edit-item.is-selected .edit-code { color: var(--accent); }

.edit-empty {
  padding: 16px;
  font-size: 15px;
  color: var(--text-secondary);
}

/* ---------------------------------------------------- desktop breakpoint */

/* Not a stretched phone layout: board and keypad sit side by side, so the
   keypad stops occupying half a tall window for no reason. */
@media (min-width: 900px) {
  .app {
    max-width: 940px;
    display: grid;
    grid-template-columns: 1fr 320px;
    grid-template-rows: auto 1fr auto;
    grid-template-areas:
      "header header"
      "board  keypad"
      "tabs   tabs";
    column-gap: 32px;
    padding: 8px 28px 28px;
  }

  .header { grid-area: header; padding-left: 0; padding-right: 0; }
  /* Board and chart share a slot — only one is ever visible, and giving them
     the same area keeps the keypad column from reflowing between tabs. */
  .board-scroll { grid-area: board; padding: 8px 0 0; }
  .chart-view { grid-area: board; padding: 8px 0 0; }
  .keypad {
    grid-area: keypad;
    align-self: start;
    padding: 8px 0 0;
  }
  .tabs {
    grid-area: tabs;
    max-width: 360px;
    background: none;
    border-top: 0;
  }
}

/* Respect a reduced-motion preference. */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
