/* Colours come from the Telegram theme (set by telegram-web-app.js); fallbacks = dark theme.
   Translucent greys use plain rgba() so older Telegram webviews render them too. */
:root {
  --bg: var(--tg-theme-secondary-bg-color, #0f1115);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #181b22));
  --text: var(--tg-theme-text-color, #eef0f4);
  --hint: var(--tg-theme-hint-color, #8a93a3);
  --accent: var(--tg-theme-button-color, #3b82f6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #60a5fa);
  --danger: var(--tg-theme-destructive-text-color, #ef4444);
  --good: #22c55e;
  --field: rgba(128, 128, 128, 0.14);
  --line: rgba(128, 128, 128, 0.28);
  --radius: 14px;
  /* Telegram fullscreen: device insets (notch, status bar) + the band under Telegram's own
     close/back and menu buttons. Both are 0 in the normal (non-fullscreen) view. */
  --safe-top: calc(max(env(safe-area-inset-top), var(--tg-safe-area-inset-top, 0px)) + var(--tg-content-safe-area-inset-top, 0px));
  --safe-bottom: calc(
    max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px)) + var(--tg-content-safe-area-inset-bottom, 0px)
  );
  --tabbar-h: 60px;
}

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

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) 12px calc(var(--tabbar-h) + var(--safe-bottom) + 76px);
}

.top-shield {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--safe-top);
  background: var(--bg);
  z-index: 25;
}

h1 {
  font-size: 22px;
  margin: 0;
}

h2 {
  font-size: 16px;
  margin: 0;
}

.hint {
  color: var(--hint);
  font-size: 13px;
  margin: 0;
}

.hidden {
  display: none !important;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ── header ── */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px 12px;
}

.badge {
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--field);
  color: var(--hint);
}

.badge.live {
  background: rgba(239, 68, 68, 0.16);
  color: var(--danger);
}

/* ── cards ── */

.card {
  display: block;
  width: 100%;
  background: var(--card);
  color: var(--text);
  border-radius: var(--radius);
  padding: 14px;
  margin: 0 0 12px;
  text-align: left;
  font: inherit;
}

/* .card sets display:block — cards that are also .stack must stay flex, or their gap is ignored */
.card.stack {
  display: flex;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.row-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 0;
  cursor: pointer;
}

.row-card span:first-child {
  display: flex;
  flex-direction: column;
}

.hero {
  display: flex;
  flex-direction: column;
  gap: 2px;
  border: 0;
  cursor: pointer;
  padding: 16px;
}

.hero-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.hero-usd {
  font-size: 36px;
  font-weight: 800;
  letter-spacing: -0.5px;
  line-height: 1.15;
}

.hero-pnl {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.hero-pnl b {
  font-size: 22px;
  font-weight: 700;
}

.chevron {
  color: var(--hint);
  font-size: 26px;
  line-height: 1;
}

.big {
  font-size: 24px;
  margin: 2px 0 0;
}

.tiles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.tile {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.tile small {
  color: var(--hint);
  font-size: 12px;
}

.tile b {
  font-size: 16px;
}

.disclaimer {
  color: var(--hint);
  font-size: 12px;
  text-align: center;
  padding: 4px 12px;
}

/* ── form controls ── */

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field > span {
  font-weight: 600;
  font-size: 14px;
}

.pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  align-items: end;
}

.pair label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

select,
input[type="number"],
input:not([type]) {
  width: 100%;
  min-width: 0;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--text);
  font-size: 16px; /* ≥16px stops iOS from zooming on focus */
  appearance: none;
}

select {
  padding-right: 34px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

/* more room between the groups of the strategy form */
#settings-form .card.stack {
  gap: 18px;
}

.stack > button:not(.link) {
  width: 100%;
}

.pair .check {
  flex-direction: row;
  align-items: center;
  align-self: center;
}

input:focus,
select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

button {
  border: 0;
  border-radius: 10px;
  padding: 11px 14px;
  font: inherit;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
}

/* loading state: spinner + text inside a busy button */
button.busy {
  opacity: 0.85;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex: none;
}

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

.center {
  text-align: center;
  margin: 0;
}

.done-icon {
  width: 56px;
  height: 56px;
  margin: 4px auto 0;
  border-radius: 50%;
  background: var(--good);
  color: #fff;
  font-size: 30px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

.big-amount {
  font-size: 26px;
  font-weight: 800;
}

button.secondary {
  background: var(--field);
  color: var(--text);
}

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

button.link {
  background: none;
  color: var(--link);
  padding: 4px 0;
  font-weight: 500;
  text-align: left;
}

button.small {
  padding: 8px 10px;
  font-size: 14px;
}

.address {
  width: 100%;
  margin-top: 4px;
  background: var(--field);
  color: var(--text);
  font: 13px ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
  text-align: left;
  font-weight: 400;
}

.check {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 4px 0;
}

.check input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex: none;
}

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

.switch-row small {
  display: block;
  margin-top: 2px;
}

.switch {
  appearance: none;
  width: 52px;
  height: 32px;
  border-radius: 999px;
  background: var(--field);
  border: 1px solid var(--line);
  position: relative;
  flex: none;
  cursor: pointer;
  transition: background 0.15s;
}

.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}

.switch:checked {
  background: var(--good);
}

.switch:checked::after {
  transform: translateX(20px);
}

.segmented {
  display: flex;
  background: var(--field);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
  margin-bottom: 12px;
}

.field .segmented {
  margin-bottom: 0;
}

.segmented button {
  flex: 1;
  background: none;
  color: var(--text);
  padding: 9px 8px;
  font-weight: 500;
  border-radius: 8px;
}

.segmented button.on {
  background: var(--card);
  font-weight: 700;
}

.chips {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chips button {
  background: var(--field);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 999px;
  font-weight: 500;
  font-size: 14px;
}

.chips button.on {
  background: var(--accent);
  color: var(--accent-text);
}

.level {
  display: grid;
  grid-template-columns: 1fr 1fr 36px;
  gap: 8px;
  align-items: end;
}

.level label {
  display: flex;
  flex-direction: column;
  font-size: 12px;
  color: var(--hint);
  gap: 4px;
  min-width: 0;
}

.remove {
  background: none;
  color: var(--danger);
  font-size: 22px;
  padding: 8px 0;
}

details summary {
  cursor: pointer;
  font-weight: 600;
  list-style: none;
}

details summary::after {
  content: " ›";
  color: var(--hint);
}

details[open] summary {
  margin-bottom: 12px;
}

/* ── trades ── */

.position {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.position header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.position .meta {
  color: var(--hint);
  font-size: 12px;
  margin: 3px 0 6px;
}

.position .actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.position .links {
  display: flex;
  gap: 16px;
  margin-top: 6px;
}

.mini {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}

.mini:last-child {
  border-bottom: 0;
}

.tx {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  width: 100%;
  background: none;
  color: var(--text);
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  font-weight: 400;
  text-align: left;
}

.tx:last-child {
  border-bottom: 0;
}

.tx > span {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.tx > span:last-child {
  align-items: flex-end;
  flex: none;
}

.up {
  color: var(--good);
}

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

.tag {
  font-size: 11px;
  color: var(--hint);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 1px 6px;
  margin-left: 6px;
  vertical-align: 2px;
}

.empty {
  color: var(--hint);
  text-align: center;
  padding: 20px 0;
  margin: 0;
}

/* ── messages ── */

.alert {
  background: rgba(239, 68, 68, 0.14);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}

.warn {
  color: var(--danger);
  font-size: 13px;
  margin: 8px 0 0;
}

code#secret {
  display: block;
  word-break: break-all;
  background: var(--field);
  padding: 10px;
  border-radius: 10px;
  font-size: 12px;
}

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 14px);
  background: rgba(20, 20, 20, 0.92);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 30;
  max-width: calc(100% - 32px);
  text-align: center;
}

.toast.error {
  background: var(--danger);
}

/* ── fixed bottom: save bar + tab bar ── */

.savebar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--card);
  border-top: 1px solid var(--line);
  z-index: 20;
}

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--card);
  border-top: 1px solid var(--line);
  z-index: 20;
}

.tabbar button {
  background: none;
  color: var(--hint);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 0;
  font-size: 11px;
  font-weight: 500;
  border-radius: 0;
  position: relative;
}

.tabbar svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.tabbar svg circle {
  fill: var(--card);
}

.tabbar button.active {
  color: var(--accent);
}

/* dot on "Strategiya" while there are unsaved changes */
.tabbar button.dirty::after {
  content: "";
  position: absolute;
  top: 8px;
  left: calc(50% + 10px);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
}
