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

:root {
  --bg: #f3f4f6;
  --card: #ffffff;
  --text: #15181f;
  --muted: #6b7280;
  --muted-2: #aab0bd;
  --accent: #5b58e6;
  --accent-press: #4a47d4;
  --accent-soft: #ecebfe;
  --profit: #0ca678;
  --profit-soft: #e5f6f0;
  --loss: #e5484d;
  --loss-soft: #fcebec;
  --border: #ebedf1;
  --field: #f5f6f8;
  --font: 'Manrope', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --shadow: 0 1px 3px rgba(20, 24, 40, 0.04), 0 8px 24px -12px rgba(20, 24, 40, 0.14);
}

html { -webkit-text-size-adjust: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 460px;
  margin: 0 auto;
  padding: 60px 14px calc(32px + env(safe-area-inset-bottom));
}

/* --- Закреплённая строка результата + кнопка «Готово» --- */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 60;
  background: rgba(243, 244, 246, 0.9);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 460px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 16px;
  min-height: 46px;
}
.topbar-info {
  display: flex;
  align-items: baseline;
  gap: 7px;
  min-width: 0;
  flex-wrap: wrap;
}
.topbar-label { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.topbar-profit {
  font-size: 17px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--muted-2);
}
.topbar-profit.profit { color: var(--profit); }
.topbar-profit.loss { color: var(--loss); }
.topbar-margin { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.topbar-done {
  flex: none;
  display: none;
  border: none;
  background: var(--accent);
  color: #fff;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 700;
  padding: 9px 20px;
  border-radius: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.topbar-done:active { background: var(--accent-press); }
body.kb-open .topbar-done { display: block; }

/* --- Шапка --- */
.head { padding: 4px 4px 14px; }
.head-title {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.head-sub {
  margin-top: 4px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

/* --- Карточка --- */
.panel {
  background: var(--card);
  border-radius: 18px;
  padding: 15px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  animation: rise 0.4s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(var(--i, 0) * 55ms);
}
@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.panel-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted-2);
  margin-bottom: 11px;
}

/* --- Поля ввода --- */
.fields { display: flex; flex-direction: column; gap: 10px; }

.field-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}
.field-label .per {
  color: var(--muted-2);
  font-weight: 500;
  font-size: 12px;
}

.field-input {
  display: flex;
  align-items: center;
  min-width: 0;
  background: var(--field);
  border: 1.5px solid transparent;
  border-radius: 13px;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.field-input:focus-within {
  border-color: var(--accent);
  background: #fff;
}
.field-input input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: var(--font);
  font-size: 17px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  padding: 12px 4px 12px 14px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
.field-input input::placeholder { color: var(--muted-2); font-weight: 500; }
.field-unit {
  flex: none;
  padding: 0 14px 0 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

/* --- Строка курса --- */
.rate-row { display: flex; gap: 9px; }
.rate-input { flex: 1; }
.btn-refresh {
  flex: none;
  width: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 13px;
  background: var(--accent-soft);
  color: var(--accent);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-refresh:active { background: #e0defb; }
.btn-refresh:disabled { opacity: 0.5; }
.btn-refresh.spin svg { animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.rate-info {
  font-size: 12px;
  color: var(--muted);
  margin-top: 9px;
  line-height: 1.45;
}
.rate-info.ok { color: var(--profit); }
.rate-info.err { color: var(--loss); }

/* --- Результат --- */
.result-hero {
  text-align: center;
  background: var(--field);
  border-radius: 14px;
  padding: 14px 12px;
  transition: background 0.2s ease;
}
.result-hero .panel-label { margin-bottom: 2px; }
.result-amount {
  display: block;
  font-weight: 800;
  font-size: clamp(2rem, 8.6vw, 2.55rem);
  line-height: 1.12;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  word-break: break-word;
  color: var(--text);
  transition: color 0.2s ease;
}
.result-rub {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-top: 3px;
  min-height: 1em;
}
.result.is-profit .result-hero { background: var(--profit-soft); }
.result.is-profit .result-amount { color: var(--profit); }
.result.is-loss .result-hero { background: var(--loss-soft); }
.result.is-loss .result-amount { color: var(--loss); }
.result-amount.bump { animation: bump 0.28s ease; }
@keyframes bump {
  0% { transform: scale(1); }
  45% { transform: scale(1.045); }
  100% { transform: scale(1); }
}

.result-stats {
  display: flex;
  align-items: stretch;
  margin-top: 12px;
}
.stat { flex: 1; text-align: center; }
.stat-divider { width: 1px; background: var(--border); }
.stat-value {
  display: block;
  font-size: 21px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.stat-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted-2);
  margin-top: 1px;
}
.result.is-profit .stat-value { color: var(--profit); }
.result.is-loss .stat-value { color: var(--loss); }

.result-rows {
  margin-top: 6px;
  border-top: 1px solid var(--border);
  padding-top: 4px;
}
.rrow {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 14px;
  padding: 7px 2px;
}
.rrow dt { font-size: 13px; color: var(--muted); }
.rrow dd {
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.result-note {
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--loss);
  margin-top: 6px;
}
.result-note.hint { color: var(--muted); }
.result-note:empty { display: none; }

.btn-save {
  width: 100%;
  margin-top: 13px;
  padding: 14px;
  border: none;
  border-radius: 13px;
  background: var(--accent);
  color: #fff;
  font-family: var(--font);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, transform 0.08s ease;
}
.btn-save:active { transform: scale(0.99); background: var(--accent-press); }
.btn-save:disabled {
  background: var(--field);
  color: var(--muted-2);
  cursor: default;
}
.btn-save.saved { background: var(--profit); color: #fff; }

/* --- История --- */
.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2px;
}
.history-head .panel-label { margin-bottom: 0; }
.history-count { color: var(--accent); }
.btn-clear {
  border: none;
  background: none;
  color: var(--muted);
  font-family: var(--font);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 2px;
  -webkit-tap-highlight-color: transparent;
}
.btn-clear:active { color: var(--loss); }

.history-list { list-style: none; }
.history-empty {
  font-size: 13px;
  color: var(--muted-2);
  padding: 8px 2px 2px;
}

.hitem {
  display: flex;
  align-items: stretch;
  gap: 4px;
  border-top: 1px solid var(--border);
}
.hitem:first-child { border-top: none; }
.hitem-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  border: none;
  background: none;
  color: var(--text);
  font-family: var(--font);
  text-align: left;
  padding: 11px 2px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.hitem-main:active { opacity: 0.55; }
.hitem-top { display: flex; align-items: baseline; gap: 9px; }
.hitem-profit {
  font-size: 15px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.hitem-profit.profit { color: var(--profit); }
.hitem-profit.loss { color: var(--loss); }
.hitem-margin {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.hitem-meta { font-size: 11.5px; color: var(--muted-2); }
.hitem-del {
  flex: none;
  width: 38px;
  border: none;
  background: none;
  color: var(--muted-2);
  font-size: 15px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.hitem-del:active { color: var(--loss); }

/* --- Как считается --- */
.info {
  background: var(--card);
  border-radius: 18px;
  padding: 0 15px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  animation: rise 0.4s cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(var(--i, 0) * 55ms);
}
.info summary {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--accent);
  padding: 14px 0;
  cursor: pointer;
  list-style: none;
  -webkit-tap-highlight-color: transparent;
}
.info summary::-webkit-details-marker { display: none; }
.info summary::before { content: '+ '; font-weight: 700; }
.info[open] summary::before { content: '− '; }
.info-body { padding-bottom: 14px; }
.info-body p {
  font-size: 12.5px;
  color: var(--muted);
  margin: 7px 0;
  line-height: 1.5;
}
.info-body b { color: var(--text); font-weight: 700; }

.foot {
  text-align: center;
  font-size: 11.5px;
  color: var(--muted-2);
  padding: 4px 8px;
  line-height: 1.5;
}

@media (prefers-reduced-motion: reduce) {
  .panel, .info { animation: none; }
  .result-amount.bump { animation: none; }
  .btn-refresh.spin svg { animation-duration: 1.6s; }
}
