:root {
  --bg: #f4f5f7;
  --card: #fff;
  --line: #d9dde3;
  --text: #1a1d23;
  --muted: #667085;
  --primary: #1b5eff;
  --danger: #c62828;
  --ok: #1b7f3a;
  --pad: 12px;
}

* {
  box-sizing: border-box;
}

html,
body {
  max-width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

.loading-bar {
  position: fixed;
  top: env(safe-area-inset-top);
  left: 0;
  right: 0;
  height: 3px;
  z-index: 50;
  overflow: hidden;
  pointer-events: none;
  opacity: 0;
}

body.loading .loading-bar {
  opacity: 1;
}

.loading-bar::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 35%;
  background: var(--primary);
  box-shadow: 0 0 8px var(--primary);
  animation: load-run 1s ease-in-out infinite;
}

@keyframes load-run {
  0% {
    transform: translateX(-120%);
  }
  100% {
    transform: translateX(380%);
  }
}

body {
  margin: 0;
  font: 14px/1.4 system-ui, -apple-system, Segoe UI, sans-serif;
  color: var(--text);
  background: var(--bg);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
}

.top,
.tabs,
main {
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
}

.top {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px var(--pad);
  padding-top: calc(10px + env(safe-area-inset-top));
}

.top-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-width: 0;
}

.source {
  font-weight: 700;
  width: auto;
  max-width: 46vw;
  flex-shrink: 0;
}

[hidden] {
  display: none !important;
}

.token-row {
  display: flex;
  gap: 6px;
  min-width: 0;
}

.token-row input {
  flex: 1;
  min-width: 0;
  width: 100%;
}

.balance {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 13px;
}

.balance strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 42vw;
}

input,
select,
button,
textarea {
  font: inherit;
  font-size: 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fff;
  max-width: 100%;
}

button {
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  flex-shrink: 0;
}

button.primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

button.ghost {
  background: transparent;
}

.tabs {
  display: flex;
  gap: 6px;
  padding: 0 var(--pad) 10px;
}

.tab {
  flex: 1;
  background: transparent;
  padding: 8px 6px;
  text-align: center;
}

.tab.active {
  background: #fff;
  border-color: var(--text);
}

.panel {
  padding: 0 var(--pad) 20px;
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px;
  min-width: 0;
  overflow: hidden;
}

.full {
  grid-column: 1 / -1;
}

.presets {
  display: flex;
  flex-direction: column;
}

.preset {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

.preset:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.preset-info {
  flex: 1 1 100%;
  min-width: 0;
}

.preset-info strong,
.preset-info span {
  overflow-wrap: anywhere;
  word-break: break-word;
}

.preset-info strong {
  display: block;
}

.preset-info span {
  color: var(--muted);
  font-size: 12px;
}

.preset .quick-rent,
.preset .del-preset {
  flex: 1 1 auto;
}

button.danger {
  color: var(--danger);
  background: transparent;
}

h2 {
  margin: 0 0 10px;
  font-size: 16px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: end;
}

.row.wrap {
  flex-wrap: wrap;
}

label,
fieldset {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0 0 10px;
  min-width: 0;
}

.grow {
  flex: 1 1 140px;
}

fieldset {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
}

.chips label {
  flex-direction: row;
  align-items: center;
  margin: 0;
  font-size: 13px;
  white-space: nowrap;
}

.service-picker {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 0 0 10px;
  min-width: 0;
}

.service-picker > label {
  margin: 0;
}

#service {
  width: 100%;
  min-height: 160px;
  max-height: 36vh;
}

.muted {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.rent-scroll {
  max-height: 280px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 10px;
}

.rent-scroll th {
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 1;
}

.copy-pair {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  max-width: 100%;
}

.copy-value,
.copy-icon {
  border: 0;
  background: transparent;
  padding: 2px;
  color: inherit;
}

.copy-value {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  text-align: left;
}

.copy-value.ok {
  color: var(--ok);
  font-weight: 700;
}

.copy-icon {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--primary);
  flex-shrink: 0;
}

.copy-icon.ok {
  color: var(--ok);
}

.copy-icon svg {
  width: 16px;
  height: 16px;
  display: block;
}

.row-cancel {
  padding: 4px 8px;
  font-size: 12px;
}

.kv dt {
  color: var(--muted);
  font-size: 12px;
  margin-top: 6px;
}

.kv dd {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

code,
.otp {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  -webkit-user-select: text;
  user-select: text;
  overflow-wrap: anywhere;
  word-break: break-all;
  min-width: 0;
  flex: 1;
}

.otp {
  font-size: 20px;
}

.copy {
  padding: 6px 8px;
  font-size: 12px;
  flex-shrink: 0;
  touch-action: manipulation;
  -webkit-touch-callout: none;
}

.sms {
  background: #f7f8fa;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  white-space: pre-wrap;
  overflow: auto;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.msg {
  margin: 8px 0 0;
  overflow-wrap: anywhere;
}

.msg.ok {
  color: var(--ok);
}

.msg.err {
  color: var(--danger);
}

.topup {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-items: start;
  margin: 12px 0;
}

.qr-box {
  text-align: center;
}

.qr-box img {
  display: block;
  width: min(200px, 70vw);
  height: auto;
  aspect-ratio: 1;
  margin: 0 auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.table-wrap {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  margin-top: 12px;
  max-width: 100%;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  text-align: left;
  border-bottom: 1px solid var(--line);
  padding: 7px 6px;
  white-space: nowrap;
}

#tab-history .row label {
  flex: 1 1 calc(50% - 8px);
  min-width: 0;
}

#tab-history .row .primary,
#rent-btn,
#save-preset,
#poll-btn,
#auto-poll,
#topup-refresh,
#refresh-balance-2 {
  width: 100%;
}

@media (min-width: 801px) {
  input,
  select,
  button,
  textarea {
    font-size: 14px;
  }

  .grid {
    grid-template-columns: 1fr 1fr;
  }

  .preset-info {
    flex: 1 1 auto;
  }

  .preset .quick-rent,
  .preset .del-preset {
    flex: 0 0 auto;
  }

  .kv {
    grid-template-columns: 130px 1fr;
    gap: 6px 10px;
  }

  .kv dt {
    font-size: 14px;
    margin-top: 0;
  }

  .topup {
    grid-template-columns: 240px 1fr;
  }

  .qr-box img {
    width: 220px;
  }

  #tab-history .row label {
    flex: 0 1 auto;
  }

  #tab-history .row .primary,
  #rent-btn,
  #save-preset,
  #poll-btn,
  #auto-poll,
  #topup-refresh,
  #refresh-balance-2 {
    width: auto;
  }

  .tab {
    flex: 0 0 auto;
    padding: 7px 10px;
  }
}
