/* ==========================================================================
   OpusMod MiniApp — Apple / iOS design language.
   Tokens follow Apple HIG system colors. Light palette on :root; dark palette
   redefined under prefers-color-scheme AND an explicit [data-theme] the JS sets
   from Telegram's colorScheme, so both switching mechanisms agree.
   ========================================================================== */

:root {
  /* Surfaces */
  --bg: #f2f2f7;              /* systemGroupedBackground */
  --surface: #ffffff;        /* secondarySystemGroupedBackground (cards) */
  --fill: rgba(118, 118, 128, 0.12); /* input fill */
  --fill-press: rgba(118, 118, 128, 0.2);

  /* Labels. --label-2 is nudged from Apple's 0.6 to 0.72 so small secondary
     text clears WCAG AA 4.5:1 on white; visually indistinguishable from iOS. */
  --label: #000000;
  --label-2: rgba(60, 60, 67, 0.72);
  --label-3: rgba(60, 60, 67, 0.3);

  /* System colors. Blue is split by role: --accent is systemBlue for
     decorative/non-text use (focus ring, glow, icon gradient — 3:1 floor);
     --accent-fill and --link are AA-compliant (>=4.5:1) for button fills and
     text, since raw systemBlue misses 4.5:1 as text. */
  --accent: #007aff;         /* systemBlue — decorative / focus only */
  --accent-fill: #0068d6;    /* filled-button bg: white text = 5.3:1 */
  --accent-fill-press: #0059b8;
  --link: #0067db;           /* blue text/links = 5.3:1 on white */
  --green: #34c759;          /* systemGreen */
  --red: #ff3b30;            /* systemRed */
  --red-press: #d70015;
  --on-accent: #ffffff;

  --separator: rgba(60, 60, 67, 0.29);
  --focus: var(--accent);

  /* Shape */
  --r-card: 20px;
  --r-btn: 14px;
  --r-field: 12px;

  /* Motion */
  --ease: cubic-bezier(0.32, 0.72, 0, 1); /* iOS-like */
  --dur: 240ms;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #000000;
    --surface: #1c1c1e;
    --fill: rgba(118, 118, 128, 0.24);
    --fill-press: rgba(118, 118, 128, 0.36);
    --label: #ffffff;
    --label-2: rgba(235, 235, 245, 0.6);
    --label-3: rgba(235, 235, 245, 0.3);
    --accent: #0a84ff;
    --accent-fill: #0068d6;
    --accent-fill-press: #0059b8;
    --link: #0a84ff;         /* 4.66:1 on #1c1c1e */
    --green: #30d158;
    --red: #ff453a;
    --red-press: #ff6961;
    --separator: rgba(84, 84, 88, 0.6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #000000;
  --surface: #1c1c1e;
  --fill: rgba(118, 118, 128, 0.24);
  --fill-press: rgba(118, 118, 128, 0.36);
  --label: #ffffff;
  --label-2: rgba(235, 235, 245, 0.6);
  --label-3: rgba(235, 235, 245, 0.3);
  --accent: #0a84ff;
  --accent-fill: #0068d6;
  --accent-fill-press: #0059b8;
  --link: #0a84ff;
  --green: #30d158;
  --red: #ff453a;
  --red-press: #ff6961;
  --separator: rgba(84, 84, 88, 0.6);
  color-scheme: dark;
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection { background: color-mix(in oklab, var(--accent) 24%, transparent); }

/* ---- layout shell -------------------------------------------------------- */

.app {
  max-width: 30rem;
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 20px
           calc(28px + env(safe-area-inset-bottom));
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ---- brand --------------------------------------------------------------- */

.brand {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.app-icon {
  width: 64px;
  height: 64px;
  border-radius: 15px; /* iOS app-icon squircle proportion */
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(180deg, #3a9bff 0%, var(--accent) 100%);
  box-shadow: 0 8px 20px -6px color-mix(in oklab, var(--accent) 55%, transparent),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
}
.app-name {
  margin: 4px 0 0;
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.4px;
}
.app-tagline { margin: 0; font-size: 14px; color: var(--label-2); }

/* ---- typography roles ---------------------------------------------------- */

.large-title {
  margin: 4px 2px 0;
  font-size: 30px;
  font-weight: 700;
  letter-spacing: -0.5px;
  line-height: 1.12;
  text-wrap: balance;
}
.subhead {
  margin: 0 2px;
  font-size: 15px;
  color: var(--label-2);
  text-wrap: pretty;
}
.footnote { margin: 0; font-size: 15px; color: var(--label-2); }
.caption {
  margin: 2px 4px 0;
  font-size: 13px;
  line-height: 1.4;
  color: var(--label-2);
  text-wrap: pretty;
}

/* ---- views --------------------------------------------------------------- */

.view {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
@keyframes slide-fwd { from { opacity: 0; transform: translateX(30px); } to { opacity: 1; transform: none; } }
@keyframes slide-back { from { opacity: 0; transform: translateX(-30px); } to { opacity: 1; transform: none; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: no-preference) {
  .view.anim-fwd:not([hidden]) { animation: slide-fwd 360ms var(--ease) both; }
  .view.anim-back:not([hidden]) { animation: slide-back 360ms var(--ease) both; }
}
@media (prefers-reduced-motion: reduce) {
  .view.anim-fwd:not([hidden]), .view.anim-back:not([hidden]) { animation: fade-in 180ms ease both; }
}

/* ---- cards / grouped surfaces ------------------------------------------- */

.card {
  background: var(--surface);
  border-radius: var(--r-card);
  padding: 16px;
}
.card.center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 32px 16px;
}

/* Grouped list: rows separated by inset hairlines, not gaps */
.card.list { padding: 0; overflow: hidden; }

/* ---- form fields --------------------------------------------------------- */

.field { display: flex; flex-direction: column; gap: 6px; }
.field label {
  font-size: 13px;
  font-weight: 500;
  color: var(--label-2);
  padding-inline-start: 2px;
}
input {
  width: 100%;
  min-height: 48px;
  padding: 12px 14px;
  border: none;
  border-radius: var(--r-field);
  background: var(--fill);
  color: var(--label);
  font: inherit;
  font-size: 17px; /* >=16px: no iOS zoom-on-focus */
  outline: none;
  transition: box-shadow 160ms var(--ease), background 160ms var(--ease);
}
input::placeholder { color: var(--label-3); }
input:focus-visible {
  background: var(--surface);
  box-shadow: 0 0 0 2px var(--focus);
}

/* ---- buttons ------------------------------------------------------------- */

.btn {
  position: relative;
  width: 100%;
  min-height: 50px;
  padding: 13px 18px;
  border: none;
  border-radius: var(--r-btn);
  font: inherit;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  transition: transform 120ms var(--ease), background 160ms var(--ease), opacity 160ms var(--ease);
}
.btn-filled { background: var(--accent-fill); color: var(--on-accent); }
.btn-plain { background: transparent; }
.btn-danger { color: var(--red); }

@media (hover: hover) {
  .btn-filled:hover { background: var(--accent-fill-press); }
  .btn-danger:hover { background: color-mix(in oklab, var(--red) 10%, transparent); }
}
.btn-filled:active { background: var(--accent-fill-press); transform: scale(0.96); }
.btn-plain:active { transform: scale(0.96); }
.btn-danger:active { background: color-mix(in oklab, var(--red) 14%, transparent); transform: scale(0.96); }

.btn:disabled { opacity: 0.55; cursor: default; }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--focus); }

/* loading: keep the label in the DOM for screen readers, hide it visually,
   show a spinner. aria-busy is set on the button in JS. */
.btn.is-loading .btn-label { visibility: hidden; }
.btn.is-loading::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 2.5px solid color-mix(in oklab, currentColor 30%, transparent);
  border-top-color: currentColor;
  animation: spin 0.7s linear infinite;
}
.btn-filled.is-loading::after { color: var(--on-accent); }

/* ---- rows (grouped list items) ------------------------------------------ */

.row {
  width: 100%;
  min-height: 50px;
  padding: 13px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  color: var(--label);
  border: none;
  font: inherit;
  font-size: 17px;
  text-align: start;
}
.row + .row { box-shadow: inset 0 0.5px 0 var(--separator); }
.row-action { cursor: pointer; -webkit-tap-highlight-color: transparent; }
@media (hover: hover) { .row-action:hover { background: var(--fill); } }
.row-action:active { background: var(--fill-press); }
.row-action:focus-visible { outline: none; box-shadow: inset 0 0 0 2px var(--focus); }
.row-title { font-size: 17px; }
.chevron { color: var(--label-3); flex: 0 0 auto; }

.row-status { justify-content: space-between; }
.status-pill { display: inline-flex; align-items: center; gap: 8px; color: var(--label-2); font-size: 16px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--label-3); flex: 0 0 auto; }
.dot.on { background: var(--green); box-shadow: 0 0 0 4px color-mix(in oklab, var(--green) 22%, transparent); }
.dot.off { background: var(--red); }

/* ---- stats --------------------------------------------------------------- */

.stats { display: flex; gap: 10px; }
.stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 16px 8px;
}
.stat-num {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.4px;
  font-variant-numeric: tabular-nums; /* no layout shift as counts change */
}
.stat-label { font-size: 12px; color: var(--label-2); }

/* ---- nav (chat filters back) -------------------------------------------- */

.nav { display: flex; align-items: center; min-height: 32px; margin-inline-start: -6px; }
.nav-back {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-height: 44px;
  padding: 0 6px;
  background: none;
  border: none;
  color: var(--link);
  font: inherit;
  font-size: 17px;
  cursor: pointer;
  border-radius: 10px;
}
.nav-back:active { opacity: 0.6; }
.nav-back:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--focus); }

/* ---- chat list items ----------------------------------------------------- */

#chats-list { max-height: 58vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-item {
  min-height: 52px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: var(--surface);
}
.chat-item + .chat-item { box-shadow: inset 0 0.5px 0 var(--separator); }
.chat-title {
  font-size: 17px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chats-empty { padding: 20px 16px; color: var(--label-2); font-size: 15px; }

/* iOS switch */
.switch { position: relative; width: 51px; height: 31px; flex: 0 0 auto; }
.switch input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  cursor: pointer;
}
.slider {
  position: absolute;
  inset: 0;
  background: var(--fill-press);
  border-radius: 31px;
  transition: background var(--dur) var(--ease);
  pointer-events: none;
}
.slider::before {
  content: "";
  position: absolute;
  width: 27px;
  height: 27px;
  left: 2px;
  top: 2px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.28);
  transition: transform var(--dur) var(--ease);
}
.switch input:checked + .slider { background: var(--green); }
.switch input:checked + .slider::before { transform: translateX(20px); }
.switch input:focus-visible + .slider { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--focus); }

/* ---- spinner / feedback -------------------------------------------------- */

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--fill-press);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error {
  margin: 0;
  padding: 12px 16px;
  border-radius: var(--r-field);
  background: color-mix(in oklab, var(--red) 12%, var(--surface));
  color: var(--red);
  font-size: 15px;
}

.foot {
  margin-top: auto;
  padding-top: 8px;
  color: var(--label-2);
  font-size: 12px;
  line-height: 1.4;
  text-align: center;
  text-wrap: pretty;
}
.foot em { font-style: italic; color: var(--label-2); }

[hidden] { display: none !important; }

/* ---- number display (keypad-driven field) ------------------------------- */

.display {
  width: 100%;
  min-height: 62px;
  padding: 14px;
  text-align: center;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: 0.5px;
  font-variant-numeric: tabular-nums;
  border: none;
  border-radius: var(--r-field);
  background: var(--fill);
  color: var(--label);
  caret-color: transparent;
  cursor: default;
}
.display::placeholder { color: var(--label-3); font-weight: 400; letter-spacing: normal; }
.display-code { letter-spacing: 10px; padding-inline-start: 24px; }
.display:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--focus); }

/* ---- tinted secondary button (share number) ----------------------------- */

.btn-tinted {
  gap: 8px;
  background: color-mix(in oklab, var(--accent) 12%, transparent);
  color: var(--link);
}
@media (hover: hover) {
  .btn-tinted:hover { background: color-mix(in oklab, var(--accent) 18%, transparent); }
}
.btn-tinted:active { background: color-mix(in oklab, var(--accent) 22%, transparent); transform: scale(0.96); }

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

.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.key {
  min-height: 56px;
  border: none;
  border-radius: 14px;
  background: var(--surface);
  color: var(--label);
  font: inherit;
  font-size: 26px;
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background 120ms var(--ease), transform 100ms var(--ease);
}
@media (hover: hover) { .key:hover { background: var(--fill); } }
.key:active { background: var(--fill-press); transform: scale(0.94); }
.key:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--focus); }
.key-empty { background: transparent; pointer-events: none; }
.key-del { color: var(--label-2); }

/* ---- ghost loaders (skeletons) ------------------------------------------ */

.sk {
  background: var(--fill);
  border-radius: 8px;
  position: relative;
  overflow: hidden;
}
@media (prefers-reduced-motion: no-preference) {
  .sk::after {
    content: "";
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(90deg, transparent,
      color-mix(in oklab, var(--label) 9%, transparent), transparent);
    animation: shimmer 1.4s ease-in-out infinite;
  }
}
@keyframes shimmer { to { transform: translateX(100%); } }

.skeleton-card { display: flex; align-items: center; justify-content: space-between; }
.sk-line { height: 16px; }
.sk-pill { width: 64px; height: 22px; border-radius: 11px; }
.sk-stat { align-items: center; gap: 10px; padding: 18px 8px; }
.sk-num { width: 34px; height: 26px; }
.sk-cap { width: 46px; height: 11px; }
.sk-row {
  min-height: 52px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
}
.sk-row + .sk-row { box-shadow: inset 0 0.5px 0 var(--separator); }
.sk-chev { width: 18px; height: 18px; border-radius: 5px; }
.sk-switch { width: 51px; height: 31px; border-radius: 31px; }

/* ---- first-setup option rows -------------------------------------------- */

.opt-row {
  min-height: 60px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: var(--surface);
}
.opt-row + .opt-row { box-shadow: inset 0 0.5px 0 var(--separator); }
.opt-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.opt-title { font-size: 17px; }
.opt-sub { font-size: 13px; color: var(--label-2); }

/* ---- wipe progress overlay ---------------------------------------------- */

.wipe-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: color-mix(in oklab, var(--bg) 55%, transparent);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
}
.wipe-box {
  width: 100%;
  max-width: 320px;
  background: var(--surface);
  border-radius: var(--r-card);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
  box-shadow: 0 24px 60px -16px rgba(0, 0, 0, 0.45);
}
.wipe-title { font-size: 18px; font-weight: 600; }
.wipe-bar { width: 100%; height: 8px; border-radius: 4px; background: var(--fill); overflow: hidden; }
.wipe-fill { height: 100%; width: 0; background: var(--red); border-radius: 4px; transition: width 0.9s var(--ease); }
.wipe-status { font-size: 13px; color: var(--label-2); }

/* Blur the app while wiping (belt-and-suspenders with the overlay's backdrop). */
body.wiping .app { filter: blur(6px); pointer-events: none; user-select: none; }

/* Respect reduced motion: kill non-essential motion; the loading spinner and
   skeleton shimmer (functional loading feedback) are handled separately above. */
@media (prefers-reduced-motion: reduce) {
  .btn:active, .btn-filled:active, .btn-plain:active,
  .btn-tinted:active, .key:active { transform: none; }
  .slider, .slider::before, input, .btn, .key, .wipe-fill { transition: none; }
}
