:root {
  --bg: #f2f3f5;
  --card: #ffffff;
  --card-2: #f6f7f9;
  --text: #0f1419;
  --muted: #6b7380;
  --faint: #a3aab5;
  --line: rgba(15, 20, 25, .08);
  --accent: #0f9d6e;
  --accent-soft: rgba(15, 157, 110, .10);
  --accent-text: #ffffff;
  --danger: #e5484d;
  --shadow: 0 1px 2px rgba(15, 20, 25, .05), 0 4px 14px rgba(15, 20, 25, .05);
  --sheet: #f7f8fa;
  --warn: #c2410c;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0e12;
    --card: #171a20;
    --card-2: #1e222a;
    --text: #f2f4f7;
    --muted: #8b93a1;
    --faint: #5b6270;
    --line: rgba(255, 255, 255, .07);
    --accent: #34d399;
    --accent-soft: rgba(52, 211, 153, .12);
    --accent-text: #06281c;
    --danger: #ff6369;
    --shadow: 0 1px 0 rgba(255, 255, 255, .03) inset;
    --sheet: #121419;
    --warn: #fb923c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }

.app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
  padding:
    0
    max(16px, env(safe-area-inset-right))
    calc(96px + env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

/* ---------- шапка ---------- */

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: calc(env(safe-area-inset-top) + 14px) 4px 10px;
  background: linear-gradient(var(--bg) 82%, transparent);
}

.top-row { display: flex; align-items: center; justify-content: space-between; }

h1 { margin: 0; font-size: 34px; line-height: 1.1; font-weight: 750; letter-spacing: -.02em; }

.pill {
  height: 34px;
  padding: 0 15px;
  border-radius: 17px;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 15px;
  font-weight: 600;
  color: var(--accent);
}
.pill.on { background: var(--accent); color: var(--accent-text); }

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  min-height: 30px;
  font-size: 13px;
  color: var(--muted);
}
.status.stale #status { color: var(--warn); }

.refresh {
  width: 30px;
  height: 30px;
  border-radius: 15px;
  font-size: 15px;
  line-height: 30px;
  text-align: center;
}
.refresh:active { background: var(--card); }
.refresh.spin { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- список ---------- */

.list { list-style: none; margin: 4px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 72px;
  padding: 10px 14px 10px 12px;
  background: var(--card);
  border-radius: 18px;
  box-shadow: var(--shadow);
  outline: 2px solid transparent;
  outline-offset: -2px;
  transition: outline-color .15s, background-color .15s;
}
.row.active { outline-color: var(--accent); background: linear-gradient(var(--accent-soft), var(--accent-soft)), var(--card); }

.flag {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 21px;
  background: var(--card-2);
  display: grid;
  place-items: center;
  font-size: 26px;
  line-height: 1;
}

.meta { flex: 1 1 0; min-width: 0; }
.code { font-size: 17px; font-weight: 700; letter-spacing: .01em; }
.name, .rate {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.name { margin-top: 1px; }

.value { flex: 1.35 1 0; min-width: 0; text-align: right; }
.rate { margin-top: -2px; font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums; }
.row.active .rate { color: var(--accent); opacity: .8; }

.amount {
  display: block;
  width: 100%;
  min-width: 0;
  height: 38px;
  padding: 0 2px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -.01em;
  text-align: right;
  font-variant-numeric: tabular-nums;
  text-overflow: ellipsis;
  -webkit-appearance: none;
  appearance: none;
}
.amount.small { font-size: 20px; }
.amount.tiny { font-size: 17px; }
.amount::placeholder { color: var(--faint); }
.row.active .amount { color: var(--accent); }

.remove, .handle { display: none; flex: none; }
.remove {
  width: 30px;
  height: 30px;
  border-radius: 15px;
  background: var(--danger);
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  line-height: 30px;
  text-align: center;
}
.handle {
  width: 40px;
  height: 48px;
  margin-right: -8px;
  color: var(--faint);
  font-size: 22px;
  line-height: 48px;
  text-align: center;
  touch-action: none;
  cursor: grab;
}

.editing .remove, .editing .handle { display: block; }
.editing .value { display: none; }
.editing .row { outline-color: transparent; background: var(--card); }
.row.dragging { z-index: 3; box-shadow: 0 10px 30px rgba(0, 0, 0, .18); transition: none; }
.row.removing { animation: out .22s ease-in forwards; }
@keyframes out { to { opacity: 0; transform: translateX(-30px); } }
.row.added { animation: in .35s ease-out; }
@keyframes in { from { opacity: 0; transform: translateY(8px); } }

.empty { text-align: center; color: var(--muted); padding: 60px 30px; }
.empty-icon { font-size: 48px; margin-bottom: 8px; }
.empty p { margin: 0; line-height: 1.45; }

/* ---------- нижняя кнопка ---------- */

.bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  padding: 18px max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 12px) max(16px, env(safe-area-inset-left));
  background: linear-gradient(transparent, var(--bg) 40%);
  pointer-events: none;
}
.add {
  pointer-events: auto;
  display: block;
  width: 100%;
  max-width: 528px;
  margin: 0 auto;
  height: 54px;
  border-radius: 27px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 17px;
  font-weight: 650;
  box-shadow: 0 6px 20px rgba(15, 157, 110, .28);
}
.add span { font-weight: 800; margin-right: 4px; }
.add:active, .pill:active { transform: scale(.98); }
.keyboard .bottom { display: none; }

/* ---------- шторка добавления ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: rgba(0, 0, 0, .35);
  opacity: 0;
  transition: opacity .25s;
}
.sheet-backdrop.show { opacity: 1; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 11;
  max-width: 560px;
  margin: 0 auto;
  height: calc(100dvh - env(safe-area-inset-top) - 24px);
  display: flex;
  flex-direction: column;
  background: var(--sheet);
  border-radius: 22px 22px 0 0;
  transform: translateY(105%);
  transition: transform .32s cubic-bezier(.2, .8, .2, 1);
  visibility: hidden;
}
.sheet.open { transform: none; visibility: visible; }

.sheet-head { padding: 8px 16px 10px; }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--faint); opacity: .5; margin: 0 auto 10px; }
.sheet-title { display: flex; align-items: center; justify-content: space-between; }
.sheet-title h2 { margin: 0; font-size: 22px; font-weight: 700; }

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  margin-top: 12px;
  padding: 0 10px 0 12px;
  border-radius: 12px;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 15px;
}
.search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 17px;
  -webkit-appearance: none;
}
.search input::-webkit-search-cancel-button { display: none; }
#searchClear { width: 24px; height: 24px; border-radius: 12px; background: var(--faint); color: var(--card); font-size: 12px; }

.sheet-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 20px);
}

.group-title {
  margin: 16px 4px 6px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
}
.group { background: var(--card); border-radius: 16px; overflow: hidden; box-shadow: var(--shadow); }

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 58px;
  padding: 8px 14px 8px 12px;
  text-align: left;
  border-top: 1px solid var(--line);
}
.item:first-child { border-top: 0; }
.item:active { background: var(--card-2); }
.item .flag { width: 38px; height: 38px; font-size: 24px; }
.item .meta .code { font-size: 16px; }
.item .meta .code small { font-weight: 500; color: var(--muted); margin-left: 6px; font-size: 14px; }
.check {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 13px;
  border: 2px solid var(--faint);
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 800;
  color: transparent;
}
.item.on .check { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.nothing { text-align: center; color: var(--muted); padding: 40px 10px; }

/* ---------- всплывашка ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  z-index: 20;
  max-width: calc(100% - 40px);
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(20, 22, 28, .92);
  color: #fff;
  font-size: 14px;
  text-align: center;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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