/* ═══════════════════════════════════════════════════════════════════════
   TOTP Authenticator v0.2 — Telegram Web App
   Dark theme, mobile-first, passphrase + cloud sync
   ═══════════════════════════════════════════════════════════════════════ */

:root {
  --bg:           #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-card:      #1e1e36;
  --bg-card-hover:#26264a;
  --bg-elevated:  #2a2a45;
  --border:       #2e2e50;
  --text:         #e8e8f0;
  --text-secondary:#a0a0b8;
  --hint:         #6b6b80;
  --accent:       #6c5ce7;
  --accent-glow:  rgba(108, 92, 231, 0.3);
  --success:      #00cec9;
  --danger:       #e74c3c;
  --warning:      #fdcb6e;
  --radius:       14px;
  --radius-sm:    10px;
  --font:         -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono:    'SF Mono', 'Cascadia Code', 'JetBrains Mono', monospace;
  --shadow:       0 4px 24px rgba(0, 0, 0, 0.4);
  --transition:   0.2s cubic-bezier(0.4, 0, 0.2, 1);

  --tg-bg:           var(--tg-theme-bg-color, var(--bg));
  --tg-bg-secondary: var(--tg-theme-secondary-bg-color, var(--bg-secondary));
  --tg-text:         var(--tg-theme-text-color, var(--text));
  --tg-hint:         var(--tg-theme-hint-color, var(--hint));
  --tg-link:         var(--tg-theme-link-color, var(--accent));
  --tg-button:       var(--tg-theme-button-color, var(--accent));
  --tg-button-text:  var(--tg-theme-button-text-color, #fff);
}

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

html, body {
  height: 100%; overflow: hidden;
  font-family: var(--font);
  color: var(--tg-text);
  background: var(--tg-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body { display: flex; flex-direction: column; }

#app {
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  overflow: hidden; position: relative;
}

/* ── Fullscreen overlay ─────────────────────────────────────────────── */
.fullscreen {
  position: fixed; inset: 0;
  background: var(--tg-bg);
  z-index: 200;
  display: flex; align-items: center; justify-content: center;
}

/* ── Auth Container (passphrase screen) ─────────────────────────────── */
.auth-container {
  display: flex; flex-direction: column;
  align-items: center; gap: 16px;
  padding: 32px 24px;
  width: 100%; max-width: 360px;
}

.auth-icon { color: var(--accent); opacity: 0.8; margin-bottom: 4px; }
.auth-container h2 { font-size: 22px; font-weight: 700; text-align: center; }
.auth-container .hint-text { font-size: 13px; text-align: center; line-height: 1.5; }

.auth-form {
  width: 100%;
  display: flex; flex-direction: column; gap: 12px;
  margin-top: 8px;
}

.auth-input {
  width: 100%; padding: 14px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--tg-text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}
.auth-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
.auth-input::placeholder { color: var(--hint); opacity: 0.6; }

/* ── Toggle ─────────────────────────────────────────────────────────── */
.toggle-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: 8px 4px;
}
.toggle-row span {
  font-size: 13px; color: var(--tg-hint);
  line-height: 1.4; flex: 1;
}

.toggle-switch {
  position: relative; display: inline-block;
  width: 48px; height: 28px; flex-shrink: 0;
}
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; cursor: pointer;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--border);
  border-radius: 28px;
  transition: var(--transition);
}
.toggle-slider::before {
  content: ""; position: absolute;
  width: 22px; height: 22px;
  left: 3px; top: 3px;
  background: var(--tg-text);
  border-radius: 50%;
  transition: var(--transition);
}
.toggle-switch input:checked + .toggle-slider { background: var(--accent); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(20px); }

/* ── PIN Screen ─────────────────────────────────────────────────────── */
.pin-container {
  display: flex; flex-direction: column;
  align-items: center; gap: 20px;
  padding: 24px;
}
.pin-icon { color: var(--accent); opacity: 0.8; }
.pin-screen h2, #pin-screen h2 { font-size: 20px; font-weight: 600; }
#pin-screen .hint-text { font-size: 13px; text-align: center; }

.pin-dots { display: flex; gap: 12px; }
.pin-dots .dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--border);
  transition: var(--transition);
}
.pin-dots .dot.filled { background: var(--accent); box-shadow: 0 0 8px var(--accent-glow); }
.pin-dots .dot.error { background: var(--danger); }

.pin-pad {
  display: grid;
  grid-template-columns: repeat(3, 60px);
  gap: 10px; justify-content: center;
}
.pin-key {
  width: 60px; height: 60px;
  border-radius: 50%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--tg-text);
  font-size: 22px; font-weight: 500;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: var(--transition);
}
.pin-key:active { background: var(--accent); color: #fff; transform: scale(0.92); }
.pin-key.placeholder { visibility: hidden; }

/* ── Settings Screen ────────────────────────────────────────────────── */
.settings-container {
  width: 100%; max-width: 420px;
  padding: 0 16px 24px;
  overflow-y: auto;
  max-height: 100%;
}

.settings-header {
  display: flex; align-items: center;
  gap: 8px;
  padding: 12px 0 20px;
}
.settings-header h2 { flex: 1; font-size: 20px; font-weight: 700; }

.settings-section {
  margin-bottom: 24px;
}
.settings-section h3 {
  font-size: 11px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.5px;
  color: var(--hint);
  margin-bottom: 10px;
  padding: 0 4px;
}

.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 4px;
  border-bottom: 1px solid var(--border);
}
.settings-row span {
  font-size: 15px; color: var(--tg-text);
  flex: 1; margin-right: 12px;
}

.settings-btn {
  width: 100%; display: flex; align-items: center; justify-content: space-between;
  padding: 14px 4px;
  background: none; border: none; border-bottom: 1px solid var(--border);
  color: var(--tg-text);
  font-size: 15px; font-family: var(--font);
  cursor: pointer; transition: var(--transition);
}
.settings-btn:active { background: var(--bg-card); }
.settings-btn.danger { color: var(--danger); }

/* ── Header ─────────────────────────────────────────────────────────── */
.app-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  background: var(--tg-bg);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0; z-index: 10;
}
.header-icon { color: var(--accent); display: flex; }
.app-header h1 {
  flex: 1; font-size: 18px; font-weight: 700;
  color: var(--tg-text); letter-spacing: -0.3px;
}
.header-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: 8px;
  color: var(--tg-hint); cursor: pointer;
  transition: var(--transition);
}
.icon-btn:active { background: var(--bg-elevated); color: var(--tg-text); }

/* ── Main ───────────────────────────────────────────────────────────── */
#main-content {
  flex: 1; overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  height: 100%; text-align: center; gap: 12px;
}
.empty-icon { opacity: 0.4; margin-bottom: 8px; }
.empty-state h2 { font-size: 20px; font-weight: 600; color: var(--tg-text); }

.hint-text { color: var(--tg-hint); font-size: 14px; line-height: 1.5; }

/* ── Account Cards ──────────────────────────────────────────────────── */
.account-list { display: flex; flex-direction: column; gap: 10px; }

.account-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 14px 16px 0;
  cursor: pointer; transition: var(--transition);
  position: relative; overflow: hidden;
  border: 1px solid var(--border);
}
.account-card:active { background: var(--bg-card-hover); transform: scale(0.985); }

.account-card-header {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 10px;
}
.issuer-avatar {
  width: 40px; height: 40px;
  border-radius: 10px; background: var(--accent);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700; flex-shrink: 0;
}
.account-info { flex: 1; min-width: 0; }
.issuer-name {
  font-size: 15px; font-weight: 600; color: var(--tg-text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.account-label {
  font-size: 12px; color: var(--tg-hint);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.otp-code {
  font-family: var(--font-mono);
  font-size: 32px; font-weight: 700;
  letter-spacing: 6px;
  color: var(--accent);
  text-align: center;
  padding: 6px 0 4px;
  transition: color 0.2s;
}
.otp-code.pulse { animation: otpPulse 0.4s ease; }

@keyframes otpPulse {
  0%   { transform: scale(1); opacity: 1; }
  50%  { transform: scale(1.08); opacity: 0.7; }
  100% { transform: scale(1); opacity: 1; }
}

.countdown-bar {
  height: 3px; background: var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  margin: 10px -16px 0; overflow: hidden; position: relative;
}
.countdown-fill {
  height: 100%; background: var(--accent);
  transition: width 1s linear;
  border-radius: 0 2px 2px 0;
}
.countdown-fill.warning { background: var(--warning); }
.countdown-fill.danger { background: var(--danger); }

.account-card .delete-hint {
  position: absolute; right: 12px; top: 14px;
  opacity: 0; transition: opacity 0.3s;
  color: var(--danger); font-size: 20px;
}

/* ── FAB ────────────────────────────────────────────────────────────── */
.fab {
  position: fixed; bottom: 20px; right: 20px;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--accent); color: #fff;
  border: none;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px var(--accent-glow);
  cursor: pointer; z-index: 20; transition: var(--transition);
}
.fab:active { transform: scale(0.92); }

/* ── Modal ──────────────────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: flex-end;
}
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.5); backdrop-filter: blur(4px);
}
.modal-sheet {
  position: relative;
  width: 100%; max-height: 85vh;
  background: var(--tg-bg);
  border-radius: 20px 20px 0 0;
  padding: 8px 16px 24px;
  overflow-y: auto;
  animation: slideUp 0.3s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.modal-handle {
  width: 40px; height: 4px;
  background: var(--hint); border-radius: 2px;
  margin: 0 auto 16px; opacity: 0.4;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.modal-header h2 { font-size: 18px; font-weight: 700; }

.tab-bar { display: flex; gap: 8px; margin-bottom: 16px; }
.tab {
  flex: 1; padding: 10px;
  background: var(--bg-secondary);
  border: none; border-radius: var(--radius-sm);
  color: var(--tg-hint); font-size: 14px; font-weight: 500;
  cursor: pointer; transition: var(--transition);
}
.tab.active { background: var(--accent); color: #fff; }

.tab-content.hidden { display: none; }

.qr-reader {
  width: 100%; max-width: 300px;
  margin: 0 auto 12px;
  border-radius: var(--radius-sm); overflow: hidden;
  background: #000; aspect-ratio: 1;
}
.qr-reader video { width: 100%; height: 100%; object-fit: cover; }
#qr-reader:empty + .qr-placeholder { display: flex; }
#qr-reader:not(:empty) ~ .qr-placeholder { display: none; }

.qr-placeholder {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 32px 0;
}

.scan-result {
  text-align: center; padding: 12px;
  background: var(--bg-card); border-radius: var(--radius-sm);
  margin-top: 12px;
}
.scan-result .result-issuer { font-weight: 700; color: var(--success); }
.scan-result .result-label { color: var(--tg-hint); font-size: 13px; }

.field {
  display: flex; flex-direction: column; gap: 6px;
  margin-bottom: 14px;
}
.field span { font-size: 13px; color: var(--tg-hint); font-weight: 500; }
.field input {
  width: 100%; padding: 12px 14px;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--tg-text); font-size: 16px;
  font-family: var(--font-mono); outline: none;
  transition: border-color 0.2s;
}
.field input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.field input::placeholder { color: var(--hint); opacity: 0.6; }

.primary-btn {
  width: 100%; padding: 14px;
  background: var(--accent); color: #fff;
  font-size: 16px; font-weight: 600;
  border: none; border-radius: var(--radius-sm);
  cursor: pointer; transition: var(--transition);
  margin-top: 4px;
}
.primary-btn:active { opacity: 0.85; transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.5; cursor: default; }

.text-btn {
  background: none; border: none;
  color: var(--tg-hint); font-size: 14px;
  cursor: pointer; padding: 8px;
}
.full-width { width: 100%; }

.error-text {
  color: var(--danger); font-size: 13px; text-align: center;
}

/* ── Toast ──────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 90px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elevated); color: var(--tg-text);
  padding: 12px 24px; border-radius: 24px;
  font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow); z-index: 300;
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: none;
}
.toast.hidden { opacity: 0; transform: translateX(-50%) translateY(20px); }

.hidden { display: none !important; }

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.account-card { animation: fadeIn 0.3s ease both; }
.account-card:nth-child(1) { animation-delay: 0s; }
.account-card:nth-child(2) { animation-delay: 0.05s; }
.account-card:nth-child(3) { animation-delay: 0.1s; }
.account-card:nth-child(4) { animation-delay: 0.15s; }
.account-card:nth-child(5) { animation-delay: 0.2s; }
